From b8c88bbf4e928a0ac4af89dfe72f0661beb35827 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 14 Aug 2009 17:07:26 +0200 Subject: Parse text and join lines. Add users from text lines. Fixed hash table code. --- user.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'user.c') diff --git a/user.c b/user.c index 00e44da..68310e7 100644 --- a/user.c +++ b/user.c @@ -24,12 +24,17 @@ struct user_t *user_get(char *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) { + /* If hash doesn't match and there exists another user, fetch it. */ + while(user->hash != hash && user->next) user = user->next; + /* If hash still doesn't match and the user exists, add a new user. */ + if(user->hash != hash && user->nick) { struct user_t *temp_user = malloc(sizeof(struct user_t)); user->next = temp_user; user = temp_user; + /* Initialize nick to NULL so the user can be correctly added. */ + user->nick = NULL; } + /* Add the new user data to the current pointer if none was found. */ if(!user->nick) { user->hash = hash; user->nick = strdup(nick); @@ -39,5 +44,15 @@ struct user_t *user_get(char *nick) { } void user_free() { + struct user_t *user; + for(int i = 0; i < USERS_MAX; i++) { + user = users[i].next; + while(user) { + struct user_t *temp = user->next; + free(user->nick); + free(user); + user = temp; + } + } free(users); } -- cgit v1.2.3