aboutsummaryrefslogtreecommitdiffstats
path: root/src/locate.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/locate.c')
-rw-r--r--src/locate.c193
1 files changed, 83 insertions, 110 deletions
diff --git a/src/locate.c b/src/locate.c
index 9645da9cd..3cf842e90 100644
--- a/src/locate.c
+++ b/src/locate.c
@@ -25,13 +25,35 @@
#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"
-int
+/* 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) ||
@@ -41,23 +63,23 @@ locate_parse_type(const char *str)
if (0 == g_ascii_strcasecmp(str, LOCATE_TAG_ANY_KEY))
return LOCATE_TAG_ANY_TYPE;
- enum tag_type i = tag_name_parse_i(str);
- if (i != TAG_NUM_OF_ITEM_TYPES)
- return i;
-
- return -1;
+ return tag_name_parse_i(str);
}
static bool
locate_item_init(struct locate_item *item,
- const char *type_string, const char *needle)
+ const char *type_string, const char *needle,
+ bool fold_case)
{
item->tag = locate_parse_type(type_string);
- if (item->tag < 0)
+ if (item->tag == TAG_NUM_OF_ITEM_TYPES)
return false;
- item->needle = g_strdup(needle);
+ item->fold_case = fold_case;
+ item->needle = fold_case
+ ? g_utf8_casefold(needle, -1)
+ : g_strdup(needle);
return true;
}
@@ -71,28 +93,38 @@ locate_item_list_free(struct locate_item_list *list)
g_free(list);
}
-struct locate_item_list *
+static struct locate_item_list *
locate_item_list_new(unsigned length)
{
struct locate_item_list *list =
- g_malloc0(sizeof(*list) - sizeof(list->items[0]) +
- length * sizeof(list->items[0]));
+ 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_parse(char *argv[], int argc)
+locate_item_list_new_single(unsigned tag, const char *needle)
{
- if (argc % 2 != 0)
+ 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])) {
+ argv[i * 2 + 1], fold_case)) {
locate_item_list_free(list);
return NULL;
}
@@ -101,61 +133,41 @@ locate_item_list_parse(char *argv[], int argc)
return list;
}
-struct locate_item_list *
-locate_item_list_casefold(const struct locate_item_list *list)
+gcc_pure
+static bool
+locate_string_match(const struct locate_item *item, const char *value)
{
- struct locate_item_list *new_list = locate_item_list_new(list->length);
+ assert(item != NULL);
+ assert(value != NULL);
- for (unsigned i = 0; i < list->length; i++){
- new_list->items[i].needle =
- g_utf8_casefold(list->items[i].needle, -1);
- new_list->items[i].tag = list->items[i].tag;
+ 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;
}
-
- return new_list;
-}
-
-void
-locate_item_free(struct locate_item *item)
-{
- g_free(item->needle);
- g_free(item);
}
+gcc_pure
static bool
-locate_tag_search(const struct song *song, enum tag_type type, const char *str)
+locate_tag_match(const struct locate_item *item, const struct tag *tag)
{
- bool ret = false;
-
- if (type == LOCATE_TAG_FILE_TYPE || type == LOCATE_TAG_ANY_TYPE) {
- char *uri = song_get_uri(song);
- char *p = g_utf8_casefold(uri, -1);
- g_free(uri);
-
- if (strstr(p, str))
- ret = true;
- g_free(p);
- if (ret == 1 || type == LOCATE_TAG_FILE_TYPE)
- return ret;
- }
-
- if (!song->tag)
- return false;
+ 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 < song->tag->num_items && !ret; i++) {
- visited_types[song->tag->items[i]->type] = true;
- if (type != LOCATE_TAG_ANY_TYPE &&
- song->tag->items[i]->type != type) {
+ 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;
- }
- char *duplicate = g_utf8_casefold(song->tag->items[i]->value, -1);
- if (*str && strstr(duplicate, str))
- ret = true;
- g_free(duplicate);
+ if (locate_string_match(item, tag->items[i]->value))
+ return true;
}
/** If the search critieron was not visited during the sweep
@@ -164,75 +176,36 @@ locate_tag_search(const struct song *song, enum tag_type type, const char *str)
* empty (first char is a \0), then it's a match as well and
* we should return true.
*/
- if (!*str && !visited_types[type])
+ if (*item->needle == 0 && item->tag != LOCATE_TAG_ANY_TYPE &&
+ !visited_types[item->tag])
return true;
- return ret;
-}
-
-bool
-locate_song_search(const struct song *song,
- const struct locate_item_list *criteria)
-{
- for (unsigned i = 0; i < criteria->length; i++)
- if (!locate_tag_search(song, criteria->items[i].tag,
- criteria->items[i].needle))
- return false;
-
- return true;
+ return false;
}
+gcc_pure
static bool
-locate_tag_match(const struct song *song, enum tag_type type, const char *str)
+locate_song_match(const struct locate_item *item, const struct song *song)
{
- if (type == LOCATE_TAG_FILE_TYPE || type == LOCATE_TAG_ANY_TYPE) {
+ if (item->tag == LOCATE_TAG_FILE_TYPE ||
+ item->tag == LOCATE_TAG_ANY_TYPE) {
char *uri = song_get_uri(song);
- bool matches = strcmp(str, uri) == 0;
+ const bool result = locate_string_match(item, uri);
g_free(uri);
- if (matches)
- return true;
-
- if (type == LOCATE_TAG_FILE_TYPE)
- return false;
+ if (result || item->tag == LOCATE_TAG_FILE_TYPE)
+ return result;
}
- if (!song->tag)
- return false;
-
- bool visited_types[TAG_NUM_OF_ITEM_TYPES];
- memset(visited_types, 0, sizeof(visited_types));
-
- for (unsigned i = 0; i < song->tag->num_items; i++) {
- visited_types[song->tag->items[i]->type] = true;
- if (type != LOCATE_TAG_ANY_TYPE &&
- song->tag->items[i]->type != type) {
- continue;
- }
-
- if (0 == strcmp(str, song->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 (!*str && !visited_types[type])
- return true;
-
- return false;
+ return song->tag != NULL && locate_tag_match(item, song->tag);
}
bool
-locate_song_match(const struct song *song,
- const struct locate_item_list *criteria)
+locate_list_song_match(const struct song *song,
+ const struct locate_item_list *criteria)
{
for (unsigned i = 0; i < criteria->length; i++)
- if (!locate_tag_match(song, criteria->items[i].tag,
- criteria->items[i].needle))
+ if (!locate_song_match(&criteria->items[i], song))
return false;
return true;