libassa  3.5.1
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
ASSA::IniFile Class Reference

#include <IniFile.h>

Public Types

typedef pair< string, string > tuple_type
 A tuple is a name/value pair. More...
 
typedef pair< string, list< tuple_type > > sect_type
 A section is a logical subcategory of related configuration information. More...
 
typedef list< sect_typeconfig_type
 INI configuration is the collection of sections. More...
 
typedef config_type::iterator config_iterator
 Mutable iterator over the list of configuration sections. More...
 
typedef config_type::const_iterator const_config_iterator
 Constant iterator over the list of configuration sections. More...
 
typedef list< tuple_type >::iterator tuple_iterator
 Mutable iterator over name/value pairs in a section. More...
 
typedef list< tuple_type >::const_iterator const_tuple_iterator
 Constant iterator over name/value pairs in a section. More...
 

Public Member Functions

 IniFile (const string &fname_)
 Do-nothing constructor. More...
 
 ~IniFile ()
 Destructor does not save cache data to the file. More...
 
bool operator== (const IniFile &rhs_) const
 Compare two configurations. More...
 
bool operator!= (const IniFile &rhs_) const
 Compare two configurations. More...
 
int load ()
 Load configuration data from the file. More...
 
void drop_all ()
 Clear up configuration cache. More...
 
int sync ()
 Write cached configuration to the file. More...
 
int sync (const string &fname_)
 Write cached configuration to the file fname_. More...
 
void add_section (const string &section_)
 Add new section. More...
 
int drop_section (const string &section_)
 Remove section from cache. More...
 
int set_pair (const string &section_, const tuple_type &newkey_)
 Add or change name/value pair in the section. More...
 
int drop_pair (const string &section_, const string &name_)
 Remove name/value pair from the section in cache. More...
 
string get_value (const string &section_, const string &name_) const
 Find and return a value of the name/value pair in the section section_. More...
 
config_iterator find_section (const string &section_)
 Find section by its name. More...
 
const_config_iterator find_section (const string &section_) const
 Find section by its name. More...
 
const_config_iterator sect_begin () const
 Return iterator to the first section. More...
 
config_iterator sect_end ()
 Return iterator past the last section. More...
 
unsigned int size () const
 Return number of sections in the cache. More...
 
void dump () const
 Dump cache to the log file. More...
 

Private Member Functions

int trim_section_name (string &text_)
 Remove square brakets around section name. More...
 

Private Attributes

string m_fname
 INI file name. More...
 
std::fstream m_stream
 File stream. More...
 
config_type m_config
 Cache holds the entire INI file in memory. More...
 
Regexp m_section_pttrn
 Section header match. More...
 
Regexp m_tuple_pttrn
 Name/value pair match. More...
 
Regexp m_comment_pttrn
 Comment match. More...
 

Detailed Description

Definition at line 39 of file IniFile.h.

Member Typedef Documentation

◆ config_iterator

typedef config_type::iterator ASSA::IniFile::config_iterator

Mutable iterator over the list of configuration sections.

Definition at line 56 of file IniFile.h.

◆ config_type

INI configuration is the collection of sections.

Definition at line 53 of file IniFile.h.

◆ const_config_iterator

typedef config_type::const_iterator ASSA::IniFile::const_config_iterator

Constant iterator over the list of configuration sections.

Definition at line 59 of file IniFile.h.

◆ const_tuple_iterator

typedef list<tuple_type>::const_iterator ASSA::IniFile::const_tuple_iterator

Constant iterator over name/value pairs in a section.

Definition at line 65 of file IniFile.h.

◆ sect_type

typedef pair<string, list<tuple_type> > ASSA::IniFile::sect_type

A section is a logical subcategory of related configuration information.

Definition at line 49 of file IniFile.h.

◆ tuple_iterator

typedef list<tuple_type>::iterator ASSA::IniFile::tuple_iterator

Mutable iterator over name/value pairs in a section.

Definition at line 62 of file IniFile.h.

◆ tuple_type

typedef pair<string, string> ASSA::IniFile::tuple_type

A tuple is a name/value pair.

Definition at line 44 of file IniFile.h.

Constructor & Destructor Documentation

◆ IniFile()

ASSA::IniFile::IniFile ( const string &  fname_)

Do-nothing constructor.

Parameters
fname_Name of the INI file

◆ ~IniFile()

ASSA::IniFile::~IniFile ( )

Destructor does not save cache data to the file.

You should explicitly call sync() if you want your data to be saved to the file.

Member Function Documentation

◆ add_section()

void ASSA::IniFile::add_section ( const string &  section_)

Add new section.

Parameters
section_Section name to add

◆ drop_all()

void ASSA::IniFile::drop_all ( )
inline

Clear up configuration cache.

Definition at line 100 of file IniFile.h.

100 { m_config.clear (); }
config_type m_config
Cache holds the entire INI file in memory.
Definition: IniFile.h:199

References m_config.

◆ drop_pair()

int ASSA::IniFile::drop_pair ( const string &  section_,
const string &  name_ 
)

Remove name/value pair from the section in cache.

Parameters
section_Section that holds name/value pair.
name_Name part of name/value pair.
Returns
0 on success; -1 if section_ or name_ was not found

◆ drop_section()

int ASSA::IniFile::drop_section ( const string &  section_)

Remove section from cache.

Parameters
section_Section to remove
Returns
0 on success; -1 if section was not found

◆ dump()

