Dune::ConfigParser Class Reference
[Common (foundation classes)]

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>

List of all members.

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
ConfigParsersub (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 >
get (const std::string &key)
 Get value.
const KeyVectorgetValueKeys () const
 get value keys
const KeyVectorgetSubKeys () const
 get substructure keys

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::ConfigParser::KeyVector

Constructor & Destructor Documentation

ConfigParser::ConfigParser (  ) 

Create new ConfigParser.


Member Function Documentation

bool Dune::ConfigParser::get< bool > ( const std::string &  key  )  [inline]

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
bool ConfigParser::get ( const std::string &  key,
bool  defaultValue 
)

get value as bool

Returns value for given key interpreted as bool.

Parameters:
key key name
defaultValue default if key does not exist
Returns:
value as bool, false if values = '0', true if value = '1'
double ConfigParser::get ( const std::string &  key,
double  defaultValue 
)

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
int ConfigParser::get ( const std::string &  key,
int  defaultValue 
)

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
string ConfigParser::get ( const std::string &  key,
const char *  defaultValue 
)

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 hasKey().

string ConfigParser::get ( const std::string &  key,
const std::string &  defaultValue 
)

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 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

Returns:
reference to entry vector
const ConfigParser::KeyVector & ConfigParser::getValueKeys (  )  const

get value keys

Returns a vector of all keys associated to (key,values) entries in order of appearance

Returns:
reference to entry vector
bool ConfigParser::hasKey ( const std::string &  key  ) 

test for key

Tests wether given key exists.

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

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.

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

References hasSub(), and sub().

Referenced by hasSub(), and operator[]().

string & ConfigParser::operator[] ( const std::string &  key  ) 

get value reference for key

Returns refference to value for given key name. This creates the key, if not existent.

Parameters:
key key name
Returns:
reference to coresponding value

References hasKey(), hasSub(), and sub().

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  ) 

parse file

Parses file with given name and build hierarchical config structure.

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

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

Parameters:
sub substructure name
Returns:
reference to substructure

References sub().

Referenced by hasKey(), hasSub(), operator[](), and sub().


The documentation for this class was generated from the following files:
Generated on Mon Apr 26 10:45:23 2010 for dune-common by  doxygen 1.6.3