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 <cxxtools/convert.h>
35 #include <map>
36 
37 namespace cxxtools
38 {
39  class IniFile
40  {
41  friend std::istream& operator >> (std::istream& in, IniFile& ini);
42  friend std::ostream& operator << (std::ostream& out, const IniFile& ini);
43 
44  typedef std::map<std::string, std::map<std::string, std::string> > MapType;
45  MapType data;
46 
47  public:
48  IniFile() { }
49  explicit IniFile(const std::string& filename);
50  explicit IniFile(std::istream& in);
51 
53  bool exists(const std::string& section) const
54  { return data.find(section) != data.end(); }
55 
57  bool exists(const std::string& section,
58  const std::string& token) const
59  {
60  MapType::const_iterator si = data.find(section);
61  return si != data.end()
62  && si->second.find(token) != si->second.end();
63  }
64 
66  std::string getValue(const std::string& section,
67  const std::string& token, const std::string& def = std::string()) const
68  {
69  // find section
70 
71  MapType::const_iterator si = data.find(section);
72 
73  if (si != data.end())
74  {
75  // find token
76 
77  MapType::mapped_type::const_iterator token_it = si->second.find(token);
78 
79  if (token_it != si->second.end())
80  return token_it->second;
81  }
82 
83  return def;
84  }
85 
88  template <typename T>
89  T getValueT(const std::string& section,
90  const std::string& token,
91  const T& def) const
92  {
93  // find section
94 
95  MapType::const_iterator si = data.find(section);
96 
97  if (si != data.end())
98  {
99  // find token
100 
101  MapType::mapped_type::const_iterator token_it = si->second.find(token);
102 
103  if (token_it != si->second.end())
104  {
105  try
106  {
107  return convert<T>(token_it->second);
108  }
109  catch (const cxxtools::ConversionError&)
110  {
111  // conversion error leads to returning of default value
112  }
113  }
114  }
115 
116  return def;
117  }
118 
120  void setValue(const std::string& section, const std::string& key,
121  const std::string& value)
122  {
123  data[section][key] = value;
124  }
125 
127  template <typename T>
128  void setValueT(const std::string& section, const std::string& key,
129  const T& value)
130  {
131  data[section][key] = convert<std::string>(value);
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::istream& operator >> (std::istream& in, IniFile& ini);
185 
187  std::ostream& operator << (std::ostream& out, const IniFile& ini);
188 
189 }
190 
191 #endif // CXXTOOLS_INIFILE_H