diff gfxconv.c @ 481:c3f0fca5b596

Improve remapping option parsing.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 07 Nov 2012 04:25:16 +0200
parents d7fc7e011c90
children 6fdee3ec2894
line wrap: on
line diff
--- a/gfxconv.c	Wed Nov 07 04:09:51 2012 +0200
+++ b/gfxconv.c	Wed Nov 07 04:25:16 2012 +0200
@@ -244,18 +244,25 @@
 }
 
 
-static BOOL dmParseMapOptionMapItem(char *opt, DMMapValue *value, const int nmax, const char *msg)
+static BOOL dmParseMapOptionMapItem(const char *popt, DMMapValue *value, const int nmax, const char *msg)
 {
-    char *split = strchr(opt, ':');
+    char *end, *split, *opt = dm_strdup(popt);
 
-    if (split == NULL)
+    if (opt == NULL)
+        goto error;
+
+    if ((end = split = strchr(opt, ':')) == NULL)
     {
         dmError("Invalid %s value '%s', expected <(#|%)RRGGBB|[$|0x]index>:<[$|0x]index>.\n", msg, opt);
-        return FALSE;
+        goto error;
     }
 
-    *split = 0;
+    // Trim whitespace
+    *end = 0;
+    for (end--; end > opt && *end && isspace(*end); end--)
+        *end = 0;
 
+    // Parse either a hex triplet color definition or a normal value
     if (*opt == '#' || *opt == '%')
     {
         int colR, colG, colB, colA;
@@ -270,8 +277,8 @@
         if (sscanf(opt + 1, "%2x%2x%2x", &colR, &colG, &colB) != 3 &&
             sscanf(opt + 1, "%2X%2X%2X", &colR, &colG, &colB) != 3)
         {
-            dmError("Invalid %s value '%s', expected a hex triplet after #.\n", msg, opt);
-            return FALSE;
+            dmError("Invalid %s value '%s', expected a hex triplet, got '%s'.\n", msg, popt, opt + 1);
+            goto error;
         }
 
         value->color.r = colR;
@@ -283,31 +290,41 @@
     {
         if (!dmGetIntVal(opt, &value->from))
         {
-            dmError("Invalid %s value '%s', could not parse.\n", msg, opt);
-            return FALSE;
+            dmError("Invalid %s value '%s', could not parse source value '%s'.\n", msg, popt, opt);
+            goto error;
         }
         value->triplet = FALSE;
     }
     
-    if (!dmGetIntVal(split + 1, &value->to))
+    // Trim whitespace
+    split++;
+    while (*split && isspace(*split)) split++;
+    
+    // Parse destination value
+    if (!dmGetIntVal(split, &value->to))
     {
-        dmError("Invalid %s value '%s', could not parse.\n", msg, opt);
-        return FALSE;
+        dmError("Invalid %s value '%s', could not parse destination value '%s'.\n", msg, popt, split);
+        goto error;
     }
 
     if (!value->triplet && (value->from < 0 || value->from > 255))
     {
-        dmError("Invalid %s map source color index value %d.\n", msg, value->from);
-        return FALSE;
+        dmError("Invalid %s map source color index value %d, must be [0..255].\n", msg, value->from);
+        goto error;
     }
 
     if (value->to < 0 || value->to > nmax)
     {
-        dmError("Invalid %s map destination color index value %d.\n", msg, value->to);
-        return FALSE;
+        dmError("Invalid %s map destination color index value %d, must be [0..%d].\n", msg, value->to, nmax);
+        goto error;
     }
-    
+
+    dmFree(opt);
     return TRUE;
+
+error:
+    dmFree(opt);
+    return FALSE;
 }