changeset 2608:ca5cc3978926

Implement -W / --warnings and --warn-loc options in mkloc, to enable output of warnings.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 16 Feb 2024 14:48:17 +0200
parents f08dfd76b456
children f69cc460d9f1
files src/mkloc.c
diffstat 1 files changed, 89 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/mkloc.c	Fri Feb 16 12:27:55 2024 +0200
+++ b/src/mkloc.c	Fri Feb 16 14:48:17 2024 +0200
@@ -25,6 +25,17 @@
     GMAPS_LAST
 };
 
+enum
+{
+    WARN_NONE                = 0x0000,
+    WARN_MARKER_INVALID      = 0x0001,
+    WARN_MARKER_MISSING      = 0x0002,
+    WARN_CREATORS            = 0x0004,
+    WARN_TIMESTAMPS          = 0x0008,
+    WARN_ALL                 = 0xffff
+};
+
+
 // These must be in lower case
 static const char *gmapsModes[GMAPS_LAST] =
 {
@@ -44,7 +55,9 @@
 bool    optGetUpdateLoc = false,
         optNoLabels = false,
         optNoAdjust = false,
-        optLabelType = false;
+        optLabelType = false,
+        optWarningsToLoc = false;
+int     optWarnings = WARN_NONE;
 int     optOutput = OUTFMT_MAP,
         optGMapsMode = -1;
 float   optScale = -1,
@@ -77,6 +90,9 @@
     { 16,'N', "no-adjust",   "No label adjustment", OPT_NONE },
     { 18,'t', "type-prefix", "Prepend labels with type prefix", OPT_NONE },
     { 19,'X', "markers",     "Location markers ('" LOC_MARKERS "')", OPT_ARGREQ },
+    { 22,'W', "warnings",    "Output warnings about: "
+                             "none, all, timestamps, creators", OPT_ARGREQ },
+    { 23,  0, "warn-loc",    "Output warnings to loc file instead of stderr", OPT_NONE },
 };
 
 static const int optListN = sizeof(optList) / sizeof(optList[0]);
@@ -240,6 +256,31 @@
         THMSG(2, "GMaps output mode '%s' selected\n", gmapsModes[optGMapsMode]);
         break;
 
+    case 22:
+        if (th_strcasecmp(optArg, "none") == 0)
+            optWarnings = WARN_NONE;
+        else
+        if (th_strcasecmp(optArg, "all") == 0)
+            optWarnings = WARN_ALL;
+        else
+        switch (tolower(optArg[0]))
+        {
+            case 't':
+                optWarnings |= WARN_TIMESTAMPS;
+                break;
+            case 'c':
+                optWarnings |= WARN_CREATORS;
+                break;
+            default:
+                THERR("Invalid argument to -W: '%s'\n", optArg);
+                return false;
+        }
+        break;
+
+    case 23:
+        THMSG(2, "Outputting warnings to loc file.\n");
+        optWarningsToLoc = true;
+        break;
 
     default:
         THERR("Unknown option '%s'.\n", currArg);
@@ -631,6 +672,35 @@
 }
 
 
+void printLocWarning(bool *first, FILE *fh, const LocMarker *loc, const char *msg)
+{
+    FILE *outFH = optWarningsToLoc ? fh : stderr;
+    if (first)
+    {
+        // Get continent name
+        char *csep, *continent = th_strdup(
+            (loc->file->continent != NULL) ?
+            loc->file->continent : loc->file->filename);
+
+        // Remove filename extension, if found
+        if (continent != NULL &&
+            (csep = strstr(continent, ".loc")) != NULL &&
+            csep[4] == 0)
+            *csep = 0;
+
+        fprintf(outFH, "\n# '%s' @ go %d,%d,%s\n",
+            loc->names[0].name,
+            loc->ox + 1, loc->oy + 1,
+            continent);
+
+        th_free(continent);
+    }
+
+    fprintf(outFH, "# - %s\n", msg);
+    *first = false;
+}
+
+
 void outputLocationFile(FILE *outFile, MapLocations *lp)
 {
     // Output header
@@ -650,19 +720,27 @@
         LocMarker *loc = lp->locations[i];
 
         // Add comment in few cases
-        if (loc->flags & LOCF_Q_MASK)
+        if (optWarnings)
         {
-            char *s = NULL;
-            if (loc->flags & LOCF_NOMARKER)
-                s = "Location missing marker";
-            else
-            if (loc->flags & LOCF_INVALID)
-                s = "Possibly invalid location";
+            bool first = false;
+            if ((optWarnings & WARN_MARKER_INVALID) &&
+                (loc->flags & LOCF_INVALID))
+                printLocWarning(&first, outFile, loc, "Possibly invalid location marker");
+
+            if ((optWarnings & WARN_MARKER_MISSING) &&
+                (loc->flags & LOCF_NOMARKER))
+                printLocWarning(&first, outFile, loc, "Location missing marker");
 
-            if (s)
+            // The next warnings apply only to visible non-pcity / non-shrine
+            if ((loc->flags & LOCF_INVIS) == 0 &&
+                (loc->flags & LOCF_M_PCITY) == 0 &&
+                (loc->flags & LOCF_T_SHRINE) == 0)
             {
-                fprintf(outFile, "\n# %s #%d: %s\n",
-                    s, i, loc->names[0].name);
+                if ((optWarnings & WARN_TIMESTAMPS) && !loc->valid)
+                    printLocWarning(&first, outFile, loc, "No timestamp");
+
+                if ((optWarnings & WARN_CREATORS) && loc->ncreators == 0)
+                    printLocWarning(&first, outFile, loc, "No creators listed");
             }
         }