cuesheet.c 19 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
/* grabbag - Convenience lib for various routines common to several tools
2
3
 * Copyright (C) 2002-2009  Josh Coalson
 * Copyright (C) 2011-2013  Xiph.Org Foundation
Josh Coalson's avatar
Josh Coalson committed
4
 *
5
6
7
8
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
Josh Coalson's avatar
Josh Coalson committed
9
 *
10
 * This library is distributed in the hope that it will be useful,
Josh Coalson's avatar
Josh Coalson committed
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Josh Coalson's avatar
Josh Coalson committed
14
 *
15
16
17
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Josh Coalson's avatar
Josh Coalson committed
18
19
 */

20
#ifdef HAVE_CONFIG_H
Josh Coalson's avatar
Josh Coalson committed
21
22
23
#  include <config.h>
#endif

Josh Coalson's avatar
Josh Coalson committed
24
25
26
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
27
28
29
30
#include "FLAC/assert.h"
#include "share/compat.h"
#include "share/grabbag.h"
#include "share/safe_str.h"
Josh Coalson's avatar
Josh Coalson committed
31

32
unsigned grabbag__cuesheet_msf_to_frame(unsigned minutes, unsigned seconds, unsigned frames)
Josh Coalson's avatar
Josh Coalson committed
33
34
35
36
{
	return ((minutes * 60) + seconds) * 75 + frames;
}

37
void grabbag__cuesheet_frame_to_msf(unsigned frame, unsigned *minutes, unsigned *seconds, unsigned *frames)
Josh Coalson's avatar
Josh Coalson committed
38
39
40
41
42
43
44
{
	*frames = frame % 75;
	frame /= 75;
	*seconds = frame % 60;
	frame /= 60;
	*minutes = frame;
}
45

Josh Coalson's avatar
Josh Coalson committed
46
/* since we only care about values >= 0 or error, returns < 0 for any illegal string, else value */
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
static int local__parse_int_(const char *s)
{
	int ret = 0;
	char c;

	if(*s == '\0')
		return -1;

	while('\0' != (c = *s++))
		if(c >= '0' && c <= '9')
			ret = ret * 10 + (c - '0');
		else
			return -1;

	return ret;
}

Josh Coalson's avatar
Josh Coalson committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* since we only care about values >= 0 or error, returns < 0 for any illegal string, else value */
static FLAC__int64 local__parse_int64_(const char *s)
{
	FLAC__int64 ret = 0;
	char c;

	if(*s == '\0')
		return -1;

	while('\0' != (c = *s++))
		if(c >= '0' && c <= '9')
			ret = ret * 10 + (c - '0');
		else
			return -1;

	return ret;
}

82
/* accept minute:second:frame syntax of '[0-9]+:[0-9][0-9]?:[0-9][0-9]?', but max second of 59 and max frame of 74, e.g. 0:0:0, 123:45:67
Josh Coalson's avatar
Josh Coalson committed
83
 * return sample number or <0 for error
84
 * WATCHOUT: if sample rate is not evenly divisible by 75, the resulting sample number will be approximate
Josh Coalson's avatar
Josh Coalson committed
85
 */
86
static FLAC__int64 local__parse_msf_(const char *s, unsigned sample_rate)
Josh Coalson's avatar
Josh Coalson committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{
	FLAC__int64 ret, field;
	char c;

	c = *s++;
	if(c >= '0' && c <= '9')
		field = (c - '0');
	else
		return -1;
	while(':' != (c = *s++)) {
		if(c >= '0' && c <= '9')
			field = field * 10 + (c - '0');
		else
			return -1;
	}

103
	ret = field * 60 * sample_rate;
Josh Coalson's avatar
Josh Coalson committed
104
105
106
107
108
109
110

	c = *s++;
	if(c >= '0' && c <= '9')
		field = (c - '0');
	else
		return -1;
	if(':' != (c = *s++)) {
111
		if(c >= '0' && c <= '9') {
Josh Coalson's avatar
Josh Coalson committed
112
			field = field * 10 + (c - '0');
113
114
115
116
			c = *s++;
			if(c != ':')
				return -1;
		}
Josh Coalson's avatar
Josh Coalson committed
117
118
119
120
121
122
123
		else
			return -1;
	}

	if(field >= 60)
		return -1;

124
	ret += field * sample_rate;
Josh Coalson's avatar
Josh Coalson committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

	c = *s++;
	if(c >= '0' && c <= '9')
		field = (c - '0');
	else
		return -1;
	if('\0' != (c = *s++)) {
		if(c >= '0' && c <= '9') {
			field = field * 10 + (c - '0');
			c = *s++;
		}
		else
			return -1;
	}

	if(c != '\0')
		return -1;

	if(field >= 75)
		return -1;

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
	ret += field * (sample_rate / 75);

	return ret;
}

