update copyright for 2008
[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  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
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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, 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, 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         sprintf(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, 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         sprintf(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         FLAC__bool is_cdda = false;
115         const char *usage = "usage: test_cuesheet cuesheet_file lead_out_offset [ cdda ]\n";
116
117         if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
118                 printf(usage);
119                 return 0;
120         }
121
122         if(argc < 3 || argc > 4) {
123                 fprintf(stderr, usage);
124                 return 255;
125         }
126
127         lead_out_offset = (FLAC__uint64)strtoul(argv[2], 0, 10);
128         if(argc == 4) {
129                 if(0 == strcmp(argv[3], "cdda"))
130                         is_cdda = true;
131                 else {
132                         fprintf(stderr, usage);
133                         return 255;
134                 }
135         }
136
137         return do_cuesheet(argv[1], is_cdda, lead_out_offset);
138 }