From e252c497ae20351882d8b98a5c59ca19e479c5a8 Mon Sep 17 00:00:00 2001 From: Paul LeoNerd Evans Date: Mon, 13 Feb 2012 23:40:42 +0000 Subject: Added (undocumented) termkey_set_buffer_size() --- t/06buffer.c | 32 ++++++++++++++++++++++++++++++++ termkey.c | 12 ++++++++++++ termkey.h.in | 1 + 3 files changed, 45 insertions(+) create mode 100644 t/06buffer.c diff --git a/t/06buffer.c b/t/06buffer.c new file mode 100644 index 0000000..fa1dada --- /dev/null +++ b/t/06buffer.c @@ -0,0 +1,32 @@ +#include +#include "../termkey.h" +#include "taplib.h" + +int main(int argc, char *argv[]) +{ + TermKey *tk; + TermKeyKey key; + + plan_tests(9); + + tk = termkey_new_abstract("vt100", 0); + + is_int(termkey_get_buffer_remaining(tk), 256, "buffer free initially 256"); + is_int(termkey_get_buffer_size(tk), 256, "buffer size initially 256"); + + is_int(termkey_push_bytes(tk, "h", 1), 1, "push_bytes returns 1"); + + is_int(termkey_get_buffer_remaining(tk), 255, "buffer free 255 after push_bytes"); + is_int(termkey_get_buffer_size(tk), 256, "buffer size 256 after push_bytes"); + + ok(!!termkey_set_buffer_size(tk, 512), "buffer set size OK"); + + is_int(termkey_get_buffer_remaining(tk), 511, "buffer free 511 after push_bytes"); + is_int(termkey_get_buffer_size(tk), 512, "buffer size 512 after push_bytes"); + + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "buffered key still useable after resize"); + + termkey_destroy(tk); + + return exit_status(); +} diff --git a/termkey.c b/termkey.c index 1cc964b..8e443d1 100644 --- a/termkey.c +++ b/termkey.c @@ -466,6 +466,18 @@ size_t termkey_get_buffer_size(TermKey *tk) return tk->buffsize; } +int termkey_set_buffer_size(TermKey *tk, size_t size) +{ + unsigned char *buffer = realloc(tk->buffer, size); + if(!buffer) + return 0; + + tk->buffer = buffer; + tk->buffsize = size; + + return 1; +} + size_t termkey_get_buffer_remaining(TermKey *tk) { /* Return the total number of free bytes in the buffer, because that's what diff --git a/termkey.h.in b/termkey.h.in index 6c1af1e..d7317b0 100644 --- a/termkey.h.in +++ b/termkey.h.in @@ -173,6 +173,7 @@ int termkey_get_canonflags(TermKey *tk); void termkey_set_canonflags(TermKey *tk, int); size_t termkey_get_buffer_size(TermKey *tk); +int termkey_set_buffer_size(TermKey *tk, size_t size); size_t termkey_get_buffer_remaining(TermKey *tk); -- cgit v1.2.3-70-g09d2