changeset 2488:87c8be2bc8ba

Rework some option parsing to be more robust.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 28 Apr 2020 18:09:32 +0300
parents 14a4f8d78fe6
children c64806412be0
files tools/gfxconv.c
diffstat 1 files changed, 44 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/tools/gfxconv.c	Tue Apr 28 18:09:07 2020 +0300
+++ b/tools/gfxconv.c	Tue Apr 28 18:09:32 2020 +0300
@@ -912,29 +912,57 @@
             break;
 
         case 40:
+            if (strcasecmp(optArg, "auto") == 0)
             {
-                int tx0, ty0, tx1, ty1;
-                if (strcasecmp(optArg, "auto") == 0)
+                optCropMode = CROP_AUTO;
+            }
+            else
+            {
+                unsigned int tx0, ty0, tx1, ty1;
+                char sep, modeSep = 0, *tmpTok, *tmpStr;
+                BOOL ok;
+                if ((tmpTok = tmpStr = dm_strdup(optArg)) == NULL)
                 {
-                    optCropMode = CROP_AUTO;
+                    dmErrorMsg("Could not allocate memory for temporary string.\n");
+                    return FALSE;
                 }
-                else
-                if (sscanf(optArg, "%d:%d-%d:%d", &tx0, &ty0, &tx1, &ty1) == 4)
+
+                // Check for 'x0:y0-x1:y1' pattern
+                ok = dmParseIntValWithSep(&tmpTok, &tx0, &sep, ":") &&
+                    sep == ':' &&
+                        dmParseIntValWithSep(&tmpTok, &ty0, &modeSep, ":-") &&
+                    (sep == ':' || sep == '-') &&
+                    dmParseIntValWithSep(&tmpTok, &tx1, &sep, ":") &&
+                    sep == ':' &&
+                    dmParseIntValWithSep(&tmpTok, &ty1, &sep, "");
+
+                dmFree(tmpStr);
+
+                if (ok)
                 {
                     optCropMode = CROP_SIZE;
                     optCropX0   = tx0;
                     optCropY0   = ty0;
-                    optCropW    = tx1 - tx0 + 1;
-                    optCropH    = ty1 - ty0 + 1;
-                }
-                else
-                if (sscanf(optArg, "%d:%d:%d:%d", &tx0, &ty0, &tx1, &ty1) == 4)
-                {
-                    optCropMode = CROP_SIZE;
-                    optCropX0   = tx0;
-                    optCropY0   = ty0;
-                    optCropW    = tx1;
-                    optCropH    = ty1;
+
+                    if (modeSep == '-')
+                    {
+                        DM_SWAP_IF(unsigned int, tx0, tx1);
+                        DM_SWAP_IF(unsigned int, ty0, ty1);
+
+                        optCropW    = tx1 - tx0;
+                        optCropH    = ty1 - ty0;
+
+                        dmMsg(1, "Crop coordinates %d, %d - %d, %d [dim %d, %d]\n",
+                            tx0, ty0, tx1, ty1, optCropW, optCropH);
+                    }
+                    else
+                    {
+                        optCropW    = tx1;
+                        optCropH    = ty1;
+
+                        dmMsg(1, "Crop coordinates %d, %d - %d, %d [dim %d, %d]\n",
+                            tx0, ty0, tx0 + tx1, ty0 + ty1, optCropW, optCropH);
+                    }
                 }
                 else
                 {