6 #include "../PicoInt.h"
7 // #define elprintf(w,f,...) printf(f "\n",##__VA_ARGS__);
10 #define snprintf _snprintf
13 static char *mystrip(char *str)
18 for (i = 0; i < len; i++)
19 if (str[i] != ' ') break;
20 if (i > 0) memmove(str, str + i, len - i + 1);
23 for (i = len - 1; i >= 0; i--)
24 if (str[i] != ' ' && str[i] != '\r' && str[i] != '\n') break;
30 static int get_token(const char *buff, char *dest, int len)
36 while (*p && *p == ' ') {
45 while (*p && *p != sep && d < len-1)
49 if (sep == '\"' && *p != sep)
50 elprintf(EL_STATUS, "cue: bad token: \"%s\"", buff);
55 static char *get_ext(char *fname)
57 int len = strlen(fname);
58 return (len >= 3) ? (fname + len - 3) : fname;
62 #define BEGINS(buff,str) (strncmp(buff,str,sizeof(str)-1) == 0)
64 /* note: tracks[0] is not used */
65 cue_data_t *cue_parse(const char *fname)
67 char buff[256], current_file[256], buff2[32], *current_filep;
69 int ret, count = 0, count_alloc = 2, pending_pregap = 0;
73 f = fopen(fname, "r");
74 if (f == NULL) return NULL;
76 snprintf(current_file, sizeof(current_file), "%s", fname);
77 for (current_filep = current_file + strlen(current_file); current_filep > current_file; current_filep--)
78 if (current_filep[-1] == '/' || current_filep[-1] == '\\') break;
80 data = calloc(1, sizeof(*data) + count_alloc * sizeof(cue_track));
88 tmp = fgets(buff, sizeof(buff), f);
89 if (tmp == NULL) break;
92 if (buff[0] == 0) continue;
93 if (BEGINS(buff, "TITLE ") || BEGINS(buff, "PERFORMER ") || BEGINS(buff, "SONGWRITER "))
94 continue; /* who would put those here? Ignore! */
95 else if (BEGINS(buff, "FILE "))
97 get_token(buff+5, current_filep, sizeof(current_file) - (current_filep - current_file));
99 else if (BEGINS(buff, "TRACK "))
102 if (count >= count_alloc) {
104 tmp = realloc(data, sizeof(*data) + count_alloc * sizeof(cue_track));
105 if (tmp == NULL) { count--; break; }
108 memset(&data->tracks[count], 0, sizeof(data->tracks[0]));
109 if (count == 1 || strcmp(data->tracks[1].fname, current_file) != 0)
111 data->tracks[count].fname = strdup(current_file);
112 if (data->tracks[count].fname == NULL) break;
114 tmpf = fopen(current_file, "rb");
116 elprintf(EL_STATUS, "cue: bad/missing file: \"%s\"", current_file);
121 data->tracks[count].pregap = pending_pregap;
124 ret = get_token(buff+6, buff2, sizeof(buff2));
125 if (count != atoi(buff2))
126 elprintf(EL_STATUS, "cue: track index mismatch: track %i is track %i in cue",
129 get_token(buff+6+ret, buff2, sizeof(buff2));
130 if (strcmp(buff2, "MODE1/2352") == 0)
131 data->tracks[count].type = CT_BIN;
132 else if (strcmp(buff2, "MODE1/2048") == 0)
133 data->tracks[count].type = CT_ISO;
134 else if (strcmp(buff2, "AUDIO") == 0)
136 if (data->tracks[count].fname != NULL)
138 // rely on extension, not type in cue..
139 char *ext = get_ext(data->tracks[count].fname);
140 if (strcasecmp(ext, "mp3") == 0)
141 data->tracks[count].type = CT_MP3;
142 else if (strcasecmp(ext, "wav") == 0)
143 data->tracks[count].type = CT_WAV;
145 elprintf(EL_STATUS, "unhandled audio format: \"%s\"",
146 data->tracks[count].fname);
151 // propagate previous
152 data->tracks[count].type = data->tracks[count-1].type;
156 elprintf(EL_STATUS, "unhandled track type: \"%s\"", buff2);
159 else if (BEGINS(buff, "INDEX "))
163 ret = get_token(buff+6, buff2, sizeof(buff2));
164 if (atoi(buff2) == 0) continue;
165 if (atoi(buff2) != 1) {
166 elprintf(EL_STATUS, "cue: don't know how to handle: \"%s\"", buff);
170 get_token(buff+6+ret, buff2, sizeof(buff2));
171 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
173 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
176 data->tracks[count].sector_offset = m*60*75 + s*75 + f;
177 // some strange .cues may need this
178 if (data->tracks[count].fname != NULL && strcmp(data->tracks[count].fname, current_file) != 0)
180 free(data->tracks[count].fname);
181 data->tracks[count].fname = strdup(current_file);
183 if (data->tracks[count].fname == NULL && strcmp(data->tracks[1].fname, current_file) != 0)
185 data->tracks[count].fname = strdup(current_file);
188 else if (BEGINS(buff, "PREGAP ") || BEGINS(buff, "POSTGAP "))
191 get_token(buff+7, buff2, sizeof(buff2));
192 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
194 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
197 // pregap overrides previous postgap?
198 // by looking at some .cues produced by some programs I've decided that..
199 if (BEGINS(buff, "PREGAP "))
200 data->tracks[count].pregap = m*60*75 + s*75 + f;
202 pending_pregap = m*60*75 + s*75 + f;
204 else if (BEGINS(buff, "REM LENGTH ")) // custom "extension"
207 get_token(buff+11, buff2, sizeof(buff2));
208 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
209 if (ret != 3) continue;
210 data->tracks[count].sector_xlength = m*60*75 + s*75 + f;
212 else if (BEGINS(buff, "REM"))
216 elprintf(EL_STATUS, "cue: unhandled line: \"%s\"", buff);
220 if (count < 1 || data->tracks[1].fname == NULL) {
222 for (; count > 0; count--)
223 if (data->tracks[count].fname != NULL)
224 free(data->tracks[count].fname);
229 data->track_count = count;
234 void cue_destroy(cue_data_t *data)
238 if (data == NULL) return;
240 for (c = data->track_count; c > 0; c--)
241 if (data->tracks[c].fname != NULL)
242 free(data->tracks[c].fname);
248 int main(int argc, char *argv[])
250 cue_data_t *data = cue_parse(argv[1]);
253 if (data == NULL) return 1;
255 for (c = 1; c <= data->track_count; c++)
256 printf("%2i: %i %9i %02i:%02i:%02i %9i %s\n", c, data->tracks[c].type, data->tracks[c].sector_offset,
257 data->tracks[c].sector_offset / (75*60), data->tracks[c].sector_offset / 75 % 60,
258 data->tracks[c].sector_offset % 75, data->tracks[c].pregap, data->tracks[c].fname);