From b57481d1dfcc9a479290f4b1849c210f951c03f2 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 16 Aug 2010 15:21:14 +0200 Subject: Use GSList with music storage. --- commands.c | 6 ++++-- music.c | 39 +++++++++++++++------------------------ music.h | 5 ++--- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/commands.c b/commands.c index d380c32..08b0366 100644 --- a/commands.c +++ b/commands.c @@ -20,14 +20,16 @@ static void commands_list(GSocketConnection *connection, const gchar *cmd) { GSocket *socket = g_socket_connection_get_socket(connection); - for(struct directory *d = directory->sub; d; d = d->next) { + for(GSList *node = directory->sub; node; node = g_slist_next(node)) { + struct directory *d = node->data; gchar *name = g_path_get_basename(d->path); g_socket_send(socket, name, strlen(name), NULL, NULL); g_socket_send(socket, "\n", 1, NULL, NULL); g_free(name); } - for(struct file *f = directory->files; f; f = f->next) { + for(GSList *node = directory->files; node; node = g_slist_next(node)) { + struct file *f = node->data; g_socket_send(socket, f->name, strlen(f->name), NULL, NULL); g_socket_send(socket, "\n", 1, NULL, NULL); } diff --git a/music.c b/music.c index c64a2ba..7a4b37e 100644 --- a/music.c +++ b/music.c @@ -39,26 +39,14 @@ gboolean music_scan(struct directory *directory) { struct file *f = g_new0(struct file, 1); f->name = g_strdup(entry); - if(directory->files == NULL) { - directory->files = f; - } else { - struct file *last = directory->files; - while(last->next) last = last->next; - last->next = f; - } + directory->files = g_slist_prepend(directory->files, f); g_debug("added file %s to %s", entry, directory->path); } else if(S_ISDIR(st.st_mode)) { struct directory *d = g_new0(struct directory, 1); d->path = g_build_filename(directory->path, entry, NULL); - if(directory->sub == NULL) { - directory->sub = d; - } else { - struct directory *last = directory->sub; - while(last->next) last = last->next; - last->next = d; - } + directory->sub = g_slist_prepend(directory->sub, d); g_debug("added subdir %s to %s", entry, directory->path); @@ -82,7 +70,9 @@ static struct directory *music_find_dir_rec(struct directory *root, const gchar if(g_strcmp0(root->path, path) == 0) return root; - for(struct directory *d = root->sub; d; d = d->next) { + for(GSList *node = root->sub; node; node = g_slist_next(node)) { + struct directory *d = node->data; + g_debug(d->path); if(g_strcmp0(d->path, path) == 0) { return d; } @@ -110,7 +100,8 @@ struct file *music_find_file(const gchar *path) { return NULL; } - for(struct file *f = directory->files; f; f = f->next) { + for(GSList *node = directory->files; node; node = g_slist_next(node)) { + struct file *f = node->data; if(g_strcmp0(filename, f->name) == 0) { return f; } @@ -120,23 +111,23 @@ struct file *music_find_file(const gchar *path) { } static void music_do_free(struct directory *root) { - struct directory *node; - g_assert(root != NULL); - for(node = root->sub; node; node = node->next) { - music_do_free(node); + for(GSList *node = root->sub; node; node = g_slist_next(node)) { + struct directory *d = node->data; + music_do_free(d); } - for(node = root->next; node; node = node->next) { - music_do_free(node); - } + g_slist_free(root->sub); - for(struct file *f = root->files; f; f = f->next) { + for(GSList *node = root->files; node; node = g_slist_next(node)) { + struct file *f = node->data; g_free(f->name); g_free(f); } + g_slist_free(root->files); + g_free(root->path); g_free(root); } diff --git a/music.h b/music.h index b643c8e..4213fc5 100644 --- a/music.h +++ b/music.h @@ -9,13 +9,12 @@ struct file { gchar *name; gssize size; - struct file *next; }; struct directory { gchar *path; - struct directory *sub, *next; - struct file *files; + GSList *sub; + GSList *files; }; extern struct directory *music_root; -- cgit v1.2.3