From 579de4a4f77a32f7580bd7ee1563a9a635aec8aa Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 11 Jun 2009 15:19:44 +0200 Subject: [PATCH] mpdclient: return from mpdclient_cmd_crop() on error When the playlist is too small for crop, or when MPD is not playing, "return" from mpdclient_cmd_crop() instead of stepping over the command list block. This patch also simplifies the command list, and frees the mpd_Status object before all checks are done. --- src/mpdclient.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/mpdclient.c b/src/mpdclient.c index 7a9b661..9aab2fc 100644 --- a/src/mpdclient.c +++ b/src/mpdclient.c @@ -285,7 +285,8 @@ mpdclient_cmd_crop(mpdclient_t *c) { gint error; mpd_Status *status; - int length; + bool playing; + int length, current; mpd_sendStatusCommand(c->connection); status = mpd_getStatus(c->connection); @@ -293,27 +294,23 @@ mpdclient_cmd_crop(mpdclient_t *c) if (error) return error; - length = status->playlistLength - 1; + playing = status->state == MPD_STATUS_STATE_PLAY || + status->state == MPD_STATUS_STATE_PAUSE; + length = status->playlistLength; + current = status->song; - if (length <= 0) { - mpd_freeStatus(status); - } else if (status->state == 3 || status->state == 2) { - /* If playing or paused */ + mpd_freeStatus(status); - mpd_sendCommandListBegin( c->connection ); + if (!playing || length < 2) + return 0; - while (length >= 0) { - if (length != status->song) - mpd_sendDeleteCommand(c->connection, length); + mpd_sendCommandListBegin( c->connection ); - length--; - } + while (--length >= 0) + if (length != current) + mpd_sendDeleteCommand(c->connection, length); - mpd_sendCommandListEnd(c->connection); - mpd_freeStatus(status); - } else { - mpd_freeStatus(status); - } + mpd_sendCommandListEnd(c->connection); return mpdclient_finish_command(c); } -- 2.30.2