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 | #define LIBCONFIG_API |
---|
31 | |
---|
32 | #include <stdio.h> |
---|
33 | |
---|
34 | #define CONFIG_TYPE_NONE 0 |
---|
35 | #define CONFIG_TYPE_GROUP 1 |
---|
36 | #define CONFIG_TYPE_INT 2 |
---|
37 | #define CONFIG_TYPE_INT64 3 |
---|
38 | #define CONFIG_TYPE_FLOAT 4 |
---|
39 | #define CONFIG_TYPE_STRING 5 |
---|
40 | #define CONFIG_TYPE_BOOL 6 |
---|
41 | #define CONFIG_TYPE_ARRAY 7 |
---|
42 | #define CONFIG_TYPE_LIST 8 |
---|
43 | |
---|
44 | #define CONFIG_FORMAT_DEFAULT 0 |
---|
45 | #define CONFIG_FORMAT_HEX 1 |
---|
46 | |
---|
47 | #define CONFIG_OPTION_AUTOCONVERT 0x01 |
---|
48 | |
---|
49 | #define CONFIG_TRUE (1) |
---|
50 | #define CONFIG_FALSE (0) |
---|
51 | |
---|
52 | typedef union config_value_t |
---|
53 | { |
---|
54 | long ival; |
---|
55 | long long llval; |
---|
56 | double fval; |
---|
57 | char *sval; |
---|
58 | struct config_list_t *list; |
---|
59 | } config_value_t; |
---|
60 | |
---|
61 | typedef struct config_setting_t |
---|
62 | { |
---|
63 | char *name; |
---|
64 | short type; |
---|
65 | short format; |
---|
66 | config_value_t value; |
---|
67 | struct config_setting_t *parent; |
---|
68 | struct config_t *config; |
---|
69 | void *hook; |
---|
70 | unsigned int line; |
---|
71 | } config_setting_t; |
---|
72 | |
---|
73 | typedef struct config_list_t |
---|
74 | { |
---|
75 | unsigned int length; |
---|
76 | unsigned int capacity; |
---|
77 | config_setting_t **elements; |
---|
78 | } config_list_t; |
---|
79 | |
---|
80 | typedef struct config_t |
---|
81 | { |
---|
82 | config_setting_t *root; |
---|
83 | void (*destructor)(void *); |
---|
84 | int flags; |
---|
85 | const char *error_text; |
---|
86 | int error_line; |
---|
87 | } config_t; |
---|
88 | |
---|
89 | extern LIBCONFIG_API int config_read(config_t *config, FILE *stream); |
---|
90 | extern LIBCONFIG_API void config_write(const config_t *config, FILE *stream); |
---|
91 | |
---|
92 | extern LIBCONFIG_API void config_set_auto_convert(config_t *config, int flag); |
---|
93 | extern LIBCONFIG_API int config_get_auto_convert(const config_t *config); |
---|
94 | |
---|
95 | extern LIBCONFIG_API int config_read_file(config_t *config, |
---|
96 | const char *filename); |
---|
97 | extern LIBCONFIG_API int config_write_file(config_t *config, |
---|
98 | const char *filename); |
---|
99 | |
---|
100 | extern LIBCONFIG_API void config_set_destructor(config_t *config, |
---|
101 | void (*destructor)(void *)); |
---|
102 | |
---|
103 | extern LIBCONFIG_API void config_init(config_t *config); |
---|
104 | extern LIBCONFIG_API void config_destroy(config_t *config); |
---|
105 | |
---|
106 | extern LIBCONFIG_API long config_setting_get_int( |
---|
107 | const config_setting_t *setting); |
---|
108 | extern LIBCONFIG_API long long config_setting_get_int64( |
---|
109 | const config_setting_t *setting); |
---|
110 | extern LIBCONFIG_API double config_setting_get_float( |
---|
111 | const config_setting_t *setting); |
---|
112 | extern LIBCONFIG_API int config_setting_get_bool( |
---|
113 | const config_setting_t *setting); |
---|
114 | extern LIBCONFIG_API const char *config_setting_get_string( |
---|
115 | const config_setting_t *setting); |
---|
116 | |
---|
117 | extern LIBCONFIG_API int config_setting_set_int(config_setting_t *setting, |
---|
118 | long value); |
---|
119 | extern LIBCONFIG_API int config_setting_set_int64(config_setting_t *setting, |
---|
120 | long long value); |
---|
121 | extern LIBCONFIG_API int config_setting_set_float(config_setting_t *setting, |
---|
122 | double value); |
---|
123 | extern LIBCONFIG_API int config_setting_set_bool(config_setting_t *setting, |
---|
124 | int value); |
---|
125 | extern LIBCONFIG_API int config_setting_set_string(config_setting_t *setting, |
---|
126 | const char *value); |
---|
127 | |
---|
128 | extern LIBCONFIG_API int config_setting_set_format(config_setting_t *setting, |
---|
129 | short format); |
---|
130 | extern LIBCONFIG_API short config_setting_get_format(config_setting_t *setting); |
---|
131 | |
---|
132 | extern LIBCONFIG_API long config_setting_get_int_elem( |
---|
133 | const config_setting_t *setting, int idx); |
---|
134 | extern LIBCONFIG_API long long config_setting_get_int64_elem( |
---|
135 | const config_setting_t *setting, int idx); |
---|
136 | extern LIBCONFIG_API double config_setting_get_float_elem( |
---|
137 | const config_setting_t *setting, int idx); |
---|
138 | extern LIBCONFIG_API int config_setting_get_bool_elem( |
---|
139 | const config_setting_t *setting, int idx); |
---|
140 | extern LIBCONFIG_API const char *config_setting_get_string_elem( |
---|
141 | const config_setting_t *setting, int idx); |
---|
142 | |
---|
143 | extern LIBCONFIG_API config_setting_t *config_setting_set_int_elem( |
---|
144 | config_setting_t *setting, int idx, long value); |
---|
145 | extern LIBCONFIG_API config_setting_t *config_setting_set_int64_elem( |
---|
146 | config_setting_t *setting, int idx, long long value); |
---|
147 | extern LIBCONFIG_API config_setting_t *config_setting_set_float_elem( |
---|
148 | config_setting_t *setting, int idx, double value); |
---|
149 | extern LIBCONFIG_API config_setting_t *config_setting_set_bool_elem( |
---|
150 | config_setting_t *setting, int idx, int value); |
---|
151 | extern LIBCONFIG_API config_setting_t *config_setting_set_string_elem( |
---|
152 | config_setting_t *setting, int idx, const char *value); |
---|
153 | |
---|
154 | #define /* int */ config_setting_type(/* const config_setting_t * */ S) \ |
---|
155 | ((S)->type) |
---|
156 | |
---|
157 | #define /* int */ config_setting_is_group(/* const config_setting_t * */ S) \ |
---|
158 | ((S)->type == CONFIG_TYPE_GROUP) |
---|
159 | #define /* int */ config_setting_is_array(/* const config_setting_t * */ S) \ |
---|
160 | ((S)->type == CONFIG_TYPE_ARRAY) |
---|
161 | #define /* int */ config_setting_is_list(/* const config_setting_t * */ S) \ |
---|
162 | ((S)->type == CONFIG_TYPE_LIST) |
---|
163 | |
---|
164 | #define /* int */ config_setting_is_aggregate( \ |
---|
165 | /* const config_setting_t * */ S) \ |
---|
166 | (((S)->type == CONFIG_TYPE_GROUP) || ((S)->type == CONFIG_TYPE_LIST) \ |
---|
167 | || ((S)->type == CONFIG_TYPE_ARRAY)) |
---|
168 | |
---|
169 | #define /* int */ config_setting_is_number(/* const config_setting_t * */ S) \ |
---|
170 | (((S)->type == CONFIG_TYPE_INT) \ |
---|
171 | || ((S)->type == CONFIG_TYPE_INT64) \ |
---|
172 | || ((S)->type == CONFIG_TYPE_FLOAT)) |
---|
173 | |
---|
174 | #define /* int */ config_setting_is_scalar(/* const config_setting_t * */ S) \ |
---|
175 | (((S)->type == CONFIG_TYPE_BOOL) || ((S)->type == CONFIG_TYPE_STRING) \ |
---|
176 | || config_setting_is_number(S)) |
---|
177 | |
---|
178 | #define /* const char * */ config_setting_name( \ |
---|
179 | /* const config_setting_t * */ S) \ |
---|
180 | ((S)->name) |
---|
181 | |
---|
182 | #define /* config_setting_t * */ config_setting_parent( \ |
---|
183 | /* const config_setting_t * */ S) \ |
---|
184 | ((S)->parent) |
---|
185 | |
---|
186 | #define /* int */ config_setting_is_root( \ |
---|
187 | /* const config_setting_t * */ S) \ |
---|
188 | ((S)->parent ? CONFIG_FALSE : CONFIG_TRUE) |
---|
189 | |
---|
190 | extern LIBCONFIG_API int config_setting_index(const config_setting_t *setting); |
---|
191 | |
---|
192 | extern LIBCONFIG_API int config_setting_length( |
---|
193 | const config_setting_t *setting); |
---|
194 | extern LIBCONFIG_API config_setting_t *config_setting_get_elem( |
---|
195 | const config_setting_t *setting, unsigned int idx); |
---|
196 | |
---|
197 | extern LIBCONFIG_API config_setting_t *config_setting_get_member( |
---|
198 | const config_setting_t *setting, const char *name); |
---|
199 | |
---|
200 | extern LIBCONFIG_API config_setting_t *config_setting_add( |
---|
201 | config_setting_t *parent, const char *name, int type); |
---|
202 | extern LIBCONFIG_API int config_setting_remove(config_setting_t *parent, |
---|
203 | const char *name); |
---|
204 | extern LIBCONFIG_API int config_setting_remove_elem(config_setting_t *parent, |
---|
205 | unsigned int idx); |
---|
206 | extern LIBCONFIG_API void config_setting_set_hook(config_setting_t *setting, |
---|
207 | void *hook); |
---|
208 | |
---|
209 | #define config_setting_get_hook(S) ((S)->hook) |
---|
210 | |
---|
211 | extern LIBCONFIG_API config_setting_t *config_lookup(const config_t *config, |
---|
212 | const char *path); |
---|
213 | |
---|
214 | extern LIBCONFIG_API long config_lookup_int(const config_t *config, |
---|
215 | const char *path); |
---|
216 | extern LIBCONFIG_API long long config_lookup_int64(const config_t *config, |
---|
217 | const char *path); |
---|
218 | extern LIBCONFIG_API double config_lookup_float(const config_t *config, |
---|
219 | const char *path); |
---|
220 | extern LIBCONFIG_API int config_lookup_bool(const config_t *config, |
---|
221 | const char *path); |
---|
222 | extern LIBCONFIG_API const char *config_lookup_string(const config_t *config, |
---|
223 | const char *path); |
---|
224 | |
---|
225 | #define /* config_setting_t * */ config_root_setting( \ |
---|
226 | /* const config_t * */ C) \ |
---|
227 | ((C)->root) |
---|
228 | |
---|
229 | #define /* unsigned short */ config_setting_source_line( \ |
---|
230 | /* const config_t */ C) \ |
---|
231 | ((C)->line) |
---|
232 | |
---|
233 | #define /* const char * */ config_error_text(/* const config_t * */ C) \ |
---|
234 | ((C)->error_text) |
---|
235 | |
---|
236 | #define /* int */ config_error_line(/* const config_t * */ C) \ |
---|
237 | ((C)->error_line) |
---|
238 | |
---|
239 | #ifdef __cplusplus |
---|
240 | } |
---|
241 | #endif /* __cplusplus */ |
---|
242 | |
---|
243 | #endif /* __libconfig_h */ |
---|