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 note that there is a check to prevent circular dependencies.
#include <Config.h>
Public Member Functions | |
Config () | |
Empty constructor. The user MUST later one fill the internal key/value map object. More... | |
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 | addKey (std::string key, 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. More... | |
void | deleteKey (std::string key, std::string section) |
Delete a specific key/value pair from the internal map object, key/section are case insensitive. More... | |
void | deleteKeys (std::string keymatch, std::string section, 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 (std::string key, std::string section) const |
Return if a given key exists in a given section (matching is case insensitive) More... | |
bool | keyExistsRegex (std::string key_pattern, std::string section) const |
bool | sectionExists (std::string section) const |
Return if a given section exists in the Config object. More... | |
const std::string | toString () const |
Print the content of the Config object (useful 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, 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 (std::string key, std::string section, 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... | |
const ConfigProxy | get (const std::string &key, const std::string §ion) const |
A function that allows to retrieve a value for a key as return parameter (vectors of values too). More... | |
template<typename T > | |
T | get (const std::string &key, const std::string §ion, const T &dflt) const |
A function that allows to retrieve a value for a key as return parameter (vectors of values too). More... | |
std::string | get (const std::string &key, const std::string §ion, const std::string &dflt) const |
A function that allows to retrieve a value for a key as return parameter (vectors of values too). More... | |
std::string | get (const std::string &key, const std::string §ion, const char dflt[]) const |
double | get (const std::string &key, const std::string §ion, const double &dflt) const |
bool | get (const std::string &key, const std::string §ion, const bool &dflt) 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 (std::string key, std::string section, T &t, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const |
Template function to retrieve a value of class T for a certain key. More... | |
void | getValue (std::string key, std::string section, Date &t, const double &time_zone, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const |
Function to retrieve a Date value for a certain key. More... | |
template<typename T > | |
void | getValues (std::string keymatch, std::string section, 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 (std::string keymatch, std::string section, std::vector< T > &vecT, std::vector< std::string > &vecKeys) const |
std::vector< std::pair< std::string, std::string > > | getValues (std::string keymatch, std::string section, const bool &anywhere=false) const |
Function that searches for a given string within the keys of a given section (default: GENERAL) it returns all the <keys,value> pairs that match (partial matches are considered, matching is case insensitive) into a vector. More... | |
std::vector< std::pair< std::string, std::string > > | getValuesRegex (const std::string ®ex_str, std::string section) const |
std::vector< std::string > | getKeys (std::string keymatch, std::string section, const bool &anywhere=false) const |
Function that searches for a given string within the keys of a given section (default: GENERAL) it returns all the keys that match (partial matches are considered, matching is case insensitive) into a vector<string>. More... | |
std::vector< std::string > | getKeysRegex (const std::string ®ex_str, std::string section) const |
std::set< std::string > | getSections () const |
Returns all the sections that are present in the config object. More... | |
void | moveSection (std::string org, std::string dest, const bool &overwrite) |
Move all keys of the org section to the dest section. More... | |
std::vector< std::pair< std::string, std::string > > | parseArgs (const std::string §ion, const std::string &cmd_id, const unsigned int &cmd_nr, const std::string &arg_pattern) const |
Extract the arguments for a given command and store them into a vector of key / value pairs. More... | |
std::vector< std::pair< std::string, std::string > > | getArgumentsForAlgorithm (const std::string &parname, const std::string &algorithm, const std::string §ion="Interpolations1d") const |
retrieve the resampling algorithm to be used for the 1D interpolation of meteo parameters. The potential arguments are also extracted. More... | |
std::vector< std::pair< std::string, std::string > > | getArgumentsForAlgorithm (const std::string &parname, const std::string &algorithm, const size_t &algo_index, const std::string §ion="Interpolations1d") const |
Static Public Member Functions | |
static unsigned int | getCommandNr (const std::string §ion, const std::string &cmd_pattern, const std::string &cmd_key) |
Extract the command number from a given command string, given the command pattern. More... | |
Friends | |
std::ostream & | operator<< (std::ostream &os, const Config &cfg) |
std::istream & | operator>> (std::istream &is, Config &cfg) |
mio::Config::Config | ( | ) |
Empty constructor. The user MUST later one fill the internal key/value map object.
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::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 | ( | std::string | key, |
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 | ( | std::string | key, |
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 | ( | std::string | keymatch, |
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 beginning only) Config cfg("io.ini");
cfg.deleteKeys("STATION", "Input");
A class that reads a key/value file. These files (typically named *.ini) follow the INI file format s... Definition: Config.h:79 |
const ConfigProxy mio::Config::get | ( | const std::string & | key, |
const std::string & | section | ||
) | const |
A function that allows to retrieve a value for a key as return parameter (vectors of values too).
If the key is not found, an exception is thrown.
[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 |
Example Usage:
bool mio::Config::get | ( | const std::string & | key, |
const std::string & | section, | ||
const bool & | dflt | ||
) | const |
std::string mio::Config::get | ( | const std::string & | key, |
const std::string & | section, | ||
const char | dflt[] | ||
) | const |
double mio::Config::get | ( | const std::string & | key, |
const std::string & | section, | ||
const double & | dflt | ||
) | const |
std::string mio::Config::get | ( | const std::string & | key, |
const std::string & | section, | ||
const std::string & | dflt | ||
) | const |
A function that allows to retrieve a value for a key as return parameter (vectors of values too).
If the key is not found, the provided default value is returned.
[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 |
[in] | dflt | default value, if the key is not found |
Example Usage:
T mio::Config::get | ( | const std::string & | key, |
const std::string & | section, | ||
const T & | dflt | ||
) | const |
A function that allows to retrieve a value for a key as return parameter (vectors of values too).
If the key is not found, the provided default value is returned.
[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 |
[in] | dflt | default value, if the key is not found |
Example Usage:
std::vector< std::pair< std::string, std::string > > mio::Config::getArgumentsForAlgorithm | ( | const std::string & | parname, |
const std::string & | algorithm, | ||
const size_t & | algo_index, | ||
const std::string & | section = "Interpolations1d" |
||
) | const |
std::vector< std::pair< std::string, std::string > > mio::Config::getArgumentsForAlgorithm | ( | const std::string & | parname, |
const std::string & | algorithm, | ||
const std::string & | section = "Interpolations1d" |
||
) | const |
retrieve the resampling algorithm to be used for the 1D interpolation of meteo parameters. The potential arguments are also extracted.
parname | meteo parameter to deal with |
algorithm | algorithm name |
section | INI section to look into |
|
static |
Extract the command number from a given command string, given the command pattern.
Each new command is defined as {cmd_id}::{cmd_pattern}# = {value} and this call extracts the command number out of a given "{cmd_id}::{cmd_pattern}#" string.
For example, a filter command will have as command pattern "TA::FILTER", as command key "TA::FILTER3" and this call will return 3.
[in] | section | The section this command belongs to (for error messages) |
[in] | cmd_pattern | Pattern used to build the stack of commands, such as "TA::FILTER" or even just "::FILTER" |
[in] | cmd_key | the base command key, such as "TA::FILTER3" that will be parsed as {cmd_id}::{cmd_pattern}# to extract the command number |
|
inline |
Returns the directory where the root configuration file is (needed to resolv relative paths).
std::vector< std::string > mio::Config::getKeys | ( | std::string | keymatch, |
std::string | section, | ||
const bool & | anywhere = false |
||
) | const |
Function that searches for a given string within the keys of a given section (default: GENERAL) it returns all the keys that match (partial matches are considered, matching is case insensitive) into a vector<string>.
[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 beginning only) |
std::vector< std::string > mio::Config::getKeysRegex | ( | const std::string & | regex_str, |
std::string | section | ||
) | const |
|
inline |
Returns all the sections that are present in the config object.
|
inline |
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 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 |
Function to retrieve a Date value 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 Date into which the value for the corresponding key is saved |
[out] | time_zone | timezone for the date (if the date provides its own timezone, it will be ignored) |
[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 |
[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 |
std::vector< std::pair< std::string, std::string > > mio::Config::getValues | ( | std::string | keymatch, |
std::string | section, | ||
const bool & | anywhere = false |
||
) | const |
Function that searches for a given string within the keys of a given section (default: GENERAL) it returns all the <keys,value> pairs that match (partial matches are considered, matching is case insensitive) into a vector.
[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 beginning only) |
|
inline |
Template function to retrieve a vector of values of class T for a certain key pattern.
Please not that if the keys are postfixed by integral numbers (ie build as {common string}{integral number}, such as STATION12) then the keys will be sorted in ascending order based on this integral number. As soon as a key does not fit this pattern, the sort will be purely alphabetical (therefore STATION11_a would appear before STATION2_a).
[in] | keymatch | 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 |
std::vector< std::pair< std::string, std::string > > mio::Config::getValuesRegex | ( | const std::string & | regex_str, |
std::string | section | ||
) | const |
bool mio::Config::keyExists | ( | std::string | key, |
std::string | section | ||
) | const |
Return if a given key exists in a given section (matching is case insensitive)
[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 |
bool mio::Config::keyExistsRegex | ( | std::string | key_pattern, |
std::string | section | ||
) | const |
void mio::Config::moveSection | ( | std::string | org, |
std::string | dest, | ||
const bool & | overwrite | ||
) |
Move all keys of the org section to the dest section.
[in] | org | Section of origin |
[in] | dest | Section of destination |
[in] | overwrite | if true, all keys in the destination section are erased before creating the new keys |
std::vector< std::pair< std::string, std::string > > mio::Config::parseArgs | ( | const std::string & | section, |
const std::string & | cmd_id, | ||
const unsigned int & | cmd_nr, | ||
const std::string & | arg_pattern | ||
) | const |
Extract the arguments for a given command and store them into a vector of key / value pairs.
The goal of this call is to provide an algorithm with easy to parse arguments, independent of the entry syntax. The syntax that is supported here is the following:
[in] | section | The section where to look for this command |
[in] | cmd_id | the command ID to process |
[in] | cmd_nr | the command number to process (most probably provided by a call to getCommandNr()) |
[in] | arg_pattern | as part of the argument definition |
bool mio::Config::sectionExists | ( | std::string | section | ) | const |
Return if a given section exists in the Config object.
[in] | section | std::string representing a section name |
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 |