libassa  3.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
77  ~IniFile ();
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::
220  {
221  trace_with_mask ("IniFile::sync", INIFILE);
222  return sync (m_fname);
223  }
224 
225 
226 } // @end namespace
227 
228 #endif // INIFILE_H