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:
- they consist of 0 or more explicitly indicated sections, which start with a sectionname enclosed in square brackets e.g. [General] or [Filter]
- within each section there are 0 or more key value pairs defined: KEY = VALUE
- in this implementation each key is unique within its section
- lines that start with a semicolon ';' or a hash sign '#' are ignored (regarded as comments)
- empty lines are ignored
- if there is no section name given in a file, the default section called "GENERAL" is assumed
- a VALUE for a KEY can consist of multiple whitespace separated values (e.g. MYNUMBERS = 17.77 -18.55 8888 99.99)
- special values: there is a special syntax to refer to environment variables, to other keys or to evaluate arithmetic expressions:
- environment variables are called by using the following syntax: ${env:my_env_var};
- refering to another key (it only needs to be defined at some point in the ini file, even in an included file is enough): ${other_key} or ${section::other_key} (make sure to prefix the key with its section if it refers to another section!)
- evaluating an arithmetic expression: ${{arithm. expression}}. It is possible to perform arithmetics on other variables: for example ${{ 10*${SAMPLING_RATE_MIN}+3*${MY_VAR} }} will be properly evaluated. Please note that in such a case, it is necessary to add white spaces around the arithmetic expression within its "{{" and "}}" markers so it is properly parsed.
- Note
- The arithemic expressions are evaluated thanks to the tinyexpr math library (under the zlib license) and can use standard operators (including "^"), standard functions (such as "sin", "sqrt", "ln", "log", "exp", "floor"...) as well as the "pi" and "e" constants.
Imports
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.
- IMPORT_BEFORE = {file and path to import}. This must take place before any non-import key or section header. This imports the specified file before processing the current file, allowing to overwrite the imported parameters in the current configuration file.
- IMPORT_AFTER = {file and path to import}. This can occur anywhere and imports the specified file after processing the current file, allowing to overwrite the local parameters by the imported parameters.
Exemples
[Input] ; this defines a section
InputFile = ./input/myfile.dat ; this defines a key "InputFile" with the associated value "./input/myfile.dat"
#oldInput = ./test/test.dat ; this is commented out
smart_read = false ; this defines the boolean key "smart_read"
fast_read = T ; this defines another boolean key, "fast_read"
user = ${env:LOGNAME} ; this uses the value of the environment variable "LOGNAME" for the key "user"
output_log = ${env:LOGNAME}_output.log ; we can even concatenate environment variables with other elements
ConfigBackup = ${Input::user}_${smart_read}.bak ; using other keys to build a value (the section reference can be omitted within the same section)
Target_rate = ${{24*3600}} ; arithmetic expression that will be evaluated when reading the key
|
| 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 |
|