diff options
author | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2020-03-14 23:55:13 +0000 |
---|---|---|
committer | giraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8> | 2020-03-14 23:55:13 +0000 |
commit | f774ed4525c52fb51f8ed6fd9fa2594c6ac178b1 (patch) | |
tree | fd55005bccf4605a29ec7e1a5a8388484246fae6 /lib | |
parent | 8d2cc3fb65f50e112ad7f66da872c974156f34c4 (diff) | |
download | netpbm-mirror-f774ed4525c52fb51f8ed6fd9fa2594c6ac178b1.tar.gz netpbm-mirror-f774ed4525c52fb51f8ed6fd9fa2594c6ac178b1.tar.xz netpbm-mirror-f774ed4525c52fb51f8ed6fd9fa2594c6ac178b1.zip |
Add pm_string_to_int, pm_string_to_long
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@3747 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/util/nstring.c | 80 | ||||
-rw-r--r-- | lib/util/nstring.h | 10 |
2 files changed, 68 insertions, 22 deletions
diff --git a/lib/util/nstring.c b/lib/util/nstring.c index 7ef9fcfb..6d68af49 100644 --- a/lib/util/nstring.c +++ b/lib/util/nstring.c @@ -1007,47 +1007,83 @@ pm_strishex(const char * const subject) { void -pm_string_to_uint(const char * const string, - unsigned int * const uintP, +pm_string_to_long(const char * const string, + long * const longP, const char ** const errorP) { if (strlen(string) == 0) pm_asprintf(errorP, "Value is a null string"); else { char * tailptr; - long longValue; - - /* We can't use 'strtoul'. Contrary to expectations, though as - designed, it returns junk if there is a minus sign. - */ /* strtol() does a bizarre thing where if the number is out of range, it returns a clamped value but tells you about it by setting errno = ERANGE. If it is not out of range, strtol() leaves errno alone. */ - errno = 0; /* So we can tell if strtoul() overflowed */ + errno = 0; /* So we can tell if strtol() overflowed */ - longValue = strtol(string, &tailptr, 10); + *longP = strtol(string, &tailptr, 10); if (*tailptr != '\0') pm_asprintf(errorP, "Non-numeric crap in string: '%s'", tailptr); else { if (errno == ERANGE) pm_asprintf(errorP, "Number is too large for computation"); - 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; - } - } - } + else + *errorP = NULL; + } + } +} + + + +void +pm_string_to_int(const char * const string, + int * const intP, + const char ** const errorP) { + + long longValue; + + pm_string_to_long(string, &longValue, errorP); + + if (!*errorP) { + if ((int)longValue != longValue) + pm_asprintf(errorP, + "Number is too large for computation"); + else { + *intP = (int)longValue; + *errorP = NULL; + } + } +} + + + +void +pm_string_to_uint(const char * const string, + unsigned int * const uintP, + const char ** const errorP) { + + /* We can't use 'strtoul'. Contrary to expectations, though as + designed, it returns junk if there is a minus sign. + */ + + long longValue; + + pm_string_to_long(string, &longValue, errorP); + + if (!*errorP) { + 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 f9d3dffa..8829617d 100644 --- a/lib/util/nstring.h +++ b/lib/util/nstring.h @@ -209,6 +209,16 @@ bool pm_strishex(const char * const subject); void +pm_string_to_long(const char * const string, + long * const longP, + const char ** const errorP); + +void +pm_string_to_int(const char * const string, + int * const intP, + const char ** const errorP); + +void pm_string_to_uint(const char * const string, unsigned int * const uintP, const char ** const errorP); |