Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

mpcl::util::prefs::TConfigProcessor Class Reference

#include <config_processor.hh>

Inheritance diagram for mpcl::util::prefs::TConfigProcessor:

mpcl::net::cgi::TConfigProcessor List of all members.

Public Types

typedef vector< TOptionTOptionVector
 Vector of options type.


Public Methods

 TConfigProcessor (void)
 Builds a new instance.

 TConfigProcessor (int iPARAMETER_COUNT, const char **ppkcPARAMETERS)
virtual ~TConfigProcessor (void)
 Destroyes the instance.

virtual void addOption (const TString &rkyNAME, const TString &rkySHORTCUT)
virtual void addOption (const TString &rkyNAME, const TString &rkySHORTCUT, const TString &rkyDEFAULT_VALUE)
void addEnvironmentVariable (const char *pkcENVIRONMENT_NAME, bool gREQUIRED=false) throw (TVariableNotFoundException)
virtual void processOptions (void)
 Loads options from configuration file and command-line, if any.

void setConfigFileName (const char *pkcCONFIG_FILE_NAME)
 Sets configuration file name.

void setCommandLineParameters (int iPARAMETER_COUNT, const char **ppkcPARAMETERS)
void setCurrentNameSpace (const TString &rkyNAME_SPACE)
void setUsageStringHeader (const TString &rkyUSAGE_HEADER)
 Sets the usage header string.

TString argumentValue (size_type zARGUMENT_INDEX) const
TString environmentValue (const char *pkcENVIRONMENT_NAME) const
bool hasArguments (void) const
size_type numberOfArguments (void) const
 Gets number of arguments in command line.

TOptionoperator[] (const char *pkcNAME) throw (TVariableNotFoundException)
const TOptionoperator[] (const char *pkcNAME) const throw (TVariableNotFoundException)
TOptionoptionWithName (const TString &rkyNAME)
 Gets option corresponding to name rkyNAME.

TString formatUsageString (void) const
 Print all options with it's help string, if any.

const TString programFileName (void) const
 Gets program name.

void saveConfigFile (void) const throw (TFileErrorException)
 Saves the configuration file with the current values.


Protected Methods

virtual void processCommandLine (void)
 Updates options with command-line settings.

virtual void processConfigFile (void)
 Updates options with configuration file settings.

void read (std::basic_istream< char_type, traits_type > &rtSOURCE_ISTREAM)
virtual TOptionupdateOptionWithName (const TString &rkyNAME, const TString &rkyVALUE)
virtual TOptionupdateOptionWithShortcut (const TString &rkySHORTCUT, const TString &rkyVALUE)
bool isThereOptionWithName (const char *pkcNAME) const
bool isThereOptionWithShortcut (const char *pkcSHORTCUT) const
unsigned int longestOptionLength (void) const
 Returns the longest string identifiying an option (needed for formatting).

TString nameSpace (const TString &rkyNAME) const
TString nameForShortcut (const TString &rkySHORTCUT) const
 Gets option name corresponding to shortcut rkySHORTCUT.

TString shortcutForName (const TString &rkyNAME) const
 Gets option command-line name corresponding to name rkyNAME.

void write (std::basic_ostream< char_type, traits_type > &rtTARGET_OSTREAM) const

Protected Attributes

bool gInputConfigFile
 Boolean that tells if there is a config-file.

bool gOptionsProcessed
 Boolean that tells if options have been processed.

int iParametersCount
const char ** ppkcParametersList
 Pointer to the array of command-line parameters.

vector< TString > tCommandLineArgumentsArray
TStringToStringMap tEnvironmentVariableMap
 Map with environment variables.

TOptionVector tOptionVector
std::set< TString > tValidNameSpaceSet
 Set of valid name spaces.

TString yConfigFileName
TString yCurrentNameSpace
TString yUsageHeader
 String with the header of the usage message.


Static Protected Attributes

const unsigned int _kuiMinPadding = 5
 Minimun padding size for Usage formatting.

const unsigned int _kuiTerminalWidth = 80
 Column width for Usage formatting.


Detailed Description

Configuration processor. Parses input from command-line, streams, and configuration files.

