PatchworkOS
Loading...
Searching...
No Matches
Config files

System configuration files. More...

Data Structures

struct  config_array_t
 Configuration array structure. More...
 
struct  config_t
 Opaque configuration structure. More...
 

Typedefs

typedef struct config config_t
 

Functions

config_tconfig_open (const char *prefix, const char *name)
 Open a configuration file.
 
void config_close (config_t *config)
 Close a configuration file.
 
const char * config_get_string (config_t *config, const char *section, const char *key, const char *fallback)
 Get a string value from a configuration file.
 
int64_t config_get_int (config_t *config, const char *section, const char *key, int64_t fallback)
 Get an integer value from a configuration file.
 
bool config_get_bool (config_t *config, const char *section, const char *key, bool fallback)
 Get a boolean value from a configuration file.
 
config_array_tconfig_get_array (config_t *config, const char *section, const char *key)
 Get an array of strings from a configuration file.
 
void config_array_free (config_array_t *array)
 Free a configuration array.
 

Detailed Description

System configuration files.

Patchwork uses a /cfg folder for all system configuration files. These files are simple INI style text files that store key-value pairs in sections.

TODO: The current system is rather simplistic and in the future it might, if i can be bothered, be worth implementing a database like configuration system.

Typedef Documentation

◆ config_t

typedef struct config config_t

Definition at line 31 of file config.h.

Function Documentation

◆ config_array_free()

void config_array_free ( config_array_t array)

Free a configuration array.

Parameters
arrayThe configuration array to free.

Definition at line 388 of file config.c.

References free(), and NULL.

◆ config_close()

void config_close ( config_t config)

Close a configuration file.

Parameters
configThe configuration file to close.

Definition at line 202 of file config.c.

References CONTAINER_OF, free(), config_pair_t::key, list_is_empty(), list_pop(), config_section_t::name, NULL, config_section_t::pairs, config_t::sections, and config_pair_t::value.

Referenced by config_open(), main(), start_menu_load_entries(), and theme_lazy_load().

◆ config_get_array()

config_array_t * config_get_array ( config_t config,
const char *  section,
const char *  key 
)

Get an array of strings from a configuration file.

Parses the string value into an array of strings, split by commas and with the whitespace trimmed.

Parameters
configThe configuration file.
sectionThe section to get the value from, case insensitive.
keyThe key to get the value for, case insensitive.
Returns
The configuration array or an empty array, will only be NULL on memory allocation failure.

Definition at line 300 of file config.c.

References config_get_string(), data, isspace(), config_array_t::items, config_array_t::length, malloc(), memcpy(), NULL, start(), and strlen().

Referenced by execute_commands(), start_menu_load_entries(), start_programs(), and start_services().

◆ config_get_bool()

bool config_get_bool ( config_t config,
const char *  section,
const char *  key,
bool  fallback 
)

Get a boolean value from a configuration file.

Recognized "true" values (case-insensitive): "true", "yes", "on", "1". Recognized "false" values (case-insensitive): "false", "no", "off", "0".

Parameters
configThe configuration file.
sectionThe section to get the value from, case insensitive.
keyThe key to get the value for, case insensitive.
fallbackA default value to return if not found or unrecognized.
Returns
The boolean value if found and parsed, or fallback otherwise.

Definition at line 273 of file config.c.

References config_get_string(), NULL, strcasecmp(), and strcmp().

◆ config_get_int()

int64_t config_get_int ( config_t config,
const char *  section,
const char *  key,
int64_t  fallback 
)

Get an integer value from a configuration file.

Parameters
configThe configuration file.
sectionThe section to get the value from, case insensitive.
keyThe key to get the value for, case insensitive.
fallbackA default value to return if the key is not found or cannot be parsed as an integer.
Returns
The integer value if found and parsed, or fallback otherwise.

Definition at line 250 of file config.c.

References config_get_string(), NULL, and strtoll().

Referenced by start_programs(), start_services(), theme_colors_load(), and theme_lazy_load().

◆ config_get_string()

const char * config_get_string ( config_t config,
const char *  section,
const char *  key,
const char *  fallback 
)

Get a string value from a configuration file.

The value returned is owned by the configuration system and should not be freed or modified.

Parameters
configThe configuration file.
sectionThe section to get the value from, case insensitive.
keyThe key to get the value for, case insensitive.
fallbackA default value to return if the key is not found.
Returns
The string value if found, or fallback otherwise.

Definition at line 228 of file config.c.

References config_find_pair(), config_find_section(), NULL, and config_pair_t::value.

Referenced by config_get_array(), config_get_bool(), config_get_int(), and theme_lazy_load().

◆ config_open()

config_t * config_open ( const char *  prefix,
const char *  name 
)

Open a configuration file.

All configuration files have this full path: /cfg/<prefix>-<name>.cfg.

The goal is that each system or application uses its own prefix to avoid name collisions.

Parameters
prefixThe prefix of the configuration file, for example theme for theme related settings.
nameThe name of the configuration file, for example colors for color related settings.
Returns
On success, the opened configuration file. On failure, returns NULL.

Definition at line 93 of file config.c.

References config_close(), config_trim_whitespace(), config_pair_t::entry, config_section_t::entry, fclose(), fgets(), file, fopen(), free(), config_pair_t::key, lineBuffer, list_entry_init(), list_init(), list_push(), malloc(), MAX_PATH, config_section_t::name, NULL, config_section_t::pairs, config_t::sections, snprintf(), strchr(), strdup(), and config_pair_t::value.

Referenced by main(), start_menu_load_entries(), and theme_lazy_load().