X-Git-Url: http://git.droids-corp.org/?p=protos%2Flibecoli.git;a=blobdiff_plain;f=src%2Fecoli_string.c;fp=src%2Fecoli_string.c;h=c82366400fe8889cf32d1d3c6a915f9dca10e47c;hp=fd427b4a0d64f96a70594f9f21ab7d6376624a38;hb=a83942778fc85ad92db9082cf0d7c2d958d98aaf;hpb=e900ed6e1cb162855289bc7a9acc9b3d1af9d697 diff --git a/src/ecoli_string.c b/src/ecoli_string.c index fd427b4..c823664 100644 --- a/src/ecoli_string.c +++ b/src/ecoli_string.c @@ -6,6 +6,9 @@ #include #include #include +#include +#include +#include #include #include @@ -87,3 +90,66 @@ bool ec_str_is_space(const char *s) } return true; } + +int ec_str_parse_llint(const char *str, unsigned int base, int64_t min, + int64_t max, int64_t *val) +{ + char *endptr; + int save_errno = errno; + + errno = 0; + *val = strtoll(str, &endptr, base); + + if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) || + (errno != 0 && *val == 0)) + return -1; + + if (*val < min) { + errno = ERANGE; + return -1; + } + + if (*val > max) { + errno = ERANGE; + return -1; + } + + if (*endptr != 0) { + errno = EINVAL; + return -1; + } + + errno = save_errno; + return 0; +} + +int ec_str_parse_ullint(const char *str, unsigned int base, uint64_t min, + uint64_t max, uint64_t *val) +{ + char *endptr; + int save_errno = errno; + + /* since a negative input is silently converted to a positive + * one by strtoull(), first check that it is positive */ + if (strchr(str, '-')) + return -1; + + errno = 0; + *val = strtoull(str, &endptr, base); + + if ((errno == ERANGE && *val == ULLONG_MAX) || + (errno != 0 && *val == 0)) + return -1; + + if (*val < min) + return -1; + + if (*val > max) + return -1; + + if (*endptr != 0) + return -1; + + errno = save_errno; + return 0; +}