commit c68f913e25b52cb755d84702c0ae8f047f30d34a
parent ec6157f5d4e6924d949e312cd82f2a56cd0fefce
Author: sin <sin@2f30.org>
Date: Fri, 2 Jan 2015 15:48:21 +0000
Add support to remove playlist entries
Diffstat:
3 files changed, 44 insertions(+), 0 deletions(-)
diff --git a/cmd.c b/cmd.c
@@ -252,6 +252,30 @@ cmdempty(int fd, char *arg)
static void
cmdremove(int fd, char *arg)
{
+ Song *s;
+ const char *errstr;
+ int id;
+
+ if (!arg[0]) {
+ s = getcursong();
+ stopsong(s);
+ } else {
+ id = strtonum(arg, 0, INT_MAX, &errstr);
+ if (errstr) {
+ dprintf(fd, "ERR invalid song id\n");
+ return;
+ }
+ s = findsongid(id);
+ if (!s) {
+ dprintf(fd, "ERR cannot find song with given id\n");
+ return;
+ }
+ }
+ if (rmplaylist(s->id) < 0) {
+ dprintf(fd, "ERR failed to remove song\n");
+ return;
+ }
+ dprintf(fd, "OK\n");
}
static void
diff --git a/playlist.c b/playlist.c
@@ -36,6 +36,25 @@ addplaylist(int id)
return s;
}
+int
+rmplaylist(int id)
+{
+ Song *s;
+ int i;
+
+ for (i = 0; i < playlist.nsongs; i++)
+ if (playlist.songs[i]->id == id)
+ break;
+ if (i == playlist.nsongs)
+ return -1;
+ s = getnextsong();
+ memmove(&playlist.songs[i], &playlist.songs[i+1],
+ (playlist.nsongs - i - 1) * sizeof(*playlist.songs[i]));
+ putcursong(s);
+ playlist.nsongs--;
+ return 0;
+}
+
Song *
getnextsong(void)
{
diff --git a/sad.h b/sad.h
@@ -84,6 +84,7 @@ int docmd(int);
/* playlist.c */
Song *addplaylist(int);
+int rmplaylist(int);
Song *getnextsong(void);
Song *getprevsong(void);
Song *getcursong(void);