- Home
- About DUNE
- Download
- Documentation
- Community
- Development
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 | parseStream (std::istream &in, bool overwrite) |
parse C++ stream | |
void | parseStream (std::istream &in, const std::string srcname="stream", bool overwrite=true) |
parse C++ stream | |
void | parseFile (std::string file, bool overwrite=true) |
parse file | |
void | parseCmd (int argc, char *argv[]) |
parse command line | |
ConfigParser & | sub (const std::string &sub) |
get substructure by name | |
const ConfigParser & | sub (const std::string &sub) const |
get const substructure by name | |
bool | hasKey (const std::string &key) const |
test for key | |
bool | hasSub (const std::string &sub) const |
test for substructure | |
std::string & | operator[] (const std::string &key) |
get value reference for key | |
const std::string & | operator[] (const std::string &key) const |
get value reference for key | |
void | report (std::ostream &stream=std::cout, const std::string &prefix="") const |
print distinct substructure to stream | |
std::string | get (const std::string &key, const std::string &defaultValue) const |
get value as string | |
std::string | get (const std::string &key, const char *defaultValue) const |
get value as string | |
int | get (const std::string &key, int defaultValue) const |
get value as int | |
double | get (const std::string &key, double defaultValue) const |
get value as double | |
template<typename T > | |
T | get (const std::string &key, const T &defaultValue) const |
get value converted to a certain type | |
template<class T > | |
T | get (const std::string &key) const |
Get value. | |
const KeyVector & | getValueKeys () const |
get value keys | |
const KeyVector & | getSubKeys () const |
get substructure keys | |
Static Protected Member Functions | |
static std::string | ltrim (const std::string &s) |
static std::string | rtrim (const std::string &s) |
static std::vector< std::string > | split (const std::string &s) |
template<class Iterator > | |
static void | parseRange (const std::string &str, Iterator it, const Iterator &end) |
Protected Attributes | |
KeyVector | valueKeys |
KeyVector | subKeys |
std::map< std::string, std::string > | values |
std::map< std::string, ParameterTree > | subs |
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::ParameterTree::KeyVector [inherited] |
ConfigParser::ConfigParser | ( | ) |
Create new ConfigParser.
std::string ParameterTree::get | ( | const std::string & | key, | |
const std::string & | defaultValue | |||
) | const [inherited] |
get value as string
Returns pure string value for given key.
key | key name | |
defaultValue | default if key does not exist |
References Dune::ParameterTree::hasKey().
T Dune::ParameterTree::get | ( | const std::string & | key, | |
const T & | defaultValue | |||
) | const [inline, inherited] |
get value converted to a certain type
Returns value as type T for given key.
T | type of returned value. |
key | key name | |
defaultValue | default if key does not exist |
References Dune::ParameterTree::hasKey().
T Dune::ParameterTree::get | ( | const std::string & | key | ) | const [inline, inherited] |
Get value.
T | Type of the value |
key | Key name |
RangeError | if key does not exist | |
NotImplemented | Type is not supported |
References DUNE_THROW, Dune::ParameterTree::hasKey(), and Dune::ParameterTree::Parser< T >::parse().
std::string ParameterTree::get | ( | const std::string & | key, | |
const char * | defaultValue | |||
) | const [inherited] |
get value as string
Returns pure string value for given key.
key | key name | |
defaultValue | default if key does not exist |
References Dune::ParameterTree::hasKey().
int ParameterTree::get | ( | const std::string & | key, | |
int | defaultValue | |||
) | const [inherited] |
get value as int
Returns value for given key interpreted as int.
key | key name | |
defaultValue | default if key does not exist |
double ParameterTree::get | ( | const std::string & | key, | |
double | defaultValue | |||
) | const [inherited] |
get value as double
Returns value for given key interpreted as double.
key | key name | |
defaultValue | default if key does not exist |
References Dune::ParameterTree::hasKey().
const ParameterTree::KeyVector & ParameterTree::getSubKeys | ( | ) | const [inherited] |
get substructure keys
Returns a vector of all keys associated to (key,substructure) entries in order of appearance
References Dune::ParameterTree::subKeys.
const ParameterTree::KeyVector & ParameterTree::getValueKeys | ( | ) | const [inherited] |
get value keys
Returns a vector of all keys associated to (key,values) entries in order of appearance
References Dune::ParameterTree::valueKeys.
bool ParameterTree::hasKey | ( | const std::string & | key | ) | const [inherited] |
test for key
Tests whether given key exists.
key | key name |
References Dune::ParameterTree::hasKey(), Dune::ParameterTree::sub(), Dune::ParameterTree::subs, and Dune::ParameterTree::values.
Referenced by Dune::ParameterTree::get(), Dune::ParameterTree::hasKey(), Dune::ParameterTree::operator[](), parseStream(), and Dune::ParameterTreeParser::readINITree().
bool ParameterTree::hasSub | ( | const std::string & | sub | ) | const [inherited] |
test for substructure
Tests whether given substructure exists.
sub | substructure name |
References Dune::ParameterTree::hasSub(), Dune::ParameterTree::sub(), and Dune::ParameterTree::subs.
Referenced by Dune::ParameterTree::hasSub(), and Dune::ParameterTree::operator[]().
std::string ParameterTree::ltrim | ( | const std::string & | s | ) | [static, protected, inherited] |
std::string & ParameterTree::operator[] | ( | const std::string & | key | ) | [inherited] |
get value reference for key
Returns reference to value for given key name. This creates the key, if not existent.
key | key name |
References Dune::ParameterTree::hasKey(), Dune::ParameterTree::hasSub(), Dune::ParameterTree::sub(), Dune::ParameterTree::subKeys, Dune::ParameterTree::subs, Dune::ParameterTree::valueKeys, and Dune::ParameterTree::values.
const std::string & ParameterTree::operator[] | ( | const std::string & | key | ) | const [inherited] |
get value reference for key
Returns reference to value for given key name. This creates the key, if not existent.
key | key name |
Dune::RangeError | if key is not found |
References DUNE_THROW, Dune::ParameterTree::hasKey(), Dune::ParameterTree::hasSub(), Dune::ParameterTree::sub(), and Dune::ParameterTree::values.
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, | |
bool | overwrite = true | |||
) |
parse file
Parses file with given name and build hierarchical config structure.
file | filename | |
overwrite | Whether to overwrite already existing values. If false, values in the stream will be ignored if the key is already present. |
References DUNE_THROW, and parseStream().
static void Dune::ParameterTree::parseRange | ( | const std::string & | str, | |
Iterator | it, | |||
const Iterator & | end | |||
) | [inline, static, protected, inherited] |
References DUNE_THROW.
Referenced by Dune::ParameterTree::Parser< array< T, n > >::parse(), and Dune::ParameterTree::Parser< FieldVector< T, n > >::parse().
void ConfigParser::parseStream | ( | std::istream & | in, | |
const std::string | srcname = "stream" , |
|||
bool | overwrite = true | |||
) |
parse C++ stream
Parses C++ stream and build hierarchical config structure.
in | The stream to parse | |
srcname | Name of the configuration source for error messages, "stdin" or a filename. | |
overwrite | Whether to overwrite already existing values. If false, values in the stream will be ignored if the key is already present. |
References DUNE_THROW, Dune::ParameterTree::hasKey(), Dune::ParameterTree::ltrim(), and Dune::ParameterTree::rtrim().
void ConfigParser::parseStream | ( | std::istream & | in, | |
bool | overwrite | |||
) |
parse C++ stream
Parses C++ stream and build hierarchical config structure.
in | The stream to parse | |
overwrite | Whether to overwrite already existing values. If false, values in the stream will be ignored if the key is already present. |
Referenced by parseFile().
void ParameterTree::report | ( | std::ostream & | stream = std::cout , |
|
const std::string & | prefix = "" | |||
) | const [inherited] |
print distinct substructure to stream
Prints all entries with given prefix.
stream | Stream to print to | |
prefix | for key and substructure names |
References Dune::ParameterTree::subs, and Dune::ParameterTree::values.
std::string ParameterTree::rtrim | ( | const std::string & | s | ) | [static, protected, inherited] |
std::vector< std::string > ParameterTree::split | ( | const std::string & | s | ) | [static, protected, inherited] |
Referenced by Dune::ParameterTree::Parser< std::vector< T, A > >::parse().
ConfigParser & ConfigParser::sub | ( | const std::string & | sub | ) |
get substructure by name
sub | substructure name |
Reimplemented from Dune::ParameterTree.
Referenced by sub().
const ConfigParser & ConfigParser::sub | ( | const std::string & | sub | ) | const |
get const substructure by name
sub | substructure name |
Reimplemented from Dune::ParameterTree.
References sub().
KeyVector Dune::ParameterTree::subKeys [protected, inherited] |
Referenced by Dune::ParameterTree::getSubKeys(), Dune::ParameterTree::operator[](), and Dune::ParameterTree::sub().
std::map<std::string, ParameterTree> Dune::ParameterTree::subs [protected, inherited] |
KeyVector Dune::ParameterTree::valueKeys [protected, inherited] |
Referenced by Dune::ParameterTree::getValueKeys(), and Dune::ParameterTree::operator[]().
std::map<std::string, std::string> Dune::ParameterTree::values [protected, inherited] |
Referenced by Dune::ParameterTree::hasKey(), Dune::ParameterTree::operator[](), and Dune::ParameterTree::report().
Generated on Fri Apr 29 2011 with Doxygen (ver 1.7.1) [doxygen-log,error-log].