libt3config
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
config.h
1 /* Copyright (C) 2011-2012 G.P. Halkes
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License version 3, as
4  published by the Free Software Foundation.
5 
6  This program is distributed in the hope that it will be useful,
7  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  GNU General Public License for more details.
10 
11  You should have received a copy of the GNU General Public License
12  along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14 
15 #ifndef T3_CONFIG_H
16 #define T3_CONFIG_H
17 
18 #include <stdio.h>
19 #include <limits.h>
20 #include <t3config/config_api.h>
21 #include <t3config/config_errors.h>
22 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
52 #define T3_CONFIG_VERSION 0
53 
55 typedef enum {
65 
69 typedef struct t3_config_t t3_config_t;
70 
75 
77 typedef struct {
78  int flags;
80  union {
82  struct {
83  const char **path;
84  int flags;
85  } dflt;
87  struct {
88  FILE *(*open)(const char *name, void *data);
89  void *data;
90  } user;
91  } include_callback;
93 
97 #define T3_CONFIG_VERBOSE_ERROR (1<<0)
98 
101 #define T3_CONFIG_INCLUDE_DFLT (1<<1)
102 
105 #define T3_CONFIG_INCLUDE_USER (1<<2)
106 
107 #define T3_CONFIG_ERROR_FILE_NAME (1<<3)
108 
115 typedef struct {
116  int error;
118  char *extra;
119  char *file_name;
121 
125 #define T3_ERR_OUT_OF_RANGE (-80)
126 
127 #define T3_ERR_PARSE_ERROR (-79)
128 
129 #define T3_ERR_DUPLICATE_KEY (-78)
130 
132 #define T3_ERR_INVALID_CONSTRAINT (-77)
133 
134 #define T3_ERR_INVALID_KEY_TYPE (-76)
135 
136 #define T3_ERR_INVALID_KEY (-75)
137 
138 #define T3_ERR_CONSTRAINT_VIOLATION (-74)
139 
140 #define T3_ERR_RECURSIVE_TYPE (-73)
141 
142 #define T3_ERR_RECURSIVE_INCLUDE (-72)
143 
145 #if INT_MAX < 2147483647
146 typedef long t3_config_int_t;
147 #define T3_CONFIG_INT_MAX LONG_MAX
148 #define T3_CONFIG_INT_MIN LONG_MIN
149 #define T3_CONFIG_INT_PRI "l"
150 #else
151 typedef int t3_config_int_t;
152 #define T3_CONFIG_INT_MAX INT_MAX
153 #define T3_CONFIG_INT_MIN INT_MIN
154 #define T3_CONFIG_INT_PRI ""
155 #endif
156 
161 T3_CONFIG_API t3_config_t *t3_config_new(void);
168 T3_CONFIG_API t3_config_t *t3_config_read_file(FILE *file, t3_config_error_t *error, const t3_config_opts_t *opts);
176 T3_CONFIG_API t3_config_t *t3_config_read_buffer(const char *buffer, size_t size, t3_config_error_t *error, const t3_config_opts_t *opts);
182 T3_CONFIG_API int t3_config_write_file(t3_config_t *config, FILE *file);
188 T3_CONFIG_API void t3_config_delete(t3_config_t *config);
189 
191 T3_CONFIG_API t3_config_t *t3_config_unlink(t3_config_t *config, const char *name);
196 T3_CONFIG_API void t3_config_erase(t3_config_t *config, const char *name);
199 T3_CONFIG_API void t3_config_erase_from_list(t3_config_t *list, t3_config_t *item);
200 
213 T3_CONFIG_API int t3_config_add_bool(t3_config_t *config, const char *name, t3_bool value);
217 T3_CONFIG_API int t3_config_add_int(t3_config_t *config, const char *name, t3_config_int_t value);
221 T3_CONFIG_API int t3_config_add_number(t3_config_t *config, const char *name, double value);
227 T3_CONFIG_API int t3_config_add_string(t3_config_t *config, const char *name, const char *value);
238 T3_CONFIG_API t3_config_t *t3_config_add_list(t3_config_t *config, const char *name, int *error);
242 T3_CONFIG_API t3_config_t *t3_config_add_plist(t3_config_t *config, const char *name, int *error);
246 T3_CONFIG_API t3_config_t *t3_config_add_section(t3_config_t *config, const char *name, int *error);
254 T3_CONFIG_API int t3_config_add_existing(t3_config_t *config, const char *name, t3_config_t *value);
256 T3_CONFIG_API int t3_config_set_list_type(t3_config_t *config, t3_config_type_t type);
257 
266 T3_CONFIG_API t3_config_t *t3_config_get(const t3_config_t *config, const char *name);
270 T3_CONFIG_API t3_config_type_t t3_config_get_type(const t3_config_t *config);
272 T3_CONFIG_API t3_bool t3_config_is_list(const t3_config_t *config);
277 T3_CONFIG_API const char *t3_config_get_name(const t3_config_t *config);
279 T3_CONFIG_API int t3_config_get_line(const t3_config_t *config);
280 
284 T3_CONFIG_API t3_bool t3_config_get_bool(const t3_config_t *config);
288 T3_CONFIG_API t3_config_int_t t3_config_get_int(const t3_config_t *config);
292 T3_CONFIG_API double t3_config_get_number(const t3_config_t *config);
296 T3_CONFIG_API const char *t3_config_get_string(const t3_config_t *config);
297 
301 T3_CONFIG_API t3_bool t3_config_get_bool_dflt(const t3_config_t *config, t3_bool dflt);
305 T3_CONFIG_API t3_config_int_t t3_config_get_int_dflt(const t3_config_t *config, t3_config_int_t dflt);
309 T3_CONFIG_API double t3_config_get_number_dflt(const t3_config_t *config, double dflt);
313 T3_CONFIG_API const char *t3_config_get_string_dflt(const t3_config_t *config, const char *dflt);
314 
320 T3_CONFIG_API char *t3_config_take_string(t3_config_t *config);
336 T3_CONFIG_API t3_config_t *t3_config_get_next(const t3_config_t *config);
340 T3_CONFIG_API int t3_config_get_length(const t3_config_t *config);
341 
354 T3_CONFIG_API t3_config_t *t3_config_find(const t3_config_t *config,
355  t3_bool (*predicate)(const t3_config_t *, const void *), const void *data, t3_config_t *start_from);
356 
365 T3_CONFIG_API long t3_config_get_version(void);
366 
371 T3_CONFIG_API const char *t3_config_strerror(int error);
372 
379 T3_CONFIG_API t3_config_schema_t *t3_config_read_schema_file(FILE *file, t3_config_error_t *error, const t3_config_opts_t *opts);
387 T3_CONFIG_API t3_config_schema_t *t3_config_read_schema_buffer(const char *buffer, size_t size,
388  t3_config_error_t *error, const t3_config_opts_t *opts);
398 T3_CONFIG_API t3_bool t3_config_validate(t3_config_t *config, const t3_config_schema_t *schema,
399  t3_config_error_t *error, int flags);
401 T3_CONFIG_API void t3_config_delete_schema(t3_config_schema_t *schema);
402 
406 #define T3_CONFIG_SPLIT_PATH (1<<0)
407 
412 #define T3_CONFIG_CLEAN_NAME (1<<1)
413 
423 T3_CONFIG_API FILE *t3_config_open_from_path(const char **path, const char *name, int flags);
424 
426 T3_CONFIG_API int t3_config_get_line_number(const t3_config_t *config);
432 T3_CONFIG_API const char *t3_config_get_file_name(const t3_config_t *config);
433 
435 typedef enum {
436  T3_CONFIG_XDG_CONFIG_HOME,
437  T3_CONFIG_XDG_DATA_HOME,
438  T3_CONFIG_XDG_CACHE_HOME,
439  T3_CONFIG_XDG_RUNTIME_DIR
441 
446 
448 T3_CONFIG_API t3_bool t3_config_xdg_supported(void);
449 
460 T3_CONFIG_API char *t3_config_xdg_get_path(t3_config_xdg_dirs_t xdg_dir, const char *program_dir, size_t file_name_len);
461 
473 T3_CONFIG_API FILE *t3_config_xdg_open_read(t3_config_xdg_dirs_t xdg_dir, const char *program_dir, const char *file_name);
474 
491 T3_CONFIG_API t3_config_write_file_t *t3_config_xdg_open_write(t3_config_xdg_dirs_t xdg_dir, const char *program_dir,
492  const char *file_name);
497 T3_CONFIG_API FILE *t3_config_xdg_get_file(t3_config_write_file_t *file);
513 T3_CONFIG_API t3_bool t3_config_xdg_close_write(t3_config_write_file_t *file, t3_bool cancel_rename, t3_bool force);
514 
515 
525 T3_CONFIG_API t3_config_write_file_t *t3_config_open_write(const char *file_name);
527 T3_CONFIG_API FILE *t3_config_get_write_file(t3_config_write_file_t *file);
541 T3_CONFIG_API t3_bool t3_config_close_write(t3_config_write_file_t *file, t3_bool cancel_rename, t3_bool force);
542 
543 #ifdef __cplusplus
544 } /* extern "C" */
545 #endif
546 
547 #endif