libassa  3.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Types | Public Member Functions | Private Member Functions | Private Attributes
ASSA::IniFile Class Reference

#include <IniFile.h>

List of all members.

Public Types

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

Public Member Functions

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

Private Member Functions

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

Private Attributes

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

Detailed Description

Definition at line 39 of file IniFile.h.


Member Typedef Documentation

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

Mutable iterator over the list of configuration sections.

Definition at line 56 of file IniFile.h.

INI configuration is the collection of sections.

Definition at line 53 of file IniFile.h.

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.

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.

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.

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.

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::IniFile ( const string &  fname_)

Do-nothing constructor.

Parameters:
fname_Name of the INI file

Definition at line 23 of file IniFile.cpp.

References ASSA::INIFILE, and trace_with_mask.

:
m_fname (fname_),
m_section_pttrn ("\\[[a-zA-Z0-9]+.*] *$"),
m_tuple_pttrn ("^[ \t]*[a-zA-Z0-9]+.* *= *.*"),
m_comment_pttrn ("^#.*$")
{
trace_with_mask ("IniFile::IniFile", INIFILE);
}
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.

Definition at line 34 of file IniFile.cpp.

References ASSA::INIFILE, m_config, and trace_with_mask.

{
trace_with_mask ("IniFile::~IniFile", INIFILE);
m_config.clear ();
}

Member Function Documentation

void IniFile::add_section ( const string &  section_)

Add new section.

Parameters:
section_Section name to add

Definition at line 189 of file IniFile.cpp.

References find_section(), and m_config.

{
if (i == m_config.end ()) {
m_config.push_back (sect_type (section_, std::list<tuple_type> ()));
}
}
void ASSA::IniFile::drop_all ( )
inline

Clear up configuration cache.

Definition at line 100 of file IniFile.h.

References m_config.

{ m_config.clear (); }
int 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

Definition at line 267 of file IniFile.cpp.

References DL, find_section(), ASSA::INIFILE, sect_end(), and trace_with_mask.

{
trace_with_mask ("IniFile::drop_pair", INIFILE);
config_iterator i = find_section (section_);
if (i == sect_end ()) {
DL((INIFILE,"Section [%s] is not found!\n", section_.c_str ()));
return -1;
}
tuple_iterator j = (*i).second.begin ();
while (j != (*i).second.end ()) {
if ((*j).first == name_) {
(*i).second.erase (j);
return 0;
}
j++;
}
return -1;
}
int 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

Definition at line 252 of file IniFile.cpp.

References DL, find_section(), ASSA::INIFILE, m_config, sect_end(), and trace_with_mask.

{
trace_with_mask ("IniFile::drop_section", INIFILE);
config_iterator i = find_section (section_);
if (i == sect_end ()) {
DL((INIFILE,"Section [%s] is not found!\n", section_.c_str ()));
return -1;
}
m_config.erase (i);
return 0;
}
void IniFile::dump ( void  ) const

Dump cache to the log file.

Definition at line 141 of file IniFile.cpp.

References DL, ASSA::INIFILE, m_config, and trace_with_mask.

{
trace_with_mask ("IniFile::dump", INIFILE);
DL((INIFILE,"============= Start =================\n"));
while (i != m_config.end ()) {
DL((INIFILE, "[%s]\n", (*i).first.c_str ()));
j = (*i).second.begin ();
while (j != (*i).second.end ()) {
DL((INIFILE, " %s=\%s\n",
(*j).first.c_str (), (*j).second.c_str ()));
j++;
}
i++;
}
DL((INIFILE,"============== End =================\n"));
}
IniFile::config_iterator 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.

Definition at line 199 of file IniFile.cpp.

References m_config.

Referenced by add_section(), drop_pair(), drop_section(), ASSA::CmdLineOpts::parse_config_file(), and set_pair().

{
config_iterator i = m_config.begin ();
while (i != m_config.end ()) {
if ((*i).first == section_) {
return (i);
}
i++;
}
return m_config.end ();
}
IniFile::const_config_iterator 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.

Definition at line 214 of file IniFile.cpp.

References m_config.

{
while (i != m_config.end ()) {
if ((*i).first == section_) {
return (i);
}
i++;
}
return m_config.end ();
}
string 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.

Definition at line 165 of file IniFile.cpp.

References m_config.

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

{
string ret ("");
while (i != m_config.end ()) {
if ((*i).first == section_) {
j = (*i).second.begin ();
while (j != (*i).second.end ()) {
if ((*j).first == name_) {
ret = (*j).second;
break;
}
j++;
}
}
i++;
}
return ret;
}
int 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.

From N.M.Josuttis, "The C++ Standard Library", Sec. 13.9 File Access:

"Clear eofbit and failbit set due to the end-of-file.

Note that after the processing of a file, clear() must be called to clear the state flags that are set at end-of-file. This is required because the stream object is used for multiple files. open() NEVER clears any state flags. Thus, if a stream was not in a good state, after closing and reopening it, you still have to call clear() to get to a good state. This is also the case, if you open a different file."

Definition at line 42 of file IniFile.cpp.

