MixRamp.cxx 2.02 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 "MixRamp.hxx"
21
#include "VorbisComment.hxx"
22 23
#include "MixRampInfo.hxx"
#include "util/ASCII.hxx"
24
#include "util/StringView.hxx"
25 26 27 28 29

#include <assert.h>

template<typename T>
static bool
30
ParseMixRampTagTemplate(MixRampInfo &info, const T t) noexcept
31
{
32 33 34
	const auto start = t["mixramp_start"];
	if (!start.IsNull()) {
		info.SetStart(std::string(start.data, start.size));
35
		return true;
36 37 38 39 40
	}

	const auto end = t["mixramp_end"];
	if (!start.IsNull()) {
		info.SetEnd(std::string(end.data, end.size));
41
		return true;
42
	}
43

44
	return false;
45 46 47
}

bool
48 49
ParseMixRampTag(MixRampInfo &info,
		const char *name, const char *value) noexcept
50 51 52 53 54 55 56 57 58
{
	assert(name != nullptr);
	assert(value != nullptr);

	struct NameValue {
		const char *name;
		const char *value;

		gcc_pure
59
		StringView operator[](const char *n) const noexcept {
60 61 62 63 64 65 66 67
			return StringEqualsCaseASCII(name, n)
				? value
				: nullptr;
		}
	};

	return ParseMixRampTagTemplate(info, NameValue{name, value});
}
68 69

bool
70
ParseMixRampVorbis(MixRampInfo &info, StringView entry) noexcept
71 72
{
	struct VorbisCommentEntry {
73
		StringView entry;
74 75

		gcc_pure
76 77
		StringView operator[](StringView n) const noexcept {
			return GetVorbisCommentValue(entry, n);
78 79 80 81 82
		}
	};

	return ParseMixRampTagTemplate(info, VorbisCommentEntry{entry});
}