changeset 1741:b4f728ef950c

Cleanup some of the location tag parsing/handling.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 18 Oct 2017 14:23:58 +0300
parents 8cf2c4688a84
children 5dda4803c59b
files colormap.c
diffstat 1 files changed, 47 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/colormap.c	Wed Oct 18 14:21:48 2017 +0300
+++ b/colormap.c	Wed Oct 18 14:23:58 2017 +0300
@@ -348,6 +348,37 @@
 }
 
 
+BOOL getTagStr(FILE *inFile, char *tmpStr, const size_t len, const int endch)
+{
+    size_t i;
+    int mch = EOF;
+
+    for (i = 0; i < len && (mch = fgetc(inFile)) != EOF;)
+    {
+        if (mch == endch)
+            break;
+        else
+            tmpStr[i++] = mch;
+    }
+
+    tmpStr[i] = 0;
+
+    if (mch == EOF)
+    {
+        THERR("Unexpected end of file.\n");
+        return FALSE;
+    }
+    else
+    if (mch != endch)
+    {
+        THERR("No end tag 0x%02x found.\n", endch);
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+
 BOOL processData(FILE *inFile, FILE *outFile, CMapOutFormat *fmt)
 {
     int k, currColor, prevColor;
@@ -363,59 +394,38 @@
         else
         if (k == 0xff)
         {
-            char tmpStr[MAXSTR+1], tmpStr2[MAXSTR+1];
-            int i;
+            char tmpStr[MAXSTR], tmpStr2[MAXSTR];
+            int mcol;
 
             // Location title mode
             checkEndTag(outFile, fmt, prevColor);
             currColor = prevColor = -2;
 
-            /* Location marker tag */
-            for (i = 0; i < MAXSTR && (k = fgetc(inFile)) != EOF;)
-            {
-                if (k == 0xfc)
-                    break;
-                else
-                    tmpStr[i++] = k;
-            }
-
-            tmpStr[i] = 0;
+            // Get location marker tag
+            if (!getTagStr(inFile, tmpStr, MAXSTR, 0xfc))
+                return FALSE;
 
-            if (k != 0xfc)
-            {
-                THERR("Unexpected end of file (location tag name).\n");
-                return FALSE;
-            }
-
-
-            /* Get colour */
-            k = fgetc(inFile);
-            if (k == EOF)
+            // Get color
+            mcol = fgetc(inFile);
+            if (mcol == EOF)
             {
                 THERR("Unexpected end of file (location tag colour).\n");
                 return FALSE;
             }
 
-            if (!fmt->putTagLocation && fmt->putTagStart)
-                fmt->putTagStart(outFile, -k - 1);
-
-            if (fmt->putTagLocation)
-                fmt->putTagLocation(outFile, tmpStr, k);
-
-            for (i = 0; i < MAXSTR && (k = fgetc(inFile)) != EOF;) {
-                if (k == 0xfe)
-                    break;
-                else
-                    tmpStr2[i++] = k;
-            }
-            tmpStr2[i] = 0;
-
-            if (k != 0xfe)
+            // Get location name
+            if (!getTagStr(inFile, tmpStr2, MAXSTR, 0xfe))
             {
                 THERR("Expected location tag '%s' end, but did not find one.\n", tmpStr);
                 return FALSE;
             }
 
+            if (!fmt->putTagLocation && fmt->putTagStart)
+                fmt->putTagStart(outFile, -mcol - 1);
+
+            if (fmt->putTagLocation)
+                fmt->putTagLocation(outFile, tmpStr, mcol);
+
             if (fmt->putString)
                 fmt->putString(tmpStr2, outFile);
             else