You need to sign in or sign up before continuing.
SongFilter.cxx 6.73 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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
#include "util/ASCII.hxx"
28
#include "util/UriUtil.hxx"
29
#include "lib/icu/Collate.hxx"
30

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

34 35
#define LOCATE_TAG_FILE_KEY     "file"
#define LOCATE_TAG_FILE_KEY_OLD "filename"
36 37
#define LOCATE_TAG_ANY_KEY      "any"

38
unsigned
Max Kellermann's avatar
Max Kellermann committed
39
locate_parse_type(const char *str)
40
{
41 42
	if (StringEqualsCaseASCII(str, LOCATE_TAG_FILE_KEY) ||
	    StringEqualsCaseASCII(str, LOCATE_TAG_FILE_KEY_OLD))
43 44
		return LOCATE_TAG_FILE_TYPE;

45
	if (StringEqualsCaseASCII(str, LOCATE_TAG_ANY_KEY))
46 47
		return LOCATE_TAG_ANY_TYPE;

48 49 50
	if (strcmp(str, "base") == 0)
		return LOCATE_TAG_BASE_TYPE;

51 52 53
	if (strcmp(str, "modified-since") == 0)
		return LOCATE_TAG_MODIFIED_SINCE;

54
	return tag_name_parse_i(str);
55 56
}

57
gcc_pure
58
static AllocatedString<>
59 60 61
ImportString(const char *p, bool fold_case)
{
	return fold_case
62
		? IcuCaseFold(p)
63
		: AllocatedString<>::Duplicate(p);
64 65 66 67 68
}

SongFilter::Item::Item(unsigned _tag, const char *_value, bool _fold_case)
	:tag(_tag), fold_case(_fold_case),
	 value(ImportString(_value, _fold_case))
69
{
70 71
}

72
SongFilter::Item::Item(unsigned _tag, time_t _time)
73
	:tag(_tag), value(nullptr), time(_time)
74 75 76
{
}

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

85 86
	assert(tag != LOCATE_TAG_MODIFIED_SINCE);

87
	if (fold_case) {
88 89 90
		const auto folded = IcuCaseFold(s);
		assert(!folded.IsNull());
		return StringFind(folded.c_str(), value.c_str()) != nullptr;
91
	} else {
92
		return StringIsEqual(s, value.c_str());
93
	}
94
}
95

96
bool
Max Kellermann's avatar
Max Kellermann committed
97
SongFilter::Item::Match(const TagItem &item) const
98
{
99 100 101
	return (tag == LOCATE_TAG_ANY_TYPE || (unsigned)item.type == tag) &&
		StringMatch(item.value);
}
102

103
bool
Max Kellermann's avatar
Max Kellermann committed
104
SongFilter::Item::Match(const Tag &_tag) const
105
{
106
	bool visited_types[TAG_NUM_OF_ITEM_TYPES];
107
	std::fill_n(visited_types, size_t(TAG_NUM_OF_ITEM_TYPES), false);
108

109 110
	for (const auto &i : _tag) {
		visited_types[i.type] = true;
111

112
		if (Match(i))
113
			return true;
114 115
	}

116 117 118 119
	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
120
		   searched string is also empty
121 122
		   then it's a match as well and we should return
		   true. */
123
		if (value.empty())
124 125 126 127 128
			return true;

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

136
	return false;
137 138
}

139
bool
140 141 142 143 144
SongFilter::Item::Match(const DetachedSong &song) const
{
	if (tag == LOCATE_TAG_BASE_TYPE)
		return uri_is_child_or_same(value.c_str(), song.GetURI());

145 146 147
	if (tag == LOCATE_TAG_MODIFIED_SINCE)
		return song.GetLastModified() >= time;

148 149 150 151 152 153 154 155
	if (tag == LOCATE_TAG_FILE_TYPE)
		return StringMatch(song.GetURI());

	return Match(song.GetTag());
}

bool
SongFilter::Item::Match(const LightSong &song) const
156
{
157 158 159 160 161
	if (tag == LOCATE_TAG_BASE_TYPE) {
		const auto uri = song.GetURI();
		return uri_is_child_or_same(value.c_str(), uri.c_str());
	}

162 163 164
	if (tag == LOCATE_TAG_MODIFIED_SINCE)
		return song.mtime >= time;

165
	if (tag == LOCATE_TAG_FILE_TYPE) {
166
		const auto uri = song.GetURI();
167
		return StringMatch(uri.c_str());
168 169
	}

170
	return Match(*song.tag);
171 172
}

173 174 175 176 177 178 179 180 181 182
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 */
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
#if !defined(__GLIBC__) && !defined(WIN32)

/**
 * Determine the time zone offset in a portable way.
 */
gcc_const
static time_t
GetTimeZoneOffset()
{
	time_t t = 1234567890;
	struct tm tm;
	tm.tm_isdst = 0;
	gmtime_r(&t, &tm);
	return t - mktime(&tm);
}

#endif

gcc_pure
static time_t
ParseTimeStamp(const char *s)
{
	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;

#ifdef WIN32
	/* TODO: emulate strptime()? */
	return 0;
#else
	/* try ISO 8601 */

	struct tm tm;
	const char *end = strptime(s, "%FT%TZ", &tm);
	if (end == nullptr || *end != 0)
		return 0;

#ifdef __GLIBC__
	/* timegm() is a GNU extension */
	return timegm(&tm);
#else
	tm.tm_isdst = 0;
	return mktime(&tm) + GetTimeZoneOffset();
#endif /* !__GLIBC__ */

#endif /* !WIN32 */
}

235 236 237 238 239 240 241
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;

242 243 244 245 246 247 248 249
	if (tag == LOCATE_TAG_BASE_TYPE) {
		if (!uri_safe_local(value))
			return false;

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

250 251 252 253 254 255 256 257 258
	if (tag == LOCATE_TAG_MODIFIED_SINCE) {
		time_t t = ParseTimeStamp(value);
		if (t == 0)
			return false;

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

259 260 261 262 263
	items.push_back(Item(tag, value, fold_case));
	return true;
}

bool
264
SongFilter::Parse(ConstBuffer<const char *> args, bool fold_case)
265
{
266
	if (args.size == 0 || args.size % 2 != 0)
267 268
		return false;

269 270
	for (unsigned i = 0; i < args.size; i += 2)
		if (!Parse(args[i], args[i + 1], fold_case))
271 272 273
			return false;

	return true;
274 275
}

276
bool
277
SongFilter::Match(const DetachedSong &song) const
278
{
279 280
	for (const auto &i : items)
		if (!i.Match(song))
281
			return false;
282

283
	return true;
284
}
285

286
bool
287
SongFilter::Match(const LightSong &song) const
288 289 290 291 292 293 294 295
{
	for (const auto &i : items)
		if (!i.Match(song))
			return false;

	return true;
}

296 297 298 299 300 301 302 303 304 305
bool
SongFilter::HasOtherThanBase() const
{
	for (const auto &i : items)
		if (i.GetTag() != LOCATE_TAG_BASE_TYPE)
			return true;

	return false;
}

306
const char *
307 308 309 310 311 312
SongFilter::GetBase() const
{
	for (const auto &i : items)
		if (i.GetTag() == LOCATE_TAG_BASE_TYPE)
			return i.GetValue();

313
	return nullptr;
314
}