VorbisComments.cxx 2.78 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */

#include "config.h"
21
#include "VorbisComments.hxx"
22
#include "XiphTags.hxx"
23
#include "tag/TagTable.hxx"
24
#include "tag/TagHandler.hxx"
25
#include "tag/TagBuilder.hxx"
26
#include "tag/VorbisComment.hxx"
27
#include "tag/ReplayGain.hxx"
28
#include "ReplayGainInfo.hxx"
29
#include "util/DivideString.hxx"
30

31
bool
32
vorbis_comments_to_replay_gain(ReplayGainInfo &rgi, char **comments)
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
		    const TagHandler &handler, void *handler_ctx)
56 57 58 59
{
	const char *value;

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

	return false;
}

static void
69
vorbis_scan_comment(const char *comment,
70
		    const TagHandler &handler, void *handler_ctx)
71
{
72
	if (handler.pair != nullptr) {
73
		const DivideString split(comment, '=');
74
		if (split.IsDefined() && !split.IsEmpty())
75
			tag_handler_invoke_pair(handler, handler_ctx,
76 77
						split.GetFirst(),
						split.GetSecond());
78 79
	}

80
	for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
81 82
		if (vorbis_copy_comment(comment, i->name, i->type,
					handler, handler_ctx))
83
			return;
84 85

	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
86
		if (vorbis_copy_comment(comment,
87
					tag_item_names[i], TagType(i),
88
					handler, handler_ctx))
89 90 91
			return;
}

92 93
void
vorbis_comments_scan(char **comments,
94
		     const TagHandler &handler, void *handler_ctx)
95 96 97 98 99 100 101
{
	while (*comments)
		vorbis_scan_comment(*comments++,
				    handler, handler_ctx);

}

Max Kellermann's avatar
Max Kellermann committed
102
Tag *
103 104
vorbis_comments_to_tag(char **comments)
{
105
	TagBuilder tag_builder;
106
	vorbis_comments_scan(comments, add_tag_handler, &tag_builder);
107 108
	return tag_builder.IsEmpty()
		? nullptr
109
		: tag_builder.CommitNew();
110
}