1 /* plugin_common - Routines common to several plugins
2 * Copyright (C) 2002 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.
23 #include "vorbiscomment.h"
24 #include "FLAC/metadata.h"
26 static int local__vcentry_matches(const char *field_name, const FLAC__StreamMetadata_VorbisComment_Entry *entry)
28 #if defined _MSC_VER || defined __MINGW32__
29 #define FLAC__STRNCASECMP strnicmp
31 #define FLAC__STRNCASECMP strncasecmp
33 const FLAC__byte *eq = memchr(entry->entry, '=', entry->length);
34 const unsigned field_name_length = strlen(field_name);
35 return (0 != eq && (unsigned)(eq-entry->entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, entry->entry, field_name_length));
38 static void local__vcentry_parse_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry, char **dest)
40 const FLAC__byte *eq = memchr(entry->entry, '=', entry->length);
45 const unsigned value_length = entry->length - (unsigned)((++eq) - entry->entry);
47 *dest = malloc(value_length + 1);
49 memcpy(*dest, eq, value_length);
50 (*dest)[value_length] = '\0';
55 static void local__vc_change_field(FLAC__StreamMetadata *block, const char *name, const char *value)
61 for (l = -1; (i = FLAC__metadata_object_vorbiscomment_find_entry_from(block, l + 1, name)) != -1; l = i)
64 if(!value || !strlen(value)) {
66 FLAC__metadata_object_vorbiscomment_delete_comment(block, l);
70 s = malloc(strlen(name) + strlen(value) + 2);
72 FLAC__StreamMetadata_VorbisComment_Entry entry;
74 sprintf(s, "%s=%s", name, value);
76 entry.length = strlen(s);
80 FLAC__metadata_object_vorbiscomment_insert_comment(block, block->data.vorbis_comment.num_comments, entry, /*copy=*/true);
82 FLAC__metadata_object_vorbiscomment_set_comment(block, l, entry, /*copy=*/true);
87 void FLAC_plugin__vorbiscomment_get(const char *filename, FLAC_Plugin__CanonicalTag *tag)
89 FLAC__Metadata_SimpleIterator *iterator = FLAC__metadata_simple_iterator_new();
91 if(FLAC__metadata_simple_iterator_init(iterator, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
92 FLAC__bool got_vorbis_comments = false;
94 if(FLAC__metadata_simple_iterator_get_block_type(iterator) == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
95 FLAC__StreamMetadata *block = FLAC__metadata_simple_iterator_get_block(iterator);
98 const FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment;
99 for(i = 0; i < vc->num_comments; i++) {
100 if(local__vcentry_matches("artist", &vc->comments[i]))
101 local__vcentry_parse_value(&vc->comments[i], &tag->composer);
102 else if(local__vcentry_matches("performer", &vc->comments[i]))
103 local__vcentry_parse_value(&vc->comments[i], &tag->performer);
104 else if(local__vcentry_matches("album", &vc->comments[i]))
105 local__vcentry_parse_value(&vc->comments[i], &tag->album);
106 else if(local__vcentry_matches("title", &vc->comments[i]))
107 local__vcentry_parse_value(&vc->comments[i], &tag->title);
108 else if(local__vcentry_matches("tracknumber", &vc->comments[i]))
109 local__vcentry_parse_value(&vc->comments[i], &tag->track_number);
110 else if(local__vcentry_matches("genre", &vc->comments[i]))
111 local__vcentry_parse_value(&vc->comments[i], &tag->genre);
112 else if(local__vcentry_matches("description", &vc->comments[i]))
113 local__vcentry_parse_value(&vc->comments[i], &tag->comment);
114 else if(local__vcentry_matches("date", &vc->comments[i]))
115 local__vcentry_parse_value(&vc->comments[i], &tag->year_recorded);
117 FLAC__metadata_object_delete(block);
118 got_vorbis_comments = true;
121 } while (!got_vorbis_comments && FLAC__metadata_simple_iterator_next(iterator));
123 FLAC__metadata_simple_iterator_delete(iterator);
127 FLAC__bool FLAC_plugin__vorbiscomment_set(const char *filename, FLAC_Plugin__CanonicalTag *tag)
129 FLAC__bool got_vorbis_comments = false;
130 FLAC__Metadata_SimpleIterator *iterator = FLAC__metadata_simple_iterator_new();
131 FLAC__StreamMetadata *block;
133 if(!iterator || !FLAC__metadata_simple_iterator_init(iterator, filename, /*read_only=*/false, /*preserve_file_stats=*/true))
137 if(FLAC__metadata_simple_iterator_get_block_type(iterator) == FLAC__METADATA_TYPE_VORBIS_COMMENT)
138 got_vorbis_comments = true;
139 } while (!got_vorbis_comments && FLAC__metadata_simple_iterator_next(iterator));
141 if(!got_vorbis_comments) {
142 /* create a new block */
143 block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
146 FLAC__metadata_simple_iterator_delete(iterator);
151 block = FLAC__metadata_simple_iterator_get_block(iterator);
153 local__vc_change_field(block, "ARTIST", tag->composer);
154 local__vc_change_field(block, "PERFORMER", tag->performer);
155 local__vc_change_field(block, "ALBUM", tag->album);
156 local__vc_change_field(block, "TITLE", tag->title);
157 local__vc_change_field(block, "TRACKNUMBER", tag->track_number);
158 local__vc_change_field(block, "GENRE", tag->genre);
159 local__vc_change_field(block, "DESCRIPTION", tag->comment);
160 local__vc_change_field(block, "DATE", tag->year_recorded);
162 if(!got_vorbis_comments) {
163 if(!FLAC__metadata_simple_iterator_insert_block_after(iterator, block, /*use_padding=*/true)) {
164 FLAC__metadata_object_delete(block);
165 FLAC__metadata_simple_iterator_delete(iterator);
170 if(!FLAC__metadata_simple_iterator_set_block(iterator, block, /*use_padding=*/true)) {
171 FLAC__metadata_object_delete(block);
172 FLAC__metadata_simple_iterator_delete(iterator);
177 FLAC__metadata_object_delete(block);
178 FLAC__metadata_simple_iterator_delete(iterator);