Commit 1b4de327 authored by Eric Wong's avatar Eric Wong

storedPlaylist: remove the needless StoredPlaylist struct and just use List

Abstractions should reduce the amount of code and make things easier to follow. The StoredPlaylist struct did not do that, so get rid of it and just use our standard linked list implementation instead. diffstat agrees with me that the abstraction is pointless: 3 files changed, 60 insertions(+), 90 deletions(-) git-svn-id: https://svn.musicpd.org/mpd/trunk@7120 09075e82-0dd4-0310-85a5-a0d7c8717e4f
parent 09936358
...@@ -1516,12 +1516,12 @@ int getPlaylistSongId(int song) ...@@ -1516,12 +1516,12 @@ int getPlaylistSongId(int song)
int PlaylistInfo(int fd, char *utf8file, int detail) int PlaylistInfo(int fd, char *utf8file, int detail)
{ {
ListNode *node; ListNode *node;
StoredPlaylist sp; List *list;
if (loadStoredPlaylist(fd, &sp, utf8file) < 0) if (!(list = loadStoredPlaylist(fd, utf8file)))
return -1; return -1;
node = sp.list->firstNode; node = list->firstNode;
while (node != NULL) { while (node != NULL) {
char *temp = node->data; char *temp = node->data;
int wrote = 0; int wrote = 0;
...@@ -1541,19 +1541,19 @@ int PlaylistInfo(int fd, char *utf8file, int detail) ...@@ -1541,19 +1541,19 @@ int PlaylistInfo(int fd, char *utf8file, int detail)
node = node->nextNode; node = node->nextNode;
} }
freeStoredPlaylist(&sp); freeList(list);
return 0; return 0;
} }
int loadPlaylist(int fd, char *utf8file) int loadPlaylist(int fd, char *utf8file)
{ {
ListNode *node; ListNode *node;
StoredPlaylist sp; List *list;
if (loadStoredPlaylist(fd, &sp, utf8file) < 0) if (!(list = loadStoredPlaylist(fd, utf8file)))
return -1; return -1;
node = sp.list->firstNode; node = list->firstNode;
while (node != NULL) { while (node != NULL) {
char *temp = node->data; char *temp = node->data;
if ((addToPlaylist(STDERR_FILENO, temp, 0)) < 0) { if ((addToPlaylist(STDERR_FILENO, temp, 0)) < 0) {
...@@ -1575,7 +1575,7 @@ int loadPlaylist(int fd, char *utf8file) ...@@ -1575,7 +1575,7 @@ int loadPlaylist(int fd, char *utf8file)
node = node->nextNode; node = node->nextNode;
} }
freeStoredPlaylist(&sp); freeList(list);
return 0; return 0;
} }
......
...@@ -29,27 +29,22 @@ ...@@ -29,27 +29,22 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
static unsigned int lengthOfStoredPlaylist(StoredPlaylist *sp) static ListNode *nodeOfStoredPlaylist(List *list, int index)
{
return sp->list->numberOfNodes;
}
static ListNode *nodeOfStoredPlaylist(StoredPlaylist *sp, int index)
{ {
int forward; int forward;
ListNode *node; ListNode *node;
int i; int i;
if (index >= lengthOfStoredPlaylist(sp) || index < 0) if (index >= list->numberOfNodes || index < 0)
return NULL; return NULL;
if (index > lengthOfStoredPlaylist(sp)/2) { if (index > (list->numberOfNodes/2)) {
forward = 0; forward = 0;
node = sp->list->lastNode; node = list->lastNode;
i = lengthOfStoredPlaylist(sp) - 1; i = list->numberOfNodes - 1;
} else { } else {
forward = 1; forward = 1;
node = sp->list->firstNode; node = list->firstNode;
i = 0; i = 0;
} }
...@@ -69,7 +64,7 @@ static ListNode *nodeOfStoredPlaylist(StoredPlaylist *sp, int index) ...@@ -69,7 +64,7 @@ static ListNode *nodeOfStoredPlaylist(StoredPlaylist *sp, int index)
return NULL; return NULL;
} }
static int writeStoredPlaylistToPath(StoredPlaylist *sp, const char *fspath) static int writeStoredPlaylistToPath(int fd, List *list, const char *fspath)
{ {
ListNode *node; ListNode *node;
FILE *file; FILE *file;
...@@ -80,12 +75,12 @@ static int writeStoredPlaylistToPath(StoredPlaylist *sp, const char *fspath) ...@@ -80,12 +75,12 @@ static int writeStoredPlaylistToPath(StoredPlaylist *sp, const char *fspath)
while (!(file = fopen(fspath, "w")) && errno == EINTR); while (!(file = fopen(fspath, "w")) && errno == EINTR);
if (file == NULL) { if (file == NULL) {
commandError(sp->fd, ACK_ERROR_NO_EXIST, "could not open file " commandError(fd, ACK_ERROR_NO_EXIST, "could not open file "
"\"%s\": %s", fspath, strerror(errno)); "\"%s\": %s", fspath, strerror(errno));
return -1; return -1;
} }
node = sp->list->firstNode; node = list->firstNode;
while (node != NULL) { while (node != NULL) {
char path_max_tmp[MPD_PATH_MAX]; char path_max_tmp[MPD_PATH_MAX];
...@@ -100,40 +95,26 @@ static int writeStoredPlaylistToPath(StoredPlaylist *sp, const char *fspath) ...@@ -100,40 +95,26 @@ static int writeStoredPlaylistToPath(StoredPlaylist *sp, const char *fspath)
return 0; return 0;
} }
static void appendSongToStoredPlaylist(StoredPlaylist *sp, Song *song) List *loadStoredPlaylist(int fd, const char *utf8path)
{
char path_max_tmp[MPD_PATH_MAX];
get_song_url(path_max_tmp, song);
insertInListWithoutKey(sp->list, xstrdup(path_max_tmp));
}
static void initStoredPlaylist(int fd, StoredPlaylist *sp, const char *utf8name)
{
utf8_to_fs_playlist_path(sp->fs_path, utf8name);
sp->list = makeList(DEFAULT_FREE_DATA_FUNC, 0);
sp->fd = fd;
}
int loadStoredPlaylist(int fd, StoredPlaylist *sp, const char *utf8path)
{ {
List *list;
FILE *file; FILE *file;
char buffer[MPD_PATH_MAX]; char buffer[MPD_PATH_MAX];
char path_max_tmp[MPD_PATH_MAX]; char path_max_tmp[MPD_PATH_MAX];
const size_t musicDir_len = strlen(musicDir); const size_t musicDir_len = strlen(musicDir);
if (!valid_playlist_name(fd, utf8path)) if (!valid_playlist_name(fd, utf8path))
return -1; return NULL;
utf8_to_fs_playlist_path(path_max_tmp, utf8path); utf8_to_fs_playlist_path(path_max_tmp, utf8path);
while (!(file = fopen(path_max_tmp, "r")) && errno == EINTR); while (!(file = fopen(path_max_tmp, "r")) && errno == EINTR);
if (file == NULL) { if (file == NULL) {
commandError(fd, ACK_ERROR_NO_EXIST, "could not open file " commandError(fd, ACK_ERROR_NO_EXIST, "could not open file "
"\"%s\": %s", path_max_tmp, strerror(errno)); "\"%s\": %s", path_max_tmp, strerror(errno));
return -1; return NULL;
} }
initStoredPlaylist(fd, sp, utf8path); list = makeList(DEFAULT_FREE_DATA_FUNC, 0);
while (myFgets(buffer, sizeof(buffer), file)) { while (myFgets(buffer, sizeof(buffer), file)) {
char *s = buffer; char *s = buffer;
...@@ -145,61 +126,57 @@ int loadStoredPlaylist(int fd, StoredPlaylist *sp, const char *utf8path) ...@@ -145,61 +126,57 @@ int loadStoredPlaylist(int fd, StoredPlaylist *sp, const char *utf8path)
!strncmp(s, musicDir, musicDir_len)) !strncmp(s, musicDir, musicDir_len))
memmove(s, s + musicDir_len + 1, memmove(s, s + musicDir_len + 1,
strlen(s + musicDir_len + 1) + 1); strlen(s + musicDir_len + 1) + 1);
if ((song = getSongFromDB(s))) if ((song = getSongFromDB(s))) {
appendSongToStoredPlaylist(sp, song); get_song_url(path_max_tmp, song);
else if (isValidRemoteUtf8Url(s)) insertInListWithoutKey(list, xstrdup(path_max_tmp));
insertInListWithoutKey(sp->list, xstrdup(s)); } else if (isValidRemoteUtf8Url(s))
insertInListWithoutKey(list, xstrdup(s));
} }
while (fclose(file) && errno == EINTR); while (fclose(file) && errno == EINTR);
return 0; return list;
}
void freeStoredPlaylist(StoredPlaylist *sp)
{
if (sp->list)
freeList(sp->list);
} }
static int moveSongInStoredPlaylist(int fd, StoredPlaylist *sp, int src, int dest) static int moveSongInStoredPlaylist(int fd, List *list, int src, int dest)
{ {
ListNode *srcNode, *destNode; ListNode *srcNode, *destNode;
if (src >= lengthOfStoredPlaylist(sp) || dest >= lengthOfStoredPlaylist(sp) || src < 0 || dest < 0 || src == dest) { if (src >= list->numberOfNodes || dest >= list->numberOfNodes ||
src < 0 || dest < 0 || src == dest) {
commandError(fd, ACK_ERROR_ARG, "argument out of range"); commandError(fd, ACK_ERROR_ARG, "argument out of range");
return -1; return -1;
} }
srcNode = nodeOfStoredPlaylist(sp, src); srcNode = nodeOfStoredPlaylist(list, src);
if (!srcNode) if (!srcNode)
return -1; return -1;
destNode = nodeOfStoredPlaylist(sp, dest); destNode = nodeOfStoredPlaylist(list, dest);
/* remove src */ /* remove src */
if (srcNode->prevNode) if (srcNode->prevNode)
srcNode->prevNode->nextNode = srcNode->nextNode; srcNode->prevNode->nextNode = srcNode->nextNode;
else else
sp->list->firstNode = srcNode->nextNode; list->firstNode = srcNode->nextNode;
if (srcNode->nextNode) if (srcNode->nextNode)
srcNode->nextNode->prevNode = srcNode->prevNode; srcNode->nextNode->prevNode = srcNode->prevNode;
else else
sp->list->lastNode = srcNode->prevNode; list->lastNode = srcNode->prevNode;
/* this is all a bit complicated - but I tried to /* this is all a bit complicated - but I tried to
* maintain the same order stuff is moved as in the * maintain the same order stuff is moved as in the
* real playlist */ * real playlist */
if (dest == 0) { if (dest == 0) {
sp->list->firstNode->prevNode = srcNode; list->firstNode->prevNode = srcNode;
srcNode->nextNode = sp->list->firstNode; srcNode->nextNode = list->firstNode;
srcNode->prevNode = NULL; srcNode->prevNode = NULL;
sp->list->firstNode = srcNode; list->firstNode = srcNode;
} else if ((dest + 1) == lengthOfStoredPlaylist(sp)) { } else if ((dest + 1) == list->numberOfNodes) {
sp->list->lastNode->nextNode = srcNode; list->lastNode->nextNode = srcNode;
srcNode->nextNode = NULL; srcNode->nextNode = NULL;
srcNode->prevNode = sp->list->lastNode; srcNode->prevNode = list->lastNode;
sp->list->lastNode = srcNode; list->lastNode = srcNode;
} else { } else {
if (destNode == NULL) { if (destNode == NULL) {
/* this shouldn't be happening. */ /* this shouldn't be happening. */
...@@ -225,25 +202,25 @@ static int moveSongInStoredPlaylist(int fd, StoredPlaylist *sp, int src, int des ...@@ -225,25 +202,25 @@ static int moveSongInStoredPlaylist(int fd, StoredPlaylist *sp, int src, int des
int moveSongInStoredPlaylistByPath(int fd, const char *utf8path, int moveSongInStoredPlaylistByPath(int fd, const char *utf8path,
int src, int dest) int src, int dest)
{ {
StoredPlaylist sp; List *list;
if (loadStoredPlaylist(fd, &sp, utf8path) < 0) { if (!(list = loadStoredPlaylist(fd, utf8path))) {
commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist"); commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist");
return -1; return -1;
} }
if (moveSongInStoredPlaylist(fd, &sp, src, dest) != 0) { if (moveSongInStoredPlaylist(fd, list, src, dest) != 0) {
freeStoredPlaylist(&sp); freeList(list);
return -1; return -1;
} }
if (writeStoredPlaylistToPath(&sp, utf8path) != 0) { if (writeStoredPlaylistToPath(fd, list, utf8path) != 0) {
commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist"); commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist");
freeStoredPlaylist(&sp); freeList(list);
return -1; return -1;
} }
freeStoredPlaylist(&sp); freeList(list);
return 0; return 0;
} }
...@@ -267,41 +244,41 @@ int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path) ...@@ -267,41 +244,41 @@ int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path)
return 0; return 0;
} }
static int removeOneSongFromStoredPlaylist(int fd, StoredPlaylist *sp, int pos) static int removeOneSongFromStoredPlaylist(int fd, List *list, int pos)
{ {
ListNode *node = nodeOfStoredPlaylist(sp, pos); ListNode *node = nodeOfStoredPlaylist(list, pos);
if (!node) { if (!node) {
commandError(fd, ACK_ERROR_ARG, commandError(fd, ACK_ERROR_ARG,
"could not find song at position"); "could not find song at position");
return -1; return -1;
} }
deleteNodeFromList(sp->list, node); deleteNodeFromList(list, node);
return 0; return 0;
} }
int removeOneSongFromStoredPlaylistByPath(int fd, const char *utf8path, int pos) int removeOneSongFromStoredPlaylistByPath(int fd, const char *utf8path, int pos)
{ {
StoredPlaylist sp; List *list;
if (loadStoredPlaylist(fd, &sp, utf8path) < 0) { if (!(list = loadStoredPlaylist(fd, utf8path))) {
commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist"); commandError(fd, ACK_ERROR_UNKNOWN, "could not open playlist");
return -1; return -1;
} }
if (removeOneSongFromStoredPlaylist(fd, &sp, pos) != 0) { if (removeOneSongFromStoredPlaylist(fd, list, pos) != 0) {
freeStoredPlaylist(&sp); freeList(list);
return -1; return -1;
} }
if (writeStoredPlaylistToPath(&sp, utf8path) != 0) { if (writeStoredPlaylistToPath(fd, list, utf8path) != 0) {
commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist"); commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist");
freeStoredPlaylist(&sp); freeList(list);
return -1; return -1;
} }
freeStoredPlaylist(&sp); freeList(list);
return 0; return 0;
} }
......
...@@ -24,14 +24,7 @@ ...@@ -24,14 +24,7 @@
#include "playlist.h" #include "playlist.h"
#include "path.h" #include "path.h"
typedef struct _storedPlaylist { List *loadStoredPlaylist(int fd, const char *utf8path);
List *list;
char fs_path[MPD_PATH_MAX];
int fd;
} StoredPlaylist;
int loadStoredPlaylist(int fd, StoredPlaylist *sp, const char *utf8path);
void freeStoredPlaylist(StoredPlaylist *sp);
int moveSongInStoredPlaylistByPath(int fd, const char *utf8path, int src, int dest); int moveSongInStoredPlaylistByPath(int fd, const char *utf8path, int src, int dest);
int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path); int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment