diff options
-rw-r--r-- | src/command.c | 101 | ||||
-rw-r--r-- | src/dbUtils.c | 11 | ||||
-rw-r--r-- | src/locate.c | 52 | ||||
-rw-r--r-- | src/locate.h | 24 | ||||
-rw-r--r-- | src/queue_print.c | 10 |
5 files changed, 111 insertions, 87 deletions
diff --git a/src/command.c b/src/command.c index 79a6fa7ba..744aec8e7 100644 --- a/src/command.c +++ b/src/command.c @@ -833,22 +833,23 @@ static enum command_return handle_find(struct client *client, int argc, char *argv[]) { int ret; - struct locate_item *items; - int numItems = locate_item_list_parse(argv + 1, - argc - 1, - &items); + struct locate_item_list *list = + locate_item_list_parse(argv + 1, argc - 1); + + if (list == NULL || list->length == 0) { + if (list != NULL) + locate_item_list_free(list); - if (numItems <= 0) { command_error(client, ACK_ERROR_ARG, "incorrect arguments"); return COMMAND_RETURN_ERROR; } - ret = findSongsIn(client, NULL, numItems, items); + ret = findSongsIn(client, NULL, list->length, list->items); if (ret == -1) command_error(client, ACK_ERROR_NO_EXIST, "directory or file not found"); - locate_item_list_free(numItems, items); + locate_item_list_free(list); return ret; } @@ -857,22 +858,23 @@ static enum command_return handle_search(struct client *client, int argc, char *argv[]) { int ret; - struct locate_item *items; - int numItems = locate_item_list_parse(argv + 1, - argc - 1, - &items); + struct locate_item_list *list = + locate_item_list_parse(argv + 1, argc - 1); + + if (list == NULL || list->length == 0) { + if (list != NULL) + locate_item_list_free(list); - if (numItems <= 0) { command_error(client, ACK_ERROR_ARG, "incorrect arguments"); return COMMAND_RETURN_ERROR; } - ret = searchForSongsIn(client, NULL, numItems, items); + ret = searchForSongsIn(client, NULL, list->length, list->items); if (ret == -1) command_error(client, ACK_ERROR_NO_EXIST, "directory or file not found"); - locate_item_list_free(numItems, items); + locate_item_list_free(list); return ret; } @@ -881,22 +883,23 @@ static enum command_return handle_count(struct client *client, int argc, char *argv[]) { int ret; - struct locate_item *items; - int numItems = locate_item_list_parse(argv + 1, - argc - 1, - &items); + struct locate_item_list *list = + locate_item_list_parse(argv + 1, argc - 1); + + if (list == NULL || list->length == 0) { + if (list != NULL) + locate_item_list_free(list); - if (numItems <= 0) { command_error(client, ACK_ERROR_ARG, "incorrect arguments"); return COMMAND_RETURN_ERROR; } - ret = searchStatsForSongsIn(client, NULL, numItems, items); + ret = searchStatsForSongsIn(client, NULL, list->length, list->items); if (ret == -1) command_error(client, ACK_ERROR_NO_EXIST, "directory or file not found"); - locate_item_list_free(numItems, items); + locate_item_list_free(list); return ret; } @@ -904,19 +907,20 @@ handle_count(struct client *client, int argc, char *argv[]) static enum command_return handle_playlistfind(struct client *client, int argc, char *argv[]) { - struct locate_item *items; - int numItems = locate_item_list_parse(argv + 1, - argc - 1, - &items); + struct locate_item_list *list = + locate_item_list_parse(argv + 1, argc - 1); + + if (list == NULL || list->length == 0) { + if (list != NULL) + locate_item_list_free(list); - if (numItems <= 0) { command_error(client, ACK_ERROR_ARG, "incorrect arguments"); return COMMAND_RETURN_ERROR; } - queue_find(client, playlist_get_queue(), numItems, items); + queue_find(client, playlist_get_queue(), list->length, list->items); - locate_item_list_free(numItems, items); + locate_item_list_free(list); return COMMAND_RETURN_OK; } @@ -924,19 +928,20 @@ handle_playlistfind(struct client *client, int argc, char *argv[]) static enum command_return handle_playlistsearch(struct client *client, int argc, char *argv[]) { - struct locate_item *items; - int numItems = locate_item_list_parse(argv + 1, - argc - 1, - &items); + struct locate_item_list *list = + locate_item_list_parse(argv + 1, argc - 1); + + if (list == NULL || list->length == 0) { + if (list != NULL) + locate_item_list_free(list); - if (numItems <= 0) { command_error(client, ACK_ERROR_ARG, "incorrect arguments"); return COMMAND_RETURN_ERROR; } - queue_search(client, playlist_get_queue(), numItems, items); + queue_search(client, playlist_get_queue(), list->length, list->items); - locate_item_list_free(numItems, items); + locate_item_list_free(list); return COMMAND_RETURN_OK; } @@ -1111,8 +1116,7 @@ handle_clearerror(G_GNUC_UNUSED struct client *client, static enum command_return handle_list(struct client *client, int argc, char *argv[]) { - int numConditionals; - struct locate_item *conditionals = NULL; + struct locate_item_list *conditionals; int tagType = locate_parse_type(argv[1]); int ret; @@ -1135,25 +1139,26 @@ handle_list(struct client *client, int argc, char *argv[]) mpdTagItemKeys[TAG_ITEM_ALBUM]); return COMMAND_RETURN_ERROR; } - conditionals = locate_item_new(mpdTagItemKeys[TAG_ITEM_ARTIST], - argv[2]); - numConditionals = 1; - } else { - numConditionals = - locate_item_list_parse(argv + 2, - argc - 2, &conditionals); - if (numConditionals < 0) { + locate_item_list_parse(argv + 1, argc - 1); + + conditionals = locate_item_list_new(1); + conditionals->items[0].tag = TAG_ITEM_ARTIST; + conditionals->items[0].needle = g_strdup(argv[2]); + } else { + conditionals = + locate_item_list_parse(argv + 2, argc - 2); + if (conditionals == NULL) { command_error(client, ACK_ERROR_ARG, "not able to parse args"); return COMMAND_RETURN_ERROR; } } - ret = listAllUniqueTags(client, tagType, numConditionals, conditionals); + ret = listAllUniqueTags(client, tagType, conditionals->length, + conditionals->items); - if (conditionals) - locate_item_list_free(numConditionals, conditionals); + locate_item_list_free(conditionals); if (ret == -1) command_error(client, ACK_ERROR_NO_EXIST, diff --git a/src/dbUtils.c b/src/dbUtils.c index bfe262b85..e130ba680 100644 --- a/src/dbUtils.c +++ b/src/dbUtils.c @@ -92,20 +92,21 @@ searchForSongsIn(struct client *client, const char *name, { int ret; int i; - struct locate_item *new_items = - g_memdup(items, sizeof(items[0]) * numItems); + struct locate_item_list *new_list; struct search_data data; + new_list = locate_item_list_new(numItems); for (i = 0; i < numItems; i++) - new_items[i].needle = g_utf8_casefold(new_items[i].needle, -1); + new_list->items[i].needle = + g_utf8_casefold(items[i].needle, -1); data.client = client; data.array.numItems = numItems; - data.array.items = new_items; + data.array.items = new_list->items; ret = db_walk(name, searchInDirectory, NULL, &data); - locate_item_list_free(numItems, new_items); + locate_item_list_free(new_list); return ret; } diff --git a/src/locate.c b/src/locate.c index 2833a5f8b..838378797 100644 --- a/src/locate.c +++ b/src/locate.c @@ -76,45 +76,45 @@ locate_item_new(const char *type_string, const char *needle) } void -locate_item_list_free(int count, struct locate_item *array) +locate_item_list_free(struct locate_item_list *list) { - int i; - - for (i = 0; i < count; i++) - free(array[i].needle); + for (unsigned i = 0; i < list->length; ++i) + g_free(list->items[i].needle); - free(array); + g_free(list); } -int -locate_item_list_parse(char *argv[], int argc, struct locate_item **arrayRet) +struct locate_item_list * +locate_item_list_new(unsigned length) { - int i, j; - struct locate_item *item; + struct locate_item_list *list; - if (argc == 0) - return 0; + list = g_malloc0(sizeof(*list) - sizeof(list->items[0]) + + length * sizeof(list->items[0])); + list->length = length; - if (argc % 2 != 0) - return -1; + return list; +} - *arrayRet = g_new(struct locate_item, argc / 2); +struct locate_item_list * +locate_item_list_parse(char *argv[], int argc) +{ + struct locate_item_list *list; - for (i = 0, item = *arrayRet; i < argc / 2; i++, item++) { - if (!locate_item_init(item, argv[i * 2], argv[i * 2 + 1])) - goto fail; - } + if (argc % 2 != 0) + return NULL; - return argc / 2; + list = locate_item_list_new(argc / 2); -fail: - for (j = 0; j < i; j++) { - free((*arrayRet)[j].needle); + for (unsigned i = 0; i < list->length; ++i) { + if (!locate_item_init(&list->items[i], argv[i * 2], + argv[i * 2 + 1])) { + locate_item_list_free(list); + return NULL; + } } - free(*arrayRet); - *arrayRet = NULL; - return -1; + return list; } void diff --git a/src/locate.h b/src/locate.h index 7442cb3a3..d0083a426 100644 --- a/src/locate.h +++ b/src/locate.h @@ -34,6 +34,17 @@ struct locate_item { 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]; +}; + int locate_parse_type(const char *str); @@ -41,12 +52,19 @@ locate_parse_type(const char *str); struct locate_item * locate_item_new(const char *type_string, const char *needle); +/** + * Allocates a new struct locate_item_list, and initializes all + * members with zero bytes. + */ +struct locate_item_list * +locate_item_list_new(unsigned length); + /* return number of items or -1 on error */ -int -locate_item_list_parse(char *argv[], int argc, struct locate_item **arrayRet); +struct locate_item_list * +locate_item_list_parse(char *argv[], int argc); void -locate_item_list_free(int count, struct locate_item *array); +locate_item_list_free(struct locate_item_list *list); void locate_item_free(struct locate_item *item); diff --git a/src/queue_print.c b/src/queue_print.c index b7510a15d..3c0437ca0 100644 --- a/src/queue_print.c +++ b/src/queue_print.c @@ -84,20 +84,20 @@ queue_search(struct client *client, const struct queue *queue, unsigned num_items, const struct locate_item *items) { unsigned i; - struct locate_item *new_items = - g_memdup(items, sizeof(items[0]) * num_items); + struct locate_item_list *new_list = locate_item_list_new(num_items); for (i = 0; i < num_items; i++) - new_items[i].needle = g_utf8_casefold(new_items[i].needle, -1); + new_list->items[i].needle = + g_utf8_casefold(items[i].needle, -1); for (i = 0; i < queue_length(queue); i++) { const struct song *song = queue_get(queue, i); - if (locate_song_search(song, num_items, new_items)) + if (locate_song_search(song, num_items, new_list->items)) queue_print_song_info(client, queue, i); } - locate_item_list_free(num_items, new_items); + locate_item_list_free(new_list); } void |