-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_core.c
343 lines (291 loc) · 7.27 KB
/
parser_core.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include "parser_core.h"
struct file_info *open_file(const char *filename) {
struct file_info *finfo;
finfo = (struct file_info *)malloc(sizeof(struct file_info));
if (!finfo) {
perror(__FUNCTION__);
exit(EXIT_FAILURE);
}
struct stat fstats;
int error = stat(filename,&fstats);
if (error) {
perror(__FUNCTION__);
exit(EXIT_FAILURE);
}
int fd = open(filename,O_RDONLY);
if (fd == -1) {
perror(__FUNCTION__);
exit(EXIT_FAILURE);
}
char *raw_data =
mmap(NULL,fstats.st_size,PROT_READ,MAP_POPULATE | MAP_PRIVATE,fd,0);
if (raw_data == MAP_FAILED) {
perror(__FUNCTION__);
exit(EXIT_FAILURE);
}
error = close(fd);
if (error) {
perror(__FUNCTION__);
exit(EXIT_FAILURE);
}
finfo->name = (char *)malloc((strlen(filename) + 1) * sizeof(char));
if (!finfo->name) {
perror(__FUNCTION__);
exit(EXIT_FAILURE);
}
finfo->name = strcpy(finfo->name,filename);
finfo->size = fstats.st_size;
finfo->raw_begin = raw_data;
finfo->raw_end = &raw_data[fstats.st_size];
finfo->pos = finfo->raw_begin;
finfo->line_num = 1;
return finfo;
}
void close_file(struct file_info **finfo) {
int error = munmap((*finfo)->raw_begin,(*finfo)->size);
if (error) {
perror(__FUNCTION__);
exit(EXIT_FAILURE);
}
free((*finfo)->name);
free(*finfo);
*finfo = NULL;
}
void parse_eat_whitechars(struct file_info *input) {
assert(input);
char **buf = &input->pos;
if (!*buf)
return;
char *end = input->raw_end;
if (*buf == end) {
*buf = NULL;
return;
}
while (*buf != end) {
if (!isspace(**buf) || **buf == '\n')
break;
(*buf)++;
}
if (*buf == end)
*buf = NULL;
}
void parse_eat_newline(struct file_info *input) {
/* skip all CR and LF */
assert(input);
char **buf = &input->pos;
char *end = input->raw_end;
while (*buf != end) {
if (**buf == '\r')
;
else if (**buf != '\n')
break;
else
input->line_num++;
(*buf)++;
}
if (*buf == end)
*buf = NULL;
}
int discard_line(struct file_info *input) {
assert(input);
char **buf = &input->pos;
int bad_chars = 0;
char *end = input->raw_end;
if (!(*buf))
return 0;
while (*buf != end) {
if (**buf == '\n')
break;
else if (**buf != '\r')
bad_chars++;
(*buf)++;
}
if (*buf == end)
*buf = NULL;
return bad_chars;
}
int isdelimiter(char c) {
if (isspace(c))
return 1;
if (isalnum(c))
return 0;
//custom ispunct()
switch (c) {
case '!':
case '"':
case '#':
return 1;
case '$':
return 0;
case '%':
case '&':
case 0x27:
case '(':
case ')':
case '*':
case '+':
case ',':
return 1;
case '-':
return 0;
case '.':
case '/':
case ':':
case ';':
case '<':
case '=':
case '>':
case '?':
case '@':
case '[':
case '\\':
case ']':
case '^':
return 1;
case '_':
return 0;
case '`':
case '{':
case '|':
case '}':
case '~':
return 1;
}
return 1;
}
char *parse_string(struct file_info *input, char *info) {
assert(input);
char **buf = &input->pos;
//printf("in function: %s\n",__FUNCTION__);
if (!*buf || isdelimiter(**buf)) {
printf("error:%lu: expected %s - exit.\n",input->line_num,info);
exit(EXIT_FAILURE);
}
char *start = *buf;
int size = 0;
char *end = input->raw_end;
while (*buf != end) {
if (isdelimiter(**buf)) {
break;
}
(*buf)++;
size++;
}
char *name = (char*)malloc((size+1) * sizeof(char));
if (!name) {
perror(__FUNCTION__);
exit(EXIT_FAILURE);
}
name = strncpy(name,start,size);
name[size] = '\0';
//convert to lowercase
int i;
for (i=0; i<size; ++i)
name[i] = tolower(name[i]);
//printf("debug: new string : %s\n",name);
parse_eat_whitechars(input);
//printf("debug: new string \"%s\"\tsize=%d\n",*name,size);
//printf("out of function: %s\n",__FUNCTION__);
return name;
}
char parse_char(struct file_info *input, char *patterns, char *info) {
assert(input);
char **buf = &input->pos;
//if info == NULL, do not fail if patterns not found and return NULL char '\0'
assert(patterns && strlen(patterns));
char *end = input->raw_end;
if (*buf == end)
*buf = NULL;
if (!*buf) {
if (!info)
return '\0';
printf("error:%lu: expected %s - exit.\n",input->line_num,info);
exit(EXIT_FAILURE);
}
char c = tolower(**buf);
char *p = patterns;
while (*p != '\0' && c != tolower(*p))
p++;
if (*p == '\0') {
if (!info)
return '\0';
printf("error:%lu: parsing %s, expected '%s' got '%c' 0x%02x (hex ascii) - exit\n",
input->line_num,info,patterns,c,c);
exit(EXIT_FAILURE);
}
(*buf)++;
if (*buf == end)
*buf = NULL;
return c;
}
double parse_value(struct file_info *input, char *prefix, char *info) {
//printf("in function: %s\n",__FUNCTION__);
assert(input);
char **buf = &input->pos;
if (!*buf || isdelimiter(**buf)) {
printf("error:%lu: expected %s - exit\n",input->line_num,info);
exit(EXIT_FAILURE);
}
if (prefix) {
int i;
int size = strlen(prefix);
for (i=0; i<size; ++i) {
char c = tolower(**buf);
char p = tolower(prefix[i]);
if (c != p) {
printf("error:%lu: expected prefix '%s' before value - exit\n",
input->line_num,prefix);
exit(EXIT_FAILURE);
}
(*buf)++;
}
}
char *end = input->raw_end;
errno = 0;
double value;
int offset = 0;
int status = sscanf(*buf,"%lf%n",&value,&offset);
if (errno) {
perror(__FUNCTION__);
exit(EXIT_FAILURE);
}
if (status == EOF) {
printf("error: sscanf() early fail\n");
exit(EXIT_FAILURE);
}
if (status == 0) {
printf("error: sscanf() bad input\n");
exit(EXIT_FAILURE);
}
(*buf) += offset;
if (*buf == end)
*buf = NULL;
if (*buf && !isdelimiter(**buf)) {
printf("error:%lu: invalid character '%c' after value %lf - exit.\n",
input->line_num,**buf,value);
exit(EXIT_FAILURE);
}
parse_eat_whitechars(input);
//printf("debug: value=%lf\n",*value);
return value;
}
double parse_value_optional(struct file_info *input, char *prefix,
double default_value) {
//printf("in function: %s\n",__FUNCTION__);
assert(input);
char **buf = &input->pos;
if (!*buf || isdelimiter(**buf))
return default_value;
else
return parse_value(input,prefix,"value");
}