From 5f1285238b09a5f01836138f5bb94654f78588a7 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 22 Apr 2015 17:15:19 -0700 Subject: 1136 - switch from ncurses to https://github.com/nsf/termbox --- cpp/termbox/bytebuffer.inl | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 cpp/termbox/bytebuffer.inl (limited to 'cpp/termbox/bytebuffer.inl') diff --git a/cpp/termbox/bytebuffer.inl b/cpp/termbox/bytebuffer.inl new file mode 100644 index 00000000..c476742d --- /dev/null +++ b/cpp/termbox/bytebuffer.inl @@ -0,0 +1,78 @@ +struct bytebuffer { + char *buf; + int len; + int cap; +}; + +static void bytebuffer_reserve(struct bytebuffer *b, int cap) { + if (b->cap >= cap) { + return; + } + + // prefer doubling capacity + if (b->cap * 2 >= cap) { + cap = b->cap * 2; + } + + char *newbuf = malloc(cap); + if (b->len > 0) { + // copy what was there, b->len > 0 assumes b->buf != null + memcpy(newbuf, b->buf, b->len); + } + if (b->buf) { + // in case there was an allocated buffer, free it + free(b->buf); + } + b->buf = newbuf; + b->cap = cap; +} + +static void bytebuffer_init(struct bytebuffer *b, int cap) { + b->cap = 0; + b->len = 0; + b->buf = 0; + + if (cap > 0) { + b->cap = cap; + b->buf = malloc(cap); // just assume malloc works always + } +} + +static void bytebuffer_free(struct bytebuffer *b) { + if (b->buf) + free(b->buf); +} + +static void bytebuffer_clear(struct bytebuffer *b) { + b->len = 0; +} + +static void bytebuffer_append(struct bytebuffer *b, const char *data, int len) { + bytebuffer_reserve(b, b->len + len); + memcpy(b->buf + b->len, data, len); + b->len += len; +} + +static void bytebuffer_puts(struct bytebuffer *b, const char *str) { + bytebuffer_append(b, str, strlen(str)); +} + +static void bytebuffer_resize(struct bytebuffer *b, int len) { + bytebuffer_reserve(b, len); + b->len = len; +} + +static void bytebuffer_flush(struct bytebuffer *b, int fd) { + write(fd, b->buf, b->len); + bytebuffer_clear(b); +} + +static void bytebuffer_truncate(struct bytebuffer *b, int n) { + if (n <= 0) + return; + if (n > b->len) + n = b->len; + const int nmove = b->len - n; + memmove(b->buf, b->buf+n, nmove); + b->len -= n; +} -- cgit 1.4.1-2-gfad0 range'>range
path: root/callcc.mu
blob: 20dffeff15f338a67d92d7bce8a82d6bd1811a2d (plain) (tree)
1
2
3
4
5
6
7
8