changeset 1815:7ac487466456

Fixes to the block parsing.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 30 Oct 2017 02:53:05 +0200
parents daf7dcc635d6
children 89829bd4122f
files mapsearch.c
diffstat 1 files changed, 17 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/mapsearch.c	Mon Oct 30 01:45:23 2017 +0200
+++ b/mapsearch.c	Mon Oct 30 02:53:05 2017 +0200
@@ -454,92 +454,55 @@
 {
     size_t offs = 0;
     int x = 0, x2 = 0;
-    BOOL flag = TRUE;
 
-    *height = 0;
-    *width = -1;
+    *width = *height = 0;
 
     while (offs < len)
     {
-        const unsigned char c = data[offs++];
-        if (c == '\n')
+        const unsigned char ch = data[offs++];
+        if (ch == '\n')
         {
-            if (x == 0)
-                return;
-
             if (x > *width)
                 *width = x;
 
+            (*height)++;
             x = x2 = 0;
-            flag = TRUE;
         }
         else
         {
-            if (flag)
-            {
-                (*height)++;
-                flag = FALSE;
-            }
             x2++;
-            if (c != ' ')
+            if (ch != ' ')
                 x = x2;
         }
     }
 
+    if (x > *width)
+        *width = x;
+
     if (x > 0)
         (*height)++;
-
-    if (x > *width)
-        *width = x;
 }
 
 
 BOOL mapBlockParse(const unsigned char *data, const size_t len, MapBlock *res)
 {
-    unsigned char *dp = res->data;
     size_t offs = 0;
-    BOOL flag = FALSE;
-    int x = 0, y = 0;
-
-    while (offs < len && y < res->height)
-    {
-        const unsigned char c = data[offs++];
-        if (c == '\n')
-        {
-            if (x == 0)
-                return TRUE;
-            else
-            if (x != res->width)
-            {
-                THERR("Broken block line #%d width %d != %d!\n",
-                    y, x, res->width);
 
-                return FALSE;
-            }
-            flag = TRUE;
-        }
-        else
+    for (int yc = 0; yc < res->height; yc++)
+    {
+        unsigned char *dp = res->data + (yc * res->scansize);
+        for (int xc = 0; xc < res->width; xc++)
         {
-            if (flag)
+            if (offs < len)
             {
-                x = 0;
-                y++;
-                dp = res->data + (y * res->scansize);
-                flag = FALSE;
-            }
-            dp[x++] = c;
-
-            if (x > res->scansize)
-            {
-                THERR("Broken block line #%d width %d > scansize %d!\n",
-                    y, x, res->scansize);
-
-                return FALSE;
+                dp[xc] = data[offs++];
             }
         }
+        while (offs < len && data[offs] != '\n')
+            offs++;
     }
 
-    return TRUE;
+    return offs == len;
 }