rename plugin-common lib to plugin_common
[flac.git] / src / plugin_common / vorbiscomment.c
1 /* plugin_common - Routines common to several plugins
2  * Copyright (C) 2002  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 <string.h>
20 #include <stdlib.h>
21
22 #include "vorbiscomment.h"
23 #include "FLAC/metadata.h"
24
25 static int local__vcentry_matches(const char *field_name, const FLAC__StreamMetadata_VorbisComment_Entry *entry)
26 {
27         const FLAC__byte *eq = memchr(entry->entry, '=', entry->length);
28         const unsigned field_name_length = strlen(field_name);
29         return (0 != eq && (unsigned)(eq-entry->entry) == field_name_length && 0 == strncasecmp(field_name, entry->entry, field_name_length));
30 }
31
32 static void local__vcentry_parse_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry, char **dest)
33 {
34         const FLAC__byte *eq = memchr(entry->entry, '=', entry->length);
35
36         if(0 == eq)
37                 return;
38         else {
39                 const unsigned value_length = entry->length - (unsigned)((++eq) - entry->entry);
40
41                 *dest = malloc(value_length + 1);
42                 if(0 != *dest) {
43                         memcpy(*dest, eq, value_length);
44                         (*dest)[value_length] = '\0';
45                 }
46         }
47 }
48
49 void FLAC_plugin__vorbiscomment_get(const char *filename, FLAC_Plugin__CanonicalTag *tag)
50 {
51         FLAC__Metadata_SimpleIterator *iterator = FLAC__metadata_simple_iterator_new();
52         if(0 != iterator) {
53                 if(FLAC__metadata_simple_iterator_init(iterator, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
54                         FLAC__bool got_vorbis_comments = false;
55                         do {
56                                 if(FLAC__metadata_simple_iterator_get_block_type(iterator) == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
57                                         FLAC__StreamMetadata *block = FLAC__metadata_simple_iterator_get_block(iterator);
58                                         if(0 != block) {
59                                                 unsigned i;
60                                                 const FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment;
61                                                 for(i = 0; i < vc->num_comments; i++) {
62                                                         if(local__vcentry_matches("artist", &vc->comments[i]))
63                                                                 local__vcentry_parse_value(&vc->comments[i], &tag->composer);
64                                                         else if(local__vcentry_matches("performer", &vc->comments[i]))
65                                                                 local__vcentry_parse_value(&vc->comments[i], &tag->performer);
66                                                         else if(local__vcentry_matches("album", &vc->comments[i]))
67                                                                 local__vcentry_parse_value(&vc->comments[i], &tag->album);
68                                                         else if(local__vcentry_matches("title", &vc->comments[i]))
69                                                                 local__vcentry_parse_value(&vc->comments[i], &tag->title);
70                                                         else if(local__vcentry_matches("tracknumber", &vc->comments[i]))
71                                                                 local__vcentry_parse_value(&vc->comments[i], &tag->track_number);
72                                                         else if(local__vcentry_matches("genre", &vc->comments[i]))
73                                                                 local__vcentry_parse_value(&vc->comments[i], &tag->genre);
74                                                         else if(local__vcentry_matches("description", &vc->comments[i]))
75                                                                 local__vcentry_parse_value(&vc->comments[i], &tag->comment);
76                                                 }
77                                                 FLAC__metadata_object_delete(block);
78                                                 got_vorbis_comments = true;
79                                         }
80                                 }
81                         } while (!got_vorbis_comments && FLAC__metadata_simple_iterator_next(iterator));
82                 }
83                 FLAC__metadata_simple_iterator_delete(iterator);
84         }
85 }