2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2018-07-25 07:58:35 +02:00
|
|
|
* Copyright 2003-2018 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
|
|
|
*/
|
|
|
|
|
2012-08-29 19:12:26 +02:00
|
|
|
#ifndef MPD_SONG_FILTER_HXX
|
|
|
|
#define MPD_SONG_FILTER_HXX
|
2008-08-28 20:01:08 +02:00
|
|
|
|
2017-09-20 23:26:38 +02:00
|
|
|
#include "lib/icu/Compare.hxx"
|
2013-10-15 09:21:13 +02:00
|
|
|
#include "Compiler.h"
|
2011-09-13 21:36:51 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
#include <memory>
|
2018-01-19 23:35:36 +01:00
|
|
|
#include <string>
|
2012-08-29 19:27:03 +02:00
|
|
|
#include <list>
|
2017-01-18 13:03:05 +01:00
|
|
|
#include <chrono>
|
2012-08-29 19:27:03 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2017-12-18 21:36:47 +01:00
|
|
|
/**
|
|
|
|
* Special value for the db_selection_print() sort parameter.
|
|
|
|
*/
|
|
|
|
#define SORT_TAG_LAST_MODIFIED (TAG_NUM_OF_ITEM_TYPES + 3)
|
|
|
|
|
2014-04-24 09:43:08 +02:00
|
|
|
template<typename T> struct ConstBuffer;
|
2018-07-25 07:58:35 +02:00
|
|
|
enum TagType : uint8_t;
|
2013-07-30 20:11:57 +02:00
|
|
|
struct Tag;
|
|
|
|
struct TagItem;
|
2014-01-19 10:51:34 +01:00
|
|
|
struct LightSong;
|
2018-07-25 07:58:35 +02:00
|
|
|
class ISongFilter;
|
|
|
|
using ISongFilterPtr = std::unique_ptr<ISongFilter>;
|
|
|
|
|
|
|
|
class ISongFilter {
|
|
|
|
public:
|
|
|
|
virtual ~ISongFilter() noexcept {}
|
|
|
|
|
|
|
|
virtual ISongFilterPtr Clone() const noexcept = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert this object into an "expression". This is
|
|
|
|
* only useful for debugging.
|
|
|
|
*/
|
|
|
|
virtual std::string ToExpression() const noexcept = 0;
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
virtual bool Match(const LightSong &song) const noexcept = 0;
|
|
|
|
};
|
2008-10-08 10:49:11 +02:00
|
|
|
|
2018-07-25 10:18:02 +02:00
|
|
|
class StringFilter {
|
|
|
|
std::string value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This value is only set if case folding is enabled.
|
|
|
|
*/
|
|
|
|
IcuCompare fold_case;
|
|
|
|
|
|
|
|
public:
|
|
|
|
template<typename V>
|
|
|
|
StringFilter(V &&_value, bool _fold_case)
|
|
|
|
:value(std::forward<V>(_value)),
|
|
|
|
fold_case(_fold_case
|
|
|
|
? IcuCompare(value.c_str())
|
|
|
|
: IcuCompare()) {}
|
|
|
|
|
|
|
|
bool empty() const noexcept {
|
|
|
|
return value.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &GetValue() const noexcept {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetFoldCase() const noexcept {
|
|
|
|
return fold_case;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
bool Match(const char *s) const noexcept;
|
|
|
|
};
|
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
class UriSongFilter final : public ISongFilter {
|
|
|
|
StringFilter filter;
|
|
|
|
|
|
|
|
bool negated;
|
|
|
|
|
2013-10-29 20:47:52 +01:00
|
|
|
public:
|
2018-07-25 07:58:35 +02:00
|
|
|
template<typename V>
|
|
|
|
UriSongFilter(V &&_value, bool fold_case, bool _negated)
|
|
|
|
:filter(std::forward<V>(_value), fold_case),
|
|
|
|
negated(_negated) {}
|
|
|
|
|
|
|
|
const auto &GetValue() const noexcept {
|
|
|
|
return filter.GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetFoldCase() const {
|
|
|
|
return filter.GetFoldCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNegated() const noexcept {
|
|
|
|
return negated;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISongFilterPtr Clone() const noexcept override {
|
|
|
|
return std::make_unique<UriSongFilter>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToExpression() const noexcept override;
|
|
|
|
bool Match(const LightSong &song) const noexcept override;
|
|
|
|
};
|
2012-08-29 19:27:03 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
class BaseSongFilter final : public ISongFilter {
|
|
|
|
std::string value;
|
2018-07-21 11:20:50 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
public:
|
|
|
|
BaseSongFilter(const BaseSongFilter &) = default;
|
2017-09-20 23:26:38 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
template<typename V>
|
|
|
|
explicit BaseSongFilter(V &&_value)
|
|
|
|
:value(std::forward<V>(_value)) {}
|
2014-08-11 22:08:26 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
const char *GetValue() const noexcept {
|
|
|
|
return value.c_str();
|
|
|
|
}
|
2012-08-29 19:27:03 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
ISongFilterPtr Clone() const noexcept override {
|
|
|
|
return std::make_unique<BaseSongFilter>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToExpression() const noexcept override;
|
|
|
|
bool Match(const LightSong &song) const noexcept override;
|
|
|
|
};
|
2018-07-24 22:11:14 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
class TagSongFilter final : public ISongFilter {
|
|
|
|
TagType type;
|
2012-08-29 19:27:03 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
bool negated;
|
2018-07-21 11:20:50 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
StringFilter filter;
|
2018-07-21 11:20:50 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
public:
|
|
|
|
template<typename V>
|
|
|
|
TagSongFilter(TagType _type, V &&_value, bool fold_case, bool _negated)
|
|
|
|
:type(_type), negated(_negated),
|
|
|
|
filter(std::forward<V>(_value), fold_case) {}
|
2013-10-30 10:00:57 +01:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
TagType GetTagType() const {
|
|
|
|
return type;
|
|
|
|
}
|
2013-10-29 20:47:52 +01:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
const auto &GetValue() const noexcept {
|
|
|
|
return filter.GetValue();
|
|
|
|
}
|
2018-07-21 11:20:50 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
bool GetFoldCase() const {
|
|
|
|
return filter.GetFoldCase();
|
|
|
|
}
|
2012-08-29 19:27:03 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
bool IsNegated() const noexcept {
|
|
|
|
return negated;
|
|
|
|
}
|
2012-08-29 19:27:03 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
ISongFilterPtr Clone() const noexcept override {
|
|
|
|
return std::make_unique<TagSongFilter>(*this);
|
|
|
|
}
|
2018-07-21 11:20:50 +02:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
std::string ToExpression() const noexcept override;
|
|
|
|
bool Match(const LightSong &song) const noexcept override;
|
2012-08-29 19:27:03 +02:00
|
|
|
|
2013-10-29 20:47:52 +01:00
|
|
|
private:
|
2018-07-25 07:58:35 +02:00
|
|
|
bool MatchNN(const Tag &tag) const noexcept;
|
|
|
|
bool MatchNN(const TagItem &tag) const noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ModifiedSinceSongFilter final : public ISongFilter {
|
|
|
|
std::chrono::system_clock::time_point value;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ModifiedSinceSongFilter(std::chrono::system_clock::time_point _value) noexcept
|
|
|
|
:value(_value) {}
|
|
|
|
|
|
|
|
ISongFilterPtr Clone() const noexcept override {
|
|
|
|
return std::make_unique<ModifiedSinceSongFilter>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToExpression() const noexcept override;
|
|
|
|
bool Match(const LightSong &song) const noexcept override;
|
|
|
|
};
|
|
|
|
|
2018-07-29 18:39:47 +02:00
|
|
|
/**
|
|
|
|
* Combine multiple #ISongFilter instances with logical "and".
|
|
|
|
*/
|
|
|
|
class AndSongFilter final : public ISongFilter {
|
2018-07-25 07:58:35 +02:00
|
|
|
std::list<ISongFilterPtr> items;
|
2012-08-29 19:27:03 +02:00
|
|
|
|
2018-07-29 18:39:47 +02:00
|
|
|
public:
|
|
|
|
const auto &GetItems() const noexcept {
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename I>
|
|
|
|
void AddItem(I &&_item) {
|
|
|
|
items.emplace_back(std::forward<I>(_item));
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
bool IsEmpty() const noexcept {
|
|
|
|
return items.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual methods from ISongFilter */
|
|
|
|
ISongFilterPtr Clone() const noexcept override;
|
|
|
|
std::string ToExpression() const noexcept override;
|
|
|
|
bool Match(const LightSong &song) const noexcept override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SongFilter {
|
|
|
|
AndSongFilter and_filter;
|
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
public:
|
|
|
|
SongFilter() = default;
|
|
|
|
|
|
|
|
gcc_nonnull(3)
|
2018-07-25 07:58:35 +02:00
|
|
|
SongFilter(TagType tag, const char *value, bool fold_case=false);
|
2012-08-29 19:27:03 +02:00
|
|
|
|
|
|
|
~SongFilter();
|
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
SongFilter(SongFilter &&) = default;
|
|
|
|
SongFilter &operator=(SongFilter &&) = default;
|
|
|
|
|
2018-07-24 22:11:14 +02:00
|
|
|
/**
|
|
|
|
* Convert this object into an "expression". This is
|
|
|
|
* only useful for debugging.
|
|
|
|
*/
|
|
|
|
std::string ToExpression() const noexcept;
|
|
|
|
|
2018-07-21 07:21:27 +02:00
|
|
|
private:
|
2018-07-25 07:58:35 +02:00
|
|
|
ISongFilterPtr ParseExpression(const char *&s, bool fold_case=false);
|
2018-07-24 19:21:51 +02:00
|
|
|
|
2012-08-29 19:27:03 +02:00
|
|
|
gcc_nonnull(2,3)
|
2018-07-21 07:20:59 +02:00
|
|
|
void Parse(const char *tag, const char *value, bool fold_case=false);
|
2012-08-29 19:27:03 +02:00
|
|
|
|
2018-07-21 07:21:27 +02:00
|
|
|
public:
|
2018-07-21 07:20:59 +02:00
|
|
|
/**
|
|
|
|
* Throws on error.
|
|
|
|
*/
|
|
|
|
void Parse(ConstBuffer<const char *> args, bool fold_case=false);
|
2012-08-29 19:27:03 +02:00
|
|
|
|
|
|
|
gcc_pure
|
2017-05-08 14:44:49 +02:00
|
|
|
bool Match(const LightSong &song) const noexcept;
|
2014-01-07 21:39:47 +01:00
|
|
|
|
2018-07-25 07:58:35 +02:00
|
|
|
const auto &GetItems() const noexcept {
|
2018-07-29 18:39:47 +02:00
|
|
|
return and_filter.GetItems();
|
2013-10-29 20:47:52 +01:00
|
|
|
}
|
2013-10-29 18:54:34 +01:00
|
|
|
|
2014-06-23 09:12:51 +02:00
|
|
|
gcc_pure
|
2017-05-08 14:44:49 +02:00
|
|
|
bool IsEmpty() const noexcept {
|
2018-07-29 18:39:47 +02:00
|
|
|
return and_filter.IsEmpty();
|
2014-06-23 09:12:51 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 10:00:57 +01:00
|
|
|
/**
|
|
|
|
* Is there at least one item with "fold case" enabled?
|
|
|
|
*/
|
|
|
|
gcc_pure
|
2018-07-25 07:58:35 +02:00
|
|
|
bool HasFoldCase() const noexcept;
|
2013-10-30 10:00:57 +01:00
|
|
|
|
2014-06-23 09:12:51 +02:00
|
|
|
/**
|
|
|
|
* Does this filter contain constraints other than "base"?
|
|
|
|
*/
|
|
|
|
gcc_pure
|
2017-05-08 14:44:49 +02:00
|
|
|
bool HasOtherThanBase() const noexcept;
|
2014-06-23 09:12:51 +02:00
|
|
|
|
2013-10-29 18:54:34 +01:00
|
|
|
/**
|
2015-06-25 23:14:40 +02:00
|
|
|
* Returns the "base" specification (if there is one) or
|
|
|
|
* nullptr.
|
2013-10-29 18:54:34 +01:00
|
|
|
*/
|
|
|
|
gcc_pure
|
2017-05-08 14:44:49 +02:00
|
|
|
const char *GetBase() const noexcept;
|
2018-01-19 23:35:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a copy of the filter with the given prefix stripped
|
|
|
|
* from all #LOCATE_TAG_BASE_TYPE items. This is used to
|
|
|
|
* filter songs in mounted databases.
|
|
|
|
*/
|
|
|
|
SongFilter WithoutBasePrefix(const char *prefix) const noexcept;
|
2012-08-29 19:27:03 +02:00
|
|
|
};
|
|
|
|
|
2008-08-28 20:01:08 +02:00
|
|
|
#endif
|