From 89cb29afede55d2813a2bf0a8ca08be799b3c050 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 20 Sep 2013 20:35:03 -0400 Subject: [PATCH] moved misc helper functions to separate file and removed redundant includes --- build/Makefile.am | 4 +- src/chat.c | 73 +--------------------------------- src/chat.h | 4 -- src/commands.c | 5 +-- src/friendlist.c | 2 - src/groupchat.c | 5 +-- src/main.c | 3 +- src/misc_tools.c | 97 +++++++++++++++++++++++++++++++++++++++++++++ src/misc_tools.h | 18 +++++++++ src/prompt.c | 30 +++----------- src/prompt.h | 4 -- src/toxic_windows.h | 4 +- 12 files changed, 131 insertions(+), 118 deletions(-) create mode 100644 src/misc_tools.c create mode 100644 src/misc_tools.h diff --git a/build/Makefile.am b/build/Makefile.am index 520af77..153235d 100644 --- a/build/Makefile.am +++ b/build/Makefile.am @@ -15,7 +15,9 @@ toxic_SOURCES = $(top_srcdir)/src/main.c \ $(top_srcdir)/src/groupchat.c \ $(top_srcdir)/src/groupchat.h \ $(top_srcdir)/src/commands.c \ - $(top_srcdir)/src/commands.h + $(top_srcdir)/src/commands.h \ + $(top_srcdir)/src/misc_tools.c \ + $(top_srcdir)/src/misc_tools.h toxic_CFLAGS = -I$(top_srcdir) \ $(NCURSES_CFLAGS) \ diff --git a/src/chat.c b/src/chat.c index 27567c4..e574548 100644 --- a/src/chat.c +++ b/src/chat.c @@ -8,29 +8,16 @@ #include #include -#include -#include -#include #include -#include #include "toxic_windows.h" #include "friendlist.h" -#include "chat.h" #include "commands.h" +#include "misc_tools.h" extern char *DATA_FILE; extern int store_data(Tox *m, char *path); -struct tm *get_time(void) -{ - struct tm *timeinfo; - time_t now; - time(&now); - timeinfo = localtime(&now); - return timeinfo; -} - static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) { if (self->num != num) @@ -54,7 +41,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 beep(); } -void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) +static void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) { if (self->num != num) return; @@ -113,62 +100,6 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status snprintf(statusbar->statusmsg, len, "%s", status); } -/* check that the string has one non-space character */ -int string_is_empty(char *string) -{ - int rc = 0; - char *copy = strdup(string); - rc = ((strtok(copy, " ") == NULL) ? 1 : 0); - free(copy); - return rc; -} - -/* convert wide characters to null terminated string */ -uint8_t *wcs_to_char(wchar_t *string) -{ - size_t len = 0; - char *ret = NULL; - - len = wcstombs(NULL, string, 0); - if (len != (size_t) -1) { - len++; - ret = malloc(len); - if (ret != NULL) - wcstombs(ret, string, len); - } else { - ret = malloc(2); - if (ret != NULL) { - ret[0] = ' '; - ret[1] = '\0'; - } - } - - if (ret == NULL) { - endwin(); - fprintf(stderr, "malloc() failed. Aborting...\n"); - exit(EXIT_FAILURE); - } - - return ret; -} - -/* convert a wide char to null terminated string */ -char *wc_to_char(wchar_t ch) -{ - int len = 0; - static char ret[MB_LEN_MAX + 1]; - - len = wctomb(ret, ch); - if (len == -1) { - ret[0] = ' '; - ret[1] = '\0'; - } else { - ret[len] = '\0'; - } - - return ret; -} - static void print_chat_help(ChatContext *ctx) { wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD); diff --git a/src/chat.h b/src/chat.h index 10405ec..25e64b7 100644 --- a/src/chat.h +++ b/src/chat.h @@ -1,10 +1,6 @@ #ifndef CHAT_H_6489PZ13 #define CHAT_H_6489PZ13 -struct tm *get_time(void); -char *wc_to_char(wchar_t ch); -uint8_t *wcs_to_char(wchar_t *string); -int string_is_empty(char *string); ToxWindow new_chat(Tox *m, ToxWindow *prompt, int friendnum); #endif /* end of include guard: CHAT_H_6489PZ13 */ diff --git a/src/commands.c b/src/commands.c index 55e1fdb..ab73577 100644 --- a/src/commands.c +++ b/src/commands.c @@ -8,10 +8,9 @@ #include #include -#include #include "toxic_windows.h" -#include "prompt.h" +#include "misc_tools.h" #include "commands.h" extern char *DATA_FILE; @@ -513,4 +512,4 @@ void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len /* no match */ free(cmdargs); wprintw(window, "Invalid command.\n"); -} \ No newline at end of file +} diff --git a/src/friendlist.c b/src/friendlist.c index c8c6377..0142f4e 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -8,8 +8,6 @@ #include #include -#include -#include #include #include diff --git a/src/groupchat.c b/src/groupchat.c index 28d4515..41751eb 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -8,14 +8,11 @@ #include #include -#include -#include -#include #include #include "toxic_windows.h" -#include "chat.h" #include "commands.h" +#include "misc_tools.h" static GroupChat groupchats[MAX_GROUPCHAT_NUM]; static int group_chat_index = 0; diff --git a/src/main.c b/src/main.c index b28a02e..ca1a8a5 100644 --- a/src/main.c +++ b/src/main.c @@ -35,8 +35,9 @@ #include "configdir.h" #include "toxic_windows.h" -#include "prompt.h" #include "friendlist.h" +#include "prompt.h" +#include "misc_tools.h" #ifndef PACKAGE_DATADIR #define PACKAGE_DATADIR "." diff --git a/src/misc_tools.c b/src/misc_tools.c new file mode 100644 index 0000000..ff807d1 --- /dev/null +++ b/src/misc_tools.c @@ -0,0 +1,97 @@ +/* + * Toxic -- Tox Curses Client + */ + +#include +#include +#include +#include + +#include "toxic_windows.h" + +// XXX: FIX +unsigned char *hex_string_to_bin(char hex_string[]) +{ + size_t len = strlen(hex_string); + unsigned char *val = malloc(len); + + if (val == NULL) { + endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); + exit(EXIT_FAILURE); + } + + char *pos = hex_string; + size_t i; + + for (i = 0; i < len; ++i, pos += 2) + sscanf(pos, "%2hhx", &val[i]); + + return val; +} + +/* Get the current local time */ +struct tm *get_time(void) +{ + struct tm *timeinfo; + time_t now; + time(&now); + timeinfo = localtime(&now); + return timeinfo; +} + +/* check that the string has one non-space character */ +int string_is_empty(char *string) +{ + int rc = 0; + char *copy = strdup(string); + rc = ((strtok(copy, " ") == NULL) ? 1 : 0); + free(copy); + return rc; +} + +/* convert wide characters to null terminated string */ +uint8_t *wcs_to_char(wchar_t *string) +{ + size_t len = 0; + char *ret = NULL; + + len = wcstombs(NULL, string, 0); + if (len != (size_t) -1) { + len++; + ret = malloc(len); + if (ret != NULL) + wcstombs(ret, string, len); + } else { + ret = malloc(2); + if (ret != NULL) { + ret[0] = ' '; + ret[1] = '\0'; + } + } + + if (ret == NULL) { + endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); + exit(EXIT_FAILURE); + } + + return ret; +} + +/* convert a wide char to null terminated string */ +char *wc_to_char(wchar_t ch) +{ + int len = 0; + static char ret[MB_LEN_MAX + 1]; + + len = wctomb(ret, ch); + if (len == -1) { + ret[0] = ' '; + ret[1] = '\0'; + } else { + ret[len] = '\0'; + } + + return ret; +} diff --git a/src/misc_tools.h b/src/misc_tools.h new file mode 100644 index 0000000..52ca967 --- /dev/null +++ b/src/misc_tools.h @@ -0,0 +1,18 @@ +/* + * Toxic -- Tox Curses Client + */ + +/* convert a hex string to binary */ +unsigned char *hex_string_to_bin(char hex_string[]); + +/* get the current local time */ +struct tm *get_time(void); + +/* check that the string has one non-space character */ +int string_is_empty(char *string); + +/* convert wide characters to null terminated string */ +uint8_t *wcs_to_char(wchar_t *string); + +/* convert a wide char to null terminated string */ +char *wc_to_char(wchar_t ch); diff --git a/src/prompt.c b/src/prompt.c index 149b3f6..3ff396e 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -8,10 +8,11 @@ #include #include -#include +#include "toxic_windows.h" #include "prompt.h" #include "commands.h" +#include "misc_tools.h" uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE]; uint8_t num_frnd_requests = 0; @@ -76,28 +77,7 @@ int add_group_req(uint8_t *group_pub_key) return -1; } -// XXX: FIX -unsigned char *hex_string_to_bin(char hex_string[]) -{ - size_t len = strlen(hex_string); - unsigned char *val = malloc(len); - - if (val == NULL) { - endwin(); - fprintf(stderr, "malloc() failed. Aborting...\n"); - exit(EXIT_FAILURE); - } - - char *pos = hex_string; - size_t i; - - for (i = 0; i < len; ++i, pos += 2) - sscanf(pos, "%2hhx", &val[i]); - - return val; -} - -void print_prompt_help(ToxWindow *self) +static void print_prompt_help(ToxWindow *self) { wclear(self->window); wattron(self->window, COLOR_PAIR(CYAN) | A_BOLD); @@ -235,7 +215,7 @@ static void prompt_onInit(ToxWindow *self, Tox *m) wclrtoeol(self->window); } -void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, uint16_t length) +static void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, uint16_t length) { int n = add_friend_req(key); @@ -259,7 +239,7 @@ void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, uint16 beep(); } -void prompt_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *group_pub_key) +static void prompt_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *group_pub_key) { if (friendnumber < 0) return; diff --git a/src/prompt.h b/src/prompt.h index 4ea4762..e5bb5c5 100644 --- a/src/prompt.h +++ b/src/prompt.h @@ -5,11 +5,7 @@ #ifndef PROMPT_H_UZYGWFFL #define PROMPT_H_UZYGWFFL -#include "toxic_windows.h" - ToxWindow new_prompt(void); -int add_req(uint8_t *public_key); -unsigned char *hex_string_to_bin(char hex_string[]); void prompt_init_statusbar(ToxWindow *self, Tox *m); void prompt_update_nick(ToxWindow *prompt, uint8_t *nick, uint16_t len); void prompt_update_statusmessage(ToxWindow *prompt, uint8_t *statusmsg, uint16_t len); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index ccd1d2c..2a8ddfa 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -6,12 +6,10 @@ #define _windows_h #ifndef TOXICVER -#define TOXICVER "NOVER" /* Use the -D flag to set this */ +#define TOXICVER "NOVER_" /* Use the -D flag to set this */ #endif #include -#include -#include #include #include