/gme/M3u_Playlist.cpp

http://game-music-emu.googlecode.com/ · C++ · 426 lines · 345 code · 58 blank · 23 comment · 98 complexity · c808dfffeb2a7a9919752dd309817527 MD5 · raw file

  1. // Game_Music_Emu 0.5.5. http://www.slack.net/~ant/
  2. #include "M3u_Playlist.h"
  3. #include "Music_Emu.h"
  4. #include <string.h>
  5. /* Copyright (C) 2006 Shay Green. This module is free software; you
  6. can redistribute it and/or modify it under the terms of the GNU Lesser
  7. General Public License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version. This
  9. module is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. details. You should have received a copy of the GNU Lesser General Public
  13. License along with this module; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
  15. #include "blargg_source.h"
  16. // gme functions defined here to avoid linking in m3u code unless it's used
  17. blargg_err_t Gme_File::load_m3u_( blargg_err_t err )
  18. {
  19. require( raw_track_count_ ); // file must be loaded first
  20. if ( !err )
  21. {
  22. if ( playlist.size() )
  23. track_count_ = playlist.size();
  24. int line = playlist.first_error();
  25. if ( line )
  26. {
  27. // avoid using bloated printf()
  28. char* out = &playlist_warning [sizeof playlist_warning];
  29. *--out = 0;
  30. do {
  31. *--out = line % 10 + '0';
  32. } while ( (line /= 10) > 0 );
  33. static const char str [] = "Problem in m3u at line ";
  34. out -= sizeof str - 1;
  35. memcpy( out, str, sizeof str - 1 );
  36. set_warning( out );
  37. }
  38. }
  39. return err;
  40. }
  41. blargg_err_t Gme_File::load_m3u( const char* path ) { return load_m3u_( playlist.load( path ) ); }
  42. blargg_err_t Gme_File::load_m3u( Data_Reader& in ) { return load_m3u_( playlist.load( in ) ); }
  43. BLARGG_EXPORT gme_err_t gme_load_m3u( Music_Emu* me, const char* path ) { return me->load_m3u( path ); }
  44. BLARGG_EXPORT gme_err_t gme_load_m3u_data( Music_Emu* me, const void* data, long size )
  45. {
  46. Mem_File_Reader in( data, size );
  47. return me->load_m3u( in );
  48. }
  49. static char* skip_white( char* in )
  50. {
  51. while ( *in == ' ' )
  52. in++;
  53. return in;
  54. }
  55. inline unsigned from_dec( unsigned n ) { return n - '0'; }
  56. static char* parse_filename( char* in, M3u_Playlist::entry_t& entry )
  57. {
  58. entry.file = in;
  59. entry.type = "";
  60. char* out = in;
  61. while ( 1 )
  62. {
  63. int c = *in;
  64. if ( !c ) break;
  65. in++;
  66. if ( c == ',' ) // commas in filename
  67. {
  68. char* p = skip_white( in );
  69. if ( *p == '$' || from_dec( *p ) <= 9 )
  70. {
  71. in = p;
  72. break;
  73. }
  74. }
  75. if ( c == ':' && in [0] == ':' && in [1] && in [2] != ',' ) // ::type suffix
  76. {
  77. entry.type = ++in;
  78. while ( (c = *in) != 0 && c != ',' )
  79. in++;
  80. if ( c == ',' )
  81. {
  82. *in++ = 0; // terminate type
  83. in = skip_white( in );
  84. }
  85. break;
  86. }
  87. if ( c == '\\' ) // \ prefix for special characters
  88. {
  89. c = *in;
  90. if ( !c ) break;
  91. in++;
  92. }
  93. *out++ = (char) c;
  94. }
  95. *out = 0; // terminate string
  96. return in;
  97. }
  98. static char* next_field( char* in, int* result )
  99. {
  100. while ( 1 )
  101. {
  102. in = skip_white( in );
  103. if ( !*in )
  104. break;
  105. if ( *in == ',' )
  106. {
  107. in++;
  108. break;
  109. }
  110. *result = 1;
  111. in++;
  112. }
  113. return skip_white( in );
  114. }
  115. static char* parse_int_( char* in, int* out )
  116. {
  117. int n = 0;
  118. while ( 1 )
  119. {
  120. unsigned d = from_dec( *in );
  121. if ( d > 9 )
  122. break;
  123. in++;
  124. n = n * 10 + d;
  125. *out = n;
  126. }
  127. return in;
  128. }
  129. static char* parse_int( char* in, int* out, int* result )
  130. {
  131. return next_field( parse_int_( in, out ), result );
  132. }
  133. // Returns 16 or greater if not hex
  134. inline int from_hex_char( int h )
  135. {
  136. h -= 0x30;
  137. if ( (unsigned) h > 9 )
  138. h = ((h - 0x11) & 0xDF) + 10;
  139. return h;
  140. }
  141. static char* parse_track( char* in, M3u_Playlist::entry_t& entry, int* result )
  142. {
  143. if ( *in == '$' )
  144. {
  145. in++;
  146. int n = 0;
  147. while ( 1 )
  148. {
  149. int h = from_hex_char( *in );
  150. if ( h > 15 )
  151. break;
  152. in++;
  153. n = n * 16 + h;
  154. entry.track = n;
  155. }
  156. }
  157. else
  158. {
  159. in = parse_int_( in, &entry.track );
  160. if ( entry.track >= 0 )
  161. entry.decimal_track = 1;
  162. }
  163. return next_field( in, result );
  164. }
  165. static char* parse_time_( char* in, int* out )
  166. {
  167. *out = -1;
  168. int n = -1;
  169. in = parse_int_( in, &n );
  170. if ( n >= 0 )
  171. {
  172. *out = n;
  173. if ( *in == ':' )
  174. {
  175. n = -1;
  176. in = parse_int_( in + 1, &n );
  177. if ( n >= 0 )
  178. *out = *out * 60 + n;
  179. }
  180. }
  181. return in;
  182. }
  183. static char* parse_time( char* in, int* out, int* result )
  184. {
  185. return next_field( parse_time_( in, out ), result );
  186. }
  187. static char* parse_name( char* in )
  188. {
  189. char* out = in;
  190. while ( 1 )
  191. {
  192. int c = *in;
  193. if ( !c ) break;
  194. in++;
  195. if ( c == ',' ) // commas in string
  196. {
  197. char* p = skip_white( in );
  198. if ( *p == ',' || *p == '-' || from_dec( *p ) <= 9 )
  199. {
  200. in = p;
  201. break;
  202. }
  203. }
  204. if ( c == '\\' ) // \ prefix for special characters
  205. {
  206. c = *in;
  207. if ( !c ) break;
  208. in++;
  209. }
  210. *out++ = (char) c;
  211. }
  212. *out = 0; // terminate string
  213. return in;
  214. }
  215. static int parse_line( char* in, M3u_Playlist::entry_t& entry )
  216. {
  217. int result = 0;
  218. // file
  219. entry.file = in;
  220. entry.type = "";
  221. in = parse_filename( in, entry );
  222. // track
  223. entry.track = -1;
  224. entry.decimal_track = 0;
  225. in = parse_track( in, entry, &result );
  226. // name
  227. entry.name = in;
  228. in = parse_name( in );
  229. // time
  230. entry.length = -1;
  231. in = parse_time( in, &entry.length, &result );
  232. // loop
  233. entry.intro = -1;
  234. entry.loop = -1;
  235. if ( *in == '-' )
  236. {
  237. entry.loop = entry.length;
  238. in++;
  239. }
  240. else
  241. {
  242. in = parse_time_( in, &entry.loop );
  243. if ( entry.loop >= 0 )
  244. {
  245. entry.intro = 0;
  246. if ( *in == '-' ) // trailing '-' means that intro length was specified
  247. {
  248. in++;
  249. entry.intro = entry.loop;
  250. entry.loop = entry.length - entry.intro;
  251. }
  252. }
  253. }
  254. in = next_field( in, &result );
  255. // fade
  256. entry.fade = -1;
  257. in = parse_time( in, &entry.fade, &result );
  258. // repeat
  259. entry.repeat = -1;
  260. in = parse_int( in, &entry.repeat, &result );
  261. return result;
  262. }
  263. static void parse_comment( char* in, M3u_Playlist::info_t& info, bool first )
  264. {
  265. in = skip_white( in + 1 );
  266. const char* field = in;
  267. while ( *in && *in != ':' )
  268. in++;
  269. if ( *in == ':' )
  270. {
  271. const char* text = skip_white( in + 1 );
  272. if ( *text )
  273. {
  274. *in = 0;
  275. if ( !strcmp( "Composer", field ) ) info.composer = text;
  276. else if ( !strcmp( "Engineer", field ) ) info.engineer = text;
  277. else if ( !strcmp( "Ripping" , field ) ) info.ripping = text;
  278. else if ( !strcmp( "Tagging" , field ) ) info.tagging = text;
  279. else
  280. text = 0;
  281. if ( text )
  282. return;
  283. *in = ':';
  284. }
  285. }
  286. if ( first )
  287. info.title = field;
  288. }
  289. blargg_err_t M3u_Playlist::parse_()
  290. {
  291. info_.title = "";
  292. info_.composer = "";
  293. info_.engineer = "";
  294. info_.ripping = "";
  295. info_.tagging = "";
  296. int const CR = 13;
  297. int const LF = 10;
  298. data.end() [-1] = LF; // terminate input
  299. first_error_ = 0;
  300. bool first_comment = true;
  301. int line = 0;
  302. int count = 0;
  303. char* in = data.begin();
  304. while ( in < data.end() )
  305. {
  306. // find end of line and terminate it
  307. line++;
  308. char* begin = in;
  309. while ( *in != CR && *in != LF )
  310. {
  311. if ( !*in )
  312. return "Not an m3u playlist";
  313. in++;
  314. }
  315. if ( in [0] == CR && in [1] == LF ) // treat CR,LF as a single line
  316. *in++ = 0;
  317. *in++ = 0;
  318. // parse line
  319. if ( *begin == '#' )
  320. {
  321. parse_comment( begin, info_, first_comment );
  322. first_comment = false;
  323. }
  324. else if ( *begin )
  325. {
  326. if ( (int) entries.size() <= count )
  327. RETURN_ERR( entries.resize( count * 2 + 64 ) );
  328. if ( !parse_line( begin, entries [count] ) )
  329. count++;
  330. else if ( !first_error_ )
  331. first_error_ = line;
  332. first_comment = false;
  333. }
  334. }
  335. if ( count <= 0 )
  336. return "Not an m3u playlist";
  337. if ( !(info_.composer [0] | info_.engineer [0] | info_.ripping [0] | info_.tagging [0]) )
  338. info_.title = "";
  339. return entries.resize( count );
  340. }
  341. blargg_err_t M3u_Playlist::parse()
  342. {
  343. blargg_err_t err = parse_();
  344. if ( err )
  345. {
  346. entries.clear();
  347. data.clear();
  348. }
  349. return err;
  350. }
  351. blargg_err_t M3u_Playlist::load( Data_Reader& in )
  352. {
  353. RETURN_ERR( data.resize( in.remain() + 1 ) );
  354. RETURN_ERR( in.read( data.begin(), data.size() - 1 ) );
  355. return parse();
  356. }
  357. blargg_err_t M3u_Playlist::load( const char* path )
  358. {
  359. GME_FILE_READER in;
  360. RETURN_ERR( in.open( path ) );
  361. return load( in );
  362. }
  363. blargg_err_t M3u_Playlist::load( void const* in, long size )
  364. {
  365. RETURN_ERR( data.resize( size + 1 ) );
  366. memcpy( data.begin(), in, size );
  367. return parse();
  368. }