My Project
Loading...
Searching...
No Matches
Classes | Macros | Enumerations | Functions
config_file.h File Reference
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Classes

union  ConfigString
 A short-string-optimizing string type. More...
 
struct  ConfigOption
 An option consisting of one (an item) or many (a list) values. More...
 
struct  ConfigSection
 A section consisting of options. More...
 
struct  Config
 A configuration consisting of sections. More...
 

Macros

#define COMPILE_TIME_ASSERT(name, cond)   typedef int assert_ ## name[(cond) * 2 - 1]
 Cause compile error if compile-time computable condition fails.
 
#define COUNTOF(a)   (sizeof(a) / sizeof(*(a)))
 Calculate the number of elements in a fixed-length array.
 
#define foreach_option_value(string_value, option)
 Iterate over the values assigned to the option.
 
#define foreach_option_i_value(i, string_value, option)
 Iterate over the values assigned to the option.
 
#define foreach_remove_option_value()
 Remove a value from an option during iteration. Should be followed by continue.
 

Enumerations

enum  ConfigBoolStyle { ZERO_ONE = 0 , NO_YES = 1 , OFF_ON = 2 , FALSE_TRUE = 3 }
 The styles of boolean values.
 

Functions

void config_init (Config *config)
 Initialize a configuration.
 
void config_deinit (Config *config)
 Release any memory allocated inside a configuration.
 
bool config_parse (Config *config, FILE *file)
 Parse a configuration from a file.
 
void config_write (const Config *config, FILE *file)
 Write a configuration to a file.
 
ConfigSection * config_add_section_len (Config *config, const char *type, size_t type_len, const char *name, size_t name_len)
 
ConfigSection * config_find_sections (Config *config, const char *type, ConfigSection **save)
 Iterate sections by type.
 
ConfigSection * config_find_section (Config *config, const char *type, const char *name)
 Find a section by type and name.
 
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.
 
ConfigOption * config_set_option_len (ConfigSection *section, const char *key, size_t key_len, const char *value, size_t value_len)
 
ConfigOption * config_get_option (const ConfigSection *section, const char *key)
 Get an option by key.
 
ConfigOption * config_get_or_set_option_len (ConfigSection *section, const char *key, size_t key_len, const char *value, size_t value_len)
 
void config_set_string_option_len (ConfigSection *section, const char *key, size_t key_len, const char *value, size_t value_len)
 
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.
 
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 option if it did not exist.
 
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.
 
bool config_get_bool_option (const ConfigSection *section, const char *key, bool *out_value)
 Get a boolean value of an 'item' option by key.
 
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 option if it did not exist.
 
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.
 
bool config_get_int_option (const ConfigSection *section, const char *key, int *out_value)
 Get an integer value of an 'item' option by key.
 
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 option if it did not exist.
 
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.
 
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.
 
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 creating the option if it did not exist.
 
ConfigOption * config_set_value_len (ConfigOption *option, const char *value, size_t value_len)
 
ConfigOption * config_add_value_len (ConfigOption *option, const char *value, size_t value_len)
 
ConfigOption * config_remove_value (ConfigOption *option, unsigned int i)
 Remove a value from a 'list' option.
 
const char * config_get_value (const ConfigOption *option)
 Get the value of an 'item' option.
 

Detailed Description

Author
Carl Reinke
Date
2015

Macro Definition Documentation

◆ COMPILE_TIME_ASSERT

#define COMPILE_TIME_ASSERT ( name,
cond )   typedef int assert_ ## name[(cond) * 2 - 1]

Cause compile error if compile-time computable condition fails.

Parameters
[in]namethe unique identifier of the assertion
[in]condthe condition

◆ COUNTOF

#define COUNTOF ( a)    (sizeof(a) / sizeof(*(a)))

Calculate the number of elements in a fixed-length array.

Parameters
[in]athe fixed-length array
Returns
the number of elements in the array

◆ foreach_option_i_value

#define foreach_option_i_value ( i,
string_value,
option )
Value:
for (unsigned int (i) = 0; (i) == 0; (i) = ~0) \
for (ConfigOption *_option = (option); _option != NULL; _option = NULL) \
for (ConfigString *_values_begin = _option->values_count == 0 ? &_option->v.value : &_option->v.values[0], \
*_values_end = _option->values_count == 0 ? _values_begin + 1 : &_option->v.values[_option->values_count], \
*_value = _values_begin; _value < _values_end; ++_value, (i) = _value - _values_begin) \
for (const char *(string_value) = config_string_to_cstr(_value); (string_value) != NULL; (string_value) = NULL)
An option consisting of one (an item) or many (a list) values.
Definition config_file.h:110
A short-string-optimizing string type.
Definition config_file.h:67

