34#ifndef COMPILE_TIME_ASSERT
41#define COMPILE_TIME_ASSERT(name, cond) typedef int assert_ ## name[(cond) * 2 - 1]
51#define COUNTOF(a) (sizeof(a) / sizeof(*(a)))
86#define CONFIG_STRING_LONG_TAG(s) ((s).short_buf[COUNTOF((s).short_buf) - 1])
95static inline const char *config_string_to_cstr(
const ConfigString *
string)
97 assert(
string != NULL);
98 char is_long = CONFIG_STRING_LONG_TAG(*
string);
245static inline ConfigSection *config_add_section(
Config *config,
const char *type,
const char *name)
247 assert(type != NULL);
342static inline void config_set_string_option(
ConfigSection *section,
const char *key,
const char *value)
505 assert(value != NULL);
532static inline bool config_is_value_list(
const ConfigOption *option)
534 assert(option != NULL);
536 config_string_to_cstr(&option->
v.
value) == NULL;
546static inline unsigned int config_get_value_count(
const ConfigOption *option)
548 assert(option != NULL);
550 (config_string_to_cstr(&option->
v.
value) == NULL ? 0 : 1) :
551 option->values_count;
560#define foreach_option_value(string_value, option) \
561 for (ConfigOption *_option = (option); _option != NULL; _option = NULL) \
562 for (ConfigString *_values_begin = _option->values_count == 0 ? &_option->v.value : &_option->v.values[0], \
563 *_values_end = _option->values_count == 0 ? _values_begin + 1 : &_option->v.values[_option->values_count], \
564 *_value = _values_begin; _value < _values_end; ++_value) \
565 for (const char *(string_value) = config_string_to_cstr(_value); (string_value) != NULL; (string_value) = NULL)
574#define foreach_option_i_value(i, string_value, option) \
575 for (unsigned int (i) = 0; (i) == 0; (i) = ~0) \
576 for (ConfigOption *_option = (option); _option != NULL; _option = NULL) \
577 for (ConfigString *_values_begin = _option->values_count == 0 ? &_option->v.value : &_option->v.values[0], \
578 *_values_end = _option->values_count == 0 ? _values_begin + 1 : &_option->v.values[_option->values_count], \
579 *_value = _values_begin; _value < _values_end; ++_value, (i) = _value - _values_begin) \
580 for (const char *(string_value) = config_string_to_cstr(_value); (string_value) != NULL; (string_value) = NULL)
585#define foreach_remove_option_value() \
587 extern void config_oom(void); \
588 unsigned int _value_i = _value - _values_begin; \
589 if (config_remove_value(_option, _value_i) == NULL) \
591 _values_begin = _option->values_count == 0 ? &_option->v.value : &_option->v.values[0]; \
592 _values_end = _option->values_count == 0 ? _values_begin + 1 : &_option->v.values[_option->values_count]; \
593 _value = _values_begin + _value_i - 1; \
ConfigSection * config_add_section_len(Config *config, const char *type, size_t type_len, const char *name, size_t name_len)
Definition config_file.c:149
int config_get_or_set_int_option(ConfigSection *section, const char *key, int value)
Get an integer value of an 'item' option by key, setting the option if it was invalid or creating the...
Definition config_file.c:439
bool config_parse(Config *config, FILE *file)
Parse a configuration from a file.
Definition config_file.c:768
#define COMPILE_TIME_ASSERT(name, cond)
Cause compile error if compile-time computable condition fails.
Definition config_file.h:41
ConfigSection * config_find_sections(Config *config, const char *type, ConfigSection **save)
Iterate sections by type.
Definition config_file.c:168
unsigned int config_get_or_set_uint_option(ConfigSection *section, const char *key, unsigned int value)
Get an unsigned integer value of an 'item' option by key, setting the option if it was invalid or cre...
Definition config_file.c:478
ConfigSection * config_find_section(Config *config, const char *type, const char *name)
Find a section by type and name.
Definition config_file.c:189
void config_init(Config *config)
Initialize a configuration.
Definition config_file.c:98
ConfigOption * config_set_option_len(ConfigSection *section, const char *key, size_t key_len, const char *value, size_t value_len)
Definition config_file.c:290
ConfigOption * config_set_value_len(ConfigOption *option, const char *value, size_t value_len)
Definition config_file.c:487
void config_set_bool_option(ConfigSection *section, const char *key, bool value, ConfigBoolStyle style)
Set a boolean value of an 'item' option by key, creating the option if necessary.
Definition config_file.c:369
void config_deinit(Config *config)
Release any memory allocated inside a configuration.
Definition config_file.c:106
const char * config_get_value(const ConfigOption *option)
Get the value of an 'item' option.
Definition config_file.c:569
ConfigOption * config_get_or_set_option_len(ConfigSection *section, const char *key, size_t key_len, const char *value, size_t value_len)
Definition config_file.c:316
void config_set_uint_option(ConfigSection *section, const char *key, unsigned int value)
Set an unsigned integer value of an 'item' option by key, creating the option if necessary.
Definition config_file.c:446
ConfigSection * config_find_or_add_section(Config *config, const char *type, const char *name)
Find a section by type and name, creating the section if it did not exist.
Definition config_file.c:209
ConfigBoolStyle
The styles of boolean values.
Definition config_file.h:373
void config_write(const Config *config, FILE *file)
Write a configuration to a file.
Definition config_file.c:987
bool config_get_or_set_bool_option(ConfigSection *section, const char *key, bool value, ConfigBoolStyle style)
Get a boolean value of an 'item' option by key, setting the option if it was invalid or creating the ...
Definition config_file.c:400
void config_set_string_option_len(ConfigSection *section, const char *key, size_t key_len, const char *value, size_t value_len)
Definition config_file.c:329
ConfigOption * config_add_value_len(ConfigOption *option, const char *value, size_t value_len)
Definition config_file.c:498
ConfigOption * config_get_option(const ConfigSection *section, const char *key)
Get an option by key.
Definition config_file.c:303
#define COUNTOF(a)
Calculate the number of elements in a fixed-length array.
Definition config_file.h:51
ConfigOption * config_remove_value(ConfigOption *option, unsigned int i)
Remove a value from a 'list' option.
Definition config_file.c:531
const char * config_get_or_set_string_option(ConfigSection *section, const char *key, const char *value)
Get a string value of an 'item' option by key, setting the option if it was invalid or creating the o...
Definition config_file.c:354
bool config_get_int_option(const ConfigSection *section, const char *key, int *out_value)
Get an integer value of an 'item' option by key.
Definition config_file.c:418
bool config_get_bool_option(const ConfigSection *section, const char *key, bool *out_value)
Get a boolean value of an 'item' option by key.
Definition config_file.c:375
bool config_get_string_option(const ConfigSection *section, const char *key, const char **out_value)
Get a string value of an 'item' option by key.
Definition config_file.c:335
bool config_get_uint_option(const ConfigSection *section, const char *key, unsigned int *out_value)
Get an unsigned integer value of an 'item' option by key.
Definition config_file.c:457
void config_set_int_option(ConfigSection *section, const char *key, int value)
Set an integer value of an 'item' option by key, creating the option if necessary.
Definition config_file.c:407
A configuration consisting of sections.
Definition config_file.h:182
unsigned int sections_count
The number of sections in the configuration.
Definition config_file.h:186
ConfigSection * sections
The sections in the configuration.
Definition config_file.h:193
An option consisting of one (an item) or many (a list) values.
Definition config_file.h:110
unsigned int values_count
The number of values in the option if it is a 'list' option.
Definition config_file.h:123
ConfigString * values
The values of a non-empty 'list' option.
Definition config_file.h:144
union ConfigOption::@4 v
The value or values.
ConfigString key
The key of the option.
Definition config_file.h:114
ConfigString value
The value of an 'item' option or an empty 'list' option.
Definition config_file.h:139
A section consisting of options.
Definition config_file.h:152
unsigned int options_count
The number of options in the section.
Definition config_file.h:168
ConfigOption * options
The options in the section.
Definition config_file.h:175
ConfigString type
The type of the section.
Definition config_file.h:156
ConfigString name
The optional name of the section.
Definition config_file.h:163
A short-string-optimizing string type.
Definition config_file.h:67
char * long_buf
The buffer for long strings.
Definition config_file.h:78