diff options
Diffstat (limited to 'playlist.c')
-rw-r--r-- | playlist.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/playlist.c b/playlist.c new file mode 100644 index 0000000..8dda913 --- /dev/null +++ b/playlist.c @@ -0,0 +1,123 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include "plat.h" +#include "playlist.h" + +void init_playlist(Playlist* p) { + p->name[0] = 0; + p->cnt = 0; +} + +void playlist_rm(Playlist* p, int i) { + int j; + for (j = i + 1; j < p->cnt; j++) { + p->songs[j - 1] = p->songs[j]; + } + p->cnt--; +} + +void playlist_add(Playlist* p, Song* s) { + if (p->cnt >= playlist_max) + return; + p->songs[p->cnt++] = s; +} + +void playlist_add_first(Playlist* p, Song* s) { + int i; + if (p->cnt >= playlist_max) + return; + for (i = p->cnt - 1; i >= 0; i--) { + p->songs[i + 1] = p->songs[i]; + } + p->songs[0] = s; + p->cnt++; +} + +void playlist_shuffle(Playlist* p) { + int ci, ri; + Song* t; + for (ci = p->cnt; ci;) { + ri = rand() % ci; + ci--; + t = p->songs[ci]; + p->songs[ci] = p->songs[ri]; + p->songs[ri] = t; + } +} + +void get_playlist_dir(char* buf) { + int llen; + llen = strlen(library_path); + strcpy(buf, library_path); + if (buf[llen - 1] != '/') + strcat(buf, "/"); + strcat(buf, "playlists/"); +} + +void playlist_save(const Playlist* p) { + int i, plen, llen; + const char* path, * ws; + FILE* f; + char wp[256]; + llen = strlen(library_path); + get_playlist_dir(wp); + if (!dir_exist(wp)) + if (!make_dir(wp)) return; + strcat(wp, p->name); + strcat(wp, ".m3u"); + f = fopen(wp, "w"); + if (!f) return; + for (i = 0; i < p->cnt; i++) { + path = p->songs[i]->path; + plen = strlen(path); + if ( + plen >= llen && + !memcmp(path, library_path, llen) + ) + ws = path + llen; + else + ws = path; + fputs(ws, f); + fputs("\n", f); + } + fclose(f); +} + +int playlist_load(Library* l, Playlist* p, const char* name) { + char path[256]; + char line[256]; + const char* sp; + FILE* f; + Song* s; + int ll, lpl; + lpl = strlen(library_path); + get_playlist_dir(path); + strcat(path, name); + strcat(path, ".m3u"); + f = fopen(path, "r"); + if (!f) return 0; + strcpy(p->name, name); + p->cnt = 0; + while (fgets(line, sizeof line, f)) { + ll = strlen(line); + for ( + ; + ll > 1 && line[ll - 1] == '\n'; + ll--, line[ll] = 0 + ); + if (strstr(line, library_path) != line) { + strcpy(path, library_path); + if (path[lpl - 1] != '/') + strcat(path, "/"); + strcat(path, line); + sp = path; + } else { + sp = line; + } + s = find_song(l, sp); + if (s && s->path[0]) + playlist_add(p, s); + } + return 1; +} |