main.c 4.14 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
/* test_cuesheet - Simple tester for cuesheet routines in grabbag
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

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

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

31
static int do_cuesheet(const char *infilename, unsigned sample_rate, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
Josh Coalson's avatar
Josh Coalson committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{
	FILE *fin, *fout;
	const char *error_message;
	char tmpfilename[4096];
	unsigned last_line_read;
	FLAC__StreamMetadata *cuesheet;

	FLAC__ASSERT(strlen(infilename) + 2 < sizeof(tmpfilename));

	/*
	 * pass 1
	 */
	if(0 == strcmp(infilename, "-")) {
		fin = stdin;
	}
	else if(0 == (fin = fopen(infilename, "r"))) {
Josh Coalson's avatar
Josh Coalson committed
48
		fprintf(stderr, "can't open file %s for reading: %s\n", infilename, strerror(errno));
Josh Coalson's avatar
Josh Coalson committed
49
50
		return 255;
	}
51
	if(0 != (cuesheet = grabbag__cuesheet_parse(fin, &error_message, &last_line_read, sample_rate, is_cdda, lead_out_offset))) {
Josh Coalson's avatar
Josh Coalson committed
52
53
54
55
56
57
58
59
60
61
62
		if(fin != stdin)
			fclose(fin);
	}
	else {
		printf("pass1: parse error, line %u: \"%s\"\n", last_line_read, error_message);
		if(fin != stdin)
			fclose(fin);
		return 1;
	}
	if(!FLAC__metadata_object_cuesheet_is_legal(cuesheet, is_cdda, &error_message)) {
		printf("pass1: illegal cuesheet: \"%s\"\n", error_message);
Josh Coalson's avatar
Josh Coalson committed
63
		FLAC__metadata_object_delete(cuesheet);
Josh Coalson's avatar
Josh Coalson committed
64
65
66
67
		return 1;
	}
	sprintf(tmpfilename, "%s.1", infilename);
	if(0 == (fout = fopen(tmpfilename, "w"))) {
Josh Coalson's avatar
Josh Coalson committed
68
		fprintf(stderr, "can't open file %s for writing: %s\n", tmpfilename, strerror(errno));
Josh Coalson's avatar
Josh Coalson committed
69
		FLAC__metadata_object_delete(cuesheet);
Josh Coalson's avatar
Josh Coalson committed
70
71
		return 255;
	}
72
	grabbag__cuesheet_emit(fout, cuesheet, "\"dummy.wav\" WAVE");
Josh Coalson's avatar
Josh Coalson committed
73
	FLAC__metadata_object_delete(cuesheet);
Josh Coalson's avatar
Josh Coalson committed
74
75
76
77
78
79
	fclose(fout);

	/*
	 * pass 2
	 */
	if(0 == (fin = fopen(tmpfilename, "r"))) {
Josh Coalson's avatar
Josh Coalson committed
80
		fprintf(stderr, "can't open file %s for reading: %s\n", tmpfilename, strerror(errno));
Josh Coalson's avatar
Josh Coalson committed
81
82
		return 255;
	}
83
	if(0 != (cuesheet = grabbag__cuesheet_parse(fin, &error_message, &last_line_read, sample_rate, is_cdda, lead_out_offset))) {
Josh Coalson's avatar
Josh Coalson committed
84
85
86
87
88
89
90
91
92
93
94
		if(fin != stdin)
			fclose(fin);
	}
	else {
		printf("pass2: parse error, line %u: \"%s\"\n", last_line_read, error_message);
		if(fin != stdin)
			fclose(fin);
		return 1;
	}
	if(!FLAC__metadata_object_cuesheet_is_legal(cuesheet, is_cdda, &error_message)) {
		printf("pass2: illegal cuesheet: \"%s\"\n", error_message);
Josh Coalson's avatar
Josh Coalson committed
95
		FLAC__metadata_object_delete(cuesheet);
Josh Coalson's avatar
Josh Coalson committed
96
97
98
99
		return 1;
	}
	sprintf(tmpfilename, "%s.2", infilename);
	if(0 == (fout = fopen(tmpfilename, "w"))) {
Josh Coalson's avatar
Josh Coalson committed
100
		fprintf(stderr, "can't open file %s for writing: %s\n", tmpfilename, strerror(errno));
Josh Coalson's avatar
Josh Coalson committed
101
		FLAC__metadata_object_delete(cuesheet);
Josh Coalson's avatar
Josh Coalson committed
102
103
		return 255;
	}
104
	grabbag__cuesheet_emit(fout, cuesheet, "\"dummy.wav\" WAVE");
Josh Coalson's avatar
Josh Coalson committed
105
	FLAC__metadata_object_delete(cuesheet);
Josh Coalson's avatar
Josh Coalson committed
106
107
108
109
110
111
112
113
	fclose(fout);

	return 0;
}

int main(int argc, char *argv[])
{
	FLAC__uint64 lead_out_offset;
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
114
	unsigned sample_rate = 48000;
Josh Coalson's avatar
Josh Coalson committed
115
	FLAC__bool is_cdda = false;
116
	const char *usage = "usage: test_cuesheet cuesheet_file lead_out_offset [ [ sample_rate ] cdda ]\n";
Josh Coalson's avatar
Josh Coalson committed
117

118
	if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
119
		printf("%s", usage);
120
121
122
		return 0;
	}

123
	if(argc < 3 || argc > 5) {
124
		fprintf(stderr, "%s", usage);
Josh Coalson's avatar
Josh Coalson committed
125
126
127
128
		return 255;
	}

	lead_out_offset = (FLAC__uint64)strtoul(argv[2], 0, 10);
129
130
131
132
133
134
	if(argc >= 4) {
		sample_rate = (unsigned)atoi(argv[3]);
		if(argc >= 5) {
			if(0 == strcmp(argv[4], "cdda"))
				is_cdda = true;
			else {
135
				fprintf(stderr, "%s", usage);
136
137
				return 255;
			}
Josh Coalson's avatar
Josh Coalson committed
138
139
140
		}
	}

141
	return do_cuesheet(argv[1], sample_rate, is_cdda, lead_out_offset);
Josh Coalson's avatar
Josh Coalson committed
142
}