Mercurial > hg > sidinfo
comparison sidinfo.c @ 340:6f8c431a3040
Rename some variables and function arguments.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 13 Jan 2020 16:24:57 +0200 |
parents | 923e63b9653b |
children | fe061ead51cc |
comparison
equal
deleted
inserted
replaced
339:923e63b9653b | 340:6f8c431a3040 |
---|---|
708 | 708 |
709 return TRUE; | 709 return TRUE; |
710 } | 710 } |
711 | 711 |
712 | 712 |
713 static void siPrintFieldPrefixName(FILE *outFile, const char *name, const BOOL multifield) | 713 static void siPrintFieldPrefixName(FILE *outfh, const char *name, const BOOL multifield) |
714 { | 714 { |
715 if (optFieldNamePrefix) | 715 if (optFieldNamePrefix) |
716 { | 716 { |
717 if (optFieldOutput && optOneLineFieldSep == NULL) | 717 if (optFieldOutput && optOneLineFieldSep == NULL) |
718 fprintf(outFile, optParsable ? "%s=" : "%-20s : ", name); | 718 fprintf(outfh, optParsable ? "%s=" : "%-20s : ", name); |
719 else | 719 else |
720 if (multifield) | 720 if (multifield) |
721 fprintf(outFile, "[%s] ", name); | 721 fprintf(outfh, "[%s] ", name); |
722 } | 722 } |
723 } | 723 } |
724 | 724 |
725 | 725 |
726 static void siPrintFieldPrefix(FILE *outFile, const PSFOption *opt) | 726 static void siPrintFieldPrefix(FILE *outfh, const PSFOption *opt) |
727 { | 727 { |
728 siPrintFieldPrefixName(outFile, | 728 siPrintFieldPrefixName(outfh, |
729 (optParsable || opt->lname == NULL) ? opt->name : opt->lname, | 729 (optParsable || opt->lname == NULL) ? opt->name : opt->lname, |
730 FALSE); | 730 FALSE); |
731 } | 731 } |
732 | 732 |
733 | 733 |
734 static void siPrintFieldSeparator(FILE *outFile, const BOOL multifield, const BOOL last) | 734 static void siPrintFieldSeparator(FILE *outfh, const BOOL multifield, const BOOL last) |
735 { | 735 { |
736 if (optFieldOutput) | 736 if (optFieldOutput) |
737 fputs(optOneLineFieldSep != NULL ? optOneLineFieldSep : "\n", outFile); | 737 fputs(optOneLineFieldSep != NULL ? optOneLineFieldSep : "\n", outfh); |
738 else | 738 else |
739 if (multifield && !last) | 739 if (multifield && !last) |
740 fputs(optOneLineFieldSep != NULL ? optOneLineFieldSep : ", ", outFile); | 740 fputs(optOneLineFieldSep != NULL ? optOneLineFieldSep : ", ", outfh); |
741 } | 741 } |
742 | 742 |
743 | 743 |
744 static const char *siGetInfoFormat(const PSFStackItem *item, const int otype) | 744 static const char *siGetInfoFormat(const PSFStackItem *item, const int otype) |
745 { | 745 { |
761 return NULL; | 761 return NULL; |
762 } | 762 } |
763 } | 763 } |
764 | 764 |
765 | 765 |
766 static void siPrintPSIDInfoLine(FILE *outFile, BOOL *shown, | 766 static void siPrintPSIDInfoLine(FILE *outfh, BOOL *shown, |
767 const char *fmt, const int otype, | 767 const char *fmt, const int otype, |
768 const char *d_str, const int d_int, | 768 const char *d_str, const int d_int, |
769 const BOOL convert) | 769 const BOOL convert) |
770 { | 770 { |
771 char *str, *tmp; | 771 char *str, *tmp; |
778 } | 778 } |
779 else | 779 else |
780 tmp = sidutil_escape_string(d_str, optEscapeChars); | 780 tmp = sidutil_escape_string(d_str, optEscapeChars); |
781 | 781 |
782 if ((str = siItemFormatStrPrint(fmt, otype, tmp, d_int)) != NULL) | 782 if ((str = siItemFormatStrPrint(fmt, otype, tmp, d_int)) != NULL) |
783 fputs(str, outFile); | 783 fputs(str, outfh); |
784 | 784 |
785 th_free(str); | 785 th_free(str); |
786 th_free(tmp); | 786 th_free(tmp); |
787 | 787 |
788 *shown = TRUE; | 788 *shown = TRUE; |
789 } | 789 } |
790 | 790 |
791 | 791 |
792 #define PRS(d_str, d_conv) do { \ | 792 #define PRS(d_str, d_conv) do { \ |
793 siPrintFieldPrefix(outFile, opt); \ | 793 siPrintFieldPrefix(outfh, opt); \ |
794 siPrintPSIDInfoLine(outFile, shown, siGetInfoFormat(item, opt->type), opt->type, d_str, -1, d_conv); \ | 794 siPrintPSIDInfoLine(outfh, shown, siGetInfoFormat(item, opt->type), opt->type, d_str, -1, d_conv); \ |
795 siPrintFieldSeparator(outFile, FALSE, TRUE); \ | 795 siPrintFieldSeparator(outfh, FALSE, TRUE); \ |
796 } while (0) | 796 } while (0) |
797 | 797 |
798 #define PRI(d_int) do { \ | 798 #define PRI(d_int) do { \ |
799 siPrintFieldPrefix(outFile, opt); \ | 799 siPrintFieldPrefix(outfh, opt); \ |
800 siPrintPSIDInfoLine(outFile, shown, siGetInfoFormat(item, opt->type), opt->type, NULL, d_int, FALSE); \ | 800 siPrintPSIDInfoLine(outfh, shown, siGetInfoFormat(item, opt->type), opt->type, NULL, d_int, FALSE); \ |
801 siPrintFieldSeparator(outFile, FALSE, TRUE); \ | 801 siPrintFieldSeparator(outfh, FALSE, TRUE); \ |
802 } while (0) | 802 } while (0) |
803 | 803 |
804 | 804 |
805 static void siPrintPSIDInformationField(FILE *outFile, const char *filename, | 805 static void siPrintPSIDInformationField(FILE *outfh, const char *filename, |
806 const SIDLibPSIDHeader *psid, BOOL *shown, const PSFStackItem *item) | 806 const SIDLibPSIDHeader *psid, BOOL *shown, const PSFStackItem *item) |
807 { | 807 { |
808 const PSFOption *opt = &optPSOptions[item->cmd]; | 808 const PSFOption *opt = &optPSOptions[item->cmd]; |
809 char tmp[128]; | 809 char tmp[128]; |
810 | 810 |
884 break; | 884 break; |
885 | 885 |
886 case 22: | 886 case 22: |
887 if (psid->lengths != NULL && psid->lengths->nlengths > 0) | 887 if (psid->lengths != NULL && psid->lengths->nlengths > 0) |
888 { | 888 { |
889 siPrintFieldPrefix(outFile, opt); | 889 siPrintFieldPrefix(outfh, opt); |
890 for (int i = 0; i < psid->lengths->nlengths; i++) | 890 for (int i = 0; i < psid->lengths->nlengths; i++) |
891 { | 891 { |
892 int len = psid->lengths->lengths[i]; | 892 int len = psid->lengths->lengths[i]; |
893 | 893 |
894 snprintf(tmp, sizeof(tmp), "%d:%02d%s", | 894 snprintf(tmp, sizeof(tmp), "%d:%02d%s", |
895 len / 60, len % 60, | 895 len / 60, len % 60, |
896 (i < psid->lengths->nlengths - 1) ? ", " : ""); | 896 (i < psid->lengths->nlengths - 1) ? ", " : ""); |
897 | 897 |
898 siPrintPSIDInfoLine(outFile, shown, | 898 siPrintPSIDInfoLine(outfh, shown, |
899 siGetInfoFormat(item, OTYPE_STR), | 899 siGetInfoFormat(item, OTYPE_STR), |
900 OTYPE_STR, | 900 OTYPE_STR, |
901 tmp, | 901 tmp, |
902 -1, FALSE); | 902 -1, FALSE); |
903 } | 903 } |
904 siPrintFieldSeparator(outFile, FALSE, TRUE); | 904 siPrintFieldSeparator(outfh, FALSE, TRUE); |
905 } | 905 } |
906 break; | 906 break; |
907 | 907 |
908 case 23: | 908 case 23: |
909 if (psid->stil != NULL) | 909 if (psid->stil != NULL) |
948 { | 948 { |
949 snprintf(tmp, sizeof(tmp), "STIL/%s", | 949 snprintf(tmp, sizeof(tmp), "STIL/%s", |
950 sidlib_stil_fields[nfield]); | 950 sidlib_stil_fields[nfield]); |
951 } | 951 } |
952 | 952 |
953 siPrintFieldPrefixName(outFile, tmp, TRUE); | 953 siPrintFieldPrefixName(outfh, tmp, TRUE); |
954 siPrintPSIDInfoLine(outFile, shown, | 954 siPrintPSIDInfoLine(outfh, shown, |
955 siGetInfoFormat(item, OTYPE_STR), | 955 siGetInfoFormat(item, OTYPE_STR), |
956 OTYPE_STR, | 956 OTYPE_STR, |
957 fld->data[nitem], | 957 fld->data[nitem], |
958 -1, TRUE); | 958 -1, TRUE); |
959 | 959 |
960 siPrintFieldSeparator(outFile, TRUE, | 960 siPrintFieldSeparator(outfh, TRUE, |
961 ++nfieldn >= nfieldcount); | 961 ++nfieldn >= nfieldcount); |
962 } | 962 } |
963 } | 963 } |
964 } | 964 } |
965 } | 965 } |
983 | 983 |
984 | 984 |
985 BOOL siHandleSIDFile(const char *filename) | 985 BOOL siHandleSIDFile(const char *filename) |
986 { | 986 { |
987 SIDLibPSIDHeader *psid = NULL; | 987 SIDLibPSIDHeader *psid = NULL; |
988 th_ioctx *inFile = NULL; | 988 th_ioctx *infh = NULL; |
989 FILE *outFile; | 989 FILE *outfh; |
990 BOOL shown = FALSE; | 990 BOOL shown = FALSE; |
991 int res; | 991 int res; |
992 | 992 |
993 outFile = stdout; | 993 outfh = stdout; |
994 | 994 |
995 if ((res = th_io_fopen(&inFile, &th_stdio_io_ops, filename, "rb")) != THERR_OK) | 995 if ((res = th_io_fopen(&infh, &th_stdio_io_ops, filename, "rb")) != THERR_OK) |
996 { | 996 { |
997 THERR("Could not open file '%s': %s\n", | 997 THERR("Could not open file '%s': %s\n", |
998 filename, th_error_str(res)); | 998 filename, th_error_str(res)); |
999 goto error; | 999 goto error; |
1000 } | 1000 } |
1001 | 1001 |
1002 th_io_set_handlers(inFile, siPSIDError, NULL); | 1002 th_io_set_handlers(infh, siPSIDError, NULL); |
1003 | 1003 |
1004 // Read PSID data | 1004 // Read PSID data |
1005 if ((res = sidlib_read_sid_file_alloc(inFile, &psid, setSLDBNewFormat, NULL)) != THERR_OK) | 1005 if ((res = sidlib_read_sid_file_alloc(infh, &psid, setSLDBNewFormat, NULL)) != THERR_OK) |
1006 goto error; | 1006 goto error; |
1007 | 1007 |
1008 // Get songlength information, if any | 1008 // Get songlength information, if any |
1009 if (sidSLDB != NULL) | 1009 if (sidSLDB != NULL) |
1010 psid->lengths = sidlib_sldb_get_by_hash(sidSLDB, psid->hash); | 1010 psid->lengths = sidlib_sldb_get_by_hash(sidSLDB, psid->hash); |
1024 { | 1024 { |
1025 PSFStackItem *item = &optFormat.items[index]; | 1025 PSFStackItem *item = &optFormat.items[index]; |
1026 switch (item->cmd) | 1026 switch (item->cmd) |
1027 { | 1027 { |
1028 case -1: | 1028 case -1: |
1029 sidutil_print_string_escaped(outFile, item->str); | 1029 sidutil_print_string_escaped(outfh, item->str); |
1030 break; | 1030 break; |
1031 | 1031 |
1032 case -2: | 1032 case -2: |
1033 fputc(item->chr, outFile); | 1033 fputc(item->chr, outfh); |
1034 break; | 1034 break; |
1035 | 1035 |
1036 default: | 1036 default: |
1037 siPrintPSIDInformationField(outFile, filename, psid, &shown, item); | 1037 siPrintPSIDInformationField(outfh, filename, psid, &shown, item); |
1038 break; | 1038 break; |
1039 } | 1039 } |
1040 } | 1040 } |
1041 | 1041 |
1042 if (optFieldOutput && shown) | 1042 if (optFieldOutput && shown) |
1043 { | 1043 { |
1044 fprintf(outFile, "\n"); | 1044 fprintf(outfh, "\n"); |
1045 } | 1045 } |
1046 | 1046 |
1047 // Shutdown | 1047 // Shutdown |
1048 error: | 1048 error: |
1049 sidlib_free_sid_file(psid); | 1049 sidlib_free_sid_file(psid); |
1050 th_io_free(inFile); | 1050 th_io_free(infh); |
1051 | 1051 |
1052 return TRUE; | 1052 return TRUE; |
1053 } | 1053 } |
1054 | 1054 |
1055 | 1055 |
1155 } | 1155 } |
1156 | 1156 |
1157 | 1157 |
1158 int main(int argc, char *argv[]) | 1158 int main(int argc, char *argv[]) |
1159 { | 1159 { |
1160 th_ioctx *inFile = NULL; | 1160 th_ioctx *infh = NULL; |
1161 char *setLang = getenv("LANG"); | 1161 char *setLang = getenv("LANG"); |
1162 int ret; | 1162 int ret; |
1163 | 1163 |
1164 // Get HVSC_BASE env variable if it is set | 1164 // Get HVSC_BASE env variable if it is set |
1165 th_pstr_cpy(&setHVSCPath, getenv("HVSC_BASE")); | 1165 th_pstr_cpy(&setHVSCPath, getenv("HVSC_BASE")); |
1245 if (setSLDBPath != NULL) | 1245 if (setSLDBPath != NULL) |
1246 { | 1246 { |
1247 // Initialize SLDB | 1247 // Initialize SLDB |
1248 setSLDBNewFormat = th_strrcasecmp(setSLDBPath, ".md5") != NULL; | 1248 setSLDBNewFormat = th_strrcasecmp(setSLDBPath, ".md5") != NULL; |
1249 | 1249 |
1250 if ((ret = th_io_fopen(&inFile, &th_stdio_io_ops, setSLDBPath, "r")) != THERR_OK) | 1250 if ((ret = th_io_fopen(&infh, &th_stdio_io_ops, setSLDBPath, "r")) != THERR_OK) |
1251 { | 1251 { |
1252 THERR("Could not open SLDB '%s': %s\n", | 1252 THERR("Could not open SLDB '%s': %s\n", |
1253 setSLDBPath, th_error_str(ret)); | 1253 setSLDBPath, th_error_str(ret)); |
1254 goto err1; | 1254 goto err1; |
1255 } | 1255 } |
1256 | 1256 |
1257 th_io_set_handlers(inFile, siSTILError, NULL); | 1257 th_io_set_handlers(infh, siSTILError, NULL); |
1258 | 1258 |
1259 THMSG(1, "Reading SLDB (%s format [%s]): %s\n", | 1259 THMSG(1, "Reading SLDB (%s format [%s]): %s\n", |
1260 setSLDBNewFormat ? "new" : "old", | 1260 setSLDBNewFormat ? "new" : "old", |
1261 setSLDBNewFormat ? ".md5" : ".txt", | 1261 setSLDBNewFormat ? ".md5" : ".txt", |
1262 setSLDBPath); | 1262 setSLDBPath); |
1266 THERR("Could not allocate SLDB database structure: %s\n", | 1266 THERR("Could not allocate SLDB database structure: %s\n", |
1267 th_error_str(ret)); | 1267 th_error_str(ret)); |
1268 goto err1; | 1268 goto err1; |
1269 } | 1269 } |
1270 | 1270 |
1271 if ((ret = sidlib_sldb_read(inFile, sidSLDB)) != THERR_OK) | 1271 if ((ret = sidlib_sldb_read(infh, sidSLDB)) != THERR_OK) |
1272 { | 1272 { |
1273 THERR("Error parsing SLDB: %s\n", | 1273 THERR("Error parsing SLDB: %s\n", |
1274 th_error_str(ret)); | 1274 th_error_str(ret)); |
1275 goto err1; | 1275 goto err1; |
1276 } | 1276 } |
1281 th_error_str(ret)); | 1281 th_error_str(ret)); |
1282 goto err1; | 1282 goto err1; |
1283 } | 1283 } |
1284 | 1284 |
1285 err1: | 1285 err1: |
1286 th_io_free(inFile); | 1286 th_io_free(infh); |
1287 inFile = NULL; | 1287 infh = NULL; |
1288 } | 1288 } |
1289 | 1289 |
1290 if (setSTILDBPath != NULL) | 1290 if (setSTILDBPath != NULL) |
1291 { | 1291 { |
1292 // Initialize STILDB | 1292 // Initialize STILDB |
1293 if ((ret = th_io_fopen(&inFile, &th_stdio_io_ops, setSTILDBPath, "r")) != THERR_OK) | 1293 if ((ret = th_io_fopen(&infh, &th_stdio_io_ops, setSTILDBPath, "r")) != THERR_OK) |
1294 { | 1294 { |
1295 THERR("Could not open STIL database '%s': %s\n", | 1295 THERR("Could not open STIL database '%s': %s\n", |
1296 setSTILDBPath, th_error_str(ret)); | 1296 setSTILDBPath, th_error_str(ret)); |
1297 goto err2; | 1297 goto err2; |
1298 } | 1298 } |
1299 | 1299 |
1300 th_io_set_handlers(inFile, siSTILError, NULL); | 1300 th_io_set_handlers(infh, siSTILError, NULL); |
1301 | 1301 |
1302 THMSG(1, "Reading STIL database: %s\n", | 1302 THMSG(1, "Reading STIL database: %s\n", |
1303 setSTILDBPath); | 1303 setSTILDBPath); |
1304 | 1304 |
1305 if ((ret = sidlib_stildb_new(&sidSTILDB)) != THERR_OK) | 1305 if ((ret = sidlib_stildb_new(&sidSTILDB)) != THERR_OK) |
1307 THERR("Could not allocate STIL database structure: %s\n", | 1307 THERR("Could not allocate STIL database structure: %s\n", |
1308 th_error_str(ret)); | 1308 th_error_str(ret)); |
1309 goto err2; | 1309 goto err2; |
1310 } | 1310 } |
1311 | 1311 |
1312 if ((ret = sidlib_stildb_read(inFile, sidSTILDB, NULL)) != THERR_OK) | 1312 if ((ret = sidlib_stildb_read(infh, sidSTILDB, NULL)) != THERR_OK) |
1313 { | 1313 { |
1314 THERR("Error parsing STIL: %s\n", | 1314 THERR("Error parsing STIL: %s\n", |
1315 th_error_str(ret)); | 1315 th_error_str(ret)); |
1316 goto err2; | 1316 goto err2; |
1317 } | 1317 } |
1322 th_error_str(ret)); | 1322 th_error_str(ret)); |
1323 goto err2; | 1323 goto err2; |
1324 } | 1324 } |
1325 | 1325 |
1326 err2: | 1326 err2: |
1327 th_io_free(inFile); | 1327 th_io_free(infh); |
1328 inFile = NULL; | 1328 infh = NULL; |
1329 } | 1329 } |
1330 | 1330 |
1331 // Process files | 1331 // Process files |
1332 if (!th_args_process(argc, argv, optList, optListN, | 1332 if (!th_args_process(argc, argv, optList, optListN, |
1333 NULL, argHandleFile, OPTH_ONLY_OTHER)) | 1333 NULL, argHandleFile, OPTH_ONLY_OTHER)) |