changeset 1861:f865ee78711e

Modularize a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 02 Nov 2017 15:40:20 +0200
parents 0d09c435ad0f
children 53214478fa6e
files mapsearch.c
diffstat 1 files changed, 79 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/mapsearch.c	Thu Nov 02 15:05:34 2017 +0200
+++ b/mapsearch.c	Thu Nov 02 15:40:20 2017 +0200
@@ -479,28 +479,6 @@
 }
 
 
-void mapSigHandler(uv_signal_t *watcher, int signum)
-{
-    (void) signum;
-
-    THERR("Signal %d caught, exiting...\n", watcher->signum);
-
-    switch (watcher->signum)
-    {
-        case SIGTERM:
-        case SIGINT:
-            break;
-
-        default:
-            signal(SIGABRT, SIG_DFL);
-            abort();
-            break;
-    }
-
-    lws_libuv_stop(setLWSContext);
-}
-
-
 void mapBlockGetDimensions(const unsigned char *data, const size_t len, int *width, int *height)
 {
     size_t offs = 0;
@@ -1072,6 +1050,81 @@
 }
 
 
+BOOL mapLoadMaps(void)
+{
+    THMSG(1, "Trying to load %d map specs. World origin at [%d, %d].\n",
+        optNMaps, optWorldXC, optWorldYC);
+
+    for (int n = 0; n < optNMaps; n++)
+    {
+        MAPInfoCtx *info = &optMaps[n];
+        FILE *fh;
+
+        THMSG(1, "Map ID '%s', data '%s', locations '%s' at [%d, %d]\n",
+            info->locFile.continent,
+            info->mapFilename,
+            info->locFile.filename,
+            info->locFile.x, info->locFile.y);
+
+        if ((info->map = mapBlockParseFile(info->mapFilename, FALSE)) == NULL)
+        {
+            THERR("Could not read map data file '%s'.\n", info->mapFilename);
+            return FALSE;
+        }
+
+        if ((fh = fopen(info->locFile.filename, "rb")) == NULL)
+        {
+            THERR("Could not open location file '%s' for reading.\n",
+                info->locFile.filename);
+            return FALSE;
+        }
+
+        if (!locParseLocStream(fh, &info->locFile, &(info->loc), info->locFile.x, info->locFile.y))
+        {
+            fclose(fh);
+            return FALSE;
+        }
+
+        fclose(fh);
+    }
+
+    return TRUE;
+}
+
+
+void mapFreeMaps(void)
+{
+    for (int n = 0; n < optNMaps; n++)
+    {
+        MAPInfoCtx *info = &optMaps[n];
+
+        mapBlockFree(info->map);
+        locFreeMapLocations(&info->loc);
+    }
+}
+
+
+void mapSigHandler(uv_signal_t *watcher, int signum)
+{
+    (void) signum;
+
+    switch (watcher->signum)
+    {
+        case SIGTERM:
+        case SIGINT:
+            THERR("Signal %d caught, exiting...\n", watcher->signum);
+            lws_libuv_stop(setLWSContext);
+            break;
+
+        default:
+            THERR("Signal %d caught, aborting...\n", watcher->signum);
+            signal(SIGABRT, SIG_DFL);
+            abort();
+            break;
+    }
+}
+
+
 int main(int argc, char *argv[])
 {
     // Initialize
@@ -1100,41 +1153,8 @@
     }
 
     // Load maps
-    THMSG(1, "Trying to load %d map specs. World origin at [%d, %d].\n",
-        optNMaps, optWorldXC, optWorldYC);
-
-    for (int n = 0; n < optNMaps; n++)
-    {
-        MAPInfoCtx *info = &optMaps[n];
-        FILE *fh;
-
-        THMSG(1, "Map ID '%s', data '%s', locations '%s' at [%d, %d]\n",
-            info->locFile.continent,
-            info->mapFilename,
-            info->locFile.filename,
-            info->locFile.x, info->locFile.y);
-
-        if ((info->map = mapBlockParseFile(info->mapFilename, FALSE)) == NULL)
-        {
-            THERR("Could not read map data file '%s'.\n", info->mapFilename);
-            goto exit;
-        }
-
-        if ((fh = fopen(info->locFile.filename, "rb")) == NULL)
-        {
-            THERR("Could not open location file '%s' for reading.\n",
-                info->locFile.filename);
-            goto exit;
-        }
-
-        if (!locParseLocStream(fh, &info->locFile, &(info->loc), info->locFile.x, info->locFile.y))
-        {
-            fclose(fh);
-            goto exit;
-        }
-
-        fclose(fh);
-    }
+    if (!mapLoadMaps())
+        goto exit;
 
     // Check for test mode
     if (optTest != NULL)
@@ -1262,6 +1282,8 @@
     for (int n = 0; n < optNListenTo; n++)
         mapFreeListenCtx(optListenTo[n]);
 
+    mapFreeMaps();
+
     for (int n = 0; n < optNMaps; n++)
     {
         MAPInfoCtx *info = &optMaps[n];
@@ -1269,9 +1291,6 @@
         th_free(info->mapFilename);
         th_free(info->locFile.filename);
         th_free(info->locFile.continent);
-
-        mapBlockFree(info->map);
-        locFreeMapLocations(&info->loc);
     }
 
     th_free(setLWSBuffer);