annotate sidlib.c @ 264:5afa6052f796

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