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