From 11ff809614169d26efaf9c0d0a30185cf971730c Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 14 Aug 2009 15:31:16 +0200 Subject: Added a user struct which is stored in a hash table. User nicks are hashed using the sdbm hash function. --- user.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 user.c (limited to 'user.c') diff --git a/user.c b/user.c new file mode 100644 index 0000000..00e44da --- /dev/null +++ b/user.c @@ -0,0 +1,43 @@ +#include +#include + +#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); +} -- cgit v1.2.3