Parser for hierarchical configuration filesThis class parses config files into a hierarchical structure. Config files should look like this. More...
#include <dune/common/configparser.hh>
Public Types | |
typedef std::vector< std::string > | KeyVector |
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 |
Parser for hierarchical configuration files
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'. Leading and trailing spaces and tabs are removed from the values unless you use single or double quotes around them. Using single or double quotes you can also have multiline values.
typedef std::vector<std::string> Dune::ConfigParser::KeyVector |
ConfigParser::ConfigParser | ( | ) |
Create new ConfigParser.
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 |
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 |
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 |
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 |
string ConfigParser::get | ( | const std::string & | key, | |
const char * | defaultValue | |||
) |
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().
const ConfigParser::KeyVector & ConfigParser::getSubKeys | ( | ) | const |
get substructure keys
Returns a vector of all keys associated to (key,substructure) entries in order of appearance
const ConfigParser::KeyVector & ConfigParser::getValueKeys | ( | ) | const |
get value keys
Returns a vector of all keys associated to (key,values) entries in order of appearance
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::parseCmd | ( | int | argc, | |
char * | argv[] | |||
) |
parse command line
Parses command line options and build hierarchical config structure.
argc | arg count | |
argv | arg values |
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::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().
void ConfigParser::report | ( | ) | const |
print structure to std::cout
Referenced by report().
ConfigParser & ConfigParser::sub | ( | const std::string & | sub | ) |
get substructure by name
sub | substructure name |
References sub().
Referenced by hasKey(), hasSub(), operator[](), and sub().