Dune::ConfigParser Class Reference
[Common]

#include <configparser.hh>

List of all members.


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

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 KeyVector & getValueKeys () const
 get value keys
const KeyVector & getSubKeys () const
 get substructure keys


Member Function Documentation

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::parseCmd ( int  argc,
char *  argv[] 
)

parse command line

Parses command line options and build hierarchical config structure.

Parameters:
argc arg count
argv arg values

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

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

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

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

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

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

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'

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

Get value.

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

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

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


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

Generated on Thu Apr 2 10:39:54 2009 for dune-common by  doxygen 1.5.6