OSB_LIB::CfgFile Class Reference

Collaboration diagram for OSB_LIB::CfgFile:

Collaboration graph
[legend]
List of all members.

Detailed Description

win.ini like configuration file.

A configuration file consists of sections containing key - value pairs or arbitrary text:

Sections
The begin of section is identified by a single line in the format [section-name], without leading/trailing white-space.
A section ends with:

Keys and values
A key-value pair consists of a single line and has the format key=value, without leading white-space and optional white-space around `='. A pair of double quotes (") enclosing value is removed.

Comments
Comments start with '#' and apply till the end of the line.

Remarks:
Modifications to an existing configuration file are made without a back-up copy. If desired applications should use link (man 2 link) or another appropriate method to keep a copy of the original file.

Definition at line 86 of file osbcfgfile.h.

Public Types

Public Member Functions

Static Public Member Functions

Private Member Functions

Copy construction and assignment not implemented:

Private Attributes


Member Enumeration Documentation

enum OSB_LIB::CfgFile::Flags

Behavior flags for nextLine(), getValue() and getSection().

Enumerator:
withComments  Do not remove comments.
withEmptyLines  Include empty lines.

Definition at line 89 of file osbcfgfile.h.


Constructor & Destructor Documentation

OSB_LIB::CfgFile::CfgFile (  ) 

Empty default constructor.

Clears flags_, i.e., sets them to 0.

OSB_LIB::CfgFile::CfgFile ( const std::string &  path  )  [explicit]

Constructor with path.

Opens the configuration file pointed to by path.

Parameters:
path Path of the configuration file.

OSB_LIB::CfgFile::~CfgFile (  ) 

Destructor: closes the input stream.

OSB_LIB::CfgFile::CfgFile ( const CfgFile  )  [private]


Member Function Documentation

bool OSB_LIB::CfgFile::addKey ( const std::string &  section,
const std::string &  key,
const std::string &  value 
)

Add or change a key/value pair.

Parameters:
section Name of section (without []).
key Key to add.
value Value for key, it may contain a comment.
Returns:
true in case of success, else false.
Adds a line key = value to section:
An existing key is overwritten, a new key is added at the end of section.

Remarks:
The function uses a temporary file "#path_.tmp" and -if otherwise successful- calls replace() at the end.

void OSB_LIB::CfgFile::close (  ) 

Close the input stream.

bool OSB_LIB::CfgFile::comments (  )  const

Include comments?

bool OSB_LIB::CfgFile::copy ( std::ofstream &  dest,
size_t  first,
size_t  last = (size_t)-1 
) [private]

Copy content of configuration file.

Parameters:
dest Output stream to write to.
first First line to copy.
last Last line to copy.
Returns:
true in case of success, false in case of an error.
Precondition:
is_ and dest must be open.
Postcondition:
If successful, curLine_ contains the last copied line.

const std::string& OSB_LIB::CfgFile::curLine (  )  const

Get the current line.

size_t OSB_LIB::CfgFile::curLineNo (  )  const

Get the current line number.

void OSB_LIB::CfgFile::delComment ( std::string &  line  )  const [private]

Strip comment from one line.

bool OSB_LIB::CfgFile::delKey ( const std::string &  section,
const std::string &  key 
)

Delete a key.

Parameters:
section Name of section (without []).
key Key to delete.
Returns:
true if the key does no more exist in the configuration file after the function returns, else false.
Remarks:
The function uses a temporary file "#path_.tmp" and -if otherwise successful- calls replace() at the end.

bool OSB_LIB::CfgFile::emptyLines (  )  const

Include empty lines?

bool OSB_LIB::CfgFile::eoSection (  )  const

Check if the end of a section has been reached.

int OSB_LIB::CfgFile::flags (  )  const

Get the flags.

bool OSB_LIB::CfgFile::getSection ( std::string &  dest,
const std::string &  section,
size_t *  lineNo = 0 
)

Get the contents of a whole section.

Parameters:
dest Gets the section's content.
section Name of section to retrieve (without []).
lineNo If not 0: gets the line number of the section's content.
Returns:
true in case of success, else false.

bool OSB_LIB::CfgFile::getValue ( std::string &  dest,
const std::string &  section,
const std::string &  key 
)

Get a configuration value.

Parameters:
dest Gets the key's value.
section Name of section with the key (without []).
key Key to search.
Returns:
true in case of success, else false.

bool OSB_LIB::CfgFile::gotoLine ( size_t  lineNo  ) 

Goto a line number.

bool OSB_LIB::CfgFile::isOpen (  ) 

Input stream open?

bool OSB_LIB::CfgFile::nextLine ( bool  applyFlags = true  ) 

Read the next line.

Parameters:
applyFlags Flag if flags_ should be applied.
Returns:
true if the next line was read, else false (as from is_ operator bool()).

bool OSB_LIB::CfgFile::open ( const std::string &  p  ) 

Open the input stream.

Parameters:
p Path to the configuration file.
Returns:
true in case of success, else false.
Remarks:
Closes any previously opened input stream.

CfgFile& OSB_LIB::CfgFile::operator= ( CfgFile  )  [private]

const char* OSB_LIB::CfgFile::path (  )  const

Path of configuration file.

bool OSB_LIB::CfgFile::replace ( const std::string &  replacePath  )  [private]

Replace configuration file.

Parameters:
replacePath Full path name of the replacing file.
Returns:
true in case of success, else false.
The function closes the current configuration file, renames replacePath to path_ and opens the new configuration file.

bool OSB_LIB::CfgFile::rewind (  ) 

Rewind to the beginning of the file.

Returns:
true if case of sucess, else false (as from is_ operator bool()).

static const std::string& OSB_LIB::CfgFile::sectionEnd (  )  [static]

End of section marker: "[end]".

bool OSB_LIB::CfgFile::seekKey ( const std::string &  section,
const std::string &  key 
)

Go to the line with a key.

Parameters:
section Name of section (without []).
key Key to search.
Returns:
true if key exists in section, else false. In case of success curLine_ contains the line with the searched key.

bool OSB_LIB::CfgFile::seekSection ( const std::string &  section  ) 

Go to the begin of section data.

Parameters:
section Name of section (without []).
Returns:
true if the section was found, else false.
If successful curLine_ contains the section marker after the function returns. The example below shows how all lines of a section can be processed:
          std::vector<std::string> content;
          CfgGile cfg("cfg.txt");
          if (!cfg.seekSection("domains")) {
            // error message, return
          }

          // copy all lines of the section into `content'
          while (cfg.nextLine() && !cfg.eoSection()) {
              content.push_back(cfg.curLine());
          }

