Loading...
Searching...
No Matches
IniFile.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// $Id: IniFile.h,v 1.6 2005/12/17 19:53:29 vlg Exp $
//------------------------------------------------------------------------------
// IniFile.h
//------------------------------------------------------------------------------
// Copyright (C) 2003 Vladislav Grinchenko <vlg@users.sourceforge.net>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// Date: Sat Sep 6 00:17:52 EDT 2003
//------------------------------------------------------------------------------
#ifndef INIFILE_H
#define INIFILE_H
#include <string>
#include <list>
#include <fstream>
#include <iostream>
#include <iomanip>
using std::list;
using std::string;
using std::pair;
#include <assa/Regexp.h>
#include <assa/CommonUtils.h>
namespace ASSA {
class IniFile
{
public:
typedef pair<string, string> tuple_type;
typedef pair<string, list<tuple_type> > sect_type;
typedef list<sect_type> config_type;
typedef config_type::iterator config_iterator;
typedef config_type::const_iterator const_config_iterator;
typedef list<tuple_type>::iterator tuple_iterator;
typedef list<tuple_type>::const_iterator const_tuple_iterator;
public:
IniFile (const string& fname_);
~IniFile ();
bool operator== (const IniFile& rhs_) const
{ return (m_config == rhs_.m_config); }
bool operator!= (const IniFile& rhs_) const
{ return (! (*this == rhs_)); }
int load ();
void drop_all () { m_config.clear (); }
int sync ();
int sync (const string& fname_);
void add_section (const string& section_);
int drop_section (const string& section_);
int set_pair (const string& section_, const tuple_type& newkey_);
int drop_pair (const string& section_, const string& name_);
string get_value (const string& section_, const string& name_) const;
config_iterator find_section (const string& section_);
const_config_iterator find_section (const string& section_) const;
const_config_iterator sect_begin () const { return m_config.begin (); }
config_iterator sect_end () { return m_config.end (); }
unsigned int size () const { return m_config.size (); }
void dump () const;
private:
int trim_section_name (string& text_);
private:
string m_fname;
std::fstream m_stream;
config_type m_config;
Regexp m_section_pttrn;
Regexp m_tuple_pttrn;
Regexp m_comment_pttrn;
};
inline int IniFile::
trim_section_name (string& text_)
{
return (Utils::ltrim (text_, "[") == 0 &&
Utils::rtrim (text_, "]") == 0) ? 0 : -1;
}
inline int IniFile::
sync ()
{
trace_with_mask ("IniFile::sync", INIFILE);
return sync (m_fname);
}
} // @end namespace
#endif // INIFILE_H
CommonUtils.h
trace_with_mask
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
Regexp.h
Wrapper class for UNIX regexp (3).
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::IniFile
Definition IniFile.h:40
ASSA::IniFile::m_tuple_pttrn
Regexp m_tuple_pttrn
Name/value pair match.
Definition IniFile.h:205
ASSA::IniFile::m_stream
std::fstream m_stream
File stream.
Definition IniFile.h:196
ASSA::IniFile::trim_section_name
int trim_section_name(string &text_)
Remove square brakets around section name.
Definition IniFile.h:212
ASSA::IniFile::tuple_type
pair< string, string > tuple_type
A tuple is a name/value pair.
Definition IniFile.h:44
ASSA::IniFile::m_config
config_type m_config
Cache holds the entire INI file in memory.
Definition IniFile.h:199
ASSA::IniFile::config_iterator
config_type::iterator config_iterator
Mutable iterator over the list of configuration sections.
Definition IniFile.h:56
ASSA::IniFile::add_section
void add_section(const string &section_)
Add new section.
Definition IniFile.cpp:189
ASSA::IniFile::operator==
bool operator==(const IniFile &rhs_) const
Compare two configurations.
Definition IniFile.h:82
ASSA::IniFile::operator!=
bool operator!=(const IniFile &rhs_) const
Compare two configurations.
Definition IniFile.h:88
ASSA::IniFile::dump
void dump() const
Dump cache to the log file.
Definition IniFile.cpp:141
ASSA::IniFile::const_config_iterator
config_type::const_iterator const_config_iterator
Constant iterator over the list of configuration sections.
Definition IniFile.h:59
ASSA::IniFile::set_pair
int set_pair(const string &section_, const tuple_type &newkey_)
Add or change name/value pair in the section.
Definition IniFile.cpp:229
ASSA::IniFile::~IniFile
~IniFile()
Destructor does not save cache data to the file.
Definition IniFile.cpp:34
ASSA::IniFile::find_section
config_iterator find_section(const string &section_)
Find section by its name.
Definition IniFile.cpp:199
ASSA::IniFile::load
int load()
Load configuration data from the file.
Definition IniFile.cpp:42
ASSA::IniFile::drop_section
int drop_section(const string &section_)
Remove section from cache.
Definition IniFile.cpp:252
ASSA::IniFile::get_value
string get_value(const string &section_, const string &name_) const
Find and return a value of the name/value pair in the section section_.
Definition IniFile.cpp:165
ASSA::IniFile::m_comment_pttrn
Regexp m_comment_pttrn
Comment match.
Definition IniFile.h:208
ASSA::IniFile::drop_all
void drop_all()
Clear up configuration cache.
Definition IniFile.h:100
ASSA::IniFile::m_section_pttrn
Regexp m_section_pttrn
Section header match.
Definition IniFile.h:202
ASSA::IniFile::config_type
list< sect_type > config_type
INI configuration is the collection of sections.
Definition IniFile.h:53
ASSA::IniFile::sect_end
config_iterator sect_end()
Return iterator past the last section.
Definition IniFile.h:174
ASSA::IniFile::sync
int sync()
Write cached configuration to the file.
Definition IniFile.h:219
ASSA::IniFile::tuple_iterator
list< tuple_type >::iterator tuple_iterator
Mutable iterator over name/value pairs in a section.
Definition IniFile.h:62
ASSA::IniFile::sect_begin
const_config_iterator sect_begin() const
Return iterator to the first section.
Definition IniFile.h:170
ASSA::IniFile::size
unsigned int size() const
Return number of sections in the cache.
Definition IniFile.h:178
ASSA::IniFile::const_tuple_iterator
list< tuple_type >::const_iterator const_tuple_iterator
Constant iterator over name/value pairs in a section.
Definition IniFile.h:65
ASSA::IniFile::m_fname
string m_fname
INI file name.
Definition IniFile.h:193
ASSA::IniFile::drop_pair
int drop_pair(const string &section_, const string &name_)
Remove name/value pair from the section in cache.
Definition IniFile.cpp:267
ASSA::IniFile::sect_type
pair< string, list< tuple_type > > sect_type
A section is a logical subcategory of related configuration information.
Definition IniFile.h:49
ASSA::Regexp
Regexp class.
Definition Regexp.h:43
ASSA::Utils::ltrim
int ltrim(std::string &text_, const std::string &delim_)
Trim string from the beginning to the left of the delimiter.
Definition CommonUtils.cpp:67
ASSA::Utils::rtrim
int rtrim(std::string &text_, const std::string &delim_)
Trim string from the delimiter to the end of the string.
Definition CommonUtils.cpp:80
ASSA
Definition Acceptor.h:40
ASSA::INIFILE
@ INIFILE
Class IniFile messages
Definition LogMask.h:52