48 static struct rbnode *config_section_create(
config_t *config,
const char *name);
49 static void config_section_destroy(
struct rbnode *node);
51 static const char *config_section_set(
52 struct rbnode *node,
const char *
key,
const char *value);
54 static void config_section_foreach(
66 const char *
key,
const char *value);
67 static void config_entry_destroy(
struct rbnode *node);
72 void *ptr = malloc(
sizeof(
struct __config));
79 __config_free(
void *ptr)
85 __config_init(
struct __config *config,
int flags)
92 if (!config_section_create(config,
""))
99 __config_fini(
struct __config *config)
105 config_section_destroy(node);
114 config_t *config = __config_alloc();
117 goto error_alloc_config;
120 if (!__config_init(config, flags)) {
122 goto error_init_config;
128 __config_free(config);
138 __config_fini(config);
139 __config_free(config);
153 for (
size_t i = 0; node && i < n; node =
rbnode_next(node), i++)
154 sections[i] = node->
key;
180 for (
size_t i = 0; node && i < n; node =
rbnode_next(node), i++)
227 node = config_section_create(config, section);
229 return node ? config_section_set(node, key, value) : NULL;
240 config_section_foreach(node, func, data);
243 const char *section = node->
key;
245 if (!section || !*section)
247 config_section_foreach(node, func, data);
252 config_section_create(
config_t *config,
const char *name)
262 goto error_alloc_section;
267 node->
key = malloc(strlen(name) + 1);
270 goto error_alloc_key;
272 strcpy((
char *)node->
key, name);
288 config_section_destroy(
struct rbnode *node)
294 free((
char *)node->
key);
298 config_entry_destroy(node);
305 config_section_set(
struct rbnode *node,
const char *key,
const char *value)
315 config_entry_destroy(node);
320 node = config_entry_create(section, key, value);
325 config_section_foreach(
341 config_entry_create(
struct config_section *section,
const char *key,
352 goto error_alloc_entry;
357 node->
key = malloc(strlen(key) + 1);
360 goto error_alloc_key;
362 strcpy((
char *)node->
key, key);
364 entry->
value = malloc(strlen(value) + 1);
367 goto error_alloc_value;
369 strcpy(entry->
value, value);
377 free((
char *)node->
key);
386 config_entry_destroy(
struct rbnode *node)
391 free((
char *)node->
key);
const void * key
A pointer to the key for this node.
const char * config_set(config_t *config, const char *section, const char *key, const char *value)
Sets a key in or removes a key from a configuration struct.
const char * config_get(const config_t *config, const char *section, const char *key)
Retrieves a key from a configuration struct.
rbtree_cmp_t * cmp
A pointer to the function used to compare two keys.
struct rbnode node
The node of this entry in the tree of entries.
This header file is part of the C11 and POSIX compatibility library; it includes <string.h> and defines any missing functionality.
struct rbtree tree
The tree containing the sections.
This header file is part of the utilities library; it contains the comparison function definitions...
size_t rbtree_size(const struct rbtree *tree)
Returns the size (in number of nodes) of a red-black tree.
This header file is part of the utilities library; it contains the configuration functions.
This header file is part of the utilities library; it contains the red-black tree declarations...
void config_foreach_func_t(const char *section, const char *key, const char *value, void *data)
The type of a function called by config_foreach() for each key in a configuration struct...
size_t config_get_keys(const config_t *config, const char *section, size_t n, const char **keys)
Retrieves a list of key names from a section in a configuration struct.
An entry in a configuration section.
void set_errc(int errc)
Sets the current (thread-specific) native error code to errc.
This header file is part of the utilities library; it contains the native and platform-independent er...
#define rbtree_foreach(tree, node)
Iterates over each node in a red-black tree in ascending order.
A section in a configuration struct.
config_t * config_create(int flags)
Creates a new configuration struct with an unnamed empty root section.
This is the internal header file of the utilities library.
int get_errc(void)
Returns the last (thread-specific) native error code set by a system call or library function...
struct rbtree tree
The tree containing the entries.
void config_foreach(const config_t *config, config_foreach_func_t *func, void *data)
Invokes a function for each key in a configuration struct.
struct rbnode * rbtree_find(const struct rbtree *tree, const void *key)
Finds a node in a red-black tree.
void rbtree_remove(struct rbtree *tree, struct rbnode *node)
Removes a node from a red-black tree.
int errno2c(int errnum)
Transforms a standard C error number to a native error code.
size_t config_get_sections(const config_t *config, size_t n, const char **sections)
Retrieves a list of section names from a configuration struct.
#define structof(ptr, type, member)
Obtains the address of a structure from the address of one of its members.
struct rbnode node
The node of this section in the tree of sections.
Section and key names are case-insensitive.
char * value
The value of the entry.
void rbtree_init(struct rbtree *tree, rbtree_cmp_t *cmp)
Initializes a red-black tree.
struct rbnode * rbnode_next(const struct rbnode *node)
Returns a pointer to the next (in-order) node in a red-black tree with respect to node...
void rbtree_insert(struct rbtree *tree, struct rbnode *node)
Inserts a node into a red-black tree.
This header file is part of the C11 and POSIX compatibility library; it includes <stdlib.h> and defines any missing functionality.
struct rbnode * rbtree_first(const struct rbtree *tree)
Returns a pointer to the first (leftmost) node in a red-black tree.
A node in a red-black tree.
void config_destroy(config_t *config)
Destroys a configuration struct.