/* accept minute:second syntax of '[0-9]+:[0-9][0-9]?{,.[0-9]+}', but second < 60, e.g. 0:0.0, 3:5, 15:31.731
 * return sample number or <0 for error
 * WATCHOUT: depending on the sample rate, the resulting sample number may be approximate with fractional seconds
 */
static FLAC__int64 local__parse_ms_(const char *s, unsigned sample_rate)
{
	FLAC__int64 ret, field;
	double x;
	char c, *end;

	c = *s++;
	if(c >= '0' && c <= '9')
		field = (c - '0');
	else
		return -1;
	while(':' != (c = *s++)) {
		if(c >= '0' && c <= '9')
			field = field * 10 + (c - '0');
		else
			return -1;
	}

	ret = field * 60 * sample_rate;

	s++; /* skip the ':' */
	if(strspn(s, "0123456789.") != strlen(s))
		return -1;
	x = strtod(s, &end);
	if(*end || end == s)
		return -1;
	if(x < 0.0 || x >= 60.0)
		return -1;

	ret += (FLAC__int64)(x * sample_rate);
Josh Coalson's avatar
Josh Coalson committed
185
186
187
188

	return ret;
}

189
static char *local__get_field_(char **s, FLAC__bool allow_quotes)
190
{
191
	FLAC__bool has_quote = false;
192
193
194
195
196
197
198
199
200
201
202
	char *p;

	FLAC__ASSERT(0 != s);

	if(0 == *s)
		return 0;

	/* skip leading whitespace */
	while(**s && 0 != strchr(" \t\r\n", **s))
		(*s)++;

203
	if(**s == 0) {
204
		*s = 0;
205
206
207
208
209
210
211
212
213
214
215
		return 0;
	}

	if(allow_quotes && (**s == '"')) {
		has_quote = true;
		(*s)++;
		if(**s == 0) {
			*s = 0;
			return 0;
		}
	}
216
217
218

	p = *s;

219
220
221
222
223
224
225
226
227
228
229
	if(has_quote) {
		*s = strchr(*s, '\"');
		/* if there is no matching end quote, it's an error */
		if(0 == *s)
			p = *s = 0;
		else {
			**s = '\0';
			(*s)++;
		}
	}
	else {
230
231
		while(**s && 0 == strchr(" \t\r\n", **s))
			(*s)++;
232
		if(**s) {
233
			**s = '\0';
234
235
			(*s)++;
		}
236
237
238
239
240
241
242
		else
			*s = 0;
	}

	return p;
}

