changeset 1801:03db1dc33af9

Handle "no matches" differently, also fix a memory leak.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 29 Oct 2017 22:10:08 +0200
parents 7c7f24e27a60
children 649998a9b6d8
files mapsearch.c
diffstat 1 files changed, 22 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/mapsearch.c	Sun Oct 29 21:54:12 2017 +0200
+++ b/mapsearch.c	Sun Oct 29 22:10:08 2017 +0200
@@ -779,38 +779,35 @@
 out:
     mapBlockFree(pattern);
 
+    // If an error occured, bail out now
     if (*verr != NULL)
         return;
 
-    if (nmatches > 0)
-    {
-        // We got some matches, output them as a JSON array
-        char *buf = NULL;
-        size_t bufLen = 0, bufSize = 0;
+    // We got some matches, output them as a JSON array
+    char *buf = NULL;
+    size_t bufLen = 0, bufSize = 0;
 
-        th_strbuf_puts(&buf, &bufSize, &bufLen, "RESULT:[");
+    th_strbuf_puts(&buf, &bufSize, &bufLen, "RESULT:[");
 
-        for (int n = 0; n < nmatches; n++)
-        {
-            MAPMatch *match = &matches[n];
-            char *vstr = th_strdup_printf(
-                "[%1.2f,%d,\"%s\",%d,%d,%d,%d]%s",
-                match->accuracy,
-                match->centered,
-                match->map,
-                match->mx, match->my,
-                match->wx, match->wy,
-                (n < nmatches - 1) ? "," : "");
+    for (int n = 0; n < nmatches; n++)
+    {
+        MAPMatch *match = &matches[n];
+        char *vstr = th_strdup_printf(
+            "[%1.2f,%d,\"%s\",%d,%d,%d,%d]%s",
+            match->accuracy,
+            match->centered,
+            match->map,
+            match->mx, match->my,
+            match->wx, match->wy,
+            (n < nmatches - 1) ? "," : "");
 
-            th_strbuf_puts(&buf, &bufSize, &bufLen, vstr);
-            th_free(vstr);
-        }
+        th_strbuf_puts(&buf, &bufSize, &bufLen, vstr);
+        th_free(vstr);
+    }
 
-        th_strbuf_puts(&buf, &bufSize, &bufLen, "]");
-        lws_write(wsi, (unsigned char *) buf, bufLen, LWS_WRITE_TEXT);
-    }
-    else
-        *verr = "No matches found.";
+    th_strbuf_puts(&buf, &bufSize, &bufLen, "]");
+    lws_write(wsi, (unsigned char *) buf, bufLen, LWS_WRITE_TEXT);
+    th_free(buf);
 }