MeteoIODoc  MeteoIODoc-2.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mio::Config Class Reference

Detailed Description

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)

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.

  • 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.
Author
Thomas Egger & Mathias Bavay
Date
2008-11-30

#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 &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 (const std::string &key, const std::string &section)
 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 &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 (const std::string &key, const std::string &section) 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 &section, 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 &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...
 
ConfigProxy get (const std::string &key, const IOUtils::ThrowOptions &opt=IOUtils::dothrow) const
 
ConfigProxy get (const std::string &key, const std::string &section, 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 &section, 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 &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 (const std::string &keystart, const std::string &section, 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)
 

Constructor & Destructor Documentation

mio::Config::Config ( )

Empty constructor. The user MUST later one fill the internal key/value map object.

virtual mio::Config::~Config ( )
inlinevirtual
mio::Config::Config ( const std::string &  filename_in)

Main constructor. The file is parsed and a key/value map object is internally created.

Parameters
[in]filename_instring representing the absolute filename of the key/value file

Member Function Documentation

void mio::Config::addCmdLine ( const std::string &  cmd_line)

Add the content of the given command line to the internal key/value map object.

Parameters
[in]cmd_linestring 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.

Parameters
[in]filename_instring 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.

Parameters
[in]keystring representing the key to be added
[in]sectionstd::string representing a section name; the key has to be part of this section
[in]valuestring 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.

Parameters
[in]keystring representing the key to be added
[in]sectionstd::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.

Parameters
[in]keymatchA string representing the beginning of a key to search for
[in]sectionA string defining which section to search through (default: GENERAL)
[in]anywhereMatch 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.

Parameters
[out]vecResultA vector that will hold all keys that partially match keystart
[in]keymatchA string representing the beginning of a key to search for
[in]sectionA string defining which section to search through (default: GENERAL)
[in]anywhereMatch 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)

Parameters
[in]keystd::string representing a KEY in the key/value file (default section "GENERAL" is assumed)
[in]optindicating whether an exception should be raised, when key is not present
Returns
A value of type T
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)

Parameters
[in]keystd::string representing a KEY in the key/value file (default section "GENERAL" is assumed)
[in]sectionstd::string representing a section name; the key has to be part of this section
[in]optindicating whether an exception should be raised, when key is not present
Returns
A value of type T

Example Usage:

Config cfg("io.ini");
vector<int> = cfg.get("DEPTHS", "INPUT", IOUtils::nothrow);
string mystr = cfg.get("PATH", "OUTPUT");
std::string mio::Config::getConfigRootDir ( ) const

Returns the directory where the root configuration file is (needed to resolv relative paths).

Returns
The absolute path to the root config file (resolved for symlinks, relative paths, etc).
std::string mio::Config::getSourceName ( ) const

Returns the filename that the Config object was constructed with.

Returns
The absolute filename of the key/value file.
template<typename T >
std::vector<T> mio::Config::getValue ( const std::string &  key,
const std::string &  section,
const IOUtils::ThrowOptions opt = IOUtils::dothrow 
) const
inline
template<typename T >
void mio::Config::getValue ( const std::string &  key,
std::vector< T > &  vecT,
const IOUtils::ThrowOptions opt = IOUtils::dothrow 
) const
inline

Template function to retrieve a vector of values of class T for a certain key.

algorithms = lsm linregres idw_kriging\n
MYNUMBERS = 17.87 19.89 -19.89 +7777.007
Parameters
[in]keystd::string representing a KEY in the key/value file (default section "GENERAL" is assumed)
[out]vecTa variable of class vector<T> into which the values for the corresponding key are saved
[in]optindicating whether an exception should be raised, when key is not present
template<typename T >
void mio::Config::getValue ( const std::string &  key,
const std::string &  section,
std::vector< T > &  vecT,
const IOUtils::ThrowOptions opt = IOUtils::dothrow 
) const
inline

Template function to retrieve a vector of values of class T for a certain key.

algorithms = lsm linregres idw_kriging\n
MYNUMBERS = 17.87 19.89 -19.89 +7777.007
Parameters
[in]keystd::string representing a KEY in the key/value file
[in]sectionstd::string representing a section name; the key has to be part of this section
[out]vecTa variable of class vector<T> into which the values for the corresponding key are saved
[in]optindicating whether an exception should be raised, when key is not present
template<typename T >
void mio::Config::getValue ( const std::string &  key,
T &  t,
const IOUtils::ThrowOptions opt = IOUtils::dothrow 
) const
inline

Template function to retrieve a value of class T for a certain key.

Parameters
[in]keystd::string representing a KEY in the key/value file (default section "GENERAL" is assumed)
[out]ta variable of class T into which the value for the corresponding key is saved (e.g. double, int, std::string)
[in]optindicating whether an exception should be raised, when key is not present
template<typename T >
void mio::Config::getValue ( const std::string &  key,
const std::string &  section,
T &  t,
const IOUtils::ThrowOptions opt = IOUtils::dothrow 
) const
inline

Template function to retrieve a value of class T for a certain key.

Parameters
[in]keystd::string representing a KEY in the key/value file
[in]sectionstd::string representing a section name; the key has to be part of this section
[out]ta variable of class T into which the value for the corresponding key is saved (e.g. double, int, std::string)
[in]optindicating whether an exception should be raised, when key is not present
template<typename T >
void mio::Config::getValues ( const std::string &  keystart,
const std::string &  section,
std::vector< T > &  vecT 
) const
inline

Template function to retrieve a vector of values of class T for a certain key pattern.

Parameters
[in]keystartstd::string representing a pattern for the key in the key/value file
[in]sectionstd::string representing a section name; the key has to be part of this section
[out]vecTa vector of class T into which the values for the corresponding keys are saved
template<typename T >
void mio::Config::getValues ( const std::string &  keystart,
const std::string &  section,
std::vector< T > &  vecT,
std::vector< std::string > &  vecKeys 
) const
inline
bool mio::Config::keyExists ( const std::string &  key,
const std::string &  section 
) const

Return if a given key exists in a given section.

Parameters
[in]keystring representing the key to be searched
[in]sectionstd::string representing a section name; the key has to be part of this section
Returns
true if the key exists
const std::string mio::Config::toString ( ) const

Print the content of the Config object (usefull for debugging) The Config is bound by "<Config>" and "</Config>" on separate lines.

void mio::Config::write ( const std::string &  filename) const

Write the Config object to a file.

Parameters
filenameThe filename including the path, e.g. "/tmp/test.ini"

Friends And Related Function Documentation

std::iostream& operator<< ( std::iostream &  os,
const Config cfg 
)
friend
std::iostream& operator>> ( std::iostream &  is,
Config cfg 
)
friend

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