Mercurial > hg > sidinfo
changeset 349:a6153837c138
Split HVSC SLDB and STILDB reading boilerplate code from sidinfo to sidutil.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 19 Jan 2020 04:38:29 +0200 |
parents | 9bf282a79ac4 |
children | c2ebcb0f0d62 |
files | sidinfo.c sidutil.c sidutil.h |
diffstat | 3 files changed, 101 insertions(+), 79 deletions(-) [+] |
line wrap: on
line diff
--- a/sidinfo.c Thu Jan 16 22:41:13 2020 +0200 +++ b/sidinfo.c Sun Jan 19 04:38:29 2020 +0200 @@ -978,13 +978,6 @@ } -void siSTILError(th_ioctx *fh, const int err, const char *msg) -{ - (void) err; - THERR("[%s:%" PRIu_SIZE_T "] %s\n", fh->filename, fh->line, msg); -} - - BOOL siHandleSIDFile(const char *filename) { SIDLibPSIDHeader *psid = NULL; @@ -1160,9 +1153,8 @@ int main(int argc, char *argv[]) { - th_ioctx *infh = NULL; char *setLang = getenv("LANG"); - int ret; + int res; // Get HVSC_BASE env variable if it is set th_pstr_cpy(&setHVSCPath, getenv("HVSC_BASE")); @@ -1208,10 +1200,10 @@ } // Initialize character set conversion - if ((ret = sidutil_chconv_init(&setChConv, setLang)) != THERR_OK) + if ((res = sidutil_chconv_init(&setChConv, setLang)) != THERR_OK) { THERR("Could not initialize character set conversion (LANG='%s'): %s\n", - setLang, th_error_str(ret)); + setLang, th_error_str(res)); } THMSG(2, "Requested output LANG='%s', use charset conversion=%s\n", @@ -1247,88 +1239,24 @@ // Read SLDB and STILDB if (setSLDBPath != NULL) { - // Initialize SLDB setSLDBNewFormat = th_strrcasecmp(setSLDBPath, ".md5") != NULL; - if ((ret = th_io_fopen(&infh, &th_stdio_io_ops, setSLDBPath, "r")) != THERR_OK) - { - THERR("Could not open SLDB '%s': %s\n", - setSLDBPath, th_error_str(ret)); - goto err1; - } - - th_io_set_handlers(infh, siSTILError, NULL); - THMSG(1, "Reading SLDB (%s format [%s]): %s\n", setSLDBNewFormat ? "new" : "old", setSLDBNewFormat ? ".md5" : ".txt", setSLDBPath); - if ((ret = sidlib_sldb_new(&sidSLDB)) != THERR_OK) - { - THERR("Could not allocate SLDB database structure: %s\n", - th_error_str(ret)); - goto err1; - } - - if ((ret = sidlib_sldb_read(infh, sidSLDB)) != THERR_OK) - { - THERR("Error parsing SLDB: %s\n", - th_error_str(ret)); - goto err1; - } - - if ((ret = sidlib_sldb_build_index(sidSLDB)) != THERR_OK) - { - THERR("Error building SLDB index: %s\n", - th_error_str(ret)); - goto err1; - } - -err1: - th_io_free(infh); - infh = NULL; + if ((res = sidutil_read_sldb_file(setSLDBPath, &sidSLDB)) != THERR_OK) + goto exit; } if (setSTILDBPath != NULL) { - // Initialize STILDB - if ((ret = th_io_fopen(&infh, &th_stdio_io_ops, setSTILDBPath, "r")) != THERR_OK) - { - THERR("Could not open STIL database '%s': %s\n", - setSTILDBPath, th_error_str(ret)); - goto err2; - } - - th_io_set_handlers(infh, siSTILError, NULL); - THMSG(1, "Reading STIL database: %s\n", setSTILDBPath); - if ((ret = sidlib_stildb_new(&sidSTILDB)) != THERR_OK) - { - THERR("Could not allocate STIL database structure: %s\n", - th_error_str(ret)); - goto err2; - } - - if ((ret = sidlib_stildb_read(infh, sidSTILDB, NULL)) != THERR_OK) - { - THERR("Error parsing STIL: %s\n", - th_error_str(ret)); - goto err2; - } - - if ((ret = sidlib_stildb_build_index(sidSTILDB)) != THERR_OK) - { - THERR("Error building STIL index: %s\n", - th_error_str(ret)); - goto err2; - } - -err2: - th_io_free(infh); - infh = NULL; + if ((res = sidutil_read_stildb_file(setSTILDBPath, &sidSTILDB, NULL)) != THERR_OK) + goto exit; } // Process files
--- a/sidutil.c Thu Jan 16 22:41:13 2020 +0200 +++ b/sidutil.c Sun Jan 19 04:38:29 2020 +0200 @@ -337,3 +337,94 @@ th_free(ctx->outLang); } + + +void sidutil_ioctx_error(th_ioctx *fh, const int err, const char *msg) +{ + (void) err; + THERR("[%s:%" PRIu_SIZE_T "] %s\n", fh->filename, fh->line, msg); +} + + +int sidutil_read_sldb_file(const char *filename, SIDLibSLDB **pdbh) +{ + th_ioctx *infh = NULL; + int res; + + // Initialize SLDB + if ((res = th_io_fopen(&infh, &th_stdio_io_ops, filename, "r")) != THERR_OK) + { + THERR("Could not open SLDB '%s': %s\n", + filename, th_error_str(res)); + goto out; + } + + th_io_set_handlers(infh, sidutil_ioctx_error, NULL); + + if ((res = sidlib_sldb_new(pdbh)) != THERR_OK) + { + THERR("Could not allocate SLDB database structure: %s\n", + th_error_str(res)); + goto out; + } + + if ((res = sidlib_sldb_read(infh, *pdbh)) != THERR_OK) + { + THERR("Error parsing SLDB: %s\n", + th_error_str(res)); + goto out; + } + + if ((res = sidlib_sldb_build_index(*pdbh)) != THERR_OK) + { + THERR("Error building SLDB index: %s\n", + th_error_str(res)); + goto out; + } + +out: + th_io_free(infh); + return res; +} + + +int sidutil_read_stildb_file(const char *filename, SIDLibSTILDB **pdbh, SIDLibChConvCtx *chconv) +{ + th_ioctx *infh = NULL; + int res; + + // Initialize STILDB + if ((res = th_io_fopen(&infh, &th_stdio_io_ops, filename, "r")) != THERR_OK) + { + THERR("Could not open STIL database '%s': %s\n", + filename, th_error_str(res)); + goto out; + } + + th_io_set_handlers(infh, sidutil_ioctx_error, NULL); + + if ((res = sidlib_stildb_new(pdbh)) != THERR_OK) + { + THERR("Could not allocate STIL database structure: %s\n", + th_error_str(res)); + goto out; + } + + if ((res = sidlib_stildb_read(infh, *pdbh, chconv)) != THERR_OK) + { + THERR("Error parsing STIL: %s\n", + th_error_str(res)); + goto out; + } + + if ((res = sidlib_stildb_build_index(*pdbh)) != THERR_OK) + { + THERR("Error building STIL index: %s\n", + th_error_str(res)); + goto out; + } + +out: + th_io_free(infh); + return res; +}
--- a/sidutil.h Thu Jan 16 22:41:13 2020 +0200 +++ b/sidutil.h Sun Jan 19 04:38:29 2020 +0200 @@ -7,6 +7,7 @@ #define SIDUTIL_H 1 #include "th_util.h" +#include "sidlib.h" #ifdef HAVE_ICONV # include <iconv.h> #endif @@ -71,6 +72,8 @@ int sidutil_chconv_init(SIDUtilChConvCtx *ctx, const char *outLang); void sidutil_chconv_close(SIDUtilChConvCtx *ctx); +int sidutil_read_sldb_file(const char *filename, SIDLibSLDB **pdbh); +int sidutil_read_stildb_file(const char *filename, SIDLibSTILDB **pdbh, SIDLibChConvCtx *chconv); #ifdef __cplusplus }