Second patch from X-Fixer: tweaks for better MSVC build
[platform/upstream/flac.git] / src / plugin_common / vorbiscomment.c
1 /* plugin_common - Routines common to several plugins
2  * Copyright (C) 2002,2003  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 <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22
23 #include "vorbiscomment.h"
24 #include "FLAC/metadata.h"
25
26 static int local__vcentry_matches(const char *field_name, const FLAC__StreamMetadata_VorbisComment_Entry *entry)
27 {
28 #if defined _MSC_VER || defined __MINGW32__
29 #define FLAC__STRNCASECMP strnicmp
30 #else
31 #define FLAC__STRNCASECMP strncasecmp
32 #endif
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));
36 }
37
38 static void local__vcentry_parse_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry, char **dest)
39 {
40         const FLAC__byte *eq = memchr(entry->entry, '=', entry->length);
41
42         if(0 == eq)
43                 return;
44         else {
45                 const unsigned value_length = entry->length - (unsigned)((++eq) - entry->entry);
46
47                 *dest = malloc(value_length + 1);
48                 if(0 != *dest) {
49                         memcpy(*dest, eq, value_length);
50                         (*dest)[value_length] = '\0';
51                 }
52         }
53 }
54
55 static void local__vc_change_field(FLAC__StreamMetadata *block, const char *name, const char *value)
56 {
57         int i, l;
58         char *s;
59
60         /* find last */
61         for (l = -1; (i = FLAC__metadata_object_vorbiscomment_find_entry_from(block, l + 1, name)) != -1; l = i)
62                 ;
63                         
64         if(!value || !strlen(value)) {
65                 if (l != -1)
66                         FLAC__metadata_object_vorbiscomment_delete_comment(block, l);
67                 return;
68         }
69
70         s = malloc(strlen(name) + strlen(value) + 2);
71         if(s) {
72                 FLAC__StreamMetadata_VorbisComment_Entry entry;
73
74                 sprintf(s, "%s=%s", name, value);
75
76                 entry.length = strlen(s);
77                 entry.entry = s;
78                 
79                 if(l == -1)
80                         FLAC__metadata_object_vorbiscomment_insert_comment(block, block->data.vorbis_comment.num_comments, entry, /*copy=*/true);
81                 else
82                         FLAC__metadata_object_vorbiscomment_set_comment(block, l, entry, /*copy=*/true);
83                 free(s);
84         }
85 }
86
87 void FLAC_plugin__vorbiscomment_get(const char *filename, FLAC_Plugin__CanonicalTag *tag)
88 {
89         FLAC__Metadata_SimpleIterator *iterator = FLAC__metadata_simple_iterator_new();
90         if(0 != iterator) {
91                 if(FLAC__metadata_simple_iterator_init(iterator, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
92                         FLAC__bool got_vorbis_comments = false;
93                         do {
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);
96                                         if(0 != block) {
97                                                 unsigned i;
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);
116                                                 }
117                                                 FLAC__metadata_object_delete(block);
118                                                 got_vorbis_comments = true;
119                                         }
120                                 }
121                         } while (!got_vorbis_comments && FLAC__metadata_simple_iterator_next(iterator));
122                 }
123                 FLAC__metadata_simple_iterator_delete(iterator);
124         }
125 }
126
127 FLAC__bool FLAC_plugin__vorbiscomment_set(const char *filename, FLAC_Plugin__CanonicalTag *tag)
128 {
129         FLAC__bool got_vorbis_comments = false;
130         FLAC__Metadata_SimpleIterator *iterator = FLAC__metadata_simple_iterator_new();
131         FLAC__StreamMetadata *block;
132                 
133         if(!iterator || !FLAC__metadata_simple_iterator_init(iterator, filename, /*read_only=*/false, /*preserve_file_stats=*/true))
134                 return false;
135
136         do {
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));
140
141         if(!got_vorbis_comments) {
142                 /* create a new block */
143                 block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
144
145                 if(!block) {
146                         FLAC__metadata_simple_iterator_delete(iterator);
147                         return false;
148                 }
149         }
150         else
151                 block = FLAC__metadata_simple_iterator_get_block(iterator);
152
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);
161
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);
166                         return false;
167                 }
168         }
169         else {
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);
173                         return false;
174                 }
175         }
176
177         FLAC__metadata_object_delete(block);
178         FLAC__metadata_simple_iterator_delete(iterator);
179         return true;
180 }