locate: add a per-item "fold_case" flag

Merge locate_song_search() and locate_song_match().
This commit is contained in:
Max Kellermann 2012-08-07 23:59:17 +02:00
parent 860e9eb8c9
commit 0823711106
13 changed files with 59 additions and 183 deletions

View File

@ -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;
}

View File

@ -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);
}

View File

@ -138,33 +138,11 @@ db_selection_print(struct client *client, const DatabaseSelection &selection,
return GetDatabase()->Visit(selection, d, s, p, error_r);
}
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;

View File

@ -44,12 +44,6 @@ bool
printInfoForAllIn(struct client *client, const char *uri_utf8,
GError **error_r);
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,

View File

@ -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);
}

View File

@ -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

View File

@ -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);

View File

@ -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,95 +135,38 @@ 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;
assert(item != NULL);
assert(value != NULL);
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;
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;
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) {
continue;
}
char *duplicate = g_utf8_casefold(song->tag->items[i]->value, -1);
if (*str && strstr(duplicate, str))
ret = true;
g_free(duplicate);
}
/** 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 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;
}
gcc_pure
static bool
locate_tag_match(const struct song *song, enum tag_type type, const char *str)
locate_tag_match(const struct locate_item *item, const struct tag *tag)
{
if (type == LOCATE_TAG_FILE_TYPE || type == LOCATE_TAG_ANY_TYPE) {
char *uri = song_get_uri(song);
bool matches = strcmp(str, uri) == 0;
g_free(uri);
if (matches)
return true;
if (type == LOCATE_TAG_FILE_TYPE)
return false;
}
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; 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;
}
if (0 == strcmp(str, song->tag->items[i]->value))
if (locate_string_match(item, tag->items[i]->value))
return true;
}
@ -227,19 +176,36 @@ locate_tag_match(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 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_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;

View File

@ -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

View File

@ -93,13 +93,6 @@ playlist_print_find(struct client *client, const struct playlist *playlist,
queue_find(client, &playlist->queue, list);
}
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,

View File

@ -68,13 +68,6 @@ void
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.
*/

View File

@ -91,18 +91,6 @@ queue_print_changes_position(struct client *client, const struct queue *queue,
i, queue_position_to_id(queue, i));
}
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)
@ -110,7 +98,7 @@ queue_find(struct client *client, const struct queue *queue,
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);
}
}

View File

@ -47,10 +47,6 @@ void
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);