From ea7cd94f7aeb177618db3907a6c86b7252e018f0 Mon Sep 17 00:00:00 2001 From: quou Date: Sat, 1 Jun 2024 12:19:16 +1000 Subject: Initial commit. --- library.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 library.c (limited to 'library.c') diff --git a/library.c b/library.c new file mode 100644 index 0000000..7459579 --- /dev/null +++ b/library.c @@ -0,0 +1,48 @@ +#include + +#include "library.h" +#include "plat.h" + +void init_library(Library* l) { + int i; + l->song_count = 0; + for (i = 0; i < max_songs; i++) + l->songs[i].file[0] = 0; +} + +static unsigned hash(const char* s) { + const unsigned char* p = (const unsigned char*)s; + unsigned h = 2166136261; + while (*p) + h = (h ^ *p++) * 16777619; + return h; +} + +Song* find_song(Library* l, const char* path) { + int i = hash(path) % max_songs; + int c; + Song* s; + for (c = 0; c < max_songs; c++) { + s = &l->songs[i]; + if (!s->file[0] || !strcmp(path, s->file)) + return s; + i++; + i %= max_songs; + } + return 0; +} + +void add_song(void* l, const char* path) { + Song* s; + s = find_song(l, path); + if (!s) { + print_err("Too many songs!"); + return; + } + strcpy(s->file, path); + strcpy(s->name, path); +} + +void build_library(Library* l, const char* path) { + iter_dir(path, add_song, l); +} -- cgit v1.2.3-54-g00ecf