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)
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 - 3) : (fname - len);
58 #define BEGINS(buff,str) (strncmp(buff,str,sizeof(str)-1) == 0)
60 /* note: tracks[0] is not used */
61 cue_data *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; }
96 memset(&data->tracks[count], 0, sizeof(data->tracks[0]));
97 if (count == 1 || strcmp(data->tracks[1].fname, current_file) != 0)
99 data->tracks[count].fname = strdup(current_file);
100 if (data->tracks[count].fname == NULL) break;
102 tmpf = fopen(current_file, "rb");
104 elprintf(EL_STATUS, "cue: bad/missing file: \"%s\"", current_file);
110 ret = get_token(buff+6, buff2, sizeof(buff2));
111 if (count != atoi(buff2))
112 elprintf(EL_STATUS, "cue: track index mismatch: track %i is track %i in cue",
115 get_token(buff+6+ret, buff2, sizeof(buff2));
116 if (strcmp(buff2, "MODE1/2352"))
117 data->tracks[count].type = CT_BIN;
118 else if (strcmp(buff2, "MODE1/2048"))
119 data->tracks[count].type = CT_ISO;
120 else if (strcmp(buff2, "AUDIO"))
122 if (data->tracks[count].fname != NULL)
124 // rely on extension, not type in cue..
125 char *ext = get_ext(data->tracks[count].fname);
126 if (strcasecmp(ext, "mp3") == 0)
127 data->tracks[count].type = CT_MP3;
128 else if (strcasecmp(ext, "wav") == 0)
129 data->tracks[count].type = CT_WAV;
131 elprintf(EL_STATUS, "unhandled audio format: \"%s\"",
132 data->tracks[count].fname);
137 elprintf(EL_STATUS, "unhandled track type: \"%s\"", buff2);
140 else if (BEGINS(buff, "INDEX "))
144 ret = get_token(buff+6, buff2, sizeof(buff2));
145 if (atoi(buff2) == 0) continue;
146 if (atoi(buff2) != 1) {
147 elprintf(EL_STATUS, "cue: don't know how to handle: \"%s\"", buff);
151 get_token(buff+6+ret, buff2, sizeof(buff2));
152 ret = sscanf(buff2, "%i:%i:%i", &m, &s, &f);
154 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
157 data->tracks[count].sector_offset = m*60*75 + s*75 + f;
159 else if (BEGINS(buff, "PREGAP "))
162 get_token(buff+7, buff2, sizeof(buff2));
163 ret = sscanf(buff2, "%i:%i:%i", &m, &s, &f);
165 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
168 data->tracks[count].pregap = m*60*75 + s*75 + f;
172 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
176 if (count < 1 || data->tracks[1].fname == NULL) {
178 for (; count > 0; count--)
179 if (data->tracks[count].fname != NULL)
180 free(data->tracks[count].fname);
185 data->track_count = count;
190 void cue_destroy(cue_data *data)
194 if (data == NULL) return;
196 for (c = data->track_count; c > 0; c--)
197 if (data->tracks[c].fname != NULL)
198 free(data->tracks[c].fname);
203 int main(int argc, char *argv[])
205 cue_data *data = cue_parse(argv[1]);
208 if (data == NULL) return 1;
210 for (c = 1; c <= data->track_count; c++)
211 printf("%2i: %i %9i %9i %s\n", c, data->tracks[c].type, data->tracks[c].sector_offset,
212 data->tracks[c].pregap, data->tracks[c].fname);