void OSB_LIB::CfgFile::setFlags ( int  flags  ) 

Set the flags.

const std::ifstream& OSB_LIB::CfgFile::stream (  )  const

Read access to input stream.

void OSB_LIB::CfgFile::trimValue ( std::string &  value  ) 

Remove white-space and enclosing quotes from value.

Parameters:
value Value to trim.

bool OSB_LIB::CfgFile::wsOnly ( const std::string &  line  )  const [private]

Check if a line consists of white-space only.


Member Data Documentation

std::string OSB_LIB::CfgFile::curLine_ [private]

Content of the current line.

Definition at line 294 of file osbcfgfile.h.

size_t OSB_LIB::CfgFile::curLineNo_ [private]

Number of the current line.

Definition at line 296 of file osbcfgfile.h.

Flags OSB_LIB::CfgFile::flags_ [private]

Behaviour flags.

Definition at line 298 of file osbcfgfile.h.

std::ifstream OSB_LIB::CfgFile::is_ [private]

The input stream.

Definition at line 292 of file osbcfgfile.h.

std::string OSB_LIB::CfgFile::path_ [private]

Path of configuration file.

Definition at line 300 of file osbcfgfile.h.


The documentation for this class was generated from the following file:
Generated on Sat Sep 2 14:11:53 2006 for OSB Library by  doxygen 1.4.7