ApeTag.cxx 2.19 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

Max Kellermann's avatar
Max Kellermann committed
20 21
#include "ApeTag.hxx"
#include "ApeLoader.hxx"
22
#include "ParseName.hxx"
23 24
#include "Table.hxx"
#include "Handler.hxx"
25
#include "util/StringView.hxx"
26
#include "util/IterableSplitString.hxx"
27

28
static constexpr struct tag_table ape_tags[] = {
29
	{ "album artist", TAG_ALBUM_ARTIST },
30
	{ "year", TAG_DATE },
Max Kellermann's avatar
Max Kellermann committed
31
	{ nullptr, TAG_NUM_OF_ITEM_TYPES }
32 33
};

34
static TagType
35 36
tag_ape_name_parse(const char *name)
{
37
	TagType type = tag_table_lookup_i(ape_tags, name);
38 39 40 41
	if (type == TAG_NUM_OF_ITEM_TYPES)
		type = tag_name_parse_i(name);

	return type;
42 43
}

44 45 46 47
/**
 * @return true if the item was recognized
 */
static bool
48
tag_ape_import_item(unsigned long flags,
49
		    const char *key, StringView value,
50
		    TagHandler &handler) noexcept
51 52 53
{
	/* we only care about utf-8 text tags */
	if ((flags & (0x3 << 1)) != 0)
54
		return false;
55

56
	if (handler.WantPair())
57
		for (const auto i : IterableSplitString(value, '\0'))
58
			handler.OnPair(key, i);
59

60
	TagType type = tag_ape_name_parse(key);
61
	if (type == TAG_NUM_OF_ITEM_TYPES)
62
		return false;
63

64
	for (const auto i : IterableSplitString(value, '\0'))
65
		handler.OnTag(type, i);
66

67
	return true;
68 69
}

70
bool
71
tag_ape_scan2(InputStream &is, TagHandler &handler)
72 73 74
{
	bool recognized = false;

75
	auto callback = [&handler, &recognized]
76 77
		(unsigned long flags, const char *key,
		 StringView value) {
78
		recognized |= tag_ape_import_item(flags, key, value, handler);
79 80 81 82 83
		return true;
	};

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