aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2012-08-07 23:59:17 +0200
committerMax Kellermann <max@duempel.org>2012-08-08 00:47:45 +0200
commit0823711106912fc75f1bdb2461243b12ba670f8b (patch)
treeee2bc012a9c3fb8e25b149a0b8f19d2421795256
parent860e9eb8c9ad07721aad4526046753d6fcc3b3ac (diff)
locate: add a per-item "fold_case" flag
Merge locate_song_search() and locate_song_match().
-rw-r--r--src/DatabaseCommands.cxx10
-rw-r--r--src/DatabasePlaylist.cxx2
-rw-r--r--src/DatabasePrint.cxx28
-rw-r--r--src/DatabasePrint.hxx6
-rw-r--r--src/DatabaseQueue.cxx23
-rw-r--r--src/DatabaseQueue.hxx5
-rw-r--r--src/command.c2
-rw-r--r--src/locate.c116
-rw-r--r--src/locate.h10
-rw-r--r--src/playlist_print.c7
-rw-r--r--src/playlist_print.h7
-rw-r--r--src/queue_print.c14
-rw-r--r--src/queue_print.h4
13 files changed, 55 insertions, 179 deletions
diff --git a/src/DatabaseCommands.cxx b/src/DatabaseCommands.cxx
index 913e338d..f54f0498 100644
--- a/src/DatabaseCommands.cxx
+++ b/src/DatabaseCommands.cxx
@@ -110,7 +110,7 @@ handle_search(struct client *client, int argc, char *argv[])
}
GError *error = NULL;
- enum command_return ret = searchForSongsIn(client, "", list, &error)
+ enum command_return ret = findSongsIn(client, "", list, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
@@ -131,8 +131,8 @@ handle_searchadd(struct client *client, int argc, char *argv[])
}
GError *error = NULL;
- enum command_return ret = search_add_songs(client->player_control,
- "", list, &error)
+ enum command_return ret =
+ findAddIn(client->player_control, "", list, &error)
? COMMAND_RETURN_OK
: print_error(client, error);
@@ -205,9 +205,9 @@ enum command_return
handle_list(struct client *client, int argc, char *argv[])
{
struct locate_item_list *conditionals;
- int tagType = locate_parse_type(argv[1]);
+ unsigned tagType = locate_parse_type(argv[1]);
- if (tagType < 0) {
+ if (tagType == TAG_NUM_OF_ITEM_TYPES) {
command_error(client, ACK_ERROR_ARG, "\"%s\" is not known", argv[1]);
return COMMAND_RETURN_ERROR;
}
diff --git a/src/DatabasePlaylist.cxx b/src/DatabasePlaylist.cxx
index c1524a33..bdd55fc5 100644
--- a/src/DatabasePlaylist.cxx
+++ b/src/DatabasePlaylist.cxx
@@ -55,7 +55,7 @@ SearchAddSong(const char *playlist_path_utf8,
const struct locate_item_list *criteria,
song &song, GError **error_r)
{
- return !locate_song_search(&song, criteria) ||
+ return !locate_list_song_match(&song, criteria) ||
spl_append_song(playlist_path_utf8, &song, error_r);
}
diff --git a/src/DatabasePrint.cxx b/src/DatabasePrint.cxx
index 0c5b2bb9..9f2f46d6 100644
--- a/src/DatabasePrint.cxx
+++ b/src/DatabasePrint.cxx
@@ -139,32 +139,10 @@ db_selection_print(struct client *client, const DatabaseSelection &selection,
}
static bool
-SearchPrintSong(struct client *client, const struct locate_item_list *criteria,
- song &song)
-{
- if (locate_song_search(&song, criteria))
- song_print_info(client, &song);
-
- return true;
-}
-
-bool
-searchForSongsIn(struct client *client, const char *uri,
- const struct locate_item_list *criteria,
- GError **error_r)
-{
- const DatabaseSelection selection(uri, true);
-
- using namespace std::placeholders;
- const auto f = std::bind(SearchPrintSong, client, criteria, _1);
- return GetDatabase()->Visit(selection, f, error_r);
-}
-
-static bool
MatchPrintSong(struct client *client, const struct locate_item_list *criteria,
song &song)
{
- if (locate_song_match(&song, criteria))
+ if (locate_list_song_match(&song, criteria))
song_print_info(client, &song);
return true;
@@ -197,7 +175,7 @@ static bool
stats_visitor_song(SearchStats &stats, const struct locate_item_list *criteria,
song &song)
{
- if (locate_song_match(&song, criteria)) {
+ if (locate_list_song_match(&song, criteria)) {
stats.numberOfSongs++;
stats.playTime += song_get_duration(&song);
}
@@ -282,7 +260,7 @@ unique_tags_visitor_song(struct client *client,
const struct locate_item_list *criteria,
StringSet &set, song &song)
{
- if (locate_song_match(&song, criteria))
+ if (locate_list_song_match(&song, criteria))
visitTag(client, set, song, tag_type);
return true;
diff --git a/src/DatabasePrint.hxx b/src/DatabasePrint.hxx
index b06d7716..95670502 100644
--- a/src/DatabasePrint.hxx
+++ b/src/DatabasePrint.hxx
@@ -46,12 +46,6 @@ printInfoForAllIn(struct client *client, const char *uri_utf8,
gcc_nonnull(1,2,3)
bool
-searchForSongsIn(struct client *client, const char *name,
- const struct locate_item_list *criteria,
- GError **error_r);
-
-gcc_nonnull(1,2,3)
-bool
findSongsIn(struct client *client, const char *name,
const struct locate_item_list *criteria,
GError **error_r);
diff --git a/src/DatabaseQueue.cxx b/src/DatabaseQueue.cxx
index 7bc18363..acbf9074 100644
--- a/src/DatabaseQueue.cxx
+++ b/src/DatabaseQueue.cxx
@@ -61,7 +61,7 @@ MatchAddSong(struct player_control *pc,
const struct locate_item_list *criteria,
song &song, GError **error_r)
{
- return !locate_song_match(&song, criteria) ||
+ return !locate_list_song_match(&song, criteria) ||
AddToQueue(pc, song, error_r);
}
@@ -75,24 +75,3 @@ findAddIn(struct player_control *pc, const char *uri,
const auto f = std::bind(MatchAddSong, pc, criteria, _1, _2);
return GetDatabase()->Visit(selection, f, error_r);
}
-
-static bool
-SearchAddSong(struct player_control *pc,
- const struct locate_item_list *criteria,
- song &song, GError **error_r)
-{
- return !locate_song_search(&song, criteria) ||
- AddToQueue(pc, song, error_r);
-}
-
-bool
-search_add_songs(struct player_control *pc, const char *uri,
- const struct locate_item_list *criteria,
- GError **error_r)
-{
- const DatabaseSelection selection(uri, true);
-
- using namespace std::placeholders;
- const auto f = std::bind(SearchAddSong, pc, criteria, _1, _2);
- return GetDatabase()->Visit(selection, f, error_r);
-}
diff --git a/src/DatabaseQueue.hxx b/src/DatabaseQueue.hxx
index a7ff13d6..546836ca 100644
--- a/src/DatabaseQueue.hxx
+++ b/src/DatabaseQueue.hxx
@@ -31,9 +31,4 @@ bool
findAddIn(struct player_control *pc, const char *name,
const struct locate_item_list *criteria, GError **error_r);
-gcc_nonnull(1,2,3)
-bool
-search_add_songs(struct player_control *pc, const char *uri,
- const struct locate_item_list *criteria, GError **error_r);
-
#endif
diff --git a/src/command.c b/src/command.c
index 4631e769..8da5c76b 100644
--- a/src/command.c
+++ b/src/command.c
@@ -702,7 +702,7 @@ handle_playlistsearch(struct client *client, int argc, char *argv[])
return COMMAND_RETURN_ERROR;
}
- playlist_print_search(client, &g_playlist, list);
+ playlist_print_find(client, &g_playlist, list);
locate_item_list_free(list);
diff --git a/src/locate.c b/src/locate.c
index c63c48c9..3cf842e9 100644
--- a/src/locate.c
+++ b/src/locate.c
@@ -25,6 +25,7 @@
#include <glib.h>
+#include <assert.h>
#include <stdlib.h>
#define LOCATE_TAG_FILE_KEY "file"
@@ -34,6 +35,9 @@
/* struct used for search, find, list queries */
struct locate_item {
uint8_t tag;
+
+ bool fold_case;
+
/* what we are looking for */
char *needle;
};
@@ -72,6 +76,7 @@ locate_item_init(struct locate_item *item,
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);
@@ -104,6 +109,7 @@ 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;
}
@@ -129,39 +135,39 @@ locate_item_list_parse(char *argv[], unsigned argc, bool fold_case)
gcc_pure
static bool
-locate_tag_search(const struct song *song, enum tag_type type, const char *str)
+locate_string_match(const struct locate_item *item, const char *value)
{
- 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);
+ assert(item != NULL);
+ assert(value != NULL);
- if (strstr(p, str))
- ret = true;
+ if (item->fold_case) {
+ char *p = g_utf8_casefold(value, -1);
+ const bool result = strstr(p, item->needle) != NULL;
g_free(p);
- if (ret == 1 || type == LOCATE_TAG_FILE_TYPE)
- return ret;
+ return result;
+ } else {
+ return strcmp(value, item->needle) == 0;
}
+}
- if (!song->tag)
- return false;
+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 < 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
@@ -170,76 +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 (!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 (result || item->tag == LOCATE_TAG_FILE_TYPE)
+ return result;
}
- /** 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;
diff --git a/src/locate.h b/src/locate.h
index fa5f2ce1..c012a9c6 100644
--- a/src/locate.h
+++ b/src/locate.h
@@ -54,13 +54,7 @@ locate_item_list_free(struct locate_item_list *list);
gcc_pure
gcc_nonnull(1,2)
bool
-locate_song_search(const struct song *song,
- const struct locate_item_list *criteria);
-
-gcc_pure
-gcc_nonnull(1,2)
-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);
#endif
diff --git a/src/playlist_print.c b/src/playlist_print.c
index a6bf84cc..78f1cb29 100644
--- a/src/playlist_print.c
+++ b/src/playlist_print.c
@@ -94,13 +94,6 @@ playlist_print_find(struct client *client, const struct playlist *playlist,
}
void
-playlist_print_search(struct client *client, const struct playlist *playlist,
- const struct locate_item_list *list)
-{
- queue_search(client, &playlist->queue, list);
-}
-
-void
playlist_print_changes_info(struct client *client,
const struct playlist *playlist,
uint32_t version)
diff --git a/src/playlist_print.h b/src/playlist_print.h
index d4f1911d..20d3703d 100644
--- a/src/playlist_print.h
+++ b/src/playlist_print.h
@@ -69,13 +69,6 @@ playlist_print_find(struct client *client, const struct playlist *playlist,
const struct locate_item_list *list);
/**
- * Search for songs in the playlist.
- */
-void
-playlist_print_search(struct client *client, const struct playlist *playlist,
- const struct locate_item_list *list);
-
-/**
* Print detailed changes since the specified playlist version.
*/
void
diff --git a/src/queue_print.c b/src/queue_print.c
index 032a7dec..d956bbf9 100644
--- a/src/queue_print.c
+++ b/src/queue_print.c
@@ -92,25 +92,13 @@ queue_print_changes_position(struct client *client, const struct queue *queue,
}
void
-queue_search(struct client *client, const struct queue *queue,
- const struct locate_item_list *criteria)
-{
- for (unsigned i = 0; i < queue_length(queue); i++) {
- const struct song *song = queue_get(queue, i);
-
- if (locate_song_search(song, criteria))
- queue_print_song_info(client, queue, i);
- }
-}
-
-void
queue_find(struct client *client, const struct queue *queue,
const struct locate_item_list *criteria)
{
for (unsigned i = 0; i < queue_length(queue); i++) {
const struct song *song = queue_get(queue, i);
- if (locate_song_match(song, criteria))
+ if (locate_list_song_match(song, criteria))
queue_print_song_info(client, queue, i);
}
}
diff --git a/src/queue_print.h b/src/queue_print.h
index 371e2041..28620c41 100644
--- a/src/queue_print.h
+++ b/src/queue_print.h
@@ -48,10 +48,6 @@ queue_print_changes_position(struct client *client, const struct queue *queue,
uint32_t version);
void
-queue_search(struct client *client, const struct queue *queue,
- const struct locate_item_list *criteria);
-
-void
queue_find(struct client *client, const struct queue *queue,
const struct locate_item_list *criteria);