-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.c
68 lines (66 loc) · 1.36 KB
/
parse.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
void
parse(FILE *file, void (*fspec)(char *, size_t))
{
char bufstack[8192], *bufalloc = NULL;
char *buf = bufstack, *pos, *end, *rec;
size_t len, max = sizeof(bufstack);
rec = buf;
pos = buf;
do {
if (pos - buf > max / 2) {
len = pos - buf;
max *= 2;
bufalloc = realloc(bufalloc, max);
if (!bufalloc)
fatal(NULL);
if (buf == bufstack)
memcpy(bufalloc, bufstack, len);
buf = bufalloc;
rec = buf;
pos = buf + len;
}
len = fread(pos, 1, max - (pos - buf), file);
if (len != max - (pos - buf) && ferror(file))
fatal("read:");
if (rec == pos) {
next:
while (len > 0 && *pos == '\n')
++pos, --len;
if (len == 0) {
rec = pos = buf;
continue;
}
rec = pos;
if (*rec != '/')
fatal("invalid fspec: paths must begin with '/'");
}
for (;;) {
end = memchr(pos, '\n', len);
if (!end) {
if (rec > buf) {
memmove(buf, rec, pos - rec + len);
pos = buf + (pos - rec);
rec = buf;
}
pos += len;
break;
}
len -= end + 1 - pos;
pos = end + 1;
if (end > rec && end[-1] == '\n') {
fspec(rec, end - rec);
goto next;
}
}
} while (!feof(file));
if (pos - rec + len > 0) {
if (pos[len - 1] != '\n')
fatal("invalid fspec: truncated");
fspec(rec, pos - rec + len);
}
free(bufalloc);
}