cue.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 _MSC_VER
  15. #define snprintf _snprintf
  16. #endif
  17. #ifdef __EPOC32__
  18. #define snprintf(b,s,...) sprintf(b,##__VA_ARGS__)
  19. #endif
  20. static char *mystrip(char *str)
  21. {
  22. int i, len;
  23. len = strlen(str);
  24. for (i = 0; i < len; i++)
  25. if (str[i] != ' ') break;
  26. if (i > 0) memmove(str, str + i, len - i + 1);
  27. len = strlen(str);
  28. for (i = len - 1; i >= 0; i--)
  29. if (str[i] != ' ' && str[i] != '\r' && str[i] != '\n') break;
  30. str[i+1] = 0;
  31. return str;
  32. }
  33. static int get_token(const char *buff, char *dest, int len)
  34. {
  35. const char *p = buff;
  36. char sep = ' ';
  37. int d = 0, skip = 0;
  38. while (*p && *p == ' ') {
  39. skip++;
  40. p++;
  41. }
  42. if (*p == '\"') {
  43. sep = '\"';
  44. p++;
  45. }
  46. while (*p && *p != sep && d < len-1)
  47. dest[d++] = *p++;
  48. dest[d] = 0;
  49. if (sep == '\"' && *p != sep)
  50. elprintf(EL_STATUS, "cue: bad token: \"%s\"", buff);
  51. return d + skip;
  52. }
  53. static char *get_ext(char *fname)
  54. {
  55. int len = strlen(fname);
  56. return (len >= 3) ? (fname + len - 3) : fname;
  57. }
  58. #define BEGINS(buff,str) (strncmp(buff,str,sizeof(str)-1) == 0)
  59. /* note: tracks[0] is not used */
  60. cue_data_t *cue_parse(const char *fname)
  61. {
  62. char buff[256], current_file[256], buff2[32], *current_filep;
  63. FILE *f, *tmpf;
  64. int ret, count = 0, count_alloc = 2, pending_pregap = 0;
  65. cue_data_t *data;
  66. void *tmp;
  67. f = fopen(fname, "r");
  68. if (f == NULL) return NULL;
  69. snprintf(current_file, sizeof(current_file), "%s", fname);
  70. for (current_filep = current_file + strlen(current_file); current_filep > current_file; current_filep--)
  71. if (current_filep[-1] == '/' || current_filep[-1] == '\\') break;
  72. data = calloc(1, sizeof(*data) + count_alloc * sizeof(cue_track));
  73. if (data == NULL) {
  74. fclose(f);
  75. return NULL;
  76. }
  77. while (!feof(f))
  78. {
  79. tmp = fgets(buff, sizeof(buff), f);
  80. if (tmp == NULL) break;
  81. mystrip(buff);
  82. if (buff[0] == 0) continue;
  83. if (BEGINS(buff, "TITLE ") || BEGINS(buff, "PERFORMER ") || BEGINS(buff, "SONGWRITER "))
  84. continue; /* who would put those here? Ignore! */
  85. else if (BEGINS(buff, "FILE "))
  86. {
  87. get_token(buff+5, current_filep, sizeof(current_file) - (current_filep - current_file));
  88. }
  89. else if (BEGINS(buff, "TRACK "))
  90. {
  91. count++;
  92. if (count >= count_alloc) {
  93. count_alloc *= 2;
  94. tmp = realloc(data, sizeof(*data) + count_alloc * sizeof(cue_track));
  95. if (tmp == NULL) { count--; break; }
  96. data = tmp;
  97. }
  98. memset(&data->tracks[count], 0, sizeof(data->tracks[0]));
  99. if (count == 1 || strcmp(data->tracks[1].fname, current_file) != 0)
  100. {
  101. data->tracks[count].fname = strdup(current_file);
  102. if (data->tracks[count].fname == NULL) break;
  103. tmpf = fopen(current_file, "rb");
  104. if (tmpf == NULL) {
  105. elprintf(EL_STATUS, "cue: bad/missing file: \"%s\"", current_file);
  106. count--; break;
  107. }
  108. fclose(tmpf);
  109. }
  110. data->tracks[count].pregap = pending_pregap;
  111. pending_pregap = 0;
  112. // track number
  113. ret = get_token(buff+6, buff2, sizeof(buff2));
  114. if (count != atoi(buff2))
  115. elprintf(EL_STATUS, "cue: track index mismatch: track %i is track %i in cue",
  116. count, atoi(buff2));
  117. // check type
  118. get_token(buff+6+ret, buff2, sizeof(buff2));
  119. if (strcmp(buff2, "MODE1/2352") == 0)
  120. data->tracks[count].type = CT_BIN;
  121. else if (strcmp(buff2, "MODE1/2048") == 0)
  122. data->tracks[count].type = CT_ISO;
  123. else if (strcmp(buff2, "AUDIO") == 0)
  124. {
  125. if (data->tracks[count].fname != NULL)
  126. {
  127. // rely on extension, not type in cue..
  128. char *ext = get_ext(data->tracks[count].fname);
  129. if (strcasecmp(ext, "mp3") == 0)
  130. data->tracks[count].type = CT_MP3;
  131. else if (strcasecmp(ext, "wav") == 0)
  132. data->tracks[count].type = CT_WAV;
  133. else {
  134. elprintf(EL_STATUS, "unhandled audio format: \"%s\"",
  135. data->tracks[count].fname);
  136. }
  137. }
  138. else
  139. {
  140. // propagate previous
  141. data->tracks[count].type = data->tracks[count-1].type;
  142. }
  143. }
  144. else {
  145. elprintf(EL_STATUS, "unhandled track type: \"%s\"", buff2);
  146. }
  147. }
  148. else if (BEGINS(buff, "INDEX "))
  149. {
  150. int m, s, f;
  151. // type
  152. ret = get_token(buff+6, buff2, sizeof(buff2));
  153. if (atoi(buff2) == 0) continue;
  154. if (atoi(buff2) != 1) {
  155. elprintf(EL_STATUS, "cue: don't know how to handle: \"%s\"", buff);
  156. count--; break;
  157. }
  158. // offset in file
  159. get_token(buff+6+ret, buff2, sizeof(buff2));
  160. ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
  161. if (ret != 3) {
  162. elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
  163. count--; break;
  164. }
  165. data->tracks[count].sector_offset = m*60*75 + s*75 + f;
  166. // some strange .cues may need this
  167. if (data->tracks[count].fname != NULL && strcmp(data->tracks[count].fname, current_file) != 0)
  168. {
  169. free(data->tracks[count].fname);
  170. data->tracks[count].fname = strdup(current_file);
  171. }
  172. if (data->tracks[count].fname == NULL && strcmp(data->tracks[1].fname, current_file) != 0)
  173. {
  174. data->tracks[count].fname = strdup(current_file);
  175. }
  176. }
  177. else if (BEGINS(buff, "PREGAP ") || BEGINS(buff, "POSTGAP "))
  178. {
  179. int m, s, f;
  180. get_token(buff+7, buff2, sizeof(buff2));
  181. ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
  182. if (ret != 3) {
  183. elprintf(EL_STATUS, "cue: failed to parse: \"%s\"", buff);
  184. continue;
  185. }
  186. // pregap overrides previous postgap?
  187. // by looking at some .cues produced by some programs I've decided that..
  188. if (BEGINS(buff, "PREGAP "))
  189. data->tracks[count].pregap = m*60*75 + s*75 + f;
  190. else
  191. pending_pregap = m*60*75 + s*75 + f;
  192. }
  193. else if (BEGINS(buff, "REM LENGTH ")) // custom "extension"
  194. {
  195. int m, s, f;
  196. get_token(buff+11, buff2, sizeof(buff2));
  197. ret = sscanf(buff2, "%d:%d:%d", &m, &s, &f);
  198. if (ret != 3) continue;
  199. data->tracks[count].sector_xlength = m*60*75 + s*75 + f;
  200. }
  201. else if (BEGINS(buff, "REM"))
  202. continue;
  203. else
  204. {
  205. elprintf(EL_STATUS, "cue: unhandled line: \"%s\"", buff);
  206. }
  207. }
  208. if (count < 1 || data->tracks[1].fname == NULL) {
  209. // failed..
  210. for (; count > 0; count--)
  211. if (data->tracks[count].fname != NULL)
  212. free(data->tracks[count].fname);
  213. free(data);
  214. return NULL;
  215. }
  216. data->track_count = count;
  217. return data;
  218. }
  219. void cue_destroy(cue_data_t *data)
  220. {
  221. int c;
  222. if (data == NULL) return;
  223. for (c = data->track_count; c > 0; c--)
  224. if (data->tracks[c].fname != NULL)
  225. free(data->tracks[c].fname);
  226. free(data);
  227. }
  228. #if 0
  229. int main(int argc, char *argv[])
  230. {
  231. cue_data_t *data = cue_parse(argv[1]);
  232. int c;
  233. if (data == NULL) return 1;
  234. for (c = 1; c <= data->track_count; c++)
  235. printf("%2i: %i %9i %02i:%02i:%02i %9i %s\n", c, data->tracks[c].type, data->tracks[c].sector_offset,
  236. data->tracks[c].sector_offset / (75*60), data->tracks[c].sector_offset / 75 % 60,
  237. data->tracks[c].sector_offset % 75, data->tracks[c].pregap, data->tracks[c].fname);
  238. cue_destroy(data);
  239. return 0;
  240. }
  241. #endif