about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--lib/util/nstring.c40
-rw-r--r--lib/util/nstring.h5
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