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