diff src/dmlib.c @ 2004:161e731eb152

Improve dmGetIntVal() to accept an optional negative value boolean flag pointer. Also improve error handling in it.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 07 Jul 2018 01:11:22 +0300
parents 12d4da502fa4
children 3d40a6767a4e
line wrap: on
line diff
--- a/src/dmlib.c	Tue Jul 03 09:54:14 2018 +0300
+++ b/src/dmlib.c	Sat Jul 07 01:11:22 2018 +0300
@@ -136,22 +136,76 @@
 #endif
 
 
-BOOL dmGetIntVal(const char *s, unsigned int *i)
+BOOL dmGetIntVal(const char *str, unsigned int *value, BOOL *neg)
 {
-    if (s[0] == '$')
+    int ch;
+    BOOL hex = FALSE;
+
+    // Is the value negative?
+    if (*str == '-')
     {
-        if (sscanf(&s[1], "%x", i) < 1)
+        if (neg == NULL)
             return FALSE;
+
+        *neg = TRUE;
+        str++;
     }
-    else if (s[0] == '0' && s[1] == 'x')
+    else
+    if (neg != NULL)
+        *neg = FALSE;
+
+    // Is it hexadecimal?
+    if (*str == '$')
+    {
+        hex = TRUE;
+        str++;
+    }
+    else
+    if (str[0] == '0' && str[1] == 'x')
     {
-        if (sscanf(&s[2], "%x", i) < 1)
-            return FALSE;
+        hex = TRUE;
+        str += 2;
+    }
+
+    // Parse the value
+    *value = 0;
+    if (hex)
+    {
+        while ((ch = *str++))
+        {
+            if (ch >= '0' && ch <= '9')
+            {
+                *value <<= 4;
+                *value |= ch - '0';
+            }
+            else
+            if (ch >= 'A' && ch <= 'F')
+            {
+                *value <<= 4;
+                *value |= ch - 'A';
+            }
+            else
+            if (ch >= 'a' && ch <= 'f')
+            {
+                *value <<= 4;
+                *value |= ch - 'a';
+            }
+            else
+                return FALSE;
+        }
     }
     else
     {
-        if (sscanf(s, "%u", i) < 1)
-            return FALSE;
+        while ((ch = *str++))
+        {
+            if (ch >= '0' && ch <= '9')
+            {
+                *value *= 10;
+                *value += ch - '0';
+            }
+            else
+                return FALSE;
+        }
     }
     return TRUE;
 }