5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
13 #include "../pico_int.h"
14 // #define elprintf(w,f,...) printf(f "\n",##__VA_ARGS__);
17 #define snprintf _snprintf
20 #define snprintf(b,s,...) sprintf(b,##__VA_ARGS__)
23 static char *mystrip(char *str)
28 for (i = 0; i < len; i++)
29 if (str[i] != ' ') break;
30 if (i > 0) memmove(str, str + i, len - i + 1);
33 for (i = len - 1; i >= 0; i--)
34 if (str[i] != ' ' && str[i] != '\r' && str[i] != '\n') break;
40 static int get_token(const char *buff, char *dest, int len)
46 while (*p && *p == ' ') {
55 while (*p && *p != sep && d < len-1)
59 if (sep == '\"' && *p != sep)
60 elprintf(EL_STATUS, "cue: bad token: \"%s\"", buff);
65 static int get_ext(const char *fname, char ext[4],
66 char *base, size_t base_size)
74 strcpy(ext, fname + pos);
78 if (len + 1 < base_size)
80 memcpy(base, fname, len);
86 static void change_case(char *p, int to_upper)
88 for (; *p != 0; p++) {
89 if (to_upper && 'a' <= *p && *p <= 'z')
91 else if (!to_upper && 'A' <= *p && *p <= 'Z')
96 static int file_openable(const char *fname)
98 FILE *f = fopen(fname, "rb");
105 #define BEGINS(buff,str) (strncmp(buff,str,sizeof(str)-1) == 0)
107 /* note: tracks[0] is not used */
108 cue_data_t *cue_parse(const char *fname)
110 char current_file[256], *current_filep, cue_base[256];
111 char buff[256], buff2[32], ext[4], *p;
112 int ret, count = 0, count_alloc = 2, pending_pregap = 0;
113 size_t current_filep_size, fname_len;
114 cue_data_t *data = NULL;
118 if (fname == NULL || (fname_len = strlen(fname)) == 0)
121 ret = get_ext(fname, ext, cue_base, sizeof(cue_base));
122 if (strcasecmp(ext, "cue") == 0) {
123 f = fopen(fname, "r");
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");
131 strcpy(cue_base + ret, "CUE");
132 f = fopen(cue_base, "r");
140 snprintf(current_file, sizeof(current_file), "%s", fname);
141 current_filep = current_file + strlen(current_file);
142 for (; current_filep > current_file; current_filep--)
143 if (current_filep[-1] == '/' || current_filep[-1] == '\\')
146 current_filep_size = sizeof(current_file) - (current_filep - current_file);
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)
154 data = calloc(1, sizeof(*data) + count_alloc * sizeof(cue_track));
160 tmp = fgets(buff, sizeof(buff), f);
167 if (BEGINS(buff, "TITLE ") || BEGINS(buff, "PERFORMER ") || BEGINS(buff, "SONGWRITER "))
168 continue; /* who would put those here? Ignore! */
169 else if (BEGINS(buff, "FILE "))
171 get_token(buff + 5, current_filep, current_filep_size);
173 else if (BEGINS(buff, "TRACK "))
176 if (count >= count_alloc) {
178 tmp = realloc(data, sizeof(*data) + count_alloc * sizeof(cue_track));
185 memset(&data->tracks[count], 0, sizeof(data->tracks[0]));
187 if (count == 1 || strcmp(data->tracks[1].fname, current_file) != 0)
189 if (file_openable(current_file))
192 elprintf(EL_STATUS, "cue: bad/missing file: \"%s\"", current_file);
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))
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);
208 snprintf(current_filep, current_filep_size,
209 "%s%s", cue_base, ext);
210 if (file_openable(current_file))
218 data->tracks[count].fname = strdup(current_file);
219 if (data->tracks[count].fname == NULL)
222 data->tracks[count].pregap = pending_pregap;
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",
230 get_token(buff+6+ret, buff2, sizeof(buff2));
231 if (strcmp(buff2, "MODE1/2352") == 0)
232 data->tracks[count].type = CT_BIN;
233 else if (strcmp(buff2, "MODE1/2048") == 0)
234 data->tracks[count].type = CT_ISO;
235 else if (strcmp(buff2, "AUDIO") == 0)
237 if (data->tracks[count].fname != NULL)
239 // rely on extension, not type in cue..
240 get_ext(data->tracks[count].fname, ext, NULL, 0);
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;
246 elprintf(EL_STATUS, "unhandled audio format: \"%s\"",
247 data->tracks[count].fname);
252 // propagate previous
253 data->tracks[count].type = data->tracks[count-1].type;
257 elprintf(EL_STATUS, "unhandled track type: \"%s\"", buff2);
260 else if (BEGINS(buff, "INDEX "))
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);
271 get_token(buff+6+ret, buff2, sizeof(buff2));
272 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
274 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
277 data->tracks[count].sector_offset = m*60*75 + s*75 + f;
278 // some strange .cues may need this
279 if (data->tracks[count].fname != NULL && strcmp(data->tracks[count].fname, current_file) != 0)
281 free(data->tracks[count].fname);
282 data->tracks[count].fname = strdup(current_file);
284 if (data->tracks[count].fname == NULL && strcmp(data->tracks[1].fname, current_file) != 0)
286 data->tracks[count].fname = strdup(current_file);
289 else if (BEGINS(buff, "PREGAP ") || BEGINS(buff, "POSTGAP "))
292 get_token(buff+7, buff2, sizeof(buff2));
293 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
295 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
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;
303 pending_pregap = m*60*75 + s*75 + f;
305 else if (BEGINS(buff, "REM LENGTH ")) // custom "extension"
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;
313 else if (BEGINS(buff, "REM"))
317 elprintf(EL_STATUS, "cue: unhandled line: \"%s\"", buff);
321 if (count < 1 || data->tracks[1].fname == NULL) {
323 for (; count > 0; count--)
324 if (data->tracks[count].fname != NULL)
325 free(data->tracks[count].fname);
331 data->track_count = count;
340 void cue_destroy(cue_data_t *data)
344 if (data == NULL) return;
346 for (c = data->track_count; c > 0; c--)
347 if (data->tracks[c].fname != NULL)
348 free(data->tracks[c].fname);
354 int main(int argc, char *argv[])
356 cue_data_t *data = cue_parse(argv[1]);
359 if (data == NULL) return 1;
361 for (c = 1; c <= data->track_count; c++)
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);