-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuffer.c
242 lines (198 loc) · 3.72 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* General purpose unbounded character buffers. This allows
* us to buffer our scanning without arbitrary limitations
* on any lexeme size.
*/
#include <assert.h>
#include <sys/types.h>
#include <stdlib.h>
#include "c2ada.h"
#undef NULL
#define NULL 0L
#define NEXT_INDEX(x) (((x) + 1) & (MAX_OUTBUF_LEN - 1))
static buffer_t* free_list;
static void reclaim(buffer_t* buf)
{
buf->next = free_list;
free_list = buf;
}
void buf_destroy(buffer_t* buf)
{
buffer_t *next, *p;
for(p = buf->next; p; p = next)
{
next = p->next;
reclaim(p);
}
buf->next = NULL;
buf->last = NULL;
buf->head = 0;
buf->tail = 0;
}
void buf_init(buffer_t* buf)
{
buf->next = NULL;
buf->last = NULL;
buf->head = 0;
buf->tail = 0;
}
int buf_empty(buffer_t* buf)
{
buffer_t* next;
if(buf == NULL)
{
return 1;
}
if(buf->head == buf->tail)
{
next = buf->next;
if(next != NULL)
{
*buf = *next;
if(next == buf->last)
{
buf->last = NULL;
}
reclaim(next);
return 0;
}
return 1;
}
return 0;
}
char buf_get(buffer_t* buf)
{
if(buf->head == buf->tail)
{
if(buf_empty(buf))
{
/* End of file */
return 0;
}
}
buf->head = NEXT_INDEX(buf->head);
return buf->buf[buf->head];
}
static buffer_t* gen_overflow_buf()
{
buffer_t* buf;
if((buf = free_list))
{
free_list = buf->next;
}
else
{
buf = (buffer_t*)malloc(sizeof(buffer_t));
assert(buf != NULL);
}
buf_init(buf);
return buf;
}
void buf_add(buffer_t* buf, int c)
{
int index;
buffer_t* l;
l = buf->last;
if(l != NULL)
{
if(l->tail == (MAX_OUTBUF_LEN - 1))
{
l->next = gen_overflow_buf();
buf->last = l->next;
l = l->next;
}
l->tail = NEXT_INDEX(l->tail);
l->buf[l->tail] = (char)c;
return;
}
index = NEXT_INDEX(buf->tail);
if(index == buf->head)
{
buf->next = gen_overflow_buf();
l = buf->last = buf->next;
l->buf[1] = (char)c;
l->tail = 1;
return;
}
buf->buf[index] = (char)c;
buf->tail = index;
}
void buf_concat(buffer_t *buf, buffer_t* from_buf)
{
char c;
for(;;)
{
c = buf_get(from_buf);
if(c == 0)
break;
buf_add(buf, (int)c);
}
}
void buf_add_str(buffer_t* buf, char* str)
{
if(str == NULL)
{
return;
}
for(; *str; str++)
{
buf_add(buf, (int)*str);
}
}
int buf_count(buffer_t* buf)
{
buffer_t* p;
int count;
if(buf == NULL)
{
return 0;
}
if(buf->tail >= buf->head)
{
count = buf->tail - buf->head;
}
else
{
count = MAX_OUTBUF_LEN - buf->head + buf->tail;
}
for(p = buf->next; p; p = p->next)
{
count += p->tail;
}
return count;
}
void buf_move_to(buffer_t* buf, char* str)
{
int index = buf->head;
int tail = buf->tail;
buffer_t *next, *p;
while(index != tail)
{
index = NEXT_INDEX(index);
*str++ = buf->buf[index];
}
for(p = buf->next; p; p = next)
{
next = p->next;
for(index = 1; index <= p->tail; index++)
{
*str++ = p->buf[index];
}
reclaim(p);
}
*str = 0;
buf_init(buf);
}
char* buf_get_str(buffer_t* buf)
{
int len = buf_count(buf);
char* str;
if(len == 0)
{
return NULL;
}
str = (char*)malloc(len + 1);
assert(str != NULL);
buf_move_to(buf, str);
return str;
}