243
static FLAC__bool local__cuesheet_parse_(FILE *file, const char **error_message, unsigned *last_line_read, FLAC__StreamMetadata *cuesheet, unsigned sample_rate, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
244
245
{
	char buffer[4096], *line, *field;
246
	unsigned forced_leadout_track_num = 0;
Josh Coalson's avatar
Josh Coalson committed
247
248
	FLAC__uint64 forced_leadout_track_offset = 0;
	int in_track_num = -1, in_index_num = -1;
249
	FLAC__bool disc_has_catalog = false, track_has_flags = false, track_has_isrc = false, has_forced_leadout = false;
Josh Coalson's avatar
Josh Coalson committed
250
251
	FLAC__StreamMetadata_CueSheet *cs = &cuesheet->data.cue_sheet;

252
253
254
255
256
257
258
	FLAC__ASSERT(!is_cdda || sample_rate == 44100);
	/* double protection */
	if(is_cdda && sample_rate != 44100) {
		*error_message = "CD-DA cuesheet only allowed with 44.1kHz sample rate";
		return false;
	}

Josh Coalson's avatar
Josh Coalson committed
259
	cs->lead_in = is_cdda? 2 * 44100 /* The default lead-in size for CD-DA */ : 0;
260
	cs->is_cd = is_cdda;
261
262

	while(0 != fgets(buffer, sizeof(buffer), file)) {
Josh Coalson's avatar
Josh Coalson committed
263
		(*last_line_read)++;
264
265
		line = buffer;

266
267
268
269
270
271
		{
			size_t linelen = strlen(line);
			if((linelen == sizeof(buffer)-1) && line[linelen-1] != '\n') {
				*error_message = "line too long";
				return false;
			}
272
273
		}

274
		if(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
275
			if(0 == FLAC__STRCASECMP(field, "CATALOG")) {
Josh Coalson's avatar
Josh Coalson committed
276
277
278
279
				if(disc_has_catalog) {
					*error_message = "found multiple CATALOG commands";
					return false;
				}
280
				if(0 == (field = local__get_field_(&line, /*allow_quotes=*/true))) {
Josh Coalson's avatar
Josh Coalson committed
281
282
283
284
					*error_message = "CATALOG is missing catalog number";
					return false;
				}
				if(strlen(field) >= sizeof(cs->media_catalog_number)) {
285
					*error_message = "CATALOG number is too long";
Josh Coalson's avatar
Josh Coalson committed
286
287
288
289
290
291
					return false;
				}
				if(is_cdda && (strlen(field) != 13 || strspn(field, "0123456789") != 13)) {
					*error_message = "CD-DA CATALOG number must be 13 decimal digits";
					return false;
				}
292
				safe_strncpy(cs->media_catalog_number, field, sizeof(cs->media_catalog_number));
Josh Coalson's avatar
Josh Coalson committed
293
				disc_has_catalog = true;
294
295
			}
			else if(0 == FLAC__STRCASECMP(field, "FLAGS")) {
Josh Coalson's avatar
Josh Coalson committed
296
297
298
299
300
301
302
				if(track_has_flags) {
					*error_message = "found multiple FLAGS commands";
					return false;
				}
				if(in_track_num < 0 || in_index_num >= 0) {
					*error_message = "FLAGS command must come after TRACK but before INDEX";
					return false;
303
				}
304
				while(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
Josh Coalson's avatar
Josh Coalson committed
305
306
					if(0 == FLAC__STRCASECMP(field, "PRE"))
						cs->tracks[cs->num_tracks-1].pre_emphasis = 1;
307
				}
Josh Coalson's avatar
Josh Coalson committed
308
				track_has_flags = true;
309
310
			}
			else if(0 == FLAC__STRCASECMP(field, "INDEX")) {
Josh Coalson's avatar
Josh Coalson committed
311
312
313
				FLAC__int64 xx;
				FLAC__StreamMetadata_CueSheet_Track *track = &cs->tracks[cs->num_tracks-1];
				if(in_track_num < 0) {
314
315
316
					*error_message = "found INDEX before any TRACK";
					return false;
				}
317
				if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
Josh Coalson's avatar
Josh Coalson committed
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
					*error_message = "INDEX is missing index number";
					return false;
				}
				in_index_num = local__parse_int_(field);
				if(in_index_num < 0) {
					*error_message = "INDEX has invalid index number";
					return false;
				}
				FLAC__ASSERT(cs->num_tracks > 0);
				if(track->num_indices == 0) {
					/* it's the first index point of the track */
					if(in_index_num > 1) {
						*error_message = "first INDEX number of a TRACK must be 0 or 1";
						return false;
					}
				}
				else {
					if(in_index_num != track->indices[track->num_indices-1].number + 1) {
						*error_message = "INDEX numbers must be sequential";
						return false;
					}
				}
				if(is_cdda && in_index_num > 99) {
					*error_message = "CD-DA INDEX number must be between 0 and 99, inclusive";
					return false;
				}
Josh Coalson's avatar
Josh Coalson committed
344
				/*@@@ search for duplicate track number? */
345
				if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
Josh Coalson's avatar
Josh Coalson committed
346
347
348
					*error_message = "INDEX is missing an offset after the index number";
					return false;
				}
349
350
				/* first parse as minute:second:frame format */
				xx = local__parse_msf_(field, sample_rate);
Josh Coalson's avatar
Josh Coalson committed
351
				if(xx < 0) {
352
					/* CD-DA must use only MM:SS:FF format */
Josh Coalson's avatar
Josh Coalson committed
353
354
355
356
					if(is_cdda) {
						*error_message = "illegal INDEX offset (not of the form MM:SS:FF)";
						return false;
					}
357
358
					/* as an extension for non-CD-DA we allow MM:SS.SS or raw sample number */
					xx = local__parse_ms_(field, sample_rate);
Josh Coalson's avatar
Josh Coalson committed
359
					if(xx < 0) {
360
361
362
363
364
						xx = local__parse_int64_(field);
						if(xx < 0) {
							*error_message = "illegal INDEX offset";
							return false;
						}
Josh Coalson's avatar
Josh Coalson committed
365
366
					}
				}
367
368
				else if(sample_rate % 75 && xx) {
                                        /* only sample zero is exact */
369
370
371
					*error_message = "illegal INDEX offset (MM:SS:FF form not allowed if sample rate is not a multiple of 75)";
					return false;
				}
Josh Coalson's avatar
Josh Coalson committed
372
373
374
375
376
377
378
379
380
381
382
				if(is_cdda && cs->num_tracks == 1 && cs->tracks[0].num_indices == 0 && xx != 0) {
					*error_message = "first INDEX of first TRACK must have an offset of 00:00:00";
					return false;
				}
				if(is_cdda && track->num_indices > 0 && (FLAC__uint64)xx <= track->indices[track->num_indices-1].offset) {
					*error_message = "CD-DA INDEX offsets must increase in time";
					return false;
				}
				/* fill in track offset if it's the first index of the track */
				if(track->num_indices == 0)
					track->offset = (FLAC__uint64)xx;
383
384
385
386
387
388
				if(is_cdda && cs->num_tracks > 1) {
					const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-2];
					if((FLAC__uint64)xx <= prev->offset + prev->indices[prev->num_indices-1].offset) {
						*error_message = "CD-DA INDEX offsets must increase in time";
						return false;
					}
Josh Coalson's avatar
Josh Coalson committed
389
390
391
392
393
				}
				if(!FLAC__metadata_object_cuesheet_track_insert_blank_index(cuesheet, cs->num_tracks-1, track->num_indices)) {
					*error_message = "memory allocation error";
					return false;
				}
394
				track->indices[track->num_indices-1].offset = (FLAC__uint64)xx - track->offset;
Josh Coalson's avatar
Josh Coalson committed
395
				track->indices[track->num_indices-1].number = in_index_num;
396
397
			}
			else if(0 == FLAC__STRCASECMP(field, "ISRC")) {
398
				char *l, *r;
Josh Coalson's avatar
Josh Coalson committed
399
400
401
				if(track_has_isrc) {
					*error_message = "found multiple ISRC commands";
					return false;
402
				}
Josh Coalson's avatar
Josh Coalson committed
403
				if(in_track_num < 0 || in_index_num >= 0) {
404
405
406
					*error_message = "ISRC command must come after TRACK but before INDEX";
					return false;
				}
407
				if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
408
					*error_message = "ISRC is missing ISRC number";
Josh Coalson's avatar
Josh Coalson committed
409
					return false;
410
				}
411
412
413
414
415
416
				/* strip out dashes */
				for(l = r = field; *r; r++) {
					if(*r != '-')
						*l++ = *r;
				}
				*l = '\0';
Josh Coalson's avatar
Josh Coalson committed
417
418
419
420
				if(strlen(field) != 12 || strspn(field, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") < 5 || strspn(field+5, "1234567890") != 7) {
					*error_message = "invalid ISRC number";
					return false;
				}
421
				safe_strncpy(cs->tracks[cs->num_tracks-1].isrc, field, sizeof(cs->tracks[cs->num_tracks-1].isrc));
Josh Coalson's avatar
Josh Coalson committed
422
				track_has_isrc = true;
423
424
			}
			else if(0 == FLAC__STRCASECMP(field, "TRACK")) {
Josh Coalson's avatar
Josh Coalson committed
425
426
427
428
429
430
431
432
				if(cs->num_tracks > 0) {
					const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
					if(
						prev->num_indices == 0 ||
						(
						 	is_cdda &&
							(
								(prev->num_indices == 1 && prev->indices[0].number != 1) ||
433
								(prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
Josh Coalson's avatar
Josh Coalson committed
434
435
436
437
438
439
440
441
442
							)
						)
					) {
						*error_message = is_cdda?
							"previous TRACK must specify at least one INDEX 01" :
							"previous TRACK must specify at least one INDEX";
						return false;
					}
				}
443
				if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
444
445
446
					*error_message = "TRACK is missing track number";
					return false;
				}
Josh Coalson's avatar
Josh Coalson committed
447
448
				in_track_num = local__parse_int_(field);
				if(in_track_num < 0) {
449
450
451
					*error_message = "TRACK has invalid track number";
					return false;
				}
Josh Coalson's avatar
Josh Coalson committed
452
453
454
455
				if(in_track_num == 0) {
					*error_message = "TRACK number must be greater than 0";
					return false;
				}
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
				if(is_cdda) {
					if(in_track_num > 99) {
						*error_message = "CD-DA TRACK number must be between 1 and 99, inclusive";
						return false;
					}
				}
				else {
					if(in_track_num == 255) {
						*error_message = "TRACK number 255 is reserved for the lead-out";
						return false;
					}
					else if(in_track_num > 255) {
						*error_message = "TRACK number must be between 1 and 254, inclusive";
						return false;
					}
Josh Coalson's avatar
Josh Coalson committed
471
472
473
474
475
				}
				if(is_cdda && cs->num_tracks > 0 && in_track_num != cs->tracks[cs->num_tracks-1].number + 1) {
					*error_message = "CD-DA TRACK numbers must be sequential";
					return false;
				}
Josh Coalson's avatar
Josh Coalson committed
476
				/*@@@ search for duplicate track number? */
477
				if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
Josh Coalson's avatar
Josh Coalson committed
478
479
480
481
482
483
484
485
					*error_message = "TRACK is missing a track type after the track number";
					return false;
				}
				if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
					*error_message = "memory allocation error";
					return false;
				}
				cs->tracks[cs->num_tracks-1].number = in_track_num;
