summaryrefslogtreecommitdiffstats
path: root/src/data.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/data.c')
-rw-r--r--src/data.c93
1 files changed, 46 insertions, 47 deletions
diff --git a/src/data.c b/src/data.c
index d3c43ff..c2d296a 100644
--- a/src/data.c
+++ b/src/data.c
@@ -13,6 +13,7 @@
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
+#include <stdlib.h>
const char *datadir;
@@ -30,7 +31,7 @@ static void tag_free(struct tag *tag);
static struct track *track_alloc(const char *path, const char *fname);
static void track_free(struct track *t);
-static bool tag_name_cmp(struct link *l1, struct link *l2);
+static bool tag_name_cmp(const void *p1, const void *p2, void *user);
struct tag *
tag_alloc(const char *path, const char *fname)
@@ -44,8 +45,8 @@ tag_alloc(const char *path, const char *fname)
tag->name = astrdup(fname);
tag->index_dirty = false;
tag->reordered = false;
- tag->link = LINK_EMPTY;
- tag->link_sel = LINK_EMPTY;
+ tag->link = LIST_LINK_INIT;
+ tag->link_sel = LIST_LINK_INIT;
list_init(&tag->tracks);
return tag;
@@ -71,11 +72,11 @@ track_alloc(const char *dir, const char *fname)
track->fpath = aprintf("%s/%s", dir, fname);
track->name = astrdup(fname);
track->tag = NULL;
- track->link = LINK_EMPTY;
- track->link_pl = LINK_EMPTY;
- track->link_tt = LINK_EMPTY;
- track->link_pq = LINK_EMPTY;
- track->link_hs = LINK_EMPTY;
+ track->link = LIST_LINK_INIT;
+ track->link_pl = LIST_LINK_INIT;
+ track->link_tt = LIST_LINK_INIT;
+ track->link_pq = LIST_LINK_INIT;
+ track->link_hs = LIST_LINK_INIT;
return track;
}
@@ -89,12 +90,9 @@ track_free(struct track *t)
}
bool
-tag_name_cmp(struct link *l1, struct link *l2)
+tag_name_cmp(const void *p1, const void *p2, void *user)
{
- struct tag *t1, *t2;
-
- t1 = LINK_UPCAST(l1, struct tag, link);
- t2 = LINK_UPCAST(l2, struct tag, link);
+ const struct tag *t1 = p1, *t2 = p2;
return strcmp(t1->name, t2->name) <= 0;
}
@@ -214,12 +212,12 @@ move_file(const char *src, const char *dst)
void
tag_clear_tracks(struct tag *tag)
{
- struct link *link;
+ struct list_link *link;
struct track *track;
while (!list_empty(&tag->tracks)) {
link = list_pop_front(&tag->tracks);
- track = UPCAST(link, struct track, link_tt);
+ track = LIST_UPCAST(link, struct track, link_tt);
track_rm(track, false);
}
}
@@ -255,7 +253,7 @@ void
tag_save_tracks(struct tag *tag)
{
struct track *track;
- struct link *link;
+ struct list_link *link;
char *index_path;
FILE *file;
@@ -271,7 +269,7 @@ tag_save_tracks(struct tag *tag)
}
for (LIST_ITER(&tag->tracks, link)) {
- track = UPCAST(link, struct track, link_tt);
+ track = LIST_UPCAST(link, struct track, link_tt);
fprintf(file, "%s\n", track->name);
}
@@ -313,12 +311,12 @@ tag_reindex_tracks(struct tag *tag)
}
struct track *
-tracks_vis_track(struct link *link)
+tracks_vis_track(struct list_link *link)
{
if (tracks_vis == &player.playlist) {
- return UPCAST(link, struct track, link_pl);
+ return LIST_UPCAST(link, struct track, link_pl);
} else {
- return UPCAST(link, struct track, link_tt);
+ return LIST_UPCAST(link, struct track, link_tt);
}
}
@@ -332,7 +330,7 @@ playlist_clear(void)
void
playlist_update(void)
{
- struct link *link, *link2;
+ struct list_link *link, *link2;
struct track *track;
struct tag *tag;
@@ -342,11 +340,11 @@ playlist_update(void)
playlist_clear();
for (LIST_ITER(&tags_sel, link)) {
- tag = UPCAST(link, struct tag, link_sel);
+ tag = LIST_UPCAST(link, struct tag, link_sel);
for (LIST_ITER(&tag->tracks, link2)) {
- track = UPCAST(link2, struct track, link_tt);
- link_pop(&track->link_pl);
- list_push_back(&player.playlist, &track->link_pl);
+ track = LIST_UPCAST(link2, struct track, link_tt);
+ list_link_pop(&track->link_pl);
+ list_insert_back(&player.playlist, &track->link_pl);
}
}
@@ -382,7 +380,7 @@ tag_add(const char *fname)
tag = tag_alloc(datadir, fname);
if (!tag) return NULL;
- list_push_back(&tags, &tag->link);
+ list_insert_back(&tags, &tag->link);
return tag;
}
@@ -390,11 +388,11 @@ tag_add(const char *fname)
struct tag *
tag_find(const char *name)
{
- struct link *link;
+ struct list_link *link;
struct tag *tag;
for (LIST_ITER(&tags, link)) {
- tag = UPCAST(link, struct tag, link);
+ tag = LIST_UPCAST(link, struct tag, link);
if (!strcmp(tag->name, name))
return tag;
}
@@ -405,13 +403,13 @@ tag_find(const char *name)
bool
tag_rm(struct tag *tag, bool sync_fs)
{
- struct link *link;
+ struct list_link *link;
struct track *track;
/* remove contained tracks */
while (!list_empty(&tag->tracks)) {
link = list_pop_front(&tag->tracks);
- track = UPCAST(link, struct track, link_tt);
+ track = LIST_UPCAST(link, struct track, link_tt);
if (!track_rm(track, sync_fs))
return false;
}
@@ -421,10 +419,10 @@ tag_rm(struct tag *tag, bool sync_fs)
return false;
/* remove from selected */
- link_pop(&tag->link_sel);
+ list_link_pop(&tag->link_sel);
/* remove from tags list */
- link_pop(&tag->link);
+ list_link_pop(&tag->link);
tag_free(tag);
@@ -434,7 +432,7 @@ tag_rm(struct tag *tag, bool sync_fs)
bool
tag_rename(struct tag *tag, const char *name)
{
- struct link *link;
+ struct list_link *link;
struct track *track;
char *newpath;
@@ -452,7 +450,7 @@ tag_rename(struct tag *tag, const char *name)
tag->name = astrdup(name);
for (LIST_ITER(&tag->tracks, link)) {
- track = UPCAST(link, struct track, link_tt);
+ track = LIST_UPCAST(link, struct track, link_tt);
free(track->fpath);
track->fpath = aprintf("%s/%s", newpath, track->name);
}
@@ -469,13 +467,13 @@ track_add(struct tag *tag, const char *fname)
track->tag = tag;
/* insert track into sorted tracks list */
- list_push_back(&tracks, &track->link);
+ list_insert_back(&tracks, &track->link);
/* add to tag's tracks list */
- list_push_back(&tag->tracks, &track->link_tt);
+ list_insert_back(&tag->tracks, &track->link_tt);
/* if track's tag is selected, update playlist */
- if (link_inuse(&tag->link_sel))
+ if (list_link_inuse(&tag->link_sel))
playlist_outdated = true;
tag->index_dirty = true;
@@ -492,19 +490,19 @@ track_rm(struct track *track, bool sync_fs)
track->tag->index_dirty = true;
/* remove from tracks list */
- link_pop(&track->link);
+ list_link_pop(&track->link);
/* remove from tag's track list */
- link_pop(&track->link_tt);
+ list_link_pop(&track->link_tt);
/* remove from playlist */
- link_pop(&track->link_pl);
+ list_link_pop(&track->link_pl);
/* remove from player queue */
- link_pop(&track->link_pq);
+ list_link_pop(&track->link_pq);
/* remove from player history */
- link_pop(&track->link_hs);
+ list_link_pop(&track->link_hs);
/* remove the reference as last used track */
if (player.track == track)
@@ -678,7 +676,8 @@ data_load(void)
free(path);
}
- list_sort(&tags, false, tag_name_cmp);
+ list_insertion_sort(&tags, false, tag_name_cmp,
+ LIST_OFFSET(struct tag, link), NULL);
playlist_outdated = true;
@@ -688,11 +687,11 @@ data_load(void)
void
data_save(void)
{
- struct link *link;
+ struct list_link *link;
struct tag *tag;
for (LIST_ITER(&tags, link)) {
- tag = UPCAST(link, struct tag, link);
+ tag = LIST_UPCAST(link, struct tag, link);
if (tag->index_dirty)
tag_save_tracks(tag);
}
@@ -703,7 +702,7 @@ data_save(void)
void
data_free(void)
{
- struct link *link;
+ struct list_link *link;
struct tag *tag;
list_clear(&player.playlist);
@@ -712,7 +711,7 @@ data_free(void)
while (!list_empty(&tags)) {
link = list_pop_front(&tags);
- tag = UPCAST(link, struct tag, link);
+ tag = LIST_UPCAST(link, struct tag, link);
tag_rm(tag, false);
}
}