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>

Inheritance diagram for Dune::ConfigParser:
Inheritance graph

List of all members.

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
ConfigParsersub (const std::string &sub)
 get substructure by name
const ConfigParsersub (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 >
get (const std::string &key, const T &defaultValue) const
 get value converted to a certain type
template<class T >
get (const std::string &key) const
 Get value.
const KeyVectorgetValueKeys () const
 get value keys
const KeyVectorgetSubKeys () 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

Detailed Description

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.


Member Typedef Documentation

typedef std::vector<std::string> Dune::ParameterTree::KeyVector [inherited]

Constructor & Destructor Documentation

ConfigParser::ConfigParser (  ) 

Create new ConfigParser.


Member Function Documentation

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.

Parameters:
key key name
defaultValue default if key does not exist
Returns:
value as string

References Dune::ParameterTree::hasKey().

template<typename T >
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.

Template Parameters:
T type of returned value.
Parameters:
key key name
defaultValue default if key does not exist
Returns:
value converted to T

References Dune::ParameterTree::hasKey().

template<class T >
T Dune::ParameterTree::get ( const std::string &  key  )  const [inline, inherited]

Get value.

Template Parameters:
T Type of the value
Parameters:
key Key name
Exceptions:
RangeError if key does not exist
NotImplemented Type is not supported
Returns:
value as T

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.

Todo:
This is a hack so get("my_key", "xyz") compiles (without this method "xyz" resolves to bool instead of std::string)
Parameters:
key key name
defaultValue default if key does not exist
Returns:
value as string

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.

Parameters:
key key name
defaultValue default if key does not exist
Returns:
value as int
double ParameterTree::get ( const std::string &  key,
double  defaultValue 
) const [inherited]

get value as double

Returns value for given key interpreted as double.

Parameters:
key key name
defaultValue default if key does not exist
Returns:
value as double

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

Returns:
reference to entry vector

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

Returns:
reference to entry vector

References Dune::ParameterTree::valueKeys.

bool ParameterTree::hasKey ( const std::string &  key  )  const [inherited]

test for key

Tests whether given key exists.

Parameters:
key key name
Returns:
true if key exists in structure, otherwise false

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.

Parameters:
sub substructure name
Returns:
true if substructure exists in structure, otherwise false

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.

Parameters:
key key name
Returns:
reference to corresponding value

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.

Parameters:
key key name
Returns:
reference to corresponding value
Exceptions:
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.

Parameters:
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.

Parameters:
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().

template<class Iterator >
static void Dune::ParameterTree::parseRange ( const std::string &  str,
Iterator  it,
const Iterator &  end 
) [inline, static, protected, inherited]
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.

Parameters:
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.

Parameters:
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.
Note:
This method is identical to parseStream(std::istream&, const std::string&, bool) with the exception that that method allows to give a custom name for the stream.

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.

Parameters:
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]
ConfigParser & ConfigParser::sub ( const std::string &  sub  ) 

get substructure by name

Parameters:
sub substructure name
Returns:
reference to substructure
Deprecated:
Use the corresponding method in ParameterTree, which returns a ParameterTree
Deprecated:

Reimplemented from Dune::ParameterTree.

Referenced by sub().

const ConfigParser & ConfigParser::sub ( const std::string &  sub  )  const

get const substructure by name

Parameters:
sub substructure name
Returns:
reference to substructure
Deprecated:
Use the corresponding method in ParameterTree, which returns a ParameterTree
Deprecated:

Reimplemented from Dune::ParameterTree.

References sub().


Member Data Documentation

std::map<std::string, std::string> Dune::ParameterTree::values [protected, inherited]

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

Generated on Fri Apr 29 2011 with Doxygen (ver 1.7.1) [doxygen-log,error-log].