fix bug: %ll doesn't work for MSVC, use %I64 instead
[platform/upstream/flac.git] / src / metaflac / operations_shorthand_cuesheet.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001,2002,2003,2004  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 "options.h"
20 #include "utils.h"
21 #include "FLAC/assert.h"
22 #include "share/grabbag.h"
23 #include <string.h>
24
25 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, Argument_AddSeekpoint *add_seekpoint_link);
26 static FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename);
27
28 FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
29 {
30         FLAC__bool ok = true;
31         FLAC__StreamMetadata *cuesheet = 0;
32         FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
33         FLAC__uint64 lead_out_offset = 0;
34
35         if(0 == iterator)
36                 die("out of memory allocating iterator");
37
38         FLAC__metadata_iterator_init(iterator, chain);
39
40         do {
41                 FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
42                 if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
43                         lead_out_offset = block->data.stream_info.total_samples;
44                         if(lead_out_offset == 0) {
45                                 fprintf(stderr, "%s: ERROR: FLAC file must have total_samples set in STREAMINFO in order to import/export cuesheet\n", filename);
46                                 FLAC__metadata_iterator_delete(iterator);
47                                 return false;
48                         }
49                         if(block->data.stream_info.sample_rate != 44100) {
50                                 fprintf(stderr, "%s: ERROR: FLAC stream must currently be 44.1kHz in order to import/export cuesheet\n", filename);
51                                 FLAC__metadata_iterator_delete(iterator);
52                                 return false;
53                         }
54                 }
55                 else if(block->type == FLAC__METADATA_TYPE_CUESHEET)
56                         cuesheet = block;
57         } while(FLAC__metadata_iterator_next(iterator));
58
59         if(lead_out_offset == 0) {
60                 fprintf(stderr, "%s: ERROR: FLAC stream has no STREAMINFO block\n", filename);
61                 FLAC__metadata_iterator_delete(iterator);
62                 return false;
63         }
64
65         switch(operation->type) {
66                 case OP__IMPORT_CUESHEET_FROM:
67                         if(0 != cuesheet) {
68                                 fprintf(stderr, "%s: ERROR: FLAC file already has CUESHEET block\n", filename);
69                                 ok = false;
70                         }
71                         else {
72                                 ok = import_cs_from(filename, &cuesheet, operation->argument.import_cuesheet_from.filename, needs_write, lead_out_offset, operation->argument.import_cuesheet_from.add_seekpoint_link);
73                                 if(ok) {
74                                         /* append CUESHEET block */
75                                         while(FLAC__metadata_iterator_next(iterator))
76                                                 ;
77                                         if(!FLAC__metadata_iterator_insert_block_after(iterator, cuesheet)) {
78                                                 fprintf(stderr, "%s: ERROR: adding new CUESHEET block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
79                                                 FLAC__metadata_object_delete(cuesheet);
80                                                 ok = false;
81                                         }
82                                 }
83                         }
84                         break;
85                 case OP__EXPORT_CUESHEET_TO:
86                         if(0 == cuesheet) {
87                                 fprintf(stderr, "%s: ERROR: FLAC file has no CUESHEET block\n", filename);
88                                 ok = false;
89                         }
90                         else
91                                 ok = export_cs_to(filename, cuesheet, operation->argument.filename.value);
92                         break;
93                 default:
94                         ok = false;
95                         FLAC__ASSERT(0);
96                         break;
97         };
98
99         FLAC__metadata_iterator_delete(iterator);
100         return ok;
101 }
102
103 /*
104  * local routines
105  */
106
107 FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, Argument_AddSeekpoint *add_seekpoint_link)
108 {
109         FILE *f;
110         const char *error_message;
111         char **seekpoint_specification = add_seekpoint_link? &(add_seekpoint_link->specification) : 0;
112         unsigned last_line_read;
113
114         if(0 == cs_filename || strlen(cs_filename) == 0) {
115                 fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
116                 return false;
117         }
118         if(0 == strcmp(cs_filename, "-"))
119                 f = stdin;
120         else
121                 f = fopen(cs_filename, "r");
122
123         if(0 == f) {
124                 fprintf(stderr, "%s: ERROR: can't open import file %s\n", filename, cs_filename);
125                 return false;
126         }
127
128         *cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, /*@@@is_cdda=*/true, lead_out_offset);
129
130         if(f != stdin)
131                 fclose(f);
132
133         if(0 == *cuesheet) {
134                 fprintf(stderr, "%s: ERROR: while parsing cuesheet \"%s\" on line %u: %s\n", filename, cs_filename, last_line_read, error_message);
135                 return false;
136         }
137
138         /* add seekpoints for each index point if required */
139         if(0 != seekpoint_specification) {
140                 char spec[128];
141                 unsigned track, index;
142                 const FLAC__StreamMetadata_CueSheet *cs = &(*cuesheet)->data.cue_sheet;
143                 if(0 == *seekpoint_specification)
144                         *seekpoint_specification = local_strdup("");
145                 for(track = 0; track < cs->num_tracks; track++) {
146                         const FLAC__StreamMetadata_CueSheet_Track *tr = cs->tracks+track;
147                         for(index = 0; index < tr->num_indices; index++) {
148 #ifdef _MSC_VER
149                                 sprintf(spec, "%I64u;", tr->offset + tr->indices[index].offset);
150 #else
151                                 sprintf(spec, "%llu;", tr->offset + tr->indices[index].offset);
152 #endif
153                                 local_strcat(seekpoint_specification, spec);
154                         }
155                 }
156         }
157
158         *needs_write = true;
159         return true;
160 }
161
162 FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename)
163 {
164         FILE *f;
165
166         if(0 == cs_filename || strlen(cs_filename) == 0) {
167                 fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
168                 return false;
169         }
170         if(0 == strcmp(cs_filename, "-"))
171                 f = stdout;
172         else
173                 f = fopen(cs_filename, "w");
174
175         if(0 == f) {
176                 fprintf(stderr, "%s: ERROR: can't open export file %s\n", filename, cs_filename);
177                 return false;
178         }
179
180         grabbag__cuesheet_emit(f, cuesheet, "\"dummy.wav\" WAVE");
181
182         if(f != stdout)
183                 fclose(f);
184
185         return true;
186 }