locate.c 5.66 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 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
#include "config.h"
21
#include "locate.h"
Max Kellermann's avatar
Max Kellermann committed
22
#include "path.h"
Max Kellermann's avatar
Max Kellermann committed
23
#include "tag.h"
24
#include "song.h"
25

26 27
#include <glib.h>

28 29
#include <stdlib.h>

30 31
#define LOCATE_TAG_FILE_KEY     "file"
#define LOCATE_TAG_FILE_KEY_OLD "filename"
32 33
#define LOCATE_TAG_ANY_KEY      "any"

Max Kellermann's avatar
Max Kellermann committed
34 35
int
locate_parse_type(const char *str)
36 37 38
{
	int i;

39 40
	if (0 == g_ascii_strcasecmp(str, LOCATE_TAG_FILE_KEY) ||
	    0 == g_ascii_strcasecmp(str, LOCATE_TAG_FILE_KEY_OLD))
41 42
		return LOCATE_TAG_FILE_TYPE;

43
	if (0 == g_ascii_strcasecmp(str, LOCATE_TAG_ANY_KEY))
44 45
		return LOCATE_TAG_ANY_TYPE;

46 47 48
	i = tag_name_parse_i(str);
	if (i != TAG_NUM_OF_ITEM_TYPES)
		return i;
49 50 51 52

	return -1;
}

53
static bool
Max Kellermann's avatar
Max Kellermann committed
54 55
locate_item_init(struct locate_item *item,
		 const char *type_string, const char *needle)
56
{
Max Kellermann's avatar
Max Kellermann committed
57
	item->tag = locate_parse_type(type_string);
58

Max Kellermann's avatar
Max Kellermann committed
59
	if (item->tag < 0)
60
		return false;
61

62
	item->needle = g_strdup(needle);
63

64
	return true;
65 66
}

Max Kellermann's avatar
Max Kellermann committed
67
void
68
locate_item_list_free(struct locate_item_list *list)
69
{
70 71
	for (unsigned i = 0; i < list->length; ++i)
		g_free(list->items[i].needle);
72

73
	g_free(list);
74 75
}

76 77
struct locate_item_list *
locate_item_list_new(unsigned length)
78
{
79
	struct locate_item_list *list;
80

81 82 83
	list = g_malloc0(sizeof(*list) - sizeof(list->items[0]) +
			 length * sizeof(list->items[0]));
	list->length = length;
84

85 86
	return list;
}
87

88 89 90 91
struct locate_item_list *
locate_item_list_parse(char *argv[], int argc)
{
	struct locate_item_list *list;
92

93 94
	if (argc % 2 != 0)
		return NULL;
95

96
	list = locate_item_list_new(argc / 2);
97

98 99 100 101 102 103
	for (unsigned i = 0; i < list->length; ++i) {
		if (!locate_item_init(&list->items[i], argv[i * 2],
				      argv[i * 2 + 1])) {
			locate_item_list_free(list);
			return NULL;
		}
104 105
	}

106
	return list;
107 108
}

109 110 111 112 113
struct locate_item_list *
locate_item_list_casefold(const struct locate_item_list *list)
{
	struct locate_item_list *new_list = locate_item_list_new(list->length);

114
	for (unsigned i = 0; i < list->length; i++){
115 116
		new_list->items[i].needle =
			g_utf8_casefold(list->items[i].needle, -1);
117 118
		new_list->items[i].tag = list->items[i].tag;
	}
119 120 121 122

	return new_list;
}

Max Kellermann's avatar
Max Kellermann committed
123 124
void
locate_item_free(struct locate_item *item)
125
{
126 127
	g_free(item->needle);
	g_free(item);
128 129
}

