• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------------
2    libconfig - A library for processing structured configuration files
3    Copyright (C) 2005-2018  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 #define LIBCONFIG_VER_MAJOR    1
43 #define LIBCONFIG_VER_MINOR    7
44 #define LIBCONFIG_VER_REVISION 0
45 
46 #include <stdio.h>
47 
48 #define CONFIG_TYPE_NONE    0
49 #define CONFIG_TYPE_GROUP   1
50 #define CONFIG_TYPE_INT     2
51 #define CONFIG_TYPE_INT64   3
52 #define CONFIG_TYPE_FLOAT   4
53 #define CONFIG_TYPE_STRING  5
54 #define CONFIG_TYPE_BOOL    6
55 #define CONFIG_TYPE_ARRAY   7
56 #define CONFIG_TYPE_LIST    8
57 
58 #define CONFIG_FORMAT_DEFAULT  0
59 #define CONFIG_FORMAT_HEX      1
60 
61 #define CONFIG_OPTION_AUTOCONVERT                     0x01
62 #define CONFIG_OPTION_SEMICOLON_SEPARATORS            0x02
63 #define CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS     0x04
64 #define CONFIG_OPTION_COLON_ASSIGNMENT_FOR_NON_GROUPS 0x08
65 #define CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE     0x10
66 #define CONFIG_OPTION_ALLOW_SCIENTIFIC_NOTATION       0x20
67 #define CONFIG_OPTION_FSYNC                           0x40
68 #define CONFIG_OPTION_ALLOW_OVERRIDES                 0x80
69 
70 #define CONFIG_TRUE  (1)
71 #define CONFIG_FALSE (0)
72 
73 typedef union config_value_t
74 {
75   int ival;
76   long long llval;
77   double fval;
78   char *sval;
79   struct config_list_t *list;
80 } config_value_t;
81 
82 typedef struct config_setting_t
83 {
84   char *name;
85   short type;
86   short format;
87   config_value_t value;
88   struct config_setting_t *parent;
89   struct config_t *config;
90   void *hook;
91   unsigned int line;
92   const char *file;
93 } config_setting_t;
94 
95 typedef enum
96 {
97   CONFIG_ERR_NONE = 0,
98   CONFIG_ERR_FILE_IO = 1,
99   CONFIG_ERR_PARSE = 2
100 } config_error_t;
101 
102 typedef struct config_list_t
103 {
104   unsigned int length;
105   config_setting_t **elements;
106 } config_list_t;
107 
108 typedef const char ** (*config_include_fn_t)(struct config_t *,
109                                              const char *,
110                                              const char *,
111                                              const char **);
112 
113 typedef struct config_t
114 {
115   config_setting_t *root;
116   void (*destructor)(void *);
117   int options;
118   unsigned short tab_width;
119   unsigned short float_precision;
120   unsigned short default_format;
121   const char *include_dir;
122   config_include_fn_t include_fn;
123   const char *error_text;
124   const char *error_file;
125   int error_line;
126   config_error_t error_type;
127   const char **filenames;
128   void *hook;
129 } config_t;
130 
131 extern LIBCONFIG_API int config_read(config_t *config, FILE *stream);
132 extern LIBCONFIG_API void config_write(const config_t *config, FILE *stream);
133 
134 extern LIBCONFIG_API void config_set_default_format(config_t *config,
135                                                     short format);
136 
137 extern LIBCONFIG_API void config_set_options(config_t *config, int options);
138 extern LIBCONFIG_API int config_get_options(const config_t *config);
139 
140 extern LIBCONFIG_API void config_set_option(config_t *config, int option,
141                                             int flag);
142 extern LIBCONFIG_API int config_get_option(const config_t *config, int option);
143 
144 extern LIBCONFIG_API int config_read_string(config_t *config, const char *str);
145 
146 extern LIBCONFIG_API int config_read_file(config_t *config,
147                                           const char *filename);
148 extern LIBCONFIG_API int config_write_file(config_t *config,
149                                            const char *filename);
150 
151 extern LIBCONFIG_API void config_set_destructor(config_t *config,
152                                                 void (*destructor)(void *));
153 extern LIBCONFIG_API void config_set_include_dir(config_t *config,
154                                                  const char *include_dir);
155 extern LIBCONFIG_API void config_set_include_func(config_t *config,
156                                                   config_include_fn_t func);
157 
158 extern LIBCONFIG_API void config_set_float_precision(config_t *config,
159                                                      unsigned short digits);
160 extern LIBCONFIG_API unsigned short config_get_float_precision(
161   const config_t *config);
162 
163 extern LIBCONFIG_API void config_set_tab_width(config_t *config,
164                                                unsigned short width);
165 extern LIBCONFIG_API unsigned short config_get_tab_width(
166   const config_t *config);
167 
168 extern LIBCONFIG_API void config_set_hook(config_t *config, void *hook);
169 
170 #define config_get_hook(C) ((C)->hook)
171 
172 extern LIBCONFIG_API void config_init(config_t *config);
173 extern LIBCONFIG_API void config_destroy(config_t *config);
174 extern LIBCONFIG_API void config_clear(config_t *config);
175 
176 extern LIBCONFIG_API int config_setting_get_int(
177   const config_setting_t *setting);
178 extern LIBCONFIG_API long long config_setting_get_int64(
179   const config_setting_t *setting);
180 extern LIBCONFIG_API double config_setting_get_float(
181   const config_setting_t *setting);
182 extern LIBCONFIG_API int config_setting_get_bool(
183   const config_setting_t *setting);
184 extern LIBCONFIG_API const char *config_setting_get_string(
185   const config_setting_t *setting);
186 
187 extern LIBCONFIG_API int config_setting_lookup_int(
188   const config_setting_t *setting, const char *name, int *value);
189 extern LIBCONFIG_API int config_setting_lookup_int64(
190   const config_setting_t *setting, const char *name, long long *value);
191 extern LIBCONFIG_API int config_setting_lookup_float(
192   const config_setting_t *setting, const char *name, double *value);
193 extern LIBCONFIG_API int config_setting_lookup_bool(
194   const config_setting_t *setting, const char *name, int *value);
195 extern LIBCONFIG_API int config_setting_lookup_string(
196   const config_setting_t *setting, const char *name, const char **value);
197 
198 extern LIBCONFIG_API int config_setting_set_int(config_setting_t *setting,
199                                                 int value);
200 extern LIBCONFIG_API int config_setting_set_int64(config_setting_t *setting,
201                                                   long long value);
202 extern LIBCONFIG_API int config_setting_set_float(config_setting_t *setting,
203                                                   double value);
204 extern LIBCONFIG_API int config_setting_set_bool(config_setting_t *setting,
205                                                  int value);
206 extern LIBCONFIG_API int config_setting_set_string(config_setting_t *setting,
207                                                    const char *value);
208 
209 extern LIBCONFIG_API int config_setting_set_format(config_setting_t *setting,
210                                                    short format);
211 extern LIBCONFIG_API short config_setting_get_format(
212   const config_setting_t *setting);
213 
214 extern LIBCONFIG_API int config_setting_get_int_elem(
215   const config_setting_t *setting, int idx);
216 extern LIBCONFIG_API long long config_setting_get_int64_elem(
217   const config_setting_t *setting, int idx);
218 extern LIBCONFIG_API double config_setting_get_float_elem(
219   const config_setting_t *setting, int idx);
220 extern LIBCONFIG_API int config_setting_get_bool_elem(
221   const config_setting_t *setting, int idx);
222 extern LIBCONFIG_API const char *config_setting_get_string_elem(
223   const config_setting_t *setting, int idx);
224 
225 extern LIBCONFIG_API config_setting_t *config_setting_set_int_elem(
226   config_setting_t *setting, int idx, int value);
227 extern LIBCONFIG_API config_setting_t *config_setting_set_int64_elem(
228   config_setting_t *setting, int idx, long long value);
229 extern LIBCONFIG_API config_setting_t *config_setting_set_float_elem(
230   config_setting_t *setting, int idx, double value);
231 extern LIBCONFIG_API config_setting_t *config_setting_set_bool_elem(
232   config_setting_t *setting, int idx, int value);
233 extern LIBCONFIG_API config_setting_t *config_setting_set_string_elem(
234   config_setting_t *setting, int idx, const char *value);
235 
236 extern LIBCONFIG_API const char **config_default_include_func(
237     config_t *config, const char *include_dir, const char *path,
238     const char **error);
239 
240 extern LIBCONFIG_API int config_setting_is_scalar(
241     const config_setting_t *setting);
242 
243 extern LIBCONFIG_API int config_setting_is_aggregate(
244     const config_setting_t *setting);
245 
246 #define /* const char * */ config_get_include_dir(/* const config_t * */ C) \
247   ((C)->include_dir)
248 
249 #define /* void */ config_set_auto_convert(/* config_t * */ C, F) \
250   config_set_option((C), CONFIG_OPTION_AUTOCONVERT, (F))
251 #define /* int */ config_get_auto_convert(/* const config_t * */ C) \
252   config_get_option((C), CONFIG_OPTION_AUTOCONVERT)
253 
254 #define /* int */ config_setting_type(/* const config_setting_t * */ S) \
255   ((S)->type)
256 
257 #define /* int */ config_setting_is_group(/* const config_setting_t * */ S) \
258   ((S)->type == CONFIG_TYPE_GROUP)
259 #define /* int */ config_setting_is_array(/* const config_setting_t * */ S) \
260   ((S)->type == CONFIG_TYPE_ARRAY)
261 #define /* int */ config_setting_is_list(/* const config_setting_t * */ S) \
262   ((S)->type == CONFIG_TYPE_LIST)
263 
264 #define /* int */ config_setting_is_number(/* const config_setting_t * */ S) \
265   (((S)->type == CONFIG_TYPE_INT)                                       \
266    || ((S)->type == CONFIG_TYPE_INT64)                                  \
267    || ((S)->type == CONFIG_TYPE_FLOAT))
268 
269 #define /* const char * */ config_setting_name( \
270   /* const config_setting_t * */ S)             \
271   ((S)->name)
272 
273 #define /* config_setting_t * */ config_setting_parent( \
274   /* const config_setting_t * */ S)                     \
275   ((S)->parent)
276 
277 #define /* int */ config_setting_is_root(       \
278   /* const config_setting_t * */ S)             \
279   ((S)->parent ? CONFIG_FALSE : CONFIG_TRUE)
280 
281 extern LIBCONFIG_API int config_setting_index(const config_setting_t *setting);
282 
283 extern LIBCONFIG_API int config_setting_length(
284   const config_setting_t *setting);
285 extern LIBCONFIG_API config_setting_t *config_setting_get_elem(
286   const config_setting_t *setting, unsigned int idx);
287 
288 extern LIBCONFIG_API config_setting_t *config_setting_get_member(
289   const config_setting_t *setting, const char *name);
290 
291 extern LIBCONFIG_API config_setting_t *config_setting_add(
292   config_setting_t *parent, const char *name, int type);
293 extern LIBCONFIG_API int config_setting_remove(config_setting_t *parent,
294                                                const char *name);
295 extern LIBCONFIG_API int config_setting_remove_elem(config_setting_t *parent,
296                                                     unsigned int idx);
297 extern LIBCONFIG_API void config_setting_set_hook(config_setting_t *setting,
298                                                   void *hook);
299 
300 #define config_setting_get_hook(S) ((S)->hook)
301 
302 extern LIBCONFIG_API config_setting_t *config_lookup(const config_t *config,
303                                                      const char *path);
304 extern LIBCONFIG_API config_setting_t *config_setting_lookup(
305   config_setting_t *setting, const char *path);
306 
307 extern LIBCONFIG_API int config_lookup_int(const config_t *config,
308                                            const char *path, int *value);
309 extern LIBCONFIG_API int config_lookup_int64(const config_t *config,
310                                              const char *path,
311                                              long long *value);
312 extern LIBCONFIG_API int config_lookup_float(const config_t *config,
313                                              const char *path, double *value);
314 extern LIBCONFIG_API int config_lookup_bool(const config_t *config,
315                                             const char *path, int *value);
316 extern LIBCONFIG_API int config_lookup_string(const config_t *config,
317                                               const char *path,
318                                               const char **value);
319 
320 #define /* config_setting_t * */ config_root_setting( \
321   /* const config_t * */ C)                           \
322   ((C)->root)
323 
324 #define  /* void */ config_set_default_format(/* config_t * */ C,       \
325                                               /* short */ F)            \
326   (C)->default_format = (F)
327 
328 #define /* short */ config_get_default_format(/* config_t * */ C)       \
329   ((C)->default_format)
330 
331 #define /* unsigned short */ config_setting_source_line(   \
332   /* const config_setting_t * */ S)                        \
333   ((S)->line)
334 
335 #define /* const char */ config_setting_source_file(    \
336   /* const config_setting_t * */ S)                     \
337   ((S)->file)
338 
339 #define /* const char * */ config_error_text(/* const config_t * */ C)  \
340   ((C)->error_text)
341 
342 #define /* const char * */ config_error_file(/* const config_t * */ C)  \
343   ((C)->error_file)
344 
345 #define /* int */ config_error_line(/* const config_t * */ C)   \
346   ((C)->error_line)
347 
348 #define /* config_error_t */ config_error_type(/* const config_t * */ C) \
349   ((C)->error_type)
350 
351 #ifdef __cplusplus
352 }
353 #endif /* __cplusplus */
354 
355 #endif /* __libconfig_h */
356