void ASSA::IniFile::dump ( ) const

Dump cache to the log file.

◆ find_section() [1/2]

config_iterator ASSA::IniFile::find_section ( const string &  section_)

Find section by its name.

Parameters
section_Section name to earch for
Returns
An iterator pointing to sect_type of the section if it is found or pointing to sect_end() if not.

Referenced by ASSA::CmdLineOpts::parse_config_file().

◆ find_section() [2/2]

const_config_iterator ASSA::IniFile::find_section ( const string &  section_) const

Find section by its name.

Parameters
section_Section name to earch for
Returns
An iterator pointing to the sect_type of the section if it is found or pointing to sect_end() if not.

◆ get_value()

string ASSA::IniFile::get_value ( const string &  section_,
const string &  name_ 
) const

Find and return a value of the name/value pair in the section section_.

Parameters
section_Section name to search for name/value
name_Name part of name/value pair
Returns
value part of name/value; or an empty string if not found.

Referenced by ASSA::CmdLineOpts::parse_config_file().

◆ load()

int ASSA::IniFile::load ( )

Load configuration data from the file.

The file name is specified in the constructor.

Returns
0 on success; -1 if there was a syntax error.

◆ operator!=()

bool ASSA::IniFile::operator!= ( const IniFile rhs_) const
inline

Compare two configurations.

Returns
true if two configurations are the same; false otherwise.

Definition at line 88 of file IniFile.h.

89  { return (! (*this == rhs_)); }

◆ operator==()

bool ASSA::IniFile::operator== ( const IniFile rhs_) const
inline

Compare two configurations.

Returns
true if two configurations are the same; false otherwise.

Definition at line 82 of file IniFile.h.

83  { return (m_config == rhs_.m_config); }

References m_config.

◆ sect_begin()

const_config_iterator ASSA::IniFile::sect_begin ( ) const
inline

Return iterator to the first section.

Definition at line 170 of file IniFile.h.

170 { return m_config.begin (); }

References m_config.

◆ sect_end()

config_iterator ASSA::IniFile::sect_end ( )
inline

Return iterator past the last section.

Definition at line 174 of file IniFile.h.

174 { return m_config.end (); }

References m_config.

Referenced by ASSA::CmdLineOpts::parse_config_file().

◆ set_pair()

int ASSA::IniFile::set_pair ( const string &  section_,
const tuple_type newkey_ 
)

Add or change name/value pair in the section.

Parameters
section_Section name
newkey_Name/value pair
Returns
0 on success; -1 if section is not found

◆ size()

unsigned int ASSA::IniFile::size ( ) const
inline

Return number of sections in the cache.

Definition at line 178 of file IniFile.h.

178 { return m_config.size (); }

References m_config.

◆ sync() [1/2]

int ASSA::IniFile::sync ( )
inline

Write cached configuration to the file.

Filename used is the one given in the constructor.

Returns
0 on success; -1 if opening/writing to the file failed.

Definition at line 218 of file IniFile.h.

220  {
221  trace_with_mask ("IniFile::sync", INIFILE);
222  return sync (m_fname);
223  }
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
int sync()
Write cached configuration to the file.
Definition: IniFile.h:219
string m_fname
INI file name.
Definition: IniFile.h:193
@ INIFILE
Class IniFile messages
Definition: LogMask.h:52

References ASSA::INIFILE, m_fname, and trace_with_mask.

◆ sync() [2/2]

int ASSA::IniFile::sync ( const string &  fname_)

Write cached configuration to the file fname_.

Parameters
fname_Name of the output file.
Returns
0 on success; -1 if opening/writing to the file failed.

◆ trim_section_name()

int ASSA::IniFile::trim_section_name ( string &  text_)
inlineprivate

Remove square brakets around section name.

Parameters
text_(IN/OUT) String to work on
Returns
0 on success; -1 on error

Definition at line 211 of file IniFile.h.

213  {
214  return (Utils::ltrim (text_, "[") == 0 &&
215  Utils::rtrim (text_, "]") == 0) ? 0 : -1;
216  }
int ltrim(std::string &text_, const std::string &delim_)
Trim string from the beginning to the left of the delimiter.
Definition: CommonUtils.cpp:67
int rtrim(std::string &text_, const std::string &delim_)
Trim string from the delimiter to the end of the string.
Definition: CommonUtils.cpp:80

References ASSA::Utils::ltrim(), and ASSA::Utils::rtrim().

Member Data Documentation

◆ m_comment_pttrn

Regexp ASSA::IniFile::m_comment_pttrn
private

Comment match.

Definition at line 208 of file IniFile.h.

◆ m_config

config_type ASSA::IniFile::m_config
private

Cache holds the entire INI file in memory.

Definition at line 199 of file IniFile.h.

Referenced by drop_all(), operator==(), sect_begin(), sect_end(), and size().

◆ m_fname

string ASSA::IniFile::m_fname
private

INI file name.

Definition at line 193 of file IniFile.h.

Referenced by sync().

◆ m_section_pttrn

Regexp ASSA::IniFile::m_section_pttrn
private

Section header match.

Definition at line 202 of file IniFile.h.

◆ m_stream

std::fstream ASSA::IniFile::m_stream
private

File stream.

Definition at line 196 of file IniFile.h.

◆ m_tuple_pttrn

Regexp ASSA::IniFile::m_tuple_pttrn
private

Name/value pair match.

Definition at line 205 of file IniFile.h.


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