diff tools/gfxconv.c @ 2565:d56a0e86067a

Improve error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 28 Feb 2022 11:49:58 +0200
parents acaf678a2129
children d75431bf1a7d
line wrap: on
line diff
--- a/tools/gfxconv.c	Mon Feb 28 10:18:38 2022 +0200
+++ b/tools/gfxconv.c	Mon Feb 28 11:49:58 2022 +0200
@@ -2574,7 +2574,7 @@
     DMImage *inImage = NULL, *outImage = NULL;
     Uint8 *dataBuf = NULL, *dataBufOrig = NULL;
     size_t dataSize, dataSizeOrig, dataRealOffs;
-    int i, n, res;
+    int i, n, res = DMERR_OK;
 
     // Default color mapping
     for (i = 0; i < D64_NCOLORS; i++)
@@ -2588,7 +2588,7 @@
     {
         dmErrorMsg("Could not initialize lib64gfx: %s\n",
             dmErrorStr(res));
-        goto exit;
+        goto out;
     }
 
     nconvFormatList = ndmImageFormatList + ndmPaletteFormatList + nbaseFormatList;
@@ -2625,14 +2625,13 @@
 
     if (!dmArgsProcess(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
-        exit(1);
+        goto out;
 
     switch (optShowHelp)
     {
         case 1:
             argShowHelp();
-            exit(0);
-            break;
+            goto out;
 
         case 2:
             argShowHelp();
@@ -2646,14 +2645,12 @@
                 if (str != NULL)
                     fprintf(stdout, "\n%s\n", str);
             }
-            exit(0);
-            break;
+            goto out;
 
         case 3:
             argShowFormats();
             argShowC64Formats(stdout, TRUE, dmVerbosity > 0);
-            exit(0);
-            break;
+            goto out;
     }
 
     // Determine input format, if not specified
@@ -2677,7 +2674,7 @@
         {
             dmErrorMsg("Standard input cannot be used without specifying input format.\n");
             dmErrorMsg("Perhaps you should try --help or --longhelp\n");
-            goto exit;
+            goto out;
         }
         inFile = stdin;
         optInFilename = "stdin";
@@ -2688,7 +2685,7 @@
         res = dmGetErrno();
         dmErrorMsg("Error opening input file '%s': %s\n",
               optInFilename, dmErrorStr(res));
-        goto exit;
+        goto out;
     }
 
     // Determine output format, if not specified
@@ -2715,7 +2712,7 @@
     if ((res = dmReadDataFile(inFile, NULL, &dataBufOrig, &dataSizeOrig)) != DMERR_OK)
     {
         dmErrorMsg("Could not read input: %s.\n", dmErrorStr(res));
-        goto exit;
+        goto out;
     }
 
     fclose(inFile);
@@ -2725,7 +2722,7 @@
     {
         dmErrorMsg("Input skip value %d (0x%x) is larger than input size %" DM_PRIu_SIZE_T ".\n",
             optInSkip, optInSkip, dataSizeOrig);
-        goto exit;
+        goto out;
     }
 
     if (optInSkipNeg)
@@ -2787,7 +2784,7 @@
         if (res != DMERR_OK && (forced != NULL || optInType == FFMT_BITMAP))
         {
             dmErrorMsg("Could not decode input image: %s.\n", dmErrorStr(res));
-            goto exit;
+            goto out;
         }
     }
 
@@ -2822,7 +2819,7 @@
     if (optInType == FFMT_AUTO)
     {
         dmErrorMsg("No input format specified, and could not be determined automatically.\n");
-        goto exit;
+        goto out;
     }
 
     if (dmGetConvFormat(optInType, optInFormat, &inFormat) &&
@@ -2864,7 +2861,7 @@
     // Handle palette stuff that is generic for different operation modes
     if (optPaletteFile != NULL &&
         (res = dmHandleExternalPalette(optPaletteFile, &optPaletteData)) != DMERR_OK)
-        goto exit;
+        goto out;
 
     switch (optInType)
     {
@@ -2884,7 +2881,7 @@
                 {
                     dmErrorMsg("Could not set up palette: %s.\n",
                         dmErrorStr(res));
-                    goto exit;
+                    goto out;
                 }
             }
 
@@ -2892,7 +2889,7 @@
             {
                 dmErrorMsg("Palette does not have enough colors (%d < %d)\n",
                     optPaletteData->ncolors, D64_NCOLORS);
-                goto exit;
+                goto out;
             }
 
             if (optPaletteData->ncolors > D64_NCOLORS)
@@ -2914,7 +2911,7 @@
                 {
                     dmErrorMsg("Could not set up palette: %s.\n",
                         dmErrorStr(res));
-                    goto exit;
+                    goto out;
                 }
             }
     }
