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