Add 2003 to copyright notice
[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  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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "FLAC/assert.h"
23 #include "FLAC/metadata.h"
24 #include "share/grabbag.h"
25
26 static int do_cuesheet(const char *infilename, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
27 {
28         FILE *fin, *fout;
29         const char *error_message;
30         char tmpfilename[4096];
31         unsigned last_line_read;
32         FLAC__StreamMetadata *cuesheet;
33
34         FLAC__ASSERT(strlen(infilename) + 2 < sizeof(tmpfilename));
35
36         /*
37          * pass 1
38          */
39         if(0 == strcmp(infilename, "-")) {
40                 fin = stdin;
41         }
42         else if(0 == (fin = fopen(infilename, "r"))) {
43                 fprintf(stderr, "can't open file %s for reading\n", infilename);
44                 return 255;
45         }
46         if(0 != (cuesheet = grabbag__cuesheet_parse(fin, &error_message, &last_line_read, is_cdda, lead_out_offset))) {
47                 if(fin != stdin)
48                         fclose(fin);
49         }
50         else {
51                 printf("pass1: parse error, line %u: \"%s\"\n", last_line_read, error_message);
52                 if(fin != stdin)
53                         fclose(fin);
54                 return 1;
55         }
56         if(!FLAC__metadata_object_cuesheet_is_legal(cuesheet, is_cdda, &error_message)) {
57                 printf("pass1: illegal cuesheet: \"%s\"\n", error_message);
58                 FLAC__metadata_object_delete(cuesheet);
59                 return 1;
60         }
61         sprintf(tmpfilename, "%s.1", infilename);
62         if(0 == (fout = fopen(tmpfilename, "w"))) {
63                 fprintf(stderr, "can't open file %s for writing\n", tmpfilename);
64                 FLAC__metadata_object_delete(cuesheet);
65                 return 255;
66         }
67         grabbag__cuesheet_emit(fout, cuesheet, "\"dummy.wav\" WAVE");
68         FLAC__metadata_object_delete(cuesheet);
69         fclose(fout);
70
71         /*
72          * pass 2
73          */
74         if(0 == (fin = fopen(tmpfilename, "r"))) {
75                 fprintf(stderr, "can't open file %s for reading\n", tmpfilename);
76                 return 255;
77         }
78         if(0 != (cuesheet = grabbag__cuesheet_parse(fin, &error_message, &last_line_read, is_cdda, lead_out_offset))) {
79                 if(fin != stdin)
80                         fclose(fin);
81         }
82         else {
83                 printf("pass2: parse error, line %u: \"%s\"\n", last_line_read, error_message);
84                 if(fin != stdin)
85                         fclose(fin);
86                 return 1;
87         }
88         if(!FLAC__metadata_object_cuesheet_is_legal(cuesheet, is_cdda, &error_message)) {
89                 printf("pass2: illegal cuesheet: \"%s\"\n", error_message);
90                 FLAC__metadata_object_delete(cuesheet);
91                 return 1;
92         }
93         sprintf(tmpfilename, "%s.2", infilename);
94         if(0 == (fout = fopen(tmpfilename, "w"))) {
95                 fprintf(stderr, "can't open file %s for writing\n", tmpfilename);
96                 FLAC__metadata_object_delete(cuesheet);
97                 return 255;
98         }
99         grabbag__cuesheet_emit(fout, cuesheet, "\"dummy.wav\" WAVE");
100         FLAC__metadata_object_delete(cuesheet);
101         fclose(fout);
102
103         return 0;
104 }
105
106 int main(int argc, char *argv[])
107 {
108         FLAC__uint64 lead_out_offset;
109         FLAC__bool is_cdda = false;
110         const char *usage = "usage: test_cuesheet cuesheet_file lead_out_offset [ cdda ]\n";
111
112         if(argc > 1 && 0 == strcmp(argv[1], "-h")) {
113                 printf(usage);
114                 return 0;
115         }
116
117         if(argc < 3 || argc > 4) {
118                 fprintf(stderr, usage);
119                 return 255;
120         }
121
122         lead_out_offset = (FLAC__uint64)strtoul(argv[2], 0, 10);
123         if(argc == 4) {
124                 if(0 == strcmp(argv[3], "cdda"))
125                         is_cdda = true;
126                 else {
127                         fprintf(stderr, usage);
128                         return 255;
129                 }
130         }
131
132         return do_cuesheet(argv[1], is_cdda, lead_out_offset);
133 }