Commit ac3ad452 authored by Max Kellermann's avatar Max Kellermann

playlist_vector: use the list_head library

parent 027c0151
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "client.h" #include "client.h"
#include "song.h" #include "song.h"
#include "song_print.h" #include "song_print.h"
#include "playlist_vector.h"
#include "tag.h" #include "tag.h"
#include "strset.h" #include "strset.h"
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "directory.h" #include "directory.h"
#include "song.h" #include "song.h"
#include "song_sort.h" #include "song_sort.h"
#include "playlist_vector.h"
#include "path.h" #include "path.h"
#include "util/list_sort.h" #include "util/list_sort.h"
#include "db_visitor.h" #include "db_visitor.h"
...@@ -45,11 +46,11 @@ directory_new(const char *path, struct directory *parent) ...@@ -45,11 +46,11 @@ directory_new(const char *path, struct directory *parent)
sizeof(directory->path) + pathlen + 1); sizeof(directory->path) + pathlen + 1);
INIT_LIST_HEAD(&directory->children); INIT_LIST_HEAD(&directory->children);
INIT_LIST_HEAD(&directory->songs); INIT_LIST_HEAD(&directory->songs);
INIT_LIST_HEAD(&directory->playlists);
directory->parent = parent; directory->parent = parent;
memcpy(directory->path, path, pathlen + 1); memcpy(directory->path, path, pathlen + 1);
playlist_vector_init(&directory->playlists);
return directory; return directory;
} }
...@@ -282,9 +283,8 @@ directory_walk(const struct directory *directory, bool recursive, ...@@ -282,9 +283,8 @@ directory_walk(const struct directory *directory, bool recursive,
} }
if (visitor->playlist != NULL) { if (visitor->playlist != NULL) {
const struct playlist_vector *pv = &directory->playlists; struct playlist_metadata *i;
for (const struct playlist_metadata *i = pv->head; directory_for_each_playlist(i, directory)
i != NULL; i = i->next)
if (!visitor->playlist(i, directory, ctx, error_r)) if (!visitor->playlist(i, directory, ctx, error_r))
return false; return false;
} }
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include "check.h" #include "check.h"
#include "util/list.h" #include "util/list.h"
#include "playlist_vector.h"
#include <glib.h> #include <glib.h>
#include <stdbool.h> #include <stdbool.h>
...@@ -45,6 +44,12 @@ ...@@ -45,6 +44,12 @@
#define directory_for_each_song_safe(pos, n, directory) \ #define directory_for_each_song_safe(pos, n, directory) \
list_for_each_entry_safe(pos, n, &directory->songs, siblings) 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 song;
struct db_visitor; struct db_visitor;
...@@ -75,7 +80,7 @@ struct directory { ...@@ -75,7 +80,7 @@ struct directory {
*/ */
struct list_head songs; struct list_head songs;
struct playlist_vector playlists; struct list_head playlists;
struct directory *parent; struct directory *parent;
time_t mtime; time_t mtime;
...@@ -129,7 +134,7 @@ directory_is_empty(const struct directory *directory) ...@@ -129,7 +134,7 @@ directory_is_empty(const struct directory *directory)
{ {
return list_empty(&directory->children) && return list_empty(&directory->children) &&
list_empty(&directory->songs) && list_empty(&directory->songs) &&
playlist_vector_is_empty(&directory->playlists); list_empty(&directory->playlists);
} }
static inline const char * static inline const char *
......
...@@ -33,10 +33,10 @@ playlist_database_quark(void) ...@@ -33,10 +33,10 @@ playlist_database_quark(void)
} }
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; struct playlist_metadata *pm;
pm != NULL; pm = pm->next) playlist_vector_for_each(pm, pv)
fprintf(fp, PLAYLIST_META_BEGIN "%s\n" fprintf(fp, PLAYLIST_META_BEGIN "%s\n"
"mtime: %li\n" "mtime: %li\n"
"playlist_end\n", "playlist_end\n",
...@@ -44,7 +44,7 @@ playlist_vector_save(FILE *fp, const struct playlist_vector *pv) ...@@ -44,7 +44,7 @@ playlist_vector_save(FILE *fp, const struct playlist_vector *pv)
} }
bool 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) GString *buffer, GError **error_r)
{ {
struct playlist_metadata pm = { struct playlist_metadata pm = {
......
...@@ -28,13 +28,13 @@ ...@@ -28,13 +28,13 @@
#define PLAYLIST_META_BEGIN "playlist_begin: " #define PLAYLIST_META_BEGIN "playlist_begin: "
struct playlist_vector; struct list_head;
void void
playlist_vector_save(FILE *fp, const struct playlist_vector *pv); playlist_vector_save(FILE *fp, const struct list_head *pv);
bool 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); GString *buffer, GError **error_r);
#endif #endif
...@@ -46,60 +46,43 @@ playlist_metadata_free(struct playlist_metadata *pm) ...@@ -46,60 +46,43 @@ playlist_metadata_free(struct playlist_metadata *pm)
} }
void void
playlist_vector_deinit(struct playlist_vector *pv) playlist_vector_deinit(struct list_head *pv)
{ {
assert(pv != NULL); assert(pv != NULL);
while (pv->head != NULL) { struct playlist_metadata *pm, *n;
struct playlist_metadata *pm = pv->head; playlist_vector_for_each_safe(pm, n, pv)
pv->head = pm->next;
playlist_metadata_free(pm); playlist_metadata_free(pm);
}
} }
static struct playlist_metadata ** struct playlist_metadata *
playlist_vector_find_p(struct playlist_vector *pv, const char *name) playlist_vector_find(struct list_head *pv, const char *name)
{ {
assert(pv != NULL); assert(pv != NULL);
assert(name != NULL); assert(name != NULL);
struct playlist_metadata **pmp = &pv->head; struct playlist_metadata *pm;
playlist_vector_for_each(pm, pv)
for (;;) {
struct playlist_metadata *pm = *pmp;
if (pm == NULL)
return NULL;
if (strcmp(pm->name, name) == 0) if (strcmp(pm->name, name) == 0)
return pmp; return pm;
pmp = &pm->next;
}
}
struct playlist_metadata * return NULL;
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;
} }
void void
playlist_vector_add(struct playlist_vector *pv, playlist_vector_add(struct list_head *pv,
const char *name, time_t mtime) const char *name, time_t mtime)
{ {
struct playlist_metadata *pm = playlist_metadata_new(name, mtime); struct playlist_metadata *pm = playlist_metadata_new(name, mtime);
pm->next = pv->head; list_add(&pm->siblings, pv);
pv->head = pm;
} }
bool 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) const char *name, time_t mtime)
{ {
struct playlist_metadata **pmp = playlist_vector_find_p(pv, name); struct playlist_metadata *pm = playlist_vector_find(pv, name);
if (pmp != NULL) { if (pm != NULL) {
struct playlist_metadata *pm = *pmp;
if (mtime == pm->mtime) if (mtime == pm->mtime)
return false; return false;
...@@ -111,15 +94,13 @@ playlist_vector_update_or_add(struct playlist_vector *pv, ...@@ -111,15 +94,13 @@ playlist_vector_update_or_add(struct playlist_vector *pv,
} }
bool 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); struct playlist_metadata *pm = playlist_vector_find(pv, name);
if (pmp == NULL) if (pm == NULL)
return false; return false;
struct playlist_metadata *pm = *pmp; list_del(&pm->siblings);
*pmp = pm->next;
playlist_metadata_free(pm); playlist_metadata_free(pm);
return true; return true;
} }
...@@ -20,15 +20,23 @@ ...@@ -20,15 +20,23 @@
#ifndef MPD_PLAYLIST_VECTOR_H #ifndef MPD_PLAYLIST_VECTOR_H
#define MPD_PLAYLIST_VECTOR_H #define MPD_PLAYLIST_VECTOR_H
#include "util/list.h"
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <sys/time.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. * A directory entry pointing to a playlist file.
*/ */
struct playlist_metadata { struct playlist_metadata {
struct playlist_metadata *next; struct list_head siblings;
/** /**
* The UTF-8 encoded name of the playlist file. * The UTF-8 encoded name of the playlist file.
...@@ -38,40 +46,24 @@ struct playlist_metadata { ...@@ -38,40 +46,24 @@ struct playlist_metadata {
time_t mtime; time_t mtime;
}; };
struct playlist_vector {
struct playlist_metadata *head;
};
static inline void
playlist_vector_init(struct playlist_vector *pv)
{
pv->head = NULL;
}
void void
playlist_vector_deinit(struct playlist_vector *pv); playlist_vector_deinit(struct list_head *pv);
static inline bool
playlist_vector_is_empty(const struct playlist_vector *pv)
{
return pv->head == NULL;
}
struct playlist_metadata * struct playlist_metadata *
playlist_vector_find(struct playlist_vector *pv, const char *name); playlist_vector_find(struct list_head *pv, const char *name);
void void
playlist_vector_add(struct playlist_vector *pv, playlist_vector_add(struct list_head *pv,
const char *name, time_t mtime); const char *name, time_t mtime);
/** /**
* @return true if the vector or one of its items was modified * @return true if the vector or one of its items was modified
*/ */
bool 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); const char *name, time_t mtime);
bool bool
playlist_vector_remove(struct playlist_vector *pv, const char *name); playlist_vector_remove(struct list_head *pv, const char *name);
#endif /* SONGVEC_H */ #endif /* SONGVEC_H */
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "update_remove.h" #include "update_remove.h"
#include "directory.h" #include "directory.h"
#include "song.h" #include "song.h"
#include "playlist_vector.h"
#include "db_lock.h" #include "db_lock.h"
#include <glib.h> #include <glib.h>
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "exclude.h" #include "exclude.h"
#include "directory.h" #include "directory.h"
#include "song.h" #include "song.h"
#include "playlist_vector.h"
#include "uri.h" #include "uri.h"
#include "mapper.h" #include "mapper.h"
#include "path.h" #include "path.h"
...@@ -159,14 +160,10 @@ removeDeletedFromDirectory(struct directory *directory) ...@@ -159,14 +160,10 @@ removeDeletedFromDirectory(struct directory *directory)
g_free(path); g_free(path);
} }
for (const struct playlist_metadata *pm = directory->playlists.head; struct playlist_metadata *pm, *np;
pm != NULL;) { directory_for_each_playlist_safe(pm, np, directory) {
const struct playlist_metadata *next = pm->next;
if (!directory_child_is_regular(directory, pm->name)) if (!directory_child_is_regular(directory, pm->name))
playlist_vector_remove(&directory->playlists, pm->name); playlist_vector_remove(&directory->playlists, pm->name);
pm = next;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment