|
cwist
Modern C web framework with libttak runtime
|
#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>
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. | |
| cwist_config * cwist_config_create | ( | void | ) |
Create an empty config store.
Create a new configuration object.
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().
| void cwist_config_destroy | ( | cwist_config * | cfg | ) |
Free a config store and all of its key/value entries.
Destroy a configuration object.
| cfg | Config 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().
| const char * cwist_config_get | ( | cwist_config * | cfg, |
| const char * | key | ||
| ) |
Look up the value for a key.
Get a string value by key.
| cfg | Config store; NULL is accepted. |
| key | Key to look up; NULL is accepted. |
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().
| 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.
| cfg | Config store. |
| key | Key to look up. |
| default_val | Value returned when the key is missing or matches no recognized spelling. |
default_val on unrecognized values. Definition at line 140 of file config.c.
References cap, and cwist_config_get().
| 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.
| cfg | Config store. |
| key | Key to look up. |
| default_val | Value returned when the key is missing or is not a well-formed base-10 integer. |
default_val on missing/malformed values. Definition at line 122 of file config.c.
References cap, and cwist_config_get().
| 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.
| cfg | Config store; NULL is accepted and ignored. |
| prefix | Optional 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.
| 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.
| cfg | Config store; NULL is accepted and ignored. |
| path | Path of the file to load; NULL is accepted and ignored. |
Definition at line 184 of file config.c.
References cap, and cwist_config_set().
Set a config key to a value, replacing any existing entry.
Set a string value.
| cfg | Config store; NULL is accepted and ignored. |
| key | Key to set; NULL is accepted and ignored. A copy is stored. |
| value | Value 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().