mirror of
https://github.com/Tha14/toxic.git
synced 2024-12-23 12:33:25 +01:00
moved misc helper functions to separate file and removed redundant includes
This commit is contained in:
parent
ec6b37e09e
commit
89cb29afed
@ -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) \
|
||||
|
73
src/chat.c
73
src/chat.c
@ -8,29 +8,16 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
|
||||
#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);
|
||||
|
@ -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 */
|
||||
|
@ -8,10 +8,9 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#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");
|
||||
}
|
||||
}
|
||||
|
@ -8,8 +8,6 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <tox/tox.h>
|
||||
|
@ -8,14 +8,11 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <time.h>
|
||||
|
||||
#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;
|
||||
|
@ -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 "."
|
||||
|
97
src/misc_tools.c
Normal file
97
src/misc_tools.c
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Toxic -- Tox Curses Client
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
|
||||
#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;
|
||||
}
|
18
src/misc_tools.h
Normal file
18
src/misc_tools.h
Normal file
@ -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);
|
30
src/prompt.c
30
src/prompt.c
@ -8,10 +8,11 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#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;
|
||||
|
@ -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);
|
||||
|
@ -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 <curses.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <wctype.h>
|
||||
#include <wchar.h>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user