annotate sidlib.c @ 409:5a27c0fbbf20 default tip

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