libassa  3.5.1
IniFile.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // $Id: IniFile.h,v 1.6 2005/12/17 19:53:29 vlg Exp $
4 //------------------------------------------------------------------------------
5 // IniFile.h
6 //------------------------------------------------------------------------------
7 // Copyright (C) 2003 Vladislav Grinchenko <vlg@users.sourceforge.net>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
13 //
14 // Date: Sat Sep 6 00:17:52 EDT 2003
15 //------------------------------------------------------------------------------
16 #ifndef INIFILE_H
17 #define INIFILE_H
18 
19 #include <string>
20 #include <list>
21 #include <fstream>
22 #include <iostream>
23 #include <iomanip>
24 
25 using std::list;
26 using std::string;
27 using std::pair;
28 
29 #include <assa/Regexp.h>
30 #include <assa/CommonUtils.h>
31 
37 namespace ASSA {
38 
39  class IniFile
40  {
41  public:
44  typedef pair<string, string> tuple_type;
45 
49  typedef pair<string, list<tuple_type> > sect_type;
50 
53  typedef list<sect_type> config_type;
54 
56  typedef config_type::iterator config_iterator;
57 
59  typedef config_type::const_iterator const_config_iterator;
60 
62  typedef list<tuple_type>::iterator tuple_iterator;
63 
65  typedef list<tuple_type>::const_iterator const_tuple_iterator;
66 
67  public:
71  IniFile (const string& fname_);
72 
78 
82  bool operator== (const IniFile& rhs_) const
83  { return (m_config == rhs_.m_config); }
84 
88  bool operator!= (const IniFile& rhs_) const
89  { return (! (*this == rhs_)); }
90 
96  int load ();
97 
100  void drop_all () { m_config.clear (); }
101 
107  int sync ();
108 
114  int sync (const string& fname_);
115 
120  void add_section (const string& section_);
121 
126  int drop_section (const string& section_);
127 
134  int set_pair (const string& section_, const tuple_type& newkey_);
135 
141  int drop_pair (const string& section_, const string& name_);
142 
150  string get_value (const string& section_, const string& name_) const;
151 
158  config_iterator find_section (const string& section_);
159 
166  const_config_iterator find_section (const string& section_) const;
167 
170  const_config_iterator sect_begin () const { return m_config.begin (); }
171 
174  config_iterator sect_end () { return m_config.end (); }
175 
178  unsigned int size () const { return m_config.size (); }
179 
182  void dump () const;
183 
184  private:
189  int trim_section_name (string& text_);
190 
191  private:
193  string m_fname;
194 
196  std::fstream m_stream;
197 
200 
203 
206 
209  };
210 
211  inline int IniFile::
212  trim_section_name (string& text_)
213  {
214  return (Utils::ltrim (text_, "[") == 0 &&
215  Utils::rtrim (text_, "]") == 0) ? 0 : -1;
216  }
217 
218  inline int IniFile::
219  sync ()
220  {
221  trace_with_mask ("IniFile::sync", INIFILE);
222  return sync (m_fname);
223  }
224 
225 
226 } // @end namespace
227 
228 #endif // INIFILE_H
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition: Logger.h:437
Wrapper class for UNIX regexp (3).
Regexp m_tuple_pttrn
Name/value pair match.
Definition: IniFile.h:205
std::fstream m_stream
File stream.
Definition: IniFile.h:196
void add_section(const string &section_)
Add new section.
int trim_section_name(string &text_)
Remove square brakets around section name.
Definition: IniFile.h:212
pair< string, string > tuple_type
A tuple is a name/value pair.
Definition: IniFile.h:44
config_type m_config
Cache holds the entire INI file in memory.
Definition: IniFile.h:199
int load()
Load configuration data from the file.
config_type::iterator config_iterator
Mutable iterator over the list of configuration sections.
Definition: IniFile.h:56
int set_pair(const string &section_, const tuple_type &newkey_)
Add or change name/value pair in the section.
bool operator==(const IniFile &rhs_) const
Compare two configurations.
Definition: IniFile.h:82
bool operator!=(const IniFile &rhs_) const
Compare two configurations.
Definition: IniFile.h:88
int drop_pair(const string &section_, const string &name_)
Remove name/value pair from the section in cache.
config_type::const_iterator const_config_iterator
Constant iterator over the list of configuration sections.
Definition: IniFile.h:59
config_iterator find_section(const string &section_)
Find section by its name.
IniFile(const string &fname_)
Do-nothing constructor.
int drop_section(const string &section_)
Remove section from cache.
Regexp m_comment_pttrn
Comment match.
Definition: IniFile.h:208
void drop_all()
Clear up configuration cache.
Definition: IniFile.h:100
Regexp m_section_pttrn
Section header match.
Definition: IniFile.h:202
list< sect_type > config_type
INI configuration is the collection of sections.
Definition: IniFile.h:53
config_iterator sect_end()
Return iterator past the last section.
Definition: IniFile.h:174
int sync()
Write cached configuration to the file.
Definition: IniFile.h:219
list< tuple_type >::iterator tuple_iterator
Mutable iterator over name/value pairs in a section.
Definition: IniFile.h:62
void dump() const
Dump cache to the log file.
int sync(const string &fname_)
Write cached configuration to the file fname_.
const_config_iterator sect_begin() const
Return iterator to the first section.
Definition: IniFile.h:170
string get_value(const string &section_, const string &name_) const
Find and return a value of the name/value pair in the section section_.
const_config_iterator find_section(const string &section_) const
Find section by its name.
unsigned int size() const
Return number of sections in the cache.
Definition: IniFile.h:178
list< tuple_type >::const_iterator const_tuple_iterator
Constant iterator over name/value pairs in a section.
Definition: IniFile.h:65
string m_fname
INI file name.
Definition: IniFile.h:193
pair< string, list< tuple_type > > sect_type
A section is a logical subcategory of related configuration information.
Definition: IniFile.h:49
~IniFile()
Destructor does not save cache data to the file.
Regexp class.
Definition: Regexp.h:43
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
Definition: Acceptor.h:40
@ INIFILE
Class IniFile messages
Definition: LogMask.h:52