1 | /* ---------------------------------------------------------------------------- |
---|
2 | libconfig - A library for processing structured configuration files |
---|
3 | Copyright (C) 2005-2008 Mark A Lindner |
---|
4 | |
---|
5 | This file is part of libconfig. |
---|
6 | |
---|
7 | This library is free software; you can redistribute it and/or |
---|
8 | modify it under the terms of the GNU Lesser General Public License |
---|
9 | as published by the Free Software Foundation; either version 2.1 of |
---|
10 | the License, or (at your option) any later version. |
---|
11 | |
---|
12 | This library is distributed in the hope that it will be useful, but |
---|
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | Lesser General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Library General Public |
---|
18 | License along with this library; if not, see |
---|
19 | <http://www.gnu.org/licenses/>. |
---|
20 | ---------------------------------------------------------------------------- |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef __libconfig_h |
---|
24 | #define __libconfig_h |
---|
25 | |
---|
26 | #ifdef __cplusplus |
---|
27 | extern "C" { |
---|
28 | #endif /* __cplusplus */ |
---|
29 | |
---|
30 | #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
---|
31 | #if defined(LIBCONFIG_STATIC) |
---|
32 | #define LIBCONFIG_API |
---|
33 | #elif defined(LIBCONFIG_EXPORTS) |
---|
34 | #define LIBCONFIG_API __declspec(dllexport) |
---|
35 | #else /* ! LIBCONFIG_EXPORTS */ |
---|
36 | #define LIBCONFIG_API __declspec(dllimport) |
---|
37 | #endif /* LIBCONFIG_STATIC */ |
---|
38 | #else /* ! WIN32 */ |
---|
39 | #define LIBCONFIG_API |
---|
40 | #endif /* WIN32 */ |
---|
41 | |
---|
42 | #include <stdio.h> |
---|
43 | |
---|
44 | #define CONFIG_TYPE_NONE 0 |
---|
45 | #define CONFIG_TYPE_GROUP 1 |
---|
46 | #define CONFIG_TYPE_INT 2 |
---|
47 | #define CONFIG_TYPE_INT64 3 |
---|
48 | #define CONFIG_TYPE_FLOAT 4 |
---|
49 | #define CONFIG_TYPE_STRING 5 |
---|
50 | #define CONFIG_TYPE_BOOL 6 |
---|
51 | #define CONFIG_TYPE_ARRAY 7 |
---|
52 | #define CONFIG_TYPE_LIST 8 |
---|
53 | |
---|
54 | #define CONFIG_FORMAT_DEFAULT 0 |
---|
55 | #define CONFIG_FORMAT_HEX 1 |
---|
56 | |
---|
57 | #define CONFIG_OPTION_AUTOCONVERT 0x01 |
---|
58 | |
---|
59 | #define CONFIG_TRUE (1) |
---|
60 | #define CONFIG_FALSE (0) |
---|
61 | |
---|
62 | typedef union config_value_t |
---|
63 | { |
---|
64 | long ival; |
---|
65 | long long llval; |
---|
66 | double fval; |
---|
67 | char *sval; |
---|
68 | struct config_list_t *list; |
---|
69 | } config_value_t; |
---|
70 | |
---|
71 | typedef struct config_setting_t |
---|
72 | { |
---|
73 | char *name; |
---|
74 | short type; |
---|
75 | short format; |
---|
76 | config_value_t value; |
---|
77 | struct config_setting_t *parent; |
---|
78 | struct config_t *config; |
---|
79 | void *hook; |
---|
80 | unsigned int line; |
---|
81 | } config_setting_t; |
---|
82 | |
---|
83 | typedef struct config_list_t |
---|
84 | { |
---|
85 | unsigned int length; |
---|
86 | unsigned int capacity; |
---|
87 | config_setting_t **elements; |
---|
88 | } config_list_t; |
---|
89 | |
---|
90 | typedef struct config_t |
---|
91 | { |
---|
92 | config_setting_t *root; |
---|
93 | void (*destructor)(void *); |
---|
94 | int flags; |
---|
95 | const char *error_text; |
---|
96 | int error_line; |
---|
97 | } config_t; |
---|
98 | |
---|
99 | extern LIBCONFIG_API int config_read(config_t *config, FILE *stream); |
---|
100 | extern LIBCONFIG_API void config_write(const config_t *config, FILE *stream); |
---|
101 | |
---|
102 | extern LIBCONFIG_API void config_set_auto_convert(config_t *config, int flag); |
---|
103 | extern LIBCONFIG_API int config_get_auto_convert(const config_t *config); |
---|
104 | |
---|
105 | extern LIBCONFIG_API int config_read_file(config_t *config, |
---|
106 | const char *filename); |
---|
107 | extern LIBCONFIG_API int config_write_file(config_t *config, |
---|
108 | const char *filename); |
---|
109 | |
---|
110 | extern LIBCONFIG_API void config_set_destructor(config_t *config, |
---|
111 | void (*destructor)(void *)); |
---|
112 | |
---|
113 | extern LIBCONFIG_API void config_init(config_t *config); |
---|
114 | extern LIBCONFIG_API void config_destroy(config_t *config); |
---|
115 | |
---|
116 | extern LIBCONFIG_API long config_setting_get_int( |
---|
117 | const config_setting_t *setting); |
---|
118 | extern LIBCONFIG_API long long config_setting_get_int64( |
---|
119 | const config_setting_t *setting); |
---|
120 | extern LIBCONFIG_API double config_setting_get_float( |
---|
121 | const config_setting_t *setting); |
---|
122 | extern LIBCONFIG_API int config_setting_get_bool( |
---|
123 | const config_setting_t *setting); |
---|
124 | extern LIBCONFIG_API const char *config_setting_get_string( |
---|
125 | const config_setting_t *setting); |
---|
126 | |
---|
127 | extern LIBCONFIG_API int config_setting_set_int(config_setting_t *setting, |
---|
128 | long value); |
---|
129 | extern LIBCONFIG_API int config_setting_set_int64(config_setting_t *setting, |
---|
130 | long long value); |
---|
131 | extern LIBCONFIG_API int config_setting_set_float(config_setting_t *setting, |
---|
132 | double value); |
---|
133 | extern LIBCONFIG_API int config_setting_set_bool(config_setting_t *setting, |
---|
134 | int value); |
---|
135 | extern LIBCONFIG_API int config_setting_set_string(config_setting_t *setting, |
---|
136 | const char *value); |
---|
137 | |
---|
138 | extern LIBCONFIG_API int config_setting_set_format(config_setting_t *setting, |
---|
139 | short format); |
---|
140 | extern LIBCONFIG_API short config_setting_get_format(config_setting_t *setting); |
---|
141 | |
---|
142 | extern LIBCONFIG_API long config_setting_get_int_elem( |
---|
143 | const config_setting_t *setting, int idx); |
---|
144 | extern LIBCONFIG_API long long config_setting_get_int64_elem( |
---|
145 | const config_setting_t *setting, int idx); |
---|
146 | extern LIBCONFIG_API double config_setting_get_float_elem( |
---|
147 | const config_setting_t *setting, int idx); |
---|
148 | extern LIBCONFIG_API int config_setting_get_bool_elem( |
---|
149 | const config_setting_t *setting, int idx); |
---|
150 | extern LIBCONFIG_API const char *config_setting_get_string_elem( |
---|
151 | const config_setting_t *setting, int idx); |
---|
152 | |
---|
153 | extern LIBCONFIG_API config_setting_t *config_setting_set_int_elem( |
---|
154 | config_setting_t *setting, int idx, long value); |
---|
155 | extern LIBCONFIG_API config_setting_t *config_setting_set_int64_elem( |
---|
156 | config_setting_t *setting, int idx, long long value); |
---|
157 | extern LIBCONFIG_API config_setting_t *config_setting_set_float_elem( |
---|
158 | config_setting_t *setting, int idx, double value); |
---|
159 | extern LIBCONFIG_API config_setting_t *config_setting_set_bool_elem( |
---|
160 | config_setting_t *setting, int idx, int value); |
---|
161 | extern LIBCONFIG_API config_setting_t *config_setting_set_string_elem( |
---|
162 | config_setting_t *setting, int idx, const char *value); |
---|
163 | |
---|
164 | #define /* int */ config_setting_type(/* const config_setting_t * */ S) \ |
---|
165 | ((S)->type) |
---|
166 | |
---|
167 | #define /* int */ config_setting_is_group(/* const config_setting_t * */ S) \ |
---|
168 | ((S)->type == CONFIG_TYPE_GROUP) |
---|
169 | #define /* int */ config_setting_is_array(/* const config_setting_t * */ S) \ |
---|
170 | ((S)->type == CONFIG_TYPE_ARRAY) |
---|
171 | #define /* int */ config_setting_is_list(/* const config_setting_t * */ S) \ |
---|
172 | ((S)->type == CONFIG_TYPE_LIST) |
---|
173 | |
---|
174 | #define /* int */ config_setting_is_aggregate( \ |
---|
175 | /* const config_setting_t * */ S) \ |
---|
176 | (((S)->type == CONFIG_TYPE_GROUP) || ((S)->type == CONFIG_TYPE_LIST) \ |
---|
177 | || ((S)->type == CONFIG_TYPE_ARRAY)) |
---|
178 | |
---|
179 | #define /* int */ config_setting_is_number(/* const config_setting_t * */ S) \ |
---|
180 | (((S)->type == CONFIG_TYPE_INT) \ |
---|
181 | || ((S)->type == CONFIG_TYPE_INT64) \ |
---|
182 | || ((S)->type == CONFIG_TYPE_FLOAT)) |
---|
183 | |
---|
184 | #define /* int */ config_setting_is_scalar(/* const config_setting_t * */ S) \ |
---|
185 | (((S)->type == CONFIG_TYPE_BOOL) || ((S)->type == CONFIG_TYPE_STRING) \ |
---|
186 | || config_setting_is_number(S)) |
---|
187 | |
---|
188 | #define /* const char * */ config_setting_name( \ |
---|
189 | /* const config_setting_t * */ S) \ |
---|
190 | ((S)->name) |
---|
191 | |
---|
192 | #define /* config_setting_t * */ config_setting_parent( \ |
---|
193 | /* const config_setting_t * */ S) \ |
---|
194 | ((S)->parent) |
---|
195 | |
---|
196 | #define /* int */ config_setting_is_root( \ |
---|
197 | /* const config_setting_t * */ S) \ |
---|
198 | ((S)->parent ? CONFIG_FALSE : CONFIG_TRUE) |
---|
199 | |
---|
200 | extern LIBCONFIG_API int config_setting_index(const config_setting_t *setting); |
---|
201 | |
---|
202 | extern LIBCONFIG_API int config_setting_length( |
---|
203 | const config_setting_t *setting); |
---|
204 | extern LIBCONFIG_API config_setting_t *config_setting_get_elem( |
---|
205 | const config_setting_t *setting, unsigned int idx); |
---|
206 | |
---|
207 | extern LIBCONFIG_API config_setting_t *config_setting_get_member( |
---|
208 | const config_setting_t *setting, const char *name); |
---|
209 | |
---|
210 | extern LIBCONFIG_API config_setting_t *config_setting_add( |
---|
211 | config_setting_t *parent, const char *name, int type); |
---|
212 | extern LIBCONFIG_API int config_setting_remove(config_setting_t *parent, |
---|
213 | const char *name); |
---|
214 | extern LIBCONFIG_API int config_setting_remove_elem(config_setting_t *parent, |
---|
215 | unsigned int idx); |
---|
216 | extern LIBCONFIG_API void config_setting_set_hook(config_setting_t *setting, |
---|
217 | void *hook); |
---|
218 | |
---|
219 | #define config_setting_get_hook(S) ((S)->hook) |
---|
220 | |
---|
221 | extern LIBCONFIG_API config_setting_t *config_lookup(const config_t *config, |
---|
222 | const char *path); |
---|
223 | |
---|
224 | extern LIBCONFIG_API long config_lookup_int(const config_t *config, |
---|
225 | const char *path); |
---|
226 | extern LIBCONFIG_API long long config_lookup_int64(const config_t *config, |
---|
227 | const char *path); |
---|
228 | extern LIBCONFIG_API double config_lookup_float(const config_t *config, |
---|
229 | const char *path); |
---|
230 | extern LIBCONFIG_API int config_lookup_bool(const config_t *config, |
---|
231 | const char *path); |
---|
232 | extern LIBCONFIG_API const char *config_lookup_string(const config_t *config, |
---|
233 | const char *path); |
---|
234 | |
---|
235 | #define /* config_setting_t * */ config_root_setting( \ |
---|
236 | /* const config_t * */ C) \ |
---|
237 | ((C)->root) |
---|
238 | |
---|
239 | #define /* unsigned short */ config_setting_source_line( \ |
---|
240 | /* const config_t */ C) \ |
---|
241 | ((C)->line) |
---|
242 | |
---|
243 | #define /* const char * */ config_error_text(/* const config_t * */ C) \ |
---|
244 | ((C)->error_text) |
---|
245 | |
---|
246 | #define /* int */ config_error_line(/* const config_t * */ C) \ |
---|
247 | ((C)->error_line) |
---|
248 | |
---|
249 | #ifdef __cplusplus |
---|
250 | } |
---|
251 | #endif /* __cplusplus */ |
---|
252 | |
---|
253 | #endif /* __libconfig_h */ |
---|