My Project
Loading...
Searching...
No Matches
Macros | Enumerations | Functions
config_file.c File Reference
#include "config_file.h"
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>

Macros

#define udecsizeof(t)   ((CHAR_BIT * sizeof(t) / 3) + 1)
 
#define sdecsizeof(t)   (udecsizeof(t) + 1)
 

Enumerations

enum  Directive { INVALID_DIRECTIVE = 0 , SECTION_DIRECTIVE , ITEM_DIRECTIVE , LIST_DIRECTIVE }
 

Functions

void config_oom (void)
 
void config_init (Config *config)
 Initialize a configuration.
 
void config_deinit (Config *config)
 Release any memory allocated inside a configuration.
 
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.
 
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.
 

Detailed Description

Author
Carl Reinke
Date
2015

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 )
See also
config_add_section()

◆ config_add_value_len()

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

◆ config_deinit()

void config_deinit ( Config * config)

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 )

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 )

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 )

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 )

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 )

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 )

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 )

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 )

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 )
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 )

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 )

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 )

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 )

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)

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)

Initialize a configuration.

Parameters
[in]configthe configuration
Returns
void

◆ config_parse()

bool config_parse ( Config * config,
FILE * file )

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 )

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 )

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 )

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 )
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 )
See also
config_set_string_option()

◆ config_set_uint_option()

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.

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 )
See also
config_set_value()

◆ config_write()

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

Write a configuration to a file.

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