inifile.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_INIFILE_H
30 #define CXXTOOLS_INIFILE_H
31 
32 #include <iostream>
33 #include <string>
34 #include <sstream>
35 #include <map>
36 
37 namespace cxxtools
38 {
39  class IniFile
40  {
41  friend std::ostream& operator << (std::ostream& out, const IniFile& ini);
42 
43  typedef std::map<std::string, std::map<std::string, std::string> > MapType;
44  MapType data;
45 
46  public:
47  IniFile() { }
48  explicit IniFile(const std::string& filename);
49  explicit IniFile(std::istream& in);
50 
52  bool exists(const std::string& section) const
53  { return data.find(section) != data.end(); }
54 
56  bool exists(const std::string& section,
57  const std::string& token) const
58  {
59  MapType::const_iterator si = data.find(section);
60  return si != data.end()
61  && si->second.find(token) != si->second.end();
62  }
63 
65  std::string getValue(const std::string& section,
66  const std::string& token, const std::string& def = std::string()) const
67  {
68  // find section
69 
70  MapType::const_iterator si = data.find(section);
71 
72  if (si != data.end())
73  {
74  // find token
75 
76  MapType::mapped_type::const_iterator token_it = si->second.find(token);
77 
78  if (token_it != si->second.end())
79  return token_it->second;
80  }
81 
82  return def;
83  }
84 
87  template <typename T>
88  T getValueT(const std::string& section,
89  const std::string& token,
90  const T& def) const
91  {
92  // find section
93 
94  MapType::const_iterator si = data.find(section);
95 
96  if (si != data.end())
97  {
98  // find token
99 
100  MapType::mapped_type::const_iterator token_it = si->second.find(token);
101 
102  if (token_it != si->second.end())
103  {
104  // extract value with stream
105  T value;
106  std::istringstream s(token_it->second);
107  s >> value;
108 
109  if (s)
110  return value;
111  }
112  }
113 
114  return def;
115  }
116 
118  void setValue(const std::string& section, const std::string& key,
119  const std::string& value)
120  {
121  data[section][key] = value;
122  }
123 
125  template <typename T>
126  void setValueT(const std::string& section, const std::string& key,
127  const T& value)
128  {
129  std::ostringstream v;
130  v << value;
131  data[section][key] = v.str();
132  }
133 
148  template <typename OutputIterator>
149  void getSections(OutputIterator oi)
150  {
151  for (MapType::const_iterator it = data.begin(); it != data.end(); ++it, ++oi)
152  *oi = it->first;
153  }
154 
169  template <typename OutputIterator>
170  void getKeys(const std::string& section, OutputIterator oi)
171  {
172  MapType::const_iterator si = data.find(section);
173  if (si != data.end())
174  {
175  for (MapType::mapped_type::const_iterator it = si->second.begin();
176  it != si->second.end(); ++it, ++oi)
177  *oi = it->first;
178  }
179  }
180 
181  };
182 
184  std::ostream& operator << (std::ostream& out, const IniFile& ini);
185 
186 }
187 
188 #endif // CXXTOOLS_INIFILE_H