#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); }