forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
111 lines (92 loc) · 2.43 KB
/
buffer.c
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
#include <stdlib.h>
#include <string.h>
#include "buffer.h"
#include "util.h"
#define BUF_SIZE 1024
void buffer_init(Buffer *buf) {
memset(buf, 0, sizeof *buf);
}
bool buffer_grow(Buffer *buf, size_t size) {
/* ensure minimal buffer size, to avoid repeated realloc(3) calls */
if (size < BUF_SIZE)
size = BUF_SIZE;
if (buf->size < size) {
size = MAX(size, buf->size*2);
char *data = realloc(buf->data, size);
if (!data)
return false;
buf->size = size;
buf->data = data;
}
return true;
}
void buffer_truncate(Buffer *buf) {
buf->len = 0;
}
void buffer_release(Buffer *buf) {
if (!buf)
return;
free(buf->data);
buffer_init(buf);
}
void buffer_clear(Buffer *buf) {
buf->len = 0;
}
bool buffer_put(Buffer *buf, const void *data, size_t len) {
if (!buffer_grow(buf, len))
return false;
memmove(buf->data, data, len);
buf->len = len;
return true;
}
bool buffer_put0(Buffer *buf, const char *data) {
return buffer_put(buf, data, strlen(data)+1);
}
bool buffer_insert(Buffer *buf, size_t pos, const void *data, size_t len) {
if (pos > buf->len)
return false;
if (!buffer_grow(buf, buf->len + len))
return false;
memmove(buf->data + pos + len, buf->data + pos, buf->len - pos);
memcpy(buf->data + pos, data, len);
buf->len += len;
return true;
}
bool buffer_insert0(Buffer *buf, size_t pos, const char *data) {
if (pos == 0)
return buffer_prepend0(buf, data);
if (pos == buf->len)
return buffer_append0(buf, data);
return buffer_insert(buf, pos, data, strlen(data));
}
bool buffer_append(Buffer *buf, const void *data, size_t len) {
return buffer_insert(buf, buf->len, data, len);
}
bool buffer_append0(Buffer *buf, const char *data) {
if (buf->len > 0 && buf->data[buf->len-1] == '\0')
buf->len--;
return buffer_append(buf, data, strlen(data)) && buffer_append(buf, "\0", 1);
}
bool buffer_prepend(Buffer *buf, const void *data, size_t len) {
return buffer_insert(buf, 0, data, len);
}
bool buffer_prepend0(Buffer *buf, const char *data) {
return buffer_prepend(buf, data, strlen(data) + (buf->len == 0));
}
size_t buffer_length0(Buffer *buf) {
size_t len = buf->len;
if (len > 0 && buf->data[len-1] == '\0')
len--;
return len;
}
size_t buffer_length(Buffer *buf) {
return buf->len;
}
const char *buffer_content(Buffer *buf) {
return buf->data;
}
const char *buffer_content0(Buffer *buf) {
if (buf->len == 0 || (buf->data[buf->len-1] != '\0' && !buffer_append(buf, "\0", 1)))
return "";
return buf->data;
}