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.
22 #include "vorbiscomment.h"
23 #include "FLAC/metadata.h"
25 static int local__vcentry_matches(const char *field_name, const FLAC__StreamMetadata_VorbisComment_Entry *entry)
27 #if defined _MSC_VER || defined __MINGW32__
28 #define FLAC__STRNCASECMP strnicmp
30 #define FLAC__STRNCASECMP strncasecmp
32 const FLAC__byte *eq = memchr(entry->entry, '=', entry->length);
33 const unsigned field_name_length = strlen(field_name);
34 return (0 != eq && (unsigned)(eq-entry->entry) == field_name_length && 0 == FLAC__STRNCASECMP(field_name, entry->entry, field_name_length));
37 static void local__vcentry_parse_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry, char **dest)
39 const FLAC__byte *eq = memchr(entry->entry, '=', entry->length);
44 const unsigned value_length = entry->length - (unsigned)((++eq) - entry->entry);
46 *dest = malloc(value_length + 1);
48 memcpy(*dest, eq, value_length);
49 (*dest)[value_length] = '\0';
54 void FLAC_plugin__vorbiscomment_get(const char *filename, FLAC_Plugin__CanonicalTag *tag)
56 FLAC__Metadata_SimpleIterator *iterator = FLAC__metadata_simple_iterator_new();
58 if(FLAC__metadata_simple_iterator_init(iterator, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
59 FLAC__bool got_vorbis_comments = false;
61 if(FLAC__metadata_simple_iterator_get_block_type(iterator) == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
62 FLAC__StreamMetadata *block = FLAC__metadata_simple_iterator_get_block(iterator);
65 const FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment;
66 for(i = 0; i < vc->num_comments; i++) {
67 if(local__vcentry_matches("artist", &vc->comments[i]))
68 local__vcentry_parse_value(&vc->comments[i], &tag->composer);
69 else if(local__vcentry_matches("performer", &vc->comments[i]))
70 local__vcentry_parse_value(&vc->comments[i], &tag->performer);
71 else if(local__vcentry_matches("album", &vc->comments[i]))
72 local__vcentry_parse_value(&vc->comments[i], &tag->album);
73 else if(local__vcentry_matches("title", &vc->comments[i]))
74 local__vcentry_parse_value(&vc->comments[i], &tag->title);
75 else if(local__vcentry_matches("tracknumber", &vc->comments[i]))
76 local__vcentry_parse_value(&vc->comments[i], &tag->track_number);
77 else if(local__vcentry_matches("genre", &vc->comments[i]))
78 local__vcentry_parse_value(&vc->comments[i], &tag->genre);
79 else if(local__vcentry_matches("description", &vc->comments[i]))
80 local__vcentry_parse_value(&vc->comments[i], &tag->comment);
82 FLAC__metadata_object_delete(block);
83 got_vorbis_comments = true;
86 } while (!got_vorbis_comments && FLAC__metadata_simple_iterator_next(iterator));
88 FLAC__metadata_simple_iterator_delete(iterator);