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