aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2008-01-01 10:09:36 +0000
committerEric Wong <normalperson@yhbt.net>2008-01-01 10:09:36 +0000
commit1b4de32706f6c43c4833146a98b0689cbe624fe6 (patch)
tree7a63f6c2036c4ebaac076fdb9126bafcd04e27d3 /src
parent09936358fa8098444019fc2a470b1685af1a19de (diff)
downloadmpd-1b4de32706f6c43c4833146a98b0689cbe624fe6.tar.gz
mpd-1b4de32706f6c43c4833146a98b0689cbe624fe6.tar.xz
mpd-1b4de32706f6c43c4833146a98b0689cbe624fe6.zip
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
Diffstat (limited to '')
-rw-r--r--src/playlist.c16
-rw-r--r--src/storedPlaylist.c125
-rw-r--r--src/storedPlaylist.h9
3 files changed, 60 insertions, 90 deletions
diff --git a/src/playlist.c b/src/playlist.c
index 3087fd444..4689cc2de 100644
--- a/src/playlist.c
+++ b/src/playlist.c
@@ -1516,12 +1516,12 @@ int getPlaylistSongId(int song)
int PlaylistInfo(int fd, char *utf8file, int detail)
{
ListNode *node;
- StoredPlaylist sp;
+ List *list;
- if (loadStoredPlaylist(fd, &sp, utf8file) < 0)
+ if (!(list = loadStoredPlaylist(fd, utf8file)))
return -1;
- node = sp.list->firstNode;
+ node = list->firstNode;
while (node != NULL) {
char *temp = node->data;
int wrote = 0;
@@ -1541,19 +1541,19 @@ int PlaylistInfo(int fd, char *utf8file, int detail)
node = node->nextNode;
}
- freeStoredPlaylist(&sp);
+ freeList(list);
return 0;
}
int loadPlaylist(int fd, char *utf8file)
{
ListNode *node;
- StoredPlaylist sp;
+ List *list;
- if (loadStoredPlaylist(fd, &sp, utf8file) < 0)
+ if (!(list = loadStoredPlaylist(fd, utf8file)))
return -1;
- node = sp.list->firstNode;
+ node = list->firstNode;
while (node != NULL) {
char *temp = node->data;
if ((addToPlaylist(STDERR_FILENO, temp, 0)) < 0) {
@@ -1575,7 +1575,7 @@ int loadPlaylist(int fd, char *utf8file)
node = node->nextNode;
}
- freeStoredPlaylist(&sp);
+ freeList(list);
return 0;
}
diff --git a/src/storedPlaylist.c b/src/storedPlaylist.c
index 3e846dce9..153a0fcdb 100644
--- a/src/storedPlaylist.c
+++ b/src/storedPlaylist.c
@@ -29,27 +29,22 @@
#include <string.h>
#include <errno.h>
-static unsigned int lengthOfStoredPlaylist(StoredPlaylist *sp)
-{
- return sp->list->numberOfNodes;
-}
-
-static ListNode *nodeOfStoredPlaylist(StoredPlaylist *sp, int index)
+static ListNode *nodeOfStoredPlaylist(List *list, int index)
{
int forward;
ListNode *node;
int i;
- if (index >= lengthOfStoredPlaylist(sp) || index < 0)
+ if (index >= list->numberOfNodes || index < 0)
return NULL;
- if (index > lengthOfStoredPlaylist(sp)/2) {
+ if (index > (list->numberOfNodes/2)) {
forward = 0;
- node = sp->list->lastNode;
- i = lengthOfStoredPlaylist(sp) - 1;
+ node = list->lastNode;
+ i = list->numberOfNodes - 1;
} else {
forward = 1;
- node = sp->list->firstNode;
+ node = list->firstNode;
i = 0;
}
@@ -69,7 +64,7 @@ static ListNode *nodeOfStoredPlaylist(StoredPlaylist *sp, int index)
return NULL;
}
-static int writeStoredPlaylistToPath(StoredPlaylist *sp, const char *fspath)
+static int writeStoredPlaylistToPath(int fd, List *list, const char *fspath)
{
ListNode *node;
FILE *file;
@@ -80,12 +75,12 @@ static int writeStoredPlaylistToPath(StoredPlaylist *sp, const char *fspath)
while (!(file = fopen(fspath, "w")) && errno == EINTR);
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));
return -1;
}
- node = sp->list->firstNode;
+ node = list->firstNode;
while (node != NULL) {
char path_max_tmp[MPD_PATH_MAX];
@@ -100,40 +95,26 @@ static int writeStoredPlaylistToPath(StoredPlaylist *sp, const char *fspath)
return 0;
}
-static void appendSongToStoredPlaylist(StoredPlaylist *sp, Song *song)
-{
- 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 *loadStoredPlaylist(int fd, const char *utf8path)
{
+ List *list;
FILE *file;
char buffer[MPD_PATH_MAX];
char path_max_tmp[MPD_PATH_MAX];
const size_t musicDir_len = strlen(musicDir);
if (!valid_playlist_name(fd, utf8path))
- return -1;
+ return NULL;
utf8_to_fs_playlist_path(path_max_tmp, utf8path);
while (!(file = fopen(path_max_tmp, "r")) && errno == EINTR);
if (file == NULL) {
commandError(fd, ACK_ERROR_NO_EXIST, "could not open file "
"\"%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)) {
char *s = buffer;
@@ -145,61 +126,57 @@ int loadStoredPlaylist(int fd, StoredPlaylist *sp, const char *utf8path)
!strncmp(s, musicDir, musicDir_len))
memmove(s, s + musicDir_len + 1,
strlen(s + musicDir_len + 1) + 1);
- if ((song = getSongFromDB(s)))
- appendSongToStoredPlaylist(sp, song);
- else if (isValidRemoteUtf8Url(s))
- insertInListWithoutKey(sp->list, xstrdup(s));
+ if ((song = getSongFromDB(s))) {
+ get_song_url(path_max_tmp, song);
+ insertInListWithoutKey(list, xstrdup(path_max_tmp));
+ } else if (isValidRemoteUtf8Url(s))
+ insertInListWithoutKey(list, xstrdup(s));
}
while (fclose(file) && errno == EINTR);
- return 0;
-}
-
-void freeStoredPlaylist(StoredPlaylist *sp)
-{
- if (sp->list)
- freeList(sp->list);
+ return 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;
- 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");
return -1;
}
- srcNode = nodeOfStoredPlaylist(sp, src);
+ srcNode = nodeOfStoredPlaylist(list, src);
if (!srcNode)
return -1;
- destNode = nodeOfStoredPlaylist(sp, dest);
+ destNode = nodeOfStoredPlaylist(list, dest);
/* remove src */
if (srcNode->prevNode)
srcNode->prevNode->nextNode = srcNode->nextNode;
else
- sp->list->firstNode = srcNode->nextNode;
+ list->firstNode = srcNode->nextNode;
if (srcNode->nextNode)
srcNode->nextNode->prevNode = srcNode->prevNode;
else
- sp->list->lastNode = srcNode->prevNode;
+ list->lastNode = srcNode->prevNode;
/* this is all a bit complicated - but I tried to
* maintain the same order stuff is moved as in the
* real playlist */
if (dest == 0) {
- sp->list->firstNode->prevNode = srcNode;
- srcNode->nextNode = sp->list->firstNode;
+ list->firstNode->prevNode = srcNode;
+ srcNode->nextNode = list->firstNode;
srcNode->prevNode = NULL;
- sp->list->firstNode = srcNode;
- } else if ((dest + 1) == lengthOfStoredPlaylist(sp)) {
- sp->list->lastNode->nextNode = srcNode;
+ list->firstNode = srcNode;
+ } else if ((dest + 1) == list->numberOfNodes) {
+ list->lastNode->nextNode = srcNode;
srcNode->nextNode = NULL;
- srcNode->prevNode = sp->list->lastNode;
- sp->list->lastNode = srcNode;
+ srcNode->prevNode = list->lastNode;
+ list->lastNode = srcNode;
} else {
if (destNode == NULL) {
/* this shouldn't be happening. */
@@ -225,25 +202,25 @@ static int moveSongInStoredPlaylist(int fd, StoredPlaylist *sp, int src, int des
int moveSongInStoredPlaylistByPath(int fd, const char *utf8path,
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");
return -1;
}
- if (moveSongInStoredPlaylist(fd, &sp, src, dest) != 0) {
- freeStoredPlaylist(&sp);
+ if (moveSongInStoredPlaylist(fd, list, src, dest) != 0) {
+ freeList(list);
return -1;
}
- if (writeStoredPlaylistToPath(&sp, utf8path) != 0) {
+ if (writeStoredPlaylistToPath(fd, list, utf8path) != 0) {
commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist");
- freeStoredPlaylist(&sp);
+ freeList(list);
return -1;
}
- freeStoredPlaylist(&sp);
+ freeList(list);
return 0;
}
@@ -267,41 +244,41 @@ int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path)
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) {
commandError(fd, ACK_ERROR_ARG,
"could not find song at position");
return -1;
}
- deleteNodeFromList(sp->list, node);
+ deleteNodeFromList(list, node);
return 0;
}
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");
return -1;
}
- if (removeOneSongFromStoredPlaylist(fd, &sp, pos) != 0) {
- freeStoredPlaylist(&sp);
+ if (removeOneSongFromStoredPlaylist(fd, list, pos) != 0) {
+ freeList(list);
return -1;
}
- if (writeStoredPlaylistToPath(&sp, utf8path) != 0) {
+ if (writeStoredPlaylistToPath(fd, list, utf8path) != 0) {
commandError(fd, ACK_ERROR_UNKNOWN, "failed to save playlist");
- freeStoredPlaylist(&sp);
+ freeList(list);
return -1;
}
- freeStoredPlaylist(&sp);
+ freeList(list);
return 0;
}
diff --git a/src/storedPlaylist.h b/src/storedPlaylist.h
index 698e5bb2d..9b50ddc2e 100644
--- a/src/storedPlaylist.h
+++ b/src/storedPlaylist.h
@@ -24,14 +24,7 @@
#include "playlist.h"
#include "path.h"
-typedef struct _storedPlaylist {
- List *list;
- char fs_path[MPD_PATH_MAX];
- int fd;
-} StoredPlaylist;
-
-int loadStoredPlaylist(int fd, StoredPlaylist *sp, const char *utf8path);
-void freeStoredPlaylist(StoredPlaylist *sp);
+List *loadStoredPlaylist(int fd, const char *utf8path);
int moveSongInStoredPlaylistByPath(int fd, const char *utf8path, int src, int dest);
int removeAllFromStoredPlaylistByPath(int fd, const char *utf8path);