A class that reads a key/value file. These files (typically named *.ini) follow the INI file format standard (see http://en.wikipedia.org/wiki/INI_file) and have the following structure:
It is possible to import another ini file, by specifying as many of the keys listed below as necessary. Please not that in order to prevent circular dependencies, it is not possible to import the same file several times.
#include <Config.h>
Public Member Functions | |
Config () | |
Empty constructor. The user MUST later one fill the internal key/value map object. More... | |
virtual | ~Config () |
Config (const std::string &filename_in) | |
Main constructor. The file is parsed and a key/value map object is internally created. More... | |
void | write (const std::string &filename) const |
Write the Config object to a file. More... | |
void | addFile (const std::string &filename_in) |
Add the content of a file to the internal key/value map object. More... | |
void | addCmdLine (const std::string &cmd_line) |
Add the content of the given command line to the internal key/value map object. More... | |
void | addKey (const std::string &key, const std::string §ion, const std::string &value) |
Add a specific key/value pair to the internal key/value map object. key and section are case insensitive. More... | |
void | deleteKey (const std::string &key, const std::string §ion) |
Delete a specific key/value pair from the internal map object, key/section are case insensitive. More... | |
void | deleteKeys (const std::string &keymatch, const std::string §ion, const bool &anywhere=false) |
Delete keys matching a specific pattern from the internal map object, key/section are case insensitive. More... | |
std::string | getSourceName () const |
Returns the filename that the Config object was constructed with. More... | |
std::string | getConfigRootDir () const |
Returns the directory where the root configuration file is (needed to resolv relative paths). More... | |
bool | keyExists (const std::string &key, const std::string §ion) const |
Return if a given key exists in a given section. More... | |
const std::string | toString () const |
Print the content of the Config object (usefull for debugging) The Config is bound by "<Config>" and "</Config>" on separate lines. More... | |
template<typename T > | |
std::vector< T > | getValue (const std::string &key, const std::string §ion, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const |
template<typename T > | |
void | getValue (const std::string &key, std::vector< T > &vecT, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const |
Template function to retrieve a vector of values of class T for a certain key. More... | |
template<typename T > | |
void | getValue (const std::string &key, const std::string §ion, std::vector< T > &vecT, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const |
Template function to retrieve a vector of values of class T for a certain key. More... | |
ConfigProxy | get (const std::string &key, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const |
ConfigProxy | get (const std::string &key, const std::string §ion, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const |
template<typename T > | |
void | getValue (const std::string &key, T &t, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const |
Template function to retrieve a value of class T for a certain key. More... | |
template<typename T > | |
void | getValue (const std::string &key, const std::string §ion, T &t, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const |
Template function to retrieve a value of class T for a certain key. More... | |
template<typename T > | |
void | getValues (const std::string &keystart, const std::string §ion, std::vector< T > &vecT) const |
Template function to retrieve a vector of values of class T for a certain key pattern. More... | |
template<typename T > | |
void | getValues (const std::string &keystart, const std::string §ion, std::vector< T > &vecT, std::vector< std::string > &vecKeys) const |
size_t | findKeys (std::vector< std::string > &vecResult, const std::string &keymatch, std::string section, const bool &anywhere=false) const |
Function that searches for a given string within the keys of section (default: GENERAL) it returns the number of matches (partial matches are considered) and writes all the keys into a vector<string> that is handed to the function as reference. More... | |
Friends | |
std::iostream & | operator<< (std::iostream &os, const Config &cfg) |
std::iostream & | operator>> (std::iostream &is, Config &cfg) |
mio::Config::Config | ( | ) |
Empty constructor. The user MUST later one fill the internal key/value map object.
|
inlinevirtual |
mio::Config::Config | ( | const std::string & | filename_in | ) |
Main constructor. The file is parsed and a key/value map object is internally created.
[in] | filename_in | string representing the absolute filename of the key/value file |
void mio::Config::addCmdLine | ( | const std::string & | cmd_line | ) |
Add the content of the given command line to the internal key/value map object.
[in] | cmd_line | string representing the command line to be parsed for key/value pairs or switches |
void mio::Config::addFile | ( | const std::string & | filename_in | ) |
Add the content of a file to the internal key/value map object.
[in] | filename_in | string representing the absolute filename of the key/value file |
void mio::Config::addKey | ( | const std::string & | key, |
const std::string & | section, | ||
const std::string & | value | ||
) |
Add a specific key/value pair to the internal key/value map object. key and section are case insensitive.
[in] | key | string representing the key to be added |
[in] | section | std::string representing a section name; the key has to be part of this section |
[in] | value | string representing the matching value to be added |
void mio::Config::deleteKey | ( | const std::string & | key, |
const std::string & | section | ||
) |
Delete a specific key/value pair from the internal map object, key/section are case insensitive.
[in] | key | string representing the key to be added |
[in] | section | std::string representing a section name; the key has to be part of this section |
void mio::Config::deleteKeys | ( | const std::string & | keymatch, |
const std::string & | section, | ||
const bool & | anywhere = false |
||
) |
Delete keys matching a specific pattern from the internal map object, key/section are case insensitive.
[in] | keymatch | A string representing the beginning of a key to search for |
[in] | section | A string defining which section to search through (default: GENERAL) |
[in] | anywhere | Match substring anywhere in the key string (default=false, ie at the begining only) Config cfg("io.ini");
cfg.deleteKeys("STATION", "Input");
|
size_t mio::Config::findKeys | ( | std::vector< std::string > & | vecResult, |
const std::string & | keymatch, | ||
std::string | section, | ||
const bool & | anywhere = false |
||
) | const |
Function that searches for a given string within the keys of section (default: GENERAL) it returns the number of matches (partial matches are considered) and writes all the keys into a vector<string> that is handed to the function as reference.
[out] | vecResult | A vector that will hold all keys that partially match keystart |
[in] | keymatch | A string representing the beginning of a key to search for |
[in] | section | A string defining which section to search through (default: GENERAL) |
[in] | anywhere | Match substring anywhere in the key string (default=false, ie at the begining only) vector<string> myVec;
size_t nrOfMatches = cfg.findKeys(myVec, "TA::", "Filters");
|
ConfigProxy mio::Config::get | ( | const std::string & | key, |
const IOUtils::ThrowOptions & | opt = IOUtils::dothrow |
||
) | const |
@ brief A function that allows to retrieve a value for a key as return parameter (vectors of values too)
[in] | key | std::string representing a KEY in the key/value file (default section "GENERAL" is assumed) |
[in] | opt | indicating whether an exception should be raised, when key is not present |
ConfigProxy mio::Config::get | ( | const std::string & | key, |
const std::string & | section, | ||
const IOUtils::ThrowOptions & | opt = IOUtils::dothrow |
||
) | const |
@ brief A function that allows to retrieve a value for a key as return parameter (vectors of values too)
[in] | key | std::string representing a KEY in the key/value file (default section "GENERAL" is assumed) |
[in] | section | std::string representing a section name; the key has to be part of this section |
[in] | opt | indicating whether an exception should be raised, when key is not present |
Example Usage:
std::string mio::Config::getConfigRootDir | ( | ) | const |
Returns the directory where the root configuration file is (needed to resolv relative paths).
std::string mio::Config::getSourceName | ( | ) | const |
Returns the filename that the Config object was constructed with.
|
inline |
|
inline |
Template function to retrieve a vector of values of class T for a certain key.
[in] | key | std::string representing a KEY in the key/value file (default section "GENERAL" is assumed) |
[out] | vecT | a variable of class vector<T> into which the values for the corresponding key are saved |
[in] | opt | indicating whether an exception should be raised, when key is not present |
|
inline |
Template function to retrieve a vector of values of class T for a certain key.
[in] | key | std::string representing a KEY in the key/value file |
[in] | section | std::string representing a section name; the key has to be part of this section |
[out] | vecT | a variable of class vector<T> into which the values for the corresponding key are saved |
[in] | opt | indicating whether an exception should be raised, when key is not present |
|
inline |
Template function to retrieve a value of class T for a certain key.
[in] | key | std::string representing a KEY in the key/value file (default section "GENERAL" is assumed) |
[out] | t | a variable of class T into which the value for the corresponding key is saved (e.g. double, int, std::string) |
[in] | opt | indicating whether an exception should be raised, when key is not present |
|
inline |
Template function to retrieve a value of class T for a certain key.
[in] | key | std::string representing a KEY in the key/value file |
[in] | section | std::string representing a section name; the key has to be part of this section |
[out] | t | a variable of class T into which the value for the corresponding key is saved (e.g. double, int, std::string) |
[in] | opt | indicating whether an exception should be raised, when key is not present |
|
inline |
Template function to retrieve a vector of values of class T for a certain key pattern.
[in] | keystart | std::string representing a pattern for the key in the key/value file |
[in] | section | std::string representing a section name; the key has to be part of this section |
[out] | vecT | a vector of class T into which the values for the corresponding keys are saved |
|
inline |
bool mio::Config::keyExists | ( | const std::string & | key, |
const std::string & | section | ||
) | const |
Return if a given key exists in a given section.
[in] | key | string representing the key to be searched |
[in] | section | std::string representing a section name; the key has to be part of this section |
const std::string mio::Config::toString | ( | ) | const |
void mio::Config::write | ( | const std::string & | filename | ) | const |
Write the Config object to a file.
filename | The filename including the path, e.g. "/tmp/test.ini" |
|
friend |
|
friend |