aboutsummaryrefslogtreecommitdiffstats
path: root/src/locate.c
blob: 3cf842e90f7153adf1bad72f6fc5dbb4715e5f53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (C) 2003-2011 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.
 */

#include "config.h"
#include "locate.h"
#include "path.h"
#include "tag.h"
#include "song.h"

#include <glib.h>

#include <assert.h>
#include <stdlib.h>

#define LOCATE_TAG_FILE_KEY     "file"
#define LOCATE_TAG_FILE_KEY_OLD "filename"
#define LOCATE_TAG_ANY_KEY      "any"

/* struct used for search, find, list queries */
struct locate_item {
	uint8_t tag;

	bool fold_case;

	/* what we are looking for */
	char *needle;
};

/**
 * An array of struct locate_item objects.
 */
struct locate_item_list {
	/** number of items */
	unsigned length;

	/** this is a variable length array */
	struct locate_item items[1];
};

unsigned
locate_parse_type(const char *str)
{
	if (0 == g_ascii_strcasecmp(str, LOCATE_TAG_FILE_KEY) ||
	    0 == g_ascii_strcasecmp(str, LOCATE_TAG_FILE_KEY_OLD))
		return LOCATE_TAG_FILE_TYPE;

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

	return tag_name_parse_i(str);
}

static bool
locate_item_init(struct locate_item *item,
		 const char *type_string, const char *needle,
		 bool fold_case)
{
	item->tag = locate_parse_type(type_string);

	if (item->tag == TAG_NUM_OF_ITEM_TYPES)
		return false;

	item->fold_case = fold_case;
	item->needle = fold_case
		? g_utf8_casefold(needle, -1)
		: g_strdup(needle);

	return true;
}

void
locate_item_list_free(struct locate_item_list *list)
{
	for (unsigned i = 0; i < list->length; ++i)
		g_free(list->items[i].needle);

	g_free(list);
}

static struct locate_item_list *
locate_item_list_new(unsigned length)
{
	struct locate_item_list *list =
		g_malloc(sizeof(*list) - sizeof(list->items[0]) +
			 length * sizeof(list->items[0]));
	list->length = length;

	return list;
}

struct locate_item_list *
locate_item_list_new_single(unsigned tag, const char *needle)
{
	struct locate_item_list *list = locate_item_list_new(1);
	list->items[0].tag = tag;
	list->items[0].fold_case = false;
	list->items[0].needle = g_strdup(needle);
	return list;
}

struct locate_item_list *
locate_item_list_parse(char *argv[], unsigned argc, bool fold_case)
{
	if (argc == 0 || argc % 2 != 0)
		return NULL;

	struct locate_item_list *list = locate_item_list_new(argc / 2);

	for (unsigned i = 0; i < list->length; ++i) {
		if (!locate_item_init(&list->items[i], argv[i * 2],
				      argv[i * 2 + 1], fold_case)) {
			locate_item_list_free(list);
			return NULL;
		}
	}

	return list;
}

gcc_pure
static bool
locate_string_match(const struct locate_item *item, const char *value)
{
	assert(item != NULL);
	assert(value != NULL);

	if (item->fold_case) {
		char *p = g_utf8_casefold(value, -1);
		const bool result = strstr(p, item->needle) != NULL;
		g_free(p);
		return result;
	} else {
		return strcmp(value, item->needle) == 0;
	}
}

gcc_pure
static bool
locate_tag_match(const struct locate_item *item, const struct tag *tag)
{
	assert(item != NULL);
	assert(tag != NULL);

	bool visited_types[TAG_NUM_OF_ITEM_TYPES];
	memset(visited_types, 0, sizeof(visited_types));

	for (unsigned i = 0; i < tag->num_items; i++) {
		visited_types[tag->items[i]->type] = true;
		if (item->tag != LOCATE_TAG_ANY_TYPE &&
		    tag->items[i]->type != item->tag)
			continue;

		if (locate_string_match(item, tag->items[i]->value))
			return true;
	}

	/** 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
	 *  we should return true.
	 */
	if (*item->needle == 0 && item->tag != LOCATE_TAG_ANY_TYPE &&
	    !visited_types[item->tag])
		return true;

	return false;
}

gcc_pure
static bool
locate_song_match(const struct locate_item *item, const struct song *song)
{
	if (item->tag == LOCATE_TAG_FILE_TYPE ||
	    item->tag == LOCATE_TAG_ANY_TYPE) {
		char *uri = song_get_uri(song);
		const bool result = locate_string_match(item, uri);
		g_free(uri);

		if (result || item->tag == LOCATE_TAG_FILE_TYPE)
			return result;
	}

	return song->tag != NULL && locate_tag_match(item, song->tag);
}

bool
locate_list_song_match(const struct song *song,
		       const struct locate_item_list *criteria)
{
	for (unsigned i = 0; i < criteria->length; i++)
		if (!locate_song_match(&criteria->items[i], song))
			return false;

	return true;
}