forked from andbof/yastg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseconfig.c
208 lines (172 loc) · 3.83 KB
/
parseconfig.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
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include "common.h"
#include "log.h"
#include "parseconfig.h"
#define MALLOC_DIE(VAR, SIZE) \
do { \
if (!(VAR = malloc(SIZE))) \
die("malloc %zu bytes failed", SIZE); \
} while(0)
#define REALLOC_DIE(VAR, SIZE) \
do { \
if (!(VAR = realloc(VAR, SIZE))) \
die("realloc %zu bytes failed", SIZE); \
} while(0)
/*
* Duplicates a string but removes whitespace from the beginning and end of the string.
* If the string consists of just whitespace, an empty string is returned.
*/
static char* strdupsp(char *s)
{
long unsigned int i = 0;
char *t;
i = 0;
while ((i < strlen(s)) && ((s[i] == ' ') || (s[i] == '\t')))
i++;
if (i < strlen(s)) {
t = strdup(s+i);
i = strlen(t);
while ((t[i] == ' ') || (t[i] == '\t'))
i--;
t[i] = '\0';
} else {
t = NULL;
}
return t;
}
/*
* Parses config files with the following syntax
*
* KEY1 DATA
* KEY2 DATA
* KEY4 DATA {
* KEY4.1 DATA
* KEY4.2 DATA
* }
*
* and returns a config structure.
*/
struct config* parseconfig(char *fname)
{
FILE *f;
struct config *root;
MALLOC_DIE(root, sizeof(*root));
if (!(f = fopen(fname, "r"))) {
die("%s", "parseconfig failed to open file\n");
}
root->key = strdup(fname);
root->data = NULL;
root->next = NULL;
root->sub = recparseconfig(f, fname);
printf("Done with %s", fname);
fclose(f);
return root;
}
struct config* recparseconfig(FILE *f, char *fname)
{
struct config *root = NULL, *ce = NULL;
int hassub;
size_t i, j;
unsigned long linen = 1;
size_t buflen = 128, bufidx = 0;
char *buf;
char *c;
MALLOC_DIE(buf, buflen);
while (fgets(buf+bufidx, buflen-bufidx, f) != NULL) {
if (!(strchr(buf, '\n'))) {
/* We haven't read a whole line. Increase the buffer size
and try again. */
if (buflen < INT_MAX << 2)
buflen <<= 2;
else if (buflen == INT_MAX)
die("line %lu too long in %s", linen, fname);
else
buflen = INT_MAX;
REALLOC_DIE(buf, buflen);
} else {
/* Remove line break */
c = strchr(buf, '\n');
*c = '\0';
/* Remove comments */
c = strchr(buf, '#');
if (c)
*c = '\0';
if (strlen(buf) > 0) {
/* Remove preceding whitespace */
i = 0;
while ((buf[i] == ' ') || (buf[i] == '\t'))
i++;
/* This is the end of a sub */
if (buf[i] == '}')
break;
/* Allocate space for field */
if (root == NULL) {
MALLOC_DIE(root, sizeof(*root));
ce = root;
} else {
MALLOC_DIE(ce->next, sizeof(*(ce->next)));
ce = ce->next;
ce->next = NULL;
}
/* Extract key */
j = i;
while ((j < strlen(buf)) && (buf[j] != ' '))
j++;
if (j == strlen(buf)) {
ce->key = strdupsp(buf+i);
ce->data = NULL;
ce->sub = NULL;
} else {
buf[j] = '\0';
ce->key = strdupsp(buf+i);
j++;
/* Determine if this line is a parent or not (trailed by '{') */
hassub = 0;
if ((c = strchr(buf+j, '{'))) {
*c = '\0';
hassub = 1;
}
/* Remove trailing whitespace */
/* printf("j is %d\n", j); */
c = strchr(buf+j, '\0')-1;
while ((*c == ' ' || *c == '\t' ) && (*c != '\0'))
c--;
if (c == '\0') {
ce->data = NULL;
} else {
*(c+1) = '\0';
ce->data = strdupsp(buf+j);
}
/* If this has subelements, parse them recursively */
if (hassub)
ce->sub = recparseconfig(f, fname);
else
ce->sub = NULL;
}
}
}
}
free(buf);
return root;
}
/*
* Recursively free all subnodes in a config.
* Will free the entire tree if called on root node.
*/
void destroyctree(struct config *ctree)
{
struct config *ptree;
while (ctree) {
destroyctree(ctree->sub);
if (ctree->key)
free(ctree->key);
if (ctree->data)
free(ctree->data);
ptree = ctree->next;
free(ctree);
ctree = ptree;
}
}