summaryrefslogtreecommitdiff
path: root/library.c
diff options
context:
space:
mode:
Diffstat (limited to 'library.c')
-rw-r--r--library.c48
1 files changed, 48 insertions, 0 deletions
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 <string.h>
+
+#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);
+}