Josh Coalson's avatar
Josh Coalson committed
486
				cs->tracks[cs->num_tracks-1].type = (0 == FLAC__STRCASECMP(field, "AUDIO"))? 0 : 1; /*@@@ should we be more strict with the value here? */
Josh Coalson's avatar
Josh Coalson committed
487
488
489
490
491
				in_index_num = -1;
				track_has_flags = false;
				track_has_isrc = false;
			}
			else if(0 == FLAC__STRCASECMP(field, "REM")) {
492
				if(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
Josh Coalson's avatar
Josh Coalson committed
493
494
					if(0 == strcmp(field, "FLAC__lead-in")) {
						FLAC__int64 xx;
495
						if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
Josh Coalson's avatar
Josh Coalson committed
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
							*error_message = "FLAC__lead-in is missing offset";
							return false;
						}
						xx = local__parse_int64_(field);
						if(xx < 0) {
							*error_message = "illegal FLAC__lead-in offset";
							return false;
						}
						if(is_cdda && xx % 588 != 0) {
							*error_message = "illegal CD-DA FLAC__lead-in offset, must be even multiple of 588 samples";
							return false;
						}
						cs->lead_in = (FLAC__uint64)xx;
					}
					else if(0 == strcmp(field, "FLAC__lead-out")) {
						int track_num;
						FLAC__int64 offset;
						if(has_forced_leadout) {
							*error_message = "multiple FLAC__lead-out commands";
							return false;
						}
517
						if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
Josh Coalson's avatar
Josh Coalson committed
518
519
520
521
522
523
524
525
526
							*error_message = "FLAC__lead-out is missing track number";
							return false;
						}
						track_num = local__parse_int_(field);
						if(track_num < 0) {
							*error_message = "illegal FLAC__lead-out track number";
							return false;
						}
						forced_leadout_track_num = (unsigned)track_num;
Josh Coalson's avatar
Josh Coalson committed
527
						/*@@@ search for duplicate track number? */
528
						if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
Josh Coalson's avatar
Josh Coalson committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
							*error_message = "FLAC__lead-out is missing offset";
							return false;
						}
						offset = local__parse_int64_(field);
						if(offset < 0) {
							*error_message = "illegal FLAC__lead-out offset";
							return false;
						}
						forced_leadout_track_offset = (FLAC__uint64)offset;
						if(forced_leadout_track_offset != lead_out_offset) {
							*error_message = "FLAC__lead-out offset does not match end-of-stream offset";
							return false;
						}
						has_forced_leadout = true;
					}
				}
