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. --- music.c | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) (limited to 'music.c') 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); } -- cgit v1.2.3