mpd/src/SongFilter.hxx

210 lines
4.5 KiB
C++
Raw Normal View History

/*
2017-01-03 20:48:59 +01:00
* Copyright 2003-2017 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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.
*
* 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.
*/
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"
#include <string>
2012-08-29 19:27:03 +02:00
#include <list>
#include <chrono>
2012-08-29 19:27:03 +02:00
/**
* Limit the search to files within the given directory.
*/
#define LOCATE_TAG_BASE_TYPE (TAG_NUM_OF_ITEM_TYPES + 1)
#define LOCATE_TAG_MODIFIED_SINCE (TAG_NUM_OF_ITEM_TYPES + 2)
/**
* Special value for the db_selection_print() sort parameter.
*/
#define SORT_TAG_LAST_MODIFIED (TAG_NUM_OF_ITEM_TYPES + 3)
#define LOCATE_TAG_FILE_TYPE TAG_NUM_OF_ITEM_TYPES+10
#define LOCATE_TAG_ANY_TYPE TAG_NUM_OF_ITEM_TYPES+20
template<typename T> struct ConstBuffer;
2013-07-30 20:11:57 +02:00
struct Tag;
struct TagItem;
struct LightSong;
class DetachedSong;
2012-08-29 19:27:03 +02:00
class SongFilter {
public:
2012-08-29 19:27:03 +02:00
class Item {
unsigned tag;
2012-08-29 19:27:03 +02:00
bool negated = false;
std::string value;
2012-08-29 19:27:03 +02:00
2017-09-20 23:26:38 +02:00
/**
* This value is only set if case folding is enabled.
*/
IcuCompare fold_case;
/**
* For #LOCATE_TAG_MODIFIED_SINCE
*/
std::chrono::system_clock::time_point time;
2012-08-29 19:27:03 +02:00
public:
Item(unsigned tag, std::string &&_value, bool fold_case=false);
Item(unsigned tag, std::chrono::system_clock::time_point time);
2012-08-29 19:27:03 +02:00
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;
2012-08-29 19:27:03 +02:00
unsigned GetTag() const {
return tag;
}
bool IsNegated() const noexcept {
return negated;
}
void SetNegated(bool _negated=true) noexcept {
negated = _negated;
}
bool GetFoldCase() const {
return fold_case;
}
const char *GetValue() const {
return value.c_str();
}
2018-07-21 07:15:09 +02:00
private:
/* note: the "NN" suffix means "no negation", i.e. the
method pretends negation is unset, and the caller
is responsibly for considering it */
2012-08-29 19:27:03 +02:00
gcc_pure gcc_nonnull(2)
bool StringMatchNN(const char *s) const noexcept;
2012-08-29 19:27:03 +02:00
gcc_pure
bool MatchNN(const TagItem &tag_item) const noexcept;
2012-08-29 19:27:03 +02:00
gcc_pure
bool MatchNN(const Tag &tag) const noexcept;
2012-08-29 19:27:03 +02:00
gcc_pure
bool MatchNN(const DetachedSong &song) const noexcept;
gcc_pure
bool MatchNN(const LightSong &song) const noexcept;
public:
template<typename T>
gcc_pure
bool Match(const T &t) const noexcept {
return MatchNN(t) != IsNegated();
}
2012-08-29 19:27:03 +02:00
};
private:
2012-08-29 19:27:03 +02:00
std::list<Item> items;
public:
SongFilter() = default;
gcc_nonnull(3)
SongFilter(unsigned tag, const char *value, bool fold_case=false);
~SongFilter();
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;
private:
2012-08-29 19:27:03 +02:00
gcc_nonnull(2,3)
void Parse(const char *tag, const char *value, bool fold_case=false);
2012-08-29 19:27:03 +02:00
public:
/**
* Throws on error.
*/
void Parse(ConstBuffer<const char *> args, bool fold_case=false);
2012-08-29 19:27:03 +02:00
gcc_pure
bool Match(const DetachedSong &song) const noexcept;
gcc_pure
bool Match(const LightSong &song) const noexcept;
const std::list<Item> &GetItems() const noexcept {
return items;
}
gcc_pure
bool IsEmpty() const noexcept {
return items.empty();
}
/**
* Is there at least one item with "fold case" enabled?
*/
gcc_pure
bool HasFoldCase() const noexcept {
for (const auto &i : items)
if (i.GetFoldCase())
return true;
return false;
}
/**
* Does this filter contain constraints other than "base"?
*/
gcc_pure
bool HasOtherThanBase() const noexcept;
/**
* Returns the "base" specification (if there is one) or
* nullptr.
*/
gcc_pure
const char *GetBase() const noexcept;
/**
* 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
};
2012-08-08 00:45:46 +02:00
/**
* @return #TAG_NUM_OF_ITEM_TYPES on error
*/
2012-08-07 23:43:36 +02:00
gcc_pure
2012-08-08 00:45:46 +02:00
unsigned
locate_parse_type(const char *str) noexcept;
2008-08-28 20:01:08 +02:00
#endif