_snprintf hackery for MSVC6
[platform/upstream/flac.git] / src / metaflac / operations_shorthand_cuesheet.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001,2002,2003,2004,2005,2006  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> /* for snprintf() */
25 #include <string.h>
26 #include "options.h"
27 #include "utils.h"
28 #include "FLAC/assert.h"
29 #include "share/grabbag.h"
30
31 static FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link);
32 static FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename);
33
34 FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
35 {
36         FLAC__bool ok = true;
37         FLAC__StreamMetadata *cuesheet = 0;
38         FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
39         FLAC__uint64 lead_out_offset = 0;
40         FLAC__bool is_cdda = false;
41
42         if(0 == iterator)
43                 die("out of memory allocating iterator");
44
45         FLAC__metadata_iterator_init(iterator, chain);
46
47         do {
48                 FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
49                 if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
50                         lead_out_offset = block->data.stream_info.total_samples;
51                         if(lead_out_offset == 0) {
52                                 fprintf(stderr, "%s: ERROR: FLAC file must have total_samples set in STREAMINFO in order to import/export cuesheet\n", filename);
53                                 FLAC__metadata_iterator_delete(iterator);
54                                 return false;
55                         }
56                         is_cdda = (block->data.stream_info.channels == 1 || block->data.stream_info.channels == 2) && (block->data.stream_info.bits_per_sample == 16) && (block->data.stream_info.sample_rate == 44100);
57                 }
58                 else if(block->type == FLAC__METADATA_TYPE_CUESHEET)
59                         cuesheet = block;
60         } while(FLAC__metadata_iterator_next(iterator));
61
62         if(lead_out_offset == 0) {
63                 fprintf(stderr, "%s: ERROR: FLAC stream has no STREAMINFO block\n", filename);
64                 FLAC__metadata_iterator_delete(iterator);
65                 return false;
66         }
67
68         switch(operation->type) {
69                 case OP__IMPORT_CUESHEET_FROM:
70                         if(0 != cuesheet) {
71                                 fprintf(stderr, "%s: ERROR: FLAC file already has CUESHEET block\n", filename);
72                                 ok = false;
73                         }
74                         else {
75                                 ok = import_cs_from(filename, &cuesheet, operation->argument.import_cuesheet_from.filename, needs_write, lead_out_offset, is_cdda, operation->argument.import_cuesheet_from.add_seekpoint_link);
76                                 if(ok) {
77                                         /* append CUESHEET block */
78                                         while(FLAC__metadata_iterator_next(iterator))
79                                                 ;
80                                         if(!FLAC__metadata_iterator_insert_block_after(iterator, cuesheet)) {
81                                                 print_error_with_chain_status(chain, "%s: ERROR: adding new CUESHEET block to metadata", filename);
82                                                 FLAC__metadata_object_delete(cuesheet);
83                                                 ok = false;
84                                         }
85                                 }
86                         }
87                         break;
88                 case OP__EXPORT_CUESHEET_TO:
89                         if(0 == cuesheet) {
90                                 fprintf(stderr, "%s: ERROR: FLAC file has no CUESHEET block\n", filename);
91                                 ok = false;
92                         }
93                         else
94                                 ok = export_cs_to(filename, cuesheet, operation->argument.filename.value);
95                         break;
96                 default:
97                         ok = false;
98                         FLAC__ASSERT(0);
99                         break;
100         };
101
102         FLAC__metadata_iterator_delete(iterator);
103         return ok;
104 }
105
106 /*
107  * local routines
108  */
109
110 FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link)
111 {
112         FILE *f;
113         const char *error_message;
114         char **seekpoint_specification = add_seekpoint_link? &(add_seekpoint_link->specification) : 0;
115         unsigned last_line_read;
116
117         if(0 == cs_filename || strlen(cs_filename) == 0) {
118                 fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
119                 return false;
120         }
121         if(0 == strcmp(cs_filename, "-"))
122                 f = stdin;
123         else
124                 f = fopen(cs_filename, "r");
125
126         if(0 == f) {
127                 fprintf(stderr, "%s: ERROR: can't open import file %s: %s\n", filename, cs_filename, strerror(errno));
128                 return false;
129         }
130
131         *cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, is_cdda, lead_out_offset);
132
133         if(f != stdin)
134                 fclose(f);
135
136         if(0 == *cuesheet) {
137                 fprintf(stderr, "%s: ERROR: while parsing cuesheet \"%s\" on line %u: %s\n", filename, cs_filename, last_line_read, error_message);
138                 return false;
139         }
140
141         if(!FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/false, &error_message)) {
142                 fprintf(stderr, "%s: ERROR parsing cuesheet \"%s\": %s\n", filename, cs_filename, error_message);
143                 return false;
144         }
145
146         /* if we're expecting CDDA, warn about non-compliance */
147         if(is_cdda && !FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/true, &error_message)) {
148                 fprintf(stderr, "%s: WARNING cuesheet \"%s\" is not audio CD compliant: %s\n", filename, cs_filename, error_message);
149                 (*cuesheet)->data.cue_sheet.is_cd = false;
150         }
151
152         /* add seekpoints for each index point if required */
153         if(0 != seekpoint_specification) {
154                 char spec[128];
155                 unsigned track, index;
156                 const FLAC__StreamMetadata_CueSheet *cs = &(*cuesheet)->data.cue_sheet;
157                 if(0 == *seekpoint_specification)
158                         *seekpoint_specification = local_strdup("");
159                 for(track = 0; track < cs->num_tracks; track++) {
160                         const FLAC__StreamMetadata_CueSheet_Track *tr = cs->tracks+track;
161                         for(index = 0; index < tr->num_indices; index++) {
162 #ifdef _MSC_VER
163                                 sprintf(spec, "%I64u;", tr->offset + tr->indices[index].offset);
164 #else
165                                 sprintf(spec, "%llu;", tr->offset + tr->indices[index].offset);
166 #endif
167                                 local_strcat(seekpoint_specification, spec);
168                         }
169                 }
170         }
171
172         *needs_write = true;
173         return true;
174 }
175
176 FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename)
177 {
178         FILE *f;
179         char *ref = 0;
180         size_t reflen;
181
182         if(0 == cs_filename || strlen(cs_filename) == 0) {
183                 fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
184                 return false;
185         }
186         if(0 == strcmp(cs_filename, "-"))
187                 f = stdout;
188         else
189                 f = fopen(cs_filename, "w");
190
191         if(0 == f) {
192                 fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, cs_filename, strerror(errno));
193                 return false;
194         }
195
196         reflen = strlen(filename) + 7 + 1;
197         if(0 == (ref = malloc(reflen))) {
198                 fprintf(stderr, "%s: ERROR: allocating memory\n", filename);
199                 return false;
200         }
201
202 #if defined _MSC_VER || defined __MINGW32__
203         _snprintf(ref, reflen, "\"%s\" FLAC", filename);
204 #else
205         snprintf(ref, reflen, "\"%s\" FLAC", filename);
206 #endif
207
208         grabbag__cuesheet_emit(f, cuesheet, ref);
209
210         free(ref);
211
212         if(f != stdout)
213                 fclose(f);
214
215         return true;
216 }