Configuration Class Reference

Parse and hold configuration parameters from command line as well as from configuration files. More...

#include <configuration.h>

List of all members.

Public Types

typedef std::vector< OptionOptionList
typedef std::vector< OptionEntryEntryList

Public Member Functions

 Configuration ()
 ~Configuration ()
void addOption (const Option &)
 Add an option to the internal list.
bool setCommandLine (int argc, char **argv)
 Add an option to the internal list.
bool hasKey (const std::string &key) const
 Check if key is in the parsed options.
std::string value (const std::string &key) const
 Returns string representation of correponding value.
int intValue (const std::string &key) const
 Returns correponding value as int.
bool loadFile (const std::string &filename)
 Load a file.
void setHelpText (const std::string &text)
 Set an additional help text.
std::string helpText () const
 Return helptext containing all options.
void printEntries () const
 Helper: Print commandline entries to stdout.

Protected Member Functions

int findOption (const std::string &optionText) const
 Return index of given option.
int findShort (const std::string &optionText) const
 Return index of given short option.
int findLong (const std::string &optionText) const
 Return index of given long option.
int findEntry (const std::string &key) const
 Return index in entries of given key.
void insertEntry (const std::string &key, const std::string &value)
 Insert (or replace existing) key in entries.

Protected Attributes

OptionList m_option
 List of known options.
EntryList m_entry
 List of found options.
std::string m_helpText
 Additional help text.

Classes

class  Option
 Description of a command line option. More...
class  OptionEntry
 Description of a given commandline option. More...


Detailed Description

Parse and hold configuration parameters from command line as well as from configuration files.

Parse commandline parameters with setCommandLine. The parameter have to be in the GNU style short: -s or long: --long-parameter. The parameter are read from left to right. A key can only exist once. Duplicate keys override the previously defined. This class also can read configuration files containing key, value pairs separated with '='. The files my also contain comments. Lines beginning with '#' are treated as comments. Load files with loadFile.

Example:

##################################
# This is a comment
#
long-value = 2
string = "this is a string"

Definition at line 46 of file configuration.h.


Member Typedef Documentation

typedef std::vector<OptionEntry> Configuration::EntryList
 

Definition at line 110 of file configuration.h.

typedef std::vector<Option> Configuration::OptionList
 

Definition at line 109 of file configuration.h.


Constructor & Destructor Documentation

Configuration::Configuration  ) 
 

Definition at line 25 of file configuration.cpp.

Configuration::~Configuration  ) 
 

Definition at line 29 of file configuration.cpp.


Member Function Documentation

void Configuration::addOption const Option  ) 
 

Add an option to the internal list.

Only options added through this function will be accepted by readFile and setCommandLine.

Parameters:
option Option to be added

Definition at line 33 of file configuration.cpp.

References m_option.

Referenced by create_config().

int Configuration::findEntry const std::string &  key  )  const [protected]
 

Return index in entries of given key.

Parameters:
key key
Returns:
index of entry or -1 if not found

Definition at line 165 of file configuration.cpp.

References m_entry.

Referenced by insertEntry().

int Configuration::findLong const std::string &  optionText  )  const [protected]
 

Return index of given long option.

Parameters:
optionText long option
Returns:
index of option or -1 if not found

Definition at line 148 of file configuration.cpp.

References m_option.

Referenced by findOption().

int Configuration::findOption const std::string &  optionText  )  const [protected]
 

Return index of given option.

Parameters:
optionText option (may be long or short representation)
Returns:
index of option or -1 if not found

Definition at line 123 of file configuration.cpp.

References findLong(), and findShort().

Referenced by loadFile(), and setCommandLine().

int Configuration::findShort const std::string &  optionText  )  const [protected]
 

Return index of given short option.

Parameters:
optionText short option
Returns:
index of option or -1 if not found

Definition at line 131 of file configuration.cpp.

References m_option.

Referenced by findOption().

bool Configuration::hasKey const std::string &  key  )  const
 

Check if key is in the parsed options.

Parameters:
key Key to be checked
Returns:
true if key is in the internal list

Definition at line 177 of file configuration.cpp.

References m_entry.

std::string Configuration::helpText  )  const
 

Return helptext containing all options.

Definition at line 278 of file configuration.cpp.

References m_option.

void Configuration::insertEntry const std::string &  key,
const std::string &  value
[protected]
 

Insert (or replace existing) key in entries.

Parameters:
key key
value value

Definition at line 109 of file configuration.cpp.

References findEntry(), and m_entry.

Referenced by loadFile(), and setCommandLine().

int Configuration::intValue const std::string &  key  )  const
 

Returns correponding value as int.

Parameters:
key Key to be checked
Returns:
Assigned value tranformed to int value

Definition at line 202 of file configuration.cpp.

References Util::fromString(), and value().

bool Configuration::loadFile const std::string &  filename  ) 
 

Load a file.

Load a configuration file containing key, value pairs (or single keys). The keys must have been added with addOption before.

Parameters:
filename Filename of file to be loaded
Returns:
true if file could be loaded

Definition at line 211 of file configuration.cpp.

References findOption(), insertEntry(), m_option, Util::strip_whitespace(), and Util::tokenize().

void Configuration::printEntries  )  const
 

Helper: Print commandline entries to stdout.

Definition at line 268 of file configuration.cpp.

References m_entry.

bool Configuration::setCommandLine int  argc,
char **  argv
 

Add an option to the internal list.

Only options added through this function will be accepted by readFile and setCommandLine.

Parameters:
option Option to be added

Definition at line 38 of file configuration.cpp.

References findOption(), insertEntry(), m_option, and Util::tokenize().

Referenced by main().

void Configuration::setHelpText const std::string &  text  )  [inline]
 

Set an additional help text.

Definition at line 139 of file configuration.h.

References m_helpText.

Referenced by create_config().

std::string Configuration::value const std::string &  key  )  const
 

Returns string representation of correponding value.

Parameters:
key Key to be checked
Returns:
String representation of the assigned value

Definition at line 188 of file configuration.cpp.

References m_entry.

Referenced by intValue().


Member Data Documentation

EntryList Configuration::m_entry [protected]
 

List of found options.

Definition at line 151 of file configuration.h.

Referenced by findEntry(), hasKey(), insertEntry(), printEntries(), and value().

std::string Configuration::m_helpText [protected]
 

Additional help text.

Definition at line 153 of file configuration.h.

Referenced by setHelpText().

OptionList Configuration::m_option [protected]
 

List of known options.

Definition at line 149 of file configuration.h.

Referenced by addOption(), findLong(), findShort(), helpText(), loadFile(), and setCommandLine().


The documentation for this class was generated from the following files:
Generated on Mon Jan 22 23:24:18 2007 for cdmm by  doxygen 1.4.6