545
546
547
548
			}
		}
	}

Josh Coalson's avatar
Josh Coalson committed
549
550
551
552
	if(cs->num_tracks == 0) {
		*error_message = "there must be at least one TRACK command";
		return false;
	}
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
	else {
		const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
		if(
			prev->num_indices == 0 ||
			(
				is_cdda &&
				(
					(prev->num_indices == 1 && prev->indices[0].number != 1) ||
					(prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
				)
			)
		) {
			*error_message = is_cdda?
				"previous TRACK must specify at least one INDEX 01" :
				"previous TRACK must specify at least one INDEX";
			return false;
		}
	}
Josh Coalson's avatar
Josh Coalson committed
571
572

	if(!has_forced_leadout) {
573
		forced_leadout_track_num = is_cdda? 170 : 255;
Josh Coalson's avatar
Josh Coalson committed
574
575
576
577
578
579
580
581
582
		forced_leadout_track_offset = lead_out_offset;
	}
	if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
		*error_message = "memory allocation error";
		return false;
	}
	cs->tracks[cs->num_tracks-1].number = forced_leadout_track_num;
	cs->tracks[cs->num_tracks-1].offset = forced_leadout_track_offset;

583
584
585
586
587
588
589
590
	if(!feof(file)) {
		*error_message = "read error";
		return false;
	}
	return true;
