about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-10-14 19:36:45 +0100
committerJames Booth <boothj5@gmail.com>2012-10-14 19:36:45 +0100
commitd2be6929921170ef8d6395781b4f4c7808bf2923 (patch)
tree9f24d806b93c3db723b129d3b8392d1de621a966 /src
parent43a7f5885081ef8baa0f2a2587513397b093921c (diff)
downloadprofani-tty-d2be6929921170ef8d6395781b4f4c7808bf2923.tar.gz
Using const for chat sessions
Allocating memory for keys and values
Diffstat (limited to 'src')
-rw-r--r--src/chat_session.c27
-rw-r--r--src/chat_session.h12
2 files changed, 20 insertions, 19 deletions
diff --git a/src/chat_session.c b/src/chat_session.c
index ff51b29d..6f1d624e 100644
--- a/src/chat_session.c
+++ b/src/chat_session.c
@@ -27,7 +27,7 @@
 
 #include "chat_session.h"
 
-static ChatSession _chat_session_new(char *recipient);
+static ChatSession _chat_session_new(const char * const recipient);
 static void _chat_session_free(ChatSession session);
 
 struct chat_session_t {
@@ -41,25 +41,26 @@ static GHashTable *sessions;
 void
 chat_session_init(void)
 {
-    sessions = g_hash_table_new_full(NULL, g_str_equal, g_free, 
+    sessions = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, 
         (GDestroyNotify)_chat_session_free);
 }
 
 void
-chat_session_start(char *recipient)
+chat_session_start(const char * const recipient)
 {
-    ChatSession session = _chat_session_new(recipient);
-    g_hash_table_insert(sessions, recipient, session);
+    char *key = strdup(recipient);
+    ChatSession session = _chat_session_new(key);
+    g_hash_table_insert(sessions, key, session);
 }
 
 void
-chat_session_end(char *recipient)
+chat_session_end(const char * const recipient)
 {
     g_hash_table_remove(sessions, recipient);
 }
 
 chat_state_t
-chat_session_get_state(char *recipient)
+chat_session_get_state(const char * const recipient)
 {
     ChatSession session = g_hash_table_lookup(sessions, recipient);
     if (session == NULL) {
@@ -70,32 +71,31 @@ chat_session_get_state(char *recipient)
 }
 
 void
-chat_session_set_state(char *recipient, chat_state_t state)
+chat_session_set_state(const char * const recipient, chat_state_t state)
 {
     ChatSession session = g_hash_table_lookup(sessions, recipient);
     session->state = state;
 }
 
 gboolean
-chat_session_get_sent(char *recipient)
+chat_session_get_sent(const char * const recipient)
 {
     ChatSession session = g_hash_table_lookup(sessions, recipient);
     return session->sent;
 }
 
 void
-chat_session_sent(char *recipient)
+chat_session_sent(const char * const recipient)
 {
     ChatSession session = g_hash_table_lookup(sessions, recipient);
     session->sent = TRUE;
 }
 
 static ChatSession
-_chat_session_new(char *recipient)
+_chat_session_new(const char * const recipient)
 {
     ChatSession new_session = malloc(sizeof(struct chat_session_t));
-    new_session->recipient = malloc(strlen(recipient) + 1);
-    strcpy(new_session->recipient, recipient);
+    new_session->recipient = strdup(recipient);
     new_session->state = ACTIVE;
     new_session->sent = FALSE;
     
@@ -109,4 +109,5 @@ _chat_session_free(ChatSession session)
         g_free(session->recipient);
         g_free(session);
     }
+    session = NULL;
 }
diff --git a/src/chat_session.h b/src/chat_session.h
index 8c0d9048..b17fb2ee 100644
--- a/src/chat_session.h
+++ b/src/chat_session.h
@@ -37,11 +37,11 @@ typedef enum {
 } chat_state_t;
 
 void chat_session_init(void);
-void chat_session_start(char *recipient);
-void chat_session_end(char *recipient);
-chat_state_t chat_session_get_state(char *recipient);
-void chat_session_set_state(char *recipient, chat_state_t state);
-gboolean chat_session_get_sent(char *recipient);
-void chat_session_sent(char *recipient);
+void chat_session_start(const char * const recipient);
+void chat_session_end(const char * const recipient);
+chat_state_t chat_session_get_state(const char * const recipient);
+void chat_session_set_state(const char * const recipient, chat_state_t state);
+gboolean chat_session_get_sent(const char * const recipient);
+void chat_session_sent(const char * const recipient);
 
 #endif