Iterate over the values assigned to the option.

Parameters
[out]ithe index variable to declare
[out]string_valuethe value variable to declare
[in]optionthe option

◆ foreach_option_value

#define foreach_option_value ( string_value,
option )
Value:
for (ConfigOption *_option = (option); _option != NULL; _option = NULL) \
for (ConfigString *_values_begin = _option->values_count == 0 ? &_option->v.value : &_option->v.values[0], \
*_values_end = _option->values_count == 0 ? _values_begin + 1 : &_option->v.values[_option->values_count], \
*_value = _values_begin; _value < _values_end; ++_value) \
for (const char *(string_value) = config_string_to_cstr(_value); (string_value) != NULL; (string_value) = NULL)

Iterate over the values assigned to the option.

Parameters
[out]string_valuethe value variable to declare
[in]optionthe option

◆ foreach_remove_option_value

#define foreach_remove_option_value ( )
Value:
{ \
extern void config_oom(void); \
unsigned int _value_i = _value - _values_begin; \
if (config_remove_value(_option, _value_i) == NULL) \
config_oom(); \
_values_begin = _option->values_count == 0 ? &_option->v.value : &_option->v.values[0]; \
_values_end = _option->values_count == 0 ? _values_begin + 1 : &_option->v.values[_option->values_count]; \
_value = _values_begin + _value_i - 1; \
}
ConfigOption * config_remove_value(ConfigOption *option, unsigned int i)
Remove a value from a 'list' option.
Definition config_file.c:531

Remove a value from an option during iteration. Should be followed by continue.

Function Documentation

◆ config_add_section_len()

ConfigSection * config_add_section_len ( Config * config,
const char * type,
size_t type_len,
const char * name,
size_t name_len )
extern
See also
config_add_section()

◆ config_add_value_len()

ConfigOption * config_add_value_len ( ConfigOption * option,
const char * value,
size_t value_len )
extern
See also
config_add_value()

◆ config_deinit()

void config_deinit ( Config * config)
extern

Release any memory allocated inside a configuration.

Parameters
[in]configthe configuration
Returns
void

◆ config_find_or_add_section()

ConfigSection * config_find_or_add_section ( Config * config,
const char * type,
const char * name )
extern

Find a section by type and name, creating the section if it did not exist.

Parameters
[in]configthe configuration containing the section
[in]typethe type of the section
[in]namethe name of the section; may be NULL
Returns
the section; NULL if out of memory

◆ config_find_section()

ConfigSection * config_find_section ( Config * config,
const char * type,
const char * name )
extern

Find a section by type and name.

Parameters
[in]configthe configuration containing the section
[in]typethe type of the section
[in]namethe name of the section
Returns
the section; NULL if it does not exist

◆ config_find_sections()

ConfigSection * config_find_sections ( Config * config,
const char * type,
ConfigSection ** save )
extern

Iterate sections by type.

Parameters
[in]configthe configuration containing the sections
[in]typethe type of the section
[in,out]savethe saved state of the iterator; initialize *save to NULL before iteration
Returns
the section; NULL if iteration finished

◆ config_get_bool_option()

bool config_get_bool_option ( const ConfigSection * section,
const char * key,
bool * out_value )
extern

Get a boolean value of an 'item' option by key.

Parameters
[in]sectionthe section containing the option
[in]keythe option key
[out]out_valuethe item value if a valid option exists; otherwise unset
Returns
whether out_value was set

◆ config_get_int_option()

bool config_get_int_option ( const ConfigSection * section,
const char * key,
int * out_value )
extern

Get an integer value of an 'item' option by key.

Parameters
[in]sectionthe section containing the option
[in]keythe option key
[out]out_valuethe item value if a valid option exists; otherwise unset
Returns
whether out_value was set

◆ config_get_option()

ConfigOption * config_get_option ( const ConfigSection * section,
const char * key )
extern

Get an option by key.

Parameters
[in]sectionthe section containing the option
[in]keythe option key
Returns
the option; NULL if it does not exist

◆ config_get_or_set_bool_option()

bool config_get_or_set_bool_option ( ConfigSection * section,
const char * key,
bool value,
ConfigBoolStyle style )
extern

Get a boolean value of an 'item' option by key, setting the option if it was invalid or creating the option if it did not exist.

Parameters
[in]sectionthe section containing the option
[in]keythe option key
[in]valuethe default item value
[in]stylethe style of boolean value
Returns
the value

