#include <IniFile.h>
Collaboration diagram for ASSA::IniFile:
Public Types | |
typedef pair< string, string > | tuple_type |
A tuple is a name/value pair. | |
typedef pair< string, list< tuple_type > > | sect_type |
A section is a logical subcategory of related configuration information. | |
typedef list< sect_type > | config_type |
INI configuration is the collection of sections. | |
typedef config_type::iterator | config_iterator |
Mutable iterator over the list of configuration sections. | |
typedef config_type::const_iterator | const_config_iterator |
Constant iterator over the list of configuration sections. | |
typedef list< tuple_type >::iterator | tuple_iterator |
Mutable iterator over name/value pairs in a section. | |
typedef list< tuple_type >::const_iterator | const_tuple_iterator |
Constant iterator over name/value pairs in a section. | |
Public Member Functions | |
IniFile (const string &fname_) | |
Do-nothing constructor. | |
~IniFile () | |
Destructor does not save cache data to the file. | |
bool | operator== (const IniFile &rhs_) const |
Compare two configurations. | |
bool | operator!= (const IniFile &rhs_) const |
Compare two configurations. | |
int | load () |
Load configuration data from the file. | |
void | drop_all () |
Clear up configuration cache. | |
int | sync () |
Write cached configuration to the file. | |
int | sync (const string &fname_) |
Write cached configuration to the file fname_. | |
void | add_section (const string §ion_) |
Add new section. | |
int | drop_section (const string §ion_) |
Remove section from cache. | |
int | set_pair (const string §ion_, const tuple_type &newkey_) |
Add or change name/value pair in the section. | |
int | drop_pair (const string §ion_, const string &name_) |
Remove name/value pair from the section in cache. | |
string | get_value (const string §ion_, const string &name_) const |
Find and return a value of the name/value pair in the section section_. | |
config_iterator | find_section (const string §ion_) |
Find section by its name. | |
const_config_iterator | find_section (const string §ion_) const |
Find section by its name. | |
const_config_iterator | sect_begin () const |
Return iterator to the first section. | |
config_iterator | sect_end () |
Return iterator past the last section. | |
unsigned int | size () const |
Return number of sections in the cache. | |
void | dump () const |
Dump cache to the log file. | |
Private Member Functions | |
int | trim_section_name (string &text_) |
Remove square brakets around section name. | |
Private Attributes | |
string | m_fname |
INI file name. | |
std::fstream | m_stream |
File stream. | |
config_type | m_config |
Cache holds the entire INI file in memory. | |
Regexp | m_section_pttrn |
Section header match. | |
Regexp | m_tuple_pttrn |
Name/value pair match. | |
Regexp | m_comment_pttrn |
Comment match. |
Definition at line 39 of file IniFile.h.
|
Mutable iterator over the list of configuration sections.
|
|
INI configuration is the collection of sections.
|
|
Constant iterator over the list of configuration sections.
|
|
Constant iterator over name/value pairs in a section.
|
|
A section is a logical subcategory of related configuration information.
|
|
Mutable iterator over name/value pairs in a section.
|
|
A tuple is a name/value pair.
|
|
Do-nothing constructor.
Definition at line 23 of file IniFile.cpp. References ASSA::INIFILE, and trace_with_mask. 00024 : 00025 m_fname (fname_), 00026 m_section_pttrn ("\\[[a-zA-Z0-9]+.*] *$"), 00027 m_tuple_pttrn ("^[ \t]*[a-zA-Z0-9]+.* *= *.*"), 00028 m_comment_pttrn ("^#.*$") 00029 { 00030 trace_with_mask ("IniFile::IniFile", INIFILE); 00031 }
|
|
Destructor does not save cache data to the file. You should explicitly call sync() if you want your data to be saved to the file. Definition at line 34 of file IniFile.cpp. References ASSA::INIFILE, m_config, and trace_with_mask. 00035 { 00036 trace_with_mask ("IniFile::~IniFile", INIFILE); 00037 m_config.clear (); 00038 }
|
|
Add new section.
Definition at line 189 of file IniFile.cpp. References find_section(), and m_config. 00190 { 00191 const_config_iterator i = find_section (section_); 00192 if (i == m_config.end ()) { 00193 m_config.push_back (sect_type (section_, std::list<tuple_type> ())); 00194 } 00195 }
|
|
Clear up configuration cache.
Definition at line 100 of file IniFile.h. References m_config. 00100 { m_config.clear (); }
|
|
Remove name/value pair from the section in cache.
Definition at line 267 of file IniFile.cpp. References DL, find_section(), ASSA::INIFILE, sect_end(), and trace_with_mask. 00268 { 00269 trace_with_mask ("IniFile::drop_pair", INIFILE); 00270 00271 config_iterator i = find_section (section_); 00272 if (i == sect_end ()) { 00273 DL((INIFILE,"Section [%s] is not found!\n", section_.c_str ())); 00274 return -1; 00275 } 00276 00277 tuple_iterator j = (*i).second.begin (); 00278 while (j != (*i).second.end ()) { 00279 if ((*j).first == name_) { 00280 (*i).second.erase (j); 00281 return 0; 00282 } 00283 j++; 00284 } 00285 return -1; 00286 }
|
|
Remove section from cache.
Definition at line 252 of file IniFile.cpp. References DL, find_section(), ASSA::INIFILE, m_config, sect_end(), and trace_with_mask. 00253 { 00254 trace_with_mask ("IniFile::drop_section", INIFILE); 00255 00256 config_iterator i = find_section (section_); 00257 if (i == sect_end ()) { 00258 DL((INIFILE,"Section [%s] is not found!\n", section_.c_str ())); 00259 return -1; 00260 } 00261 m_config.erase (i); 00262 return 0; 00263 }
|
|
Dump cache to the log file.
Definition at line 141 of file IniFile.cpp. References DL, ASSA::INIFILE, m_config, and trace_with_mask. 00142 { 00143 trace_with_mask ("IniFile::dump", INIFILE); 00144 00145 const_config_iterator i = m_config.begin (); 00146 const_tuple_iterator j; 00147 00148 DL((INIFILE,"============= Start =================\n")); 00149 00150 while (i != m_config.end ()) { 00151 DL((INIFILE, "[%s]\n", (*i).first.c_str ())); 00152 j = (*i).second.begin (); 00153 while (j != (*i).second.end ()) { 00154 DL((INIFILE, " %s=\%s\n", 00155 (*j).first.c_str (), (*j).second.c_str ())); 00156 j++; 00157 } 00158 i++; 00159 } 00160 DL((INIFILE,"============== End =================\n")); 00161 }
|
|
Find section by its name.
Definition at line 214 of file IniFile.cpp. References m_config. 00215 { 00216 const_config_iterator i = m_config.begin (); 00217 00218 while (i != m_config.end ()) { 00219 if ((*i).first == section_) { 00220 return (i); 00221 } 00222 i++; 00223 } 00224 return m_config.end (); 00225 }
|
|
Find section by its name.
Definition at line 199 of file IniFile.cpp. References m_config. Referenced by add_section(), drop_pair(), drop_section(), ASSA::CmdLineOpts::parse_config_file(), and set_pair(). 00200 { 00201 config_iterator i = m_config.begin (); 00202 00203 while (i != m_config.end ()) { 00204 if ((*i).first == section_) { 00205 return (i); 00206 } 00207 i++; 00208 } 00209 return m_config.end (); 00210 }
|
|
Find and return a value of the name/value pair in the section section_.
Definition at line 165 of file IniFile.cpp. References m_config. 00166 { 00167 const_config_iterator i = m_config.begin (); 00168 const_tuple_iterator j; 00169 string ret (""); 00170 00171 while (i != m_config.end ()) { 00172 if ((*i).first == section_) { 00173 j = (*i).second.begin (); 00174 while (j != (*i).second.end ()) { 00175 if ((*j).first == name_) { 00176 ret = (*j).second; 00177 break; 00178 } 00179 j++; 00180 } 00181 } 00182 i++; 00183 } 00184 return ret; 00185 }
|
|
Load configuration data from the file. The file name is specified in the constructor.
Definition at line 42 of file IniFile.cpp. References DL, ASSA::INIFILE, m_comment_pttrn, m_config, m_fname, m_section_pttrn, m_stream, ASSA::Regexp::match(), size(), trace_with_mask, and trim_section_name(). 00043 { 00044 trace_with_mask ("IniFile::load", INIFILE); 00045 00046 const int size = 1024; 00047 char inbuf [size]; 00048 00049 string line; 00050 string name; 00051 string value; 00052 int ret = -1; 00053 00054 m_stream.open (m_fname.c_str (), std::ios::in); 00055 if (!m_stream) { 00056 goto done; 00057 } 00058 00059 while (m_stream) { 00060 m_stream.getline (inbuf, size, '\n'); 00061 DL((INIFILE,"Input: \"%s\"\n", inbuf)); 00062 00063 if (::strlen (inbuf) == 0 || m_comment_pttrn.match (inbuf) == 0) { 00064 continue; 00065 } 00066 line = inbuf; 00067 if (m_section_pttrn.match (inbuf) == 0) { 00068 if (trim_section_name (line) < 0) { 00069 goto done; 00070 } 00071 m_config.push_back (sect_type (line, std::list<tuple_type> ())); 00072 } 00073 else if (m_tuple_pttrn.match (inbuf) == 0) { 00074 if (Utils::split_pair (line, '=', name, value) < 0) { 00075 goto done; 00076 } 00077 Utils::trim_sides (name); 00078 Utils::trim_sides (value); 00079 m_config.back ().second.push_back (tuple_type (name, value)); 00080 } 00081 else { 00082 goto done; 00083 } 00084 } 00097 ret = 0; 00098 00099 done: 00100 if (ret < 0) { 00101 DL((INIFILE, "Parse error: illegal syntax!\n")); 00102 } 00103 m_stream.clear (); 00104 m_stream.close (); 00105 return ret; 00106 }
|
|
Compare two configurations.
Definition at line 88 of file IniFile.h.
|
|
Compare two configurations.
Definition at line 82 of file IniFile.h. References m_config. 00083 { return (m_config == rhs_.m_config); }
|
|
Return iterator to the first section.
Definition at line 170 of file IniFile.h. References m_config. 00170 { return m_config.begin (); }
|
|
Return iterator past the last section.
Definition at line 174 of file IniFile.h. References m_config. Referenced by drop_pair(), drop_section(), ASSA::CmdLineOpts::parse_config_file(), and set_pair(). 00174 { return m_config.end (); }
|
|
Add or change name/value pair in the section.
Definition at line 229 of file IniFile.cpp. References DL, find_section(), ASSA::INIFILE, sect_end(), and trace_with_mask. 00230 { 00231 trace_with_mask ("IniFile::set_pair", INIFILE); 00232 00233 config_iterator i = find_section (section_); 00234 if (i == sect_end ()) { 00235 DL((INIFILE,"Section [%s] is not found!\n", section_.c_str ())); 00236 return -1; 00237 } 00238 tuple_iterator j = (*i).second.begin (); 00239 while (j != (*i).second.end ()) { 00240 if ((*j).first == newkey_.first) { 00241 (*j).second = newkey_.second; 00242 return 0; 00243 } 00244 j++; 00245 } 00246 (*i).second.push_back (newkey_); 00247 return 0; 00248 }
|
|
Return number of sections in the cache.
Definition at line 178 of file IniFile.h. References m_config. Referenced by load(). 00178 { return m_config.size (); }
|
|
Write cached configuration to the file fname_.
Definition at line 110 of file IniFile.cpp. References EL, ASSA::INIFILE, m_config, m_stream, and trace_with_mask. 00111 { 00112 trace_with_mask ("IniFile::sync(fname)", INIFILE); 00113 00114 ::unlink (fname_.c_str ()); 00115 m_stream.open (fname_.c_str (), std::ios::app | std::ios::out); 00116 if (!m_stream) { 00117 EL((INIFILE,"Failed to open(\"%s\", app|out)\n", fname_.c_str ())); 00118 return -1; 00119 } 00120 const_config_iterator i = m_config.begin (); 00121 const_tuple_iterator j; 00122 00123 while (i != m_config.end ()) { 00124 m_stream << "[" << (*i).first << "]\n"; 00125 j = (*i).second.begin (); 00126 00127 while (j != (*i).second.end ()) { 00128 m_stream << (*j).first << "=" << (*j).second << "\n"; 00129 j++; 00130 } 00131 m_stream << "\n"; 00132 i++; 00133 } 00134 m_stream.clear (); 00135 m_stream.close (); 00136 return 0; 00137 }
|
|
Write cached configuration to the file. Filename used is the one given in the constructor.
Definition at line 219 of file IniFile.h. References ASSA::INIFILE, m_fname, and trace_with_mask. 00220 { 00221 trace_with_mask ("IniFile::sync", INIFILE); 00222 return sync (m_fname); 00223 }
|
|
Remove square brakets around section name.
Definition at line 212 of file IniFile.h. References ASSA::Utils::ltrim(), and ASSA::Utils::rtrim(). Referenced by load(). 00213 { 00214 return (Utils::ltrim (text_, "[") == 0 && 00215 Utils::rtrim (text_, "]") == 0) ? 0 : -1; 00216 }
|
|
Comment match.
Definition at line 208 of file IniFile.h. Referenced by load(). |
|
Cache holds the entire INI file in memory.
Definition at line 199 of file IniFile.h. Referenced by add_section(), drop_all(), drop_section(), dump(), find_section(), get_value(), load(), operator==(), sect_begin(), sect_end(), size(), sync(), and ~IniFile(). |
|
INI file name.
|
|
Section header match.
Definition at line 202 of file IniFile.h. Referenced by load(). |
|
File stream.
|
|
Name/value pair match.
|