about summary refs log tree commit diff stats
path: root/archive/2.vm/termbox/bytebuffer.inl
diff options
context:
space:
mode:
Diffstat (limited to 'archive/2.vm/termbox/bytebuffer.inl')
-rw-r--r--archive/2.vm/termbox/bytebuffer.inl79
1 files changed, 79 insertions, 0 deletions
diff --git a/archive/2.vm/termbox/bytebuffer.inl b/archive/2.vm/termbox/bytebuffer.inl
new file mode 100644
index 00000000..aae8f073
--- /dev/null
+++ b/archive/2.vm/termbox/bytebuffer.inl
@@ -0,0 +1,79 @@
+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) {
+  int yyy = write(fd, b->buf, b->len);
+  (void) yyy;
+  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;
+}
le='author Thomas E. Dickey <dickey@invisible-island.net> 1997-09-19 01:14:00 -0400 committer Thomas E. Dickey <dickey@invisible-island.net> 1997-09-19 01:14:00 -0400 snapshot of project "lynx", label v2-7-1ac_0-69' href='/ingrix/lynx-snapshots/commit/src/LYStrings.h?id=443226a5ffcf805f6ab3ccbcc2a6b4802793b07d'>443226a5 ^
e087f6d4






















































97d3287a ^
e47cfd56 ^
97d3287a ^
e087f6d4
1d80538b ^











e087f6d4













1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168