From 36b1c08ad7245a403578731c5d1240da8fc61d72 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 29 Aug 2012 19:12:26 +0200 Subject: locate: rename to SongFilter.cxx --- Makefile.am | 5 +- src/DatabaseCommands.cxx | 2 +- src/DatabasePrint.cxx | 2 +- src/Directory.cxx | 2 +- src/QueueCommands.cxx | 2 +- src/QueuePrint.cxx | 2 +- src/SongFilter.cxx | 215 ++++++++++++++++++++++++++++++++++++++++ src/SongFilter.hxx | 60 +++++++++++ src/db/SimpleDatabasePlugin.cxx | 2 +- src/locate.c | 212 --------------------------------------- src/locate.h | 60 ----------- 11 files changed, 283 insertions(+), 281 deletions(-) create mode 100644 src/SongFilter.cxx create mode 100644 src/SongFilter.hxx delete mode 100644 src/locate.c delete mode 100644 src/locate.h diff --git a/Makefile.am b/Makefile.am index aa221c209..b846183e4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -212,7 +212,6 @@ mpd_headers = \ src/string_util.h \ src/volume.h \ src/zeroconf.h src/zeroconf-internal.h \ - src/locate.h \ src/stored_playlist.h \ src/timer.h \ src/archive_api.h \ @@ -362,7 +361,7 @@ src_mpd_SOURCES = \ src/utils.c \ src/string_util.c \ src/volume.c \ - src/locate.c \ + src/SongFilter.cxx src/SongFilter.hxx \ src/stored_playlist.c \ src/timer.c @@ -1070,7 +1069,7 @@ test_DumpDatabase_SOURCES = test/DumpDatabase.cxx \ src/Song.cxx src/song_sort.c src/song_save.c \ src/tag.c src/tag_pool.c src/tag_save.c \ src/path.c \ - src/locate.c \ + src/SongFilter.cxx \ src/text_file.c \ src/conf.c src/tokenizer.c src/utils.c src/string_util.c diff --git a/src/DatabaseCommands.cxx b/src/DatabaseCommands.cxx index 288980468..047d3a550 100644 --- a/src/DatabaseCommands.cxx +++ b/src/DatabaseCommands.cxx @@ -27,9 +27,9 @@ #include "client_internal.h" #include "tag.h" #include "uri.h" +#include "SongFilter.hxx" extern "C" { -#include "locate.h" #include "protocol/result.h" } diff --git a/src/DatabasePrint.cxx b/src/DatabasePrint.cxx index 621679729..9ff833ffd 100644 --- a/src/DatabasePrint.cxx +++ b/src/DatabasePrint.cxx @@ -20,9 +20,9 @@ #include "config.h" #include "DatabasePrint.hxx" #include "DatabaseSelection.hxx" +#include "SongFilter.hxx" extern "C" { -#include "locate.h" #include "database.h" #include "client.h" #include "song.h" diff --git a/src/Directory.cxx b/src/Directory.cxx index e366906e8..d4564d69e 100644 --- a/src/Directory.cxx +++ b/src/Directory.cxx @@ -19,6 +19,7 @@ #include "config.h" #include "directory.h" +#include "SongFilter.hxx" extern "C" { #include "song.h" @@ -27,7 +28,6 @@ extern "C" { #include "path.h" #include "util/list_sort.h" #include "db_lock.h" -#include "locate.h" } #include diff --git a/src/QueueCommands.cxx b/src/QueueCommands.cxx index de00cb18d..4801d2326 100644 --- a/src/QueueCommands.cxx +++ b/src/QueueCommands.cxx @@ -21,6 +21,7 @@ #include "QueueCommands.hxx" #include "CommandError.h" #include "DatabaseQueue.hxx" +#include "SongFilter.hxx" extern "C" { #include "protocol/argparser.h" @@ -29,7 +30,6 @@ extern "C" { #include "playlist_print.h" #include "ls.h" #include "uri.h" -#include "locate.h" #include "client_internal.h" #include "client_file.h" } diff --git a/src/QueuePrint.cxx b/src/QueuePrint.cxx index bc7f86e52..0a61cc5dd 100644 --- a/src/QueuePrint.cxx +++ b/src/QueuePrint.cxx @@ -18,13 +18,13 @@ */ #include "config.h" +#include "SongFilter.hxx" extern "C" { #include "queue_print.h" #include "queue.h" #include "song.h" #include "song_print.h" -#include "locate.h" #include "client.h" #include "mapper.h" } diff --git a/src/SongFilter.cxx b/src/SongFilter.cxx new file mode 100644 index 000000000..c99776e18 --- /dev/null +++ b/src/SongFilter.cxx @@ -0,0 +1,215 @@ +/* + * Copyright (C) 2003-2012 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 "SongFilter.hxx" +#include "path.h" +#include "song.h" + +extern "C" { +#include "tag.h" +} + +#include + +#include +#include + +#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 = (struct locate_item_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; +} diff --git a/src/SongFilter.hxx b/src/SongFilter.hxx new file mode 100644 index 000000000..38c1dae67 --- /dev/null +++ b/src/SongFilter.hxx @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2003-2012 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. + */ + +#ifndef MPD_SONG_FILTER_HXX +#define MPD_SONG_FILTER_HXX + +#include "gcc.h" + +#include +#include + +#define LOCATE_TAG_FILE_TYPE TAG_NUM_OF_ITEM_TYPES+10 +#define LOCATE_TAG_ANY_TYPE TAG_NUM_OF_ITEM_TYPES+20 + +struct locate_item_list; +struct song; + +/** + * @return #TAG_NUM_OF_ITEM_TYPES on error + */ +gcc_pure +unsigned +locate_parse_type(const char *str); + +gcc_malloc +struct locate_item_list * +locate_item_list_new_single(unsigned tag, const char *needle); + +/* return number of items or -1 on error */ +gcc_nonnull(1) +struct locate_item_list * +locate_item_list_parse(char *argv[], unsigned argc, bool fold_case); + +gcc_nonnull(1) +void +locate_item_list_free(struct locate_item_list *list); + +gcc_pure +gcc_nonnull(1,2) +bool +locate_list_song_match(const struct song *song, + const struct locate_item_list *criteria); + +#endif diff --git a/src/db/SimpleDatabasePlugin.cxx b/src/db/SimpleDatabasePlugin.cxx index 94318f4e9..2d357eb98 100644 --- a/src/db/SimpleDatabasePlugin.cxx +++ b/src/db/SimpleDatabasePlugin.cxx @@ -21,13 +21,13 @@ #include "SimpleDatabasePlugin.hxx" #include "DatabaseSelection.hxx" #include "DatabaseHelpers.hxx" +#include "SongFilter.hxx" extern "C" { #include "db_error.h" #include "db_save.h" #include "db_lock.h" #include "conf.h" -#include "locate.h" } #include "directory.h" diff --git a/src/locate.c b/src/locate.c deleted file mode 100644 index 3cf842e90..000000000 --- a/src/locate.c +++ /dev/null @@ -1,212 +0,0 @@ -/* - * 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 - -#include -#include - -#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; -} diff --git a/src/locate.h b/src/locate.h deleted file mode 100644 index c012a9c63..000000000 --- a/src/locate.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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. - */ - -#ifndef MPD_LOCATE_H -#define MPD_LOCATE_H - -#include "gcc.h" - -#include -#include - -#define LOCATE_TAG_FILE_TYPE TAG_NUM_OF_ITEM_TYPES+10 -#define LOCATE_TAG_ANY_TYPE TAG_NUM_OF_ITEM_TYPES+20 - -struct locate_item_list; -struct song; - -/** - * @return #TAG_NUM_OF_ITEM_TYPES on error - */ -gcc_pure -unsigned -locate_parse_type(const char *str); - -gcc_malloc -struct locate_item_list * -locate_item_list_new_single(unsigned tag, const char *needle); - -/* return number of items or -1 on error */ -gcc_nonnull(1) -struct locate_item_list * -locate_item_list_parse(char *argv[], unsigned argc, bool fold_case); - -gcc_nonnull(1) -void -locate_item_list_free(struct locate_item_list *list); - -gcc_pure -gcc_nonnull(1,2) -bool -locate_list_song_match(const struct song *song, - const struct locate_item_list *criteria); - -#endif -- cgit v1.2.3