Simplify option scanning
authorparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Fri, 23 Jul 2010 01:24:57 +0000 (01:24 +0000)
committerparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Fri, 23 Jul 2010 01:24:57 +0000 (01:24 +0000)
git-svn-id: https://s.snth.net/svn/neverball/trunk@3216 78b8d119-cf0a-0410-b17c-f493084dd1d7

share/config.c

index 0dddce3..1d94217 100644 (file)
@@ -292,55 +292,33 @@ void config_init(void)
 }
 
 /*
- * Scan a NUL-terminated string LINE according to the format
- * '^<space>?<key><space><value>$' and store pointers to the start of key and
- * value at DST_KEY and DST_VAL, respectively.  No memory is allocated to store
- * the strings;  instead, the memory pointed to by LINE modified in-place as
- * needed.
- *
- * Return 1 if LINE matches the format, return 0 otherwise.
+ * Scan an option string and store pointers to the start of key and
+ * value at the passed-in locations.  No memory is allocated to store
+ * the strings; instead, the option string is modified in-place as
+ * needed.  Return 1 on success, 0 on error.
  */
-
 static int scan_key_and_value(char **dst_key, char **dst_val, char *line)
 {
     if (line)
     {
-        char *key, *val, *space;
+        int ks, ke, vs;
 
-        for (key = line; *key && isspace(*key); key++);
+        ks = -1;
+        ke = -1;
+        vs = -1;
 
-        if (*key)
-        {
-            if (dst_key)
-                *dst_key = key;
-        }
-        else
-            return 0;
-
-        for (space = key; *space && !isspace(*space); space++);
+        sscanf(line, " %n%*s%n %n", &ks, &ke, &vs);
 
-        if (*space)
-        {
-            /* NUL-terminate the key, if necessary. */
+        if (ks < 0 || ke < 0 || vs < 0)
+            return 0;
 
-            if (dst_key)
-            {
-                *space = '\0';
-                space++;
-            }
-        }
-        else
+        if (vs - ke < 1)
             return 0;
 
-        for (val = space; *val && isspace(*val); val++);
+        line[ke] = 0;
 
-        if (*val)
-        {
-            if (dst_val)
-                *dst_val = val;
-        }
-        else
-            return 0;
+        *dst_key = line + ks;
+        *dst_val = line + vs;
 
         return 1;
     }