From 4c4bf8ffce9de2e629ce0c98762d1ebfbe353af2 Mon Sep 17 00:00:00 2001 From: Moritz Grimm Date: Sat, 10 Sep 2022 19:26:18 +0200 Subject: [PATCH] Store playlist location alongside playlists --- src/playlist.c | 20 ++++++++++++++++++++ src/playlist.h | 5 +++++ tests/check_playlist.c | 1 + 3 files changed, 26 insertions(+) diff --git a/src/playlist.c b/src/playlist.c index 422b250..a3e7edd 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -27,6 +27,9 @@ #endif #include +#if defined(HAVE_LIBGEN_H) +# include +#endif /* HAVE_LIBGEN_H */ #include #include #include @@ -45,6 +48,7 @@ struct playlist { char *filename; + char *location; char **list; size_t size; size_t num; @@ -62,9 +66,15 @@ static struct playlist * _playlist_create(const char *filename) { struct playlist *pl; + char *tmp; pl = xcalloc(1UL, sizeof(*pl)); pl->filename = xstrdup(filename); + tmp = xstrdup(filename); + pl->location = xstrdup(dirname(tmp)); + xfree(tmp); + if (NULL == pl->location) + pl->location = xstrdup("."); return (pl); } @@ -289,6 +299,10 @@ playlist_free(struct playlist **pl_p) xfree(pl->filename); pl->filename = NULL; } + if (pl->location != NULL) { + xfree(pl->location); + pl->location = NULL; + } if (pl->list != NULL) { if (pl->size > 0) { @@ -437,3 +451,9 @@ playlist_shuffle(struct playlist *pl) pl->list[i] = temp; } } + +const char * +playlist_get_location(struct playlist *pl) +{ + return (pl->location); +} diff --git a/src/playlist.h b/src/playlist.h index 097723e..7de70fa 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -101,4 +101,9 @@ int playlist_reread(playlist_t *); */ void playlist_shuffle(playlist_t); +/* + * Return the path where the playlist file or program is located on disk. + */ +const char * playlist_get_location(playlist_t); + #endif /* __PLAYLIST_H__ */ diff --git a/tests/check_playlist.c b/tests/check_playlist.c index e5f601f..3cb53f3 100644 --- a/tests/check_playlist.c +++ b/tests/check_playlist.c @@ -15,6 +15,7 @@ START_TEST(test_playlist_file) ck_assert_ptr_eq(playlist_read("nonexistent.txt"), NULL); p = playlist_read(SRCDIR "/playlist.txt"); ck_assert_ptr_ne(p, NULL); + ck_assert_str_eq(playlist_get_location(p), SRCDIR); ck_assert_uint_gt(playlist_get_num_items(p), 0); ck_assert_str_eq(playlist_get_next(p), "1.ogg"); playlist_skip_next(p);