added zram clearing on reset for Psycho Pinball (does it happen on hw?)
[picodrive.git] / Pico / cd / cue.c
... / ...
CommitLineData
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#ifdef _MSC_VER
10#define snprintf _snprintf
11#endif
12
13static char *mystrip(char *str)
14{
15 int i, len;
16
17 len = strlen(str);
18 for (i = 0; i < len; i++)
19 if (str[i] != ' ') break;
20 if (i > 0) memmove(str, str + i, len - i + 1);
21
22 len = strlen(str);
23 for (i = len - 1; i >= 0; i--)
24 if (str[i] != ' ' && str[i] != '\r' && str[i] != '\n') break;
25 str[i+1] = 0;
26
27 return str;
28}
29
30static int get_token(const char *buff, char *dest, int len)
31{
32 const char *p = buff;
33 char sep = ' ';
34 int d = 0, skip = 0;
35
36 while (*p && *p == ' ') {
37 skip++;
38 p++;
39 }
40
41 if (*p == '\"') {
42 sep = '\"';
43 p++;
44 }
45 while (*p && *p != sep && d < len-1)
46 dest[d++] = *p++;
47 dest[d] = 0;
48
49 if (sep == '\"' && *p != sep)
50 elprintf(EL_STATUS, "cue: bad token: \"%s\"", buff);
51
52 return d + skip;
53}
54
55static char *get_ext(char *fname)
56{
57 int len = strlen(fname);
58 return (len >= 3) ? (fname + len - 3) : fname;
59}
60
61
62#define BEGINS(buff,str) (strncmp(buff,str,sizeof(str)-1) == 0)
63
64/* note: tracks[0] is not used */
65cue_data_t *cue_parse(const char *fname)
66{
67 char buff[256], current_file[256], buff2[32], *current_filep;
68 FILE *f, *tmpf;
69 int ret, count = 0, count_alloc = 2, pending_pregap = 0;
70 cue_data_t *data;
71 void *tmp;
72
73 f = fopen(fname, "r");
74 if (f == NULL) return NULL;
75
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;
79
80 data = calloc(1, sizeof(*data) + count_alloc * sizeof(cue_track));
81 if (data == NULL) {
82 fclose(f);
83 return NULL;
84 }
85
86 while (!feof(f))
87 {
88 tmp = fgets(buff, sizeof(buff), f);
89 if (tmp == NULL) break;
90
91 mystrip(buff);
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 "))
96 {
97 get_token(buff+5, current_filep, sizeof(current_file) - (current_filep - current_file));
98 }
99 else if (BEGINS(buff, "TRACK "))
100 {
101 count++;
102 if (count >= count_alloc) {
103 count_alloc *= 2;
104 tmp = realloc(data, sizeof(*data) + count_alloc * sizeof(cue_track));
105 if (tmp == NULL) { count--; break; }
106 data = tmp;
107 }
108 memset(&data->tracks[count], 0, sizeof(data->tracks[0]));
109 if (count == 1 || strcmp(data->tracks[1].fname, current_file) != 0)
110 {
111 data->tracks[count].fname = strdup(current_file);
112 if (data->tracks[count].fname == NULL) break;
113
114 tmpf = fopen(current_file, "rb");
115 if (tmpf == NULL) {
116 elprintf(EL_STATUS, "cue: bad/missing file: \"%s\"", current_file);
117 count--; break;
118 }
119 fclose(tmpf);
120 }
121 data->tracks[count].pregap = pending_pregap;
122 pending_pregap = 0;
123 // track number
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",
127 count, atoi(buff2));
128 // check type
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)
135 {
136 if (data->tracks[count].fname != NULL)
137 {
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;
144 else {
145 elprintf(EL_STATUS, "unhandled audio format: \"%s\"",
146 data->tracks[count].fname);
147 }
148 }
149 else
150 {
151 // propagate previous
152 data->tracks[count].type = data->tracks[count-1].type;
153 }
154 }
155 else {
156 elprintf(EL_STATUS, "unhandled track type: \"%s\"", buff2);
157 }
158 }
159 else if (BEGINS(buff, "INDEX "))
160 {
161 int m, s, f;
162 // type
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);
167 count--; break;
168 }
169 // offset in file
170 get_token(buff+6+ret, buff2, sizeof(buff2));
171 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
172 if (ret != 3) {
173 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
174 count--; break;
175 }
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)
179 {
180 free(data->tracks[count].fname);
181 data->tracks[count].fname = strdup(current_file);
182 }
183 if (data->tracks[count].fname == NULL && strcmp(data->tracks[1].fname, current_file) != 0)
184 {
185 data->tracks[count].fname = strdup(current_file);
186 }
187 }
188 else if (BEGINS(buff, "PREGAP ") || BEGINS(buff, "POSTGAP "))
189 {
190 int m, s, f;
191 get_token(buff+7, buff2, sizeof(buff2));
192 ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
193 if (ret != 3) {
194 elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
195 continue;
196 }
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;
201 else
202 pending_pregap = m*60*75 + s*75 + f;
203 }
204 else if (BEGINS(buff, "REM LENGTH ")) // custom "extension"
205 {
206 int m, s, f;
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;
211 }
212 else if (BEGINS(buff, "REM"))
213 continue;
214 else
215 {
216 elprintf(EL_STATUS, "cue: unhandled line: \"%s\"", buff);
217 }
218 }
219
220 if (count < 1 || data->tracks[1].fname == NULL) {
221 // failed..
222 for (; count > 0; count--)
223 if (data->tracks[count].fname != NULL)
224 free(data->tracks[count].fname);
225 free(data);
226 return NULL;
227 }
228
229 data->track_count = count;
230 return data;
231}
232
233
234void cue_destroy(cue_data_t *data)
235{
236 int c;
237
238 if (data == NULL) return;
239
240 for (c = data->track_count; c > 0; c--)
241 if (data->tracks[c].fname != NULL)
242 free(data->tracks[c].fname);
243 free(data);
244}
245
246
247#if 0
248int main(int argc, char *argv[])
249{
250 cue_data_t *data = cue_parse(argv[1]);
251 int c;
252
253 if (data == NULL) return 1;
254
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);
259
260 cue_destroy(data);
261
262 return 0;
263}
264#endif
265