2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2007-02-24 04:14:00 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2007-02-24 04:14:00 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2012-08-29 19:12:26 +02:00
|
|
|
#include "SongFilter.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/LightSong.hxx"
|
2014-01-07 21:39:47 +01:00
|
|
|
#include "DetachedSong.hxx"
|
2017-02-08 08:57:22 +01:00
|
|
|
#include "tag/ParseName.hxx"
|
2014-04-24 09:43:08 +02:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2015-06-25 22:43:55 +02:00
|
|
|
#include "util/StringAPI.hxx"
|
2013-10-20 23:09:51 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2017-01-07 22:11:45 +01:00
|
|
|
#include "util/TimeParser.hxx"
|
2013-10-29 18:54:34 +01:00
|
|
|
#include "util/UriUtil.hxx"
|
2014-02-23 19:27:08 +01:00
|
|
|
#include "lib/icu/Collate.hxx"
|
2008-10-15 19:36:37 +02:00
|
|
|
|
2017-01-07 22:11:45 +01:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2012-08-07 23:59:17 +02:00
|
|
|
#include <assert.h>
|
2009-01-02 16:24:16 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2007-03-20 21:22:23 +01:00
|
|
|
#define LOCATE_TAG_FILE_KEY "file"
|
|
|
|
#define LOCATE_TAG_FILE_KEY_OLD "filename"
|
2007-02-24 01:54:54 +01:00
|
|
|
#define LOCATE_TAG_ANY_KEY "any"
|
|
|
|
|
2012-08-08 00:45:46 +02:00
|
|
|
unsigned
|
2017-05-08 14:44:49 +02:00
|
|
|
locate_parse_type(const char *str) noexcept
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2013-10-20 23:09:51 +02:00
|
|
|
if (StringEqualsCaseASCII(str, LOCATE_TAG_FILE_KEY) ||
|
|
|
|
StringEqualsCaseASCII(str, LOCATE_TAG_FILE_KEY_OLD))
|
2007-02-24 01:54:54 +01:00
|
|
|
return LOCATE_TAG_FILE_TYPE;
|
|
|
|
|
2013-10-20 23:09:51 +02:00
|
|
|
if (StringEqualsCaseASCII(str, LOCATE_TAG_ANY_KEY))
|
2007-02-24 01:54:54 +01:00
|
|
|
return LOCATE_TAG_ANY_TYPE;
|
|
|
|
|
2013-10-29 18:54:34 +01:00
|
|
|
if (strcmp(str, "base") == 0)
|
|
|
|
return LOCATE_TAG_BASE_TYPE;
|
|
|
|
|
2014-08-11 22:08:26 +02:00
|
|
|
if (strcmp(str, "modified-since") == 0)
|
|
|
|
return LOCATE_TAG_MODIFIED_SINCE;
|
|
|
|
|
2012-08-08 00:45:46 +02:00
|
|
|
return tag_name_parse_i(str);
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2015-06-25 22:43:55 +02:00
|
|
|
static AllocatedString<>
|
2013-10-29 19:39:17 +01:00
|
|
|
ImportString(const char *p, bool fold_case)
|
|
|
|
{
|
|
|
|
return fold_case
|
2014-02-23 19:27:08 +01:00
|
|
|
? IcuCaseFold(p)
|
2015-06-25 22:43:55 +02:00
|
|
|
: AllocatedString<>::Duplicate(p);
|
2013-10-29 19:39:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SongFilter::Item::Item(unsigned _tag, const char *_value, bool _fold_case)
|
|
|
|
:tag(_tag), fold_case(_fold_case),
|
|
|
|
value(ImportString(_value, _fold_case))
|
2009-01-24 15:56:30 +01:00
|
|
|
{
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2014-08-11 22:08:26 +02:00
|
|
|
SongFilter::Item::Item(unsigned _tag, time_t _time)
|
2015-06-25 22:43:55 +02:00
|
|
|
:tag(_tag), value(nullptr), time(_time)
|
2014-08-11 22:08:26 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
SongFilter::Item::StringMatch(const char *s) const noexcept
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2014-12-26 13:40:17 +01:00
|
|
|
#if !CLANG_CHECK_VERSION(3,6)
|
|
|
|
/* disabled on clang due to -Wtautological-pointer-compare */
|
2012-08-29 19:27:03 +02:00
|
|
|
assert(s != nullptr);
|
2014-12-26 13:40:17 +01:00
|
|
|
#endif
|
2007-12-28 03:56:25 +01:00
|
|
|
|
2015-06-25 23:29:07 +02:00
|
|
|
assert(tag != LOCATE_TAG_MODIFIED_SINCE);
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
if (fold_case) {
|
2015-06-25 22:43:55 +02:00
|
|
|
const auto folded = IcuCaseFold(s);
|
|
|
|
assert(!folded.IsNull());
|
|
|
|
return StringFind(folded.c_str(), value.c_str()) != nullptr;
|
2012-08-07 23:59:17 +02:00
|
|
|
} else {
|
2015-06-25 22:43:55 +02:00
|
|
|
return StringIsEqual(s, value.c_str());
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
2012-08-07 23:59:17 +02:00
|
|
|
}
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
SongFilter::Item::Match(const TagItem &item) const noexcept
|
2012-08-07 23:59:17 +02:00
|
|
|
{
|
2012-08-29 19:27:03 +02:00
|
|
|
return (tag == LOCATE_TAG_ANY_TYPE || (unsigned)item.type == tag) &&
|
|
|
|
StringMatch(item.value);
|
|
|
|
}
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
SongFilter::Item::Match(const Tag &_tag) const noexcept
|
2012-08-29 19:27:03 +02:00
|
|
|
{
|
2012-06-27 09:36:02 +02:00
|
|
|
bool visited_types[TAG_NUM_OF_ITEM_TYPES];
|
2014-01-14 22:40:07 +01:00
|
|
|
std::fill_n(visited_types, size_t(TAG_NUM_OF_ITEM_TYPES), false);
|
2009-01-24 15:27:09 +01:00
|
|
|
|
2014-07-12 17:22:39 +02:00
|
|
|
for (const auto &i : _tag) {
|
|
|
|
visited_types[i.type] = true;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2014-07-12 17:22:39 +02:00
|
|
|
if (Match(i))
|
2012-08-07 23:59:17 +02:00
|
|
|
return true;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2013-09-26 19:25:13 +02:00
|
|
|
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
|
2013-10-29 19:39:17 +01:00
|
|
|
searched string is also empty
|
2013-09-26 19:25:13 +02:00
|
|
|
then it's a match as well and we should return
|
|
|
|
true. */
|
2013-10-29 19:39:17 +01:00
|
|
|
if (value.empty())
|
2013-09-26 19:25:13 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (tag == TAG_ALBUM_ARTIST && visited_types[TAG_ARTIST]) {
|
|
|
|
/* if we're looking for "album artist", but
|
|
|
|
only "artist" exists, use that */
|
2014-07-12 17:22:39 +02:00
|
|
|
for (const auto &item : _tag)
|
2013-09-26 19:25:13 +02:00
|
|
|
if (item.type == TAG_ARTIST &&
|
|
|
|
StringMatch(item.value))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-09-29 13:18:49 +02:00
|
|
|
|
2012-08-07 23:59:17 +02:00
|
|
|
return false;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
SongFilter::Item::Match(const DetachedSong &song) const noexcept
|
2014-01-19 10:51:34 +01:00
|
|
|
{
|
|
|
|
if (tag == LOCATE_TAG_BASE_TYPE)
|
|
|
|
return uri_is_child_or_same(value.c_str(), song.GetURI());
|
|
|
|
|
2014-08-11 22:08:26 +02:00
|
|
|
if (tag == LOCATE_TAG_MODIFIED_SINCE)
|
2017-01-18 13:19:13 +01:00
|
|
|
return song.GetLastModified() >= std::chrono::system_clock::from_time_t(time);
|
2014-08-11 22:08:26 +02:00
|
|
|
|
2014-01-19 10:51:34 +01:00
|
|
|
if (tag == LOCATE_TAG_FILE_TYPE)
|
|
|
|
return StringMatch(song.GetURI());
|
|
|
|
|
|
|
|
return Match(song.GetTag());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
SongFilter::Item::Match(const LightSong &song) const noexcept
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2013-10-29 18:54:34 +01:00
|
|
|
if (tag == LOCATE_TAG_BASE_TYPE) {
|
|
|
|
const auto uri = song.GetURI();
|
|
|
|
return uri_is_child_or_same(value.c_str(), uri.c_str());
|
|
|
|
}
|
|
|
|
|
2014-08-11 22:08:26 +02:00
|
|
|
if (tag == LOCATE_TAG_MODIFIED_SINCE)
|
2017-01-18 13:19:13 +01:00
|
|
|
return song.mtime >= std::chrono::system_clock::from_time_t(time);
|
2014-08-11 22:08:26 +02:00
|
|
|
|
2013-10-29 19:54:40 +01:00
|
|
|
if (tag == LOCATE_TAG_FILE_TYPE) {
|
2013-10-17 01:01:15 +02:00
|
|
|
const auto uri = song.GetURI();
|
2013-10-29 19:54:40 +01:00
|
|
|
return StringMatch(uri.c_str());
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2014-01-19 10:51:34 +01:00
|
|
|
return Match(*song.tag);
|
2014-01-07 21:39:47 +01:00
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
2014-08-11 22:08:26 +02:00
|
|
|
gcc_pure
|
|
|
|
static time_t
|
2017-05-08 14:44:49 +02:00
|
|
|
ParseTimeStamp(const char *s) noexcept
|
2014-08-11 22:08:26 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2017-01-07 22:11:45 +01:00
|
|
|
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 &) {
|
2014-08-11 22:08:26 +02:00
|
|
|
return 0;
|
2017-01-07 22:11:45 +01:00
|
|
|
}
|
2014-08-11 22:08:26 +02:00
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
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;
|
|
|
|
|
2013-10-29 18:54:34 +01:00
|
|
|
if (tag == LOCATE_TAG_BASE_TYPE) {
|
|
|
|
if (!uri_safe_local(value))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* case folding doesn't work with "base" */
|
|
|
|
fold_case = false;
|
|
|
|
}
|
|
|
|
|
2014-08-11 22:08:26 +02:00
|
|
|
if (tag == LOCATE_TAG_MODIFIED_SINCE) {
|
|
|
|
time_t t = ParseTimeStamp(value);
|
|
|
|
if (t == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
items.push_back(Item(tag, t));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
items.push_back(Item(tag, value, fold_case));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-04-24 09:43:08 +02:00
|
|
|
SongFilter::Parse(ConstBuffer<const char *> args, bool fold_case)
|
2012-08-29 19:27:03 +02:00
|
|
|
{
|
2014-04-24 09:43:08 +02:00
|
|
|
if (args.size == 0 || args.size % 2 != 0)
|
2012-08-29 19:27:03 +02:00
|
|
|
return false;
|
|
|
|
|
2014-04-24 09:43:08 +02:00
|
|
|
for (unsigned i = 0; i < args.size; i += 2)
|
|
|
|
if (!Parse(args[i], args[i + 1], fold_case))
|
2012-08-29 19:27:03 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
SongFilter::Match(const DetachedSong &song) const noexcept
|
2007-02-24 01:54:54 +01:00
|
|
|
{
|
2012-08-29 19:27:03 +02:00
|
|
|
for (const auto &i : items)
|
|
|
|
if (!i.Match(song))
|
2009-01-24 15:27:05 +01:00
|
|
|
return false;
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2009-01-24 15:27:05 +01:00
|
|
|
return true;
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
2013-10-29 18:54:34 +01:00
|
|
|
|
2014-01-07 21:39:47 +01:00
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
SongFilter::Match(const LightSong &song) const noexcept
|
2014-01-07 21:39:47 +01:00
|
|
|
{
|
|
|
|
for (const auto &i : items)
|
|
|
|
if (!i.Match(song))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-23 09:12:51 +02:00
|
|
|
bool
|
2017-05-08 14:44:49 +02:00
|
|
|
SongFilter::HasOtherThanBase() const noexcept
|
2014-06-23 09:12:51 +02:00
|
|
|
{
|
|
|
|
for (const auto &i : items)
|
|
|
|
if (i.GetTag() != LOCATE_TAG_BASE_TYPE)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-25 23:14:40 +02:00
|
|
|
const char *
|
2017-05-08 14:44:49 +02:00
|
|
|
SongFilter::GetBase() const noexcept
|
2013-10-29 18:54:34 +01:00
|
|
|
{
|
|
|
|
for (const auto &i : items)
|
|
|
|
if (i.GetTag() == LOCATE_TAG_BASE_TYPE)
|
|
|
|
return i.GetValue();
|
|
|
|
|
2015-06-25 23:14:40 +02:00
|
|
|
return nullptr;
|
2013-10-29 18:54:34 +01:00
|
|
|
}
|