changeset 972:6d5edc6af2ad

Error handling cleanups/fixes.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 27 Feb 2015 14:17:12 +0200
parents f654435df15e
children 15e6c2bd207b
files tools/gfxconv.c
diffstat 1 files changed, 30 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/tools/gfxconv.c	Fri Feb 27 06:10:16 2015 +0200
+++ b/tools/gfxconv.c	Fri Feb 27 14:17:12 2015 +0200
@@ -467,7 +467,7 @@
     if ((fp = fopen(filename, "r")) == NULL)
     {
         res = dmGetErrno();
-        dmErrorMsg("Could not open color remap file '%s' for reading, %d: %s\n",
+        dmError(res, "Could not open color remap file '%s' for reading, %d: %s\n",
             res, dmErrorStr(res));
         return res;
     }
@@ -865,8 +865,8 @@
 
     if (npal == NULL || mapping == NULL || mapped == NULL || used == NULL)
     {
-        dmErrorMsg("Could not allocate memory for reused palette.\n");
-        return DMERR_MALLOC;
+        return dmError(DMERR_MALLOC,
+            "Could not allocate memory for reused palette.\n");
     }
 
     for (index = 0; index < image->ncolors; index++)
@@ -1040,7 +1040,7 @@
     if ((outFile = fopen(filename, "wb")) == NULL)
     {
         res = dmGetErrno();
-        dmErrorMsg("Error opening output file '%s', %d: %s\n",
+        dmError(res, "Error opening output file '%s', %d: %s\n",
             filename, res, dmErrorStr(res));
         goto error;
     }
@@ -1048,7 +1048,7 @@
     if (!dm_fwrite_str(outFile, buf, bufSize))
     {
         res = dmGetErrno();
-        dmErrorMsg("Error writing image data to '%s', %d: %s\n",
+        dmError(res, "Error writing image data to '%s', %d: %s\n",
             filename, res, dmErrorStr(res));
     }
 
@@ -1270,15 +1270,15 @@
             break;
 
         default:
-            ret = DMERR_INVALID_ARGS;
-            dmErrorMsg("Invalid output format %d, internal error.\n", outFormat);
+            ret = dmError(DMERR_INVALID_ARGS,
+                "Invalid output format %d, internal error.\n", outFormat);
             goto error;
     }
 
     if (outBlockW < 1 || outBlockH < 1)
     {
-        ret = DMERR_INVALID_ARGS;
-        dmErrorMsg("Source image dimensions too small for conversion, block dimensions %d x %d.\n",
+        ret = dmError(DMERR_INVALID_ARGS,
+            "Source image dimensions too small for conversion, block dimensions %d x %d.\n",
             outBlockW, outBlockH);
         goto error;
     }
@@ -1330,7 +1330,7 @@
         if (!dm_fwrite_str(outFile, buf, outBufSize))
         {
             ret = dmGetErrno();
-            dmErrorMsg("Error writing data block %d,%d to '%s', %d: %s\n",
+            dmError(ret, "Error writing data block %d,%d to '%s', %d: %s\n",
                 bx, by, filename, ret, dmErrorStr(ret));
             goto error;
         }
@@ -1350,7 +1350,7 @@
 
 int dmDumpSpritesAndChars(FILE *inFile)
 {
-    int dataOffs, itemCount, outWidth, outWidthPX, outHeight;
+    int dataOffs, itemCount, outWidth, outWidthPX, outHeight, ret;
     size_t bufSize;
     Uint8 *bufData;
 
@@ -1371,14 +1371,14 @@
             break;
 
         default:
-            dmErrorMsg("Invalid input format %d, internal error.\n", optInFormat);
-            return -1;
+            return dmError(DMERR_INTERNAL,
+                "Invalid input format %d, internal error.\n", optInFormat);
     }
 
     if ((bufData = dmMalloc(bufSize)) == NULL)
     {
-        dmErrorMsg("Could not allocate temporary buffer of %d bytes.\n", bufSize);
-        return -2;
+        return dmError(DMERR_INTERNAL,
+            "Could not allocate temporary buffer of %d bytes.\n", bufSize);
     }
 
 
@@ -1395,9 +1395,9 @@
         else
         if ((outFile = fopen(optOutFilename, "w")) == NULL)
         {
-            int res = dmGetErrno();
-            dmErrorMsg("Error opening output file '%s', %d: %s\n",
-                  optOutFilename, res, dmErrorStr(res));
+            ret = dmGetErrno();
+            dmError(ret, "Error opening output file '%s', %d: %s\n",
+                  optOutFilename, ret, dmErrorStr(ret));
             goto error;
         }
 
@@ -1491,8 +1491,6 @@
 
             if (optSequential)
             {
-                int eres;
-
                 outFilename = dm_strdup_printf("%s%04d.%s", optOutFilename, itemCount, convFormatList[optOutFormat].fext);
                 if (outFilename == NULL)
                 {
@@ -1500,11 +1498,11 @@
                     goto error;
                 }
                 
-                eres = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE);
-                if (eres != DMERR_OK)
+                ret = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE);
+                if (ret != DMERR_OK)
                 {
-                    dmErrorMsg("Error writing output image, %s.\n",
-                        dmErrorStr(eres));
+                    dmErrorMsg("Error writing output image, %d: %s.\n",
+                        ret, dmErrorStr(ret));
                 }
 
                 dmFree(outFilename);
@@ -1523,11 +1521,11 @@
 
         if (!optSequential)
         {
-            int eres = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE);
-            if (eres != DMERR_OK)
+            ret = dmWriteImage(optOutFilename, outImage, &optSpec, optOutSubFormat, TRUE);
+            if (ret != DMERR_OK)
             {
-                dmErrorMsg("Error writing output image, %s.\n",
-                    dmErrorStr(eres));
+                dmError(ret, "Error writing output image, %d: %s.\n",
+                    ret, dmErrorStr(ret));
             }
         }
         
@@ -1538,17 +1536,18 @@
     {
         if (optSequential)
         {
-            dmErrorMsg("Sequential output not supported for spr/char -> bitmap conversion.\n");
+            ret = dmError(DMERR_INVALID_ARGS,
+                "Sequential output not supported for spr/char -> bitmap conversion.\n");
             goto error;
         }
     }
 
     dmFree(bufData);
-    return 0;
+    return DMERR_OK;
 
 error:
     dmFree(bufData);
-    return -1;
+    return ret;
 }