00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // $Id: IniFile.h,v 1.6 2005/12/17 19:53:29 vlg Exp $ 00004 //------------------------------------------------------------------------------ 00005 // IniFile.h 00006 //------------------------------------------------------------------------------ 00007 // Copyright (C) 2003 Vladislav Grinchenko <vlg@users.sourceforge.net> 00008 // 00009 // This library is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Library General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2 of the License, or (at your option) any later version. 00013 // 00014 // Date: Sat Sep 6 00:17:52 EDT 2003 00015 //------------------------------------------------------------------------------ 00016 #ifndef INIFILE_H 00017 #define INIFILE_H 00018 00019 #include <string> 00020 #include <list> 00021 #include <fstream> 00022 #include <iostream> 00023 #include <iomanip> 00024 00025 using std::list; 00026 using std::string; 00027 using std::pair; 00028 00029 #include <assa/Regexp.h> 00030 #include <assa/CommonUtils.h> 00031 00037 namespace ASSA { 00038 00039 class IniFile 00040 { 00041 public: 00044 typedef pair<string, string> tuple_type; 00045 00049 typedef pair<string, list<tuple_type> > sect_type; 00050 00053 typedef list<sect_type> config_type; 00054 00056 typedef config_type::iterator config_iterator; 00057 00059 typedef config_type::const_iterator const_config_iterator; 00060 00062 typedef list<tuple_type>::iterator tuple_iterator; 00063 00065 typedef list<tuple_type>::const_iterator const_tuple_iterator; 00066 00067 public: 00071 IniFile (const string& fname_); 00072 00077 ~IniFile (); 00078 00082 bool operator== (const IniFile& rhs_) const 00083 { return (m_config == rhs_.m_config); } 00084 00088 bool operator!= (const IniFile& rhs_) const 00089 { return (! (*this == rhs_)); } 00090 00096 int load (); 00097 00100 void drop_all () { m_config.clear (); } 00101 00107 int sync (); 00108 00114 int sync (const string& fname_); 00115 00120 void add_section (const string& section_); 00121 00126 int drop_section (const string& section_); 00127 00134 int set_pair (const string& section_, const tuple_type& newkey_); 00135 00141 int drop_pair (const string& section_, const string& name_); 00142 00150 string get_value (const string& section_, const string& name_) const; 00151 00158 config_iterator find_section (const string& section_); 00159 00166 const_config_iterator find_section (const string& section_) const; 00167 00170 const_config_iterator sect_begin () const { return m_config.begin (); } 00171 00174 config_iterator sect_end () { return m_config.end (); } 00175 00178 unsigned int size () const { return m_config.size (); } 00179 00182 void dump () const; 00183 00184 private: 00189 int trim_section_name (string& text_); 00190 00191 private: 00193 string m_fname; 00194 00196 std::fstream m_stream; 00197 00199 config_type m_config; 00200 00202 Regexp m_section_pttrn; 00203 00205 Regexp m_tuple_pttrn; 00206 00208 Regexp m_comment_pttrn; 00209 }; 00210 00211 inline int IniFile:: 00212 trim_section_name (string& text_) 00213 { 00214 return (Utils::ltrim (text_, "[") == 0 && 00215 Utils::rtrim (text_, "]") == 0) ? 0 : -1; 00216 } 00217 00218 inline int IniFile:: 00219 sync () 00220 { 00221 trace_with_mask ("IniFile::sync", INIFILE); 00222 return sync (m_fname); 00223 } 00224 00225 00226 } // @end namespace 00227 00228 #endif // INIFILE_H