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