The configuration file is searched first in the current directory and then, if it isn't found, it is searched in the "home" directory of the user (using environment variable 'HOME').

All the possible options must be initially added to the instance with its name in the configuration file, its possible name in command-line, and its default value.

The option name and the option command-line name mustn't contain any punctuation character. The next regular expression, defines then valid set of characters:

[0-9a-zA-Z_.]+

This class makes distinctions between arguments (they don't begin with hyphen(s)), and options (that begin with one or two hyphens). The arguments can be positioned in any position in the command-line (even between options).

Option names are kept internally in lowercase. Lines in configuration files beginning with '#' are ignored.

Definition at line 81 of file util/prefs/config_processor.hh.


Constructor & Destructor Documentation

mpcl::util::prefs::TConfigProcessor::TConfigProcessor int    iPARAMETER_COUNT,
const char **    ppkcPARAMETERS
[inline]
 

Builds a new instance.

Parameters:
iPARAMETER_COUNT  Parameter count (in C language is argc).
ppkcPARAMETERS  Parameter values (in C language is argv).
See also:
argc, argv.

Reimplemented in mpcl::net::cgi::TConfigProcessor.

Definition at line 206 of file util/prefs/config_processor.hh.

References addEnvironmentVariable(), gInputConfigFile, gOptionsProcessed, iParametersCount, ppkcParametersList, programFileName(), tCommandLineArgumentsArray, tEnvironmentVariableMap, tOptionVector, tValidNameSpaceSet, yConfigFileName, yCurrentNameSpace, and yUsageHeader.


Member Function Documentation

void mpcl::util::prefs::TConfigProcessor::addEnvironmentVariable const char *    pkcENVIRONMENT_NAME,
bool    gREQUIRED = false
throw (TVariableNotFoundException)
 

Adds a new accessible environment variable. Only variables added with this member function, are recognized later.

If gREQUIRED is true and variable named pkcENVIRONMENT_NAME does not exist an exception is thrown, else if gREQUIRED is false, then if variable doesn't exist, it is asigned an empty value.

Definition at line 123 of file util/prefs/config_processor.cc.

References mpcl::text::Format(), and mpcl::text::TString.

Referenced by TConfigProcessor(), and mpcl::net::cgi::TConfigProcessor::TConfigProcessor().

void mpcl::util::prefs::TConfigProcessor::addOption const TString &    rkyNAME,
const TString &    rkySHORTCUT,
const TString &    rkyDEFAULT_VALUE
[virtual]
 

Adds a new option. Only options added with this member function, are recognized later.

Parameters:
rkyNAME  Name of the option.
rkySHORTCUT  Shortcut of the option.
rkyDEFAULT_VALUE  Default value of the option.
Precondition:
( !isThereOptionWithShortcut (rkySHORTCUT) )

Definition at line 81 of file util/prefs/config_processor.cc.

References mpcl::text::Format(), gOptionsProcessed, tOptionVector, mpcl::text::TString, and yCurrentNameSpace.

void mpcl::util::prefs::TConfigProcessor::addOption const TString &    rkyNAME,
const TString &    rkySHORTCUT
[virtual]
 

Adds a new option. Only options added with this member function, are recognized later.

Parameters:
rkyNAME  Name of the option.
rkySHORTCUT  Shortcut of the option.
Precondition:
( !isThereOptionWithShortcut (rkySHORTCUT) )

Definition at line 41 of file util/prefs/config_processor.cc.

References mpcl::text::Format(), gOptionsProcessed, tOptionVector, mpcl::text::TString, and yCurrentNameSpace.

TString mpcl::util::prefs::TConfigProcessor::argumentValue size_type    zARGUMENT_INDEX const [inline]
 

Gets argument value whose index is zARGUMENT_INDEX. Arguments don't include the program name.

Parameters:
zARGUMENT_INDEX  Argument index (from zero).
Returns:
The argument value.

Definition at line 371 of file util/prefs/config_processor.hh.

References tCommandLineArgumentsArray, and mpcl::text::TString.

TString mpcl::util::prefs::TConfigProcessor::environmentValue const char *    pkcENVIRONMENT_NAME const [inline]
 

Gets environment variable value for the one named pkcENVIRONMENT_NAME (must be added before).

Definition at line 380 of file util/prefs/config_processor.hh.

References tEnvironmentVariableMap, and mpcl::text::TString.

Referenced by mpcl::net::cgi::TAbstractApplication::fullName(), mpcl::net::cgi::TConfigProcessor::processOptions(), and mpcl::net::cgi::TAbstractApplication::TAbstractApplication().

bool mpcl::util::prefs::TConfigProcessor::hasArguments void    const [inline]
 

Returns true if has arguments in command line.

Returns:
True if numberOfArguments() is greater than 0.
See also:
numberOfArguments().

Definition at line 390 of file util/prefs/config_processor.hh.

References numberOfArguments().

bool mpcl::util::prefs::TConfigProcessor::isThereOptionWithName const char *    pkcNAME const [protected]
 

Returns true if there is an option named pkcNAME.

Parameters:
pkcNAME  Name of the option.
Returns:
True if there is an option named pkcNAME.

bool mpcl::util::prefs::TConfigProcessor::isThereOptionWithShortcut const char *    pkcSHORTCUT const [protected]
 

Returns true if there is an option with short-cut pkcSHORTCUT.

Parameters:
pkcSHORTCUT  Short-cut of the option.
Returns:
True if there is an option with short-cut pkcSHORTCUT.

mpcl::text::TString mpcl::util::prefs::TConfigProcessor::nameSpace const TString &    rkyNAME const [protected]
 

Returns the substring from the first character to the last occurrence of the dot character (but not including it). If the string has no name space, then it returns an empty string.

Parameters:
rkyNAME  Name.
Returns:
The name space part of the string.

Definition at line 429 of file util/prefs/config_processor.cc.

References mpcl::text::TString.

Referenced by read().

const mpcl::util::prefs::TOption & mpcl::util::prefs::TConfigProcessor::operator[] const char *    pkcNAME const throw (TVariableNotFoundException)
 

Gets option corresponding to name pkcNAME.

Parameters:
pkcNAME  Option name.
Returns:
The corresponding option.

Definition at line 444 of file util/prefs/config_processor.cc.

References mpcl::text::Format(), and mpcl::text::TString.

mpcl::util::prefs::TOption & mpcl::util::prefs::TConfigProcessor::operator[] const char *    pkcNAME throw (TVariableNotFoundException)
 

Gets option corresponding to name pkcNAME.

Parameters:
pkcNAME  Option name.
Returns:
The corresponding option.

Definition at line 474 of file util/prefs/config_processor.cc.

References mpcl::text::Format(), and mpcl::text::TString.

Referenced by optionWithName().

void mpcl::util::prefs::TConfigProcessor::read std::basic_istream< char_type, traits_type > &    rtSOURCE_ISTREAM [protected]
 

Reads the instance from stream rtSOURCE_ISTREAM.

Parameters:
rtSOURCE_ISTREAM  Source stream.

Definition at line 249 of file util/prefs/config_processor.cc.

References nameSpace(), mpcl::text::TString, tValidNameSpaceSet, updateOptionWithName(), and yConfigFileName.

Referenced by processConfigFile().

void mpcl::util::prefs::TConfigProcessor::setCommandLineParameters int    iPARAMETER_COUNT,
const char **    ppkcPARAMETERS
[inline]
 

Sets command-line parameters.

Parameters:
iPARAMETER_COUNT  Parameter count (in C language is argc).
ppkcPARAMETERS  Parameter values (in C language is argv).

Definition at line 286 of file util/prefs/config_processor.hh.

References gOptionsProcessed, iParametersCount, and ppkcParametersList.

void mpcl::util::prefs::TConfigProcessor::setCurrentNameSpace const TString &    rkyNAME_SPACE [inline]
 

Sets current name space. Any option added after calling this method, will be renamed prepending to its name, the string rkyNAME_SPACE plus a dot.

Parameters:
rkyNAME_SPACE  Name space.

Definition at line 305 of file util/prefs/config_processor.hh.

References mpcl::text::TString, tValidNameSpaceSet, and yCurrentNameSpace.

mpcl::util::prefs::TOption & mpcl::util::prefs::TConfigProcessor::updateOptionWithName const TString &    rkyNAME,
const TString &    rkyVALUE
[protected, virtual]
 

Updates option named rkyNAME with value rkyVALUE.

Parameters:
rkyNAME  Name of the option.
rkyVALUE  Value to assigned.
Returns:
The option that has been updated.

Reimplemented in mpcl::net::cgi::TConfigProcessor.

Definition at line 316 of file util/prefs/config_processor.cc.

References mpcl::text::Format(), tOptionVector, and mpcl::text::TString.

Referenced by processCommandLine(), and read().

mpcl::util::prefs::TOption & mpcl::util::prefs::TConfigProcessor::updateOptionWithShortcut const TString &    rkySHORTCUT,
const TString &    rkyVALUE
[protected, virtual]
 

Updates option with shortcut rkySHORTCUT, with value rkyVALUE.

Parameters:
rkySHORTCUT  Shortcut of the option.
rkyVALUE  Value to assigned.
Returns:
The option that has been updated.

Definition at line 349 of file util/prefs/config_processor.cc.

References mpcl::text::Format(), tOptionVector, and mpcl::text::TString.

Referenced by processCommandLine().

void mpcl::util::prefs::TConfigProcessor::write std::basic_ostream< char_type, traits_type > &    rtTARGET_OSTREAM const [protected]
 

Writes the instance onto stream rtTARGET_OSTREAM.

Parameters:
rtTARGET_OSTREAM  Target stream. @warn This method only writes valid name spaces.

Definition at line 603 of file util/prefs/config_processor.cc.

References tOptionVector.


Member Data Documentation

int mpcl::util::prefs::TConfigProcessor::iParametersCount [protected]
 

Integer with the number of command-line parameters. The term 'parameter' includes arguments and options.

Definition at line 114 of file util/prefs/config_processor.hh.

Referenced by processCommandLine(), mpcl::net::cgi::TConfigProcessor::processCommandLine(), setCommandLineParameters(), and TConfigProcessor().

vector<TString> mpcl::util::prefs::TConfigProcessor::tCommandLineArgumentsArray [protected]
 

Vector with command-line arguments. The arguments are taken from command-line and they are the parameters that aren't options.

For example:

executable_file --file=/etc/exec_conf main.c main.h

Where "main.c" and "main.h" are arguments.

Definition at line 129 of file util/prefs/config_processor.hh.

Referenced by argumentValue(), numberOfArguments(), processCommandLine(), and TConfigProcessor().

TOptionVector mpcl::util::prefs::TConfigProcessor::tOptionVector [protected]
 

Vector with options. Options are taken from command-line and from configuration file. From command-line they would appear as:

executable_file --file=/etc/exec_conf executable_file -f=/etc/exec_conf -f=/var/tmp

And from configuration file:

: file=/etc/exec_conf file=/var/tmp :

There can be quoted strings too:

: command="/usr/bin/ls -l" :

Definition at line 154 of file util/prefs/config_processor.hh.

Referenced by addOption(), formatUsageString(), mpcl::net::cgi::TConfigProcessor::hasOption(), longestOptionLength(), nameForShortcut(), shortcutForName(), TConfigProcessor(), updateOptionWithName(), mpcl::net::cgi::TConfigProcessor::updateOptionWithName(), updateOptionWithShortcut(), and write().

TString mpcl::util::prefs::TConfigProcessor::yConfigFileName [protected]
 

String with the name of the config-file (if any). If the file can't be opened, then it will be searched in the home directory (environment variable 'HOME').

Definition at line 164 of file util/prefs/config_processor.hh.

Referenced by processConfigFile(), read(), setConfigFileName(), and TConfigProcessor().

TString mpcl::util::prefs::TConfigProcessor::yCurrentNameSpace [protected]
 

String with current name space. It is empty when options are in the root name space).

Definition at line 170 of file util/prefs/config_processor.hh.

Referenced by addOption(), setCurrentNameSpace(), and TConfigProcessor().


The documentation for this class was generated from the following files:
Generated on Mon Oct 13 02:35:27 2003 for MPCL by doxygen1.2.18