changeset 208:5cdcebad7342

Autodetect which version of Songlength database file is available (.md5 or .txt), prefer .md5 version.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 21 Dec 2018 14:20:58 +0200
parents cbcd12f2e1f5
children c073c7eda531
files sidinfo.c
diffstat 1 files changed, 26 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/sidinfo.c	Tue Dec 11 02:40:36 2018 +0200
+++ b/sidinfo.c	Fri Dec 21 14:20:58 2018 +0200
@@ -19,7 +19,7 @@
 #define SET_DEF_CHARSET   "utf8" // NOTE! Do not change unless you are using iconv()!!
                                  // The fallback converter does not handle other encodings.
 
-#define SET_SLDB_FILENAME "Songlengths.md5"
+#define SET_SLDB_FILEBASE "Songlengths"
 
 
 enum
@@ -134,7 +134,7 @@
     { 4, 'x', "hex",        "Use hexadecimal values", OPT_NONE },
     { 7, 'F', "format",     "Use given format string (see below)", OPT_ARGREQ },
     { 8, 'H', "hvsc",       "Specify path to HVSC documents directory", OPT_ARGREQ },
-    { 9, 'S', "sldb",       "Specify Songlengths.txt file (use -H if possible)", OPT_ARGREQ },
+    { 9, 'S', "sldb",       "Specify Songlengths.(txt|md5) file (use -H if possible)", OPT_ARGREQ },
     {12, 'R', "recurse",    "Recurse into sub-directories", OPT_NONE },
 };
 
@@ -1172,6 +1172,22 @@
 }
 
 
+char *siCheckHVSCFilePath(const char *filebase, const char *fext)
+{
+    th_stat_data sdata;
+    char *npath = th_strdup_printf("%s%c%s%s", setHVSCPath, TH_DIR_SEPARATOR, filebase, fext);
+
+    if (npath != NULL &&
+        th_stat_path(npath, &sdata) &&
+        (sdata.flags & TH_IS_READABLE) &&
+        (sdata.flags & TH_IS_DIR) == 0)
+        return npath;
+
+    th_free(npath);
+    return NULL;
+}
+
+
 int main(int argc, char *argv[])
 {
     char *setLang = th_strdup(getenv("LANG"));
@@ -1243,11 +1259,14 @@
         }
     }
 
-    // Check paths
+    // Check if HVSC path is set
     if (setHVSCPath != NULL)
     {
+        // If SLDB path is not set, autocheck for .md5 and .txt
         if (setSLDBPath == NULL)
-            setSLDBPath = th_strdup_printf("%s%c%s", setHVSCPath, TH_DIR_SEPARATOR, SET_SLDB_FILENAME);
+            setSLDBPath = siCheckHVSCFilePath(SET_SLDB_FILEBASE, ".md5");
+        if (setSLDBPath == NULL)
+            setSLDBPath = siCheckHVSCFilePath(SET_SLDB_FILEBASE, ".txt");
     }
 
     if (setSLDBPath != NULL)
@@ -1255,6 +1274,9 @@
         // Initialize SLDB
         int ret = THERR_OK;
         th_ioctx *inFile = NULL;
+
+        setSLDBNewFormat = th_strrcasecmp(setSLDBPath, ".md5") != NULL;
+
         if ((ret = th_io_fopen(&inFile, &th_stdio_io_ops, setSLDBPath, "r")) != THERR_OK)
         {
             THERR("Could not open SLDB '%s': %s\n",
@@ -1284,8 +1306,6 @@
             goto err;
         }
 
-        setSLDBNewFormat = th_strrcasecmp(setSLDBPath, ".md5") != NULL;
-
 err:
         th_io_close(inFile);
     }