References DL, ASSA::INIFILE, m_comment_pttrn, m_config, m_fname, m_section_pttrn, m_stream, m_tuple_pttrn, ASSA::Regexp::match(), size(), ASSA::Utils::split_pair(), trace_with_mask, trim_section_name(), and ASSA::Utils::trim_sides().

{
trace_with_mask ("IniFile::load", INIFILE);
const int size = 1024;
char inbuf [size];
string line;
string name;
string value;
int ret = -1;
m_stream.open (m_fname.c_str (), std::ios::in);
if (!m_stream) {
goto done;
}
while (m_stream) {
m_stream.getline (inbuf, size, '\n');
DL((INIFILE,"Input: \"%s\"\n", inbuf));
if (::strlen (inbuf) == 0 || m_comment_pttrn.match (inbuf) == 0) {
continue;
}
line = inbuf;
if (m_section_pttrn.match (inbuf) == 0) {
if (trim_section_name (line) < 0) {
goto done;
}
m_config.push_back (sect_type (line, std::list<tuple_type> ()));
}
else if (m_tuple_pttrn.match (inbuf) == 0) {
if (Utils::split_pair (line, '=', name, value) < 0) {
goto done;
}
m_config.back ().second.push_back (tuple_type (name, value));
}
else {
goto done;
}
}
ret = 0;
done:
if (ret < 0) {
DL((INIFILE, "Parse error: illegal syntax!\n"));
}
m_stream.clear ();
m_stream.close ();
return ret;
}
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.

{ return (! (*this == rhs_)); }
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.

References m_config.

{ return (m_config == rhs_.m_config); }
const_config_iterator ASSA::IniFile::sect_begin ( ) const
inline

Return iterator to the first section.

Definition at line 170 of file IniFile.h.

References m_config.

{ return m_config.begin (); }
config_iterator ASSA::IniFile::sect_end ( )
inline

Return iterator past the last section.

Definition at line 174 of file IniFile.h.

References m_config.

Referenced by drop_pair(), drop_section(), ASSA::CmdLineOpts::parse_config_file(), and set_pair().

{ return m_config.end (); }
int 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

Definition at line 229 of file IniFile.cpp.

References DL, find_section(), ASSA::INIFILE, sect_end(), and trace_with_mask.

{
trace_with_mask ("IniFile::set_pair", INIFILE);
config_iterator i = find_section (section_);
if (i == sect_end ()) {
DL((INIFILE,"Section [%s] is not found!\n", section_.c_str ()));
return -1;
}
tuple_iterator j = (*i).second.begin ();
while (j != (*i).second.end ()) {
if ((*j).first == newkey_.first) {
(*j).second = newkey_.second;
return 0;
}
j++;
}
(*i).second.push_back (newkey_);
return 0;
}
unsigned int ASSA::IniFile::size ( ) const
inline

Return number of sections in the cache.

Definition at line 178 of file IniFile.h.

References m_config.

Referenced by load().

{ return m_config.size (); }
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 219 of file IniFile.h.

References ASSA::INIFILE, m_fname, and trace_with_mask.

{
trace_with_mask ("IniFile::sync", INIFILE);
return sync (m_fname);
}
int 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.

Definition at line 110 of file IniFile.cpp.

References EL, ASSA::INIFILE, m_config, m_stream, and trace_with_mask.

{
trace_with_mask ("IniFile::sync(fname)", INIFILE);
::unlink (fname_.c_str ());
m_stream.open (fname_.c_str (), std::ios::app | std::ios::out);
if (!m_stream) {
EL((INIFILE,"Failed to open(\"%s\", app|out)\n", fname_.c_str ()));
return -1;
}
while (i != m_config.end ()) {
m_stream << "[" << (*i).first << "]\n";
j = (*i).second.begin ();
while (j != (*i).second.end ()) {
m_stream << (*j).first << "=" << (*j).second << "\n";
j++;
}
m_stream << "\n";
i++;
}
m_stream.clear ();
m_stream.close ();
return 0;
}
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 212 of file IniFile.h.

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

Referenced by load().

{
return (Utils::ltrim (text_, "[") == 0 &&
Utils::rtrim (text_, "]") == 0) ? 0 : -1;
}

Member Data Documentation

Regexp ASSA::IniFile::m_comment_pttrn
private

Comment match.

Definition at line 208 of file IniFile.h.

Referenced by load().

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 add_section(), drop_all(), drop_section(), dump(), find_section(), get_value(), load(), operator==(), sect_begin(), sect_end(), size(), sync(), and ~IniFile().

string ASSA::IniFile::m_fname
private

INI file name.

Definition at line 193 of file IniFile.h.

Referenced by load(), and sync().

Regexp ASSA::IniFile::m_section_pttrn
private

Section header match.

Definition at line 202 of file IniFile.h.

Referenced by load().

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

File stream.

Definition at line 196 of file IniFile.h.

Referenced by load(), and sync().

Regexp ASSA::IniFile::m_tuple_pttrn
private

Name/value pair match.

Definition at line 205 of file IniFile.h.

Referenced by load().


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