◆ config_get_or_set_int_option()

int config_get_or_set_int_option ( ConfigSection * section,
const char * key,
int value )
extern

Get an integer value of an 'item' option by key, setting the option if it was invalid or creating the option if it did not exist.

Parameters
[in]sectionthe section containing the option
[in]keythe option key
[in]valuethe default item value
Returns
the value

◆ config_get_or_set_option_len()

ConfigOption * config_get_or_set_option_len ( ConfigSection * section,
const char * key,
size_t key_len,
const char * value,
size_t value_len )
extern
See also
config_get_or_set_option()

◆ config_get_or_set_string_option()

const char * config_get_or_set_string_option ( ConfigSection * section,
const char * key,
const char * value )
extern

Get a string value of an 'item' option by key, setting the option if it was invalid or creating the option if it did not exist.

Parameters
[in]sectionthe section containing the option
[in]keythe option key
[in]valuethe default item value
Returns
the value

◆ config_get_or_set_uint_option()

unsigned int config_get_or_set_uint_option ( ConfigSection * section,
const char * key,
unsigned int value )
extern

Get an unsigned integer value of an 'item' option by key, setting the option if it was invalid or creating the option if it did not exist.

Parameters
[in]sectionthe section containing the option
[in]keythe option key
[in]valuethe default item value
Returns
the value

◆ config_get_string_option()

bool config_get_string_option ( const ConfigSection * section,
const char * key,
const char ** out_value )
extern

Get a string value of an 'item' option by key.

Parameters
[in]sectionthe section containing the option
[in]keythe option key
[out]out_valuethe item value if a valid option exists; otherwise unset
Returns
whether out_value was set

◆ config_get_uint_option()

bool config_get_uint_option ( const ConfigSection * section,
const char * key,
unsigned int * out_value )
extern

Get an unsigned integer value of an 'item' option by key.

Parameters
[in]sectionthe section containing the option
[in]keythe option key
[out]out_valuethe item value if a valid option exists; otherwise unset
Returns
whether out_value was set

◆ config_get_value()

const char * config_get_value ( const ConfigOption * option)
extern

Get the value of an 'item' option.

Parameters
[in]optionthe option
Returns
the value; NULL if option was NULL or was a 'list' option

◆ config_init()

void config_init ( Config * config)
extern

Initialize a configuration.

Parameters
[in]configthe configuration
Returns
void

◆ config_parse()

bool config_parse ( Config * config,
FILE * file )
extern

Parse a configuration from a file.

Parameters
[in]configthe uninitialized configuration
[in]filethe file handle
Returns
whether parsing succeeded

◆ config_remove_value()

ConfigOption * config_remove_value ( ConfigOption * option,
unsigned int i )
extern

Remove a value from a 'list' option.

Parameters
[in]optionthe option
[in]ithe index of the value
Returns
the option; NULL if out of memory or invalid index

◆ config_set_bool_option()

void config_set_bool_option ( ConfigSection * section,
const char * key,
bool value,
ConfigBoolStyle style )
extern

Set a boolean value of an 'item' option by key, creating the option if necessary.

Parameters
[in]sectionthe section containing the option
[in]keythe option key
[in]valuethe item value
[in]stylethe style of boolean value
Returns
void

◆ config_set_int_option()

void config_set_int_option ( ConfigSection * section,
const char * key,
int value )
extern

Set an integer value of an 'item' option by key, creating the option if necessary.

Parameters
[in]sectionthe section containing the option
[in]keythe option key
[in]valuethe item value
Returns
void

◆ config_set_option_len()

ConfigOption * config_set_option_len ( ConfigSection * section,
const char * key,
size_t key_len,
const char * value,
size_t value_len )
extern
See also
config_set_option()

◆ config_set_string_option_len()

void config_set_string_option_len ( ConfigSection * section,
const char * key,
size_t key_len,
const char * value,
size_t value_len )
extern
See also
config_set_string_option()

◆ config_set_uint_option()

void config_set_uint_option ( ConfigSection * section,
const char * key,
unsigned int value )
extern

Set an unsigned integer value of an 'item' option by key, creating the option if necessary.

Parameters
[in]sectionthe section containing the option
[in]keythe option key
[in]valuethe item value
Returns
void

◆ config_set_value_len()

ConfigOption * config_set_value_len ( ConfigOption * option,
const char * value,
size_t value_len )
extern
See also
config_set_value()

◆ config_write()

void config_write ( const Config * config,
FILE * file )
extern

Write a configuration to a file.

Parameters
[in]configthe configuration
[in]filethe file handle
Returns
void