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 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "initconf.h"
34
35 InitConfig *initconfig_load(
const char *file) {
36 CxMempool *mp = cxBasicMempoolCreate(
512);
37 if(!mp) {
38 return NULL;
39 }
40
41
42 ConfigParser2 parser;
43 memset(&parser,
0,
sizeof(ConfigParser2));
44 parser.mp = mp;
45 parser.filename = file;
46 parser.validateDirective = initconfig_validate_directive;
47 parser.allow_hierarchy =
0;
48
49 ConfigNode *init_config = serverconfig_load_file(&parser, file);
50 if(!init_config) {
51 cxMempoolDestroy(mp);
52 return NULL;
53 }
54
55 InitConfig *conf = cxMalloc(mp->allocator,
sizeof(InitConfig));
56 if(!conf) {
57 cxMempoolDestroy(mp);
58 return NULL;
59 }
60
61 conf->mp = mp;
62 conf->root = init_config;
63
64 return conf;
65 }
66
67 int initconfig_validate_directive(ConfigParser2 *parser, ConfigNode *node) {
68
69
70 const char *types[] = {
"Init" };
71 size_t typeindex;
72 if(serverconfig_validate_directive_name(node, types,
1, &typeindex)) {
73 return 1;
74 }
75
76
77 ConfigParam *param_err;
78 if(serverconfig_check_param_names(node, ¶m_err)) {
79 return 1;
80 }
81
82
83 cxstring fn = serverconfig_directive_get_arg(node, cx_str(
"fn"));
84 if(fn.length ==
0) {
85 return 1;
86 }
87
88 return 0;
89 }
90
91 void initconfig_free(InitConfig *conf) {
92 cxMempoolDestroy(conf->mp);
93 }
94