2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 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"
|
2018-08-02 13:45:43 +02:00
|
|
|
#include "Filter.hxx"
|
2018-07-25 10:19:49 +02:00
|
|
|
#include "NotSongFilter.hxx"
|
2018-08-02 13:54:36 +02:00
|
|
|
#include "UriSongFilter.hxx"
|
|
|
|
#include "BaseSongFilter.hxx"
|
|
|
|
#include "TagSongFilter.hxx"
|
|
|
|
#include "ModifiedSinceSongFilter.hxx"
|
2018-08-02 13:36:14 +02:00
|
|
|
#include "AudioFormatSongFilter.hxx"
|
2020-01-18 20:07:09 +01:00
|
|
|
#include "pcm/AudioParser.hxx"
|
2017-02-08 08:57:22 +01:00
|
|
|
#include "tag/ParseName.hxx"
|
2019-05-08 15:47:58 +02:00
|
|
|
#include "time/ISO8601.hxx"
|
2018-07-24 19:21:51 +02:00
|
|
|
#include "util/CharUtil.hxx"
|
2014-04-24 09:43:08 +02:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2018-07-24 19:21:51 +02:00
|
|
|
#include "util/RuntimeError.hxx"
|
2018-01-19 23:35:05 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
2018-07-24 19:21:51 +02:00
|
|
|
#include "util/StringStrip.hxx"
|
2018-01-19 23:35:05 +01:00
|
|
|
#include "util/StringView.hxx"
|
2013-10-20 23:09:51 +02:00
|
|
|
#include "util/ASCII.hxx"
|
2013-10-29 18:54:34 +01:00
|
|
|
#include "util/UriUtil.hxx"
|
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"
|
|
|
|
|
2018-08-02 17:23:25 +02:00
|
|
|
enum {
|
|
|
|
/**
|
|
|
|
* Limit the search to files within the given directory.
|
|
|
|
*/
|
|
|
|
LOCATE_TAG_BASE_TYPE = TAG_NUM_OF_ITEM_TYPES + 1,
|
|
|
|
|
|
|
|
LOCATE_TAG_MODIFIED_SINCE,
|
2018-08-02 13:36:14 +02:00
|
|
|
LOCATE_TAG_AUDIO_FORMAT,
|
2018-08-02 17:23:25 +02:00
|
|
|
LOCATE_TAG_FILE_TYPE,
|
|
|
|
LOCATE_TAG_ANY_TYPE,
|
|
|
|
};
|
2018-07-25 11:32:41 +02:00
|
|
|
|
2018-07-25 11:30:56 +02:00
|
|
|
/**
|
|
|
|
* @return #TAG_NUM_OF_ITEM_TYPES on error
|
|
|
|
*/
|
|
|
|
gcc_pure
|
|
|
|
static 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;
|
|
|
|
|
2018-08-02 13:36:14 +02:00
|
|
|
if (StringEqualsCaseASCII(str, "AudioFormat"))
|
|
|
|
return LOCATE_TAG_AUDIO_FORMAT;
|
|
|
|
|
2012-08-08 00:45:46 +02:00
|
|
|
return tag_name_parse_i(str);
|
2007-02-24 01:54:54 +01:00
|
|
|
}
|
|
|
|
|
2018-07-29 18:39:47 +02:00
|
|
|
SongFilter::SongFilter(TagType tag, const char *value, bool fold_case)
|
|
|
|
{
|
2018-11-04 13:57:34 +01:00
|
|
|
/* for compatibility with MPD 0.20 and older, "fold_case" also
|
|
|
|
switches on "substring" */
|
2018-11-07 23:57:42 +01:00
|
|
|
and_filter.AddItem(std::make_unique<TagSongFilter>(tag,
|
|
|
|
StringFilter(value, fold_case, fold_case, false)));
|
2018-07-29 18:39:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 01:52:46 +01:00
|
|
|
/* this destructor exists here just so it won't get inlined */
|
|
|
|
SongFilter::~SongFilter() = default;
|
2018-07-29 18:39:47 +02:00
|
|
|
|
|
|
|
std::string
|
|
|
|
SongFilter::ToExpression() const noexcept
|
|
|
|
{
|
|
|
|
return and_filter.ToExpression();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2019-12-16 23:28:47 +01:00
|
|
|
try {
|
|
|
|
/* try ISO 8601 */
|
|
|
|
return ParseISO8601(s).first;
|
|
|
|
} catch (...) {
|
|
|
|
char *endptr;
|
|
|
|
unsigned long long value = strtoull(s, &endptr, 10);
|
|
|
|
if (*endptr == 0 && endptr > s)
|
|
|
|
/* it's an integral UNIX time stamp */
|
|
|
|
return std::chrono::system_clock::from_time_t((time_t)value);
|
|
|
|
|
|
|
|
/* rethrow the ParseISO8601() error */
|
|
|
|
throw;
|
|
|
|
}
|
2014-08-11 22:08:26 +02:00
|
|
|
}
|
|
|
|
|
2018-07-24 19:21:51 +02:00
|
|
|
static constexpr bool
|
|
|
|
IsTagNameChar(char ch) noexcept
|
|
|
|
{
|
2018-07-25 11:04:19 +02:00
|
|
|
return IsAlphaASCII(ch) || ch == '_' || ch == '-';
|
2018-07-24 19:21:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
FirstNonTagNameChar(const char *s) noexcept
|
|
|
|
{
|
|
|
|
while (IsTagNameChar(*s))
|
|
|
|
++s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static auto
|
2018-07-30 10:18:42 +02:00
|
|
|
ExpectWord(const char *&s)
|
2018-07-24 19:21:51 +02:00
|
|
|
{
|
2018-07-30 10:18:42 +02:00
|
|
|
const char *begin = s;
|
2018-07-24 19:21:51 +02:00
|
|
|
const char *end = FirstNonTagNameChar(s);
|
|
|
|
if (end == s)
|
2018-07-30 10:18:42 +02:00
|
|
|
throw std::runtime_error("Word expected");
|
2018-07-24 19:21:51 +02:00
|
|
|
|
|
|
|
s = StripLeft(end);
|
2018-07-30 10:18:42 +02:00
|
|
|
return std::string(begin, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
static auto
|
|
|
|
ExpectFilterType(const char *&s)
|
|
|
|
{
|
|
|
|
const auto name = ExpectWord(s);
|
2018-07-24 19:21:51 +02:00
|
|
|
|
|
|
|
const auto type = locate_parse_type(name.c_str());
|
|
|
|
if (type == TAG_NUM_OF_ITEM_TYPES)
|
|
|
|
throw FormatRuntimeError("Unknown filter type: %s",
|
|
|
|
name.c_str());
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr bool
|
|
|
|
IsQuote(char ch) noexcept
|
|
|
|
{
|
|
|
|
return ch == '"' || ch == '\'';
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string
|
|
|
|
ExpectQuoted(const char *&s)
|
|
|
|
{
|
|
|
|
const char quote = *s++;
|
|
|
|
if (!IsQuote(quote))
|
|
|
|
throw std::runtime_error("Quoted string expected");
|
|
|
|
|
2018-11-02 19:15:08 +01:00
|
|
|
char buffer[4096];
|
|
|
|
size_t length = 0;
|
|
|
|
|
|
|
|
while (*s != quote) {
|
|
|
|
if (*s == '\\')
|
|
|
|
/* backslash escapes the following character */
|
|
|
|
++s;
|
|
|
|
|
|
|
|
if (*s == 0)
|
|
|
|
throw std::runtime_error("Closing quote not found");
|
|
|
|
|
|
|
|
buffer[length++] = *s++;
|
|
|
|
|
|
|
|
if (length >= sizeof(buffer))
|
|
|
|
throw std::runtime_error("Quoted value is too long");
|
|
|
|
}
|
|
|
|
|
|
|
|
s = StripLeft(s + 1);
|
2018-07-24 19:21:51 +02:00
|
|
|
|
2018-11-02 19:15:08 +01:00
|
|
|
return {buffer, length};
|
2018-07-24 19:21:51 +02:00
|
|
|
}
|
|
|
|
|
2018-11-08 00:02:10 +01:00
|
|
|
/**
|
|
|
|
* Parse a string operator and its second operand and convert it to a
|
|
|
|
* #StringFilter.
|
|
|
|
*
|
|
|
|
* Throws on error.
|
|
|
|
*/
|
|
|
|
static StringFilter
|
|
|
|
ParseStringFilter(const char *&s, bool fold_case)
|
|
|
|
{
|
2018-11-12 12:41:29 +01:00
|
|
|
if (auto after_contains = StringAfterPrefixIgnoreCase(s, "contains ")) {
|
|
|
|
s = StripLeft(after_contains);
|
|
|
|
auto value = ExpectQuoted(s);
|
|
|
|
return StringFilter(std::move(value),
|
|
|
|
fold_case, true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto after_not_contains = StringAfterPrefixIgnoreCase(s, "!contains ")) {
|
|
|
|
s = StripLeft(after_not_contains);
|
|
|
|
auto value = ExpectQuoted(s);
|
|
|
|
return StringFilter(std::move(value),
|
|
|
|
fold_case, true, true);
|
|
|
|
}
|
|
|
|
|
2018-11-08 00:02:10 +01:00
|
|
|
bool negated = false;
|
2018-11-07 00:28:15 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_PCRE
|
|
|
|
if ((s[0] == '!' || s[0] == '=') && s[1] == '~') {
|
|
|
|
negated = s[0] == '!';
|
|
|
|
s = StripLeft(s + 2);
|
|
|
|
auto value = ExpectQuoted(s);
|
|
|
|
StringFilter f(std::move(value), fold_case, false, negated);
|
|
|
|
f.SetRegex(std::make_shared<UniqueRegex>(f.GetValue().c_str(),
|
|
|
|
false, false,
|
|
|
|
fold_case));
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-08 00:02:10 +01:00
|
|
|
if (s[0] == '!' && s[1] == '=')
|
|
|
|
negated = true;
|
|
|
|
else if (s[0] != '=' || s[1] != '=')
|
|
|
|
throw std::runtime_error("'==' or '!=' expected");
|
|
|
|
|
|
|
|
s = StripLeft(s + 2);
|
|
|
|
auto value = ExpectQuoted(s);
|
|
|
|
|
|
|
|
return StringFilter(std::move(value),
|
|
|
|
fold_case, false, negated);
|
|
|
|
}
|
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
ISongFilterPtr
|
|
|
|
SongFilter::ParseExpression(const char *&s, bool fold_case)
|
2018-07-24 19:21:51 +02:00
|
|
|
{
|
|
|
|
assert(*s == '(');
|
|
|
|
|
|
|
|
s = StripLeft(s + 1);
|
|
|
|
|
2018-07-30 10:00:38 +02:00
|
|
|
if (*s == '(') {
|
|
|
|
auto first = ParseExpression(s, fold_case);
|
|
|
|
if (*s == ')') {
|
|
|
|
++s;
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2018-07-25 07:52:38 +02:00
|
|
|
if (ExpectWord(s) != "AND")
|
|
|
|
throw std::runtime_error("'AND' expected");
|
|
|
|
|
|
|
|
auto and_filter = std::make_unique<AndSongFilter>();
|
|
|
|
and_filter->AddItem(std::move(first));
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
and_filter->AddItem(ParseExpression(s, fold_case));
|
|
|
|
|
|
|
|
if (*s == ')') {
|
|
|
|
++s;
|
|
|
|
return and_filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ExpectWord(s) != "AND")
|
|
|
|
throw std::runtime_error("'AND' expected");
|
|
|
|
}
|
2018-07-30 10:00:38 +02:00
|
|
|
}
|
2018-07-24 19:21:51 +02:00
|
|
|
|
2018-07-25 10:19:49 +02:00
|
|
|
if (*s == '!') {
|
|
|
|
s = StripLeft(s + 1);
|
|
|
|
|
|
|
|
if (*s != '(')
|
|
|
|
throw std::runtime_error("'(' expected");
|
|
|
|
|
|
|
|
auto inner = ParseExpression(s, fold_case);
|
|
|
|
if (*s != ')')
|
|
|
|
throw std::runtime_error("')' expected");
|
|
|
|
s = StripLeft(s + 1);
|
|
|
|
|
|
|
|
return std::make_unique<NotSongFilter>(std::move(inner));
|
|
|
|
}
|
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
auto type = ExpectFilterType(s);
|
2018-07-24 19:21:51 +02:00
|
|
|
|
|
|
|
if (type == LOCATE_TAG_MODIFIED_SINCE) {
|
|
|
|
const auto value_s = ExpectQuoted(s);
|
|
|
|
if (*s != ')')
|
|
|
|
throw std::runtime_error("')' expected");
|
2018-07-25 07:58:35 +02:00
|
|
|
s = StripLeft(s + 1);
|
|
|
|
return std::make_unique<ModifiedSinceSongFilter>(ParseTimeStamp(value_s.c_str()));
|
2018-07-24 19:21:51 +02:00
|
|
|
} else if (type == LOCATE_TAG_BASE_TYPE) {
|
|
|
|
auto value = ExpectQuoted(s);
|
|
|
|
if (*s != ')')
|
|
|
|
throw std::runtime_error("')' expected");
|
2018-07-25 07:58:35 +02:00
|
|
|
s = StripLeft(s + 1);
|
2018-07-24 19:21:51 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
return std::make_unique<BaseSongFilter>(std::move(value));
|
2018-08-02 13:36:14 +02:00
|
|
|
} else if (type == LOCATE_TAG_AUDIO_FORMAT) {
|
2018-08-02 21:24:52 +02:00
|
|
|
bool mask;
|
|
|
|
if (s[0] == '=' && s[1] == '=')
|
|
|
|
mask = false;
|
|
|
|
else if (s[0] == '=' && s[1] == '~')
|
|
|
|
mask = true;
|
|
|
|
else
|
|
|
|
throw std::runtime_error("'==' or '=~' expected");
|
2018-08-02 13:36:14 +02:00
|
|
|
|
|
|
|
s = StripLeft(s + 2);
|
|
|
|
|
|
|
|
const auto value = ParseAudioFormat(ExpectQuoted(s).c_str(),
|
2018-08-02 21:24:52 +02:00
|
|
|
mask);
|
2018-08-02 13:36:14 +02:00
|
|
|
|
|
|
|
if (*s != ')')
|
|
|
|
throw std::runtime_error("')' expected");
|
|
|
|
s = StripLeft(s + 1);
|
|
|
|
|
|
|
|
return std::make_unique<AudioFormatSongFilter>(value);
|
2018-07-24 19:21:51 +02:00
|
|
|
} else {
|
2018-11-08 00:02:10 +01:00
|
|
|
auto string_filter = ParseStringFilter(s, fold_case);
|
2018-07-24 19:21:51 +02:00
|
|
|
if (*s != ')')
|
|
|
|
throw std::runtime_error("')' expected");
|
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
s = StripLeft(s + 1);
|
|
|
|
|
|
|
|
if (type == LOCATE_TAG_ANY_TYPE)
|
|
|
|
type = TAG_NUM_OF_ITEM_TYPES;
|
|
|
|
|
|
|
|
if (type == LOCATE_TAG_FILE_TYPE)
|
2018-11-07 23:57:42 +01:00
|
|
|
return std::make_unique<UriSongFilter>(std::move(string_filter));
|
2018-07-25 07:58:35 +02:00
|
|
|
|
|
|
|
return std::make_unique<TagSongFilter>(TagType(type),
|
2018-11-07 23:57:42 +01:00
|
|
|
std::move(string_filter));
|
2018-07-24 19:21:51 +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);
|
2018-07-25 07:58:35 +02:00
|
|
|
|
|
|
|
switch (tag) {
|
|
|
|
case 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
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
case LOCATE_TAG_BASE_TYPE:
|
2013-10-29 18:54:34 +01:00
|
|
|
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
|
|
|
|
2018-07-29 18:39:47 +02:00
|
|
|
and_filter.AddItem(std::make_unique<BaseSongFilter>(value));
|
2018-07-25 07:58:35 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCATE_TAG_MODIFIED_SINCE:
|
2018-07-29 18:39:47 +02:00
|
|
|
and_filter.AddItem(std::make_unique<ModifiedSinceSongFilter>(ParseTimeStamp(value)));
|
2018-07-25 07:58:35 +02:00
|
|
|
break;
|
2013-10-29 18:54:34 +01:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
case LOCATE_TAG_FILE_TYPE:
|
2018-11-04 13:57:34 +01:00
|
|
|
/* for compatibility with MPD 0.20 and older,
|
|
|
|
"fold_case" also switches on "substring" */
|
2018-11-07 23:57:42 +01:00
|
|
|
and_filter.AddItem(std::make_unique<UriSongFilter>(StringFilter(value,
|
|
|
|
fold_case,
|
|
|
|
fold_case,
|
|
|
|
false)));
|
2018-07-25 07:58:35 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (tag == LOCATE_TAG_ANY_TYPE)
|
|
|
|
tag = TAG_NUM_OF_ITEM_TYPES;
|
|
|
|
|
2018-11-04 13:57:34 +01:00
|
|
|
/* for compatibility with MPD 0.20 and older,
|
|
|
|
"fold_case" also switches on "substring" */
|
2018-07-29 18:39:47 +02:00
|
|
|
and_filter.AddItem(std::make_unique<TagSongFilter>(TagType(tag),
|
2018-11-07 23:57:42 +01:00
|
|
|
StringFilter(value,
|
|
|
|
fold_case,
|
|
|
|
fold_case,
|
|
|
|
false)));
|
2018-07-25 07:58:35 +02:00
|
|
|
break;
|
|
|
|
}
|
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 {
|
2018-07-24 19:21:51 +02:00
|
|
|
if (*args.front() == '(') {
|
|
|
|
const char *s = args.shift();
|
2018-07-25 07:58:35 +02:00
|
|
|
const char *end = s;
|
|
|
|
auto f = ParseExpression(end, fold_case);
|
2018-07-24 19:21:51 +02:00
|
|
|
if (*end != 0)
|
|
|
|
throw std::runtime_error("Unparsed garbage after expression");
|
|
|
|
|
2018-07-29 18:39:47 +02:00
|
|
|
and_filter.AddItem(std::move(f));
|
2018-07-24 19:21:51 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-21 07:36:42 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-02 19:42:53 +02:00
|
|
|
void
|
|
|
|
SongFilter::Optimize() noexcept
|
|
|
|
{
|
|
|
|
OptimizeSongFilter(and_filter);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2018-07-29 18:39:47 +02:00
|
|
|
return and_filter.Match(song);
|
2014-01-07 21:39:47 +01:00
|
|
|
}
|
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
bool
|
|
|
|
SongFilter::HasFoldCase() const noexcept
|
|
|
|
{
|
2018-07-29 18:39:47 +02:00
|
|
|
for (const auto &i : and_filter.GetItems()) {
|
2018-07-25 07:58:35 +02:00
|
|
|
if (auto t = dynamic_cast<const TagSongFilter *>(i.get())) {
|
|
|
|
if (t->GetFoldCase())
|
|
|
|
return true;
|
|
|
|
} else if (auto u = dynamic_cast<const UriSongFilter *>(i.get())) {
|
|
|
|
if (u->GetFoldCase())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2018-07-29 18:39:47 +02:00
|
|
|
for (const auto &i : and_filter.GetItems()) {
|
2018-07-25 07:58:35 +02:00
|
|
|
const auto *f = dynamic_cast<const BaseSongFilter *>(i.get());
|
|
|
|
if (f == nullptr)
|
2014-06-23 09:12:51 +02:00
|
|
|
return true;
|
2018-07-25 07:58:35 +02:00
|
|
|
}
|
2014-06-23 09:12:51 +02:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2018-07-29 18:39:47 +02:00
|
|
|
for (const auto &i : and_filter.GetItems()) {
|
2018-07-25 07:58:35 +02:00
|
|
|
const auto *f = dynamic_cast<const BaseSongFilter *>(i.get());
|
|
|
|
if (f != nullptr)
|
|
|
|
return f->GetValue();
|
|
|
|
}
|
2013-10-29 18:54:34 +01:00
|
|
|
|
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;
|
|
|
|
|
2018-07-29 18:39:47 +02:00
|
|
|
for (const auto &i : and_filter.GetItems()) {
|
2018-07-25 07:58:35 +02:00
|
|
|
const auto *f = dynamic_cast<const BaseSongFilter *>(i.get());
|
|
|
|
if (f != nullptr) {
|
|
|
|
const char *s = StringAfterPrefix(f->GetValue(), prefix);
|
2018-01-19 23:35:05 +01:00
|
|
|
if (s != nullptr) {
|
|
|
|
if (*s == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*s == '/') {
|
|
|
|
++s;
|
|
|
|
|
|
|
|
if (*s != 0)
|
2018-07-29 18:39:47 +02:00
|
|
|
result.and_filter.AddItem(std::make_unique<BaseSongFilter>(s));
|
2018-01-19 23:35:05 +01:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 18:39:47 +02:00
|
|
|
result.and_filter.AddItem(i->Clone());
|
2018-01-19 23:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|