VorbisComments.cxx 2.63 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "VorbisComments.hxx"
21
#include "XiphTags.hxx"
22 23 24
#include "tag/Table.hxx"
#include "tag/Handler.hxx"
#include "tag/Builder.hxx"
25
#include "tag/Tag.hxx"
26
#include "tag/VorbisComment.hxx"
27
#include "tag/ReplayGain.hxx"
28
#include "ReplayGainInfo.hxx"
29
#include "util/StringView.hxx"
30

31
bool
32
vorbis_comments_to_replay_gain(ReplayGainInfo &rgi, char **comments) noexcept
33
{
34 35
	rgi.Clear();

36 37 38
	bool found = false;

	while (*comments) {
39
		if (ParseReplayGainVorbis(rgi, *comments))
40 41 42 43 44 45 46 47 48 49 50 51 52
			found = true;

		comments++;
	}

	return found;
}

/**
 * Check if the comment's name equals the passed name, and if so, copy
 * the comment value into the tag.
 */
static bool
53
vorbis_copy_comment(const char *comment,
54
		    const char *name, TagType tag_type,
55
		    TagHandler &handler) noexcept
56 57 58 59
{
	const char *value;

	value = vorbis_comment_value(comment, name);
60
	if (value != nullptr) {
61
		handler.OnTag(tag_type, value);
62 63 64 65 66 67 68
		return true;
	}

	return false;
}

static void
69
vorbis_scan_comment(const char *comment, TagHandler &handler) noexcept
70
{
71
	if (handler.WantPair()) {
72 73 74
		const auto split = StringView(comment).Split('=');
		if (!split.first.empty() && !split.second.IsNull())
			handler.OnPair(split.first, split.second);
75 76
	}

77
	for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
78
		if (vorbis_copy_comment(comment, i->name, i->type,
79
					handler))
80
			return;
81 82

	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
83
		if (vorbis_copy_comment(comment,
84
					tag_item_names[i], TagType(i),
85
					handler))
86 87 88
			return;
}

89
void
90
vorbis_comments_scan(char **comments, TagHandler &handler) noexcept
91 92
{
	while (*comments)
93
		vorbis_scan_comment(*comments++, handler);
94 95 96

}

97
std::unique_ptr<Tag>
98
vorbis_comments_to_tag(char **comments) noexcept
99
{
100
	TagBuilder tag_builder;
101 102
	AddTagHandler h(tag_builder);
	vorbis_comments_scan(comments, h);
103
	return tag_builder.empty()
104
		? nullptr
105
		: tag_builder.CommitNew();
106
}