30 #define CFG_CONF_H |
30 #define CFG_CONF_H |
31 |
31 |
32 #include <stdio.h> |
32 #include <stdio.h> |
33 #include <stdlib.h> |
33 #include <stdlib.h> |
34 |
34 |
35 #include <ucx/list.h> |
35 #include <cx/linked_list.h> |
36 #include <ucx/map.h> |
36 #include <cx/hash_map.h> |
37 #include <ucx/mempool.h> |
37 #include <cx/mempool.h> |
38 #include <ucx/string.h> |
38 #include <cx/basic_mempool.h> |
|
39 #include <cx/string.h> |
|
40 #include <cx/utils.h> |
|
41 #include <cx/compare.h> |
39 |
42 |
40 #include "../util/object.h" |
43 #include "../util/object.h" |
41 |
44 |
42 #ifdef __cplusplus |
45 #ifdef __cplusplus |
43 extern "C" { |
46 extern "C" { |
44 #endif |
47 #endif |
45 |
48 |
46 // mempool malloc macro |
49 // mempool malloc macro |
47 #define OBJ_NEW(p, type) (type*)(p)->malloc((p)->pool, sizeof(type)) |
50 #define OBJ_NEW(p, type) (type*)cxMalloc(p, sizeof(type)) |
48 #define OBJ_NEW_N(p, type) (type*)(p)->calloc((p)->pool, 1, sizeof(type)) |
51 #define OBJ_NEW_N(p, type) (type*)cxCalloc(p, 1, sizeof(type)) |
49 |
52 |
50 // line types |
53 // line types |
51 #define LINE_OTHER 0 |
54 #define LINE_OTHER 0 |
52 #define LINE_DIRECTIVE 1 |
55 #define LINE_DIRECTIVE 1 |
53 #define LINE_BEGIN_TAG 2 |
56 #define LINE_BEGIN_TAG 2 |
63 #define TAG_ELSE 3 |
66 #define TAG_ELSE 3 |
64 #define TAG_CLIENT 4 |
67 #define TAG_CLIENT 4 |
65 |
68 |
66 |
69 |
67 #define INIT_DIRECTIVE 16 |
70 #define INIT_DIRECTIVE 16 |
|
71 |
|
72 #define CFG_LINE_ADD(list_begin, list_end, elm) \ |
|
73 cx_linked_list_add((void**)list_begin, (void**)list_end, offsetof(ConfigLineList, prev), offsetof(ConfigLineList, next), elm) |
|
74 |
|
75 #define CFG_PARAM_ADD(list_begin, list_end, elm) \ |
|
76 cx_linked_list_add((void**)list_begin, (void**)list_end, -1, offsetof(ConfigParam, next), elm) |
68 |
77 |
|
78 #define CFG_DIRECTIVES_ADD(list, dir) \ |
|
79 cx_linked_list_add((void**)list, NULL, -1, offsetof(ConfigDirectiveList, next), dir) |
|
80 |
|
81 #define CFG_NUM_PARAMS(param) cx_linked_list_size(param, offsetof(ConfigParam, next)) |
|
82 |
69 typedef struct _cfg_line { |
83 typedef struct _cfg_line { |
70 sstr_t line; // raw line string |
84 cxmutstr line; // raw line string |
71 void *object; // pointer to data struct |
85 void *object; // pointer to data struct |
72 int type; // type, see line types |
86 int type; // type, see line types |
73 } ConfigLine; |
87 } ConfigLine; |
74 |
88 |
75 typedef int (*cfg_parse_f)(void *, ConfigLine *, ConfigLine *, sstr_t); |
89 typedef int (*cfg_parse_f)(void *, ConfigLine *, ConfigLine *, cxmutstr); |
76 |
90 |
77 typedef struct _cfg_param { |
91 typedef struct _cfg_param ConfigParam; |
78 sstr_t name; |
92 struct _cfg_param { |
79 sstr_t value; |
93 cxmutstr name; |
80 } ConfigParam; |
94 cxmutstr value; |
|
95 ConfigParam *next; |
|
96 }; |
|
97 |
|
98 typedef struct ConfigLineList ConfigLineList; |
|
99 struct ConfigLineList { |
|
100 ConfigLine *line; |
|
101 ConfigLineList *prev; |
|
102 ConfigLineList *next; |
|
103 }; |
81 |
104 |
82 typedef struct _cfg_parser { |
105 typedef struct _cfg_parser { |
83 UcxAllocator *mp; |
106 CxAllocator *mp; |
84 UcxList *lines; |
107 ConfigLineList *lines_begin; |
85 cfg_parse_f parse; |
108 ConfigLineList *lines_end; |
|
109 cfg_parse_f parse; |
86 } ConfigParser; |
110 } ConfigParser; |
87 |
111 |
88 |
112 |
89 typedef struct _conf_tag ConfigTag; |
113 typedef struct _conf_tag ConfigTag; |
90 struct _conf_tag { |
114 struct _conf_tag { |
91 ConfigLine *begin; |
115 ConfigLine *begin; |
92 ConfigLine *end; |
116 ConfigLine *end; |
93 |
117 |
94 sstr_t name; |
118 cxmutstr name; |
95 UcxList *param; |
119 ConfigParam *param; |
96 sstr_t param_str; |
120 cxmutstr param_str; |
97 ConfigTag *parent; |
121 ConfigTag *parent; |
98 ConfigTag *iftag; // only used by <ElseIf> and <Else> |
122 ConfigTag *iftag; // only used by <ElseIf> and <Else> |
99 int type_num; |
123 int type_num; |
100 }; |
124 }; |
101 |
125 |
102 typedef struct _conf_directive { |
126 typedef struct _conf_directive { |
103 ConfigLine *begin; |
127 ConfigLine *begin; |
104 ConfigLine *end; |
128 ConfigLine *end; |
105 |
129 |
106 sstr_t directive_type; |
130 cxmutstr directive_type; |
107 sstr_t value; |
131 cxmutstr value; |
108 //UcxList *param; |
132 //UcxList *param; |
109 ConfigTag *condition; |
133 ConfigTag *condition; |
110 int type_num; |
134 int type_num; |
111 } ConfigDirective; |
135 } ConfigDirective; |
112 |
136 |
|
137 typedef struct ConfigDirectiveList ConfigDirectiveList; |
|
138 struct ConfigDirectiveList { |
|
139 ConfigDirective *directive; |
|
140 ConfigDirectiveList *next; |
|
141 }; |
113 |
142 |
114 int cfg_parse_basic_file(ConfigParser *parser, FILE *in); |
143 int cfg_parse_basic_file(ConfigParser *parser, FILE *in); |
115 |
144 |
116 sstr_t cfg_readln(FILE *file); |
145 cxmutstr cfg_readln(FILE *file); |
117 |
146 |
118 sstr_t cfg_trim_comment(sstr_t line); |
147 cxmutstr cfg_trim_comment(cxmutstr line); |
119 |
148 |
120 sstr_t cfg_param(sstr_t params, sstr_t *name, sstr_t *value); |
149 cxmutstr cfg_param(cxmutstr params, cxmutstr *name, cxmutstr *value); |
121 |
150 |
122 sstr_t cfg_param_get(UcxList *list, sstr_t name); |
151 cxmutstr cfg_param_get(ConfigParam *list, cxstring name); |
123 |
152 |
124 ConfigDirective* cfg_parse_directive(sstr_t line, UcxAllocator *mp); |
153 ConfigDirective* cfg_parse_directive(cxmutstr line, CxAllocator *mp); |
125 |
154 |
126 UcxList* cfg_param_list(sstr_t param_str, UcxAllocator *mp); |
155 ConfigParam* cfg_param_list(cxmutstr param_str, CxAllocator *mp); |
127 |
156 |
128 int cfg_get_directive_type_num(sstr_t type); |
157 int cfg_get_directive_type_num(cxstring type); |
129 |
158 |
130 int cfg_get_basic_type(sstr_t line); |
159 int cfg_get_basic_type(cxmutstr line); |
131 |
160 |
132 int cfg_get_line_type(sstr_t line); |
161 int cfg_get_line_type(cxmutstr line); |
133 |
162 |
134 int cfg_get_tag_type(sstr_t tag); |
163 int cfg_get_tag_type(cxstring tag); |
135 |
164 |
136 sstr_t cfg_get_end_tag_name(sstr_t line); |
165 cxmutstr cfg_get_end_tag_name(cxmutstr line); |
137 |
166 |
138 ConfigTag* cfg_parse_begin_tag(sstr_t line, UcxAllocator *mp); |
167 ConfigTag* cfg_parse_begin_tag(cxmutstr line, CxAllocator *mp); |
139 |
168 |
140 ConfigDirective* cfg_directivelist_get(UcxList *dirs, sstr_t name); |
169 //ConfigDirective* cfg_directivelist_get(UcxList *dirs, cxmutstr name); |
141 |
170 |
142 sstr_t cfg_directivelist_get_str(UcxList *dirs, sstr_t name); |
171 //cxmutstr cfg_directivelist_get_str(UcxList *dirs, cxmutstr name); |
143 |
172 |
144 sstr_t cfg_directive_pstr1(ConfigDirective *dir); |
173 cxmutstr cfg_directive_pstr1(ConfigDirective *dir); |
145 |
174 |
146 void cfg_map_destr(UcxMempool *mp, UcxMap *map); |
175 |
147 |
176 |
148 #ifdef __cplusplus |
177 #ifdef __cplusplus |
149 } |
178 } |
150 #endif |
179 #endif |
151 |
180 |