File: FlacMetadataTag.cpp

package info (click to toggle)
alsaplayer 0.99.82-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,892 kB
  • sloc: ansic: 40,741; cpp: 14,400; makefile: 983; sh: 796; lex: 751; asm: 45; python: 29; sed: 16
file content (156 lines) | stat: -rw-r--r-- 4,451 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//---------------------------------------------------------------------------
//  Provide read-only access to flac stream tags.  Supports flac
//  VORBIS_COMMENT metadata blocks via libFLAC.
//
//  Copyright (c) 2003 by Tim Evans <t.evans@paradise.net.nz>
//
//  Based on FlacId3v1Tag.cpp by Drew Hess <dhess@bothan.net>.
//
//  Changes:
//  June 05, 2005 (Hubert Chan <hubert@uhoreg.ca>)
//    - don't use fixed-length buffers
//    - use an array instead of a long if-else chain
//
/*  This file is part of AlsaPlayer.
 *
 *  AlsaPlayer is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AlsaPlayer is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
//--------------------------------------------------------------------------


#include "FlacMetadataTag.h"

#include <cstring>
#include "reader.h"

extern "C" {
#include <FLAC/metadata.h>
}


static bool
find_vorbis_comment_metadata (const char *filename,
			      FLAC__StreamMetadata **metadata)
{
    FLAC__Metadata_SimpleIterator *flac = FLAC__metadata_simple_iterator_new ();
    if (!flac)
	return false;
    if (!FLAC__metadata_simple_iterator_init (flac, filename, true, false)) {
	FLAC__metadata_simple_iterator_delete (flac);
	return false;
    }

    bool status = false;
    FLAC__MetadataType type;
    do {
	type = FLAC__metadata_simple_iterator_get_block_type (flac);
	if (type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
	    status = true;
	    if (metadata)
		*metadata = FLAC__metadata_simple_iterator_get_block (flac);
	    break;
	}
    }
    while (FLAC__metadata_simple_iterator_next (flac));

    FLAC__metadata_simple_iterator_delete (flac);
    return status;
}


// Split the comment into key and value.  Key and value are newly allocated
// strings and should be deleted after they are no longer needed.
// Returns true if the split was successful; false otherwise.
static bool
parse_comment (const FLAC__StreamMetadata_VorbisComment_Entry *comment,
	       char **key, char **value)
{
    FLAC__byte *sep;
    size_t key_len, value_len;

    if ((sep = (FLAC__byte *) memchr(comment->entry, '=', comment->length))
	== NULL)
      return false;

    key_len = sep - comment->entry;
    value_len = comment->length - key_len - 1;

    (*key) = new char[key_len+1];
    memcpy (*key, comment->entry, key_len);
    (*key)[key_len] = '\0';

    (*value) = new char[value_len+1];
    memcpy (*value, sep + 1, value_len);
    (*value)[value_len] = '\0';

    return true;
}


namespace Flac
{

const FlacMetadataTag::field_mapping_t FlacMetadataTag::field_mappings[] =
{
    { "TITLE", &FlacMetadataTag::_title },
    { "ARTIST", &FlacMetadataTag::_artist },
    { "TRACKNUMBER", &FlacMetadataTag::_track },
    { "ALBUM", &FlacMetadataTag::_album },
    { "YEAR", &FlacMetadataTag::_year },
    { "DESCRIPTION", &FlacMetadataTag::_comment },
    { NULL, NULL }
};


// static
bool
FlacMetadataTag::hasMetadata (const std::string & name)
{
    return find_vorbis_comment_metadata (name.c_str (), NULL);

} // FlacMetadataTag::hasMetadata


FlacMetadataTag::FlacMetadataTag (const std::string & name)
    : FlacTag (name)
{
    FLAC__StreamMetadata *metadata = NULL;
    FLAC__StreamMetadata_VorbisComment_Entry *comment_;
    unsigned int i,j;
    char *key, *value;

    if (!find_vorbis_comment_metadata (name.c_str (), &metadata))
	return;

    for (i=0; i<metadata->data.vorbis_comment.num_comments; i++) {
	comment_ = &metadata->data.vorbis_comment.comments[i];
	if (parse_comment(comment_, &key, &value)) {
	    // look through the field mappings to see which field we have, and
	    // put it in the appropriate member
	    for (j=0; field_mappings[j].name != NULL; j++) {
	        if (strcmp (field_mappings[j].name, key) == 0)
		{
		    (this->*(field_mappings[j].value)) = value;
		}
	    }
	    delete key;
	    delete value;
	}
    }

    FLAC__metadata_object_delete (metadata);

} // FlacMetadataTag::FlacMetadataTag

} // namespace Flac