SongFilter.hxx 3.32 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "util/AllocatedString.hxx"
24
#include "Compiler.h"
25

26 27
#include <list>

28
#include <stdint.h>
29
#include <time.h>
30

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

37 38 39
#define LOCATE_TAG_FILE_TYPE	TAG_NUM_OF_ITEM_TYPES+10
#define LOCATE_TAG_ANY_TYPE     TAG_NUM_OF_ITEM_TYPES+20

40
template<typename T> struct ConstBuffer;
Max Kellermann's avatar
Max Kellermann committed
41 42
struct Tag;
struct TagItem;
43
struct LightSong;
44
class DetachedSong;
45

46
class SongFilter {
47
public:
48 49 50 51 52
	class Item {
		uint8_t tag;

		bool fold_case;

53
		AllocatedString<> value;
54

55 56 57 58 59
		/**
		 * For #LOCATE_TAG_MODIFIED_SINCE
		 */
		time_t time;

60 61 62
	public:
		gcc_nonnull(3)
		Item(unsigned tag, const char *value, bool fold_case=false);
63
		Item(unsigned tag, time_t time);
64 65

		Item(const Item &other) = delete;
66
		Item(Item &&) = default;
67 68 69 70 71 72 73

		Item &operator=(const Item &other) = delete;

		unsigned GetTag() const {
			return tag;
		}

74 75 76 77
		bool GetFoldCase() const {
			return fold_case;
		}

78 79
		const char *GetValue() const {
			return value.c_str();
80 81
		}

82 83 84 85
		gcc_pure gcc_nonnull(2)
		bool StringMatch(const char *s) const;

		gcc_pure
Max Kellermann's avatar
Max Kellermann committed
86
		bool Match(const TagItem &tag_item) const;
87 88

		gcc_pure
Max Kellermann's avatar
Max Kellermann committed
89
		bool Match(const Tag &tag) const;
90 91

		gcc_pure
92
		bool Match(const DetachedSong &song) const;
93 94

		gcc_pure
95
		bool Match(const LightSong &song) const;
96 97
	};

98
private:
99 100 101 102 103 104 105 106 107 108 109 110 111
	std::list<Item> items;

public:
	SongFilter() = default;

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

	~SongFilter();

	gcc_nonnull(2,3)
	bool Parse(const char *tag, const char *value, bool fold_case=false);

112
	bool Parse(ConstBuffer<const char *> args, bool fold_case=false);
113 114

	gcc_pure
Max Kellermann's avatar
Max Kellermann committed
115
	bool Match(const Tag &tag) const;
116 117

	gcc_pure
118
	bool Match(const DetachedSong &song) const;
119

120
	gcc_pure
121
	bool Match(const LightSong &song) const;
122

123 124 125
	const std::list<Item> &GetItems() const {
		return items;
	}
126

127 128 129 130 131
	gcc_pure
	bool IsEmpty() const {
		return items.empty();
	}

132 133 134 135 136 137 138 139 140 141 142 143
	/**
	 * Is there at least one item with "fold case" enabled?
	 */
	gcc_pure
	bool HasFoldCase() const {
		for (const auto &i : items)
			if (i.GetFoldCase())
				return true;

		return false;
	}

144 145 146 147 148 149
	/**
	 * Does this filter contain constraints other than "base"?
	 */
	gcc_pure
	bool HasOtherThanBase() const;

150
	/**
151 152
	 * Returns the "base" specification (if there is one) or
	 * nullptr.
153 154
	 */
	gcc_pure
155
	const char *GetBase() const;
156 157
};

158 159 160
/**
 * @return #TAG_NUM_OF_ITEM_TYPES on error
 */
161
gcc_pure
162
unsigned
Max Kellermann's avatar
Max Kellermann committed
163
locate_parse_type(const char *str);
164

165
#endif