diff options
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | contact.c | 61 | ||||
-rw-r--r-- | contact.h | 13 | ||||
-rw-r--r-- | contact_list.c | 95 | ||||
-rw-r--r-- | contact_list.h | 4 | ||||
-rw-r--r-- | test_contact_list.c | 139 | ||||
-rw-r--r-- | windows.c | 15 |
7 files changed, 189 insertions, 147 deletions
diff --git a/Makefile b/Makefile index 61210e29..c7429f9e 100644 --- a/Makefile +++ b/Makefile @@ -7,8 +7,8 @@ CPPLIB = -lstdc++ CFLAGS = -I ~/include -O3 $(WARNS) $(LIBS) `pkg-config --cflags glib-2.0` OBJS = log.o windows.o title_bar.o status_bar.o input_win.o jabber.o \ profanity.o util.o command.o history.o contact_list.o prof_history.o \ - main.o -TESTOBJS = test_contact_list.o contact_list.o \ + contact.o main.o +TESTOBJS = test_contact_list.o contact_list.o contact.o \ test_util.o test_prof_history.o prof_history.o util.o profanity: $(OBJS) @@ -24,11 +24,12 @@ profanity.o: log.h windows.h jabber.h command.h util.o: util.h command.o: command.h util.h history.h contact_list.h history.o: history.h prof_history.h -contact_list.o: contact_list.h +contact_list.o: contact_list.h contact.h prof_history.o: prof_history.h +contact.o: contact.h main.o: profanity.h -test_contact_list.o: contact_list.h +test_contact_list.o: contact_list.h contact.h test_util.o: util.h test_prof_history.o: prof_history.h diff --git a/contact.c b/contact.c new file mode 100644 index 00000000..621d029e --- /dev/null +++ b/contact.c @@ -0,0 +1,61 @@ +#include <stdlib.h> +#include <string.h> + +#include "contact.h" + +struct p_contact_t { + char *name; + char *show; + char *status; +}; + +PContact p_contact_new(const char * const name, const char * const show, + const char * const status) +{ + PContact contact = malloc(sizeof(struct p_contact_t)); + contact->name = strdup(name); + + if (show == NULL || (strcmp(show, "") == 0)) + contact->show = strdup("online"); + else + contact->show = strdup(show); + + if (status != NULL) + contact->status = strdup(status); + else + contact->status = NULL; + + return contact; +} + +void p_contact_free(PContact contact) +{ + free(contact->name); + + if (contact->show != NULL) { + free(contact->show); + contact->show = NULL; + } + if (contact->status != NULL) { + free(contact->status); + contact->status = NULL; + } + + free(contact); + contact = NULL; +} + +char * p_contact_name(PContact contact) +{ + return contact->name; +} + +char * p_contact_show(PContact contact) +{ + return contact->show; +} + +char * p_contact_status(PContact contact) +{ + return contact->status; +} diff --git a/contact.h b/contact.h new file mode 100644 index 00000000..1e2796d8 --- /dev/null +++ b/contact.h @@ -0,0 +1,13 @@ +#ifndef CONTACT_H +#define CONTACT_H + +typedef struct p_contact_t *PContact; + +PContact p_contact_new(const char * const name, const char * const show, + const char * const status); +void p_contact_free(PContact contact); +char * p_contact_name(PContact contact); +char * p_contact_show(PContact contact); +char * p_contact_status(PContact contact); + +#endif diff --git a/contact_list.c b/contact_list.c index bb2ad1a5..1715e594 100644 --- a/contact_list.c +++ b/contact_list.c @@ -24,6 +24,7 @@ #include <stdlib.h> #include <string.h> +#include "contact.h" #include "contact_list.h" // internal contact list @@ -37,9 +38,6 @@ static char * _search_str = NULL; static char * _search_contact_list_from(struct contact_node_t * curr); static struct contact_node_t * _make_contact_node(const char * const name, const char * const show, const char * const status); -static struct contact_t * _new_contact(const char * const name, - const char * const show, const char * const status); -static void _destroy_contact(struct contact_t *contact); static struct contact_node_t * _copy_contact_list(struct contact_node_t *node); static void _insert_contact(struct contact_node_t *curr, struct contact_node_t *prev, const char * const name, @@ -51,8 +49,8 @@ void contact_list_clear(void) if (curr) { while(curr) { - struct contact_t *contact = curr->contact; - _destroy_contact(contact); + PContact contact = curr->contact; + p_contact_free(contact); curr = curr->next; } @@ -66,8 +64,8 @@ void contact_list_clear(void) void destroy_list(struct contact_node_t *list) { while(list) { - struct contact_t *contact = list->contact; - _destroy_contact(contact); + PContact contact = list->contact; + p_contact_free(contact); list = list->next; } @@ -93,8 +91,8 @@ int contact_list_remove(const char * const name) struct contact_node_t *prev = NULL; while(curr) { - struct contact_t *contact = curr->contact; - if (strcmp(contact->name, name) == 0) { + PContact contact = curr->contact; + if (strcmp(p_contact_name(contact), name) == 0) { if (prev) prev->next = curr->next; else @@ -102,10 +100,11 @@ int contact_list_remove(const char * const name) // reset last found if it points at the node to be removed if (_last_found != NULL) - if (strcmp(_last_found->contact->name, contact->name) == 0) + if (strcmp(p_contact_name(_last_found->contact), + p_contact_name(contact)) == 0) _last_found = NULL; - _destroy_contact(contact); + p_contact_free(contact); free(curr); return 1; @@ -132,16 +131,16 @@ int contact_list_add(const char * const name, const char * const show, struct contact_node_t *prev = NULL; while(curr) { - struct contact_t *curr_contact = curr->contact; + PContact curr_contact = curr->contact; // insert - if (strcmp(curr_contact->name, name) > 0) { + if (strcmp(p_contact_name(curr_contact), name) > 0) { _insert_contact(curr, prev, name, show, status); return 0; // update - } else if (strcmp(curr_contact->name, name) == 0) { - _destroy_contact(curr->contact); - curr->contact = _new_contact(name, show, status); + } else if (strcmp(p_contact_name(curr_contact), name) == 0) { + p_contact_free(curr->contact); + curr->contact = p_contact_new(name, show, status); return 0; } @@ -174,11 +173,11 @@ struct contact_node_t * _copy_contact_list(struct contact_node_t *node) if (node == NULL) { return NULL; } else { - struct contact_t *curr_contact = node->contact; + PContact curr_contact = node->contact; struct contact_node_t *copy = - _make_contact_node(curr_contact->name, - curr_contact->show, - curr_contact->status); + _make_contact_node(p_contact_name(curr_contact), + p_contact_show(curr_contact), + p_contact_status(curr_contact)); copy->next = _copy_contact_list(node->next); @@ -235,18 +234,21 @@ int get_size(struct contact_node_t *list) static char * _search_contact_list_from(struct contact_node_t * curr) { while(curr) { - struct contact_t *curr_contact = curr->contact; + PContact curr_contact = curr->contact; // match found - if (strncmp(curr_contact->name, _search_str, strlen(_search_str)) == 0) { + if (strncmp(p_contact_name(curr_contact), + _search_str, + strlen(_search_str)) == 0) { char *result = - (char *) malloc((strlen(curr_contact->name) + 1) * sizeof(char)); + (char *) malloc((strlen(p_contact_name(curr_contact)) + 1) + * sizeof(char)); // set pointer to last found _last_found = curr; // return the contact, must be free'd by caller - strcpy(result, curr_contact->name); + strcpy(result, p_contact_name(curr_contact)); return result; } @@ -261,55 +263,12 @@ static struct contact_node_t * _make_contact_node(const char * const name, { struct contact_node_t *new = (struct contact_node_t *) malloc(sizeof(struct contact_node_t)); - new->contact = _new_contact(name, show, status); + new->contact = p_contact_new(name, show, status); new->next = NULL; return new; } -static struct contact_t * _new_contact(const char * const name, - const char * const show, const char * const status) -{ - struct contact_t *new = - (struct contact_t *) malloc(sizeof(struct contact_t)); - new->name = (char *) malloc((strlen(name) + 1) * sizeof(char)); - strcpy(new->name, name); - - if (show == NULL || (strcmp(show, "") == 0)) { - new->show = (char *) malloc((strlen("online") + 1) * sizeof(char)); - strcpy(new->show, "online"); - } else { - new->show = (char *) malloc((strlen(show) + 1) * sizeof(char)); - strcpy(new->show, show); - } - - if (status != NULL) { - new->status = (char *) malloc((strlen(status) + 1) * sizeof(char)); - strcpy(new->status, status); - } else { - new->status = NULL; - } - - return new; -} - -static void _destroy_contact(struct contact_t *contact) -{ - free(contact->name); - - if (contact->show != NULL) { - free(contact->show); - contact->show = NULL; - } - if (contact->status != NULL) { - free(contact->status); - contact->status = NULL; - } - - free(contact); - contact = NULL; -} - static void _insert_contact(struct contact_node_t *curr, struct contact_node_t *prev, const char * const name, const char * const show, const char * const status) diff --git a/contact_list.h b/contact_list.h index ba44738e..32021528 100644 --- a/contact_list.h +++ b/contact_list.h @@ -23,6 +23,8 @@ #ifndef CONTACT_LIST_H #define CONTACT_LIST_H +#include "contact.h" + struct contact_t { char *name; char *show; @@ -30,7 +32,7 @@ struct contact_t { }; struct contact_node_t { - struct contact_t *contact; + PContact contact; struct contact_node_t *next; }; diff --git a/test_contact_list.c b/test_contact_list.c index 39fbd983..106f6438 100644 --- a/test_contact_list.c +++ b/test_contact_list.c @@ -1,7 +1,10 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> + #include <head-unit.h> + +#include "contact.h" #include "contact_list.h" static void beforetest(void) @@ -33,9 +36,9 @@ static void first_element_correct(void) { contact_list_add("James", NULL, NULL); struct contact_node_t *list = get_contact_list(); - struct contact_t *james = list->contact; + PContact james = list->contact; - assert_string_equals("James", james->name); + assert_string_equals("James", p_contact_name(james)); destroy_list(list); } @@ -55,11 +58,11 @@ static void first_and_second_elements_correct(void) contact_list_add("Dave", NULL, NULL); struct contact_node_t *list = get_contact_list(); - struct contact_t *dave = list->contact; - struct contact_t *james = (list->next)->contact; + PContact dave = list->contact; + PContact james = (list->next)->contact; - assert_string_equals("James", james->name); - assert_string_equals("Dave", dave->name); + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Dave", p_contact_name(dave)); destroy_list(list); } @@ -80,13 +83,13 @@ static void first_three_elements_correct(void) contact_list_add("Dave", NULL, NULL); contact_list_add("James", NULL, NULL); struct contact_node_t *list = get_contact_list(); - struct contact_t *bob = list->contact; - struct contact_t *dave = (list->next)->contact; - struct contact_t *james = ((list->next)->next)->contact; + PContact bob = list->contact; + PContact dave = (list->next)->contact; + PContact james = ((list->next)->next)->contact; - assert_string_equals("James", james->name); - assert_string_equals("Dave", dave->name); - assert_string_equals("Bob", bob->name); + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Dave", p_contact_name(dave)); + assert_string_equals("Bob", p_contact_name(bob)); destroy_list(list); } @@ -97,14 +100,14 @@ static void add_twice_at_beginning_adds_once(void) contact_list_add("Dave", NULL, NULL); contact_list_add("Bob", NULL, NULL); struct contact_node_t *list = get_contact_list(); - struct contact_t *bob = list->contact; - struct contact_t *dave = (list->next)->contact; - struct contact_t *james = ((list->next)->next)->contact; + PContact bob = list->contact; + PContact dave = (list->next)->contact; + PContact james = ((list->next)->next)->contact; assert_int_equals(3, get_size(list)); - assert_string_equals("James", james->name); - assert_string_equals("Dave", dave->name); - assert_string_equals("Bob", bob->name); + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Dave", p_contact_name(dave)); + assert_string_equals("Bob", p_contact_name(bob)); destroy_list(list); } @@ -115,14 +118,14 @@ static void add_twice_in_middle_adds_once(void) contact_list_add("James", NULL, NULL); contact_list_add("Bob", NULL, NULL); struct contact_node_t *list = get_contact_list(); - struct contact_t *bob = list->contact; - struct contact_t *dave = (list->next)->contact; - struct contact_t *james = ((list->next)->next)->contact; + PContact bob = list->contact; + PContact dave = (list->next)->contact; + PContact james = ((list->next)->next)->contact; assert_int_equals(3, get_size(list)); - assert_string_equals("James", james->name); - assert_string_equals("Dave", dave->name); - assert_string_equals("Bob", bob->name); + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Dave", p_contact_name(dave)); + assert_string_equals("Bob", p_contact_name(bob)); destroy_list(list); } @@ -133,14 +136,14 @@ static void add_twice_at_end_adds_once(void) contact_list_add("Bob", NULL, NULL); contact_list_add("James", NULL, NULL); struct contact_node_t *list = get_contact_list(); - struct contact_t *bob = list->contact; - struct contact_t *dave = (list->next)->contact; - struct contact_t *james = ((list->next)->next)->contact; + PContact bob = list->contact; + PContact dave = (list->next)->contact; + PContact james = ((list->next)->next)->contact; assert_int_equals(3, get_size(list)); - assert_string_equals("James", james->name); - assert_string_equals("Dave", dave->name); - assert_string_equals("Bob", bob->name); + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Dave", p_contact_name(dave)); + assert_string_equals("Bob", p_contact_name(bob)); destroy_list(list); } @@ -172,8 +175,8 @@ static void remove_first_when_two(void) struct contact_node_t *list = get_contact_list(); assert_int_equals(1, get_size(list)); - struct contact_t *dave = list->contact; - assert_string_equals("Dave", dave->name); + PContact dave = list->contact; + assert_string_equals("Dave", p_contact_name(dave)); destroy_list(list); } @@ -186,8 +189,8 @@ static void remove_second_when_two(void) struct contact_node_t *list = get_contact_list(); assert_int_equals(1, get_size(list)); - struct contact_t *james = list->contact; - assert_string_equals("James", james->name); + PContact james = list->contact; + assert_string_equals("James", p_contact_name(james)); destroy_list(list); } @@ -201,11 +204,11 @@ static void remove_first_when_three(void) struct contact_node_t *list = get_contact_list(); assert_int_equals(2, get_size(list)); - struct contact_t *bob = list->contact; - struct contact_t *dave = (list->next)->contact; + PContact bob = list->contact; + PContact dave = (list->next)->contact; - assert_string_equals("Dave", dave->name); - assert_string_equals("Bob", bob->name); + assert_string_equals("Dave", p_contact_name(dave)); + assert_string_equals("Bob", p_contact_name(bob)); destroy_list(list); } @@ -219,11 +222,11 @@ static void remove_second_when_three(void) struct contact_node_t *list = get_contact_list(); assert_int_equals(2, get_size(list)); - struct contact_t *bob = list->contact; - struct contact_t *james = (list->next)->contact; + PContact bob = list->contact; + PContact james = (list->next)->contact; - assert_string_equals("James", james->name); - assert_string_equals("Bob", bob->name); + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Bob", p_contact_name(bob)); destroy_list(list); } @@ -237,11 +240,11 @@ static void remove_third_when_three(void) struct contact_node_t *list = get_contact_list(); assert_int_equals(2, get_size(list)); - struct contact_t *dave = list->contact; - struct contact_t *james = (list->next)->contact; + PContact dave = list->contact; + PContact james = (list->next)->contact; - assert_string_equals("James", james->name); - assert_string_equals("Dave", dave->name); + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Dave", p_contact_name(dave)); destroy_list(list); } @@ -249,9 +252,9 @@ static void test_show_when_value(void) { contact_list_add("James", "away", NULL); struct contact_node_t *list = get_contact_list(); - struct contact_t *james = list->contact; + PContact james = list->contact; - assert_string_equals("away", james->show); + assert_string_equals("away", p_contact_show(james)); destroy_list(list); } @@ -259,9 +262,9 @@ static void test_show_online_when_no_value(void) { contact_list_add("James", NULL, NULL); struct contact_node_t *list = get_contact_list(); - struct contact_t *james = list->contact; + PContact james = list->contact; - assert_string_equals("online", james->show); + assert_string_equals("online", p_contact_show(james)); destroy_list(list); } @@ -269,9 +272,9 @@ static void test_show_online_when_empty_string(void) { contact_list_add("James", "", NULL); struct contact_node_t *list = get_contact_list(); - struct contact_t *james = list->contact; + PContact james = list->contact; - assert_string_equals("online", james->show); + assert_string_equals("online", p_contact_show(james)); destroy_list(list); } @@ -279,9 +282,9 @@ static void test_status_when_value(void) { contact_list_add("James", NULL, "I'm not here right now"); struct contact_node_t *list = get_contact_list(); - struct contact_t *james = list->contact; + PContact james = list->contact; - assert_string_equals("I'm not here right now", james->status); + assert_string_equals("I'm not here right now", p_contact_status(james)); destroy_list(list); } @@ -289,9 +292,9 @@ static void test_status_when_no_value(void) { contact_list_add("James", NULL, NULL); struct contact_node_t *list = get_contact_list(); - struct contact_t *james = list->contact; + PContact james = list->contact; - assert_is_null(james->status); + assert_is_null(p_contact_status(james)); destroy_list(list); } @@ -302,9 +305,9 @@ static void update_show(void) struct contact_node_t *list = get_contact_list(); assert_int_equals(1, get_size(list)); - struct contact_t *james = list->contact; - assert_string_equals("James", james->name); - assert_string_equals("dnd", james->show); + PContact james = list->contact; + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("dnd", p_contact_show(james)); destroy_list(list); } @@ -315,9 +318,9 @@ static void set_show_to_null(void) struct contact_node_t *list = get_contact_list(); assert_int_equals(1, get_size(list)); - struct contact_t *james = list->contact; - assert_string_equals("James", james->name); - assert_string_equals("online", james->show); + PContact james = list->contact; + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("online", p_contact_show(james)); destroy_list(list); } @@ -328,9 +331,9 @@ static void update_status(void) struct contact_node_t *list = get_contact_list(); assert_int_equals(1, get_size(list)); - struct contact_t *james = list->contact; - assert_string_equals("James", james->name); - assert_string_equals("Gone to lunch", james->status); + PContact james = list->contact; + assert_string_equals("James", p_contact_name(james)); + assert_string_equals("Gone to lunch", p_contact_status(james)); destroy_list(list); } @@ -341,9 +344,9 @@ static void set_status_to_null(void) struct contact_node_t *list = get_contact_list(); assert_int_equals(1, get_size(list)); - struct contact_t *james = list->contact; - assert_string_equals("James", james->name); - assert_is_null(james->status); + PContact james = list->contact; + assert_string_equals("James", p_contact_name(james)); + assert_is_null(p_contact_status(james)); destroy_list(list); } diff --git a/windows.c b/windows.c index 6b3355c4..7af787d8 100644 --- a/windows.c +++ b/windows.c @@ -22,9 +22,12 @@ #include <string.h> #include <stdlib.h> + #include <ncurses.h> + #include "windows.h" #include "util.h" +#include "contact.h" #define CONS_WIN_TITLE "_cons" #define PAD_SIZE 200 @@ -299,14 +302,14 @@ void cons_show_online_contacts(struct contact_node_t *list) struct contact_node_t *curr = list; while(curr) { - struct contact_t *contact = curr->contact; + PContact contact = curr->contact; _win_show_time(_cons_win); wattron(_cons_win, COLOR_PAIR(2)); - wprintw(_cons_win, "%s", contact->name); - if (contact->show) - wprintw(_cons_win, " is %s", contact->show); - if (contact->status) - wprintw(_cons_win, ", \"%s\"", contact->status); + wprintw(_cons_win, "%s", p_contact_name(contact)); + if (p_contact_show(contact)) + wprintw(_cons_win, " is %s", p_contact_show(contact)); + if (p_contact_status(contact)) + wprintw(_cons_win, ", \"%s\"", p_contact_status(contact)); wprintw(_cons_win, "\n"); wattroff(_cons_win, COLOR_PAIR(2)); |