From d8bed4d3e136f6caae487c5871d13697a4227e48 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Fri, 9 Jun 2017 21:20:42 +0000 Subject: add pm_string_to_uint() git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@2990 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- lib/util/nstring.c | 40 ++++++++++++++++++++++++++++++++++++++++ lib/util/nstring.h | 5 +++++ 2 files changed, 45 insertions(+) (limited to 'lib/util') 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 -- cgit 1.4.1