From b1b2cc44dfb446ef7aac59c8686d858b59902018 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 1 Dec 2013 16:57:05 -0500 Subject: [PATCH] minor improvements --- src/chat.c | 20 +++++++++++++++----- src/misc_tools.c | 20 +++----------------- src/prompt.c | 21 +++++++++++---------- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/chat.c b/src/chat.c index 71a39a2..19e7f3c 100644 --- a/src/chat.c +++ b/src/chat.c @@ -289,17 +289,25 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) else wmove(self->window, y, x-1); } - } else if (key == KEY_DC) { /* DEL key: Remove character at pos */ + } + + else if (key == KEY_DC) { /* DEL key: Remove character at pos */ del_char_buf_frnt(ctx->line, &ctx->pos, &ctx->len); - } else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */ + } + + else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */ ctx->pos = 0; wmove(self->window, y2 - CURS_Y_OFFSET, 0); - } else if (key == KEY_END) { /* END key: move cursor to end of line */ + } + + else if (key == KEY_END) { /* END key: move cursor to end of line */ ctx->pos = ctx->len; int end_y = (ctx->len / x2) + (y2 - CURS_Y_OFFSET); int end_x = ctx->len % x2; wmove(self->window, end_y, end_x); - } else if (key == KEY_LEFT) { + } + + else if (key == KEY_LEFT) { if (ctx->pos > 0) { --ctx->pos; @@ -308,7 +316,9 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) else wmove(self->window, y, x-1); } - } else if (key == KEY_RIGHT) { + } + + else if (key == KEY_RIGHT) { if (ctx->pos < ctx->len) { ++ctx->pos; diff --git a/src/misc_tools.c b/src/misc_tools.c index 72386c6..2e2da2e 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -161,7 +161,8 @@ bool valid_nick(uint8_t *nick) } /* - * Buffer helper tools. Assumes buffers are no larger than MAX_STR_SIZE + * Buffer helper tools. + * Assumes buffers are no larger than MAX_STR_SIZE and are always null terminated at len */ /* Adds char to buffer at pos */ @@ -170,15 +171,7 @@ void add_char_to_buf(wint_t ch, wchar_t *buf, size_t *pos, size_t *len) if (*pos < 0 || *len >= MAX_STR_SIZE) return; - /* if cursor is at end of line simply insert char at last position and increment */ - if (buf[*pos] == L'\0') { - buf[(*pos)++] = ch; - buf[*pos] = L'\0'; - ++(*len); - return; - } - - /* move all chars in front of pos one space forward and insert char in pos */ + /* move all chars including null in front of pos one space forward and insert char in pos */ int i; for (i = *len; i >= *pos && i >= 0; --i) @@ -194,13 +187,6 @@ void del_char_buf_bck(wchar_t *buf, size_t *pos, size_t *len) if (*pos <= 0) return; - /* if cursor is at end of line delete previous char */ - if (buf[*pos] == L'\0') { - buf[--(*pos)] = L'\0'; - --(*len); - return; - } - int i; /* similar to add_char_to_buf but deletes a char */ diff --git a/src/prompt.c b/src/prompt.c index 9be4c13..e8eae71 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -54,18 +54,19 @@ void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected) Returns request number on success, -1 if queue is full or other error. */ static int add_friend_request(uint8_t *public_key) { - if (num_frnd_requests < MAX_FRIENDS_NUM) { - int i; + if (num_frnd_requests >= MAX_FRIENDS_NUM) + return; - for (i = 0; i <= num_frnd_requests; ++i) { - if (!strlen(pending_frnd_requests[i])) { - memcpy(pending_frnd_requests[i], public_key, TOX_CLIENT_ID_SIZE); + int i; - if (i == num_frnd_requests) - ++num_frnd_requests; + for (i = 0; i <= num_frnd_requests; ++i) { + if (!strlen(pending_frnd_requests[i])) { + memcpy(pending_frnd_requests[i], public_key, TOX_CLIENT_ID_SIZE); - return i; - } + if (i == num_frnd_requests) + ++num_frnd_requests; + + return i; } } @@ -282,4 +283,4 @@ ToxWindow new_prompt(void) } return ret; -} +} \ No newline at end of file