cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
config.c File Reference

config.c interface. More...

#include <cwist/sys/app/config.h>
#include <cwist/core/mem/alloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
Include dependency graph for config.c:

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
 
#define CWIST_CONFIG_BUCKETS   64
 

Functions

cwist_config * cwist_config_create (void)
 Create an empty config store.
 
void cwist_config_destroy (cwist_config *cfg)
 Free a config store and all of its key/value entries.
 
void cwist_config_set (cwist_config *cfg, const char *key, const char *value)
 Set a config key to a value, replacing any existing entry.
 
const char * cwist_config_get (cwist_config *cfg, const char *key)
 Look up the value for a key.
 
int cwist_config_get_int (cwist_config *cfg, const char *key, int default_val)
 Get a config value parsed as an integer.
 
bool cwist_config_get_bool (cwist_config *cfg, const char *key, bool default_val)
 Get a config value parsed as a boolean. Accepted true spellings (case-insensitive): "true", "1", "yes". Accepted false spellings (case-insensitive): "false", "0", "no".
 
void cwist_config_load_env (cwist_config *cfg, const char *prefix)
 Load key/value pairs from the process environment into the config. Entries without '=' are skipped. If prefix is non-NULL and non-empty, only variables starting with the prefix are loaded; the prefix is kept in the stored key.
 
void cwist_config_load_file (cwist_config *cfg, const char *path)
 Load key/value pairs from a simple properties-style file. Each non-blank, non-comment line must be key=value. Leading/trailing whitespace around keys and values is trimmed, and a value wrapped in double quotes is unquoted. Lines starting with '#' and lines without '=' are skipped. If the file cannot be opened, nothing is loaded.
 

Detailed Description

config.c interface.

Definition in file config.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 4 of file config.c.

◆ CWIST_CONFIG_BUCKETS

#define CWIST_CONFIG_BUCKETS   64

Definition at line 13 of file config.c.

Function Documentation

◆ cwist_config_create()

cwist_config * cwist_config_create ( void  )

Create an empty config store.

Create a new configuration object.

Returns
Newly allocated config on success, NULL if allocation fails.

Definition at line 29 of file config.c.

References cwist_config::bucket_count, cwist_config::buckets, cap, cwist_alloc(), cwist_alloc_array(), CWIST_CONFIG_BUCKETS, and cwist_free().

Referenced by cwist_app_create().

◆ cwist_config_destroy()

void cwist_config_destroy ( cwist_config *  cfg)

Free a config store and all of its key/value entries.

Destroy a configuration object.

Parameters
cfgConfig to destroy; NULL is accepted and ignored.

Definition at line 45 of file config.c.

References cwist_config::bucket_count, cwist_config::buckets, cap, cwist_free(), and cwist_config_bucket::next.

Referenced by cwist_app_destroy().

◆ cwist_config_get()

const char * cwist_config_get ( cwist_config *  cfg,
const char *  key 
)

Look up the value for a key.

Get a string value by key.

Parameters
cfgConfig store; NULL is accepted.
keyKey to look up; NULL is accepted.
Returns
Pointer to the stored value string, or NULL if the key is not found or the arguments are invalid. The pointer is owned by the config and must not be freed.

Definition at line 102 of file config.c.

References cwist_config::bucket_count, cwist_config::buckets, cap, and cwist_config_bucket::value.

Referenced by cwist_config_get_bool(), and cwist_config_get_int().

◆ cwist_config_get_bool()

bool cwist_config_get_bool ( cwist_config *  cfg,
const char *  key,
bool  default_val 
)

Get a config value parsed as a boolean. Accepted true spellings (case-insensitive): "true", "1", "yes". Accepted false spellings (case-insensitive): "false", "0", "no".

Get a boolean value by key.

Parameters
cfgConfig store.
keyKey to look up.
default_valValue returned when the key is missing or matches no recognized spelling.
Returns
The parsed boolean, or default_val on unrecognized values.

Definition at line 140 of file config.c.

References cap, and cwist_config_get().

◆ cwist_config_get_int()

int cwist_config_get_int ( cwist_config *  cfg,
const char *  key,
int  default_val 
)

Get a config value parsed as an integer.

Get an integer value by key.

Parameters
cfgConfig store.
keyKey to look up.
default_valValue returned when the key is missing or is not a well-formed base-10 integer.
Returns
The parsed integer, or default_val on missing/malformed values.

Definition at line 122 of file config.c.

References cap, and cwist_config_get().

◆ cwist_config_load_env()

void cwist_config_load_env ( cwist_config *  cfg,
const char *  prefix 
)

Load key/value pairs from the process environment into the config. Entries without '=' are skipped. If prefix is non-NULL and non-empty, only variables starting with the prefix are loaded; the prefix is kept in the stored key.

Load environment variables with a prefix.

Parameters
cfgConfig store; NULL is accepted and ignored.
prefixOptional key prefix filter; NULL or empty matches all.

Definition at line 157 of file config.c.

References cap, cwist_alloc(), cwist_config_set(), and CWIST_DEFER_FREE.

◆ cwist_config_load_file()

void cwist_config_load_file ( cwist_config *  cfg,
const char *  path 
)

Load key/value pairs from a simple properties-style file. Each non-blank, non-comment line must be key=value. Leading/trailing whitespace around keys and values is trimmed, and a value wrapped in double quotes is unquoted. Lines starting with '#' and lines without '=' are skipped. If the file cannot be opened, nothing is loaded.

Load a configuration file.

Parameters
cfgConfig store; NULL is accepted and ignored.
pathPath of the file to load; NULL is accepted and ignored.

Definition at line 184 of file config.c.

References cap, and cwist_config_set().

◆ cwist_config_set()

void cwist_config_set ( cwist_config *  cfg,
const char *  key,
const char *  value 
)

Set a config key to a value, replacing any existing entry.

Set a string value.

Parameters
cfgConfig store; NULL is accepted and ignored.
keyKey to set; NULL is accepted and ignored. A copy is stored.
valueValue to set; NULL is stored as an empty string. A copy is stored.

Definition at line 66 of file config.c.

References cwist_config::bucket_count, cwist_config::buckets, cap, cwist_alloc(), cwist_free(), cwist_strdup(), cwist_config_bucket::key, cwist_config_bucket::next, and cwist_config_bucket::value.

Referenced by cwist_config_load_env(), and cwist_config_load_file().