Commit ac3ad452 authored by Max Kellermann's avatar Max Kellermann

playlist_vector: use the list_head library

parent 027c0151
......@@ -27,6 +27,7 @@
#include "client.h"
#include "song.h"
#include "song_print.h"
#include "playlist_vector.h"
#include "tag.h"
#include "strset.h"
......
......@@ -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;
}
......
......@@ -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 *
......
......@@ -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 = {
......
......@@ -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
......@@ -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;
}
......@@ -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 */
......@@ -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>
......
......@@ -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;
}
}
......
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