SongFilter.hxx 3.83 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20 21
#ifndef MPD_SONG_FILTER_HXX
#define MPD_SONG_FILTER_HXX
22

23
#include "lib/icu/Compare.hxx"
24
#include "Compiler.h"
25

26
#include <string>
27
#include <list>
28
#include <chrono>
29

30 31 32 33
/**
 * Limit the search to files within the given directory.
 */
#define LOCATE_TAG_BASE_TYPE (TAG_NUM_OF_ITEM_TYPES + 1)
34
#define LOCATE_TAG_MODIFIED_SINCE (TAG_NUM_OF_ITEM_TYPES + 2)
35

36 37 38 39 40
/**
 * Special value for the db_selection_print() sort parameter.
 */
#define SORT_TAG_LAST_MODIFIED (TAG_NUM_OF_ITEM_TYPES + 3)

41 42 43
#define LOCATE_TAG_FILE_TYPE	TAG_NUM_OF_ITEM_TYPES+10
#define LOCATE_TAG_ANY_TYPE     TAG_NUM_OF_ITEM_TYPES+20

44
template<typename T> struct ConstBuffer;
Max Kellermann's avatar
Max Kellermann committed
45 46
struct Tag;
struct TagItem;
47
struct LightSong;
48
class DetachedSong;
49

50
class SongFilter {
51
public:
52
	class Item {
53
		unsigned tag;
54

55
		std::string value;
56

57 58 59 60 61
		/**
		 * This value is only set if case folding is enabled.
		 */
		IcuCompare fold_case;

62 63 64
		/**
		 * For #LOCATE_TAG_MODIFIED_SINCE
		 */
65
		std::chrono::system_clock::time_point time;
66

67 68 69
	public:
		gcc_nonnull(3)
		Item(unsigned tag, const char *value, bool fold_case=false);
70
		Item(unsigned tag, std::chrono::system_clock::time_point time);
71 72 73 74 75

		unsigned GetTag() const {
			return tag;
		}

76 77 78 79
		bool GetFoldCase() const {
			return fold_case;
		}

80 81
		const char *GetValue() const {
			return value.c_str();
82 83
		}

84
	private:
85
		gcc_pure gcc_nonnull(2)
86
		bool StringMatch(const char *s) const noexcept;
87 88

		gcc_pure
89
		bool Match(const TagItem &tag_item) const noexcept;
90 91

		gcc_pure
92
		bool Match(const Tag &tag) const noexcept;
93

94
	public:
95
		gcc_pure
96
		bool Match(const DetachedSong &song) const noexcept;
97 98

		gcc_pure
99
		bool Match(const LightSong &song) const noexcept;
100 101
	};

102
private:
103 104 105 106 107 108 109 110 111 112
	std::list<Item> items;

public:
	SongFilter() = default;

	gcc_nonnull(3)
	SongFilter(unsigned tag, const char *value, bool fold_case=false);

	~SongFilter();

113
private:
114
	gcc_nonnull(2,3)
115
	void Parse(const char *tag, const char *value, bool fold_case=false);
116

117
public:
118 119 120 121
	/**
	 * Throws on error.
	 */
	void Parse(ConstBuffer<const char *> args, bool fold_case=false);
122 123

	gcc_pure
124
	bool Match(const DetachedSong &song) const noexcept;
125

126
	gcc_pure
127
	bool Match(const LightSong &song) const noexcept;
128

129
	const std::list<Item> &GetItems() const noexcept {
130 131
		return items;
	}
132

133
	gcc_pure
134
	bool IsEmpty() const noexcept {
135 136 137
		return items.empty();
	}

138 139 140 141
	/**
	 * Is there at least one item with "fold case" enabled?
	 */
	gcc_pure
142
	bool HasFoldCase() const noexcept {
143 144 145 146 147 148 149
		for (const auto &i : items)
			if (i.GetFoldCase())
				return true;

		return false;
	}

150 151 152 153
	/**
	 * Does this filter contain constraints other than "base"?
	 */
	gcc_pure
154
	bool HasOtherThanBase() const noexcept;
155

156
	/**
157 158
	 * Returns the "base" specification (if there is one) or
	 * nullptr.
159 160
	 */
	gcc_pure
161
	const char *GetBase() const noexcept;
162 163 164 165 166 167 168

	/**
	 * 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;
169 170
};

171 172 173
/**
 * @return #TAG_NUM_OF_ITEM_TYPES on error
 */
174
gcc_pure
175
unsigned
176
locate_parse_type(const char *str) noexcept;
177

178
#endif