~ubuntu-branches/ubuntu/wily/cxxtools/wily-proposed

« back to all changes in this revision

Viewing changes to src/inifile.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kari Pahula
  • Date: 2008-06-16 12:24:28 UTC
  • mfrom: (3.1.3 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616122428-7bllgyt1358u779r
Tags: 1.4.8-2
Made libcxxtools-dev depend on libcxxtools6, not libcxxtools5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * cxxtools - general purpose C++-toolbox
 
3
 * Copyright (C) 2007 Tommi Maekitalo
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 */
 
20
 
 
21
#include <cxxtools/inifile.h>
 
22
#include <cxxtools/iniparser.h>
 
23
#include <cxxtools/log.h>
 
24
#include <fstream>
 
25
#include <stdexcept>
 
26
 
 
27
log_define("cxxtools.inifile")
 
28
 
 
29
namespace cxxtools
 
30
{
 
31
  namespace
 
32
  {
 
33
    class IniFileEvent : public IniParser::Event
 
34
    {
 
35
        IniFile& iniFile;
 
36
 
 
37
        std::string section;
 
38
        std::string key;
 
39
 
 
40
      public:
 
41
        IniFileEvent(IniFile& iniFile_)
 
42
          : iniFile(iniFile_)
 
43
          { }
 
44
 
 
45
        bool onSection(const std::string& section);
 
46
        bool onKey(const std::string& key);
 
47
        bool onValue(const std::string& value);
 
48
    };
 
49
 
 
50
      bool IniFileEvent::onSection(const std::string& section_)
 
51
      {
 
52
        log_debug("section \"" << section_ << '"');
 
53
        section = section_;
 
54
        return false;
 
55
      }
 
56
 
 
57
      bool IniFileEvent::onKey(const std::string& key_)
 
58
      {
 
59
        log_debug("key \"" << key_ << '"');
 
60
        key = key_;
 
61
        return false;
 
62
      }
 
63
 
 
64
      bool IniFileEvent::onValue(const std::string& value)
 
65
      {
 
66
        log_debug("value(" << section << ", " << key << ")=" << value);
 
67
        iniFile.setValue(section, key, value);
 
68
        return false;
 
69
      }
 
70
 
 
71
  }
 
72
 
 
73
  IniFile::IniFile(const std::string& filename)
 
74
  {
 
75
    log_debug("read ini-file \"" << filename << '"');
 
76
    std::ifstream in(filename.c_str());
 
77
    if (!in)
 
78
      throw std::runtime_error("could not open file \"" + filename + '"');
 
79
    IniFileEvent ev(*this);
 
80
    IniParser(ev).parse(in);
 
81
  }
 
82
 
 
83
  IniFile::IniFile(std::istream& in)
 
84
  {
 
85
    IniFileEvent ev(*this);
 
86
    IniParser(ev).parse(in);
 
87
  }
 
88
 
 
89
  std::ostream& operator << (std::ostream& out, const IniFile& ini)
 
90
  {
 
91
    for (IniFile::MapType::const_iterator si = ini.data.begin();
 
92
         si != ini.data.end(); ++si)
 
93
    {
 
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';
 
98
    }
 
99
    return out;
 
100
  }
 
101
}