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