summaryrefslogtreecommitdiff
path: root/library.c
blob: 745957992cf1c436eaffcdc5c48bd629fa4349d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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);
}