#undef FLAC__STRCASECMP
}

591
FLAC__StreamMetadata *grabbag__cuesheet_parse(FILE *file, const char **error_message, unsigned *last_line_read, unsigned sample_rate, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
{
	FLAC__StreamMetadata *cuesheet;

	FLAC__ASSERT(0 != file);
	FLAC__ASSERT(0 != error_message);
	FLAC__ASSERT(0 != last_line_read);

	*last_line_read = 0;
	cuesheet = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET);

	if(0 == cuesheet) {
		*error_message = "memory allocation error";
		return 0;
	}

607
	if(!local__cuesheet_parse_(file, error_message, last_line_read, cuesheet, sample_rate, is_cdda, lead_out_offset)) {
608
609
610
611
612
613
		FLAC__metadata_object_delete(cuesheet);
		return 0;
	}

	return cuesheet;
}
614

615
void grabbag__cuesheet_emit(FILE *file, const FLAC__StreamMetadata *cuesheet, const char *file_reference)
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
{
	const FLAC__StreamMetadata_CueSheet *cs;
	unsigned track_num, index_num;

	FLAC__ASSERT(0 != file);
	FLAC__ASSERT(0 != cuesheet);
	FLAC__ASSERT(cuesheet->type == FLAC__METADATA_TYPE_CUESHEET);

	cs = &cuesheet->data.cue_sheet;

	if(*(cs->media_catalog_number))
		fprintf(file, "CATALOG %s\n", cs->media_catalog_number);
	fprintf(file, "FILE %s\n", file_reference);

	for(track_num = 0; track_num < cs->num_tracks-1; track_num++) {
		const FLAC__StreamMetadata_CueSheet_Track *track = cs->tracks + track_num;

		fprintf(file, "  TRACK %02u %s\n", (unsigned)track->number, track->type == 0? "AUDIO" : "DATA");

		if(track->pre_emphasis)
			fprintf(file, "    FLAGS PRE\n");
		if(*(track->isrc))
			fprintf(file, "    ISRC %s\n", track->isrc);

		for(index_num = 0; index_num < track->num_indices; index_num++) {
641
			const FLAC__StreamMetadata_CueSheet_Index *indx = track->indices + index_num;
642

643
			fprintf(file, "    INDEX %02u ", (unsigned)indx->number);
644
			if(cs->is_cd) {
645
				const unsigned logical_frame = (unsigned)((track->offset + indx->offset) / (44100 / 75));
646
647
648
649
650
				unsigned m, s, f;
				grabbag__cuesheet_frame_to_msf(logical_frame, &m, &s, &f);
				fprintf(file, "%02u:%02u:%02u\n", m, s, f);
			}
			else
651
				fprintf(file, "%" PRIu64 "\n", (track->offset + indx->offset));
652
653
654
		}
	}

655
656
	fprintf(file, "REM FLAC__lead-in %" PRIu64 "\n", cs->lead_in);
	fprintf(file, "REM FLAC__lead-out %u %" PRIu64 "\n", (unsigned)cs->tracks[track_num].number, cs->tracks[track_num].offset);
657
}