-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfiguration.c
290 lines (233 loc) · 6.31 KB
/
configuration.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
#include <stdlib.h>
#include <yaml.h>
#include "configuration.h"
typedef struct
{
efflu_destination_t global;
efflu_destination_t *by_type[EFFLU_DATA_TYPE_COUNT];
}
efflu_configuration_t;
efflu_configuration_t configuration;
static int efflu_scalar_cmp(const char *str, const yaml_node_t *node)
{
size_t len;
if (YAML_SCALAR_NODE != node->type)
printf("Internal error: scalar comparison of nonscalar node.\n");
len = strlen(str);
if (len != node->data.scalar.length)
return 1;
return memcmp(str, node->data.scalar.value, len);
}
static char *efflu_strdup(const yaml_node_t *node)
{
char *tmp;
if (YAML_SCALAR_NODE != node->type)
printf("Internal error: attempt to copy nonscalar node as string.\n");
if (NULL != (tmp = malloc(node->data.scalar.length + 1)))
{
memcpy(tmp, node->data.scalar.value, node->data.scalar.length);
tmp[node->data.scalar.length] = '\0';
}
else
printf("Out of memory.\n");
return tmp;
}
typedef struct
{
const char *name;
char **storage;
}
efflu_configuration_option_t;
static int efflu_parse_node(yaml_document_t *config, const yaml_node_t *node, efflu_destination_t *top, efflu_destination_t **sub)
{
const yaml_node_pair_t *pair;
if (YAML_MAPPING_NODE != node->type)
{
printf("Node must be a mapping.\n");
return -1;
}
for (pair = node->data.mapping.pairs.start; pair < node->data.mapping.pairs.top; pair++)
{
const yaml_node_t *key;
key = yaml_document_get_node(config, pair->key);
if (YAML_SCALAR_NODE == key->type)
{
const efflu_configuration_option_t *option, options[] =
{
{"url", &top->url},
{"db", &top->db},
{"user", &top->user},
{"pass", &top->pass},
{0}
};
for (option = options; NULL != option->name; option++)
{
if (0 == efflu_scalar_cmp(option->name, key))
break;
}
if (NULL != option->name)
{
if (NULL == (*option->storage = efflu_strdup(yaml_document_get_node(config, pair->value))))
return -1;
continue;
}
if (NULL != sub)
{
efflu_data_type_t type = 0;
do
{
if (0 == efflu_scalar_cmp(efflu_data_type_string(type), key))
break;
}
while (EFFLU_DATA_TYPE_COUNT > ++type);
if (EFFLU_DATA_TYPE_COUNT > type)
{
efflu_destination_t tmp = {0};
if (NULL != sub[type])
{
printf("Configuration for \"%s\" is specified twice.\n", efflu_data_type_string(type));
return -1;
}
printf("Parsing \"%s\" configuration...\n", efflu_data_type_string(type));
if (-1 == efflu_parse_node(config, yaml_document_get_node(config, pair->value),
&tmp, NULL))
{
return -1;
}
sub[type] = malloc(sizeof(efflu_destination_t));
*sub[type] = tmp;
continue;
}
}
printf("Unexpected key \"%.*s\" in mapping.\n",
(int)key->data.scalar.length, key->data.scalar.value);
return -1;
}
else if (YAML_SEQUENCE_NODE == key->type && NULL != sub)
{
const yaml_node_item_t *item;
for (item = key->data.sequence.items.start; item < key->data.sequence.items.top; item++)
{
const yaml_node_t *elem;
efflu_data_type_t type = 0;
elem = yaml_document_get_node(config, *item);
if (YAML_SCALAR_NODE != elem->type)
{
printf("Sequence item must be a scalar.\n");
return -1;
}
do
{
if (0 == efflu_scalar_cmp(efflu_data_type_string(type), elem))
break;
}
while (EFFLU_DATA_TYPE_COUNT > ++type);
if (EFFLU_DATA_TYPE_COUNT > type)
{
efflu_destination_t tmp = {0};
if (NULL != sub[type])
{
printf("Configuration for \"%s\" is specified twice.\n", efflu_data_type_string(type));
return -1;
}
printf("Parsing \"%s\" configuration...\n", efflu_data_type_string(type));
if (-1 == efflu_parse_node(config, yaml_document_get_node(config, pair->value),
&tmp, NULL))
{
return -1;
}
sub[type] = malloc(sizeof(efflu_destination_t));
*sub[type] = tmp;
continue;
}
printf("Unexpected item \"%.*s\" in sequence.\n",
(int)elem->data.scalar.length, elem->data.scalar.value);
return -1;
}
}
else
{
printf("Unexpected type of key in mapping.\n");
return -1;
}
}
return 0;
}
int efflu_parse_configuration(FILE *config_file)
{
yaml_parser_t parser;
yaml_document_t config;
yaml_node_t *root;
int ret = -1;
yaml_parser_initialize(&parser);
yaml_parser_set_input_file(&parser, config_file);
if (0 != yaml_parser_load(&parser, &config))
{
if (NULL != (root = yaml_document_get_root_node(&config)))
{
printf("Parsing global configuration...\n");
ret = efflu_parse_node(&config, root, &configuration.global, configuration.by_type);
yaml_document_delete(&config);
}
else
printf("There are no YAML documents in the configuration file.\n");
}
else
printf("Cannot parse configuration file. Please check if it is a valid YAML file.\n");
yaml_parser_delete(&parser);
if (0 == ret)
printf("Configuration file has been successfully parsed.\n");
return ret;
}
efflu_destination_t efflu_configured_destination(efflu_data_type_t type)
{
#define EFFLU_GET_CONFIG(field) (NULL != configuration.by_type[type] ? configuration.by_type[type]->field : configuration.global.field)
return (efflu_destination_t){
.url = EFFLU_GET_CONFIG(url),
.db = EFFLU_GET_CONFIG(db),
.user = EFFLU_GET_CONFIG(user),
.pass = EFFLU_GET_CONFIG(pass)
};
#undef EFFLU_GET_CONFIG
}
const char *efflu_data_type_string(efflu_data_type_t type)
{
switch (type)
{
case EFFLU_DATA_TYPE_FLOAT:
return "dbl";
case EFFLU_DATA_TYPE_INTEGER:
return "uint";
case EFFLU_DATA_TYPE_STRING:
return "str";
case EFFLU_DATA_TYPE_TEXT:
return "text";
case EFFLU_DATA_TYPE_LOG:
return "log";
default:
return "unknown";
}
}
static void efflu_clean_destination(efflu_destination_t destination)
{
char *buffers_to_free[] = {destination.url, destination.db, destination.user, destination.pass};
size_t index = sizeof(buffers_to_free) / sizeof(char *);
while (0 < index--)
{
if (NULL != buffers_to_free[index])
free(buffers_to_free[index]);
}
}
void efflu_clean_configuration(void)
{
efflu_data_type_t type = 0;
do
{
if (NULL == configuration.by_type[type])
continue;
efflu_clean_destination(*configuration.by_type[type]);
free(configuration.by_type[type]);
}
while (EFFLU_DATA_TYPE_COUNT > ++type);
efflu_clean_destination(configuration.global);
}