changeset 2177:a4c03b6155c9

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 12 Oct 2019 20:40:32 +0300
parents 76d2b8237f23
children dbcfc30a6079
files patchmap.c stitchmap.c
diffstat 2 files changed, 51 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/patchmap.c	Sat Oct 12 20:08:01 2019 +0300
+++ b/patchmap.c	Sat Oct 12 20:40:32 2019 +0300
@@ -76,10 +76,10 @@
 
 BOOL argHandleFile(char *currArg)
 {
-    if (!mapFilename)
+    if (mapFilename == NULL)
         mapFilename = currArg;
     else
-    if (!patchFilename)
+    if (patchFilename == NULL)
         patchFilename = currArg;
     else
     {
@@ -93,8 +93,9 @@
 
 int main(int argc, char *argv[])
 {
-    MapBlock *map, *patch;
-    FILE *outFile;
+    MapBlock *map = NULL, *patch = NULL;
+    FILE *outFile = NULL;
+    int res = 0;
 
     // Initialize
     th_init("patchmap", "Patch a mapfile with a diff", "0.1", NULL, NULL);
@@ -103,20 +104,25 @@
     // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
-        exit(1);
+    {
+        res = 1;
+        goto exit;
+    }
 
     if (mapFilename == NULL || patchFilename == NULL)
     {
         THERR("Nothing to do. (try --help)\n");
-        exit(0);
+        res = 1;
+        goto exit;
     }
 
     // Read map and patch file
-    if ((map = mapBlockParseFile(mapFilename, FALSE)) == NULL)
-        exit(2);
-
-    if ((patch = mapBlockParseFile(patchFilename, TRUE)) == NULL)
-        exit(3);
+    if ((map = mapBlockParseFile(mapFilename, FALSE)) == NULL ||
+        (patch = mapBlockParseFile(patchFilename, TRUE)) == NULL)
+    {
+        res = 2;
+        goto exit;
+    }
 
     // Check map and diff sizes
     if (optCheck && (map->width != patch->width || map->height != patch->height))
@@ -124,7 +130,9 @@
         THERR("Map and patch dimensions do not match (%d x %d [map] <-> %d x %d [patch])\n",
             map->width, map->height,
             patch->width, patch->height);
-        exit(4);
+
+        res = 2;
+        goto exit;
     }
 
     // Generate
@@ -175,12 +183,17 @@
     {
         THERR("Error opening output file '%s'!\n",
             destFilename);
-        exit(1);
+        res = 6;
+        goto exit;
     }
 
     mapBlockPrint(outFile, map);
-
     fclose(outFile);
 
-    return 0;
+exit:
+
+    mapBlockFree(map);
+    mapBlockFree(patch);
+
+    return res;
 }
--- a/stitchmap.c	Sat Oct 12 20:08:01 2019 +0300
+++ b/stitchmap.c	Sat Oct 12 20:40:32 2019 +0300
@@ -412,7 +412,8 @@
     BOOL isOK;
     int i, currRounds, nmapBlocks, currBlocks,
         worldX0, worldY0, worldX1, worldY1,
-        offsetX = 0, offsetY = 0;
+        offsetX = 0, offsetY = 0,
+        res = 0;
     MapBlock *worldMap = NULL, *initialMap = NULL;
     MapBlock **mapBlocks = NULL;
 
@@ -422,12 +423,16 @@
     // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
-        exit(1);
+    {
+        res = 1;
+        goto exit;
+    }
 
     if (nsrcFiles < 1)
     {
         THERR("Nothing to do. (try --help)\n");
-        exit(0);
+        res = 1;
+        goto exit;
     }
 
     // Read initial map
@@ -445,7 +450,8 @@
         else
         {
             THERR("Initial map could not be loaded!\n");
-            exit(1);
+            res = 1;
+            goto exit;
         }
     }
 
@@ -474,7 +480,8 @@
         {
             THERR("Error opening input file '%s'!\n",
                 srcFiles[i]);
-            exit(1);
+            res = 16;
+            goto exit;
         }
 
         while (!feof(tmpFile))
@@ -512,7 +519,8 @@
     if (nmapBlocks <= 0)
     {
         THERR("No mapblocks, nothing to do.\n");
-        exit(11);
+        res = 11;
+        goto exit;
     }
 
 
@@ -567,7 +575,8 @@
     if ((worldMap = mapBlockAlloc(worldX1 - worldX0 + 1, worldY1 - worldY0 + 1)) == NULL)
     {
         THERR("Error allocating world map!\n");
-        exit(4);
+        res = 4;
+        goto exit;
     }
 
     // Place optional initial map
@@ -576,7 +585,8 @@
         if (mapBlockPut(&worldMap, initialMap, offsetX + initialMap->xc, offsetY + initialMap->yc) < 0)
         {
             THERR("Initial map mapBlockPut() failed!\n");
-            exit(9);
+            res = 9;
+            goto exit;
         }
     }
     else
@@ -585,7 +595,8 @@
         if (mapBlockPut(&worldMap, mapBlocks[i], offsetX + mapBlocks[i]->xc, offsetY + mapBlocks[i]->yc) < 0)
         {
             THERR("Initial map mapBlockPut() failed!\n");
-            exit(9);
+            res = 9;
+            goto exit;
         }
         mapBlocks[i]->mark = TRUE;
     }
@@ -704,7 +715,9 @@
     else
     {
         THERR("No map generated?\n");
+        res = 6;
     }
 
-    return 0;
+exit:
+    return res;
 }