#include <configparser.hh>
This class parses config files into a hierarchical structure. Config files should look like this.
# this file configures fruit colors in fruitsalad #these are no fruit but could also appear in fruit salad honeydewmelon = yellow watermelon = green fruit.tropicalfruit.orange = orange [fruit] strawberry = red pomegranate = red [fruit.pipfruit] apple = green/red/yellow pear = green [fruit.stonefruit] cherry = red plum = purple
If a '[prefix]' statement appears all following entries use this prefix until the next '[prefix]' statement. Fruitsalads for example contain:
honeydewmelon = yellow fruit.tropicalfruit.orange = orange fruit.pipfruit.apple = green/red/yellow fruit.stonefruit.cherry = red
All keys with a common 'prefix.' belong to the same substructure called 'prefix'.
Public Member Functions | |
ConfigParser () | |
Create new ConfigParser. | |
void | parseFile (std::string file) |
parse file | |
void | parseCmd (int argc, char *argv[]) |
parse command line | |
bool | hasKey (const std::string &key) |
test for key | |
bool | hasSub (const std::string &sub) |
test for substructure | |
std::string & | operator[] (const std::string &key) |
get value reference for key | |
void | report () const |
print structure to std::cout | |
void | report (const std::string prefix) const |
print distinct substructure to std::cout | |
ConfigParser & | sub (const std::string &sub) |
get substructure by name | |
std::string | get (const std::string &key, const std::string &defaultValue) |
get value as string | |
std::string | get (const std::string &key, const char *defaultValue) |
get value as string | |
int | get (const std::string &key, int defaultValue) |
get value as int | |
double | get (const std::string &key, double defaultValue) |
get value as double | |
bool | get (const std::string &key, bool defaultValue) |
get value as bool | |
template<class T> | |
T | get (const std::string &key) |
Get value. | |
const KeyVector & | getValueKeys () const |
get value keys | |
const KeyVector & | getSubKeys () const |
get substructure keys |
void ConfigParser::parseFile | ( | std::string | file | ) |
parse file
Parses file with given name and build hierarchical config structure.
file | filename |
References DUNE_THROW.
void ConfigParser::parseCmd | ( | int | argc, | |
char * | argv[] | |||
) |
parse command line
Parses command line options and build hierarchical config structure.
argc | arg count | |
argv | arg values |
bool ConfigParser::hasKey | ( | const std::string & | key | ) |
test for key
Tests wether given key exists.
key | key name |
References hasKey(), and sub().
Referenced by get(), hasKey(), and operator[]().
bool ConfigParser::hasSub | ( | const std::string & | sub | ) |
test for substructure
Tests wether given substructure exists.
sub | substructure name |
References hasSub(), and sub().
Referenced by hasSub(), and operator[]().
string & ConfigParser::operator[] | ( | const std::string & | key | ) |
void ConfigParser::report | ( | const std::string | prefix | ) | const |
print distinct substructure to std::cout
Prints all entries with given prefix.
prefix | for key and substructure names |
References report().
ConfigParser & ConfigParser::sub | ( | const std::string & | sub | ) |
get substructure by name
sub | substructure name |
References sub().
Referenced by hasKey(), hasSub(), operator[](), and sub().
string ConfigParser::get | ( | const std::string & | key, | |
const std::string & | defaultValue | |||
) |
get value as string
Returns pure string value for given key.
key | key name | |
defaultValue | default if key does not exist |
References hasKey().
string ConfigParser::get | ( | const std::string & | key, | |
const char * | defaultValue | |||
) |
int ConfigParser::get | ( | const std::string & | key, | |
int | defaultValue | |||
) |
get value as int
Returns value for given key interpreted as int.
key | key name | |
defaultValue | default if key does not exist |
double ConfigParser::get | ( | const std::string & | key, | |
double | defaultValue | |||
) |
get value as double
Returns value for given key interpreted as double.
key | key name | |
defaultValue | default if key does not exist |
bool ConfigParser::get | ( | const std::string & | key, | |
bool | defaultValue | |||
) |
get value as bool
Returns value for given key interpreted as bool.
key | key name | |
defaultValue | default if key does not exist |
bool Dune::ConfigParser::get< bool > | ( | const std::string & | key | ) | [inline] |
Get value.
T | Type of the value | |
key | Key name |
RangeError | if key does not exist | |
NotImplemented | Type is not supported |
const ConfigParser::KeyVector & ConfigParser::getValueKeys | ( | ) | const |
get value keys
Returns a vector of all keys associated to (key,values) entries in order of appearance
const ConfigParser::KeyVector & ConfigParser::getSubKeys | ( | ) | const |
get substructure keys
Returns a vector of all keys associated to (key,substructure) entries in order of appearance