]> git.ipfire.org Git - thirdparty/libnl.git/commitdiff
Extend nl_time2int() and rename it to nl_str2msec()
authorThomas Graf <tgr@lsx.localdomain>
Mon, 16 Jun 2008 11:54:57 +0000 (13:54 +0200)
committerThomas Graf <tgr@lsx.localdomain>
Mon, 16 Jun 2008 11:54:57 +0000 (13:54 +0200)
Support parsing of more complex time duration input.

include/netlink/utils.h
lib/utils.c

index 2b6bcb7837cb119c9cad8addbe046a089428fb88..480bab61869c3889573422b9c5bfbb9537307cc7 100644 (file)
@@ -51,6 +51,7 @@ extern long   nl_prob2int(const char *);
 extern int     nl_get_hz(void);
 extern uint32_t        nl_us2ticks(uint32_t);
 extern uint32_t        nl_ticks2us(uint32_t);
+extern int     nl_str2msec(const char *, uint64_t *);
 extern char *  nl_msec2str(uint64_t, char *, size_t);
 
 /* link layer protocol translations */
index 3158bac0e5401483d02b22d226c096131509e1ce..ad254680a088282dbaa8c596fbb3b596c81f8291 100644 (file)
@@ -356,25 +356,40 @@ uint32_t nl_ticks2us(uint32_t ticks)
        return ticks / ticks_per_usec;
 }
 
-long nl_time2int(const char *str)
+int nl_str2msec(const char *str, uint64_t *result)
 {
+       uint64_t total = 0, l;
+       int plen;
        char *p;
-       long l = strtol(str, &p, 0);
-       if (p == str)
-               return -NLE_INVAL;
 
-       if (*p) {
-               if (!strcasecmp(p, "min") == 0 || !strcasecmp(p, "m"))
-                       l *= 60;
-               else if (!strcasecmp(p, "hour") || !strcasecmp(p, "h"))
-                       l *= 60*60;
-               else if (!strcasecmp(p, "day") || !strcasecmp(p, "d"))
-                       l *= 60*60*24;
-               else if (strcasecmp(p, "s") != 0)
+       do {
+               l = strtoul(str, &p, 0);
+               if (p == str)
                        return -NLE_INVAL;
-       }
+               else if (*p) {
+                       plen = strcspn(p, " \t");
+
+                       if (!plen)
+                               total += l;
+                       else if (!strncasecmp(p, "sec", plen))
+                               total += (l * 1000);
+                       else if (!strncasecmp(p, "min", plen))
+                               total += (l * 1000*60);
+                       else if (!strncasecmp(p, "hour", plen))
+                               total += (l * 1000*60*60);
+                       else if (!strncasecmp(p, "day", plen))
+                               total += (l * 1000*60*60*24);
+                       else
+                               return -NLE_INVAL;
+
+                       str = p + plen;
+               } else
+                       total += l;
+       } while (*str && *p);
+
+       *result = total;
 
-       return l;
+       return 0;
 }
 
 /**