1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Using const for chat sessions

Allocating memory for keys and values
This commit is contained in:
James Booth 2012-10-14 19:36:45 +01:00
parent 43a7f58850
commit d2be692992
2 changed files with 20 additions and 19 deletions

View File

@ -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;
}

View File

@ -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