2
* cxxtools - general purpose C++-toolbox
3
* Copyright (C) 2007 Tommi Maekitalo
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License as
7
* published by the Free Software Foundation; either version 2 of the
8
* License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful, but
11
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
12
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
13
* NON-INFRINGEMENT. See the GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#include <cxxtools/inifile.h>
22
#include <cxxtools/iniparser.h>
23
#include <cxxtools/log.h>
27
log_define("cxxtools.inifile")
33
class IniFileEvent : public IniParser::Event
41
IniFileEvent(IniFile& iniFile_)
45
bool onSection(const std::string& section);
46
bool onKey(const std::string& key);
47
bool onValue(const std::string& value);
50
bool IniFileEvent::onSection(const std::string& section_)
52
log_debug("section \"" << section_ << '"');
57
bool IniFileEvent::onKey(const std::string& key_)
59
log_debug("key \"" << key_ << '"');
64
bool IniFileEvent::onValue(const std::string& value)
66
log_debug("value(" << section << ", " << key << ")=" << value);
67
iniFile.setValue(section, key, value);
73
IniFile::IniFile(const std::string& filename)
75
log_debug("read ini-file \"" << filename << '"');
76
std::ifstream in(filename.c_str());
78
throw std::runtime_error("could not open file \"" + filename + '"');
79
IniFileEvent ev(*this);
80
IniParser(ev).parse(in);
83
IniFile::IniFile(std::istream& in)
85
IniFileEvent ev(*this);
86
IniParser(ev).parse(in);
89
std::ostream& operator << (std::ostream& out, const IniFile& ini)
91
for (IniFile::MapType::const_iterator si = ini.data.begin();
92
si != ini.data.end(); ++si)
94
out << '[' << si->first << "]\n";
95
for (IniFile::MapType::mapped_type::const_iterator it = si->second.begin();
96
it != si->second.end(); ++it)
97
out << it->first << '=' << it->second << '\n';