scsl 1.0.1
Shimmering Clarity Standard Library
|
SimpleConfig is a basic configuration for projects. More...
#include <SimpleConfig.h>
Public Member Functions | |
SimpleConfig () | |
The constructor doesn't need any initialisation. | |
SimpleConfig (std::string &prefix) | |
The constructor can explicitly set the environment prefix. | |
int | Load (const char *path) |
Load key-value pairs from a file. | |
int | Load (std::string &path) |
Load key-value pairs from a file. | |
void | SetPrefix (const std::string &prefix) |
Set the prefix in use by the config. | |
std::vector< std::string > | KeyList () |
Return the keys cached in the config. | |
std::string | Get (std::string &key) |
Get the value stored for the key from the config. | |
std::string | Get (std::string &key, std::string defaultValue) |
Get the value stored for the key from the config. | |
SimpleConfig is a basic configuration for projects.
SimpleConfig is a basic key-value system. It can optionally load key-value pairs from a file, which should consist of key = value lines. Comments may be added by starting the line with a '#'; these lines will be skipped. Comments may have leading whitespace. Any empty lines or lines consisting solely of whitespace will also be skipped.
When values are retrieved by one of the variants on Get, they are looked up in the following order, assuming key
and an optional prefix
set on the config:
key
, it will be returned.{prefix}{key}
, the value will be cached and it will be returned.In Go projects, I've used the global config to great success. However, callers may set up an explicit configuration instance.
scsl::SimpleConfig::SimpleConfig | ( | ) |
The constructor doesn't need any initialisation.
|
explicit |
The constructor can explicitly set the environment prefix.
std::string scsl::SimpleConfig::Get | ( | std::string & | key | ) |
Get the value stored for the key from the config.
key | The key to look up. See the class documentation for information on how this is used. |
std::string scsl::SimpleConfig::Get | ( | std::string & | key, |
std::string | defaultValue | ||
) |
Get the value stored for the key from the config.
key | The key to look up. See the class documentation for information on how this is used. |
defaultValue | A default value to cache and use if no value is stored under the key. |
std::vector< std::string > scsl::SimpleConfig::KeyList | ( | ) |
Return the keys cached in the config.
Note that this won't returned any non-cached environment values.
int scsl::SimpleConfig::Load | ( | const char * | path | ) |
Load key-value pairs from a file.
path | The path to a config file. |
int scsl::SimpleConfig::Load | ( | std::string & | path | ) |
Load key-value pairs from a file.
path | The path to a config file. |
void scsl::SimpleConfig::SetPrefix | ( | const std::string & | prefix | ) |
Set the prefix in use by the config.
prefix | The prefix to prepend to the key when looking up values from the environment. |