summaryrefslogtreecommitdiff
path: root/user.c
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2009-08-14 15:31:16 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2009-08-14 15:31:16 +0200
commit11ff809614169d26efaf9c0d0a30185cf971730c (patch)
treea338e8d1b5b89e116dd3fa43f8579eda62b731cb /user.c
parent21105a00d87da96baae165a7596d0c12aaff09ce (diff)
Added a user struct which is stored in a hash table.
User nicks are hashed using the sdbm hash function.
Diffstat (limited to 'user.c')
-rw-r--r--user.c43
1 files changed, 43 insertions, 0 deletions
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 <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);
+}