Skip to content

Commit 66c3f91

Browse files
committed
Replace memstream with bytebuffer.
1 parent a2e217f commit 66c3f91

File tree

5 files changed

+110
-73
lines changed

5 files changed

+110
-73
lines changed

src/bytebuffer.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "bytebuffer.h"
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
6+
static void bytebuffer_reserve(struct bytebuffer *b, int cap) {
7+
if (b->cap >= cap) {
8+
return;
9+
}
10+
11+
// prefer doubling capacity
12+
if (b->cap * 2 >= cap) {
13+
cap = b->cap * 2;
14+
}
15+
16+
char *newbuf = malloc(cap);
17+
if (b->len > 0) {
18+
// copy what was there, b->len > 0 assumes b->buf != null
19+
memcpy(newbuf, b->buf, b->len);
20+
}
21+
if (b->buf) {
22+
// in case there was an allocated buffer, free it
23+
free(b->buf);
24+
}
25+
b->buf = newbuf;
26+
b->cap = cap;
27+
}
28+
29+
void init_bytebuffer(struct bytebuffer *b, int cap) {
30+
b->cap = 0;
31+
b->len = 0;
32+
b->buf = 0;
33+
34+
if (cap > 0) {
35+
b->cap = cap;
36+
b->buf = malloc(cap); // just assume malloc works always
37+
}
38+
}
39+
40+
void free_bytebuffer(struct bytebuffer *b) {
41+
if (b->buf)
42+
free(b->buf);
43+
}
44+
45+
void clear_bytebuffer(struct bytebuffer *b) {
46+
b->len = 0;
47+
}
48+
49+
void bytebuffer_puts(struct bytebuffer *b, const char *str) {
50+
bytebuffer_append(b, str, strlen(str));
51+
}
52+
53+
void bytebuffer_append(struct bytebuffer *b, const char *data, int len) {
54+
bytebuffer_reserve(b, b->len + len);
55+
memcpy(b->buf + b->len, data, len);
56+
b->len += len;
57+
}
58+
59+
void resize_bytebuffer(struct bytebuffer *b, int len) {
60+
bytebuffer_reserve(b, len);
61+
b->len = len;
62+
}
63+
64+
void flush_bytebuffer(struct bytebuffer *b, int fd) {
65+
write(fd, b->buf, b->len);
66+
clear_bytebuffer(b);
67+
}

src/bytebuffer.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
struct bytebuffer {
4+
char *buf;
5+
int len;
6+
int cap;
7+
};
8+
9+
void init_bytebuffer(struct bytebuffer *b, int cap);
10+
void free_bytebuffer(struct bytebuffer *b);
11+
void clear_bytebuffer(struct bytebuffer *b);
12+
void resize_bytebuffer(struct bytebuffer *b, int len);
13+
void bytebuffer_puts(struct bytebuffer *b, const char *str);
14+
void bytebuffer_append(struct bytebuffer *b, const char *data, int len);
15+
16+
// writes the contents of the buffer to the given fd and then clears the buffer
17+
void flush_bytebuffer(struct bytebuffer *b, int fd);

src/memstream.c

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/memstream.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/termbox.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "term.h"
1414
#include "termbox.h"
15-
#include "memstream.h"
15+
#include "bytebuffer.h"
1616

1717
struct cellbuf {
1818
unsigned int width;
@@ -29,8 +29,7 @@ static struct termios orig_tios;
2929

3030
static struct cellbuf back_buffer;
3131
static struct cellbuf front_buffer;
32-
static unsigned char write_buffer_data[32 * 1024];
33-
static struct memstream write_buffer;
32+
static struct bytebuffer output_buffer;
3433

3534
static unsigned int termw;
3635
static unsigned int termh;
@@ -128,11 +127,11 @@ int tb_init(void)
128127
tios.c_cc[VTIME] = 0;
129128
tcsetattr(out_fileno, TCSAFLUSH, &tios);
130129

131-
memstream_init(&write_buffer, out_fileno, write_buffer_data, sizeof(write_buffer_data));
130+
init_bytebuffer(&output_buffer, 32 * 1024);
132131

133-
memstream_puts(&write_buffer, funcs[T_ENTER_CA]);
134-
memstream_puts(&write_buffer, funcs[T_ENTER_KEYPAD]);
135-
memstream_puts(&write_buffer, funcs[T_HIDE_CURSOR]);
132+
bytebuffer_puts(&output_buffer, funcs[T_ENTER_CA]);
133+
bytebuffer_puts(&output_buffer, funcs[T_ENTER_KEYPAD]);
134+
bytebuffer_puts(&output_buffer, funcs[T_HIDE_CURSOR]);
136135
send_clear();
137136

138137
update_term_size();
@@ -147,12 +146,12 @@ int tb_init(void)
147146

148147
void tb_shutdown(void)
149148
{
150-
memstream_puts(&write_buffer, funcs[T_SHOW_CURSOR]);
151-
memstream_puts(&write_buffer, funcs[T_SGR0]);
152-
memstream_puts(&write_buffer, funcs[T_CLEAR_SCREEN]);
153-
memstream_puts(&write_buffer, funcs[T_EXIT_CA]);
154-
memstream_puts(&write_buffer, funcs[T_EXIT_KEYPAD]);
155-
memstream_flush(&write_buffer);
149+
bytebuffer_puts(&output_buffer, funcs[T_SHOW_CURSOR]);
150+
bytebuffer_puts(&output_buffer, funcs[T_SGR0]);
151+
bytebuffer_puts(&output_buffer, funcs[T_CLEAR_SCREEN]);
152+
bytebuffer_puts(&output_buffer, funcs[T_EXIT_CA]);
153+
bytebuffer_puts(&output_buffer, funcs[T_EXIT_KEYPAD]);
154+
flush_bytebuffer(&output_buffer, out_fileno);
156155
tcsetattr(out_fileno, TCSAFLUSH, &orig_tios);
157156

158157
shutdown_term();
@@ -193,16 +192,16 @@ void tb_present(void)
193192
}
194193
if (!IS_CURSOR_HIDDEN(cursor_x, cursor_y))
195194
write_cursor(cursor_x, cursor_y);
196-
memstream_flush(&write_buffer);
195+
flush_bytebuffer(&output_buffer, out_fileno);
197196
}
198197

