From f774ed4525c52fb51f8ed6fd9fa2594c6ac178b1 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sat, 14 Mar 2020 23:55:13 +0000 Subject: 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 --- lib/util/nstring.c | 80 +++++++++++++++++++++++++++++++++++++++--------------- lib/util/nstring.h | 10 +++++++ 2 files changed, 68 insertions(+), 22 deletions(-) (limited to 'lib/util') 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 @@ -208,6 +208,16 @@ pm_memmem(const void * const haystackArg, 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, -- cgit 1.4.1