comparison sidinfo.c @ 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 3b922a79aa2d
children d54112385845
comparison
equal deleted inserted replaced
348:9bf282a79ac4 349:a6153837c138
976 (void) err; 976 (void) err;
977 THERR("%s - %s\n", msg, fh->filename); 977 THERR("%s - %s\n", msg, fh->filename);
978 } 978 }
979 979
980 980
981 void siSTILError(th_ioctx *fh, const int err, const char *msg)
982 {
983 (void) err;
984 THERR("[%s:%" PRIu_SIZE_T "] %s\n", fh->filename, fh->line, msg);
985 }
986
987
988 BOOL siHandleSIDFile(const char *filename) 981 BOOL siHandleSIDFile(const char *filename)
989 { 982 {
990 SIDLibPSIDHeader *psid = NULL; 983 SIDLibPSIDHeader *psid = NULL;
991 th_ioctx *infh = NULL; 984 th_ioctx *infh = NULL;
992 FILE *outfh; 985 FILE *outfh;
1158 } 1151 }
1159 1152
1160 1153
1161 int main(int argc, char *argv[]) 1154 int main(int argc, char *argv[])
1162 { 1155 {
1163 th_ioctx *infh = NULL;
1164 char *setLang = getenv("LANG"); 1156 char *setLang = getenv("LANG");
1165 int ret; 1157 int res;
1166 1158
1167 // Get HVSC_BASE env variable if it is set 1159 // Get HVSC_BASE env variable if it is set
1168 th_pstr_cpy(&setHVSCPath, getenv("HVSC_BASE")); 1160 th_pstr_cpy(&setHVSCPath, getenv("HVSC_BASE"));
1169 1161
1170 // Initialize 1162 // Initialize
1206 argShowHelp(); 1198 argShowHelp();
1207 goto exit; 1199 goto exit;
1208 } 1200 }
1209 1201
1210 // Initialize character set conversion 1202 // Initialize character set conversion
1211 if ((ret = sidutil_chconv_init(&setChConv, setLang)) != THERR_OK) 1203 if ((res = sidutil_chconv_init(&setChConv, setLang)) != THERR_OK)
1212 { 1204 {
1213 THERR("Could not initialize character set conversion (LANG='%s'): %s\n", 1205 THERR("Could not initialize character set conversion (LANG='%s'): %s\n",
1214 setLang, th_error_str(ret)); 1206 setLang, th_error_str(res));
1215 } 1207 }
1216 1208
1217 THMSG(2, "Requested output LANG='%s', use charset conversion=%s\n", 1209 THMSG(2, "Requested output LANG='%s', use charset conversion=%s\n",
1218 setChConv.outLang, setChConv.enabled ? "yes" : "no"); 1210 setChConv.outLang, setChConv.enabled ? "yes" : "no");
1219 1211
1245 } 1237 }
1246 1238
1247 // Read SLDB and STILDB 1239 // Read SLDB and STILDB
1248 if (setSLDBPath != NULL) 1240 if (setSLDBPath != NULL)
1249 { 1241 {
1250 // Initialize SLDB
1251 setSLDBNewFormat = th_strrcasecmp(setSLDBPath, ".md5") != NULL; 1242 setSLDBNewFormat = th_strrcasecmp(setSLDBPath, ".md5") != NULL;
1252
1253 if ((ret = th_io_fopen(&infh, &th_stdio_io_ops, setSLDBPath, "r")) != THERR_OK)
1254 {
1255 THERR("Could not open SLDB '%s': %s\n",
1256 setSLDBPath, th_error_str(ret));
1257 goto err1;
1258 }
1259
1260 th_io_set_handlers(infh, siSTILError, NULL);
1261 1243
1262 THMSG(1, "Reading SLDB (%s format [%s]): %s\n", 1244 THMSG(1, "Reading SLDB (%s format [%s]): %s\n",
1263 setSLDBNewFormat ? "new" : "old", 1245 setSLDBNewFormat ? "new" : "old",
1264 setSLDBNewFormat ? ".md5" : ".txt", 1246 setSLDBNewFormat ? ".md5" : ".txt",
1265 setSLDBPath); 1247 setSLDBPath);
1266 1248
1267 if ((ret = sidlib_sldb_new(&sidSLDB)) != THERR_OK) 1249 if ((res = sidutil_read_sldb_file(setSLDBPath, &sidSLDB)) != THERR_OK)
1268 { 1250 goto exit;
1269 THERR("Could not allocate SLDB database structure: %s\n",
1270 th_error_str(ret));
1271 goto err1;
1272 }
1273
1274 if ((ret = sidlib_sldb_read(infh, sidSLDB)) != THERR_OK)
1275 {
1276 THERR("Error parsing SLDB: %s\n",
1277 th_error_str(ret));
1278 goto err1;
1279 }
1280
1281 if ((ret = sidlib_sldb_build_index(sidSLDB)) != THERR_OK)
1282 {
1283 THERR("Error building SLDB index: %s\n",
1284 th_error_str(ret));
1285 goto err1;
1286 }
1287
1288 err1:
1289 th_io_free(infh);
1290 infh = NULL;
1291 } 1251 }
1292 1252
1293 if (setSTILDBPath != NULL) 1253 if (setSTILDBPath != NULL)
1294 { 1254 {
1295 // Initialize STILDB
1296 if ((ret = th_io_fopen(&infh, &th_stdio_io_ops, setSTILDBPath, "r")) != THERR_OK)
1297 {
1298 THERR("Could not open STIL database '%s': %s\n",
1299 setSTILDBPath, th_error_str(ret));
1300 goto err2;
1301 }
1302
1303 th_io_set_handlers(infh, siSTILError, NULL);
1304
1305 THMSG(1, "Reading STIL database: %s\n", 1255 THMSG(1, "Reading STIL database: %s\n",
1306 setSTILDBPath); 1256 setSTILDBPath);
1307 1257
1308 if ((ret = sidlib_stildb_new(&sidSTILDB)) != THERR_OK) 1258 if ((res = sidutil_read_stildb_file(setSTILDBPath, &sidSTILDB, NULL)) != THERR_OK)
1309 { 1259 goto exit;
1310 THERR("Could not allocate STIL database structure: %s\n",
1311 th_error_str(ret));
1312 goto err2;
1313 }
1314
1315 if ((ret = sidlib_stildb_read(infh, sidSTILDB, NULL)) != THERR_OK)
1316 {
1317 THERR("Error parsing STIL: %s\n",
1318 th_error_str(ret));
1319 goto err2;
1320 }
1321
1322 if ((ret = sidlib_stildb_build_index(sidSTILDB)) != THERR_OK)
1323 {
1324 THERR("Error building STIL index: %s\n",
1325 th_error_str(ret));
1326 goto err2;
1327 }
1328
1329 err2:
1330 th_io_free(infh);
1331 infh = NULL;
1332 } 1260 }
1333 1261
1334 // Process files 1262 // Process files
1335 if (!th_args_process(argc, argv, optList, optListN, 1263 if (!th_args_process(argc, argv, optList, optListN,
1336 NULL, argHandleFile, OPTH_ONLY_OTHER)) 1264 NULL, argHandleFile, OPTH_ONLY_OTHER))