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];
65 int ret, count = 0, count_alloc = 2;
69 f = fopen(fname, "r");
70 if (f == NULL) return NULL;
73 data = calloc(1, sizeof(*data) + count_alloc * sizeof(cue_track));
77 tmp = fgets(buff, sizeof(buff), f);
78 if (tmp == NULL) break;
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 "))
86 get_token(buff+5, current_file, sizeof(current_file));
88 else if (BEGINS(buff, "TRACK "))
91 if (count >= count_alloc) {
93 tmp = realloc(data, sizeof(*data) + count_alloc * sizeof(cue_track));
94 if (tmp == NULL) { count--; break; }
97 memset(&data->tracks[count], 0, sizeof(data->tracks[0]));
98 if (count == 1 || strcmp(data->tracks[1].fname, current_file) != 0)
100 data->tracks[count].fname = strdup(current_file);
101 if (data->tracks[count].fname == NULL) break;
103 tmpf = fopen(current_file, "rb");
105 elprintf(EL_STATUS, "cue: bad/missing file: \"%s\"", current_file);
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",
116 get_token(buff+6+ret, buff2, sizeof(buff2));
117 if (strcmp(buff2, "MODE1/2352") == 0)
118 data->tracks[count].type = CT_BIN;
119 else if (strcmp(buff2, "MODE1/2048") == 0)
120 data->tracks[count].type = CT_ISO;
121 else if (strcmp(buff2, "AUDIO") == 0)
123 if (data->tracks[count].fname != NULL)
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;
132 elprintf(EL_STATUS, "unhandled audio format: \"%s\"",
133 data->tracks[count].fname);
138 // propagate previous
139 data->tracks[count].type = data->tracks[count-1].type;
143 elprintf(EL_STATUS, "unhandled track type: \"%s\"", buff2);
146 else if (BEGINS(buff, "INDEX "))
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);
157 get_token(buff+6+ret, buff2, sizeof(buff2));
158 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
160 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
163 data->tracks[count].sector_offset = m*60*75 + s*75 + f;
164 // some strange .cues may need this
165 if (data->tracks[count].fname != NULL && strcmp(data->tracks[count].fname, current_file) != 0)
167 free(data->tracks[count].fname);
168 data->tracks[count].fname = strdup(current_file);
170 if (data->tracks[count].fname == NULL && strcmp(data->tracks[1].fname, current_file) != 0)
172 data->tracks[count].fname = strdup(current_file);
175 else if (BEGINS(buff, "PREGAP "))
178 get_token(buff+7, buff2, sizeof(buff2));
179 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
181 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
184 data->tracks[count].pregap = m*60*75 + s*75 + f;
188 elprintf(EL_STATUS, "cue: unhandled line: \"%s\"", buff);
192 if (count < 1 || data->tracks[1].fname == NULL) {
194 for (; count > 0; count--)
195 if (data->tracks[count].fname != NULL)
196 free(data->tracks[count].fname);
201 data->track_count = count;
206 void cue_destroy(cue_data_t *data)
210 if (data == NULL) return;
212 for (c = data->track_count; c > 0; c--)
213 if (data->tracks[c].fname != NULL)
214 free(data->tracks[c].fname);
220 int main(int argc, char *argv[])
222 cue_data_t *data = cue_parse(argv[1]);
225 if (data == NULL) return 1;
227 for (c = 1; c <= data->track_count; c++)
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);