Loading...
Searching...
No Matches
IniFile.cpp
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// $Id: IniFile.cpp,v 1.7 2005/08/19 03:18:24 vlg Exp $
//------------------------------------------------------------------------------
// IniFile.cpp
//------------------------------------------------------------------------------
// Copyright (C) 2003,2004 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
//------------------------------------------------------------------------------
#include <assa/Assure.h>
#include <assa/IniFile.h>
#include <assa/CommonUtils.h>
using namespace ASSA;
IniFile::
IniFile (const string& fname_)
:
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 ()
{
trace_with_mask ("IniFile::~IniFile", INIFILE);
m_config.clear ();
}
int
IniFile::
load ()
{
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;
}
Utils::trim_sides (name);
Utils::trim_sides (value);
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;
}
int
IniFile::
sync (const string& fname_)
{
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;
}
const_config_iterator i = m_config.begin ();
const_tuple_iterator j;
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;
}
void
IniFile::
dump () const
{
trace_with_mask ("IniFile::dump", INIFILE);
const_config_iterator i = m_config.begin ();
const_tuple_iterator j;
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"));
}
string
IniFile::
get_value (const string& section_, const string& name_) const
{
const_config_iterator i = m_config.begin ();
const_tuple_iterator j;
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;
}
void
IniFile::
add_section (const string& section_)
{
const_config_iterator i = find_section (section_);
if (i == m_config.end ()) {
m_config.push_back (sect_type (section_, std::list<tuple_type> ()));
}
}
IniFile::config_iterator
IniFile::
find_section (const string& section_)
{
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
{
const_config_iterator i = m_config.begin ();
while (i != m_config.end ()) {
if ((*i).first == section_) {
return (i);
}
i++;
}
return m_config.end ();
}
int
IniFile::
set_pair (const string& section_, const tuple_type& newkey_)
{
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;
}
int
IniFile::
drop_section (const string& section_)
{
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;
}
int
IniFile::
drop_pair (const string& section_, const string& name_)
{
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;
}
Assure.h
A collection of assert function wrappers.
CommonUtils.h
IniFile.h
A Windows-style INI configuration file management class.
EL
#define EL(X)
A macro for writing error message to the Logger.
Definition Logger.h:285
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
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
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
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::IniFile
IniFile(const string &fname_)
Do-nothing constructor.
Definition IniFile.cpp:23
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::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::m_section_pttrn
Regexp m_section_pttrn
Section header match.
Definition IniFile.h:202
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::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::Regexp::match
int match(const char *text_)
Match an ASCII character string agains the pattern this class wraps.
Definition Regexp.cpp:58
ASSA::Utils::split_pair
int split_pair(const string &text_, char sep_, string &lhs_, string &rhs_)
Split input string into two parts separated by the separator character.
Definition CommonUtils.cpp:47
ASSA::Utils::trim_sides
void trim_sides(std::string &text_)
Trim white spaces and tabs from the beginning and the end of the text string.
Definition CommonUtils.cpp:93
ASSA
Definition Acceptor.h:40
ASSA::INIFILE
@ INIFILE
Class IniFile messages
Definition LogMask.h:52