changeset 2208:784ff0c54a7d

Some code cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 14 Oct 2019 09:58:02 +0300
parents 24b60de1b850
children e37dd763d329
files diffmap.c patchmap.c
diffstat 2 files changed, 42 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/diffmap.c	Mon Oct 14 09:51:29 2019 +0300
+++ b/diffmap.c	Mon Oct 14 09:58:02 2019 +0300
@@ -180,9 +180,13 @@
 
 int main(int argc, char *argv[])
 {
-    MapBlock *map1, *map2, *res;
-    FILE *outFile;
+    MapBlock
+        *srcMap1 = NULL,
+        *srcMap2 = NULL,
+        *resMap = NULL;
+    FILE *outFile = NULL;
     size_t count;
+    int res = 0;
 
     // Initialize
     th_init("diffmap", "Create a diff between two ASCII mapfiles", "0.5", NULL, NULL);
@@ -191,34 +195,42 @@
     // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, TRUE))
-        exit(1);
+    {
+        res = 1;
+        goto exit;
+    }
 
     if (srcFilename1 == NULL || srcFilename2 == NULL)
     {
         THERR("Nothing to do. (try --help)\n");
-        exit(0);
+        res = 1;
+        goto exit;
     }
 
     // Read mapfiles
-    if ((map1 = mapBlockParseFile(srcFilename1, FALSE)) == NULL)
-        exit(2);
-
-    if ((map2 = mapBlockParseFile(srcFilename2, FALSE)) == NULL)
-        exit(2);
+    if ((srcMap1 = mapBlockParseFile(srcFilename1, FALSE)) == NULL ||
+        (srcMap2 = mapBlockParseFile(srcFilename2, FALSE)) == NULL)
+    {
+        res = 2;
+        goto exit;
+    }
 
     // Compute and output data
     count = 0;
-    if ((res = diffBlocks(map1, map2, optUseOldFormat, optFilter1, optFilter2, &count)) == NULL)
+    if ((resMap = diffBlocks(srcMap1, srcMap2,
+        optUseOldFormat, optFilter1, optFilter2, &count)) == NULL)
     {
         THERR("Could not create diff between inputs!\n");
-        exit(3);
+        res = 3;
+        goto exit;
     }
     THMSG(1, "%ld differences.\n", count);
 
     // Open output file
     if (optAlwaysDiff || count > 0)
     {
-        THMSG(2, "Outputting map %d x %d...\n", res->width, res->height);
+        THMSG(2, "Outputting map diff %d x %d...\n",
+            resMap->width, resMap->height);
 
         if (destFilename == NULL)
             outFile = stdout;
@@ -226,20 +238,30 @@
         if ((outFile = fopen(destFilename, "wb")) == NULL)
         {
             THERR("Error opening output file '%s'!\n",
-            destFilename);
-            exit(1);
+                destFilename);
+            res = 6;
+            goto exit;
         }
 
         fprintf(outFile, DIFF_MAGIC "%d\n", DIFF_VERSION);
-        mapBlockPrintRaw(outFile, res);
+        mapBlockPrintRaw(outFile, resMap);
 
-        fclose(outFile);
     }
     else
     {
         THMSG(2, "Not outputting diff as no changes were found.\n");
+        res = 4;
+        goto exit;
     }
 
-    exit(count);
-    return count;
+
+exit:
+    if (outFile != NULL)
+        fclose(outFile);
+
+    mapBlockFree(srcMap1);
+    mapBlockFree(srcMap2);
+    mapBlockFree(resMap);
+
+    return res;
 }
--- a/patchmap.c	Mon Oct 14 09:51:29 2019 +0300
+++ b/patchmap.c	Mon Oct 14 09:58:02 2019 +0300
@@ -188,9 +188,10 @@
     }
 
     mapBlockPrint(outFile, map);
-    fclose(outFile);
 
 exit:
+    if (outFile != NULL)
+        fclose(outFile);
 
     mapBlockFree(map);
     mapBlockFree(patch);