1 /* test_cuesheet - Simple tester for cuesheet routines in grabbag
2  * Copyright (C) 2002-2009  Josh Coalson
3  * Copyright (C) 2011-2016  Xiph.Org Foundation
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23 
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "FLAC/assert.h"
29 #include "FLAC/metadata.h"
30 #include "share/grabbag.h"
31 
do_cuesheet(const char * infilename,unsigned sample_rate,FLAC__bool is_cdda,FLAC__uint64 lead_out_offset)32 static int do_cuesheet(const char *infilename, unsigned sample_rate, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
33 {
34 	FILE *fin, *fout;
35 	const char *error_message, *tmpfilenamebase;
36 	char tmpfilename[4096];
37 	unsigned last_line_read;
38 	FLAC__StreamMetadata *cuesheet;
39 
40 	FLAC__ASSERT(strlen(infilename) + 2 < sizeof(tmpfilename));
41 
42 	/*
43 	 * pass 1
44 	 */
45 	if(0 == strcmp(infilename, "-")) {
46 		fin = stdin;
47 	}
48 	else if(0 == (fin = flac_fopen(infilename, "r"))) {
49 		fprintf(stderr, "can't open file %s for reading: %s\n", infilename, strerror(errno));
50 		return 255;
51 	}
52 	if(0 != (cuesheet = grabbag__cuesheet_parse(fin, &error_message, &last_line_read, sample_rate, is_cdda, lead_out_offset))) {
53 		if(fin != stdin)
54 			fclose(fin);
55 	}
56 	else {
57 		printf("pass1: parse error, line %u: \"%s\"\n", last_line_read, error_message);
58 		if(fin != stdin)
59 			fclose(fin);
60 		return 1;
61 	}
62 	if(!FLAC__metadata_object_cuesheet_is_legal(cuesheet, is_cdda, &error_message)) {
63 		printf("pass1: illegal cuesheet: \"%s\"\n", error_message);
64 		FLAC__metadata_object_delete(cuesheet);
65 		return 1;
66 	}
67 
68 	tmpfilenamebase = strstr(infilename, "cuesheets/");
69 	tmpfilenamebase = tmpfilenamebase == NULL ? infilename : tmpfilenamebase;
70 
71 	flac_snprintf(tmpfilename, sizeof (tmpfilename), "%s.1", tmpfilenamebase);
72 	if(0 == (fout = flac_fopen(tmpfilename, "w"))) {
73 		fprintf(stderr, "can't open file %s for writing: %s\n", tmpfilename, strerror(errno));
74 		FLAC__metadata_object_delete(cuesheet);
75 		return 255;
76 	}
77 	grabbag__cuesheet_emit(fout, cuesheet, "\"dummy.wav\" WAVE");
78 	FLAC__metadata_object_delete(cuesheet);
79 	fclose(fout);
80 
81 	/*
82 	 * pass 2
83 	 */
84 	if(0 == (fin = flac_fopen(tmpfilename, "r"))) {
85 		fprintf(stderr, "can't open file %s for reading: %s\n", tmpfilename, strerror(errno));
86 		return 255;
87 	}
88 	if(0 != (cuesheet = grabbag__cuesheet_parse(fin, &error_message, &last_line_read, sample_rate, is_cdda, lead_out_offset))) {
89 		if(fin != stdin)
90 			fclose(fin);
91 	}
92 	else {
93 		printf("pass2: parse error, line %u: \"%s\"\n", last_line_read, error_message);
94 		if(fin != stdin)
95 			fclose(fin);
96 		return 1;
97 	}
98 	if(!FLAC__metadata_object_cuesheet_is_legal(cuesheet, is_cdda, &error_message)) {
99 		printf("pass2: illegal cuesheet: \"%s\"\n", error_message);
100 		FLAC__metadata_object_delete(cuesheet);
101 		return 1;
102 	}
103 	flac_snprintf(tmpfilename, sizeof (tmpfilename), "%s.2", tmpfilenamebase);
104 	if(0 == (fout = flac_fopen(tmpfilename, "w"))) {
105 		fprintf(stderr, "can't open file %s for writing: %s\n", tmpfilename, strerror(errno));
106 		FLAC__metadata_object_delete(cuesheet);
107 		return 255;
108 	}
109 	grabbag__cuesheet_emit(fout, cuesheet, "\"dummy.wav\" WAVE");
110 	FLAC__metadata_object_delete(cuesheet);
111 	fclose(fout);
112 
113 	return 0;
114 }
115 
main(int argc,char * argv[])116 int main(int argc, char *argv[])
117 {
118 	FLAC__uint64 lead_out_offset;
119 	unsigned sample_rate = 48000;
120 	FLAC__bool is_cdda = false;
121 	const char *usage = "usage: test_cuesheet cuesheet_file lead_out_offset [ [ sample_rate ] cdda ]\n";
122 
123 	if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
124 		puts(usage);
125 		return 0;
126 	}
127 
128 	if(argc < 3 || argc > 5) {
129 		fputs(usage, stderr);
130 		return 255;
131 	}
132 
133 	lead_out_offset = (FLAC__uint64)strtoul(argv[2], 0, 10);
134 	if(argc >= 4) {
135 		sample_rate = (unsigned)atoi(argv[3]);
136 		if(argc >= 5) {
137 			if(0 == strcmp(argv[4], "cdda"))
138 				is_cdda = true;
139 			else {
140 				fputs(usage, stderr);
141 				return 255;
142 			}
143 		}
144 	}
145 
146 	return do_cuesheet(argv[1], sample_rate, is_cdda, lead_out_offset);
147 }
148