-
Notifications
You must be signed in to change notification settings - Fork 3
/
buffer.c
165 lines (145 loc) · 3.07 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
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
#include <string.h>
#include "buffer.h"
#include "utils.h"
struct buffer *
buffer_new()
{
struct buffer * p;
p = xmalloc(sizeof(struct buffer));
p->data = xmalloc(INITIAL_BUF_SIZE);
p->length = INITIAL_BUF_SIZE;
p->roff = p->woff = 0;
return p;
}
void
buffer_free(struct buffer * b)
{
if (!b) fatal("buffer_free");
free(b->data);
free(b);
}
void
buffer_expand(struct buffer * b, size_t len)
{
if (!b) fatal("buffer_expand");
if ((b->woff + len) > b->length) {
b->data = xrealloc(b->data, b->woff + len);
}
b->length = b->woff + len;
}
void
buffer_append(struct buffer * b, void * p, size_t len)
{
if (!b) fatal("buffer_append");
buffer_expand(b, len);
if (p) {
memcpy(b->data + b->woff, p, len);
b->woff += len;
}
}
void
buffer_prepend(struct buffer * b, void * p, size_t len)
{
if (!b || !p || !len) fatal("buffer_prepend");
if (len < b->roff) {
b->roff -= len;
memcpy(b->data + b->roff, p, len);
}
else {
buffer_expand(b, len + buffer_avail(b));
memmove(b->data + len, b->data + b->roff, b->woff - b->roff);
memcpy(b->data, p, len);
b->woff += (len-b->roff);
b->roff = 0;
}
}
size_t
buffer_avail(struct buffer * b)
{
if (!b) fatal("buffer_avail");
return b->woff - b->roff;
}
void
buffer_peek(struct buffer * b, void * p, size_t len)
{
if (!b || !p || len > buffer_avail(b)) fatal("buffer_peek");
memcpy(p, b->data + b->roff, len);
}
void
buffer_consume(struct buffer * b, void * p, size_t len)
{
if (!b) fatal("buffer_consume");
if (p) buffer_peek(b, p, len);
b->roff += len;
}
int
buffer_peek_until(struct buffer * b, void * p, size_t len, char c,
size_t * consumed)
{
size_t roff;
char * p2;
if (!b) fatal("buffer_consume_until");
roff = b->roff;
while (roff < b->woff && (!p || len-- > 0)) {
p2 = b->data + roff++;
if (p) *(char *)p++ = *p2;
if (*p2 == c) {
if (consumed)
*consumed = roff - b->roff;
return 1;
}
}
if (consumed)
*consumed = roff - b->roff;
return 0;
}
int
buffer_consume_until(struct buffer * b, void * p, size_t len, char c,
size_t * consumed)
{
size_t tmpconsumed;
int ret;
if (!b) fatal("buffer_consume_until");
ret = buffer_peek_until(b, p, len, c, &tmpconsumed);
b->roff += tmpconsumed;
if (consumed) *consumed = tmpconsumed;
return ret;
}
int
buffer_findchar(struct buffer * b, int chr, size_t * off)
{
void * ret;
if (!b || !off) fatal("buffer_find");
ret = memchr(b->data + b->roff, chr, b->woff - b->roff);
if (!ret) return -1;
*off = ret - b->data - b->roff;
return 0;
}
void
buffer_reset(struct buffer * b)
{
if (!b) fatal("buffer_reset");
b->roff = b->woff = 0;
}
void
buffer_fd_append(struct buffer * b, int fd, size_t len)
{
if (!b) fatal("buffer_fd_append");
buffer_expand(b, len);
fd_read(fd, b->data + b->woff, len);
b->woff += len;
}
void
buffer_fd_write(struct buffer * b, int fd, size_t len)
{
if (!b || len > (b->woff - b->roff)) fatal("buffer_fd_write");
fd_write(fd, b->data + b->roff, len);
b->roff += len;
}
void
buffer_fd_flush(struct buffer * b, int fd)
{
if (!b) fatal("buffer_fd_flush");
fd_write(fd, b->data + b->roff, b->woff - b->roff);
b->roff = b->woff;
}