6 #include "../PicoInt.h"
7 // #define elprintf(w,f,...) printf(f "\n",##__VA_ARGS__);
9 static char *mystrip(char *str)
14 for (i = 0; i < len; i++)
15 if (str[i] != ' ') break;
16 if (i > 0) memmove(str, str + i, len - i + 1);
19 for (i = len - 1; i >= 0; i--)
20 if (str[i] != ' ' && str[i] != '\r' && str[i] != '\n') break;
26 static int get_token(const char *buff, char *dest, int len)
32 while (*p && *p == ' ') {
41 while (*p && *p != sep && d < len-1)
45 if (sep == '\"' && *p != sep)
46 elprintf(EL_STATUS, "cue: bad token: \"%s\"", buff);
51 static char *get_ext(char *fname)
53 int len = strlen(fname);
54 return (len >= 3) ? (fname + len - 3) : fname;
58 #define BEGINS(buff,str) (strncmp(buff,str,sizeof(str)-1) == 0)
60 /* note: tracks[0] is not used */
61 cue_data_t *cue_parse(const char *fname)
63 char buff[256], current_file[256], buff2[32], *current_filep;
65 int ret, count = 0, count_alloc = 2, pending_pregap = 0;
69 f = fopen(fname, "r");
70 if (f == NULL) return NULL;
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;
76 data = calloc(1, sizeof(*data) + count_alloc * sizeof(cue_track));
84 tmp = fgets(buff, sizeof(buff), f);
85 if (tmp == NULL) break;
88 if (buff[0] == 0) continue;
89 if (BEGINS(buff, "TITLE ") || BEGINS(buff, "PERFORMER ") || BEGINS(buff, "SONGWRITER "))
90 continue; /* who would put those here? Ignore! */
91 else if (BEGINS(buff, "FILE "))
93 get_token(buff+5, current_filep, sizeof(current_file) - (current_filep - current_file));
95 else if (BEGINS(buff, "TRACK "))
98 if (count >= count_alloc) {
100 tmp = realloc(data, sizeof(*data) + count_alloc * sizeof(cue_track));
101 if (tmp == NULL) { count--; break; }
104 memset(&data->tracks[count], 0, sizeof(data->tracks[0]));
105 if (count == 1 || strcmp(data->tracks[1].fname, current_file) != 0)
107 data->tracks[count].fname = strdup(current_file);
108 if (data->tracks[count].fname == NULL) break;
110 tmpf = fopen(current_file, "rb");
112 elprintf(EL_STATUS, "cue: bad/missing file: \"%s\"", current_file);
117 data->tracks[count].pregap = pending_pregap;
120 ret = get_token(buff+6, buff2, sizeof(buff2));
121 if (count != atoi(buff2))
122 elprintf(EL_STATUS, "cue: track index mismatch: track %i is track %i in cue",
125 get_token(buff+6+ret, buff2, sizeof(buff2));
126 if (strcmp(buff2, "MODE1/2352") == 0)
127 data->tracks[count].type = CT_BIN;
128 else if (strcmp(buff2, "MODE1/2048") == 0)
129 data->tracks[count].type = CT_ISO;
130 else if (strcmp(buff2, "AUDIO") == 0)
132 if (data->tracks[count].fname != NULL)
134 // rely on extension, not type in cue..
135 char *ext = get_ext(data->tracks[count].fname);
136 if (strcasecmp(ext, "mp3") == 0)
137 data->tracks[count].type = CT_MP3;
138 else if (strcasecmp(ext, "wav") == 0)
139 data->tracks[count].type = CT_WAV;
141 elprintf(EL_STATUS, "unhandled audio format: \"%s\"",
142 data->tracks[count].fname);
147 // propagate previous
148 data->tracks[count].type = data->tracks[count-1].type;
152 elprintf(EL_STATUS, "unhandled track type: \"%s\"", buff2);
155 else if (BEGINS(buff, "INDEX "))
159 ret = get_token(buff+6, buff2, sizeof(buff2));
160 if (atoi(buff2) == 0) continue;
161 if (atoi(buff2) != 1) {
162 elprintf(EL_STATUS, "cue: don't know how to handle: \"%s\"", buff);
166 get_token(buff+6+ret, buff2, sizeof(buff2));
167 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
169 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
172 data->tracks[count].sector_offset = m*60*75 + s*75 + f;
173 // some strange .cues may need this
174 if (data->tracks[count].fname != NULL && strcmp(data->tracks[count].fname, current_file) != 0)
176 free(data->tracks[count].fname);
177 data->tracks[count].fname = strdup(current_file);
179 if (data->tracks[count].fname == NULL && strcmp(data->tracks[1].fname, current_file) != 0)
181 data->tracks[count].fname = strdup(current_file);
184 else if (BEGINS(buff, "PREGAP ") || BEGINS(buff, "POSTGAP "))
187 get_token(buff+7, buff2, sizeof(buff2));
188 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
190 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
193 // pregap overrides previous postgap?
194 // by looking at some .cues produced by some programs I've decided that..
195 if (BEGINS(buff, "PREGAP "))
196 data->tracks[count].pregap = m*60*75 + s*75 + f;
198 pending_pregap = m*60*75 + s*75 + f;
200 else if (BEGINS(buff, "REM LENGTH ")) // custom "extension"
203 get_token(buff+11, buff2, sizeof(buff2));
204 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
205 if (ret != 3) continue;
206 data->tracks[count].sector_xlength = m*60*75 + s*75 + f;
208 else if (BEGINS(buff, "REM"))
212 elprintf(EL_STATUS, "cue: unhandled line: \"%s\"", buff);
216 if (count < 1 || data->tracks[1].fname == NULL) {
218 for (; count > 0; count--)
219 if (data->tracks[count].fname != NULL)
220 free(data->tracks[count].fname);
225 data->track_count = count;
230 void cue_destroy(cue_data_t *data)
234 if (data == NULL) return;
236 for (c = data->track_count; c > 0; c--)
237 if (data->tracks[c].fname != NULL)
238 free(data->tracks[c].fname);
244 int main(int argc, char *argv[])
246 cue_data_t *data = cue_parse(argv[1]);
249 if (data == NULL) return 1;
251 for (c = 1; c <= data->track_count; c++)
252 printf("%2i: %i %9i %02i:%02i:%02i %9i %s\n", c, data->tracks[c].type, data->tracks[c].sector_offset,
253 data->tracks[c].sector_offset / (75*60), data->tracks[c].sector_offset / 75 % 60,
254 data->tracks[c].sector_offset % 75, data->tracks[c].pregap, data->tracks[c].fname);