scsl 1.0.1
Shimmering Clarity Standard Library
Loading...
Searching...
No Matches
Public Member Functions | List of all members
scsl::SimpleConfig Class Reference

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.
 

Detailed Description

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:

  1. Any cached key-value pairs. Loading a file caches the key-value pairs in the file. The file is not used again, unless another call to Load is made. If the cache has a name for key, it will be returned.
  2. The value is looked up from the environment. An optional prefix can be set; if so, if there is an environment named {prefix}{key}, the value will be cached and it will be returned.
  3. If a default value has been provided, it will be cached and returned.
  4. If none of the previous steps has provided a value, an empty string will be returned.

In Go projects, I've used the global config to great success. However, callers may set up an explicit configuration instance.

Constructor & Destructor Documentation

◆ SimpleConfig() [1/2]

scsl::SimpleConfig::SimpleConfig ( )

The constructor doesn't need any initialisation.

◆ SimpleConfig() [2/2]

scsl::SimpleConfig::SimpleConfig ( std::string &  prefix)
explicit

The constructor can explicitly set the environment prefix.

Member Function Documentation

◆ Get() [1/2]

std::string scsl::SimpleConfig::Get ( std::string &  key)

Get the value stored for the key from the config.

Parameters
keyThe key to look up. See the class documentation for information on how this is used.
Returns
The value stored under the key, or an empty string.

◆ Get() [2/2]

std::string scsl::SimpleConfig::Get ( std::string &  key,
std::string  defaultValue 
)

Get the value stored for the key from the config.

Parameters
keyThe key to look up. See the class documentation for information on how this is used.
defaultValueA default value to cache and use if no value is stored under the key.
Returns
The value stored under the key, or the default value.

◆ KeyList()

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.

Returns
A list of keys stored under the config.

◆ Load() [1/2]

int scsl::SimpleConfig::Load ( const char *  path)

Load key-value pairs from a file.

Parameters
pathThe path to a config file.
Returns
0 if the file was loaded successfully.

◆ Load() [2/2]

int scsl::SimpleConfig::Load ( std::string &  path)

Load key-value pairs from a file.

Parameters
pathThe path to a config file.
Returns
0 if the file was loaded successfully.

◆ SetPrefix()

void scsl::SimpleConfig::SetPrefix ( const std::string &  prefix)

Set the prefix in use by the config.

Parameters
prefixThe prefix to prepend to the key when looking up values from the environment.

The documentation for this class was generated from the following file: