.cue support, Pico stubs
[picodrive.git] / Pico / cd / cue.c
CommitLineData
b923ecbe 1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include "cue.h"
5
9037e45d 6#include "../PicoInt.h"
7// #define elprintf(w,f,...) printf(f "\n",##__VA_ARGS__);
b923ecbe 8
9static char *mystrip(char *str)
10{
11 int i, len;
12
13 len = strlen(str);
14 for (i = 0; i < len; i++)
15 if (str[i] != ' ') break;
16 if (i > 0) memmove(str, str + i, len - i + 1);
17
18 len = strlen(str);
19 for (i = len - 1; i >= 0; i--)
20 if (str[i] != ' ' && str[i] != '\r' && str[i] != '\n') break;
21 str[i+1] = 0;
22
23 return str;
24}
25
26static int get_token(const char *buff, char *dest, int len)
27{
28 const char *p = buff;
29 char sep = ' ';
30 int d = 0, skip = 0;
31
32 while (*p && *p == ' ') {
33 skip++;
34 p++;
35 }
36
37 if (*p == '\"') {
38 sep = '\"';
39 p++;
40 }
41 while (*p && *p != sep && d < len-1)
42 dest[d++] = *p++;
43 dest[d] = 0;
44
9037e45d 45 if (sep == '\"' && *p != sep)
b923ecbe 46 elprintf(EL_STATUS, "cue: bad token: \"%s\"", buff);
47
48 return d + skip;
49}
50
51static char *get_ext(char *fname)
52{
53 int len = strlen(fname);
9037e45d 54 return (len >= 3) ? (fname + len - 3) : fname;
b923ecbe 55}
56
57
58#define BEGINS(buff,str) (strncmp(buff,str,sizeof(str)-1) == 0)
59
60/* note: tracks[0] is not used */
9037e45d 61cue_data_t *cue_parse(const char *fname)
b923ecbe 62{
63 char buff[256], current_file[256], buff2[32];
64 FILE *f, *tmpf;
65 int ret, count = 0, count_alloc = 2;
9037e45d 66 cue_data_t *data;
b923ecbe 67 void *tmp;
68
69 f = fopen(fname, "r");
70 if (f == NULL) return NULL;
71
72 current_file[0] = 0;
73 data = calloc(1, sizeof(*data) + count_alloc * sizeof(cue_track));
74
75 while (!feof(f))
76 {
77 tmp = fgets(buff, sizeof(buff), f);
78 if (tmp == NULL) break;
79
80 mystrip(buff);
81 if (buff[0] == 0) continue;
82 if (BEGINS(buff, "TITLE ") || BEGINS(buff, "PERFORMER "))
83 continue; /* who would put those here? Ignore! */
84 else if (BEGINS(buff, "FILE "))
85 {
86 get_token(buff+5, current_file, sizeof(current_file));
87 }
88 else if (BEGINS(buff, "TRACK "))
89 {
90 count++;
91 if (count >= count_alloc) {
92 count_alloc *= 2;
93 tmp = realloc(data, sizeof(*data) + count_alloc * sizeof(cue_track));
94 if (tmp == NULL) { count--; break; }
9037e45d 95 data = tmp;
b923ecbe 96 }
97 memset(&data->tracks[count], 0, sizeof(data->tracks[0]));
98 if (count == 1 || strcmp(data->tracks[1].fname, current_file) != 0)
99 {
100 data->tracks[count].fname = strdup(current_file);
101 if (data->tracks[count].fname == NULL) break;
102
103 tmpf = fopen(current_file, "rb");
104 if (tmpf == NULL) {
105 elprintf(EL_STATUS, "cue: bad/missing file: \"%s\"", current_file);
106 count--; break;
107 }
108 fclose(tmpf);
109 }
110 // track number
111 ret = get_token(buff+6, buff2, sizeof(buff2));
112 if (count != atoi(buff2))
113 elprintf(EL_STATUS, "cue: track index mismatch: track %i is track %i in cue",
114 count, atoi(buff2));
115 // check type
116 get_token(buff+6+ret, buff2, sizeof(buff2));
9037e45d 117 if (strcmp(buff2, "MODE1/2352") == 0)
b923ecbe 118 data->tracks[count].type = CT_BIN;
9037e45d 119 else if (strcmp(buff2, "MODE1/2048") == 0)
b923ecbe 120 data->tracks[count].type = CT_ISO;
9037e45d 121 else if (strcmp(buff2, "AUDIO") == 0)
b923ecbe 122 {
123 if (data->tracks[count].fname != NULL)
124 {
125 // rely on extension, not type in cue..
126 char *ext = get_ext(data->tracks[count].fname);
127 if (strcasecmp(ext, "mp3") == 0)
128 data->tracks[count].type = CT_MP3;
129 else if (strcasecmp(ext, "wav") == 0)
130 data->tracks[count].type = CT_WAV;
131 else {
132 elprintf(EL_STATUS, "unhandled audio format: \"%s\"",
133 data->tracks[count].fname);
134 }
135 }
9037e45d 136 else
137 {
138 // propagate previous
139 data->tracks[count].type = data->tracks[count-1].type;
140 }
b923ecbe 141 }
142 else {
143 elprintf(EL_STATUS, "unhandled track type: \"%s\"", buff2);
144 }
145 }
146 else if (BEGINS(buff, "INDEX "))
147 {
148 int m, s, f;
149 // type
150 ret = get_token(buff+6, buff2, sizeof(buff2));
151 if (atoi(buff2) == 0) continue;
152 if (atoi(buff2) != 1) {
153 elprintf(EL_STATUS, "cue: don't know how to handle: \"%s\"", buff);
154 count--; break;
155 }
156 // offset in file
157 get_token(buff+6+ret, buff2, sizeof(buff2));
9037e45d 158 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
b923ecbe 159 if (ret != 3) {
160 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
161 count--; break;
162 }
163 data->tracks[count].sector_offset = m*60*75 + s*75 + f;
9037e45d 164 // some strange .cues may need this
165 if (data->tracks[count].fname != NULL && strcmp(data->tracks[count].fname, current_file) != 0)
166 {
167 free(data->tracks[count].fname);
168 data->tracks[count].fname = strdup(current_file);
169 }
170 if (data->tracks[count].fname == NULL && strcmp(data->tracks[1].fname, current_file) != 0)
171 {
172 data->tracks[count].fname = strdup(current_file);
173 }
b923ecbe 174 }
175 else if (BEGINS(buff, "PREGAP "))
176 {
177 int m, s, f;
178 get_token(buff+7, buff2, sizeof(buff2));
9037e45d 179 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
b923ecbe 180 if (ret != 3) {
181 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
182 continue;
183 }
184 data->tracks[count].pregap = m*60*75 + s*75 + f;
185 }
186 else
187 {
9037e45d 188 elprintf(EL_STATUS, "cue: unhandled line: \"%s\"", buff);
b923ecbe 189 }
190 }
191
192 if (count < 1 || data->tracks[1].fname == NULL) {
193 // failed..
194 for (; count > 0; count--)
195 if (data->tracks[count].fname != NULL)
196 free(data->tracks[count].fname);
197 free(data);
198 return NULL;
199 }
200
201 data->track_count = count;
202 return data;
203}
204
205
9037e45d 206void cue_destroy(cue_data_t *data)
b923ecbe 207{
208 int c;
209
210 if (data == NULL) return;
211
212 for (c = data->track_count; c > 0; c--)
213 if (data->tracks[c].fname != NULL)
214 free(data->tracks[c].fname);
215 free(data);
216}
217
218
9037e45d 219#if 0
b923ecbe 220int main(int argc, char *argv[])
221{
9037e45d 222 cue_data_t *data = cue_parse(argv[1]);
b923ecbe 223 int c;
224
225 if (data == NULL) return 1;
226
227 for (c = 1; c <= data->track_count; c++)
9037e45d 228 printf("%2i: %i %9i %02i:%02i:%02i %9i %s\n", c, data->tracks[c].type, data->tracks[c].sector_offset,
229 data->tracks[c].sector_offset / (75*60), data->tracks[c].sector_offset / 75 % 60,
230 data->tracks[c].sector_offset % 75, data->tracks[c].pregap, data->tracks[c].fname);
b923ecbe 231
232 cue_destroy(data);
233
234 return 0;
235}
9037e45d 236#endif
b923ecbe 237