diff options
author | Max Kellermann <max@duempel.org> | 2012-02-12 17:50:30 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2012-02-12 17:50:30 +0100 |
commit | ac3ad452c03292e4c57b0d0e21c2dd407eb03a27 (patch) | |
tree | 10e12f06f7a62a0cab6eb0bf06a4e8a09e2d0d78 | |
parent | 027c01511cfb8eac1a2e3f9fc54cfbb2bf968870 (diff) | |
download | mpd-ac3ad452c03292e4c57b0d0e21c2dd407eb03a27.tar.gz mpd-ac3ad452c03292e4c57b0d0e21c2dd407eb03a27.tar.xz mpd-ac3ad452c03292e4c57b0d0e21c2dd407eb03a27.zip |
playlist_vector: use the list_head library
-rw-r--r-- | src/db_print.c | 1 | ||||
-rw-r--r-- | src/directory.c | 10 | ||||
-rw-r--r-- | src/directory.h | 11 | ||||
-rw-r--r-- | src/playlist_database.c | 8 | ||||
-rw-r--r-- | src/playlist_database.h | 6 | ||||
-rw-r--r-- | src/playlist_vector.c | 55 | ||||
-rw-r--r-- | src/playlist_vector.h | 36 | ||||
-rw-r--r-- | src/update_db.c | 1 | ||||
-rw-r--r-- | src/update_walk.c | 9 |
9 files changed, 57 insertions, 80 deletions
diff --git a/src/db_print.c b/src/db_print.c index 067d1c60f..297a6f3f6 100644 --- a/src/db_print.c +++ b/src/db_print.c @@ -27,6 +27,7 @@ #include "client.h" #include "song.h" #include "song_print.h" +#include "playlist_vector.h" #include "tag.h" #include "strset.h" diff --git a/src/directory.c b/src/directory.c index 29991e60d..12d61a81e 100644 --- a/src/directory.c +++ b/src/directory.c @@ -21,6 +21,7 @@ #include "directory.h" #include "song.h" #include "song_sort.h" +#include "playlist_vector.h" #include "path.h" #include "util/list_sort.h" #include "db_visitor.h" @@ -45,11 +46,11 @@ directory_new(const char *path, struct directory *parent) sizeof(directory->path) + pathlen + 1); INIT_LIST_HEAD(&directory->children); INIT_LIST_HEAD(&directory->songs); + INIT_LIST_HEAD(&directory->playlists); + directory->parent = parent; memcpy(directory->path, path, pathlen + 1); - playlist_vector_init(&directory->playlists); - return directory; } @@ -282,9 +283,8 @@ directory_walk(const struct directory *directory, bool recursive, } if (visitor->playlist != NULL) { - const struct playlist_vector *pv = &directory->playlists; - for (const struct playlist_metadata *i = pv->head; - i != NULL; i = i->next) + struct playlist_metadata *i; + directory_for_each_playlist(i, directory) if (!visitor->playlist(i, directory, ctx, error_r)) return false; } diff --git a/src/directory.h b/src/directory.h index 769260436..dc664a433 100644 --- a/src/directory.h +++ b/src/directory.h @@ -22,7 +22,6 @@ #include "check.h" #include "util/list.h" -#include "playlist_vector.h" #include <glib.h> #include <stdbool.h> @@ -45,6 +44,12 @@ #define directory_for_each_song_safe(pos, n, directory) \ list_for_each_entry_safe(pos, n, &directory->songs, siblings) +#define directory_for_each_playlist(pos, directory) \ + list_for_each_entry(pos, &directory->playlists, siblings) + +#define directory_for_each_playlist_safe(pos, n, directory) \ + list_for_each_entry_safe(pos, n, &directory->playlists, siblings) + struct song; struct db_visitor; @@ -75,7 +80,7 @@ struct directory { */ struct list_head songs; - struct playlist_vector playlists; + struct list_head playlists; struct directory *parent; time_t mtime; @@ -129,7 +134,7 @@ directory_is_empty(const struct directory *directory) { return list_empty(&directory->children) && list_empty(&directory->songs) && - playlist_vector_is_empty(&directory->playlists); + list_empty(&directory->playlists); } static inline const char * diff --git a/src/playlist_database.c b/src/playlist_database.c index 2ad913d00..6b9d87155 100644 --- a/src/playlist_database.c +++ b/src/playlist_database.c @@ -33,10 +33,10 @@ playlist_database_quark(void) } void -playlist_vector_save(FILE *fp, const struct playlist_vector *pv) +playlist_vector_save(FILE *fp, const struct list_head *pv) { - for (const struct playlist_metadata *pm = pv->head; - pm != NULL; pm = pm->next) + struct playlist_metadata *pm; + playlist_vector_for_each(pm, pv) fprintf(fp, PLAYLIST_META_BEGIN "%s\n" "mtime: %li\n" "playlist_end\n", @@ -44,7 +44,7 @@ playlist_vector_save(FILE *fp, const struct playlist_vector *pv) } bool -playlist_metadata_load(FILE *fp, struct playlist_vector *pv, const char *name, +playlist_metadata_load(FILE *fp, struct list_head *pv, const char *name, GString *buffer, GError **error_r) { struct playlist_metadata pm = { diff --git a/src/playlist_database.h b/src/playlist_database.h index f80ebdaff..3238fa06b 100644 --- a/src/playlist_database.h +++ b/src/playlist_database.h @@ -28,13 +28,13 @@ #define PLAYLIST_META_BEGIN "playlist_begin: " -struct playlist_vector; +struct list_head; void -playlist_vector_save(FILE *fp, const struct playlist_vector *pv); +playlist_vector_save(FILE *fp, const struct list_head *pv); bool -playlist_metadata_load(FILE *fp, struct playlist_vector *pv, const char *name, +playlist_metadata_load(FILE *fp, struct list_head *pv, const char *name, GString *buffer, GError **error_r); #endif diff --git a/src/playlist_vector.c b/src/playlist_vector.c index cfbe8939e..be418f8af 100644 --- a/src/playlist_vector.c +++ b/src/playlist_vector.c @@ -46,60 +46,43 @@ playlist_metadata_free(struct playlist_metadata *pm) } void -playlist_vector_deinit(struct playlist_vector *pv) +playlist_vector_deinit(struct list_head *pv) { assert(pv != NULL); - while (pv->head != NULL) { - struct playlist_metadata *pm = pv->head; - pv->head = pm->next; + struct playlist_metadata *pm, *n; + playlist_vector_for_each_safe(pm, n, pv) playlist_metadata_free(pm); - } } -static struct playlist_metadata ** -playlist_vector_find_p(struct playlist_vector *pv, const char *name) +struct playlist_metadata * +playlist_vector_find(struct list_head *pv, const char *name) { assert(pv != NULL); assert(name != NULL); - struct playlist_metadata **pmp = &pv->head; - - for (;;) { - struct playlist_metadata *pm = *pmp; - if (pm == NULL) - return NULL; - + struct playlist_metadata *pm; + playlist_vector_for_each(pm, pv) if (strcmp(pm->name, name) == 0) - return pmp; - - pmp = &pm->next; - } -} + return pm; -struct playlist_metadata * -playlist_vector_find(struct playlist_vector *pv, const char *name) -{ - struct playlist_metadata **pmp = playlist_vector_find_p(pv, name); - return pmp != NULL ? *pmp : NULL; + return NULL; } void -playlist_vector_add(struct playlist_vector *pv, +playlist_vector_add(struct list_head *pv, const char *name, time_t mtime) { struct playlist_metadata *pm = playlist_metadata_new(name, mtime); - pm->next = pv->head; - pv->head = pm; + list_add(&pm->siblings, pv); } bool -playlist_vector_update_or_add(struct playlist_vector *pv, +playlist_vector_update_or_add(struct list_head *pv, const char *name, time_t mtime) { - struct playlist_metadata **pmp = playlist_vector_find_p(pv, name); - if (pmp != NULL) { - struct playlist_metadata *pm = *pmp; + struct playlist_metadata *pm = playlist_vector_find(pv, name); + if (pm != NULL) { if (mtime == pm->mtime) return false; @@ -111,15 +94,13 @@ playlist_vector_update_or_add(struct playlist_vector *pv, } bool -playlist_vector_remove(struct playlist_vector *pv, const char *name) +playlist_vector_remove(struct list_head *pv, const char *name) { - struct playlist_metadata **pmp = playlist_vector_find_p(pv, name); - if (pmp == NULL) + struct playlist_metadata *pm = playlist_vector_find(pv, name); + if (pm == NULL) return false; - struct playlist_metadata *pm = *pmp; - *pmp = pm->next; - + list_del(&pm->siblings); playlist_metadata_free(pm); return true; } diff --git a/src/playlist_vector.h b/src/playlist_vector.h index 8aa19a4e0..ae21a051f 100644 --- a/src/playlist_vector.h +++ b/src/playlist_vector.h @@ -20,15 +20,23 @@ #ifndef MPD_PLAYLIST_VECTOR_H #define MPD_PLAYLIST_VECTOR_H +#include "util/list.h" + #include <stdbool.h> #include <stddef.h> #include <sys/time.h> +#define playlist_vector_for_each(pos, head) \ + list_for_each_entry(pos, head, siblings) + +#define playlist_vector_for_each_safe(pos, n, head) \ + list_for_each_entry_safe(pos, n, head, siblings) + /** * A directory entry pointing to a playlist file. */ struct playlist_metadata { - struct playlist_metadata *next; + struct list_head siblings; /** * The UTF-8 encoded name of the playlist file. @@ -38,40 +46,24 @@ struct playlist_metadata { time_t mtime; }; -struct playlist_vector { - struct playlist_metadata *head; -}; - -static inline void -playlist_vector_init(struct playlist_vector *pv) -{ - pv->head = NULL; -} - void -playlist_vector_deinit(struct playlist_vector *pv); - -static inline bool -playlist_vector_is_empty(const struct playlist_vector *pv) -{ - return pv->head == NULL; -} +playlist_vector_deinit(struct list_head *pv); struct playlist_metadata * -playlist_vector_find(struct playlist_vector *pv, const char *name); +playlist_vector_find(struct list_head *pv, const char *name); void -playlist_vector_add(struct playlist_vector *pv, +playlist_vector_add(struct list_head *pv, const char *name, time_t mtime); /** * @return true if the vector or one of its items was modified */ bool -playlist_vector_update_or_add(struct playlist_vector *pv, +playlist_vector_update_or_add(struct list_head *pv, const char *name, time_t mtime); bool -playlist_vector_remove(struct playlist_vector *pv, const char *name); +playlist_vector_remove(struct list_head *pv, const char *name); #endif /* SONGVEC_H */ diff --git a/src/update_db.c b/src/update_db.c index 2ead6d438..c6636de1a 100644 --- a/src/update_db.c +++ b/src/update_db.c @@ -22,6 +22,7 @@ #include "update_remove.h" #include "directory.h" #include "song.h" +#include "playlist_vector.h" #include "db_lock.h" #include <glib.h> diff --git a/src/update_walk.c b/src/update_walk.c index e2f7d3315..ba92cbc48 100644 --- a/src/update_walk.c +++ b/src/update_walk.c @@ -26,6 +26,7 @@ #include "exclude.h" #include "directory.h" #include "song.h" +#include "playlist_vector.h" #include "uri.h" #include "mapper.h" #include "path.h" @@ -159,14 +160,10 @@ removeDeletedFromDirectory(struct directory *directory) g_free(path); } - for (const struct playlist_metadata *pm = directory->playlists.head; - pm != NULL;) { - const struct playlist_metadata *next = pm->next; - + struct playlist_metadata *pm, *np; + directory_for_each_playlist_safe(pm, np, directory) { if (!directory_child_is_regular(directory, pm->name)) playlist_vector_remove(&directory->playlists, pm->name); - - pm = next; } } |