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