-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
301 lines (270 loc) · 6.95 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* vim: set tabstop=4:softtabstop=4:shiftwidth=4 */
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "buffer.h"
#include "utils.h"
void buf_init (buffer_t *buffer, FILE* fd)
{
memset(buffer->content, 0, BUF_SIZE);
buffer->fd = fd;
buffer->it = 0;
buffer->end = 0;
buffer->lock = 0;
buffer->islocked = false;
buffer->avail = 0;
buffer->bytesread = 0;
buffer->bytesreadsincelock = 0;
buffer->currchar = 0;
buffer->eof = false;
}
bool buf_eof_strict (buffer_t *buffer) {
return buffer->avail == 0 && buffer->eof == true;
}
bool buf_eof (buffer_t *buffer) {
buf_skipblank(buffer);
return buf_eof_strict(buffer);
}
void buf_lock (buffer_t *buffer) {
if (buffer->islocked) {
fprintf(stderr, "Warning: lock was already set.\n");
print_backtrace();
}
buffer->bytesreadsincelock = 0;
buffer->lock = buffer->it;
buffer->islocked = true;
}
void buf_unlock (buffer_t *buffer) {
if (!buffer->islocked) {
fprintf(stderr, "Warning: lock was not set.\n");
print_backtrace();
}
buffer->bytesreadsincelock = 0;
buffer->islocked = false;
}
static
size_t buf_fread (buffer_t *buffer, size_t offset, size_t n)
{
// printf("read %lu bytes\n", n);
size_t cnt = fread(&(buffer->content[offset]), 1, n, buffer->fd);
if (cnt < n) {
buffer->eof = true;
}
buffer->bytesread += cnt;
if (buffer->islocked) {
buffer->bytesreadsincelock += cnt;
}
return cnt;
}
static
void buf_mod (size_t *val, size_t toadd)
{
*val = (*val + toadd) % BUF_SIZE;
}
static
void buf_mod_backwards (size_t *val, size_t tosub)
{
*val = (*val - tosub) % BUF_SIZE;
}
static
void buf_move_it (buffer_t *buffer, size_t cnt) {
buf_mod(&buffer->it, cnt);
buffer->currchar += cnt;
}
static
void buf_move_it_bw (buffer_t *buffer, size_t cnt) {
buf_mod_backwards(&buffer->it, cnt);
buffer->currchar -= cnt;
}
char buf_getchar (buffer_t *buffer)
{
// Debugging: Print entry into function
//printf("Debug: buf_getchar called. it=%zu, avail=%zu, eof=%d\n", buffer->it, buffer->avail, buffer->eof);
size_t end;
if (buffer->avail == 0) {
if (buffer->islocked) {
// on doit charger le max de caractères
// on commence par la fin de la chaîne entre end et lock
if (buffer->end >= buffer->lock) {
if (buffer->end < BUF_SIZE - 1) {
end = buf_fread(buffer, buffer->end, BUF_SIZE - buffer->end);
} else {
end = 0;
}
end += buf_fread(buffer, 0, buffer->lock);
if (end == 0)
goto fail;
} else if (buffer->bytesreadsincelock >= BUF_SIZE) {
printf("Can't lock more than %d chars.", BUF_SIZE);
print_backtrace();
exit(1);
} else {
end = buf_fread(buffer, buffer->end, buffer->lock - buffer->end);
}
buf_mod(&buffer->end, end);
} else {
if ((end = buf_fread(buffer, 0, BUF_SIZE)) == 0) {
goto fail;
}
buffer->end = end - 1;
}
buffer->avail = end;
}
char ret = buffer->content[buffer->it];
buf_move_it(buffer, 1);
buffer->avail -= 1;
return ret;
fail:
buffer->avail = 0;
buffer->end = 0;
buffer->it = 0;
return '\0';
// Debugging: Print entry into function
printf("Debug: lexer_getnumber called.\n");
}
char buf_getchar_after_blank (buffer_t *buffer) {
buf_skipblank(buffer);
return buf_getchar(buffer);
}
void buf_getnchar (buffer_t *buffer, char *out, size_t n)
{
char *outcurs = out;
size_t cnt, max;
char *content = buffer->content;
while (n) {
// number of chars that can be read w/o going back to 0
size_t currpos = !buffer->islocked ? buffer->it : buffer->lock;
size_t nmax = BUF_SIZE - currpos < n
? BUF_SIZE - currpos
: n;
// if the current number of buffered items is not enough
if (buffer->avail < nmax) {
// if |xxxxx]------[xxxxx| then |xxxxxxxxxxx][xxxxx|
if (buffer->end < currpos)
max = currpos - buffer->end;
// if |-----[xxxxxx]-----| then |-----[xxxxxxxxxxx]|
else
max = BUF_SIZE - buffer->end;
cnt = buf_fread(buffer, buffer->end, max);
buf_mod(&buffer->end, cnt);
buffer->avail += cnt;
}
if (buffer->avail < nmax) {
out[0] = '\0';
return;
}
memcpy(outcurs, &content[currpos], nmax);
buffer->avail -= nmax;
buf_move_it(buffer, nmax);
if (n <= nmax)
break;
outcurs += nmax;
n -= nmax;
}
}
void buf_forward (buffer_t *buffer, size_t n)
{
buf_move_it(buffer, n);
buffer->avail -= n;
}
void buf_rollback (buffer_t *buffer, size_t n)
{
if (!buffer->islocked) {
fprintf(stderr, "Warning: rollback without lock.\n");
print_backtrace();
}
buf_move_it_bw(buffer, n);
buffer->avail += n;
}
void buf_rollback_and_unlock (buffer_t *buffer, size_t n)
{
buf_rollback(buffer, n);
buf_unlock(buffer);
}
size_t buf_skipblank (buffer_t *buffer)
{
bool waslocked = false;
if (!buffer->islocked) {
buf_lock(buffer);
} else {
waslocked = true;
}
size_t count = 0;
char next = buf_getchar(buffer);
while (ISBLANK(next)) {
next = buf_getchar(buffer);
count++;
}
buf_rollback(buffer, 1);
if (!waslocked)
buf_unlock(buffer);
if (next == '\0') {
buffer->avail = 0;
}
return count;
}
char buf_getchar_rollback (buffer_t *buffer)
{
bool waslocked = true;
buf_skipblank(buffer);
if (!buffer->islocked) {
buf_lock(buffer);
waslocked = false;
}
char next = buf_getchar(buffer);
buf_rollback(buffer, 1);
if (!waslocked)
buf_unlock(buffer);
return next;
}
void buf_print (buffer_t *buffer)
{
printf(COLOR_BLUE "#### <buffer> ####\n" COLOR_DEFAULT);
#ifdef WIN32
printf(COLOR_GREEN "it: %u\nend: %u\navail: %u\nlock: %u\n" COLOR_DEFAULT,
buffer->it,
buffer->end,
buffer->avail,
buffer->lock);
#else
printf(COLOR_GREEN "it: %zu\nend: %zu\navail: %zu\nlock: %zu\n" COLOR_DEFAULT,
buffer->it,
buffer->end,
buffer->avail,
buffer->lock);
#endif
char *color = NULL;
for (size_t i = 0; i < BUF_SIZE; i++) {
if (buffer->it != i && buffer->end != i && (!buffer->islocked || buffer->lock != i)) {
printf("%c", buffer->content[i]);
continue;
}
if (buffer->it == i)
color = COLOR_BG_GREEN;
else if (buffer->end == i)
color = COLOR_BG_RED;
else if (buffer->islocked && buffer->lock == i)
color = COLOR_BG_BLUE;
if (buffer->content[i] == '\n')
printf("%s %s\n", color, COLOR_DEFAULT);
else
printf("%s%c" COLOR_DEFAULT, color, buffer->content[i]);
}
printf(COLOR_BLUE "#### </buffer> ####\n" COLOR_DEFAULT);
}
/*
void buf_init_with_string(buffer_t *buffer, const char *str, size_t str_len)
{
assert(str_len < BUF_SIZE);
memset(buffer->content, 0, BUF_SIZE);
memcpy(buffer->content, str, str_len);
buffer->end = str_len - 1;
buffer->avail = str_len;
buffer->it = 0;
buffer->lock = 0;
buffer->islocked = false;
buffer->bytesread = 0;
buffer->bytesreadsincelock = 0;
buffer->currchar = 0;
buffer->eof = false;
}*/