changeset 2077:8fa055824aa7

Clean up block parsing a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 21 Aug 2019 14:48:49 +0300
parents 39b008bd2c47
children 6e8ecda15f84
files combine.c
diffstat 1 files changed, 40 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/combine.c	Wed Aug 21 14:47:22 2019 +0300
+++ b/combine.c	Wed Aug 21 14:48:49 2019 +0300
@@ -14,16 +14,16 @@
 typedef struct
 {
     char *fileName;
-    int x, y;
-    MapBlock *b;
-} mapfile_t;
+    int xc, yc;
+    MapBlock *blk;
+} MapFile;
 
 
 /* Variables
  */
 int         nsrcFiles = 0;
-mapfile_t   srcFiles[SET_MAX_FILES];
-char        *destFile = NULL;
+MapFile     srcFiles[SET_MAX_FILES];
+char        *dstFile = NULL;
 int         optFillChar = -1;
 BOOL        optInputIsDiff = FALSE;
 int         optWorldMinW = 0,
@@ -66,8 +66,8 @@
         break;
 
     case 1:
-        destFile = optArg;
-        THMSG(1, "Output file '%s'\n", destFile);
+        dstFile = optArg;
+        THMSG(1, "Output file '%s'\n", dstFile);
         break;
 
     case 2:
@@ -132,79 +132,80 @@
 
 BOOL argHandleFile(char *currArg)
 {
+    char *tmpArg = NULL;
+
     if (nsrcFiles < SET_MAX_FILES)
     {
-        char *q, *c, *tmpArg = th_strdup(currArg);
+        char *q, *c;
+        MapFile *file = &srcFiles[nsrcFiles++];
 
-        if (!tmpArg)
+        if ((tmpArg = th_strdup(currArg)) == NULL)
         {
             THERR("Could not allocate temp buffer!\n");
-            return FALSE;
+            goto err;
         }
 
         // Get filename component
         for (q = c = tmpArg; *c && (*c != ':'); c++);
         if (*c != ':')
         {
-            th_free(tmpArg);
             THERR("Expected ':' after filename in '%s'.\n",
                 currArg);
-            return FALSE;
+            goto err;
         }
 
         *(c++) = 0;
-        srcFiles[nsrcFiles].fileName = th_strdup(q);
+        file->fileName = th_strdup(q);
 
         // Get X offset
         if (!th_isdigit(*c) && *c != '-')
         {
-            th_free(tmpArg);
             THERR("Expected decimal X offset value in '%s'.\n", currArg);
-            return FALSE;
+            goto err;
         }
 
         q = c; if (*c == '-') c++;
         while (*c && th_isdigit(*c)) c++;
         if (*c != ':')
         {
-            th_free(tmpArg);
             THERR("Expected ':' after X offset value in '%s'.\n", currArg);
-            return FALSE;
+            goto err;
         }
 
         *(c++) = 0;
-        srcFiles[nsrcFiles].x = atoi(q);
+        file->xc = atoi(q);
 
         // Get Y offset
         if (!th_isdigit(*c) && *c != '-')
         {
-            th_free(tmpArg);
             THERR("Expected decimal Y offset value in '%s'.\n", currArg);
-            return FALSE;
+            goto err;
         }
 
-        q = c; if (*c == '-') c++;
+        q = c;
+        if (*c == '-') c++;
+
         while (*c && th_isdigit(*c)) c++;
         if (*c != 0)
         {
-            th_free(tmpArg);
             THERR("Invalid Y offset value in '%s'.\n", currArg);
-            return FALSE;
+            goto err;
         }
 
-        srcFiles[nsrcFiles].y = atoi(q);
-
-        th_free(tmpArg);
-
-        nsrcFiles++;
+        file->yc = atoi(q);
     }
     else
     {
         THERR("Too many input files specified (%d max)!\n", SET_MAX_FILES);
-        return FALSE;
+        goto err;
     }
 
+    th_free(tmpArg);
     return TRUE;
+
+err:
+    th_free(tmpArg);
+    return FALSE;
 }
 
 
@@ -248,16 +249,16 @@
      */
     for (i = 0; i < nsrcFiles; i++)
     {
-        if ((srcFiles[i].b = mapBlockParseFile(srcFiles[i].fileName, optInputIsDiff)) == NULL)
+        if ((srcFiles[i].blk = mapBlockParseFile(srcFiles[i].fileName, optInputIsDiff)) == NULL)
         {
             THERR("Error reading input file '%s'!\n",
                 srcFiles[i]);
             exit(1);
         }
 
-        mapBlockClean(srcFiles[i].b, (unsigned char *) " ", 1);
-        srcFiles[i].b->x = srcFiles[i].x;
-        srcFiles[i].b->y = srcFiles[i].y;
+        mapBlockClean(srcFiles[i].blk, (unsigned char *) " ", 1);
+        srcFiles[i].blk->x = srcFiles[i].xc;
+        srcFiles[i].blk->y = srcFiles[i].yc;
     }
 
     THMSG(2, "Total of %d maps read.\n", nsrcFiles);
@@ -272,7 +273,7 @@
     // Get world dimensions
     worldX0 = worldY0 = worldX1 = worldY1 = 0;
     for (i = 0; i < nsrcFiles; i++)
-        findWorldSize(srcFiles[i].b, &worldX0, &worldY0, &worldX1, &worldY1);
+        findWorldSize(srcFiles[i].blk, &worldX0, &worldY0, &worldX1, &worldY1);
 
     worldW = worldX1 - worldX0 + 1;
     worldH = worldY1 - worldY0 + 1;
@@ -328,10 +329,10 @@
     /* Blit map blocks
      */
     for (i = 0; i < nsrcFiles; i++)
-    if (mapBlockPutDo(worldMap, srcFiles[i].b, worldX0 + srcFiles[i].b->x, worldY0 + srcFiles[i].b->y) != 0)
+    if (mapBlockPutDo(worldMap, srcFiles[i].blk, worldX0 + srcFiles[i].blk->x, worldY0 + srcFiles[i].blk->y) != 0)
     {
         THERR("Mapblock #%d ['%s' @ %d,%d] placement failed!\n",
-        i, srcFiles[i].fileName, srcFiles[i].b->x, srcFiles[i].b->y);
+        i, srcFiles[i].fileName, srcFiles[i].blk->x, srcFiles[i].blk->y);
         exit(8);
     }
 
@@ -345,12 +346,12 @@
         THMSG(1, "Outputting generated map of (%d x %d) ...\n",
             worldMap->width, worldMap->height);
 
-        if (destFile == NULL)
+        if (dstFile == NULL)
             tmpFile = stdout;
         else
-        if ((tmpFile = fopen(destFile, "wb")) == NULL)
+        if ((tmpFile = fopen(dstFile, "wb")) == NULL)
         {
-            THERR("Error opening output file '%s'!\n", destFile);
+            THERR("Error opening output file '%s'!\n", dstFile);
             exit(1);
         }