summaryrefslogtreecommitdiff
path: root/user.c
blob: 00e44daab02a0f6a6464832ad8fb53eccc668aac (plain)
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
#include <stdlib.h>
#include <string.h>

#include "user.h"

struct user_t *users;

void user_init() {
	users = malloc(sizeof(struct user_t) * USERS_MAX);
	memset(users, 0, sizeof(struct user_t) * USERS_MAX);
}

static unsigned long sdbm(char *str) {
	unsigned long hash = 0;
	int c;
	while(c = *str++) {
		hash = c + (hash << 6) + (hash << 16) - hash;
	}
	return hash;
}

struct user_t *user_get(char *nick) {
	unsigned long hash = sdbm(nick);
	int index = hash % USERS_MAX;

	struct user_t *user = &users[index];
	while(user->next && user->hash != hash) user = user->next;
	if(user->hash != hash) {
		struct user_t *temp_user = malloc(sizeof(struct user_t));
		user->next = temp_user;
		user = temp_user;
	}
	if(!user->nick) {
		user->hash = hash;
		user->nick = strdup(nick);
		user->lines = user->words = 0;
		user->next = NULL;
	}
}

void user_free() {
	free(users);
}