Set.cxx 2.85 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "Set.hxx"
21
#include "Builder.hxx"
22
#include "Mask.hxx"
23
#include "Settings.hxx"
24 25 26 27 28 29 30 31 32 33 34

#include <assert.h>

/**
 * Copy all tag items of the specified type.
 */
static bool
CopyTagItem(TagBuilder &dest, TagType dest_type,
	    const Tag &src, TagType src_type)
{
	bool found = false;
35 36 37 38

	for (const auto &item : src) {
		if (item.type == src_type) {
			dest.AddItem(dest_type, item.value);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
			found = true;
		}
	}

	return found;
}

/**
 * Copy all tag items of the specified type.  Fall back to "Artist" if
 * there is no "AlbumArtist".
 */
static void
CopyTagItem(TagBuilder &dest, const Tag &src, TagType type)
{
	if (!CopyTagItem(dest, type, src, type) &&
	    type == TAG_ALBUM_ARTIST)
		CopyTagItem(dest, type, src, TAG_ARTIST);
}

/**
 * Copy all tag items of the types in the mask.
 */
static void
62
CopyTagMask(TagBuilder &dest, const Tag &src, TagMask mask)
63 64
{
	for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i)
65
		if (mask.Test(TagType(i)))
66 67 68 69 70
			CopyTagItem(dest, src, TagType(i));
}

void
TagSet::InsertUnique(const Tag &src, TagType type, const char *value,
Max Kellermann's avatar
Max Kellermann committed
71
		     TagMask group_mask) noexcept
72 73 74 75 76 77 78 79 80 81 82 83 84
{
	TagBuilder builder;
	if (value == nullptr)
		builder.AddEmptyItem(type);
	else
		builder.AddItem(type, value);
	CopyTagMask(builder, src, group_mask);
	emplace(builder.Commit());
}

bool
TagSet::CheckUnique(TagType dest_type,
		    const Tag &tag, TagType src_type,
Max Kellermann's avatar
Max Kellermann committed
85
		    TagMask group_mask) noexcept
86 87
{
	bool found = false;
88 89 90 91

	for (const auto &item : tag) {
		if (item.type == src_type) {
			InsertUnique(tag, dest_type, item.value, group_mask);
92 93 94 95 96 97 98 99 100
			found = true;
		}
	}

	return found;
}

void
TagSet::InsertUnique(const Tag &tag,
Max Kellermann's avatar
Max Kellermann committed
101
		     TagType type, TagMask group_mask) noexcept
102 103 104 105
{
	static_assert(sizeof(group_mask) * 8 >= TAG_NUM_OF_ITEM_TYPES,
		      "Mask is too small");

106
	assert(!group_mask.Test(type));
107 108 109

	if (!CheckUnique(type, tag, type, group_mask) &&
	    (type != TAG_ALBUM_ARTIST ||
110
	     !IsTagEnabled(TAG_ALBUM_ARTIST) ||
111 112 113 114
	     /* fall back to "Artist" if no "AlbumArtist" was found */
	     !CheckUnique(type, tag, TAG_ARTIST, group_mask)))
		InsertUnique(tag, type, nullptr, group_mask);
}