diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | user.c | 43 | ||||
-rw-r--r-- | user.h | 20 |
3 files changed, 64 insertions, 1 deletions
@@ -6,7 +6,7 @@ CFLAGS += $(shell pkg-config --cflags libconfig) CFLAGS += $(shell pcre-config --cflags) LDFLAGS += $(shell pkg-config --libs libconfig) LDFLAGS += $(shell pcre-config --libs) -OBJECTS = main.o config.o regexset.o channel.o +OBJECTS = main.o config.o regexset.o channel.o user.o TARGET = ircstats all: $(TARGET) @@ -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); +} @@ -0,0 +1,20 @@ +#ifndef _USER_H_ +#define _USER_H_ + +#define USERS_MAX 100 + +struct user_t { + unsigned long hash; + char *nick; + unsigned long lines; + unsigned long long words; + struct user_t *next; +}; + +void user_init(); +struct user_t *user_get(char *nick); +void user_free(); + +extern struct user_t *users; + +#endif |