199198
void tb_set_cursor(int cx, int cy)
200199
{
201200
if (IS_CURSOR_HIDDEN(cursor_x, cursor_y) && !IS_CURSOR_HIDDEN(cx, cy))
202-
memstream_puts(&write_buffer, funcs[T_SHOW_CURSOR]);
201+
bytebuffer_puts(&output_buffer, funcs[T_SHOW_CURSOR]);
203202

204203
if (!IS_CURSOR_HIDDEN(cursor_x, cursor_y) && IS_CURSOR_HIDDEN(cx, cy))
205-
memstream_puts(&write_buffer, funcs[T_HIDE_CURSOR]);
204+
bytebuffer_puts(&output_buffer, funcs[T_HIDE_CURSOR]);
206205

207206
cursor_x = cx;
208207
cursor_y = cy;
@@ -286,8 +285,8 @@ void tb_set_clear_attributes(uint16_t fg, uint16_t bg)
286285

287286
/* -------------------------------------------------------- */
288287

289-
static unsigned convertnum(uint32_t num, char* buf) {
290-
unsigned i, l = 0;
288+
static int convertnum(uint32_t num, char* buf) {
289+
int i, l = 0;
291290
int ch;
292291
do {
293292
buf[l++] = '0' + (num % 10);
@@ -301,8 +300,8 @@ static unsigned convertnum(uint32_t num, char* buf) {
301300
return l;
302301
}
303302

304-
#define WRITE_LITERAL(X) memstream_write(&write_buffer, (X), sizeof(X) -1)
305-
#define WRITE_INT(X) memstream_write(&write_buffer, buf, convertnum((X), buf))
303+
#define WRITE_LITERAL(X) bytebuffer_append(&output_buffer, (X), sizeof(X)-1)
304+
#define WRITE_INT(X) bytebuffer_append(&output_buffer, buf, convertnum((X), buf))
306305

307306
static void write_cursor(unsigned x, unsigned y) {
308307
char buf[32];
@@ -414,14 +413,14 @@ static void send_attr(uint16_t fg, uint16_t bg)
414413
#define LAST_ATTR_INIT 0xFFFF
415414
static uint16_t lastfg = LAST_ATTR_INIT, lastbg = LAST_ATTR_INIT;
416415
if (fg != lastfg || bg != lastbg) {
417-
memstream_puts(&write_buffer, funcs[T_SGR0]);
416+
bytebuffer_puts(&output_buffer, funcs[T_SGR0]);
418417
write_sgr(fg & 0x0F, bg & 0x0F);
419418
if (fg & TB_BOLD)
420-
memstream_puts(&write_buffer, funcs[T_BOLD]);
419+
bytebuffer_puts(&output_buffer, funcs[T_BOLD]);
421420
if (bg & TB_BOLD)
422-
memstream_puts(&write_buffer, funcs[T_BLINK]);
421+
bytebuffer_puts(&output_buffer, funcs[T_BLINK]);
423422
if (fg & TB_UNDERLINE)
424-
memstream_puts(&write_buffer, funcs[T_UNDERLINE]);
423+
bytebuffer_puts(&output_buffer, funcs[T_UNDERLINE]);
425424

426425
lastfg = fg;
427426
lastbg = bg;
@@ -437,16 +436,16 @@ static void send_char(unsigned int x, unsigned int y, uint32_t c)
437436
write_cursor(x, y);
438437
lastx = x; lasty = y;
439438
if(!c) buf[0] = ' '; // replace 0 with whitespace
440-
memstream_puts(&write_buffer, buf);
439+
bytebuffer_puts(&output_buffer, buf);
441440
}
442441

443442
static void send_clear(void)
444443
{
445444
send_attr(foreground, background);
446-
memstream_puts(&write_buffer, funcs[T_CLEAR_SCREEN]);
445+
bytebuffer_puts(&output_buffer, funcs[T_CLEAR_SCREEN]);
447446
if (!IS_CURSOR_HIDDEN(cursor_x, cursor_y))
448447
write_cursor(cursor_x, cursor_y);
449-
memstream_flush(&write_buffer);
448+
flush_bytebuffer(&output_buffer, out_fileno);
450449

451450
/* we need to invalidate cursor position too and these two vars are
452451
* used only for simple cursor positioning optimization, cursor

0 commit comments

Comments
 (0)