gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r26290 - gnunet/src/util


From: gnunet
Subject: [GNUnet-SVN] r26290 - gnunet/src/util
Date: Mon, 4 Mar 2013 09:28:19 +0100

Author: LRN
Date: 2013-03-04 09:28:19 +0100 (Mon, 04 Mar 2013)
New Revision: 26290

Modified:
   gnunet/src/util/strings.c
   gnunet/src/util/test_strings.c
Log:
Accept time strings where number and unit are not separated by a space

Fixes #2806

Modified: gnunet/src/util/strings.c
===================================================================
--- gnunet/src/util/strings.c   2013-03-04 05:56:11 UTC (rev 26289)
+++ gnunet/src/util/strings.c   2013-03-04 08:28:19 UTC (rev 26290)
@@ -216,21 +216,33 @@
   in = GNUNET_strdup (input);
   for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
   {
-    i = 0;
-    while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
-      i++;
-    if (table[i].name != NULL)
-      last *= table[i].value;
-    else
+    do
     {
-      ret += last;
-      last = 0;
-      if (1 != SSCANF (tok, "%llu", &last))
+      i = 0;
+      while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
+        i++;
+      if (table[i].name != NULL)
       {
-        GNUNET_free (in);
-        return GNUNET_SYSERR;   /* expected number */
+        last *= table[i].value;
+        break; /* next tok */
       }
-    }
+      else
+      {
+        char *endptr;
+        ret += last;
+        errno = 0;
+        last = strtoull (tok, &endptr, 10);
+        if ((0 != errno) || (endptr == tok))
+        {
+          GNUNET_free (in);
+          return GNUNET_SYSERR;   /* expected number */
+        }
+        if ('\0' == endptr[0])
+          break; /* next tok */
+        else
+          tok = endptr; /* and re-check (handles times like "10s") */
+      }
+    } while (GNUNET_YES);
   }
   ret += last;
   *output = ret;

Modified: gnunet/src/util/test_strings.c
===================================================================
--- gnunet/src/util/test_strings.c      2013-03-04 05:56:11 UTC (rev 26289)
+++ gnunet/src/util/test_strings.c      2013-03-04 08:28:19 UTC (rev 26290)
@@ -39,6 +39,8 @@
   const char *bc;
   struct GNUNET_TIME_Absolute at;
   struct GNUNET_TIME_Absolute atx;
+  struct GNUNET_TIME_Relative rt;
+  struct GNUNET_TIME_Relative rtx;
   const char *hdir;
 
   GNUNET_log_setup ("test_strings", "ERROR", NULL);
@@ -109,6 +111,13 @@
   b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "unknown");
   GNUNET_log_skip (0, GNUNET_YES);
   WANT ("TEST", b);
+
+  GNUNET_assert (GNUNET_OK ==
+      GNUNET_STRINGS_fancy_time_to_relative ("15m", &rt));
+  GNUNET_assert (GNUNET_OK ==
+      GNUNET_STRINGS_fancy_time_to_relative ("15 m", &rtx));
+  GNUNET_assert (rt.rel_value == rtx.rel_value);
+
   return 0;
 }
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]