#include "../PicoInt.h"
// #define elprintf(w,f,...) printf(f "\n",##__VA_ARGS__);
+#ifdef _MSC_VER
+#define snprintf _snprintf
+#endif
+
static char *mystrip(char *str)
{
int i, len;
/* note: tracks[0] is not used */
cue_data_t *cue_parse(const char *fname)
{
- char buff[256], current_file[256], buff2[32];
+ char buff[256], current_file[256], buff2[32], *current_filep;
FILE *f, *tmpf;
- int ret, count = 0, count_alloc = 2;
+ int ret, count = 0, count_alloc = 2, pending_pregap = 0;
cue_data_t *data;
void *tmp;
f = fopen(fname, "r");
if (f == NULL) return NULL;
- current_file[0] = 0;
+ snprintf(current_file, sizeof(current_file), "%s", fname);
+ for (current_filep = current_file + strlen(current_file); current_filep > current_file; current_filep--)
+ if (current_filep[-1] == '/' || current_filep[-1] == '\\') break;
+
data = calloc(1, sizeof(*data) + count_alloc * sizeof(cue_track));
+ if (data == NULL) {
+ fclose(f);
+ return NULL;
+ }
while (!feof(f))
{
mystrip(buff);
if (buff[0] == 0) continue;
- if (BEGINS(buff, "TITLE ") || BEGINS(buff, "PERFORMER "))
+ if (BEGINS(buff, "TITLE ") || BEGINS(buff, "PERFORMER ") || BEGINS(buff, "SONGWRITER "))
continue; /* who would put those here? Ignore! */
else if (BEGINS(buff, "FILE "))
{
- get_token(buff+5, current_file, sizeof(current_file));
+ get_token(buff+5, current_filep, sizeof(current_file) - (current_filep - current_file));
}
else if (BEGINS(buff, "TRACK "))
{
}
fclose(tmpf);
}
+ data->tracks[count].pregap = pending_pregap;
+ pending_pregap = 0;
// track number
ret = get_token(buff+6, buff2, sizeof(buff2));
if (count != atoi(buff2))
data->tracks[count].fname = strdup(current_file);
}
}
- else if (BEGINS(buff, "PREGAP "))
+ else if (BEGINS(buff, "PREGAP ") || BEGINS(buff, "POSTGAP "))
{
int m, s, f;
get_token(buff+7, buff2, sizeof(buff2));
elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
continue;
}
- data->tracks[count].pregap = m*60*75 + s*75 + f;
+ // pregap overrides previous postgap?
+ // by looking at some .cues produced by some programs I've decided that..
+ if (BEGINS(buff, "PREGAP "))
+ data->tracks[count].pregap = m*60*75 + s*75 + f;
+ else
+ pending_pregap = m*60*75 + s*75 + f;
+ }
+ else if (BEGINS(buff, "REM LENGTH ")) // custom "extension"
+ {
+ int m, s, f;
+ get_token(buff+11, buff2, sizeof(buff2));
+ ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
+ if (ret != 3) continue;
+ data->tracks[count].sector_xlength = m*60*75 + s*75 + f;
}
+ else if (BEGINS(buff, "REM"))
+ continue;
else
{
elprintf(EL_STATUS, "cue: unhandled line: \"%s\"", buff);