/
parser.c
206 lines (175 loc) · 5.58 KB
/
parser.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
#include "parser.h"
const int MAX_LINE = 200;
/* Auxiliary functions */
/* Strip whitespace chars off end of given string, in place. Return s. */
char *rstrip(char *s)
{
char *p = s + strlen(s);
while (p > s && isspace(*--p))
*p = '\0';
return s;
}
/* Return pointer to first non-whitespace char in given string. */
char *lskip(char *s)
{
while (*s && isspace(*s))
s++;
return (char *) s;
}
/* Return pointer to the unquoted string */
char *strip_quotes(char *s)
{
char *p = s + strlen(s);
if (*--p == '"' && *s == '"') {
s++;
*p = '\0';
}
return s;
}
/* Return pointer to first char c or '#' in given string, or pointer to
* null at end of string if neither found. */
char *find_char_or_comment(char *s, char c)
{
while (*s && *s != c && *s != '#')
s++;
return (char *) s;
}
/* Version of strncpy that ensures dest (size bytes) is null-terminated. */
char *strncpy0(char *dest, const char *src, size_t size)
{
strncpy(dest, src, size);
dest[size - 1] = '\0';
return dest;
}
int conf_parse(const char *filename,
int (*handler) (void *, const char *, const char *),
void *usercfg)
{
FILE *fin;
char buf[MAX_LINE];
char *start;
char *end;
char *name;
char *value;
int error = 0;
fin = fopen(filename, "r");
if (!fin)
return -1;
int lineno = 0;
/* Scan through file line by line */
while (fgets(buf, sizeof(buf), fin) != NULL) {
lineno++;
start = lskip(rstrip(buf)); /* chop whites */
if (*start && *start != '#') {
/* Not a comment, must be a name = value pair */
end = find_char_or_comment(start, '=');
if (*end == '=') {
*end = '\0';
name = rstrip(start);
value = lskip(end + 1);
end = find_char_or_comment(value, '#');
if (*end == '#')
*end = '\0';
value = rstrip(value);
value = strip_quotes(value);
/* Valid name=value pair found, call handler */
if (handler(usercfg, name, value) && !error)
error = lineno;
} else if (!error) {
/* No '=' found on name=value line */
error = lineno;
}
}
}
fclose(fin);
return error;
}
int extinputs_parse(const char *filename, struct pulse **cfginputs, int N)
{
FILE *fin;
char buf[MAX_LINE];
char *start;
char *end;
char *values;
int error = 0;
fin = fopen(filename, "r");
if (!fin)
return -1;
int lineno = 0;
double t, q, i, d, m; /* the values to be read */
/* Scan through file line by line */
while (fgets(buf, sizeof(buf), fin) != NULL) {
lineno++;
start = lskip(rstrip(buf)); /* chop whites */
if (*start && *start != '#') {
/* Remove possible comments at the end */
end = find_char_or_comment(start, '#');
if (*end == '#')
*end = '\0';
values = rstrip(start);
/* Not a comment, must be a line containing 4 numbers */
/* Scan and check if the inputs is correctly formatted */
if (sscanf(values, "%lf %lf %lf %lf %lf", &t, &d, &q, &i, &m) == 5) {
if (t >= 0 && d > 0 && m >= 0) {
*cfginputs = append_pulse(*cfginputs, new_pulse(N, t, d, q, i, m));
} else {
printf("Durations and time onsets should be positive.\n");
printf("Check configuration file for external inputs.\n");
error = lineno;
}
} else if (!error) {
/* No '=' found on name=value line */
error = lineno;
}
}
}
fclose(fin);
return error;
}
int extinputs_parse_2D(const char *filename, struct pulse_2D **cfginputs,
int N1, int N2)
{
FILE *fin;
char buf[MAX_LINE];
char *start;
char *end;
char *values;
int error = 0;
fin = fopen(filename, "r");
if (!fin)
return -1;
int lineno = 0;
double t, q1, q2, i, d, m; /* the values to be read */
/* Scan through file line by line */
while (fgets(buf, sizeof(buf), fin) != NULL) {
lineno++;
start = lskip(rstrip(buf)); /* chop whites */
if (*start && *start != '#') {
/* Remove possible comments at the end */
end = find_char_or_comment(start, '#');
if (*end == '#')
*end = '\0';
values = rstrip(start);
/* Not a comment, must be a line containing 6 numbers */
/* Scan and check if the inputs is correctly formatted */
if (sscanf(values, "%lf %lf %lf %lf %lf %lf",
&t, &d, &q1, &q2, &i, &m) == 6) {
if (t >= 0 && d > 0 && m >= 0) {
*cfginputs =
append_pulse_2D(*cfginputs,
new_pulse_2D(N1, N2, t, d,
-q1, -q2, i, m));
} else {
printf("Durations and time onsets should be positive.\n");
printf("Check configuration file for external inputs.\n");
error = lineno;
}
} else if (!error) {
/* No '=' found on name=value line */
error = lineno;
}
}
}
fclose(fin);
return error;
}