-
Notifications
You must be signed in to change notification settings - Fork 0
/
vbuf.c
123 lines (107 loc) · 2.43 KB
/
vbuf.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
#include "data.h"
#define vbuf_free(text) free(text->buf);
struct vbuf *vbuf_new()
{
struct vbuf *text = xmalloc(sizeof(*text));
vbuf_init(text)
return (text);
}
void vbuf_del(struct vbuf *t)
{
vbuf_free(t);
free(t);
}
static inline void vbuf_testSpace(struct vbuf *text)
{
if (text->len == text->alloc)
text->buf = xrealloc(text->buf, text->alloc <<= 1);
}
void vbuf_addChar(struct vbuf *text, char c)
{
vbuf_testSpace(text);
text->buf[text->len++] = c;
}
void vbuf_addNull(struct vbuf *text)
{
vbuf_testSpace(text);
text->buf[text->len] = 0;
}
struct vbuf *read_text()
{
char c;
struct vbuf *text = vbuf_new();
while ((c = nextChar()) != '\n' && c && c != EOF)
vbuf_addChar(text, c);
vbuf_addNull(text);
return (text);
}
void vbuf_addString(struct vbuf *text, char *s, int len)
{
if (text->len + len > text->alloc) {
while (text->len > (text->alloc <<= 1));
text->buf = realloc(text->buf, text->alloc);
}
memcpy(text->buf + text->len, s, len);
text->len += len;
}
ssize_t vbuf_getline(struct vbuf *text, FILE *in)
{
text->len = getline(&text->buf, &text->alloc, in);
if (text->len > 0)
text->buf[--text->len] = 0;
return (text->len);
}
struct vbuf *snarf(char delim, char regex)
{ // regex can be -1 (basic), 1 (extended) or 0 (never ignore delim)
char in;
struct vbuf *text = vbuf_new();
int open_paren = 0;
while ((in = nextChar()) != delim || open_paren) {
if (in == EOF) {
vbuf_del(text);
return (NULL);
}
else if (in == '\\') {
if ((in = nextChar()) == 'n')
vbuf_addChar(text, '\n');
else {
vbuf_addChar(text, '\\');
vbuf_addChar(text, in);
}
if (regex == -1)
in == '(' && ++open_paren ||
in == ')' && --open_paren;
continue;
}
(in == '(' && regex == 1 || in == '[') && ++open_paren;
(in == ')' && regex == 1 || in == ']') && --open_paren;
vbuf_addChar(text, in);
}
vbuf_addNull(text);
return (text);
}
struct vbuf *vbuf_readName()
{
char in;
struct vbuf *text = vbuf_new();
if ((in = nextChar()) == '\\' && (in = nextChar()) == '\n')
in = nextChar();
while (isblank(in = nextChar()))
;
while (in && in != '\n' && in != EOF) {
vbuf_addChar(text, in);
in = nextChar();
}
vbuf_addNull(text);
if (text->len == 0) {
vbuf_del(text);
return (NULL);
}
return (text);
}
char *vbuf_tostring(struct vbuf *vbuf)
{
char *r = vbuf->buf;
free(vbuf);
return (r);
}