Code

mpdclient: return from mpdclient_cmd_crop() on error
authorMax Kellermann <max@duempel.org>
Thu, 11 Jun 2009 13:19:44 +0000 (15:19 +0200)
committerMax Kellermann <max@duempel.org>
Thu, 11 Jun 2009 13:19:44 +0000 (15:19 +0200)
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

index 7a9b661d4a305798463521858f4d0eb6a6f83af5..9aab2fc39b99a3e7d5fafdfeee52f3a3f9ffc2d0 100644 (file)
@@ -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);
 }