improve cue handling a bit
[picodrive.git] / pico / cd / cue.c
CommitLineData
cff531af 1/*
2 * cuefile handling
3 * (C) notaz, 2008
4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
7 */
b923ecbe 8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include "cue.h"
12
efcba75f 13#include "../pico_int.h"
9037e45d 14// #define elprintf(w,f,...) printf(f "\n",##__VA_ARGS__);
b923ecbe 15
67c81ee2 16#ifdef _MSC_VER
17#define snprintf _snprintf
18#endif
f8af9634 19#ifdef __EPOC32__
ca482e5d 20#define snprintf(b,s,...) sprintf(b,##__VA_ARGS__)
21#endif
67c81ee2 22
b923ecbe 23static char *mystrip(char *str)
24{
25 int i, len;
26
27 len = strlen(str);
28 for (i = 0; i < len; i++)
29 if (str[i] != ' ') break;
30 if (i > 0) memmove(str, str + i, len - i + 1);
31
32 len = strlen(str);
33 for (i = len - 1; i >= 0; i--)
34 if (str[i] != ' ' && str[i] != '\r' && str[i] != '\n') break;
35 str[i+1] = 0;
36
37 return str;
38}
39
40static int get_token(const char *buff, char *dest, int len)
41{
42 const char *p = buff;
43 char sep = ' ';
44 int d = 0, skip = 0;
45
46 while (*p && *p == ' ') {
47 skip++;
48 p++;
49 }
50
51 if (*p == '\"') {
52 sep = '\"';
53 p++;
54 }
55 while (*p && *p != sep && d < len-1)
56 dest[d++] = *p++;
57 dest[d] = 0;
58
9037e45d 59 if (sep == '\"' && *p != sep)
b923ecbe 60 elprintf(EL_STATUS, "cue: bad token: \"%s\"", buff);
61
62 return d + skip;
63}
64
e71fae1f 65static int get_ext(const char *fname, char ext[4],
66 char *base, size_t base_size)
b923ecbe 67{
e71fae1f 68 int len, pos = 0;
69
70 len = strlen(fname);
71 if (len >= 3)
72 pos = len - 3;
73
74 strcpy(ext, fname + pos);
75
76 if (base != NULL) {
77 len = pos;
78 if (len + 1 < base_size)
79 len = base_size - 1;
80 memcpy(base, fname, len);
81 base[len] = 0;
82 }
83 return pos;
84}
85
86static void change_case(char *p, int to_upper)
87{
88 for (; *p != 0; p++) {
89 if (to_upper && 'a' <= *p && *p <= 'z')
90 *p += 'A' - 'a';
91 else if (!to_upper && 'A' <= *p && *p <= 'Z')
92 *p += 'a' - 'A';
93 }
b923ecbe 94}
95
e71fae1f 96static int file_openable(const char *fname)
97{
98 FILE *f = fopen(fname, "rb");
99 if (f == NULL)
100 return 0;
101 fclose(f);
102 return 1;
103}
b923ecbe 104
105#define BEGINS(buff,str) (strncmp(buff,str,sizeof(str)-1) == 0)
106
107/* note: tracks[0] is not used */
9037e45d 108cue_data_t *cue_parse(const char *fname)
b923ecbe 109{
e71fae1f 110 char current_file[256], *current_filep, cue_base[256];
111 char buff[256], buff2[32], ext[4], *p;
c9e1affc 112 int ret, count = 0, count_alloc = 2, pending_pregap = 0;
e71fae1f 113 size_t current_filep_size, fname_len;
114 cue_data_t *data = NULL;
115 FILE *f = NULL;
b923ecbe 116 void *tmp;
117
e71fae1f 118 if (fname == NULL || (fname_len = strlen(fname)) == 0)
119 return NULL;
120
121 ret = get_ext(fname, ext, cue_base, sizeof(cue_base));
122 if (strcasecmp(ext, "cue") == 0) {
123 f = fopen(fname, "r");
124 }
125 else {
126 // not a .cue, try one with the same base name
127 if (ret + 3 < sizeof(cue_base)) {
128 strcpy(cue_base + ret, "cue");
129 f = fopen(cue_base, "r");
130 if (f == NULL) {
131 strcpy(cue_base + ret, "CUE");
132 f = fopen(cue_base, "r");
133 }
134 }
135 }
136
137 if (f == NULL)
138 return NULL;
b923ecbe 139
c9e1affc 140 snprintf(current_file, sizeof(current_file), "%s", fname);
e71fae1f 141 current_filep = current_file + strlen(current_file);
142 for (; current_filep > current_file; current_filep--)
143 if (current_filep[-1] == '/' || current_filep[-1] == '\\')
144 break;
145
146 current_filep_size = sizeof(current_file) - (current_filep - current_file);
147
148 // the basename of cuefile, no path
149 snprintf(cue_base, sizeof(cue_base), "%s", current_filep);
150 p = cue_base + strlen(cue_base);
151 if (p - 3 >= cue_base)
152 p[-3] = 0;
c9e1affc 153
b923ecbe 154 data = calloc(1, sizeof(*data) + count_alloc * sizeof(cue_track));
e71fae1f 155 if (data == NULL)
156 goto out;
b923ecbe 157
158 while (!feof(f))
159 {
160 tmp = fgets(buff, sizeof(buff), f);
e71fae1f 161 if (tmp == NULL)
162 break;
b923ecbe 163
164 mystrip(buff);
e71fae1f 165 if (buff[0] == 0)
166 continue;
0bccafeb 167 if (BEGINS(buff, "TITLE ") || BEGINS(buff, "PERFORMER ") || BEGINS(buff, "SONGWRITER "))
b923ecbe 168 continue; /* who would put those here? Ignore! */
169 else if (BEGINS(buff, "FILE "))
170 {
e71fae1f 171 get_token(buff + 5, current_filep, current_filep_size);
b923ecbe 172 }
173 else if (BEGINS(buff, "TRACK "))
174 {
175 count++;
176 if (count >= count_alloc) {
177 count_alloc *= 2;
178 tmp = realloc(data, sizeof(*data) + count_alloc * sizeof(cue_track));
e71fae1f 179 if (tmp == NULL) {
180 count--;
181 break;
182 }
9037e45d 183 data = tmp;
b923ecbe 184 }
185 memset(&data->tracks[count], 0, sizeof(data->tracks[0]));
e71fae1f 186
b923ecbe 187 if (count == 1 || strcmp(data->tracks[1].fname, current_file) != 0)
188 {
e71fae1f 189 if (file_openable(current_file))
190 goto file_ok;
191
192 elprintf(EL_STATUS, "cue: bad/missing file: \"%s\"", current_file);
193 if (count == 1) {
194 int cue_ucase;
195 char v;
196
197 get_ext(current_file, ext, NULL, 0);
198 snprintf(current_filep, current_filep_size,
199 "%s%s", cue_base, ext);
200 if (file_openable(current_file))
201 goto file_ok;
202
203 // try with the same case (for unix)
204 v = fname[fname_len - 1];
205 cue_ucase = ('A' <= v && v <= 'Z');
206 change_case(ext, cue_ucase);
207
208 snprintf(current_filep, current_filep_size,
209 "%s%s", cue_base, ext);
210 if (file_openable(current_file))
211 goto file_ok;
b923ecbe 212 }
e71fae1f 213
214 count--;
215 break;
216
217file_ok:
218 data->tracks[count].fname = strdup(current_file);
219 if (data->tracks[count].fname == NULL)
220 break;
b923ecbe 221 }
c9e1affc 222 data->tracks[count].pregap = pending_pregap;
223 pending_pregap = 0;
b923ecbe 224 // track number
225 ret = get_token(buff+6, buff2, sizeof(buff2));
226 if (count != atoi(buff2))
227 elprintf(EL_STATUS, "cue: track index mismatch: track %i is track %i in cue",
228 count, atoi(buff2));
229 // check type
230 get_token(buff+6+ret, buff2, sizeof(buff2));
9037e45d 231 if (strcmp(buff2, "MODE1/2352") == 0)
b923ecbe 232 data->tracks[count].type = CT_BIN;
9037e45d 233 else if (strcmp(buff2, "MODE1/2048") == 0)
b923ecbe 234 data->tracks[count].type = CT_ISO;
9037e45d 235 else if (strcmp(buff2, "AUDIO") == 0)
b923ecbe 236 {
237 if (data->tracks[count].fname != NULL)
238 {
239 // rely on extension, not type in cue..
e71fae1f 240 get_ext(data->tracks[count].fname, ext, NULL, 0);
b923ecbe 241 if (strcasecmp(ext, "mp3") == 0)
242 data->tracks[count].type = CT_MP3;
243 else if (strcasecmp(ext, "wav") == 0)
244 data->tracks[count].type = CT_WAV;
245 else {
246 elprintf(EL_STATUS, "unhandled audio format: \"%s\"",
247 data->tracks[count].fname);
248 }
249 }
9037e45d 250 else
251 {
252 // propagate previous
253 data->tracks[count].type = data->tracks[count-1].type;
254 }
b923ecbe 255 }
256 else {
257 elprintf(EL_STATUS, "unhandled track type: \"%s\"", buff2);
258 }
259 }
260 else if (BEGINS(buff, "INDEX "))
261 {
262 int m, s, f;
263 // type
264 ret = get_token(buff+6, buff2, sizeof(buff2));
265 if (atoi(buff2) == 0) continue;
266 if (atoi(buff2) != 1) {
267 elprintf(EL_STATUS, "cue: don't know how to handle: \"%s\"", buff);
268 count--; break;
269 }
270 // offset in file
271 get_token(buff+6+ret, buff2, sizeof(buff2));
9037e45d 272 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
b923ecbe 273 if (ret != 3) {
274 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
275 count--; break;
276 }
277 data->tracks[count].sector_offset = m*60*75 + s*75 + f;
9037e45d 278 // some strange .cues may need this
279 if (data->tracks[count].fname != NULL && strcmp(data->tracks[count].fname, current_file) != 0)
280 {
281 free(data->tracks[count].fname);
282 data->tracks[count].fname = strdup(current_file);
283 }
284 if (data->tracks[count].fname == NULL && strcmp(data->tracks[1].fname, current_file) != 0)
285 {
286 data->tracks[count].fname = strdup(current_file);
287 }
b923ecbe 288 }
c9e1affc 289 else if (BEGINS(buff, "PREGAP ") || BEGINS(buff, "POSTGAP "))
b923ecbe 290 {
291 int m, s, f;
292 get_token(buff+7, buff2, sizeof(buff2));
9037e45d 293 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
b923ecbe 294 if (ret != 3) {
295 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
296 continue;
297 }
c9e1affc 298 // pregap overrides previous postgap?
299 // by looking at some .cues produced by some programs I've decided that..
300 if (BEGINS(buff, "PREGAP "))
301 data->tracks[count].pregap = m*60*75 + s*75 + f;
302 else
303 pending_pregap = m*60*75 + s*75 + f;
b923ecbe 304 }
0bccafeb 305 else if (BEGINS(buff, "REM LENGTH ")) // custom "extension"
306 {
307 int m, s, f;
308 get_token(buff+11, buff2, sizeof(buff2));
309 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
310 if (ret != 3) continue;
311 data->tracks[count].sector_xlength = m*60*75 + s*75 + f;
312 }
313 else if (BEGINS(buff, "REM"))
314 continue;
b923ecbe 315 else
316 {
9037e45d 317 elprintf(EL_STATUS, "cue: unhandled line: \"%s\"", buff);
b923ecbe 318 }
319 }
320
321 if (count < 1 || data->tracks[1].fname == NULL) {
322 // failed..
323 for (; count > 0; count--)
324 if (data->tracks[count].fname != NULL)
325 free(data->tracks[count].fname);
326 free(data);
e71fae1f 327 data = NULL;
328 goto out;
b923ecbe 329 }
330
331 data->track_count = count;
e71fae1f 332
333out:
334 if (f != NULL)
335 fclose(f);
b923ecbe 336 return data;
337}
338
339
9037e45d 340void cue_destroy(cue_data_t *data)
b923ecbe 341{
342 int c;
343
344 if (data == NULL) return;
345
346 for (c = data->track_count; c > 0; c--)
347 if (data->tracks[c].fname != NULL)
348 free(data->tracks[c].fname);
349 free(data);
350}
351
352
9037e45d 353#if 0
b923ecbe 354int main(int argc, char *argv[])
355{
9037e45d 356 cue_data_t *data = cue_parse(argv[1]);
b923ecbe 357 int c;
358
359 if (data == NULL) return 1;
360
361 for (c = 1; c <= data->track_count; c++)
9037e45d 362 printf("%2i: %i %9i %02i:%02i:%02i %9i %s\n", c, data->tracks[c].type, data->tracks[c].sector_offset,
363 data->tracks[c].sector_offset / (75*60), data->tracks[c].sector_offset / 75 % 60,
364 data->tracks[c].sector_offset % 75, data->tracks[c].pregap, data->tracks[c].fname);
b923ecbe 365
366 cue_destroy(data);
367
368 return 0;
369}
9037e45d 370#endif
b923ecbe 371