1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 15:03:27 +01:00

better error handling for failed string conversions

This commit is contained in:
Jfreegman 2013-12-12 00:37:29 -05:00
parent 12e1a60ca3
commit 1dad3711c4
3 changed files with 38 additions and 30 deletions

View File

@ -481,23 +481,27 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
static void chat_onDraw(ToxWindow *self, Tox *m) static void chat_onDraw(ToxWindow *self, Tox *m)
{ {
curs_set(1); curs_set(1);
int x, y; int x2, y2;
getmaxyx(self->window, y, x); getmaxyx(self->window, y2, x2);
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
wclear(ctx->linewin); wclear(ctx->linewin);
if (ctx->len > 0) {
uint8_t line[MAX_STR_SIZE]; uint8_t line[MAX_STR_SIZE];
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) {
memset(&line, 0, sizeof(line)); reset_buf(ctx->line, &ctx->pos, &ctx->len);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} else {
mvwprintw(ctx->linewin, 1, 0, "%s", line); mvwprintw(ctx->linewin, 1, 0, "%s", line);
}
}
/* Draw status bar */ /* Draw status bar */
StatusBar *statusbar = self->stb; StatusBar *statusbar = self->stb;
mvwhline(statusbar->topline, 1, 0, ACS_HLINE, x); mvwhline(statusbar->topline, 1, 0, ACS_HLINE, x2);
wmove(statusbar->topline, 0, 0); wmove(statusbar->topline, 0, 0);
/* Draw name, status and note in statusbar */ /* Draw name, status and note in statusbar */
@ -536,17 +540,17 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
} }
/* Reset statusbar->statusmsg on window resize */ /* Reset statusbar->statusmsg on window resize */
if (x != self->x) { if (x2 != self->x) {
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'}; uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
tox_get_status_message(m, self->num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); tox_get_status_message(m, self->num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
statusbar->statusmsg_len = tox_get_status_message_size(m, self->num); statusbar->statusmsg_len = tox_get_status_message_size(m, self->num);
} }
self->x = x; self->x = x2;
/* Truncate note if it doesn't fit in statusbar */ /* Truncate note if it doesn't fit in statusbar */
uint16_t maxlen = x - getcurx(statusbar->topline) - 4; uint16_t maxlen = x2 - getcurx(statusbar->topline) - 4;
if (statusbar->statusmsg_len > maxlen) { if (statusbar->statusmsg_len > maxlen) {
statusbar->statusmsg[maxlen] = '\0'; statusbar->statusmsg[maxlen] = '\0';
statusbar->statusmsg_len = maxlen; statusbar->statusmsg_len = maxlen;
@ -559,14 +563,14 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
} }
wprintw(statusbar->topline, "\n"); wprintw(statusbar->topline, "\n");
mvwhline(ctx->linewin, 0, 0, ACS_HLINE, x); mvwhline(ctx->linewin, 0, 0, ACS_HLINE, x2);
} }
static void chat_onInit(ToxWindow *self, Tox *m) static void chat_onInit(ToxWindow *self, Tox *m)
{ {
int x, y; int x2, y2;
getmaxyx(self->window, y, x); getmaxyx(self->window, y2, x2);
self->x = x; self->x = x2;
/* Init statusbar info */ /* Init statusbar info */
StatusBar *statusbar = self->stb; StatusBar *statusbar = self->stb;
@ -580,13 +584,13 @@ static void chat_onInit(ToxWindow *self, Tox *m)
/* Init subwindows */ /* Init subwindows */
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
statusbar->topline = subwin(self->window, 2, x, 0, 0); statusbar->topline = subwin(self->window, 2, x2, 0, 0);
ctx->history = subwin(self->window, y-CHATBOX_HEIGHT+1, x, 0, 0); ctx->history = subwin(self->window, y2-CHATBOX_HEIGHT+1, x2, 0, 0);
scrollok(ctx->history, 1); scrollok(ctx->history, 1);
ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x, y-CHATBOX_HEIGHT, 0); ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2-CHATBOX_HEIGHT, 0);
wprintw(ctx->history, "\n\n"); wprintw(ctx->history, "\n\n");
execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE);
wmove(self->window, y - CURS_Y_OFFSET, 0); wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} }
ToxWindow new_chat(Tox *m, int friendnum) ToxWindow new_chat(Tox *m, int friendnum)

View File

@ -438,12 +438,16 @@ static void groupchat_onDraw(ToxWindow *self, Tox *m)
wclear(ctx->linewin); wclear(ctx->linewin);
if (ctx->len > 0) {
uint8_t line[MAX_STR_SIZE]; uint8_t line[MAX_STR_SIZE];
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) {
memset(&line, 0, sizeof(line)); reset_buf(ctx->line, &ctx->pos, &ctx->len);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
} else {
mvwprintw(ctx->linewin, 1, 0, "%s", line); mvwprintw(ctx->linewin, 1, 0, "%s", line);
}
}
wclear(ctx->sidebar); wclear(ctx->sidebar);
mvwhline(ctx->linewin, 0, 0, ACS_HLINE, x2); mvwhline(ctx->linewin, 0, 0, ACS_HLINE, x2);

View File

@ -181,7 +181,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
int k = prt->orig_y + ((prt->len + p_ofst) / px2); int k = prt->orig_y + ((prt->len + p_ofst) / px2);
if (k == y2) { /* cursor is below max_y */ if (k >= y2) {
wprintw(self->window, "\n"); wprintw(self->window, "\n");
--prt->orig_y; --prt->orig_y;
} }
@ -254,8 +254,8 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
uint8_t line[MAX_STR_SIZE]; uint8_t line[MAX_STR_SIZE];
if (wcs_to_mbs_buf(line, prt->line, MAX_STR_SIZE) == -1) if (wcs_to_mbs_buf(line, prt->line, MAX_STR_SIZE) == -1)
memset(&line, 0, sizeof(line)); reset_buf(prt->line, &prt->pos, &prt->len);
else
mvwprintw(self->window, prt->orig_y, X_OFST, line); mvwprintw(self->window, prt->orig_y, X_OFST, line);
int k = prt->orig_y + ((prt->len + p_ofst) / px2); int k = prt->orig_y + ((prt->len + p_ofst) / px2);