SongFilter.cxx 6.75 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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
 */

20
#include "config.h"
21
#include "SongFilter.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "db/LightSong.hxx"
23
#include "DetachedSong.hxx"
24
#include "tag/Tag.hxx"
25
#include "util/ConstBuffer.hxx"
26
#include "util/StringAPI.hxx"
27 28
#include "util/StringCompare.hxx"
#include "util/StringView.hxx"
29
#include "util/ASCII.hxx"
30
#include "util/TimeParser.hxx"
31
#include "util/UriUtil.hxx"
32
#include "lib/icu/CaseFold.hxx"
33

34 35
#include <stdexcept>

36
#include <assert.h>
37 38
#include <stdlib.h>

39 40
#define LOCATE_TAG_FILE_KEY     "file"
#define LOCATE_TAG_FILE_KEY_OLD "filename"
41 42
#define LOCATE_TAG_ANY_KEY      "any"

43
unsigned
44
locate_parse_type(const char *str) noexcept
45
{
46 47
	if (StringEqualsCaseASCII(str, LOCATE_TAG_FILE_KEY) ||
	    StringEqualsCaseASCII(str, LOCATE_TAG_FILE_KEY_OLD))
48 49
		return LOCATE_TAG_FILE_TYPE;

50
	if (StringEqualsCaseASCII(str, LOCATE_TAG_ANY_KEY))
51 52
		return LOCATE_TAG_ANY_TYPE;

53 54 55
	if (strcmp(str, "base") == 0)
		return LOCATE_TAG_BASE_TYPE;

56 57 58
	if (strcmp(str, "modified-since") == 0)
		return LOCATE_TAG_MODIFIED_SINCE;

59
	return tag_name_parse_i(str);
60 61
}

62
SongFilter::Item::Item(unsigned _tag, const char *_value, bool _fold_case)
63
	:tag(_tag),
64
	 value(_value),
65
	 fold_case(_fold_case ? IcuCompare(value.c_str()) : IcuCompare())
66
{
67 68
}

69
SongFilter::Item::Item(unsigned _tag, time_t _time)
70
	:tag(_tag), time(_time)
71 72 73
{
}

74
bool
75
SongFilter::Item::StringMatch(const char *s) const noexcept
76
{
77 78
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
79
	assert(s != nullptr);
80
#endif
Eric Wong's avatar
Eric Wong committed
81

82 83
	assert(tag != LOCATE_TAG_MODIFIED_SINCE);

84
	if (fold_case) {
85
		return fold_case.IsIn(s);
86
	} else {
87
		return StringIsEqual(s, value.c_str());
88
	}
89
}
90

91
bool
92
SongFilter::Item::Match(const TagItem &item) const noexcept
93
{
94 95 96
	return (tag == LOCATE_TAG_ANY_TYPE || (unsigned)item.type == tag) &&
		StringMatch(item.value);
}
97

98
bool
99
SongFilter::Item::Match(const Tag &_tag) const noexcept
100
{
101
	bool visited_types[TAG_NUM_OF_ITEM_TYPES];
102
	std::fill_n(visited_types, size_t(TAG_NUM_OF_ITEM_TYPES), false);
103

104 105
	for (const auto &i : _tag) {
		visited_types[i.type] = true;
106

107
		if (Match(i))
108
			return true;
109 110
	}

111 112 113 114
	if (tag < TAG_NUM_OF_ITEM_TYPES && !visited_types[tag]) {
		/* If the search critieron was not visited during the
		   sweep through the song's tag, it means this field
		   is absent from the tag or empty. Thus, if the
115
		   searched string is also empty
116 117
		   then it's a match as well and we should return
		   true. */
118
		if (value.empty())
119 120 121 122 123
			return true;

		if (tag == TAG_ALBUM_ARTIST && visited_types[TAG_ARTIST]) {
			/* if we're looking for "album artist", but
			   only "artist" exists, use that */
124
			for (const auto &item : _tag)
125 126 127 128 129
				if (item.type == TAG_ARTIST &&
				    StringMatch(item.value))
					return true;
		}
	}
130

131
	return false;
132 133
}

134
bool
135
SongFilter::Item::Match(const DetachedSong &song) const noexcept
136 137 138 139
{
	if (tag == LOCATE_TAG_BASE_TYPE)
		return uri_is_child_or_same(value.c_str(), song.GetURI());

140 141 142
	if (tag == LOCATE_TAG_MODIFIED_SINCE)
		return song.GetLastModified() >= time;

143 144 145 146 147 148 149
	if (tag == LOCATE_TAG_FILE_TYPE)
		return StringMatch(song.GetURI());

	return Match(song.GetTag());
}