@@ -2934,14 +2931,14 @@
                 if (optOutFilename == NULL)
                 {
                     dmErrorMsg("Output filename not set, required for palette formats.\n");
-                    goto exit;
+                    goto out;
                 }
 
                 // Read palette file
                 if ((res = dmf_open_memio(NULL, optInFilename, dataBuf, dataSize, &fp)) != DMERR_OK)
                 {
                     dmErrorMsg("Could not create MemIO handle for input.\n");
-                    goto exit;
+                    goto out;
                 }
 
                 // Read input
@@ -2955,12 +2952,12 @@
                 if (res != DMERR_OK)
                 {
                     dmErrorMsg("Palette could not be read.\n");
-                    goto exit;
+                    goto out;
                 }
             }
 
             if (optPaletteData == NULL)
-                goto exit;
+                goto out;
 
             switch (optOutType)
             {
@@ -2975,14 +2972,14 @@
                     {
                         res = dmError(DMERR_MALLOC,
                             "Could not allocate memory for image.\n");
-                        goto exit;
+                        goto out;
                     }
 
                     if ((res = dmPaletteCopy(&inImage->pal, optPaletteData)) != DMERR_OK)
                     {
                         dmErrorMsg("Could not allocate image palette: %s\n",
                             dmErrorStr(res));
-                        goto exit;
+                        goto out;
                     }
 
                     res = dmWriteImage(optOutFilename, inImage, &optSpec,
@@ -2999,7 +2996,7 @@
             if (optOutFilename == NULL)
             {
                 dmErrorMsg("Output filename not set, required for bitmap formats.\n");
-                goto exit;
+                goto out;
             }
 
             switch (optOutType)
@@ -3025,7 +3022,7 @@
                         {
                             dmErrorMsg("Could not read character ROM from '%s'.\n",
                                 optCharROMFilename);
-                            goto exit;
+                            goto out;
                         }
                     }
 
@@ -3036,7 +3033,7 @@
                     {
                         dmErrorMsg("Error in bitmap to image conversion: %s.\n",
                             dmErrorStr(res));
-                        goto exit;
+                        goto out;
                     }
 
                     switch (optOutType)
@@ -3067,7 +3064,7 @@
                         &dmC64ImageFormats[optOutFormat], inC64Fmt)) != DMERR_OK)
                     {
                         dmErrorMsg("Error in bitmap format conversion.\n");
-                        goto exit;
+                        goto out;
                     }
                     if (dmVerbosity >= 2)
                     {
@@ -3091,13 +3088,13 @@
                 if (optOutFilename == NULL)
                 {
                     dmErrorMsg("Output filename not set, required for image formats.\n");
-                    goto exit;
+                    goto out;
                 }
 
                 if ((res = dmf_open_memio(NULL, optInFilename, dataBuf, dataSize, &fp)) != DMERR_OK)
                 {
                     dmErrorMsg("Could not create MemIO handle for input.\n");
-                    goto exit;
+                    goto out;
                 }
 
                 // Read input
@@ -3109,7 +3106,7 @@
                 dmf_close(fp);
 
                 if (res != DMERR_OK || inImage == NULL)
-                    goto exit;
+                    goto out;
 
                 switch (optOutType)
                 {
@@ -3122,7 +3119,7 @@
                         if (inImage->pal == NULL || inImage->pixfmt != DM_PIXFMT_PALETTE)
                         {
                             dmErrorMsg("Source image is not a paletted format or has no palette.\n");
-                            goto exit;
+                            goto out;
                         }
                         res = dmWritePalette(optOutFilename, inImage->pal, &dmPaletteFormatList[optOutFormat]);
                         break;
@@ -3144,7 +3141,7 @@
                                 dmC64ImageFree(tmpC64Image);
                                 dmErrorMsg("Error in image to bitmap conversion: %s.\n",
                                     dmErrorStr(res));
-                                goto exit;
+                                goto out;
                             }
 
                             if ((res = dmConvertC64Bitmap(&outC64Image, tmpC64Image,
@@ -3153,7 +3150,7 @@
                                 dmC64ImageFree(tmpC64Image);
                                 dmErrorMsg("Error in bitmap format conversion: %s.\n",
                                     dmErrorStr(res));
-                                goto exit;
+                                goto out;
                             }
 
                             res = dmWriteBitmap(optOutFilename, outC64Image, &dmC64ImageFormats[optOutFormat]);
@@ -3175,7 +3172,7 @@
             dmErrorStr(res));
     }
 
-exit:
+out:
     // Cleanup
     dmFree(convFormatList);
     dmFree(dataBufOrig);
@@ -3186,5 +3183,5 @@
     dmImageFree(outImage);
     dmLib64GFXClose();
 
-    return 0;
+    return res;
 }