*** empty log message ***
[platform/upstream/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 #if defined _MSC_VER || defined __MINGW32__
28 #define FLAC__STRNCASECMP strnicmp
29 #else
30 #define FLAC__STRNCASECMP strncasecmp
31 #endif
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));
35 }
36
37 static void local__vcentry_parse_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry, char **dest)
38 {
39         const FLAC__byte *eq = memchr(entry->entry, '=', entry->length);
40
41         if(0 == eq)
42                 return;
43         else {
44                 const unsigned value_length = entry->length - (unsigned)((++eq) - entry->entry);
45
46                 *dest = malloc(value_length + 1);
47                 if(0 != *dest) {
48                         memcpy(*dest, eq, value_length);
49                         (*dest)[value_length] = '\0';
50                 }
51         }
52 }
53
54 void FLAC_plugin__vorbiscomment_get(const char *filename, FLAC_Plugin__CanonicalTag *tag)
55 {
56         FLAC__Metadata_SimpleIterator *iterator = FLAC__metadata_simple_iterator_new();
57         if(0 != iterator) {
58                 if(FLAC__metadata_simple_iterator_init(iterator, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
59                         FLAC__bool got_vorbis_comments = false;
60                         do {
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);
63                                         if(0 != block) {
64                                                 unsigned i;
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);
81                                                 }
82                                                 FLAC__metadata_object_delete(block);
83                                                 got_vorbis_comments = true;
84                                         }
85                                 }
86                         } while (!got_vorbis_comments && FLAC__metadata_simple_iterator_next(iterator));
87                 }
88                 FLAC__metadata_simple_iterator_delete(iterator);
89         }
90 }