#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>
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) {
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?
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) {
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
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