130
static bool
Max Kellermann's avatar
Max Kellermann committed
131
locate_tag_search(const struct song *song, enum tag_type type, const char *str)
132
{
Max Kellermann's avatar
Max Kellermann committed
133
	char *duplicate;
134
	bool ret = false;
135
	bool visited_types[TAG_NUM_OF_ITEM_TYPES];
136 137

	if (type == LOCATE_TAG_FILE_TYPE || type == LOCATE_TAG_ANY_TYPE) {
138 139 140 141 142
		char *uri, *p;

		uri = song_get_uri(song);
		p = g_utf8_casefold(uri, -1);
		g_free(uri);
Eric Wong's avatar
Eric Wong committed
143

144
		if (strstr(p, str))
145
			ret = true;
146
		g_free(p);
Eric Wong's avatar
Eric Wong committed
147
		if (ret == 1 || type == LOCATE_TAG_FILE_TYPE)
148 149 150 151
			return ret;
	}

	if (!song->tag)
152
		return false;
153

154 155
	memset(visited_types, 0, sizeof(visited_types));

156
	for (unsigned i = 0; i < song->tag->num_items && !ret; i++) {
157
		visited_types[song->tag->items[i]->type] = true;
158
		if (type != LOCATE_TAG_ANY_TYPE &&
159
		    song->tag->items[i]->type != type) {
160 161 162
			continue;
		}

163
		duplicate = g_utf8_casefold(song->tag->items[i]->value, -1);
164
		if (*str && strstr(duplicate, str))
165
			ret = true;
166
		g_free(duplicate);
167 168
	}

169 170 171 172
	/** If the search critieron was not visited during the sweep
	 * through the song's tag, it means this field is absent from
	 * the tag or empty. Thus, if the searched string is also
	 *  empty (first char is a \0), then it's a match as well and
173
	 *  we should return true.
174
	 */
Max Kellermann's avatar
Max Kellermann committed
175
	if (!*str && !visited_types[type])
176
		return true;
177

178 179 180
	return ret;
}

181
bool
182 183
locate_song_search(const struct song *song,
		   const struct locate_item_list *criteria)
184
{
185 186 187
	for (unsigned i = 0; i < criteria->length; i++)
		if (!locate_tag_search(song, criteria->items[i].tag,
				       criteria->items[i].needle))
188
			return false;
189

190
	return true;
191 192
}

193
static bool
Max Kellermann's avatar
Max Kellermann committed
194
locate_tag_match(const struct song *song, enum tag_type type, const char *str)
195
{
196
	bool visited_types[TAG_NUM_OF_ITEM_TYPES];
197 198

	if (type == LOCATE_TAG_FILE_TYPE || type == LOCATE_TAG_ANY_TYPE) {
199
		char *uri = song_get_uri(song);
200
		bool matches = strcmp(str, uri) == 0;
201 202 203
		g_free(uri);

		if (matches)
204
			return true;
205

206
		if (type == LOCATE_TAG_FILE_TYPE)
207
			return false;
208 209 210
	}

	if (!song->tag)
211
		return false;
212

213 214
	memset(visited_types, 0, sizeof(visited_types));

Max Kellermann's avatar
Max Kellermann committed
215
	for (unsigned i = 0; i < song->tag->num_items; i++) {
216
		visited_types[song->tag->items[i]->type] = true;
217
		if (type != LOCATE_TAG_ANY_TYPE &&
218
		    song->tag->items[i]->type != type) {
219 220 221
			continue;
		}

222
		if (0 == strcmp(str, song->tag->items[i]->value))
223
			return true;
224 225
	}

226 227 228 229
	/** If the search critieron was not visited during the sweep
	 * through the song's tag, it means this field is absent from
	 * the tag or empty. Thus, if the searched string is also
	 *  empty (first char is a \0), then it's a match as well and
230
	 *  we should return true.
231
	 */
Max Kellermann's avatar
Max Kellermann committed
232
	if (!*str && !visited_types[type])
233
		return true;
234

235
	return false;
236 237
}

238
bool
239 240
locate_song_match(const struct song *song,
		  const struct locate_item_list *criteria)
241
{
242 243 244
	for (unsigned i = 0; i < criteria->length; i++)
		if (!locate_tag_match(song, criteria->items[i].tag,
				      criteria->items[i].needle))
245
			return false;
246

247
	return true;
248
}