1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002,2003,2004 Josh Coalson
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.
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.
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.
21 #include "FLAC/assert.h"
22 #include "share/utf8.h"
26 static FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write);
27 static FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
28 static FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
29 static FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw);
30 static FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool *needs_write, FLAC__bool raw);
31 static FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool raw);
33 FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw)
35 FLAC__bool ok = true, found_vc_block = false;
36 FLAC__StreamMetadata *block = 0;
37 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
40 die("out of memory allocating iterator");
42 FLAC__metadata_iterator_init(iterator, chain);
45 block = FLAC__metadata_iterator_get_block(iterator);
46 if(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
47 found_vc_block = true;
48 } while(!found_vc_block && FLAC__metadata_iterator_next(iterator));
51 /* create a new block if necessary */
52 if(operation->type == OP__SET_VC_FIELD || operation->type == OP__IMPORT_VC_FROM) {
53 block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
55 die("out of memory allocating VORBIS_COMMENT block");
56 while(FLAC__metadata_iterator_next(iterator))
58 if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) {
59 print_error_with_chain_status(chain, "%s: ERROR: adding new VORBIS_COMMENT block to metadata", filename);
62 /* iterator is left pointing to new block */
63 FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == block);
66 FLAC__metadata_iterator_delete(iterator);
71 FLAC__ASSERT(0 != block);
72 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
74 switch(operation->type) {
75 case OP__SHOW_VC_VENDOR:
76 write_vc_field(prefix_with_filename? filename : 0, &block->data.vorbis_comment.vendor_string, raw, stdout);
78 case OP__SHOW_VC_FIELD:
79 write_vc_fields(prefix_with_filename? filename : 0, operation->argument.vc_field_name.value, block->data.vorbis_comment.comments, block->data.vorbis_comment.num_comments, raw, stdout);
81 case OP__REMOVE_VC_ALL:
82 ok = remove_vc_all(filename, block, needs_write);
84 case OP__REMOVE_VC_FIELD:
85 ok = remove_vc_field(filename, block, operation->argument.vc_field_name.value, needs_write);
87 case OP__REMOVE_VC_FIRSTFIELD:
88 ok = remove_vc_firstfield(filename, block, operation->argument.vc_field_name.value, needs_write);
90 case OP__SET_VC_FIELD:
91 ok = set_vc_field(filename, block, &operation->argument.vc_field, needs_write, raw);
93 case OP__IMPORT_VC_FROM:
94 ok = import_vc_from(filename, block, &operation->argument.filename, needs_write, raw);
96 case OP__EXPORT_VC_TO:
97 ok = export_vc_to(filename, block, &operation->argument.filename, raw);
105 FLAC__metadata_iterator_delete(iterator);
113 FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write)
115 FLAC__ASSERT(0 != block);
116 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
117 FLAC__ASSERT(0 != needs_write);
119 if(0 != block->data.vorbis_comment.comments) {
120 FLAC__ASSERT(block->data.vorbis_comment.num_comments > 0);
121 if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, 0)) {
122 fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
128 FLAC__ASSERT(block->data.vorbis_comment.num_comments == 0);
134 FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write)
138 FLAC__ASSERT(0 != needs_write);
140 n = FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, field_name);
143 fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
152 FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write)
156 FLAC__ASSERT(0 != needs_write);
158 n = FLAC__metadata_object_vorbiscomment_remove_entry_matching(block, field_name);
161 fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
170 FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw)
172 FLAC__StreamMetadata_VorbisComment_Entry entry;
174 FLAC__bool needs_free = false;
176 FLAC__ASSERT(0 != block);
177 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
178 FLAC__ASSERT(0 != field);
179 FLAC__ASSERT(0 != needs_write);
182 entry.entry = (FLAC__byte *)field->field;
184 else if(utf8_encode(field->field, &converted) >= 0) {
185 entry.entry = (FLAC__byte *)converted;
189 fprintf(stderr, "%s: ERROR: couldn't convert comment to UTF-8\n", filename);
193 entry.length = strlen((const char *)entry.entry);
195 if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, block->data.vorbis_comment.num_comments, entry, /*copy=*/true)) {
198 fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
209 FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool *needs_write, FLAC__bool raw)
215 if(0 == vc_filename->value || strlen(vc_filename->value) == 0) {
216 fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
219 if(0 == strcmp(vc_filename->value, "-"))
222 f = fopen(vc_filename->value, "r");
225 fprintf(stderr, "%s: ERROR: can't open import file %s\n", filename, vc_filename->value);
230 while(ret && !feof(f)) {
231 fgets(line, sizeof(line), f);
233 char *p = strchr(line, '\n');
235 fprintf(stderr, "%s: ERROR: line too long, aborting\n", vc_filename->value);
239 const char *violation;
240 Argument_VcField field;
242 memset(&field, 0, sizeof(Argument_VcField));
243 if(!parse_vorbis_comment_field(line, &field.field, &field.field_name, &field.field_value, &field.field_value_length, &violation)) {
244 FLAC__ASSERT(0 != violation);
245 fprintf(stderr, "%s: ERROR: malformed vorbis comment field \"%s\",\n %s\n", vc_filename->value, line, violation);
249 ret = set_vc_field(filename, block, &field, needs_write, raw);
253 if(0 != field.field_name)
254 free(field.field_name);
255 if(0 != field.field_value)
256 free(field.field_value);
266 FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool raw)
271 if(0 == vc_filename->value || strlen(vc_filename->value) == 0) {
272 fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
275 if(0 == strcmp(vc_filename->value, "-"))
278 f = fopen(vc_filename->value, "w");
281 fprintf(stderr, "%s: ERROR: can't open export file %s\n", filename, vc_filename->value);
287 write_vc_fields(0, 0, block->data.vorbis_comment.comments, block->data.vorbis_comment.num_comments, raw, f);