2 * Copyright (C) 2010 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * @defgroup settings settings
25 typedef struct settings_t settings_t
;
28 #include "collections/enumerator.h"
31 * Convert a string value returned by a key/value enumerator to a boolean.
33 * @see settings_t.create_key_value_enumerator()
34 * @see settings_t.get_bool()
35 * @param value the string value
36 * @param def the default value, if value is NULL or invalid
38 bool settings_value_as_bool(char *value
, bool def
);
41 * Convert a string value returned by a key/value enumerator to an integer.
43 * @see settings_t.create_key_value_enumerator()
44 * @see settings_t.get_int()
45 * @param value the string value
46 * @param def the default value, if value is NULL or invalid
48 int settings_value_as_int(char *value
, int def
);
51 * Convert a string value returned by a key/value enumerator to a double.
53 * @see settings_t.create_key_value_enumerator()
54 * @see settings_t.get_double()
55 * @param value the string value
56 * @param def the default value, if value is NULL or invalid
58 double settings_value_as_double(char *value
, double def
);
61 * Convert a string value returned by a key/value enumerator to a time value.
63 * @see settings_t.create_key_value_enumerator()
64 * @see settings_t.get_time()
65 * @param value the string value
66 * @param def the default value, if value is NULL or invalid
68 u_int32_t
settings_value_as_time(char *value
, u_int32_t def
);
71 * Generic configuration options read from a config file.
73 * The syntax is quite simple:
75 * settings := (section|keyvalue)*
76 * section := name { settings }
77 * keyvalue := key = value\n
93 * The values are accessed using the get() functions using dotted keys, e.g.
94 * section-one.subsection.othervalue
96 * Currently only a limited set of printf format specifiers are supported
97 * (namely %s, %d and %N, see implementation for details).
99 * \section includes Including other files
100 * Other files can be included, using the include statement e.g.
102 * include /somepath/subconfig.conf
104 * Shell patterns like *.conf are possible.
106 * If the path is relative, the directory of the file containing the include
107 * statement is used as base.
109 * Sections loaded from included files extend previously loaded sections,
110 * already existing values are replaced.
112 * All settings included from files are added relative to the section the
113 * include statement is in.
115 * The following files result in the same final config as above:
120 somevalue = before include
147 * Get a settings value as a string.
149 * @param key key including sections, printf style format
150 * @param def value returned if key not found
151 * @param ... argument list for key
152 * @return value pointing to internal string
154 char* (*get_str
)(settings_t
*this, char *key
, char *def
, ...);
157 * Get a boolean yes|no, true|false value.
159 * @param key key including sections, printf style format
160 * @param def value returned if key not found
161 * @param ... argument list for key
162 * @return value of the key
164 bool (*get_bool
)(settings_t
*this, char *key
, bool def
, ...);
167 * Get an integer value.
169 * @param key key including sections, printf style format
170 * @param def value returned if key not found
171 * @param ... argument list for key
172 * @return value of the key
174 int (*get_int
)(settings_t
*this, char *key
, int def
, ...);
177 * Get an double value.
179 * @param key key including sections, printf style format
180 * @param def value returned if key not found
181 * @param ... argument list for key
182 * @return value of the key
184 double (*get_double
)(settings_t
*this, char *key
, double def
, ...);
189 * @param key key including sections, printf style format
190 * @param def value returned if key not found
191 * @param ... argument list for key
192 * @return value of the key (in seconds)
194 u_int32_t (*get_time
)(settings_t
*this, char *key
, u_int32_t def
, ...);
197 * Set a string value.
199 * @param key key including sections, printf style format
200 * @param value value to set (gets cloned)
201 * @param ... argument list for key
203 void (*set_str
)(settings_t
*this, char *key
, char *value
, ...);
206 * Set a boolean value.
208 * @param key key including sections, printf style format
209 * @param value value to set
210 * @param ... argument list for key
212 void (*set_bool
)(settings_t
*this, char *key
, bool value
, ...);
215 * Set an integer value.
217 * @param key key including sections, printf style format
218 * @param value value to set
219 * @param ... argument list for key
221 void (*set_int
)(settings_t
*this, char *key
, int value
, ...);
224 * Set an double value.
226 * @param key key including sections, printf style format
227 * @param value value to set
228 * @param ... argument list for key
230 void (*set_double
)(settings_t
*this, char *key
, double value
, ...);
235 * @param key key including sections, printf style format
236 * @param def value to set
237 * @param ... argument list for key
239 void (*set_time
)(settings_t
*this, char *key
, u_int32_t value
, ...);
242 * Create an enumerator over subsection names of a section.
244 * @param section section including parents, printf style format
245 * @param ... argument list for key
246 * @return enumerator over subsection names
248 enumerator_t
* (*create_section_enumerator
)(settings_t
*this,
252 * Create an enumerator over key/value pairs in a section.
254 * @param section section name to list key/value pairs of, printf style
255 * @param ... argument list for section
256 * @return enumerator over (char *key, char *value)
258 enumerator_t
* (*create_key_value_enumerator
)(settings_t
*this,
262 * Load settings from the files matching the given pattern.
264 * If merge is TRUE, existing sections are extended, existing values
265 * replaced, by those found in the loaded files. If it is FALSE, existing
266 * sections are purged before reading the new config.
268 * @note If any of the files matching the pattern fails to load, no settings
269 * are added at all. So, it's all or nothing.
271 * @param pattern file pattern
272 * @param merge TRUE to merge config with existing values
273 * @return TRUE, if settings were loaded successfully
275 bool (*load_files
)(settings_t
*this, char *pattern
, bool merge
);
278 * Load settings from the files matching the given pattern.
280 * If merge is TRUE, existing sections are extended, existing values
281 * replaced, by those found in the loaded files. If it is FALSE, existing
282 * sections are purged before reading the new config.
284 * All settings are loaded relative to the given section. The section is
285 * created, if it does not yet exist.
287 * @note If any of the files matching the pattern fails to load, no settings
288 * are added at all. So, it's all or nothing.
290 * @param pattern file pattern
291 * @param merge TRUE to merge config with existing values
292 * @param section section name of parent section, printf style
293 * @param ... argument list for section
294 * @return TRUE, if settings were loaded successfully
296 bool (*load_files_section
)(settings_t
*this, char *pattern
, bool merge
,
300 * Destroy a settings instance.
302 void (*destroy
)(settings_t
*this);
306 * Load settings from a file.
308 * @param file file to read settings from, NULL for default
309 * @return settings object
311 settings_t
*settings_create(char *file
);
313 #endif /** SETTINGS_H_ @}*/