# HG changeset patch # User Matti Hamalainen # Date 1545394858 -7200 # Node ID 5cdcebad734278ef338f9891ea365279ca44f962 # Parent cbcd12f2e1f5e14af32fff388393641043c01521 Autodetect which version of Songlength database file is available (.md5 or .txt), prefer .md5 version. diff -r cbcd12f2e1f5 -r 5cdcebad7342 sidinfo.c --- 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); }