cue.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * cuefile handling
  3. * (C) notaz, 2008
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "cue.h"
  12. #include "../pico_int.h"
  13. // #define elprintf(w,f,...) printf(f "\n",##__VA_ARGS__);
  14. #ifdef USE_LIBRETRO_VFS
  15. #include "file_stream_transforms.h"
  16. #endif
  17. #ifdef _MSC_VER
  18. #define snprintf _snprintf
  19. #endif
  20. #ifdef __EPOC32__
  21. #define snprintf(b,s,...) sprintf(b,##__VA_ARGS__)
  22. #endif
  23. static char *mystrip(char *str)
  24. {
  25. int i, len;
  26. len = strlen(str);
  27. for (i = 0; i < len; i++)
  28. if (str[i] != ' ') break;
  29. if (i > 0) memmove(str, str + i, len - i + 1);
  30. len = strlen(str);
  31. for (i = len - 1; i >= 0; i--)
  32. if (str[i] != ' ' && str[i] != '\r' && str[i] != '\n') break;
  33. str[i+1] = 0;
  34. return str;
  35. }
  36. static int get_token(const char *buff, char *dest, int len)
  37. {
  38. const char *p = buff;
  39. char sep = ' ';
  40. int d = 0, skip = 0;
  41. while (*p && *p == ' ') {
  42. skip++;
  43. p++;
  44. }
  45. if (*p == '\"') {
  46. sep = '\"';
  47. p++;
  48. }
  49. while (*p && *p != sep && d < len-1)
  50. dest[d++] = *p++;
  51. dest[d] = 0;
  52. if (sep == '\"' && *p != sep)
  53. elprintf(EL_STATUS, "cue: bad token: \"%s\"", buff);
  54. return d + skip;
  55. }
  56. static int get_ext(const char *fname, char ext[4],
  57. char *base, size_t base_size)
  58. {
  59. int len, pos = 0;
  60. len = strlen(fname);
  61. if (len >= 3)
  62. pos = len - 3;
  63. strcpy(ext, fname + pos);
  64. if (base != NULL) {
  65. if (pos + 1 < base_size)
  66. pos = base_size - 1;
  67. len = (pos < len) ? pos : len;
  68. memcpy(base, fname, len);
  69. base[len] = 0;
  70. }
  71. return pos;
  72. }
  73. static void change_case(char *p, int to_upper)
  74. {
  75. for (; *p != 0; p++) {
  76. if (to_upper && 'a' <= *p && *p <= 'z')
  77. *p += 'A' - 'a';
  78. else if (!to_upper && 'A' <= *p && *p <= 'Z')
  79. *p += 'a' - 'A';
  80. }
  81. }
  82. static int file_openable(const char *fname)
  83. {
  84. FILE *f = fopen(fname, "rb");
  85. if (f == NULL)
  86. return 0;
  87. fclose(f);
  88. return 1;
  89. }
  90. #define BEGINS(buff,str) (strncmp(buff,str,sizeof(str)-1) == 0)
  91. /* note: tracks[0] is not used */
  92. cue_data_t *cue_parse(const char *fname)
  93. {
  94. char current_file[256], *current_filep, cue_base[256];
  95. char buff[256], buff2[32], ext[4], *p;
  96. int ret, count = 0, count_alloc = 2, pending_pregap = 0;
  97. size_t current_filep_size, fname_len;
  98. cue_data_t *data = NULL;
  99. FILE *f = NULL;
  100. void *tmp;
  101. if (fname == NULL || (fname_len = strlen(fname)) == 0)
  102. return NULL;
  103. ret = get_ext(fname, ext, cue_base, sizeof(cue_base));
  104. if (strcasecmp(ext, "cue") == 0) {
  105. f = fopen(fname, "r");
  106. }
  107. else {
  108. // not a .cue, try one with the same base name
  109. if (ret + 3 < sizeof(cue_base)) {
  110. strcpy(cue_base + ret, "cue");
  111. f = fopen(cue_base, "r");
  112. if (f == NULL) {
  113. strcpy(cue_base + ret, "CUE");
  114. f = fopen(cue_base, "r");
  115. }
  116. }
  117. }
  118. if (f == NULL)
  119. return NULL;
  120. snprintf(current_file, sizeof(current_file), "%s", fname);
  121. current_filep = current_file + strlen(current_file);
  122. for (; current_filep > current_file; current_filep--)
  123. if (current_filep[-1] == '/' || current_filep[-1] == '\\')
  124. break;
  125. current_filep_size = sizeof(current_file) - (current_filep - current_file);
  126. // the basename of cuefile, no path
  127. snprintf(cue_base, sizeof(cue_base), "%s", current_filep);
  128. p = cue_base + strlen(cue_base);
  129. if (p - 3 >= cue_base)
  130. p[-3] = 0;
  131. data = calloc(1, sizeof(*data) + count_alloc * sizeof(cue_track));
  132. if (data == NULL)
  133. goto out;
  134. while (!feof(f))
  135. {
  136. tmp = fgets(buff, sizeof(buff), f);
  137. if (tmp == NULL)
  138. break;
  139. mystrip(buff);
  140. if (buff[0] == 0)
  141. continue;
  142. if (BEGINS(buff, "TITLE ") || BEGINS(buff, "PERFORMER ") || BEGINS(buff, "SONGWRITER "))
  143. continue; /* who would put those here? Ignore! */
  144. else if (BEGINS(buff, "FILE "))
  145. {
  146. get_token(buff + 5, current_filep, current_filep_size);
  147. }
  148. else if (BEGINS(buff, "TRACK "))
  149. {
  150. count++;
  151. if (count >= count_alloc) {
  152. count_alloc *= 2;
  153. tmp = realloc(data, sizeof(*data) + count_alloc * sizeof(cue_track));
  154. if (tmp == NULL) {
  155. count--;
  156. break;
  157. }
  158. data = tmp;
  159. }
  160. memset(&data->tracks[count], 0, sizeof(data->tracks[0]));
  161. if (count == 1 || strcmp(data->tracks[1].fname, current_file) != 0)
  162. {
  163. if (file_openable(current_file))
  164. goto file_ok;
  165. elprintf(EL_STATUS, "cue: bad/missing file: \"%s\"", current_file);
  166. if (count == 1) {
  167. int cue_ucase;
  168. char v;
  169. get_ext(current_file, ext, NULL, 0);
  170. snprintf(current_filep, current_filep_size,
  171. "%s%s", cue_base, ext);
  172. if (file_openable(current_file))
  173. goto file_ok;
  174. // try with the same case (for unix)
  175. v = fname[fname_len - 1];
  176. cue_ucase = ('A' <= v && v <= 'Z');
  177. change_case(ext, cue_ucase);
  178. snprintf(current_filep, current_filep_size,
  179. "%s%s", cue_base, ext);
  180. if (file_openable(current_file))
  181. goto file_ok;
  182. }
  183. count--;
  184. break;
  185. file_ok:
  186. data->tracks[count].fname = strdup(current_file);
  187. if (data->tracks[count].fname == NULL)
  188. break;
  189. }
  190. data->tracks[count].pregap = pending_pregap;
  191. pending_pregap = 0;
  192. // track number
  193. ret = get_token(buff+6, buff2, sizeof(buff2));
  194. if (count != atoi(buff2))
  195. elprintf(EL_STATUS, "cue: track index mismatch: track %i is track %i in cue",
  196. count, atoi(buff2));
  197. // check type
  198. get_token(buff+6+ret, buff2, sizeof(buff2));
  199. if (strcmp(buff2, "MODE1/2352") == 0)
  200. data->tracks[count].type = CT_BIN;
  201. else if (strcmp(buff2, "MODE1/2048") == 0)
  202. data->tracks[count].type = CT_ISO;
  203. else if (strcmp(buff2, "AUDIO") == 0)
  204. {
  205. if (data->tracks[count].fname != NULL)
  206. {
  207. // rely on extension, not type in cue..
  208. get_ext(data->tracks[count].fname, ext, NULL, 0);
  209. if (strcasecmp(ext, "mp3") == 0)
  210. data->tracks[count].type = CT_MP3;
  211. else if (strcasecmp(ext, "wav") == 0)
  212. data->tracks[count].type = CT_WAV;
  213. else if (strcasecmp(ext, "bin") == 0)
  214. data->tracks[count].type = CT_BIN;
  215. else {
  216. elprintf(EL_STATUS, "unhandled audio format: \"%s\"",
  217. data->tracks[count].fname);
  218. }
  219. }
  220. else
  221. {
  222. // propagate previous
  223. data->tracks[count].type = data->tracks[count-1].type;
  224. }
  225. }
  226. else {
  227. elprintf(EL_STATUS, "unhandled track type: \"%s\"", buff2);
  228. }
  229. }
  230. else if (BEGINS(buff, "INDEX "))
  231. {
  232. int m, s, f;
  233. // type
  234. ret = get_token(buff+6, buff2, sizeof(buff2));
  235. if (atoi(buff2) == 0) continue;
  236. if (atoi(buff2) != 1) {
  237. elprintf(EL_STATUS, "cue: don't know how to handle: \"%s\"", buff);
  238. count--; break;
  239. }
  240. // offset in file
  241. get_token(buff+6+ret, buff2, sizeof(buff2));
  242. ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
  243. if (ret != 3) {
  244. elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
  245. count--; break;
  246. }
  247. data->tracks[count].sector_offset = m*60*75 + s*75 + f;
  248. // some strange .cues may need this
  249. if (data->tracks[count].fname != NULL && strcmp(data->tracks[count].fname, current_file) != 0)
  250. {
  251. free(data->tracks[count].fname);
  252. data->tracks[count].fname = strdup(current_file);
  253. }
  254. if (data->tracks[count].fname == NULL && strcmp(data->tracks[1].fname, current_file) != 0)
  255. {
  256. data->tracks[count].fname = strdup(current_file);
  257. }
  258. }
  259. else if (BEGINS(buff, "PREGAP ") || BEGINS(buff, "POSTGAP "))
  260. {
  261. int m, s, f;
  262. get_token(buff+7, buff2, sizeof(buff2));
  263. ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
  264. if (ret != 3) {
  265. elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
  266. continue;
  267. }
  268. // pregap overrides previous postgap?
  269. // by looking at some .cues produced by some programs I've decided that..
  270. if (BEGINS(buff, "PREGAP "))
  271. data->tracks[count].pregap = m*60*75 + s*75 + f;
  272. else
  273. pending_pregap = m*60*75 + s*75 + f;
  274. }
  275. else if (BEGINS(buff, "REM LENGTH ")) // custom "extension"
  276. {
  277. int m, s, f;
  278. get_token(buff+11, buff2, sizeof(buff2));
  279. ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
  280. if (ret != 3) continue;
  281. data->tracks[count].sector_xlength = m*60*75 + s*75 + f;
  282. }
  283. else if (BEGINS(buff, "REM"))
  284. continue;
  285. else
  286. {
  287. elprintf(EL_STATUS, "cue: unhandled line: \"%s\"", buff);
  288. }
  289. }
  290. if (count < 1 || data->tracks[1].fname == NULL) {
  291. // failed..
  292. for (; count > 0; count--)
  293. if (data->tracks[count].fname != NULL)
  294. free(data->tracks[count].fname);
  295. free(data);
  296. data = NULL;
  297. goto out;
  298. }
  299. data->track_count = count;
  300. out:
  301. if (f != NULL)
  302. fclose(f);
  303. return data;
  304. }
  305. void cue_destroy(cue_data_t *data)
  306. {
  307. int c;
  308. if (data == NULL) return;
  309. for (c = data->track_count; c > 0; c--)
  310. if (data->tracks[c].fname != NULL)
  311. free(data->tracks[c].fname);
  312. free(data);
  313. }
  314. #if 0
  315. int main(int argc, char *argv[])
  316. {
  317. cue_data_t *data = cue_parse(argv[1]);
  318. int c;
  319. if (data == NULL) return 1;
  320. for (c = 1; c <= data->track_count; c++)
  321. printf("%2i: %i %9i %02i:%02i:%02i %9i %s\n", c, data->tracks[c].type, data->tracks[c].sector_offset,
  322. data->tracks[c].sector_offset / (75*60), data->tracks[c].sector_offset / 75 % 60,
  323. data->tracks[c].sector_offset % 75, data->tracks[c].pregap, data->tracks[c].fname);
  324. cue_destroy(data);
  325. return 0;
  326. }
  327. #endif