]> mj.ucw.cz Git - libucw.git/commitdiff
xtypes: removal of strtoul from size/timestamp parser
authorRobert Kessl <kesslr@centrum.cz>
Mon, 28 Jul 2014 11:51:17 +0000 (13:51 +0200)
committerRobert Kessl <kesslr@centrum.cz>
Mon, 28 Jul 2014 11:51:17 +0000 (13:51 +0200)
ucw/table-types.c
ucw/xtypes-test.t

index b483ec554360faa35609fa45648692dd5594ba45..84d317c2b5da5158207e68a346566bfd24c3c65d 100644 (file)
@@ -7,10 +7,10 @@
 #include <ucw/lib.h>
 #include <ucw/table-types.h>
 #include <ucw/fastbuf.h>
+#include <ucw/strtonum.h>
 #include <ucw/table.h>
 #include <time.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <inttypes.h>
 #include <errno.h>
 
@@ -100,19 +100,11 @@ static const char *xt_size_fmt_parse(const char *opt_str, u32 *dest, struct memp
 static const char *xt_size_parse(const char *str, void *dest, struct mempool *pool)
 {
   errno = 0;
-  char *units_start = NULL;
-  // FIXME: Use str_to_u64() here to avoid troubles with strtoul()
-  // FIXME: Besides, who promises that u64 fits in unsigned long int?
-  u64 parsed = strtoul(str, &units_start, 10);
-  if(str == units_start) {
-    return mp_printf(pool, "Invalid value of size: '%s'.", str);
-  }
-
-  if(errno == EINVAL) {
-    return "Error occured during parsing of size.";
-  }
-  if(errno == ERANGE) {
-    return "Error: size value either too large or too small.";
+  const char *units_start = NULL;
+  u64 parsed;
+  const char *err = str_to_u64(&parsed, str, &units_start, 10 | STN_FLAGS);
+  if(err != NULL) {
+    return mp_printf(pool, "Invalid value of size: '%s'; number parser error: %s.", str, err);
   }
 
   if(*units_start == 0) {
@@ -122,7 +114,7 @@ static const char *xt_size_parse(const char *str, void *dest, struct mempool *po
 
   int unit_idx = xtype_unit_parser(units_start, xt_size_units);
   if(unit_idx == -1) {
-    return mp_printf(pool, "Invalid units: '%s'.", units_start);
+    return mp_printf(pool, "Invalid units: '%s'.", str);
   }
 
   // FIXME: Detect overflow?
@@ -189,18 +181,11 @@ static const char *xt_timestamp_fmt_parse(const char *opt_str, u32 *dest, struct
 static const char *xt_timestamp_parse(const char *str, void *dest, struct mempool *pool)
 {
   errno = 0;
-  char *parse_end = NULL;
-  // FIXME: Again, why strtoul()?
-  u64 parsed = strtoul(str, &parse_end, 10);
+  const char *parse_end = NULL;
+  u64 parsed;
+  const char *err = str_to_u64(&parsed, str, &parse_end, 10 | STN_FLAGS);
   if(str == parse_end) {
-    return mp_printf(pool, "Invalid value of timestamp: '%s'.", str);
-  }
-  if(errno == EINVAL) {
-    return "Error occured during parsing of size.";
-  }
-
-  if(errno == ERANGE) {
-    return "Error: size value either too large or too small.";
+    return mp_printf(pool, "Invalid value of timestamp: '%s'; number parser error: %s.", str, err);
   }
 
   if(*parse_end == 0) {
index 3480a3ada78a1b55039c02836b20058301ec0d8f..86a00bf69737e5cb0a21c59f32bba7ce17875c3b 100644 (file)
@@ -5,12 +5,12 @@ Out <<EOF
 4MB 4MB
 4GB 4GB
 4TB 4TB
-xt_size.parse error: 'Invalid units: 'X'.'.
-xt_size.parse error: 'Invalid value of size: 'KB'.'.
-xt_size.parse error: 'Invalid value of size: 'X1KB'.'.
-xt_size.parse error: 'Invalid units: 'XKB'.'.
-xt_size.parse error: 'Invalid units: 'KBX'.'.
-xt_size.parse error: 'Invalid value of size: ''.'.
+xt_size.parse error: 'Invalid units: '1X'.'.
+xt_size.parse error: 'Invalid value of size: 'KB'; number parser error: Number contains no digits.'.
+xt_size.parse error: 'Invalid value of size: 'X1KB'; number parser error: Number contains no digits.'.
+xt_size.parse error: 'Invalid units: '1XKB'.'.
+xt_size.parse error: 'Invalid units: '1KBX'.'.
+xt_size.parse error: 'Invalid value of size: ''; number parser error: Number contains no digits.'.
 0 false
 1 true
 false false
@@ -21,8 +21,8 @@ xt_timestamp.parse error: 'Invalid value of timestamp: '1403685533X'.'.
 xt_timestamp.parse error: 'Invalid value of timestamp: '2014X-06-25 08:38:53'.'.
 xt_timestamp.parse error: 'Invalid value of timestamp: '2X014-06-25 08:38:53'.'.
 xt_timestamp.parse error: 'Invalid value of timestamp: '2014-06-25 08:38:53X'.'.
-xt_timestamp.parse error: 'Invalid value of timestamp: 'X2014-06-25 08:38:53'.'.
-xt_timestamp.parse error: 'Invalid value of timestamp: 'X1403685533'.'.
+xt_timestamp.parse error: 'Invalid value of timestamp: 'X2014-06-25 08:38:53'; number parser error: Number contains no digits.'.
+xt_timestamp.parse error: 'Invalid value of timestamp: 'X1403685533'; number parser error: Number contains no digits.'.
 xt_timestamp.parse error: 'Invalid value of timestamp: '14X03685533'.'.
 xt_timestamp.parse error: 'Invalid value of timestamp: '1403685533X'.'.
 EOF