-/*
- * Convert a string duration to utime_t (64 bit seconds)
- * Returns 0: if error
- 1: if OK, and value stored in value
- */
-int duration_to_utime(char *str, utime_t *value)
-{
- int i, ch, len;
- double val;
- static int mod[] = {'*', 's', 'n', 'h', 'd', 'w', 'm', 'q', 'y', 0};
- static int mult[] = {1, 1, 60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30,
- 60*60*24*91, 60*60*24*365};
-
- /* Look for modifier */
- len = strlen(str);
- ch = str[len - 1];
- i = 0;
- if (B_ISALPHA(ch)) {
- if (B_ISUPPER(ch)) {
- ch = tolower(ch);
- }
- while (mod[++i] != 0) {
- if (ch == mod[i]) {
- len--;
- str[len] = 0; /* strip modifier */
- break;
- }
- }
- }
- if (mod[i] == 0 || !is_a_number(str)) {
- return 0;
- }
- val = strtod(str, NULL);
- if (errno != 0 || val < 0) {
- return 0;
- }
- *value = (utime_t)(val * mult[i]);
- return 1;
-
-}
-
-/*
- * Edit a utime "duration" into ASCII
- */
-char *edit_utime(utime_t val, char *buf)
-{
- char mybuf[30];
- static int mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
- static char *mod[] = {"year", "month", "day", "hour", "min"};
- int i;
- uint32_t times;
-
- *buf = 0;
- for (i=0; i<5; i++) {
- times = val / mult[i];
- if (times > 0) {
- val = val - (utime_t)times * mult[i];
- sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
- strcat(buf, mybuf);
- }
- }
- if (val == 0 && strlen(buf) == 0) {
- strcat(buf, "0 secs");
- } else if (val != 0) {
- sprintf(mybuf, "%d sec%s", (uint32_t)val, val>1?"s":"");
- strcat(buf, mybuf);
- }
- return buf;
-}
-
-/*
- * Check if specified string is a number or not.
- * Taken from SQLite, cool, thanks.
- */
-int is_a_number(const char *n)
-{
- int digit_seen = 0;
-
- if( *n == '-' || *n == '+' ) {
- n++;
- }
- while (B_ISDIGIT(*n)) {
- digit_seen = 1;
- n++;
- }
- if (digit_seen && *n == '.') {
- n++;
- while (B_ISDIGIT(*n)) { n++; }
- }
- if (digit_seen && (*n == 'e' || *n == 'E')
- && (B_ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && B_ISDIGIT(n[2])))) {
- n += 2; /* skip e- or e+ or e digit */
- while (B_ISDIGIT(*n)) { n++; }
- }
- return digit_seen && *n==0;
-}
-
-
-/*
- * Edit an integer number with commas, the supplied buffer
- * must be at least 27 bytes long. The incoming number
- * is always widened to 64 bits.
- */
-char *edit_uint64_with_commas(uint64_t val, char *buf)
-{
- sprintf(buf, "%" lld, val);
- return add_commas(buf, buf);
-}
-
-/*
- * Edit an integer number, the supplied buffer
- * must be at least 27 bytes long. The incoming number
- * is always widened to 64 bits.
- */
-char *edit_uint64(uint64_t val, char *buf)
-{
- sprintf(buf, "%" lld, val);
- return buf;
-}
-
-
-/*
- * Add commas to a string, which is presumably
- * a number.
- */
-char *add_commas(char *val, char *buf)
-{
- int len, nc;
- char *p, *q;
- int i;
-
- if (val != buf) {
- strcpy(buf, val);
- }
- len = strlen(buf);
- if (len < 1) {
- len = 1;
- }
- nc = (len - 1) / 3;
- p = buf+len;
- q = p + nc;
- *q-- = *p--;
- for ( ; nc; nc--) {
- for (i=0; i < 3; i++) {
- *q-- = *p--;
- }
- *q-- = ',';
- }
- return buf;
-}
-