From ecabb04881c8e20770765d8409fb8a30ff6fbd75 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 24 Apr 2012 00:40:10 +0100 Subject: [PATCH 1/8] Added glib to Makefile --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 706fe2d2..698939ec 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ CC = gcc WARNS = -Werror -Wall -Wextra -Wno-unused-parameter -Wno-unused-but-set-variable -LIBS = -lxml2 -lexpat -lssl -lresolv -lncurses -L ~/lib -lstrophe +LIBS = -lxml2 -lexpat -lssl -lresolv -lncurses -L ~/lib -lstrophe `pkg-config --libs glib-2.0` TESTLIB = -L ~/lib -l headunit CPPLIB = -lstdc++ -CFLAGS = -I ~/include -O3 $(WARNS) $(LIBS) +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 main.o TESTOBJS = test_history.o history.o test_contact_list.o contact_list.o \ From e21e64fe28fd05f0b06e92fb3c9ea7ead24a4c90 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 24 Apr 2012 00:49:48 +0100 Subject: [PATCH 2/8] Removed ncurses.h from command.c --- command.c | 1 - 1 file changed, 1 deletion(-) diff --git a/command.c b/command.c index 19b0a9b7..7f45a445 100644 --- a/command.c +++ b/command.c @@ -22,7 +22,6 @@ #include #include -#include #include "command.h" #include "contact_list.h" #include "history.h" From b3de79997b6c7c5f8536e0c94cb60df4f36a553a Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 24 Apr 2012 00:50:43 +0100 Subject: [PATCH 3/8] Removed commented code from main --- main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/main.c b/main.c index 392b9ef6..520fb4bb 100644 --- a/main.c +++ b/main.c @@ -47,7 +47,6 @@ int main(int argc, char **argv) profanity_init(disable_tls); profanity_run(); - //profanity_shutdown(); return 0; } From 44266b19a190260b0d565920ec014948f549b9a6 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 24 Apr 2012 00:51:42 +0100 Subject: [PATCH 4/8] Removed ncurses.h from profanity.c --- profanity.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/profanity.c b/profanity.c index 2e2e27fa..6e9c70cf 100644 --- a/profanity.c +++ b/profanity.c @@ -23,8 +23,6 @@ #include #include -#include - #include "profanity.h" #include "log.h" #include "windows.h" From 04e021eb363798e4ab01777ac0bc725994d8bd3a Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 24 Apr 2012 01:02:22 +0100 Subject: [PATCH 5/8] Usage of gboolean in command.c --- command.c | 48 ++++++++++++++++++++++++++---------------------- command.h | 2 +- profanity.c | 3 ++- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/command.c b/command.c index 7f45a445..11e4667d 100644 --- a/command.c +++ b/command.c @@ -22,6 +22,9 @@ #include #include + +#include + #include "command.h" #include "contact_list.h" #include "history.h" @@ -29,19 +32,20 @@ #include "windows.h" #include "util.h" -static int _handle_command(const char * const command, const char * const inp); -static int _cmd_quit(void); -static int _cmd_help(void); -static int _cmd_who(void); -static int _cmd_ros(void); -static int _cmd_connect(const char * const inp); -static int _cmd_msg(const char * const inp); -static int _cmd_close(const char * const inp); -static int _cmd_default(const char * const inp); +static gboolean _handle_command(const char * const command, + const char * const inp); +static gboolean _cmd_quit(void); +static gboolean _cmd_help(void); +static gboolean _cmd_who(void); +static gboolean _cmd_ros(void); +static gboolean _cmd_connect(const char * const inp); +static gboolean _cmd_msg(const char * const inp); +static gboolean _cmd_close(const char * const inp); +static gboolean _cmd_default(const char * const inp); -int process_input(char *inp) +gboolean process_input(char *inp) { - int result = FALSE; + gboolean result = FALSE; if (strlen(inp) > 0) history_append(inp); @@ -65,9 +69,9 @@ int process_input(char *inp) return result; } -static int _handle_command(const char * const command, const char * const inp) +static gboolean _handle_command(const char * const command, const char * const inp) { - int result = FALSE; + gboolean result = FALSE; if (strcmp(command, "/quit") == 0) { result = _cmd_quit(); @@ -90,9 +94,9 @@ static int _handle_command(const char * const command, const char * const inp) return result; } -static int _cmd_connect(const char * const inp) +static gboolean _cmd_connect(const char * const inp) { - int result = FALSE; + gboolean result = FALSE; jabber_status_t conn_status = jabber_connection_status(); if ((conn_status != JABBER_DISCONNECTED) && (conn_status != JABBER_STARTED)) { @@ -123,19 +127,19 @@ static int _cmd_connect(const char * const inp) return result; } -static int _cmd_quit(void) +static gboolean _cmd_quit(void) { return FALSE; } -static int _cmd_help(void) +static gboolean _cmd_help(void) { cons_help(); return TRUE; } -static int _cmd_ros(void) +static gboolean _cmd_ros(void) { jabber_status_t conn_status = jabber_connection_status(); @@ -147,7 +151,7 @@ static int _cmd_ros(void) return TRUE; } -static int _cmd_who(void) +static gboolean _cmd_who(void) { jabber_status_t conn_status = jabber_connection_status(); @@ -161,7 +165,7 @@ static int _cmd_who(void) return TRUE; } -static int _cmd_msg(const char * const inp) +static gboolean _cmd_msg(const char * const inp) { char *usr = NULL; char *msg = NULL; @@ -195,7 +199,7 @@ static int _cmd_msg(const char * const inp) return TRUE; } -static int _cmd_close(const char * const inp) +static gboolean _cmd_close(const char * const inp) { if (!win_close_win()) cons_bad_command(inp); @@ -203,7 +207,7 @@ static int _cmd_close(const char * const inp) return TRUE; } -static int _cmd_default(const char * const inp) +static gboolean _cmd_default(const char * const inp) { if (win_in_chat()) { char *recipient = win_get_recipient(); diff --git a/command.h b/command.h index 7efc4acf..d0d57022 100644 --- a/command.h +++ b/command.h @@ -24,6 +24,6 @@ #define COMMAND_H void cmd_init(void); -int process_input(char *inp); +gboolean process_input(char *inp); #endif diff --git a/profanity.c b/profanity.c index 6e9c70cf..de26f8b8 100644 --- a/profanity.c +++ b/profanity.c @@ -22,6 +22,7 @@ #include #include +#include #include "profanity.h" #include "log.h" @@ -34,7 +35,7 @@ static void _profanity_shutdown(void); void profanity_run(void) { - int cmd_result = TRUE; + gboolean cmd_result = TRUE; inp_non_block(); while(cmd_result == TRUE) { From 308d7fa945a51bb28d07ea97a437b310ab297c5f Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 26 Apr 2012 23:13:10 +0100 Subject: [PATCH 6/8] Added trim tests --- test_util.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ util.c | 3 +++ 2 files changed, 68 insertions(+) diff --git a/test_util.c b/test_util.c index ae936d9a..d29a139b 100644 --- a/test_util.c +++ b/test_util.c @@ -1,4 +1,5 @@ #include +#include #include #include "util.h" @@ -145,6 +146,64 @@ void replace_when_new_null(void) assert_string_equals("hello", result); } +void trim_when_no_whitespace_returns_same(void) +{ + char *str = malloc((strlen("hi there") + 1) * sizeof(char)); + strcpy(str, "hi there"); + char *result = trim(str); + + assert_string_equals("hi there", result); + free(str); +} + +void trim_when_space_at_start(void) +{ + char *str = malloc((strlen(" hi there") + 1) * sizeof(char)); + strcpy(str, " hi there"); + char *result = trim(str); + + assert_string_equals("hi there", result); + free(str); +} + +void trim_when_space_at_end(void) +{ + char *str = malloc((strlen("hi there ") + 1) * sizeof(char)); + strcpy(str, "hi there "); + char *result = trim(str); + + assert_string_equals("hi there", result); + free(str); +} + +void trim_when_space_at_start_and_end(void) +{ + char *str = malloc((strlen(" hi there ") + 1) * sizeof(char)); + strcpy(str, " hi there "); + char *result = trim(str); + + assert_string_equals("hi there", result); + free(str); +} + +void trim_when_empty(void) +{ + char *str = malloc((strlen("") + 1) * sizeof(char)); + strcpy(str, ""); + char *result = trim(str); + + assert_string_equals("", result); + free(str); +} + +void trim_when_null(void) +{ + char *str = NULL; + trim(str); + + assert_is_null(str); +} + void register_util_tests(void) { TEST_MODULE("util tests"); @@ -161,4 +220,10 @@ void register_util_tests(void) TEST(replace_when_sub_null); TEST(replace_when_new_empty); TEST(replace_when_new_null); + TEST(trim_when_no_whitespace_returns_same); + TEST(trim_when_space_at_start); + TEST(trim_when_space_at_end); + TEST(trim_when_space_at_start_and_end); + TEST(trim_when_empty); + TEST(trim_when_null); } diff --git a/util.c b/util.c index bfe21263..b4c152a4 100644 --- a/util.c +++ b/util.c @@ -38,6 +38,9 @@ void get_time(char *thetime) char *trim(char *str) { + if (str == NULL) + return NULL; + char *end; while (isspace(*str)) From 381d4b8dc9cb77640e5968c15c2fdf08aa17d6d7 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 26 Apr 2012 23:19:28 +0100 Subject: [PATCH 7/8] Replaced trim implementation with g_strstrip --- command.c | 2 +- test_util.c | 4 ++-- util.c | 18 +++--------------- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/command.c b/command.c index 11e4667d..ae27ac35 100644 --- a/command.c +++ b/command.c @@ -53,7 +53,7 @@ gboolean process_input(char *inp) if (strlen(inp) == 0) { result = TRUE; } else if (inp[0] == '/') { - inp = trim(inp); + trim(inp); char inp_cpy[strlen(inp) + 1]; strcpy(inp_cpy, inp); char *command = strtok(inp_cpy, " "); diff --git a/test_util.c b/test_util.c index d29a139b..4888f676 100644 --- a/test_util.c +++ b/test_util.c @@ -199,9 +199,9 @@ void trim_when_empty(void) void trim_when_null(void) { char *str = NULL; - trim(str); + char *result = trim(str); - assert_is_null(str); + assert_is_null(result); } void register_util_tests(void) diff --git a/util.c b/util.c index b4c152a4..790c6a24 100644 --- a/util.c +++ b/util.c @@ -25,6 +25,8 @@ #include #include +#include + void get_time(char *thetime) { time_t rawtime; @@ -41,21 +43,7 @@ char *trim(char *str) if (str == NULL) return NULL; - char *end; - - while (isspace(*str)) - str++; - - if (*str == 0) - return str; - - end = str + strlen(str) - 1; - while (end > str && isspace(*end)) - end--; - - *(end+1) = 0; - - return str; + return g_strstrip(str); } char * str_replace (const char *string, const char *substr, From a41dab3d3c8ba290e2ae5c715844d9432ec9220b Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 26 Apr 2012 23:27:40 +0100 Subject: [PATCH 8/8] Removed trim method from util --- command.c | 3 ++- test_util.c | 64 ----------------------------------------------------- util.c | 10 --------- util.h | 1 - 4 files changed, 2 insertions(+), 76 deletions(-) diff --git a/command.c b/command.c index ae27ac35..e0e75009 100644 --- a/command.c +++ b/command.c @@ -47,13 +47,14 @@ gboolean process_input(char *inp) { gboolean result = FALSE; + g_strstrip(inp); + if (strlen(inp) > 0) history_append(inp); if (strlen(inp) == 0) { result = TRUE; } else if (inp[0] == '/') { - trim(inp); char inp_cpy[strlen(inp) + 1]; strcpy(inp_cpy, inp); char *command = strtok(inp_cpy, " "); diff --git a/test_util.c b/test_util.c index 4888f676..ffe12506 100644 --- a/test_util.c +++ b/test_util.c @@ -146,64 +146,6 @@ void replace_when_new_null(void) assert_string_equals("hello", result); } -void trim_when_no_whitespace_returns_same(void) -{ - char *str = malloc((strlen("hi there") + 1) * sizeof(char)); - strcpy(str, "hi there"); - char *result = trim(str); - - assert_string_equals("hi there", result); - free(str); -} - -void trim_when_space_at_start(void) -{ - char *str = malloc((strlen(" hi there") + 1) * sizeof(char)); - strcpy(str, " hi there"); - char *result = trim(str); - - assert_string_equals("hi there", result); - free(str); -} - -void trim_when_space_at_end(void) -{ - char *str = malloc((strlen("hi there ") + 1) * sizeof(char)); - strcpy(str, "hi there "); - char *result = trim(str); - - assert_string_equals("hi there", result); - free(str); -} - -void trim_when_space_at_start_and_end(void) -{ - char *str = malloc((strlen(" hi there ") + 1) * sizeof(char)); - strcpy(str, " hi there "); - char *result = trim(str); - - assert_string_equals("hi there", result); - free(str); -} - -void trim_when_empty(void) -{ - char *str = malloc((strlen("") + 1) * sizeof(char)); - strcpy(str, ""); - char *result = trim(str); - - assert_string_equals("", result); - free(str); -} - -void trim_when_null(void) -{ - char *str = NULL; - char *result = trim(str); - - assert_is_null(result); -} - void register_util_tests(void) { TEST_MODULE("util tests"); @@ -220,10 +162,4 @@ void register_util_tests(void) TEST(replace_when_sub_null); TEST(replace_when_new_empty); TEST(replace_when_new_null); - TEST(trim_when_no_whitespace_returns_same); - TEST(trim_when_space_at_start); - TEST(trim_when_space_at_end); - TEST(trim_when_space_at_start_and_end); - TEST(trim_when_empty); - TEST(trim_when_null); } diff --git a/util.c b/util.c index 790c6a24..58f4fbf1 100644 --- a/util.c +++ b/util.c @@ -25,8 +25,6 @@ #include #include -#include - void get_time(char *thetime) { time_t rawtime; @@ -38,14 +36,6 @@ void get_time(char *thetime) strftime(thetime, 80, "%H:%M", timeinfo); } -char *trim(char *str) -{ - if (str == NULL) - return NULL; - - return g_strstrip(str); -} - char * str_replace (const char *string, const char *substr, const char *replacement) { char *tok = NULL; diff --git a/util.h b/util.h index 0982f816..4326fbb4 100644 --- a/util.h +++ b/util.h @@ -24,7 +24,6 @@ #define UTIL_H void get_time(char *thetime); -char * trim(char *str); char * str_replace(const char *string, const char *substr, const char *replacement);