From cff9790ef2756708af32dc43ec298126e25a78ca Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 2 Oct 2009 19:49:30 +0200 Subject: [PATCH] filelist: added constructor filelist_new_recv() This receives a filelist from a mpd_connection. This code has existed before, and this patch merges those. --- src/filelist.c | 17 +++++++++++++++++ src/filelist.h | 16 ++++++++++++++++ src/mpdclient.c | 20 ++++++-------------- src/screen_search.c | 8 +------- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/filelist.c b/src/filelist.c index 575d407..2ce928c 100644 --- a/src/filelist.c +++ b/src/filelist.c @@ -217,3 +217,20 @@ filelist_find_directory(struct filelist *filelist, const char *name) return -1; } + +void +filelist_recv(struct filelist *filelist, struct mpd_connection *connection) +{ + struct mpd_entity *entity; + + while ((entity = mpd_recv_entity(connection)) != NULL) + filelist_append(filelist, entity); +} + +struct filelist * +filelist_new_recv(struct mpd_connection *connection) +{ + struct filelist *filelist = filelist_new(); + filelist_recv(filelist, connection); + return filelist; +} diff --git a/src/filelist.h b/src/filelist.h index 0f1f0c0..ee6e606 100644 --- a/src/filelist.h +++ b/src/filelist.h @@ -22,6 +22,7 @@ #include +struct mpd_connection; struct mpd_song; struct filelist_entry { @@ -82,4 +83,19 @@ filelist_find_song(struct filelist *flist, const struct mpd_song *song); int filelist_find_directory(struct filelist *filelist, const char *name); +/** + * Receives entities from the connection, and appends them to the + * specified filelist. This does not finish the response, and does + * not check for errors. + */ +void +filelist_recv(struct filelist *filelist, struct mpd_connection *connection); + +/** + * Creates a new filelist and receives entities from the connection. + * This does not finish the response, and does not check for errors. + */ +struct filelist * +filelist_new_recv(struct mpd_connection *connection); + #endif diff --git a/src/mpdclient.c b/src/mpdclient.c index 9a9154c..1c21785 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -708,25 +708,21 @@ mpdclient_playlist_update_changes(struct mpdclient *c) /*** Filelist functions *****************************************************/ /****************************************************************************/ +static struct filelist * +mpdclient_recv_filelist_response(struct mpdclient *c); + struct filelist * mpdclient_filelist_get(struct mpdclient *c, const gchar *path) { struct filelist *filelist; - struct mpd_entity *entity; if (MPD_ERROR(c)) return NULL; mpd_send_list_meta(c->connection, path); - filelist = filelist_new(); - - while ((entity = mpd_recv_entity(c->connection)) != NULL) - filelist_append(filelist, entity); - - if (!mpdclient_finish_command(c)) { - filelist_free(filelist); + filelist = mpdclient_recv_filelist_response(c); + if (filelist == NULL) return NULL; - } filelist_sort_dir_play(filelist, compare_filelistentry); @@ -737,12 +733,8 @@ static struct filelist * mpdclient_recv_filelist_response(struct mpdclient *c) { struct filelist *filelist; - struct mpd_entity *entity; - - filelist = filelist_new(); - while ((entity = mpd_recv_entity(c->connection)) != NULL) - filelist_append(filelist, entity); + filelist = filelist_new_recv(c->connection); if (!mpdclient_finish_command(c)) { filelist_free(filelist); diff --git a/src/screen_search.c b/src/screen_search.c index 0339944..8dfec1f 100644 --- a/src/screen_search.c +++ b/src/screen_search.c @@ -204,7 +204,6 @@ search_advanced_query(struct mpd_connection *connection, char *query) int table[10]; char *arg[10]; struct filelist *fl = NULL; - struct mpd_entity *entity; advanced_search_mode = FALSE; if (strchr(query, ':') == NULL) @@ -273,12 +272,7 @@ search_advanced_query(struct mpd_connection *connection, char *query) } mpd_search_commit(connection); - - fl = filelist_new(); - - while ((entity = mpd_recv_entity(connection)) != NULL) - filelist_append(fl, entity); - + fl = filelist_new_recv(connection); if (!mpd_response_finish(connection)) { filelist_free(fl); fl = NULL; -- 2.30.2