changeset 2493:c43a4607dbc0

Add -D / --data-path option to mapsearch.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 19 Jun 2023 00:59:35 +0300
parents 972cd6162ea9
children 28f3bf45d5d7
files src/mapsearch.c
diffstat 1 files changed, 83 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/src/mapsearch.c	Tue May 16 11:14:20 2023 +0300
+++ b/src/mapsearch.c	Mon Jun 19 00:59:35 2023 +0300
@@ -5,6 +5,7 @@
  */
 #include "th_args.h"
 #include "th_datastruct.h"
+#include "th_file.h"
 #include "liblocfile.h"
 #include "libmaputils.h"
 #include <stdarg.h>
@@ -98,6 +99,7 @@
 char    *optTest = NULL;
 int     optUID = -1, optGID = -1;
 char    *optLogFilename = NULL;
+char    *optDataPath = NULL;
 FILE    *setLogFH = NULL;
 int     optBenchmark = -1;
 
@@ -108,17 +110,18 @@
  */
 static const th_optarg optList[] =
 {
-    { 0, '?', "help",          "Show this help", OPT_NONE },
-    { 1, 'v', "verbose",       "Be more verbose", OPT_NONE },
-    { 2, 'l', "listen",        "Listen to interface (see below)", OPT_ARGREQ },
-    { 3,   0, "ssl-ciphers",   "Specify list of SSL/TLS ciphers", OPT_ARGREQ },
-    { 5, 'w', "world-origin",  "Specify the world origin <x:y> coordinates "
-        "to which the map offsets are relative to", OPT_ARGREQ },
-    { 6, 'T', "test",          "Test search with given file input", OPT_ARGREQ },
-    { 4, 'B', "benchmark",     "Run a benchmark on test input (-T option) for specified number cycles", OPT_ARGREQ },
-    { 7, 'U', "uid",           "Run as UID", OPT_ARGREQ },
-    { 8, 'G', "gid",           "Run as GID", OPT_ARGREQ },
-    { 9, 'L', "log-file",      "Log to specified file", OPT_ARGREQ },
+    {  0, '?', "help",          "Show this help", OPT_NONE },
+    {  1, 'v', "verbose",       "Be more verbose", OPT_NONE },
+    {  2, 'l', "listen",        "Listen to interface (see below)", OPT_ARGREQ },
+    {  3,   0, "ssl-ciphers",   "Specify list of SSL/TLS ciphers", OPT_ARGREQ },
+    {  5, 'w', "world-origin",  "Specify the world origin <x:y> coordinates "
+    "to which the map offsets are relative to", OPT_ARGREQ },
+    {  6, 'T', "test",          "Test search with given file input", OPT_ARGREQ },
+    {  4, 'B', "benchmark",     "Run a benchmark on test input (-T option) for specified number cycles", OPT_ARGREQ },
+    {  7, 'U', "uid",           "Run as UID", OPT_ARGREQ },
+    {  8, 'G', "gid",           "Run as GID", OPT_ARGREQ },
+    {  9, 'L', "log-file",      "Log to specified file", OPT_ARGREQ },
+    { 10, 'D', "data-path",     "Path to data directory", OPT_ARGREQ },
 };
 
 static const int optListN = sizeof(optList) / sizeof(optList[0]);
@@ -553,6 +556,10 @@
         optLogFilename = optArg;
         break;
 
+    case 10:
+        optDataPath = optArg;
+        break;
+
     default:
         mapERR("Unknown option '%s'.\n", currArg);
         return false;
@@ -1629,43 +1636,76 @@
 }
 
 
+bool mapLoadMapsData(void)
+{
+    char *mapFilename = NULL, *locFilename = NULL;
+    FILE *fh = NULL;
+    bool res = false;
+
+    for (int nmap = 0; nmap < optNMaps; nmap++)
+    {
+        MAPInfoCtx *info = &optMaps[nmap];
+
+        if (optDataPath != NULL)
+        {
+            mapFilename = th_strdup_printf("%s%s%s",
+                optDataPath, TH_DIR_SEPARATOR_STR, info->mapFilename);
+            locFilename = th_strdup_printf("%s%s%s",
+                optDataPath, TH_DIR_SEPARATOR_STR, info->locFile.filename);
+        }
+        else
+        {
+            mapFilename = th_strdup(info->mapFilename);
+            locFilename = th_strdup(info->locFile.filename);
+        }
+
+        mapMSG(1, "Map ID '%s', data '%s', locations '%s' at [%d, %d]\n",
+            info->locFile.continent,
+            mapFilename,
+            locFilename,
+            info->locFile.xoffs, info->locFile.yoffs);
+
+        if ((info->map = mapBlockParseFile(mapFilename, false)) == NULL)
+        {
+            mapERR("Could not read map data file '%s'.\n",
+                mapFilename);
+            goto err;
+        }
+
+        if ((fh = fopen(locFilename, "rb")) == NULL)
+        {
+            mapERR("Could not open location file '%s' for reading.\n",
+                locFilename);
+            goto err;
+        }
+
+        if (!locParseLocStream(fh, &info->locFile, &(info->loc), 0, 0))
+            goto err;
+
+        fclose(fh);
+        fh = NULL;
+
+        th_free_r(&mapFilename);
+        th_free_r(&locFilename);
+    }
+
+    res = true;
+
+err:
+    if (fh != NULL)
+        fclose(fh);
+
+    th_free_r(&mapFilename);
+    th_free_r(&locFilename);
+
+    return res;
+}
+
 bool mapLoadMaps(void)
 {
     mapMSG(1, "Trying to load %d map specs. World origin at [%d, %d].\n",
         optNMaps, optWorldXC, optWorldYC);
 
-    for (int nmap = 0; nmap < optNMaps; nmap++)
-    {
-        MAPInfoCtx *info = &optMaps[nmap];
-        FILE *fh;
-
-        mapMSG(1, "Map ID '%s', data '%s', locations '%s' at [%d, %d]\n",
-            info->locFile.continent,
-            info->mapFilename,
-            info->locFile.filename,
-            info->locFile.xoffs, info->locFile.yoffs);
-
-        if ((info->map = mapBlockParseFile(info->mapFilename, false)) == NULL)
-        {
-            mapERR("Could not read map data file '%s'.\n", info->mapFilename);
-            return false;
-        }
-
-        if ((fh = fopen(info->locFile.filename, "rb")) == NULL)
-        {
-            mapERR("Could not open location file '%s' for reading.\n",
-                info->locFile.filename);
-            return false;
-        }
-
-        if (!locParseLocStream(fh, &info->locFile, &(info->loc), 0, 0))
-        {
-            fclose(fh);
-            return false;
-        }
-
-        fclose(fh);
-    }
 
     // Get total number of non-invis locations
     optNMapLocations = 0;