changeset 1867:15cbf17a8ea6

Make output format -f option symmetric with the -i input format option.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 24 Jun 2018 17:35:02 +0300
parents 734234e7ca95
children 4bb3d10328a9
files tools/gfxconv.c
diffstat 1 files changed, 51 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/tools/gfxconv.c	Sun Jun 24 17:07:02 2018 +0300
+++ b/tools/gfxconv.c	Sun Jun 24 17:35:02 2018 +0300
@@ -89,7 +89,7 @@
         optOutSubFormat = -1,
         optItemCount = -1,
         optPlanedWidth = 1,
-        optForcedFormat = -1;
+        optForcedInSubFormat = -1;
 unsigned int optInSkip = 0;
 
 int     optCropMode = CROP_NONE,
@@ -122,7 +122,7 @@
     {  3, 'o', "output",        "Output filename", OPT_ARGREQ },
     {  1, 'i', "informat",      "Set input format (spr[:mc:sc], chr[:mc|sc] or any supported image or C64 bitmap format)", OPT_ARGREQ },
     {  4, 's', "skip",          "Skip N bytes in input from start", OPT_ARGREQ },
-    {  5, 'f', "format",        "Output format (see --formats)", OPT_ARGREQ },
+    {  5, 'f', "format",        "Output format (see --formats and --informat/-i for spr/chr formats)", OPT_ARGREQ },
     { 17, 'F', "formats",       "List supported input/output formats", OPT_NONE },
     {  8, 'q', "sequential",    "Output sequential files (image output only)", OPT_NONE },
     {  6, 'c', "colormap",      "Color mappings (see below for information)", OPT_ARGREQ },
@@ -497,6 +497,50 @@
 }
 
 
+BOOL dmParseFormatOption(const char *msg1, const char *msg2, char *optArg, int *format, int *subFormat)
+{
+    char *flags = strchr(optArg, ':');
+    if (flags != NULL)
+        *flags++ = 0;
+
+    if (!dmGetFormatByExt(optArg, format, subFormat) &&
+        !dmGetC64FormatByExt(optArg, format, subFormat))
+    {
+        dmErrorMsg("Invalid %s format '%s', see -F / --formats for format list.\n",
+            msg1, optArg);
+        return FALSE;
+    }
+
+    if (flags != NULL)
+    {
+        switch (*format)
+        {
+            case FFMT_SPRITE:
+            case FFMT_CHAR:
+                if (strcasecmp(flags, "mc") == 0)
+                    optInMulticolor = TRUE;
+                else
+                if (strcasecmp(flags, "sc") == 0)
+                    optInMulticolor = FALSE;
+                else
+                {
+                    dmErrorMsg("Invalid %s format flags for sprite/char '%s', should be 'mc' or 'sc'.\n",
+                        msg1, flags);
+                    return FALSE;
+                }
+                break;
+
+            default:
+                dmErrorMsg("%s format '%s' does not support any flags ('%s').\n",
+                    msg2, optArg, flags);
+                return FALSE;
+        }
+    }
+
+    return TRUE;
+}
+
+
 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
 {
     switch (optN)
@@ -516,44 +560,8 @@
             break;
 
         case 1:
-            {
-                char *flags = strchr(optArg, ':');
-                if (flags != NULL)
-                    *flags++ = 0;
-
-                if (!dmGetFormatByExt(optArg, &optInFormat, &optInSubFormat) &&
-                    !dmGetC64FormatByExt(optArg, &optInFormat, &optInSubFormat))
-                {
-                    dmErrorMsg("Invalid input format '%s', see -F / --formats for format list.\n", optArg);
-                    return FALSE;
-                }
-
-                if (flags != NULL)
-                {
-                    switch (optInFormat)
-                    {
-                        case FFMT_SPRITE:
-                        case FFMT_CHAR:
-                            if (strcasecmp(flags, "mc") == 0)
-                                optInMulticolor = TRUE;
-                            else
-                            if (strcasecmp(flags, "sc") == 0)
-                                optInMulticolor = FALSE;
-                            else
-                            {
-                                dmErrorMsg("Invalid input format flags for sprite/char '%s', should be 'mc' or 'sc'.\n",
-                                    flags);
-                                return FALSE;
-                            }
-                            break;
-
-                        default:
-                            dmErrorMsg("Input format '%s' does not support any flags ('%s').\n",
-                                optArg, flags);
-                            return FALSE;
-                    }
-                }
-            }
+            if (!dmParseFormatOption("input", "Input", optArg, &optInFormat, &optForcedInSubFormat))
+                return FALSE;
             break;
 
         case 3:
@@ -569,12 +577,8 @@
             break;
 
         case 5:
-            if (!dmGetFormatByExt(optArg, &optOutFormat, &optOutSubFormat) &&
-                !dmGetC64FormatByExt(optArg, &optOutFormat, &optOutSubFormat))
-            {
-                dmErrorMsg("Invalid output format '%s'.\n", optArg);
+            if (!dmParseFormatOption("output", "Output", optArg, &optOutFormat, &optOutSubFormat))
                 return FALSE;
-            }
             break;
 
         case 6:
@@ -1759,9 +1763,9 @@
         DMGrowBuf tbuf;
         int res;
 
-        if (optForcedFormat >= 0)
+        if (optForcedInSubFormat >= 0)
         {
-            forced = &dmC64ImageFormats[optForcedFormat];
+            forced = &dmC64ImageFormats[optForcedInSubFormat];
             dmMsg(0, "Forced %s format image, type %d, %s\n",
                 forced->name, forced->format->type, forced->fext);
         }