bool
150
SongFilter::Item::Match(const LightSong &song) const noexcept
151
{
152 153 154 155 156
	if (tag == LOCATE_TAG_BASE_TYPE) {
		const auto uri = song.GetURI();
		return uri_is_child_or_same(value.c_str(), uri.c_str());
	}

157 158 159
	if (tag == LOCATE_TAG_MODIFIED_SINCE)
		return song.mtime >= time;

160
	if (tag == LOCATE_TAG_FILE_TYPE) {
161
		const auto uri = song.GetURI();
162
		return StringMatch(uri.c_str());
163 164
	}

165
	return Match(*song.tag);
166 167
}

168 169 170 171 172 173 174 175 176 177
SongFilter::SongFilter(unsigned tag, const char *value, bool fold_case)
{
	items.push_back(Item(tag, value, fold_case));
}

SongFilter::~SongFilter()
{
	/* this destructor exists here just so it won't get inlined */
}

178 179
gcc_pure
static time_t
180
ParseTimeStamp(const char *s) noexcept
181 182 183 184 185 186 187 188 189
{
	assert(s != nullptr);

	char *endptr;
	unsigned long long value = strtoull(s, &endptr, 10);
	if (*endptr == 0 && endptr > s)
		/* it's an integral UNIX time stamp */
		return (time_t)value;

190 191 192 193 194
	try {
		/* try ISO 8601 */
		const auto t = ParseTimePoint(s, "%FT%TZ");
		return std::chrono::system_clock::to_time_t(t);
	} catch (const std::runtime_error &) {
195
		return 0;
196
	}
197 198
}

199 200 201 202 203 204 205
bool
SongFilter::Parse(const char *tag_string, const char *value, bool fold_case)
{
	unsigned tag = locate_parse_type(tag_string);
	if (tag == TAG_NUM_OF_ITEM_TYPES)
		return false;

206 207 208 209 210 211 212 213
	if (tag == LOCATE_TAG_BASE_TYPE) {
		if (!uri_safe_local(value))
			return false;

		/* case folding doesn't work with "base" */
		fold_case = false;
	}

214 215 216 217 218 219 220 221 222
	if (tag == LOCATE_TAG_MODIFIED_SINCE) {
		time_t t = ParseTimeStamp(value);
		if (t == 0)
			return false;

		items.push_back(Item(tag, t));
		return true;
	}

223 224 225 226 227
	items.push_back(Item(tag, value, fold_case));
	return true;
}

bool
228
SongFilter::Parse(ConstBuffer<const char *> args, bool fold_case)
229
{
230
	if (args.size == 0 || args.size % 2 != 0)
231 232
		return false;

233 234
	for (unsigned i = 0; i < args.size; i += 2)
		if (!Parse(args[i], args[i + 1], fold_case))
235 236 237
			return false;

	return true;
238 239
}

240
bool
241
SongFilter::Match(const DetachedSong &song) const noexcept
242
{
243 244
	for (const auto &i : items)
		if (!i.Match(song))
245
			return false;
246

247
	return true;
248
}
249

250
bool
251
SongFilter::Match(const LightSong &song) const noexcept
252 253 254 255 256 257 258 259
{
	for (const auto &i : items)
		if (!i.Match(song))
			return false;

	return true;
}

260
bool
261
SongFilter::HasOtherThanBase() const noexcept
262 263 264 265 266 267 268 269
{
	for (const auto &i : items)
		if (i.GetTag() != LOCATE_TAG_BASE_TYPE)
			return true;

	return false;
}

270
const char *
271
SongFilter::GetBase() const noexcept
272 273 274 275 276
{
	for (const auto &i : items)
		if (i.GetTag() == LOCATE_TAG_BASE_TYPE)
			return i.GetValue();

277
	return nullptr;
278
}
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

SongFilter
SongFilter::WithoutBasePrefix(const char *_prefix) const noexcept
{
	const StringView prefix(_prefix);
	SongFilter result;

	for (const auto &i : items) {
		if (i.GetTag() == LOCATE_TAG_BASE_TYPE) {
			const char *s = StringAfterPrefix(i.GetValue(), prefix);
			if (s != nullptr) {
				if (*s == 0)
					continue;

				if (*s == '/') {
					++s;

					if (*s != 0)
						result.items.emplace_back(LOCATE_TAG_BASE_TYPE, s);

					continue;
				}
			}
		}

		result.items.emplace_back(i);
	}

	return result;
}