-
Notifications
You must be signed in to change notification settings - Fork 4
/
cue_parser.c
473 lines (389 loc) · 9.16 KB
/
cue_parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* File : cue_parser.c
* Author : Vincent Cheung
* Date : Jul. 13, 2015
* Description : Cue file parser library.
*
* Copyright (C) 2015, Vincet Cheung<coolingfall@gmail.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "cue_utils.h"
#include "cue_parser.h"
/* parse label REM COMMENT */
static void parse_comment(CueSheet *cue_sheet, const char *input)
{
int len = strlen(input);
char buf[len];
if (!starts_with(input, "COMMENT"))
{
return;
}
substring(buf, input, strlen("COMMENT"), len);
input = trim(buf);
if (input[0] == '"' && input[strlen(input) - 1] == '"')
{
substring(buf, input, 1, strlen(input) - 1);
input = buf;
}
strcpy(cue_sheet->comment, input);
}
/* parse label REM DATE */
static void parse_date(CueSheet *cue_sheet, const char *input)
{
int len = strlen(input);
char buf[len];
if (!starts_with(input, "DATE"))
{
return;
}
substring(buf, input, strlen("DATE"), len);
input = trim(buf);
/* if total track is 0, then current date is global date */
if (cue_sheet->total_track == 0)
{
strcpy(cue_sheet->date, input);
}
else
{
TrackData *track_data = &cue_sheet->tracks_data[cue_sheet->total_track - 1];
strcpy(track_data->date, input);
}
}
/* parse label REM GENRE */
static void parse_genre(CueSheet *cue_sheet, const char *input)
{
int len = strlen(input);
char buf[len];
if (!starts_with(input, "GENRE"))
{
return;
}
substring(buf, input, strlen("GENRE"), len);
input = trim(buf);
strcpy(cue_sheet->genre, input);
}
/* parse label REM */
static void parse_rem(CueSheet *cue_sheet, const char *input)
{
int len = strlen(input);
char buf[len];
if (!starts_with(input, "REM"))
{
return;
}
substring(buf, input, strlen("REM"), len);
input = trim(buf);
switch (input[0])
{
case 'c':
case 'C':
parse_comment(cue_sheet, input);
break;
case 'd':
case 'D':
parse_date(cue_sheet, input);
break;
case 'g':
case 'G':
parse_genre(cue_sheet, input);
break;
default:
break;
}
}
/* parse label PERFORMER */
static void parse_performer(CueSheet *cue_sheet, const char *input)
{
int len = strlen(input);
char buf[len];
if (!starts_with(input, "PERFORMER"))
{
return;
}
substring(buf, input, strlen("PERFORMER"), len);
input = trim(buf);
if (input[0] == '"' && input[strlen(input) - 1] == '"')
{
substring(buf, input, 1, strlen(input) - 1);
input = buf;
}
/* if total track is 0, then current title is global title */
if (cue_sheet->total_track == 0)
{
strcpy(cue_sheet->performer, input);
}
else
{
TrackData *track_data = &cue_sheet->tracks_data[cue_sheet->total_track - 1];
strcpy(track_data->performer, input);
}
}
/* parse label FILE */
static void parse_file(CueSheet *cue_sheet, const char *input, const char *filename)
{
int len = strlen(input);
char buf[len];
char real_path[1024] = {0};
if (!starts_with(input, "FILE") || !ends_with(input, "WAVE"))
{
return;
}
substring(buf, input, strlen("FILE"), len - strlen("WAVE"));
input = trim(buf);
if (input[0] == '"' && input[strlen(input) - 1] == '"')
{
substring(buf, input, 1, strlen(input) - 1);
input = buf;
}
/* get the real path for cue file */
realpath(filename, real_path);
int index = last_index_of(real_path, "/");
if (index < 0)
{
return;
}
substring(cue_sheet->filename, real_path, 0, index + 1);
strcat(cue_sheet->filename, input);
}
/* parse label TITLE */
static void parse_title(CueSheet *cue_sheet, const char *input)
{
int len = strlen(input);
char buf[len];
if (!starts_with(input, "TITLE"))
{
return;
}
substring(buf, input, strlen("TITLE"), len);
input = trim(buf);
if (input[0] == '"' && input[strlen(input) - 1] == '"')
{
substring(buf, input, 1, strlen(input) - 1);
input = buf;
}
/* if total track is 0, then current performer is global performer */
if (cue_sheet->total_track == 0)
{
strcpy(cue_sheet->title, input);
}
else
{
TrackData *track_data = &cue_sheet->tracks_data[cue_sheet->total_track - 1];
strcpy(track_data->title, input);
}
}
/**
* Parse label TRACK.
* First track number may be > 1, but all others must be sequential.
*/
static void parse_track(CueSheet *cue_sheet, const char *input)
{
int len = strlen(input);
char buf[len];
if (!starts_with(input, "TRACK") || !ends_with(input, "AUDIO"))
{
return;
}
substring(buf, input, strlen("TRACK"), len);
input = trim(buf);
cue_sheet->total_track ++;
int track_num = atoi(input);
TrackData *track_data = &cue_sheet->tracks_data[cue_sheet->total_track - 1];
track_data->track_num = track_num;
}
/* calculate mm:ss:ff to millisecond, ff = frames (75 per second) */
static int calculate_duration(const char *input)
{
int len = strlen(input);
char buf[len];
substring(buf, input, 0, len - 6);
int minute = atoi(trim(buf));
substring(buf, input, len - 5, len - 3);
int second = atoi(trim(buf));
substring(buf, input, len - 2, len);
int frame = atoi(trim(buf));
return (int) ((minute * 60 + second + (float) frame / 75) * 1000);
}
/**
* INDEX [number] [mm:ss:ff], First index must be 0 or 1.
* First index must be 00:00:00
*/
static void parse_index(CueSheet *cue_sheet, const char *input)
{
int len = strlen(input);
char buf[len];
if (!starts_with(input, "INDEX") || strlen(input) < 8)
{
return;
}
substring(buf, input, strlen("INDEX"), len);
input = trim(buf);
TrackData *track_data = &cue_sheet->tracks_data[cue_sheet->total_track - 1];
if (starts_with(input, "00"))
{
substring(buf, input, strlen("00"), len);
input = trim(buf);
track_data->index0 = calculate_duration(input);
}
else if (starts_with(input, "01"))
{
substring(buf, input, strlen("01"), len);
input = trim(buf);
track_data->index1 = calculate_duration(input);
}
}
/**
* Parse cue file into cue sheet.
*
* @param filename the filename(absolute file path) of cue file
*/
CueSheet *parse_cue(const char *filename)
{
long file_size = get_file_size(filename);
char buffer[file_size];
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
return NULL;
}
CueSheet *cue_sheet = malloc(sizeof(CueSheet));
if (!cue_sheet)
{
fclose(fp);
fp = NULL;
return NULL;
}
memset(cue_sheet, 0, sizeof(CueSheet));
while (read_line(buffer, file_size, fp) != NULL)
{
switch (buffer[0])
{
case 'f':
case 'F':
parse_file(cue_sheet, buffer, filename);
break;
case 'i':
case 'I':
parse_index(cue_sheet, buffer);
case 'p':
case 'P':
parse_performer(cue_sheet, buffer);
break;
case 'r':
case 'R':
parse_rem(cue_sheet, buffer);
break;
case 't':
case 'T':
switch (buffer[1])
{
case 'r':
case 'R':
parse_track(cue_sheet, buffer);
break;
case 'i':
case 'I':
parse_title(cue_sheet, buffer);
break;
default:
printf("unknown symbol\n");
break;
}
break;
default:
printf("unknown symbol\n");
break;
}
}
fclose(fp);
fp = NULL;
return cue_sheet;
}
/**
* Convenience function for getting string metadata from the cue sheet.
*
* @param cue_sheet poniter to CueSheet structure
* @param track_index the index in tracks_data, if want to get global meta data, this
* param just set to -1.
* @param filed meta data filed
*/
char *get_string_metadata(CueSheet *cue_sheet, int track_index, MetaDataField filed)
{
if (!cue_sheet || track_index >= cue_sheet->total_track)
{
return NULL;
}
TrackData *track_data = &cue_sheet->tracks_data[track_index];
switch (filed)
{
case ALBUM_TITLE:
return cue_sheet->title;
case ALBUM_PREFORMER:
return cue_sheet->performer;
case ALBUM_DATE:
return cue_sheet->date;
case COMMENT:
return cue_sheet->comment;
case FILENAME:
return cue_sheet->filename;
case GENRE:
return cue_sheet->genre;
case TRACK_DATE:
return strlen(track_data->date) ? track_data->date : cue_sheet->date;
case TRACK_PERFORMER:
return strlen(track_data->performer) ? track_data->performer : cue_sheet->performer;
case TRACK_TITLE:
return track_data->title;
default:
printf("unknown");
return NULL;
}
}
/**
* Convenience function for getting string metadata from the cue sheet.
*
* @param cue_sheet poniter to CueSheet structure
* @param track_index the index in tracks_data, if want to get global meta data, this
* param just set to -1.
* @param filed meta data filed
*/
int get_int_metadata(CueSheet *cue_sheet, int track_index, MetaDataField filed)
{
if (!cue_sheet || track_index >= cue_sheet->total_track)
{
return -1;
}
TrackData *track_data = &cue_sheet->tracks_data[track_index];
TrackData *next_track_data;
int index0 = track_data->index0;
int index1 = track_data->index1;
int next_index0;
int next_index1;
switch (filed)
{
case TRACK_TOTAL:
return cue_sheet->total_track;
case TRACK_NUM:
return track_data->track_num;
case TRACK_START:
return index1 ? index1 : index0;
case TRACK_END:
/* if the track is the last track, then there's no end time */
if (track_index < cue_sheet->total_track - 1)
{
next_track_data = &cue_sheet->tracks_data[track_index + 1];
next_index0 = next_track_data->index0;
next_index1 = next_track_data->index1;
return next_index0 ? next_index0 : next_index1;
}
else
{
return -1;
}
default:
return 0;
}
}