aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-10-23 09:54:28 +0200
committerMax Kellermann <max@duempel.org>2008-10-23 09:54:28 +0200
commit39f0c41fbf94f2ac078478867ff95a92a62480fb (patch)
treecedd79631c8a7dc61cfd5f68e419aaf4c4e37ac9
parente172874cc65f39e79ae895d35382d2fa3061d3dd (diff)
downloadmpd-39f0c41fbf94f2ac078478867ff95a92a62480fb.tar.gz
mpd-39f0c41fbf94f2ac078478867ff95a92a62480fb.tar.xz
mpd-39f0c41fbf94f2ac078478867ff95a92a62480fb.zip
stored_playlist: spl_load() returns GPtrArray
Don't use our deprecated linked list library, use GLib's GPtrArray instead.
Diffstat (limited to '')
-rw-r--r--src/playlist.c25
-rw-r--r--src/stored_playlist.c190
-rw-r--r--src/stored_playlist.h6
3 files changed, 75 insertions, 146 deletions
diff --git a/src/playlist.c b/src/playlist.c
index 49be62c54..4b7ea6074 100644
--- a/src/playlist.c
+++ b/src/playlist.c
@@ -35,6 +35,7 @@
#include "stored_playlist.h"
#include "ack.h"
#include "idle.h"
+#include "list.h"
#include "os_compat.h"
#include <glib.h>
@@ -1295,15 +1296,13 @@ unsigned getPlaylistSongId(unsigned song)
int PlaylistInfo(struct client *client, const char *utf8file, int detail)
{
- ListNode *node;
- List *list;
+ GPtrArray *list;
if (!(list = spl_load(utf8file)))
return -1;
- node = list->firstNode;
- while (node != NULL) {
- char *temp = node->data;
+ for (unsigned i = 0; i < list->len; ++i) {
+ const char *temp = g_ptr_array_index(list, i);
int wrote = 0;
if (detail) {
@@ -1317,25 +1316,21 @@ int PlaylistInfo(struct client *client, const char *utf8file, int detail)
if (!wrote) {
client_printf(client, SONG_FILE "%s\n", temp);
}
-
- node = node->nextNode;
}
- freeList(list);
+ spl_free(list);
return 0;
}
enum playlist_result loadPlaylist(struct client *client, const char *utf8file)
{
- ListNode *node;
- List *list;
+ GPtrArray *list;
if (!(list = spl_load(utf8file)))
return PLAYLIST_RESULT_NO_SUCH_LIST;
- node = list->firstNode;
- while (node != NULL) {
- char *temp = node->data;
+ for (unsigned i = 0; i < list->len; ++i) {
+ const char *temp = g_ptr_array_index(list, i);
if ((addToPlaylist(temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
/* for windows compatibility, convert slashes */
char *temp2 = xstrdup(temp);
@@ -1351,11 +1346,9 @@ enum playlist_result loadPlaylist(struct client *client, const char *utf8file)
}
free(temp2);
}
-
- node = node->nextNode;
}
- freeList(list);
+ spl_free(list);
return PLAYLIST_RESULT_SUCCESS;
}
diff --git a/src/stored_playlist.c b/src/stored_playlist.c
index 9e9895e9d..cb81962d1 100644
--- a/src/stored_playlist.c
+++ b/src/stored_playlist.c
@@ -106,46 +106,9 @@ spl_list_free(GPtrArray *list)
g_ptr_array_free(list, true);
}
-static ListNode *
-spl_get_index(List *list, int idx)
-{
- int forward;
- ListNode *node;
- int i;
-
- if (idx >= list->numberOfNodes || idx < 0)
- return NULL;
-
- if (idx > (list->numberOfNodes/2)) {
- forward = 0;
- node = list->lastNode;
- i = list->numberOfNodes - 1;
- } else {
- forward = 1;
- node = list->firstNode;
- i = 0;
- }
-
- while (node != NULL) {
- if (i == idx)
- return node;
-
- if (forward) {
- i++;
- node = node->nextNode;
- } else {
- i--;
- node = node->prevNode;
- }
- }
-
- return NULL;
-}
-
static enum playlist_result
-spl_save(List *list, const char *utf8path)
+spl_save(GPtrArray *list, const char *utf8path)
{
- ListNode *node;
FILE *file;
char path_max_tmp[MPD_PATH_MAX];
@@ -157,21 +120,20 @@ spl_save(List *list, const char *utf8path)
if (file == NULL)
return PLAYLIST_RESULT_ERRNO;
- node = list->firstNode;
- while (node != NULL) {
- playlist_print_uri(file, (const char *)node->data);
- node = node->nextNode;
+ for (unsigned i = 0; i < list->len; ++i) {
+ const char *uri = g_ptr_array_index(list, i);
+ playlist_print_uri(file, uri);
}
while (fclose(file) != 0 && errno == EINTR);
return PLAYLIST_RESULT_SUCCESS;
}
-List *
+GPtrArray *
spl_load(const char *utf8path)
{
- List *list;
FILE *file;
+ GPtrArray *list;
char buffer[MPD_PATH_MAX];
char path_max_tmp[MPD_PATH_MAX];
@@ -183,7 +145,7 @@ spl_load(const char *utf8path)
if (file == NULL)
return NULL;
- list = makeList(DEFAULT_FREE_DATA_FUNC, 0);
+ list = g_ptr_array_new();
while (myFgets(buffer, sizeof(buffer), file)) {
char *s = buffer;
@@ -192,9 +154,7 @@ spl_load(const char *utf8path)
if (*s == PLAYLIST_COMMENT)
continue;
- if (isValidRemoteUtf8Url(s))
- insertInListWithoutKey(list, xstrdup(s));
- else {
+ if (!isValidRemoteUtf8Url(s)) {
struct song *song;
path_utf8 = map_fs_to_utf8(s, path_max_tmp);
@@ -205,11 +165,12 @@ spl_load(const char *utf8path)
if (song == NULL)
continue;
- song_get_url(song, path_max_tmp);
- insertInListWithoutKey(list, xstrdup(path_max_tmp));
+ s = song_get_url(song, path_max_tmp);
}
- if (list->numberOfNodes >= playlist_max_length)
+ g_ptr_array_add(list, xstrdup(s));
+
+ if (list->len >= playlist_max_length)
break;
}
@@ -217,85 +178,67 @@ spl_load(const char *utf8path)
return list;
}
-static int
-spl_move_index_internal(List *list, int src, int dest)
+void
+spl_free(GPtrArray *list)
{
- ListNode *srcNode, *destNode;
-
- if (src >= list->numberOfNodes || dest >= list->numberOfNodes ||
- src < 0 || dest < 0 || src == dest)
- return -1;
-
- srcNode = spl_get_index(list, src);
- if (!srcNode)
- return -1;
-
- destNode = spl_get_index(list, dest);
-
- /* remove src */
- if (srcNode->prevNode)
- srcNode->prevNode->nextNode = srcNode->nextNode;
- else
- list->firstNode = srcNode->nextNode;
-
- if (srcNode->nextNode)
- srcNode->nextNode->prevNode = srcNode->prevNode;
- else
- 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) {
- list->firstNode->prevNode = srcNode;
- srcNode->nextNode = list->firstNode;
- srcNode->prevNode = NULL;
- list->firstNode = srcNode;
- } else if ((dest + 1) == list->numberOfNodes) {
- list->lastNode->nextNode = srcNode;
- srcNode->nextNode = NULL;
- srcNode->prevNode = list->lastNode;
- list->lastNode = srcNode;
- } else {
- if (destNode == NULL) {
- /* this shouldn't be happening. */
- return -1;
- }
-
- if (src > dest) {
- destNode->prevNode->nextNode = srcNode;
- srcNode->prevNode = destNode->prevNode;
- srcNode->nextNode = destNode;
- destNode->prevNode = srcNode;
- } else {
- destNode->nextNode->prevNode = srcNode;
- srcNode->prevNode = destNode;
- srcNode->nextNode = destNode->nextNode;
- destNode->nextNode = srcNode;
- }
+ for (unsigned i = 0; i < list->len; ++i) {
+ char *uri = g_ptr_array_index(list, i);
+ g_free(uri);
}
- idle_add(IDLE_STORED_PLAYLIST);
- return 0;
+ g_ptr_array_free(list, true);
+}
+
+static char *
+spl_remove_index_internal(GPtrArray *list, unsigned idx)
+{
+ char *uri;
+
+ assert(idx < list->len);
+
+ uri = g_ptr_array_remove_index(list, idx);
+ assert(uri != NULL);
+ return uri;
+}
+
+static void
+spl_insert_index_internal(GPtrArray *list, unsigned idx, char *uri)
+{
+ assert(idx <= list->len);
+
+ g_ptr_array_add(list, uri);
+
+ memmove(list->pdata + idx + 1, list->pdata + idx,
+ (list->len - idx - 1) * sizeof(list->pdata[0]));
+ g_ptr_array_index(list, idx) = uri;
}
enum playlist_result
spl_move_index(const char *utf8path, unsigned src, unsigned dest)
{
- List *list;
+ GPtrArray *list;
+ char *uri;
enum playlist_result result;
+ if (src == dest)
+ /* this doesn't check whether the playlist exists, but
+ what the hell.. */
+ return PLAYLIST_RESULT_SUCCESS;
+
if (!(list = spl_load(utf8path)))
return PLAYLIST_RESULT_NO_SUCH_LIST;
- if (spl_move_index_internal(list, src, dest) != 0) {
- freeList(list);
+ if (src >= list->len || dest >= list->len) {
+ spl_free(list);
return PLAYLIST_RESULT_BAD_RANGE;
}
+ uri = spl_remove_index_internal(list, src);
+ spl_insert_index_internal(list, dest, uri);
+
result = spl_save(list, utf8path);
- freeList(list);
+ spl_free(list);
idle_add(IDLE_STORED_PLAYLIST);
return result;
@@ -322,35 +265,26 @@ spl_clear(const char *utf8path)
return PLAYLIST_RESULT_SUCCESS;
}
-static int
-spl_remove_index_internal(List *list, unsigned pos)
-{
- ListNode *node = spl_get_index(list, pos);
- if (!node)
- return -1;
-
- deleteNodeFromList(list, node);
-
- return 0;
-}
-
enum playlist_result
spl_remove_index(const char *utf8path, unsigned pos)
{
- List *list;
+ GPtrArray *list;
+ char *uri;
enum playlist_result result;
if (!(list = spl_load(utf8path)))
return PLAYLIST_RESULT_NO_SUCH_LIST;
- if (spl_remove_index_internal(list, pos) != 0) {
- freeList(list);
+ if (pos >= list->len) {
+ spl_free(list);
return PLAYLIST_RESULT_BAD_RANGE;
}
+ uri = spl_remove_index_internal(list, pos);
+ g_free(uri);
result = spl_save(list, utf8path);
- freeList(list);
+ spl_free(list);
idle_add(IDLE_STORED_PLAYLIST);
return result;
diff --git a/src/stored_playlist.h b/src/stored_playlist.h
index 4841ca80e..8ba756853 100644
--- a/src/stored_playlist.h
+++ b/src/stored_playlist.h
@@ -19,7 +19,6 @@
#ifndef MPD_STORED_PLAYLIST_H
#define MPD_STORED_PLAYLIST_H
-#include "list.h"
#include "playlist.h"
#include <glib.h>
@@ -42,9 +41,12 @@ spl_list(void);
void
spl_list_free(GPtrArray *list);
-List *
+GPtrArray *
spl_load(const char *utf8path);
+void
+spl_free(GPtrArray *list);
+
enum playlist_result
spl_move_index(const char *utf8path, unsigned src, unsigned dest);