ApeReplayGain.cxx 1.64 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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.
 */

Max Kellermann's avatar
Max Kellermann committed
20 21
#include "ApeReplayGain.hxx"
#include "ApeLoader.hxx"
22
#include "ReplayGain.hxx"
23
#include "util/StringView.hxx"
24 25 26 27 28

#include <string.h>

static bool
replay_gain_ape_callback(unsigned long flags, const char *key,
29
			 StringView _value,
30
			 ReplayGainInfo &info)
31 32 33
{
	/* we only care about utf-8 text tags */
	if ((flags & (0x3 << 1)) != 0)
Max Kellermann's avatar
Max Kellermann committed
34
		return false;
35 36

	char value[16];
37
	if (_value.size >= sizeof(value))
Max Kellermann's avatar
Max Kellermann committed
38 39
		return false;

40 41
	memcpy(value, _value.data, _value.size);
	value[_value.size] = 0;
42

43
	return ParseReplayGainTag(info, key, value);
44 45
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
bool
replay_gain_ape_read(InputStream &is, ReplayGainInfo &info)
{
	bool found = false;

	auto callback = [&info, &found]
		(unsigned long flags, const char *key,
		 StringView value) {
		found |= replay_gain_ape_callback(flags, key,
						  value,
						  info);
		return true;
	};

	return tag_ape_scan(is, callback) && found;
}