VorbisComments.cxx 2.85 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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/ASCII.hxx"
30
#include "util/DivideString.hxx"
31

32 33 34 35
#include <stddef.h>
#include <stdlib.h>

bool
36
vorbis_comments_to_replay_gain(ReplayGainInfo &rgi, char **comments)
37
{
38 39
	rgi.Clear();

40 41 42
	bool found = false;

	while (*comments) {
43
		if (ParseReplayGainVorbis(rgi, *comments))
44 45 46 47 48 49 50 51 52 53 54 55 56
			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
57
vorbis_copy_comment(const char *comment,
58
		    const char *name, TagType tag_type,
59
		    const TagHandler &handler, void *handler_ctx)
60 61 62 63
{
	const char *value;

	value = vorbis_comment_value(comment, name);
64
	if (value != nullptr) {
65
		tag_handler_invoke_tag(handler, handler_ctx, tag_type, value);
66 67 68 69 70 71 72
		return true;
	}

	return false;
}

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

84
	for (const struct tag_table *i = xiph_tags; i->name != nullptr; ++i)
85 86
		if (vorbis_copy_comment(comment, i->name, i->type,
					handler, handler_ctx))
87
			return;
88 89

	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
90
		if (vorbis_copy_comment(comment,
91
					tag_item_names[i], TagType(i),
92
					handler, handler_ctx))
93 94 95
			return;
}

96 97
void
vorbis_comments_scan(char **comments,
98
		     const TagHandler &handler, void *handler_ctx)
99 100 101 102 103 104 105
{
	while (*comments)
		vorbis_scan_comment(*comments++,
				    handler, handler_ctx);

}

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