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"
|
2017-01-18 13:03:05 +01:00
|
|
|
#include "util/ChronoUtil.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"
|
2018-01-19 23:35:05 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
|
|
|
#include "util/StringView.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"
|
2017-09-20 23:11:58 +02:00
|
|
|
#include "lib/icu/CaseFold.hxx"
|
2008-10-15 19:36:37 +02:00
|
|
|
|
2017-12-19 10:56:23 +01:00
|
|
|
#include <exception>
|
2017-01-07 22:11:45 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-29 19:39:17 +01:00
|
|
|
SongFilter::Item::Item(unsigned _tag, const char *_value, bool _fold_case)
|
2017-09-20 23:26:38 +02:00
|
|
|
:tag(_tag),
|
2018-01-19 23:35:36 +01:00
|
|
|
value(_value),
|
2017-09-20 23:26:38 +02:00
|
|
|
fold_case(_fold_case ? IcuCompare(value.c_str()) : IcuCompare())
|
2009-01-24 15:56:30 +01:00
|
|
|
{
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2017-01-18 13:03:05 +01:00
|
|
|
SongFilter::Item::Item(unsigned _tag,
|
|
|
|
std::chrono::system_clock::time_point _time)
|
2018-02-09 13:19:26 +01:00
|
|
|
:tag(_tag), time(_time)
|
2014-08-11 22:08:26 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
2018-07-21 11:20:50 +02:00
|
|
|
SongFilter::Item::StringMatchNN(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) {
|
2017-09-20 23:26:38 +02:00
|
|
|
return fold_case.IsIn(s);
|
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
|
2018-07-21 11:20:50 +02:00
|
|
|
SongFilter::Item::MatchNN(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) &&
|
2018-07-21 11:20:50 +02:00
|
|
|
StringMatchNN(item.value);
|
2012-08-29 19:27:03 +02:00
|
|
|
}
|
2007-02-24 01:54:54 +01:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
bool
|
2018-07-21 11:20:50 +02:00
|
|
|
SongFilter::Item::MatchNN(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
|
|
|
|
2018-07-21 11:20:50 +02:00
|
|
|
if (MatchNN(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 &&
|
2018-07-21 11:20:50 +02:00
|
|
|
StringMatchNN(item.value))
|
2013-09-26 19:25:13 +02:00
|
|
|
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
|
2018-07-21 11:20:50 +02:00
|
|
|
SongFilter::Item::MatchNN(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:03:05 +01:00
|
|
|
return song.GetLastModified() >= time;
|
2014-08-11 22:08:26 +02:00
|
|
|
|
2014-01-19 10:51:34 +01:00
|
|
|
if (tag == LOCATE_TAG_FILE_TYPE)
|
2018-07-21 11:20:50 +02:00
|
|
|
return StringMatchNN(song.GetURI());
|
2014-01-19 10:51:34 +01:00
|
|
|
|
2018-07-21 11:20:50 +02:00
|
|
|
return MatchNN(song.GetTag());
|
2014-01-19 10:51:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-07-21 11:20:50 +02:00
|
|
|
SongFilter::Item::MatchNN(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:03:05 +01:00
|
|
|
return song.mtime >= 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();
|
2018-07-21 11:20:50 +02:00
|
|
|
return StringMatchNN(uri.c_str());
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-21 11:20:50 +02:00
|
|
|
return MatchNN(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)
|
|
|
|
{
|
2018-07-24 19:33:15 +02:00
|
|
|
items.emplace_back(tag, value, fold_case);
|
2012-08-29 19:27:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SongFilter::~SongFilter()
|
|
|
|
{
|
|
|
|
/* this destructor exists here just so it won't get inlined */
|
|
|
|
}
|
|
|
|
|
2017-01-18 13:03:05 +01:00
|
|
|
static std::chrono::system_clock::time_point
|
|
|
|
ParseTimeStamp(const char *s)
|
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 */
|
2017-01-18 13:03:05 +01:00
|
|
|
return std::chrono::system_clock::from_time_t((time_t)value);
|
2014-08-11 22:08:26 +02:00
|
|
|
|
2018-07-21 07:20:59 +02:00
|
|
|
/* try ISO 8601 */
|
|
|
|
return ParseTimePoint(s, "%FT%TZ");
|
2014-08-11 22:08:26 +02:00
|
|
|
}
|
|
|
|
|
2018-07-21 07:20:59 +02:00
|
|
|
void
|
2012-08-29 19:27:03 +02:00
|
|
|
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)
|
2018-07-21 07:20:59 +02:00
|
|
|
throw std::runtime_error("Unknown filter type");
|
2012-08-29 19:27:03 +02:00
|
|
|
|
2013-10-29 18:54:34 +01:00
|
|
|
if (tag == LOCATE_TAG_BASE_TYPE) {
|
|
|
|
if (!uri_safe_local(value))
|
2018-07-21 07:20:59 +02:00
|
|
|
throw std::runtime_error("Bad URI");
|
2013-10-29 18:54:34 +01:00
|
|
|
|
|
|
|
/* case folding doesn't work with "base" */
|
|
|
|
fold_case = false;
|
|
|
|
}
|
|
|
|
|
2018-07-21 07:20:59 +02:00
|
|
|
if (tag == LOCATE_TAG_MODIFIED_SINCE)
|
2018-07-24 19:33:15 +02:00
|
|
|
items.emplace_back(tag, ParseTimeStamp(value));
|
2018-07-21 07:20:59 +02:00
|
|
|
else
|
2018-07-24 19:33:15 +02:00
|
|
|
items.emplace_back(tag, value, fold_case);
|
2012-08-29 19:27:03 +02:00
|
|
|
}
|
|
|
|
|
2018-07-21 07:20:59 +02:00
|
|
|
void
|
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
|
|
|
{
|
2018-07-21 07:36:42 +02:00
|
|
|
if (args.empty())
|
2018-07-21 07:20:59 +02:00
|
|
|
throw std::runtime_error("Incorrect number of filter arguments");
|
2012-08-29 19:27:03 +02:00
|
|
|
|
2018-07-21 07:36:42 +02:00
|
|
|
do {
|
|
|
|
if (args.size < 2)
|
|
|
|
throw std::runtime_error("Incorrect number of filter arguments");
|
|
|
|
|
|
|
|
const char *tag = args.shift();
|
|
|
|
const char *value = args.shift();
|
|
|
|
Parse(tag, value, fold_case);
|
|
|
|
} while (!args.empty());
|
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
|
|
|
}
|
2018-01-19 23:35:05 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|