TagBuilder.cxx 5.96 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 21 22 23 24 25
 * 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"
#include "TagBuilder.hxx"
#include "TagSettings.h"
#include "TagPool.hxx"
#include "TagString.hxx"
#include "Tag.hxx"
26
#include "util/WritableBuffer.hxx"
27

28 29
#include <array>

30 31
#include <assert.h>
#include <string.h>
32
#include <stdlib.h>
33

34
TagBuilder::TagBuilder(const Tag &other)
35
	:duration(other.duration), has_playlist(other.has_playlist)
36 37 38 39 40 41 42 43 44 45
{
	items.reserve(other.num_items);

	tag_pool_lock.lock();
	for (unsigned i = 0, n = other.num_items; i != n; ++i)
		items.push_back(tag_pool_dup_item(other.items[i]));
	tag_pool_lock.unlock();
}

TagBuilder::TagBuilder(Tag &&other)
46
	:duration(other.duration), has_playlist(other.has_playlist)
47 48 49 50
{
	/* move all TagItem pointers from the Tag object; we don't
	   need to contact the tag pool, because all we do is move
	   references */
51
	items.reserve(other.num_items);
52 53 54 55 56 57 58 59
	std::copy_n(other.items, other.num_items, std::back_inserter(items));

	/* discard the pointers from the Tag object */
	other.num_items = 0;
	delete[] other.items;
	other.items = nullptr;
}

60 61 62 63
TagBuilder &
TagBuilder::operator=(const TagBuilder &other)
{
	/* copy all attributes */
64
	duration = other.duration;
65 66 67 68 69 70 71 72 73 74 75 76
	has_playlist = other.has_playlist;
	items = other.items;

	/* increment the tag pool refcounters */
	tag_pool_lock.lock();
	for (auto i : items)
		tag_pool_dup_item(i);
	tag_pool_lock.unlock();

	return *this;
}

77 78 79
TagBuilder &
TagBuilder::operator=(TagBuilder &&other)
{
80
	duration = other.duration;
81 82 83 84 85 86 87 88 89
	has_playlist = other.has_playlist;
	items = std::move(other.items);

	return *this;
}

TagBuilder &
TagBuilder::operator=(Tag &&other)
{
90
	duration = other.duration;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	has_playlist = other.has_playlist;

	/* move all TagItem pointers from the Tag object; we don't
	   need to contact the tag pool, because all we do is move
	   references */
	items.clear();
	items.reserve(other.num_items);
	std::copy_n(other.items, other.num_items, std::back_inserter(items));

	/* discard the pointers from the Tag object */
	other.num_items = 0;
	delete[] other.items;
	other.items = nullptr;

	return *this;
}

108 109 110
void
TagBuilder::Clear()
{
111
	duration = SignedSongTime::Negative();
112
	has_playlist = false;
113
	RemoveAll();
114 115
}

116 117
void
TagBuilder::Commit(Tag &tag)
118
{
119 120
	tag.Clear();

121
	tag.duration = duration;
122
	tag.has_playlist = has_playlist;
123 124 125 126 127 128

	/* move all TagItem pointers to the new Tag object without
	   touching the TagPool reference counters; the
	   vector::clear() call is important to detach them from this
	   object */
	const unsigned n_items = items.size();
129
	tag.num_items = n_items;
130
	tag.items = new TagItem *[n_items];
131
	std::copy_n(items.begin(), n_items, tag.items);
132 133 134 135 136
	items.clear();

	/* now ensure that this object is fresh (will not delete any
	   items because we've already moved them out) */
	Clear();
137
}
138

139 140 141 142 143 144 145 146
Tag
TagBuilder::Commit()
{
	Tag tag;
	Commit(tag);
	return tag;
}

147
Tag *
148
TagBuilder::CommitNew()
149 150 151
{
	Tag *tag = new Tag();
	Commit(*tag);
152 153 154
	return tag;
}

155 156 157 158 159 160 161 162 163 164
bool
TagBuilder::HasType(TagType type) const
{
	for (auto i : items)
		if (i->type == type)
			return true;

	return false;
}

165 166 167
void
TagBuilder::Complement(const Tag &other)
{
168 169
	if (duration.IsNegative())
		duration = other.duration;
170 171 172

	has_playlist |= other.has_playlist;

173 174 175 176 177 178 179
	/* build a table of tag types that were already present in
	   this object, which will not be copied from #other */
	std::array<bool, TAG_NUM_OF_ITEM_TYPES> present;
	present.fill(false);
	for (const TagItem *i : items)
		present[i->type] = true;

180 181 182 183 184
	items.reserve(items.size() + other.num_items);

	tag_pool_lock.lock();
	for (unsigned i = 0, n = other.num_items; i != n; ++i) {
		TagItem *item = other.items[i];
185
		if (!present[item->type])
186 187 188 189 190
			items.push_back(tag_pool_dup_item(item));
	}
	tag_pool_lock.unlock();
}

191
inline void
192
TagBuilder::AddItemInternal(TagType type, const char *value, size_t length)
193
{
194 195
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
196
	assert(value != nullptr);
197
#endif
198 199
	assert(length > 0);

200 201 202 203
	auto f = FixTagString(value, length);
	if (!f.IsNull()) {
		value = f.data;
		length = f.size;
204 205 206 207 208 209
	}

	tag_pool_lock.lock();
	auto i = tag_pool_get_item(type, value, length);
	tag_pool_lock.unlock();

210
	free(f.data);
211 212 213 214 215

	items.push_back(i);
}

void
216
TagBuilder::AddItem(TagType type, const char *value, size_t length)
217
{
218 219
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
220
	assert(value != nullptr);
221
#endif
222 223 224 225 226 227 228 229

	if (length == 0 || ignore_tag_items[type])
		return;

	AddItemInternal(type, value, length);
}

void
230
TagBuilder::AddItem(TagType type, const char *value)
231
{
232 233
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
234
	assert(value != nullptr);
235
#endif
236 237 238

	AddItem(type, value, strlen(value));
}
239

240 241 242 243 244 245 246 247 248 249
void
TagBuilder::AddEmptyItem(TagType type)
{
	tag_pool_lock.lock();
	auto i = tag_pool_get_item(type, "", 0);
	tag_pool_lock.unlock();

	items.push_back(i);
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
void
TagBuilder::RemoveAll()
{
	tag_pool_lock.lock();
	for (auto i : items)
		tag_pool_put_item(i);
	tag_pool_lock.unlock();

	items.clear();
}

void
TagBuilder::RemoveType(TagType type)
{
	const auto begin = items.begin(), end = items.end();

	items.erase(std::remove_if(begin, end,
				   [type](TagItem *item) {
					   if (item->type != type)
						   return false;
					   tag_pool_put_item(item);
					   return true;
				   }),
		    end);
}