From 629041d465ca557c9c10304d91cbe88e076d1692 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 11 Dec 2013 03:29:31 -0500 Subject: [PATCH] simplify code a bit --- src/chat.c | 4 ++-- src/groupchat.c | 8 +++----- src/misc_tools.c | 2 +- src/prompt.c | 4 ++-- src/toxic_strings.c | 24 ++++++++++-------------- src/toxic_strings.h | 13 ++++++------- 6 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/chat.c b/src/chat.c index 8353ff9..7a44f9c 100644 --- a/src/chat.c +++ b/src/chat.c @@ -370,7 +370,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_UP) { /* fetches previous item in history */ if (ctx->hst_pos >= 0) { - fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, &ctx->hst_tot, + fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, &ctx->hst_pos, LN_HIST_MV_UP); mv_curs_end(self->window, ctx->len, y2, x2); } @@ -378,7 +378,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_DOWN) { /* fetches next item in history */ if (ctx->hst_pos < ctx->hst_tot) { - fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, &ctx->hst_tot, + fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, &ctx->hst_pos, LN_HIST_MV_DWN); mv_curs_end(self->window, ctx->len, y2, x2); } diff --git a/src/groupchat.c b/src/groupchat.c index f3e1d2b..4990afb 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -281,9 +281,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_END) { /* END key: move cursor to end of line */ if (ctx->pos != ctx->len) { 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); + mv_curs_end(self->window, ctx->len, y2, x2); } } @@ -311,7 +309,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_UP) { /* fetches previous item in history */ if (ctx->hst_pos >= 0) { - fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, &ctx->hst_tot, + fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, &ctx->hst_pos, LN_HIST_MV_UP); mv_curs_end(self->window, ctx->len, y2, x2); } @@ -319,7 +317,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_DOWN) { /* fetches next item in history */ if (ctx->hst_pos < ctx->hst_tot) { - fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, &ctx->hst_tot, + fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, &ctx->hst_pos, LN_HIST_MV_DWN); mv_curs_end(self->window, ctx->len, y2, x2); } diff --git a/src/misc_tools.c b/src/misc_tools.c index 5cbc764..2a42932 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -192,4 +192,4 @@ void mv_curs_end(WINDOW *w, size_t len, int max_y, int max_x) int end_y = (len / max_x) + (max_y - CURS_Y_OFFSET); int end_x = len % max_x; wmove(w, end_y, end_x); -} \ No newline at end of file +} diff --git a/src/prompt.c b/src/prompt.c index af00edc..12121a0 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -168,7 +168,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_UP) { /* fetches previous item in history */ if (prt->hst_pos >= 0) { wmove(self->window, prt->orig_y, X_OFST); - fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, &prt->hst_tot, + fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, prt->hst_tot, &prt->hst_pos, LN_HIST_MV_UP); } } @@ -176,7 +176,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_DOWN) { /* fetches next item in history */ if (prt->hst_pos < prt->hst_tot) { wmove(self->window, prt->orig_y, X_OFST); - fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, &prt->hst_tot, + fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, prt->hst_tot, &prt->hst_pos, LN_HIST_MV_DWN); } } diff --git a/src/toxic_strings.c b/src/toxic_strings.c index 83b7178..34ea2bd 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -89,45 +89,41 @@ void reset_buf(wchar_t *buf, size_t *pos, size_t *len) *len = 0; } -/* adds a line to the ln_history buffer at hst_pos and sets hst_pos to end of history. - Assumes entries are of size MAX_STR_SIZE */ -void add_line_to_hist(const wchar_t *buf, size_t len, void *ln_history, int *hst_tot, int *hst_pos) +/* adds a line to the ln_history buffer at hst_pos and sets hst_pos to end of history. */ +void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZE], int *hst_tot, + int *hst_pos) { if (len > MAX_STR_SIZE) return; - wchar_t *hst = (wchar_t *) ln_history; - /* If history is full make room for newest entry and don't increment hst_tot */ if (*hst_tot == MAX_LINE_HIST) { int i; for (i = 0; i < MAX_LINE_HIST - 1; ++i) - wmemcpy(&hst[MAX_STR_SIZE * i], &hst[MAX_STR_SIZE * (i + 1)], MAX_STR_SIZE); + wmemcpy(hst[i], hst[i+1], MAX_STR_SIZE); } else { ++(*hst_tot); } *hst_pos = *hst_tot; - wmemcpy(&hst[(*hst_tot - 1) * MAX_STR_SIZE], buf, len + 1); + wmemcpy(hst[*hst_tot-1], buf, len + 1); } /* copies history item at hst_pos to buf. Sets pos and len to the len of the history item. - hst_pos is decremented or incremented depending on key_dir. - Assumes history entries are of size MAX_STR_SIZE */ -void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, const void *ln_history, int *hst_tot, - int *hst_pos, int key_dir) + hst_pos is decremented or incremented depending on key_dir. */ +void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, wchar_t (*hst)[MAX_STR_SIZE], + int hst_tot, int *hst_pos, int key_dir) { if (key_dir == LN_HIST_MV_UP) { if (--(*hst_pos) < 0) ++(*hst_pos); } else { - if (++(*hst_pos) == *hst_tot) + if (++(*hst_pos) == hst_tot) --(*hst_pos); } - wchar_t *hst = (wchar_t *) ln_history; - const wchar_t *hst_line = &hst[*hst_pos * MAX_STR_SIZE]; + const wchar_t *hst_line = hst[*hst_pos]; size_t h_len = wcslen(hst_line); wmemcpy(buf, hst_line, h_len + 1); diff --git a/src/toxic_strings.h b/src/toxic_strings.h index 15d95a8..f97dd20 100644 --- a/src/toxic_strings.h +++ b/src/toxic_strings.h @@ -35,12 +35,11 @@ enum { LN_HIST_MV_DWN, }; -/* adds a line to the ln_history buffer at hst_pos and sets hst_pos to last history item. - Assumes entries are of size MAX_STR_SIZE */ -void add_line_to_hist(const wchar_t *buf, size_t len, void *hst, int *hst_tot, int *hst_pos); +/* adds a line to the ln_history buffer at hst_pos and sets hst_pos to last history item. */ +void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZE], int *hst_tot, + int *hst_pos); /* copies history item at hst_pos to buf. Sets pos and len to the len of the history item. - hst_pos is decremented or incremented depending on key_dir. - Assumes history entries are of size MAX_STR_SIZE */ -void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, const void *ln_history, int *hst_tot, - int *hst_pos, int key_dir); \ No newline at end of file + hst_pos is decremented or incremented depending on key_dir. */ +void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, wchar_t (*hst)[MAX_STR_SIZE], + int hst_tot, int *hst_pos, int key_dir);