Mercurial > hg > sidinfo
annotate sidlib.c @ 367:f73270cabde2
Bump copyright years.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 02 Jan 2021 11:37:13 +0200 |
parents | 4978ff445572 |
children | 22e8ee2df9ac |
rev | line source |
---|---|
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * SIDInfoLib - Way too simplistic PSID/RSID file library |
171
d86ade7d0dfd
Change copyright blurbs slightly.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
367 | 4 * (C) Copyright 2014-2021 Tecnic Software productions (TNSP) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 */ |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 #include "sidlib.h" |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 #include "th_endian.h" |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
8 #include "th_string.h" |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 |
352
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
11 const char *sidlib_stil_fields_uc[STF_LAST] = |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
12 { |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
13 "NAME", |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
14 "AUTHOR", |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
15 "TITLE", |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
16 "INFO", |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
17 "ARTIST", |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
18 "COMMENT", |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
19 |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
20 // STF_LAST |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
21 }; |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
22 |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
23 |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
24 const char *sidlib_stil_fields_lc[STF_LAST] = |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
25 { |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
26 "name", |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
27 "author", |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
28 "title", |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
29 "info", |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
30 "artist", |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
31 "comment", |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
32 |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
33 // STF_LAST |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
34 }; |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
35 |
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
36 |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
37 static char *sidlib_strdup_convert(SIDLibChConvCtx *chconv, const char *str) |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
38 { |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
39 if (chconv != NULL) |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
40 return chconv->convert(chconv, str); |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
41 else |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
42 return th_strdup(str); |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
43 } |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
44 |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
45 |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
46 static BOOL sidlib_fread_str(th_ioctx *ctx, SIDLibChConvCtx *chconv, char **str, const size_t len) |
128
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
47 { |
242 | 48 char *tmp; |
49 | |
50 if ((*str = tmp = th_malloc(len + 1)) == NULL) | |
232
03b6f8604a7f
Minor error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
231
diff
changeset
|
51 { |
03b6f8604a7f
Minor error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
231
diff
changeset
|
52 th_io_error(ctx, THERR_MALLOC, |
239 | 53 "Could not allocate %" PRIu_SIZE_T " bytes for a string.", |
232
03b6f8604a7f
Minor error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
231
diff
changeset
|
54 len); |
128
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
55 goto err; |
232
03b6f8604a7f
Minor error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
231
diff
changeset
|
56 } |
128
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
57 |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
58 if (!thfread_str(ctx, tmp, len)) |
232
03b6f8604a7f
Minor error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
231
diff
changeset
|
59 { |
03b6f8604a7f
Minor error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
231
diff
changeset
|
60 th_io_error(ctx, THERR_FREAD, |
239 | 61 "Could not read %" PRIu_SIZE_T " bytes from file.", |
232
03b6f8604a7f
Minor error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
231
diff
changeset
|
62 len); |
128
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
63 goto err; |
232
03b6f8604a7f
Minor error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
231
diff
changeset
|
64 } |
128
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
65 |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
66 tmp[len] = 0; |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
67 |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
68 if (chconv != NULL) |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
69 { |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
70 *str = chconv->convert(chconv, tmp); |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
71 th_free(tmp); |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
72 } |
128
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
73 return TRUE; |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
74 |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
75 err: |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
76 th_free(tmp); |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
77 return FALSE; |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
78 } |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
79 |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
80 |
239 | 81 static int sidlib_read_hash_data(th_ioctx *ctx, SIDLibPSIDHeader *psid, |
166
e6c8a235ce2f
Fix the load address handling in case of new SLDB format.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
82 th_md5state_t *state, const BOOL newSLDB) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 { |
239 | 84 int ret = THERR_OK; |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
85 uint8_t *data = NULL; |
239 | 86 BOOL first = TRUE; |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 size_t read; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 |
230 | 89 if ((data = (uint8_t *) th_malloc(SIDLIB_BUFFER_SIZE)) == NULL) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 { |
239 | 91 ret = th_io_error(ctx, THERR_MALLOC, |
92 "Error allocating temporary data buffer of %d bytes.", | |
230 | 93 SIDLIB_BUFFER_SIZE); |
239 | 94 goto exit; |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
97 psid->dataSize = 0; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
98 do |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
99 { |
230 | 100 read = thfread(data, sizeof(uint8_t), SIDLIB_BUFFER_SIZE, ctx); |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
101 psid->dataSize += read; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
102 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
103 // If load address is 0 in header and we have the first block, grab it |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
104 if (first && psid->loadAddress == 0) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
105 { |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
106 if (read < 4) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
107 { |
239 | 108 ret = th_io_error(ctx, THERR_FREAD, |
109 "Error reading song data, unexpectedly small file."); | |
110 goto exit; | |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
111 } |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
112 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
113 // Grab the load address |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
114 psid->loadAddress = TH_LE16_TO_NATIVE(*(uint16_t *) data); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
115 |
203
b4a349f1c2f4
Actually fix the load address handling in hash calculation.
Matti Hamalainen <ccr@tnsp.org>
parents:
185
diff
changeset
|
116 // .. do not include the load address to the hash if NEW SLDB format |
166
e6c8a235ce2f
Fix the load address handling in case of new SLDB format.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
117 if (newSLDB) |
203
b4a349f1c2f4
Actually fix the load address handling in hash calculation.
Matti Hamalainen <ccr@tnsp.org>
parents:
185
diff
changeset
|
118 th_md5_append(state, &data[2], read - 2); |
b4a349f1c2f4
Actually fix the load address handling in hash calculation.
Matti Hamalainen <ccr@tnsp.org>
parents:
185
diff
changeset
|
119 else |
166
e6c8a235ce2f
Fix the load address handling in case of new SLDB format.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
120 th_md5_append(state, data, read); |
e6c8a235ce2f
Fix the load address handling in case of new SLDB format.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
121 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
122 first = FALSE; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
123 } |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
124 else |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
125 if (read > 0) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
126 { |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
127 // Append data "as is" |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
128 th_md5_append(state, data, read); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
129 } |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
130 } while (read > 0 && !thfeof(ctx)); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
131 |
239 | 132 exit: |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
133 th_free(data); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
134 return ret; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
135 } |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
136 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
137 |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
138 int sidlib_read_sid_file(th_ioctx *ctx, SIDLibPSIDHeader *psid, |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
139 const BOOL newSLDB, SIDLibChConvCtx *chconv) |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
140 { |
239 | 141 int ret = THERR_OK; |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
142 th_md5state_t state; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
143 off_t hdrStart, hdrEnd; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
144 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
145 hdrStart = thftell(ctx); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
146 |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 // Read PSID header in |
223
a76276ff7ba8
Rename PSID_* constant defines to SIDLIB_PSID_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
222
diff
changeset
|
148 if (!thfread_str(ctx, (uint8_t *) psid->magic, SIDLIB_PSID_MAGIC_LEN) || |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
149 !thfread_be16(ctx, &psid->version) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
150 !thfread_be16(ctx, &psid->dataOffset) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
151 !thfread_be16(ctx, &psid->loadAddress) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
152 !thfread_be16(ctx, &psid->initAddress) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
153 !thfread_be16(ctx, &psid->playAddress) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
154 !thfread_be16(ctx, &psid->nSongs) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
155 !thfread_be16(ctx, &psid->startSong) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
156 !thfread_be32(ctx, &psid->speed)) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 { |
239 | 158 ret = th_io_error(ctx, ctx->status, |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
159 "Could not read PSID/RSID header."); |
239 | 160 goto exit; |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 |
223
a76276ff7ba8
Rename PSID_* constant defines to SIDLIB_PSID_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
222
diff
changeset
|
163 psid->magic[SIDLIB_PSID_MAGIC_LEN] = 0; |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 if ((psid->magic[0] != 'R' && psid->magic[0] != 'P') || |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
166 psid->magic[1] != 'S' || psid->magic[2] != 'I' || psid->magic[3] != 'D' || |
133
9cfa0553e7f9
Add support for reading PSID/RSID v4 in sidlib.
Matti Hamalainen <ccr@tnsp.org>
parents:
132
diff
changeset
|
167 psid->version < 1 || psid->version > 4) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 { |
239 | 169 ret = th_io_error(ctx, THERR_NOT_SUPPORTED, |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
170 "Not a supported PSID or RSID file."); |
239 | 171 goto exit; |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 psid->isRSID = psid->magic[0] == 'R'; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
176 if (!sidlib_fread_str(ctx, chconv, &psid->sidName, SIDLIB_PSID_STR_LEN) || |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
177 !sidlib_fread_str(ctx, chconv, &psid->sidAuthor, SIDLIB_PSID_STR_LEN) || |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
178 !sidlib_fread_str(ctx, chconv, &psid->sidCopyright, SIDLIB_PSID_STR_LEN)) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 { |
239 | 180 ret = th_io_error(ctx, ctx->status, |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
181 "Error reading SID file header data."); |
239 | 182 goto exit; |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
183 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
184 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
185 // Check if we need to load PSIDv2NG header ... |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
186 if (psid->version >= 2) |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
187 { |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
188 // Yes, we need to |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
189 if (!thfread_be16(ctx, &psid->flags) || |
98
d9f38d657433
Match with th-libs API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
190 !thfread_u8(ctx, &psid->startPage) || |
d9f38d657433
Match with th-libs API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
191 !thfread_u8(ctx, &psid->pageLength) || |
133
9cfa0553e7f9
Add support for reading PSID/RSID v4 in sidlib.
Matti Hamalainen <ccr@tnsp.org>
parents:
132
diff
changeset
|
192 !thfread_u8(ctx, &psid->sid2Addr) || |
9cfa0553e7f9
Add support for reading PSID/RSID v4 in sidlib.
Matti Hamalainen <ccr@tnsp.org>
parents:
132
diff
changeset
|
193 !thfread_u8(ctx, &psid->sid3Addr)) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 { |
239 | 195 ret = th_io_error(ctx, ctx->status, |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
196 "Error reading PSID/RSID v2+ extra header data."); |
239 | 197 goto exit; |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
200 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
201 hdrEnd = thftell(ctx); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
202 |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 // Initialize MD5-hash calculation |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 th_md5_init(&state); |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
206 if (newSLDB) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
207 { |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
208 // New Songlengths.md5 style hash calculation: |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
209 // We just hash the whole file, so seek back to beginning .. |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
210 thfseek(ctx, hdrStart, SEEK_SET); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
211 |
239 | 212 if ((ret = sidlib_read_hash_data(ctx, psid, &state, FALSE)) != THERR_OK) |
213 goto exit; | |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
214 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
215 psid->dataSize -= hdrEnd - hdrStart; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
216 } |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
217 else |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
218 { |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
219 // "Old" Songlengths.txt style MD5 hash calculation |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
220 // We need to separately hash data etc. |
239 | 221 if ((ret = sidlib_read_hash_data(ctx, psid, &state, TRUE)) != THERR_OK) |
222 goto exit; | |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
223 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
224 // Append header data to hash |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
225 th_md5_append_le16(&state, psid->initAddress); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
226 th_md5_append_le16(&state, psid->playAddress); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
227 th_md5_append_le16(&state, psid->nSongs); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
228 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
229 // Append song speed data to hash |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
230 uint8_t tmp8 = psid->isRSID ? 60 : 0; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
231 for (int index = 0; index < psid->nSongs && index < 32; index++) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 { |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
233 if (psid->isRSID) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
234 tmp8 = 60; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
235 else |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
236 tmp8 = (psid->speed & (1 << index)) ? 60 : 0; |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
237 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
238 th_md5_append(&state, &tmp8, sizeof(tmp8)); |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
239 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
241 // Rest of songs (more than 32) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
242 for (int index = 32; index < psid->nSongs; index++) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
243 th_md5_append(&state, &tmp8, sizeof(tmp8)); |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
244 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
245 // PSIDv2NG specific |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
246 if (psid->version >= 2) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
247 { |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
248 // REFER TO SIDPLAY HEADERS FOR MORE INFORMATION |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
249 tmp8 = (psid->flags >> 2) & 3; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
250 if (tmp8 == 2) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
251 th_md5_append(&state, &tmp8, sizeof(tmp8)); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
252 } |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
253 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
254 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
255 // Calculate the hash |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
256 th_md5_finish(&state, psid->hash); |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 |
239 | 258 exit: |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
259 return ret; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
260 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
263 int sidlib_read_sid_file_alloc(th_ioctx *ctx, SIDLibPSIDHeader **ppsid, |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
264 const BOOL newSLDB, SIDLibChConvCtx *chconv) |
226
c32015f4969e
Rename PSIDHeader struct to SIDLibPSIDHeader to conform with the SIDLib
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
265 { |
c32015f4969e
Rename PSIDHeader struct to SIDLibPSIDHeader to conform with the SIDLib
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
266 if ((*ppsid = th_malloc0(sizeof(SIDLibPSIDHeader))) == NULL) |
239 | 267 return THERR_MALLOC; |
228 | 268 |
226
c32015f4969e
Rename PSIDHeader struct to SIDLibPSIDHeader to conform with the SIDLib
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
269 (*ppsid)->allocated = TRUE; |
c32015f4969e
Rename PSIDHeader struct to SIDLibPSIDHeader to conform with the SIDLib
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
270 |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
271 return sidlib_read_sid_file(ctx, *ppsid, newSLDB, chconv); |
226
c32015f4969e
Rename PSIDHeader struct to SIDLibPSIDHeader to conform with the SIDLib
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
272 } |
c32015f4969e
Rename PSIDHeader struct to SIDLibPSIDHeader to conform with the SIDLib
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
273 |
c32015f4969e
Rename PSIDHeader struct to SIDLibPSIDHeader to conform with the SIDLib
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
274 |
c32015f4969e
Rename PSIDHeader struct to SIDLibPSIDHeader to conform with the SIDLib
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
275 void sidlib_free_sid_file(SIDLibPSIDHeader *psid) |
126
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
276 { |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
277 if (psid != NULL) |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
278 { |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
279 th_free(psid->sidName); |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
280 th_free(psid->sidAuthor); |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
281 th_free(psid->sidCopyright); |
228 | 282 |
226
c32015f4969e
Rename PSIDHeader struct to SIDLibPSIDHeader to conform with the SIDLib
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
283 if (psid->allocated) |
c32015f4969e
Rename PSIDHeader struct to SIDLibPSIDHeader to conform with the SIDLib
Matti Hamalainen <ccr@tnsp.org>
parents:
224
diff
changeset
|
284 th_free(psid); |
126
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
285 } |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
286 } |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
287 |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
288 |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
289 const char *sidlib_get_sid_clock_str(const int flags) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
290 { |
132
b3e034b8c4b9
Change how certain flags (for SID model and clock) are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
291 switch (flags) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
292 { |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
293 case PSF_CLOCK_UNKNOWN : return "Unknown"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
294 case PSF_CLOCK_PAL : return "PAL 50Hz"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
295 case PSF_CLOCK_NTSC : return "NTSC 60Hz"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
296 case PSF_CLOCK_ANY : return "PAL / NTSC"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
297 default : return "?"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
298 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
299 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
300 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
301 |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
302 const char *sidlib_get_sid_model_str(const int flags) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
303 { |
132
b3e034b8c4b9
Change how certain flags (for SID model and clock) are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
304 switch (flags) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
305 { |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
306 case PSF_MODEL_UNKNOWN : return "Unknown"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
307 case PSF_MODEL_MOS6581 : return "MOS6581"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
308 case PSF_MODEL_MOS8580 : return "MOS8580"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
309 case PSF_MODEL_ANY : return "MOS6581 / MOS8580"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
310 default : return "?"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
311 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
312 } |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
313 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
314 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
315 // Free memory allocated for given SLDB node |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
316 // |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
317 static void sidlib_sldb_node_free(SIDLibSLDBNode *node) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
318 { |
170 | 319 if (node != NULL) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
320 { |
256
7fdcacbdfc00
Not using th_free_r() here.
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
321 th_free(node->lengths); |
7fdcacbdfc00
Not using th_free_r() here.
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
322 th_free(node); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
323 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
324 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
325 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
326 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
327 // Parse a time-entry in SLDB format |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
328 // |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
329 static int sidlib_sldb_get_value(const char *str, size_t *pos) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
330 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
331 int result = 0; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
332 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
333 while (isdigit(str[*pos])) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
334 result = (result * 10) + (str[(*pos)++] - '0'); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
335 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
336 return result; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
337 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
338 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
339 |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
340 static int sidlib_sldb_gettime(const char *str, size_t *pos) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
341 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
342 int result; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
343 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
344 // Check if it starts with a digit |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
345 if (th_isdigit(str[*pos])) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
346 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
347 // Get minutes-field |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
348 result = sidlib_sldb_get_value(str, pos) * 60; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
349 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
350 // Check the field separator char |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
351 if (str[*pos] == ':') |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
352 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
353 // Get seconds-field |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
354 (*pos)++; |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
355 result += sidlib_sldb_get_value(str, pos); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
356 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
357 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
358 result = -2; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
359 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
360 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
361 result = -1; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
362 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
363 // Ignore and skip the possible attributes |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
364 while (str[*pos] && !th_isspace(str[*pos])) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
365 (*pos)++; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
366 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
367 return result; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
368 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
369 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
370 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
371 // Parse one SLDB definition line, return SLDB node |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
372 // |
239 | 373 static int sidlib_sldb_parse_entry(th_ioctx *ctx, SIDLibSLDBNode **pnode, const char *line) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
374 { |
239 | 375 int ret = THERR_OK; |
376 SIDLibSLDBNode *node; | |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
377 size_t pos, tmpLen, savePos; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
378 BOOL isOK; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
379 int i; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
380 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
381 // Allocate new node |
239 | 382 if ((*pnode = node = (SIDLibSLDBNode *) th_malloc0(sizeof(SIDLibSLDBNode))) == NULL) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
383 { |
239 | 384 ret = th_io_error(ctx, THERR_MALLOC, |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
385 "Error allocating new SLDB node."); |
239 | 386 goto exit; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
387 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
388 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
389 // Get hash value |
160 | 390 for (pos = 0, i = 0; i < TH_MD5HASH_LENGTH; i++, pos += 2) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
391 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
392 unsigned int tmpu; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
393 sscanf(&line[pos], "%2x", &tmpu); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
394 node->hash[i] = tmpu; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
395 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
396 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
397 // Get playtimes |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
398 th_findnext(line, &pos); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
399 if (line[pos] != '=') |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
400 { |
239 | 401 ret = th_io_error(ctx, THERR_INVALID_DATA, |
292
6454bfb584d6
Fix some format strings to use proper PRIu_SIZE_T where necessary.
Matti Hamalainen <ccr@tnsp.org>
parents:
266
diff
changeset
|
402 "'=' expected on column #%" PRIu_SIZE_T ".", |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
403 pos); |
239 | 404 goto exit; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
405 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
406 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
407 // First playtime is after '=' |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
408 savePos = ++pos; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
409 tmpLen = strlen(line); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
410 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
411 // Get number of sub-tune lengths |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
412 isOK = TRUE; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
413 while (pos < tmpLen && isOK) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
414 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
415 th_findnext(line, &pos); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
416 |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
417 if (sidlib_sldb_gettime(line, &pos) >= 0) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
418 node->nlengths++; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
419 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
420 isOK = FALSE; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
421 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
422 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
423 // Allocate memory for lengths |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
424 if (node->nlengths == 0) |
239 | 425 { |
426 ret = THERR_OK; | |
427 goto exit; | |
428 } | |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
429 |
239 | 430 if ((node->lengths = (int *) th_calloc(node->nlengths, sizeof(int))) == NULL) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
431 { |
239 | 432 ret = th_io_error(ctx, THERR_MALLOC, |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
433 "Error allocating SLDB entry length data."); |
239 | 434 goto exit; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
435 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
436 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
437 // Read lengths in |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
438 for (i = 0, pos = savePos, isOK = TRUE; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
439 pos < tmpLen && i < node->nlengths && isOK; i++) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
440 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
441 int l; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
442 th_findnext(line, &pos); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
443 |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
444 l = sidlib_sldb_gettime(line, &pos); |
239 | 445 |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
446 if (l >= 0) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
447 node->lengths[i] = l; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
448 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
449 isOK = FALSE; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
450 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
451 |
239 | 452 return ret; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
453 |
239 | 454 exit: |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
455 sidlib_sldb_node_free(node); |
239 | 456 return ret; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
457 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
458 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
459 |
236
609bfc1bd628
Change sidlib_sldb_new() API to int sidlib_sldb_new(SIDLibSLDB **pdbh).
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
460 int sidlib_sldb_new(SIDLibSLDB **pdbh) |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
461 { |
236
609bfc1bd628
Change sidlib_sldb_new() API to int sidlib_sldb_new(SIDLibSLDB **pdbh).
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
462 SIDLibSLDB *dbh; |
609bfc1bd628
Change sidlib_sldb_new() API to int sidlib_sldb_new(SIDLibSLDB **pdbh).
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
463 if ((dbh = *pdbh = (SIDLibSLDB *) th_malloc0(sizeof(SIDLibSLDB))) == NULL) |
609bfc1bd628
Change sidlib_sldb_new() API to int sidlib_sldb_new(SIDLibSLDB **pdbh).
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
464 return THERR_MALLOC; |
609bfc1bd628
Change sidlib_sldb_new() API to int sidlib_sldb_new(SIDLibSLDB **pdbh).
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
465 |
609bfc1bd628
Change sidlib_sldb_new() API to int sidlib_sldb_new(SIDLibSLDB **pdbh).
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
466 return THERR_OK; |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
467 } |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
468 |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
469 |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
470 // Read SLDB database to memory |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
471 // |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
472 int sidlib_sldb_read(th_ioctx *ctx, SIDLibSLDB *dbh) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
473 { |
239 | 474 int ret = THERR_OK; |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
475 char *line = NULL; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
476 |
230 | 477 if ((line = th_malloc(SIDLIB_BUFFER_SIZE)) == NULL) |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
478 { |
239 | 479 ret = th_io_error(ctx, THERR_MALLOC, |
480 "Error allocating temporary data buffer of %d bytes.", | |
230 | 481 SIDLIB_BUFFER_SIZE); |
239 | 482 goto exit; |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
483 } |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
484 |
230 | 485 while (thfgets(line, SIDLIB_BUFFER_SIZE, ctx) != NULL) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
486 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
487 SIDLibSLDBNode *tmnode; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
488 size_t pos = 0; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
489 ctx->line++; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
490 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
491 th_findnext(line, &pos); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
492 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
493 // Check if it is datafield |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
494 if (th_isxdigit(line[pos])) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
495 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
496 // Check the length of the hash |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
497 int hashLen; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
498 for (hashLen = 0; line[pos] && th_isxdigit(line[pos]); hashLen++, pos++); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
499 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
500 if (hashLen != TH_MD5HASH_LENGTH_CH) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
501 { |
239 | 502 ret = th_io_error(ctx, THERR_INVALID_DATA, |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
503 "Invalid MD5-hash."); |
239 | 504 goto exit; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
505 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
506 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
507 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
508 // Parse and add node to db |
239 | 509 if ((ret = sidlib_sldb_parse_entry(ctx, &tmnode, line)) != THERR_OK) |
510 goto exit; | |
511 | |
512 th_llist_append_node((th_llist_t **) &dbh->nodes, (th_llist_t *) tmnode); | |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
513 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
514 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
515 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
516 if (line[pos] != ';' && line[pos] != '[' && line[pos] != 0) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
517 { |
239 | 518 ret = th_io_error(ctx, THERR_INVALID_DATA, |
292
6454bfb584d6
Fix some format strings to use proper PRIu_SIZE_T where necessary.
Matti Hamalainen <ccr@tnsp.org>
parents:
266
diff
changeset
|
519 "Invalid line / unexpected data at column #%" PRIu_SIZE_T ".", |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
520 pos); |
239 | 521 goto exit; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
522 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
523 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
524 |
239 | 525 exit: |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
526 th_free(line); |
239 | 527 return ret; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
528 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
529 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
530 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
531 // Compare two given MD5-hashes. |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
532 // Return: 0 if equal |
102 | 533 // negative if hash1 < hash2 |
534 // positive if hash1 > hash2 | |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
535 // |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
536 static int sidlib_sldb_compare_hash(th_md5hash_t hash1, th_md5hash_t hash2) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
537 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
538 int i, delta; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
539 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
540 for (i = delta = 0; i < TH_MD5HASH_LENGTH && !delta; i++) |
102 | 541 delta = hash1[i] - hash2[i]; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
542 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
543 return delta; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
544 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
545 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
546 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
547 // Compare two nodes. |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
548 // We assume here that we never ever get NULL-pointers. |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
549 static int sidlib_sldb_compare_nodes(const void *node1, const void *node2) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
550 { |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
551 return sidlib_sldb_compare_hash( |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
552 (*(SIDLibSLDBNode **) node1)->hash, |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
553 (*(SIDLibSLDBNode **) node2)->hash); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
554 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
555 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
556 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
557 // (Re)create index |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
558 // |
237 | 559 int sidlib_sldb_build_index(SIDLibSLDB *dbh) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
560 { |
331
e35e15f07c66
Add bounds checks for node index generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
561 if (dbh == NULL) |
e35e15f07c66
Add bounds checks for node index generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
562 return THERR_NULLPTR; |
e35e15f07c66
Add bounds checks for node index generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
563 |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
564 // Free old index |
237 | 565 th_free_r(&(dbh->pindex)); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
566 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
567 // Get size of db |
224
4bec78f45188
Use th_datastruct::th_llist_t instead of yet another linked list
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
568 dbh->nnodes = th_llist_length((th_llist_t *) dbh->nodes); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
569 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
570 // Check number of nodes |
100 | 571 if (dbh->nnodes > 0) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
572 { |
224
4bec78f45188
Use th_datastruct::th_llist_t instead of yet another linked list
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
573 SIDLibSLDBNode *node; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
574 size_t i; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
575 |
331
e35e15f07c66
Add bounds checks for node index generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
576 // Check number of nodes against overflow |
e35e15f07c66
Add bounds checks for node index generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
577 if (dbh->nnodes > UINTPTR_MAX / sizeof(SIDLibSTILNode *)) |
e35e15f07c66
Add bounds checks for node index generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
578 return THERR_BOUNDS; |
e35e15f07c66
Add bounds checks for node index generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
579 |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
580 // Allocate memory for index-table |
100 | 581 dbh->pindex = (SIDLibSLDBNode **) th_malloc(sizeof(SIDLibSLDBNode *) * dbh->nnodes); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
582 if (dbh->pindex == NULL) |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
583 return THERR_MALLOC; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
584 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
585 // Get node-pointers to table |
224
4bec78f45188
Use th_datastruct::th_llist_t instead of yet another linked list
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
586 for (i = 0, node = dbh->nodes; node != NULL && i < dbh->nnodes; |
4bec78f45188
Use th_datastruct::th_llist_t instead of yet another linked list
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
587 node = (SIDLibSLDBNode *) node->node.next) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
588 dbh->pindex[i++] = node; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
589 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
590 // Sort the indexes |
332 | 591 qsort(dbh->pindex, dbh->nnodes, |
592 sizeof(SIDLibSLDBNode *), sidlib_sldb_compare_nodes); | |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
593 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
594 |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
595 return THERR_OK; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
596 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
597 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
598 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
599 // Free a given song-length database |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
600 // |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
601 void sidlib_sldb_free(SIDLibSLDB *dbh) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
602 { |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
603 if (dbh != NULL) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
604 { |
255
3c1cb0f39703
OOoops. Use th_llist_free_func_node() instead of th_llist_free_func().
Matti Hamalainen <ccr@tnsp.org>
parents:
254
diff
changeset
|
605 th_llist_free_func_node((th_llist_t *) dbh->nodes, |
3c1cb0f39703
OOoops. Use th_llist_free_func_node() instead of th_llist_free_func().
Matti Hamalainen <ccr@tnsp.org>
parents:
254
diff
changeset
|
606 (void (*)(th_llist_t *)) sidlib_sldb_node_free); |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
607 |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
608 dbh->nodes = NULL; |
100 | 609 dbh->nnodes = 0; |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
610 |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
611 th_free_r(&dbh->pindex); |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
612 th_free(dbh); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
613 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
614 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
615 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
616 |
222
3a01518fffe0
Rename sidlib functions from si_* prefix to sidlib_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
221
diff
changeset
|
617 SIDLibSLDBNode *sidlib_sldb_get_by_hash(SIDLibSLDB *dbh, th_md5hash_t hash) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
618 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
619 SIDLibSLDBNode keyItem, *key, **item; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
620 |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
621 memcpy(&keyItem.hash, hash, sizeof(th_md5hash_t)); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
622 key = &keyItem; |
332 | 623 item = bsearch(&key, dbh->pindex, dbh->nnodes, |
624 sizeof(dbh->pindex[0]), sidlib_sldb_compare_nodes); | |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
625 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
626 return (item != NULL) ? *item : NULL; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
627 } |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
628 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
629 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
630 /* STIL database handling functions |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
631 */ |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
632 int sidlib_stildb_new(SIDLibSTILDB **pdbh) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
633 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
634 SIDLibSTILDB *dbh; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
635 if ((dbh = *pdbh = (SIDLibSTILDB *) th_malloc0(sizeof(SIDLibSTILDB))) == NULL) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
636 return THERR_MALLOC; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
637 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
638 return THERR_OK; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
639 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
640 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
641 |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
642 SIDLibSTILSubTune *sidlib_stildb_find_subtune(SIDLibSTILNode *node, const int nsubtune) |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
643 { |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
644 for (size_t n = 0; n < node->nsubtunes; n++) |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
645 { |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
646 SIDLibSTILSubTune *subtune = &node->subtunes[n]; |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
647 if (subtune->tune == nsubtune) |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
648 return subtune; |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
649 } |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
650 return NULL; |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
651 } |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
652 |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
653 |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
654 static int sidlib_stildb_entry_realloc( |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
655 SIDLibSTILNode *node, |
264
5afa6052f796
Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
656 const int nsubtune, const BOOL alloc, |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
657 const int nfield, const char *fdata, |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
658 SIDLibChConvCtx *chconv) |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
659 { |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
660 SIDLibSTILSubTune *subtune; |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
661 |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
662 if (node == NULL) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
663 return THERR_NULLPTR; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
664 |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
665 // Get subtune or allocate new |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
666 if ((subtune = sidlib_stildb_find_subtune(node, nsubtune)) == NULL) |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
667 { |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
668 node->subtunes = (SIDLibSTILSubTune *) th_realloc( |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
669 node->subtunes, (node->nsubtunes + 1) * sizeof(SIDLibSTILSubTune)); |
258
8d914417227d
Avoid allocating one extra STIL node. Does not make much of a difference.
Matti Hamalainen <ccr@tnsp.org>
parents:
256
diff
changeset
|
670 |
260 | 671 if (node->subtunes == NULL) |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
672 return THERR_MALLOC; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
673 |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
674 subtune = &node->subtunes[node->nsubtunes]; |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
675 memset(subtune, 0, sizeof(SIDLibSTILSubTune)); |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
676 subtune->tune = nsubtune; |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
677 node->nsubtunes++; |
264
5afa6052f796
Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
678 } |
5afa6052f796
Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
679 |
5afa6052f796
Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
680 // Allocate field space, if needed |
5afa6052f796
Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
681 if (alloc && nfield >= 0 && nfield < STF_LAST) |
5afa6052f796
Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
682 { |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
683 SIDLibSTILField *field = &subtune->fields[nfield]; |
264
5afa6052f796
Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
684 |
5afa6052f796
Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
685 if ((field->data = th_realloc(field->data, (field->ndata + 1) * sizeof(char *))) == NULL) |
5afa6052f796
Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
686 return THERR_MALLOC; |
5afa6052f796
Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
687 |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
688 field->data[field->ndata] = sidlib_strdup_convert(chconv, fdata); |
264
5afa6052f796
Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
689 field->ndata++; |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
690 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
691 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
692 return THERR_OK; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
693 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
694 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
695 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
696 static void sidlib_stildb_node_free(SIDLibSTILNode *node) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
697 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
698 if (node != NULL) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
699 { |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
700 for (size_t nsubtune = 0; nsubtune < node->nsubtunes; nsubtune++) |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
701 { |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
702 SIDLibSTILSubTune *subtune = &node->subtunes[nsubtune]; |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
703 for (int nfield = 0; nfield < STF_LAST; nfield++) |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
704 { |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
705 SIDLibSTILField *field = &subtune->fields[nfield]; |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
706 for (int n = 0; n < field->ndata; n++) |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
707 th_free(field->data[n]); |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
708 |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
709 th_free(field->data); |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
710 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
711 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
712 |
260 | 713 th_free(node->subtunes); |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
714 th_free(node->filename); |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
715 th_free(node); |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
716 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
717 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
718 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
719 |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
720 static int sidlib_stildb_node_new(SIDLibSTILNode **pnode, const char *filename, |
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
721 SIDLibChConvCtx *chconv) |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
722 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
723 // Allocate memory for new node |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
724 SIDLibSTILNode *node; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
725 int res; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
726 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
727 if ((node = *pnode = (SIDLibSTILNode *) th_malloc0(sizeof(SIDLibSTILNode))) == NULL) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
728 return THERR_MALLOC; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
729 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
730 // Allocate filename and initial space for one subtune |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
731 // NOTE! Filename is not converted even if chconv != NULL |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
732 if ((node->filename = th_strdup(filename)) == NULL) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
733 return THERR_MALLOC; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
734 |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
735 if ((res = sidlib_stildb_entry_realloc(node, 1, FALSE, -1, NULL, chconv)) != THERR_OK) |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
736 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
737 sidlib_stildb_node_free(node); |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
738 return res; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
739 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
740 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
741 return res; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
742 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
743 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
744 |
254
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
745 /* Free a given STIL database |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
746 */ |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
747 void sidlib_stildb_free(SIDLibSTILDB *dbh) |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
748 { |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
749 if (dbh != NULL) |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
750 { |
255
3c1cb0f39703
OOoops. Use th_llist_free_func_node() instead of th_llist_free_func().
Matti Hamalainen <ccr@tnsp.org>
parents:
254
diff
changeset
|
751 th_llist_free_func_node((th_llist_t *) dbh->nodes, |
3c1cb0f39703
OOoops. Use th_llist_free_func_node() instead of th_llist_free_func().
Matti Hamalainen <ccr@tnsp.org>
parents:
254
diff
changeset
|
752 (void (*)(th_llist_t *)) sidlib_stildb_node_free); |
254
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
753 |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
754 dbh->nodes = NULL; |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
755 dbh->nnodes = 0; |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
756 |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
757 th_free_r(&dbh->pindex); |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
758 th_free(dbh); |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
759 } |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
760 } |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
761 |
cc6d8d47dbc2
More sidlib_stildb_free() to more logical position in the source.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
762 |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
763 /* Read database (additively) to given db-structure |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
764 */ |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
765 enum |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
766 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
767 PM_EOF, |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
768 PM_ERROR, |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
769 PM_IDLE, |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
770 PM_COMMENT, |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
771 PM_NEXT, |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
772 PM_ENTRY, |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
773 PM_FIELD_NAME, |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
774 PM_FIELD_DATA, |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
775 PM_SUBTUNE, |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
776 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
777 PM_LAST |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
778 }; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
779 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
780 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
781 typedef struct |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
782 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
783 size_t lineNum; |
242 | 784 ssize_t linePos; |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
785 int ch, prevMode, nextMode, parseMode; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
786 BOOL lineStart; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
787 } SIDLibSTILParserCtx; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
788 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
789 |
352
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
790 static int sidlib_stildb_get_field(const char *field) |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
791 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
792 for (int n = 0; n < STF_LAST; n++) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
793 { |
352
4978ff445572
Add lowercase versions of the STIL field names array as
Matti Hamalainen <ccr@tnsp.org>
parents:
350
diff
changeset
|
794 if (strcmp(field, sidlib_stil_fields_uc[n]) == 0) |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
795 return n; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
796 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
797 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
798 return -1; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
799 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
800 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
801 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
802 static void sidlib_stildb_set_parsemode(SIDLibSTILParserCtx *ctx, const int mode) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
803 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
804 ctx->prevMode = ctx->parseMode; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
805 ctx->parseMode = mode; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
806 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
807 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
808 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
809 static void sidlib_stildb_set_next_parsemode(SIDLibSTILParserCtx *ctx, const int mode) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
810 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
811 sidlib_stildb_set_parsemode(ctx, PM_NEXT); |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
812 ctx->nextMode = mode; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
813 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
814 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
815 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
816 #define VADDCH(ch) if (strPos < SIDLIB_BUFFER_SIZE) { tmpStr[strPos++] = ch; } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
817 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
818 |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
819 int sidlib_stildb_read(th_ioctx *fh, SIDLibSTILDB *dbh, SIDLibChConvCtx *chconv) |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
820 { |
242 | 821 int ret = THERR_OK, field = -1; |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
822 SIDLibSTILParserCtx ctx; |
242 | 823 char *tmpStr = NULL, *fieldName = NULL; |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
824 size_t strPos; |
242 | 825 SIDLibSTILNode *entry = NULL; |
249
1f1e7934ae61
Few improvements to validity checking of STIL file parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
242
diff
changeset
|
826 int subtune; |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
827 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
828 if (fh == NULL || dbh == NULL) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
829 return THERR_NULLPTR; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
830 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
831 if ((tmpStr = th_malloc(SIDLIB_BUFFER_SIZE + 1)) == NULL) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
832 return THERR_MALLOC; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
833 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
834 // Initialize values |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
835 memset(&ctx, 0, sizeof(ctx)); |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
836 ctx.nextMode = ctx.prevMode = ctx.parseMode = PM_IDLE; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
837 ctx.ch = -1; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
838 ctx.lineStart = TRUE; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
839 strPos = 0; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
840 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
841 // Parse the STIL database |
242 | 842 while (ctx.parseMode != PM_EOF && ctx.parseMode != PM_ERROR) |
843 { | |
844 if (ctx.ch == -1) | |
845 { | |
846 // Get next character | |
847 switch (ctx.ch = thfgetc(fh)) | |
848 { | |
849 case '\n': | |
850 fh->line++; | |
851 ctx.linePos = -1; | |
852 break; | |
853 | |
854 default: | |
855 if (ctx.linePos < 0) | |
856 { | |
857 ctx.lineStart = TRUE; | |
858 ctx.linePos = 0; | |
859 } | |
860 else | |
861 ctx.linePos++; | |
862 } | |
863 } | |
864 | |
865 switch (ctx.parseMode) | |
866 { | |
867 case PM_COMMENT: | |
868 // Comment parsing mode | |
869 if (ctx.ch == '\n') | |
870 { | |
871 // End of line, end of comment | |
872 sidlib_stildb_set_parsemode(&ctx, ctx.prevMode); | |
873 } | |
874 ctx.ch = -1; | |
875 break; | |
876 | |
877 case PM_NEXT: | |
878 case PM_IDLE: | |
879 // Normal parsing mode | |
261
39a03560163d
Improve STIL parsing reliability.
Matti Hamalainen <ccr@tnsp.org>
parents:
260
diff
changeset
|
880 if (ctx.ch == EOF) |
39a03560163d
Improve STIL parsing reliability.
Matti Hamalainen <ccr@tnsp.org>
parents:
260
diff
changeset
|
881 { |
39a03560163d
Improve STIL parsing reliability.
Matti Hamalainen <ccr@tnsp.org>
parents:
260
diff
changeset
|
882 sidlib_stildb_set_parsemode(&ctx, PM_EOF); |
39a03560163d
Improve STIL parsing reliability.
Matti Hamalainen <ccr@tnsp.org>
parents:
260
diff
changeset
|
883 } |
39a03560163d
Improve STIL parsing reliability.
Matti Hamalainen <ccr@tnsp.org>
parents:
260
diff
changeset
|
884 else |
242 | 885 if (ctx.ch == '#') |
886 { | |
887 sidlib_stildb_set_parsemode(&ctx, PM_COMMENT); | |
888 ctx.ch = -1; | |
889 } | |
890 else | |
891 if (th_iscrlf(ctx.ch) || th_isspace(ctx.ch)) | |
892 { | |
893 ctx.ch = -1; | |
894 } | |
895 else | |
896 if (ctx.parseMode == PM_IDLE) | |
897 { | |
898 // PM_IDLE | |
899 strPos = 0; | |
900 if (ctx.ch == '/') | |
901 { | |
902 sidlib_stildb_set_parsemode(&ctx, PM_ENTRY); | |
350
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
903 entry = NULL; |
242 | 904 } |
905 else | |
906 if (ctx.ch == '(') | |
907 { | |
350
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
908 if (entry == NULL) |
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
909 { |
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
910 ret = th_io_error(fh, THERR_INVALID_DATA, |
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
911 "Unexpected start of subtune indicator without entry."); |
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
912 goto out; |
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
913 } |
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
914 |
242 | 915 sidlib_stildb_set_parsemode(&ctx, PM_SUBTUNE); |
916 ctx.ch = -1; | |
917 ctx.lineStart = TRUE; | |
918 } | |
919 else | |
920 if (th_isalpha(ctx.ch)) | |
921 { | |
350
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
922 if (entry == NULL) |
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
923 { |
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
924 ret = th_io_error(fh, THERR_INVALID_DATA, |
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
925 "Unexpected start of field name without entry."); |
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
926 goto out; |
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
927 } |
c2ebcb0f0d62
Add some more error checking in the STIL parser.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
928 |
242 | 929 sidlib_stildb_set_parsemode(&ctx, PM_FIELD_NAME); |
930 } | |
931 else | |
932 { | |
933 // Error! Invalid character found | |
934 ret = th_io_error(fh, THERR_INVALID_DATA, | |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
935 "Unexpected character '%c'.", |
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
936 ctx.ch); |
242 | 937 goto out; |
938 } | |
939 } | |
940 else | |
941 { | |
942 // PM_NEXT - Next item found | |
943 sidlib_stildb_set_parsemode(&ctx, ctx.nextMode); | |
944 } | |
945 break; | |
946 | |
947 case PM_ENTRY: | |
261
39a03560163d
Improve STIL parsing reliability.
Matti Hamalainen <ccr@tnsp.org>
parents:
260
diff
changeset
|
948 if (ctx.ch == EOF || th_iscrlf(ctx.ch) || th_isspace(ctx.ch)) |
242 | 949 { |
950 // A new file / entry, allocate and append | |
951 tmpStr[strPos] = 0; | |
952 | |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
953 if ((ret = sidlib_stildb_node_new(&entry, tmpStr, chconv)) != THERR_OK) |
242 | 954 goto out; |
955 | |
956 th_llist_append_node((th_llist_t **) &dbh->nodes, (th_llist_t *) entry); | |
249
1f1e7934ae61
Few improvements to validity checking of STIL file parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
242
diff
changeset
|
957 |
1f1e7934ae61
Few improvements to validity checking of STIL file parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
242
diff
changeset
|
958 // Default subtune is 0, for "main tune" information |
242 | 959 subtune = 0; |
960 | |
961 sidlib_stildb_set_parsemode(&ctx, PM_IDLE); | |
962 } | |
963 else | |
964 { | |
965 VADDCH(ctx.ch) | |
966 else | |
967 { | |
968 ret = th_io_error(fh, THERR_INVALID_DATA, | |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
969 "STIL entry filename too long."); |
242 | 970 goto out; |
971 } | |
972 ctx.ch = -1; | |
973 } | |
974 break; | |
975 | |
976 case PM_SUBTUNE: | |
261
39a03560163d
Improve STIL parsing reliability.
Matti Hamalainen <ccr@tnsp.org>
parents:
260
diff
changeset
|
977 if (ctx.ch == EOF || ctx.ch == ')') |
242 | 978 { |
979 BOOL neg = FALSE; | |
980 | |
249
1f1e7934ae61
Few improvements to validity checking of STIL file parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
242
diff
changeset
|
981 // Subtune indicator end |
242 | 982 tmpStr[strPos] = 0; |
983 | |
249
1f1e7934ae61
Few improvements to validity checking of STIL file parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
242
diff
changeset
|
984 if (!th_get_int(tmpStr, (unsigned int *) &subtune, &neg) || neg) |
242 | 985 { |
986 ret = th_io_error(fh, THERR_INVALID_DATA, | |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
987 "Entry '%s' subtune indicator '%s' is not a valid integer.", |
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
988 entry->filename, tmpStr); |
242 | 989 goto out; |
990 } | |
991 | |
253
c98be346c706
Check for subtune <= 0 instead of == 0.
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
992 if (subtune <= 0 || subtune > 128) |
249
1f1e7934ae61
Few improvements to validity checking of STIL file parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
242
diff
changeset
|
993 { |
1f1e7934ae61
Few improvements to validity checking of STIL file parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
242
diff
changeset
|
994 th_io_error(fh, THERR_INVALID_DATA, |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
995 "Entry '%s' subtune number #%d is out of range/invalid.", |
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
996 entry->filename, subtune); |
249
1f1e7934ae61
Few improvements to validity checking of STIL file parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
242
diff
changeset
|
997 |
1f1e7934ae61
Few improvements to validity checking of STIL file parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
242
diff
changeset
|
998 subtune = -1; |
1f1e7934ae61
Few improvements to validity checking of STIL file parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
242
diff
changeset
|
999 } |
1f1e7934ae61
Few improvements to validity checking of STIL file parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
242
diff
changeset
|
1000 |
242 | 1001 sidlib_stildb_set_parsemode(&ctx, PM_IDLE); |
1002 } | |
1003 else | |
1004 if (th_isdigit(ctx.ch)) | |
1005 { | |
1006 if (ctx.lineStart) | |
1007 goto sub_unexpected; | |
1008 | |
1009 VADDCH(ctx.ch) | |
1010 else | |
1011 { | |
1012 ret = th_io_error(fh, THERR_INVALID_DATA, | |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
1013 "Subtune indicator too long."); |
242 | 1014 goto out; |
1015 } | |
1016 } | |
1017 else | |
1018 if ((ctx.ch == '#' && strPos > 0) || (ctx.ch != '#' && !th_isspace(ctx.ch))) | |
1019 { | |
1020 sub_unexpected: | |
1021 ret = th_io_error(fh, THERR_INVALID_DATA, | |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
1022 "Unexpected character '%c' in subtune indicator.", |
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
1023 ctx.ch); |
242 | 1024 goto out; |
1025 } | |
1026 | |
1027 ctx.ch = -1; | |
1028 ctx.lineStart = FALSE; | |
1029 break; | |
1030 | |
1031 case PM_FIELD_NAME: | |
261
39a03560163d
Improve STIL parsing reliability.
Matti Hamalainen <ccr@tnsp.org>
parents:
260
diff
changeset
|
1032 if (ctx.ch == EOF || ctx.ch == ':' || th_iscrlf(ctx.ch) || th_isspace(ctx.ch)) |
242 | 1033 { |
1034 tmpStr[strPos] = 0; | |
1035 th_pstr_cpy(&fieldName, tmpStr); | |
1036 | |
1037 field = sidlib_stildb_get_field(tmpStr); | |
1038 | |
1039 if (entry == NULL) | |
1040 { | |
1041 ret = th_io_error(fh, THERR_INVALID_DATA, | |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
1042 "No STIL entry allocated, but field '%s' found.", |
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
1043 fieldName); |
242 | 1044 goto out; |
1045 } | |
1046 | |
1047 sidlib_stildb_set_next_parsemode(&ctx, PM_FIELD_DATA); | |
1048 ctx.lineStart = FALSE; | |
1049 strPos = 0; | |
1050 } | |
1051 else | |
1052 { | |
1053 VADDCH(ctx.ch) | |
1054 else | |
1055 { | |
1056 ret = th_io_error(fh, THERR_INVALID_DATA, | |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
1057 "Field name too long."); |
242 | 1058 goto out; |
1059 } | |
1060 } | |
1061 | |
1062 ctx.ch = -1; | |
1063 break; | |
1064 | |
1065 case PM_FIELD_DATA: | |
1066 if (ctx.lineStart && th_isspace(ctx.ch) && ctx.linePos < 8) | |
1067 { | |
1068 ctx.ch = -1; | |
1069 } | |
1070 else | |
1071 if (th_iscrlf(ctx.ch)) | |
1072 { | |
1073 ctx.lineStart = TRUE; | |
1074 ctx.ch = -1; | |
1075 } | |
1076 else | |
261
39a03560163d
Improve STIL parsing reliability.
Matti Hamalainen <ccr@tnsp.org>
parents:
260
diff
changeset
|
1077 if (ctx.ch == EOF || (ctx.lineStart && ctx.linePos < 8)) |
242 | 1078 { |
1079 // Field done | |
1080 tmpStr[strPos] = 0; | |
1081 | |
249
1f1e7934ae61
Few improvements to validity checking of STIL file parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
242
diff
changeset
|
1082 if (field >= 0 && subtune >= 0) |
242 | 1083 { |
1084 // Supported field, add it | |
264
5afa6052f796
Change SIDLibSTILSubTune::fields to new SIDLibSTILField structure for
Matti Hamalainen <ccr@tnsp.org>
parents:
263
diff
changeset
|
1085 if ((ret = sidlib_stildb_entry_realloc( |
319
6d0143e43edf
Add functionality and API changes for doing on-the-fly character set
Matti Hamalainen <ccr@tnsp.org>
parents:
311
diff
changeset
|
1086 entry, subtune, TRUE, field, tmpStr, chconv)) != THERR_OK) |
242 | 1087 { |
1088 ret = th_io_error(fh, THERR_MALLOC, | |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
1089 "Could not allocate memory for field '%s'.", |
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
1090 fieldName); |
242 | 1091 goto out; |
1092 } | |
1093 } | |
1094 | |
1095 sidlib_stildb_set_parsemode(&ctx, PM_IDLE); | |
1096 } | |
1097 else | |
1098 { | |
1099 ctx.lineStart = FALSE; | |
1100 VADDCH(ctx.ch) | |
1101 else | |
1102 { | |
1103 ret = th_io_error(fh, THERR_INVALID_DATA, | |
265
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
1104 "Field '%s' data too long.", |
05bbe428a0ab
Unify SIDLib error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
264
diff
changeset
|
1105 fieldName); |
242 | 1106 goto out; |
1107 } | |
1108 ctx.ch = - 1; | |
1109 } | |
1110 break; | |
1111 } | |
1112 } | |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1113 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1114 out: |
261
39a03560163d
Improve STIL parsing reliability.
Matti Hamalainen <ccr@tnsp.org>
parents:
260
diff
changeset
|
1115 |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1116 th_free(tmpStr); |
242 | 1117 th_free(fieldName); |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1118 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1119 if (ret == THERR_OK && ctx.parseMode == PM_ERROR) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1120 ret = THERR_INVALID_DATA; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1121 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1122 return ret; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1123 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1124 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1125 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1126 /* Compare two nodes |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1127 */ |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
1128 static int sidlib_stildb_compare_subtunes(const void *node1, const void *node2) |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
1129 { |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
1130 return |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
1131 ((SIDLibSTILSubTune *) node1)->tune - ((SIDLibSTILSubTune *) node2)->tune; |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
1132 } |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
1133 |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
1134 |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
1135 static int sidlib_stildb_compare_nodes(const void *node1, const void *node2) |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1136 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1137 // We assume here that we never ever get NULL-pointers or similar |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1138 return strcmp( |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1139 (*(SIDLibSTILNode * *) node1)->filename, |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1140 (*(SIDLibSTILNode * *) node2)->filename); |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1141 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1142 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1143 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1144 /* (Re)create index |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1145 */ |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1146 int sidlib_stildb_build_index(SIDLibSTILDB *dbh) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1147 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1148 if (dbh == NULL) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1149 return THERR_NULLPTR; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1150 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1151 // Free old index |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1152 th_free_r(&(dbh->pindex)); |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1153 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1154 // Get size of db |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1155 dbh->nnodes = th_llist_length((th_llist_t *) dbh->nodes); |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1156 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1157 // Check number of nodes |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1158 if (dbh->nnodes > 0) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1159 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1160 SIDLibSTILNode *node; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1161 size_t i; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1162 |
331
e35e15f07c66
Add bounds checks for node index generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
1163 // Check number of nodes against overflow |
e35e15f07c66
Add bounds checks for node index generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
1164 if (dbh->nnodes > UINTPTR_MAX / sizeof(SIDLibSTILNode *)) |
e35e15f07c66
Add bounds checks for node index generation.
Matti Hamalainen <ccr@tnsp.org>
parents:
319
diff
changeset
|
1165 return THERR_BOUNDS; |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1166 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1167 // Allocate memory for index-table |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1168 dbh->pindex = (SIDLibSTILNode **) th_malloc(sizeof(SIDLibSTILNode *) * dbh->nnodes); |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1169 if (dbh->pindex == NULL) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1170 return THERR_MALLOC; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1171 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1172 // Get node-pointers to table |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1173 for (i = 0, node = dbh->nodes; node != NULL && i < dbh->nnodes; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1174 node = (SIDLibSTILNode *) node->node.next) |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
1175 { |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1176 dbh->pindex[i++] = node; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1177 |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
1178 // Sort the subtunes |
332 | 1179 qsort(node->subtunes, node->nsubtunes, |
1180 sizeof(SIDLibSTILSubTune), sidlib_stildb_compare_subtunes); | |
311
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
1181 } |
ee56f1f2b528
Change how the STIL subtune are handled internally. Also fix a bug that
Matti Hamalainen <ccr@tnsp.org>
parents:
292
diff
changeset
|
1182 |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1183 // Sort the indexes |
332 | 1184 qsort(dbh->pindex, dbh->nnodes, |
1185 sizeof(SIDLibSTILNode *), sidlib_stildb_compare_nodes); | |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1186 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1187 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1188 return THERR_OK; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1189 } |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1190 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1191 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1192 /* Get STIL information node from database |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1193 */ |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1194 SIDLibSTILNode *sidlib_stildb_get_node(SIDLibSTILDB *dbh, const char *filename) |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1195 { |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1196 SIDLibSTILNode keyItem, *key, **item; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1197 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1198 keyItem.filename = (char *) filename; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1199 key = &keyItem; |
332 | 1200 item = bsearch(&key, dbh->pindex, dbh->nnodes, |
1201 sizeof(SIDLibSTILNode *), sidlib_stildb_compare_nodes); | |
241
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1202 |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1203 return item != NULL ? *item : NULL; |
c9b57c8fd058
Begin work on STIL database parsing and handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
239
diff
changeset
|
1204 } |