diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2017-06-09 21:20:42 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2017-06-09 21:20:42 +0000 |
commit | d8bed4d3e136f6caae487c5871d13697a4227e48 (patch) | |
tree | f4acec742f1e1e415f395e84f54c3cce26a18958 /lib/util | |
parent | 0af8fb2809652d149173e57042d170e1126cd84c (diff) | |
download | netpbm-mirror-d8bed4d3e136f6caae487c5871d13697a4227e48.tar.gz netpbm-mirror-d8bed4d3e136f6caae487c5871d13697a4227e48.tar.xz netpbm-mirror-d8bed4d3e136f6caae487c5871d13697a4227e48.zip |
add pm_string_to_uint()
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@2990 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/util')
-rw-r--r-- | lib/util/nstring.c | 40 | ||||
-rw-r--r-- | lib/util/nstring.h | 5 |
2 files changed, 45 insertions, 0 deletions
diff --git a/lib/util/nstring.c b/lib/util/nstring.c index 6c28095f..f9c8943d 100644 --- a/lib/util/nstring.c +++ b/lib/util/nstring.c @@ -1030,3 +1030,43 @@ pm_interpret_uint(const char * const string, } +void +pm_string_to_uint(const char * const string, + unsigned int * const uintP, + const char ** const errorP) { + + if (strlen(string) == 0) + pm_asprintf(errorP, "Value is a null string"); + else { + char * tailptr; + + /* We can't use 'strtoull'. Contrary to expectations, though as + designed, it returns junk if there is a minus sign. + */ + + long longValue; + + longValue = strtol(string, &tailptr, 10); + + + *uintP = strtoul(string, &tailptr, 10); + + if (*tailptr != '\0') + pm_asprintf(errorP, "Non-numeric crap in string: '%s'", tailptr); + else { + if (longValue < 0) + pm_asprintf(errorP, "Number is negative"); + else { + if ((unsigned int)longValue != longValue) + pm_asprintf(errorP, "Number is too large for computation"); + else { + *uintP = (unsigned int)longValue; + *errorP = NULL; + } + } + } + } +} + + + diff --git a/lib/util/nstring.h b/lib/util/nstring.h index 794fb232..257b04b0 100644 --- a/lib/util/nstring.h +++ b/lib/util/nstring.h @@ -203,6 +203,11 @@ pm_interpret_uint(const char * const string, unsigned int * const valueP, const char ** const errorP); +void +pm_string_to_uint(const char * const string, + unsigned int * const uintP, + const char ** const errorP); + #ifdef __cplusplus } #endif |