clockUtils  1.1
iniParser.h
Go to the documentation of this file.
1 /*
2  * clockUtils
3  * Copyright (2015) Michael Baer, Daniel Bonrath, All rights reserved.
4  *
5  * This file is part of clockUtils; clockUtils is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __CLOCKUTILS_INIPARSER_INIPARSER_H__
26 #define __CLOCKUTILS_INIPARSER_INIPARSER_H__
27 
28 #include <cstdint>
29 #include <map>
30 #include <sstream>
31 #include <string>
32 #include <tuple>
33 #include <vector>
34 
35 #include "clockUtils/errors.h"
37 
38 namespace clockUtils {
39 namespace iniParser {
40 
45  public:
49  IniParser();
50 
55  ClockError load(const std::string & file);
56 
61  ClockError save(const std::string & file);
62 
71  template<typename T>
72  typename std::enable_if<!std::is_enum<T>::value, ClockError>::type getValue(const std::string & section, const std::string & field, T & value) const {
73  auto it = _data.find(section);
74  if (it == _data.end()) {
76  }
77  for (const std::tuple<std::string, std::string, size_t, std::string> & t : it->second) {
78  if (std::get<SECTION>(t) == section && std::get<FIELD>(t) == field) {
79  std::stringstream ss(std::get<VALUE>(t));
80  if ((ss >> value).fail() || !ss.eof()) {
82  }
83  return ClockError::SUCCESS;
84  }
85  }
86 
88  }
89 
90  template<typename T>
91  typename std::enable_if<std::is_enum<T>::value, ClockError>::type getValue(const std::string & section, const std::string & field, T & value) const {
92  auto it = _data.find(section);
93  if (it == _data.end()) {
95  }
96  for (const std::tuple<std::string, std::string, size_t, std::string> & t : it->second) {
97  if (std::get<SECTION>(t) == section && std::get<FIELD>(t) == field) {
98  std::stringstream ss(std::get<VALUE>(t));
99  int v;
100  if ((ss >> v).fail() || !ss.eof()) {
101  return ClockError::WRONG_TYPE;
102  }
103  value = T(v);
104  return ClockError::SUCCESS;
105  }
106  }
107 
109  }
110 
117  template<typename T>
118  typename std::enable_if<!std::is_enum<T>::value, void>::type setValue(const std::string & section, const std::string & field, const T & value) {
119  std::stringstream ss;
120  ss << value;
121  setValue(section, field, ss.str());
122  }
123 
124  template<typename T>
125  typename std::enable_if<std::is_enum<T>::value, void>::type setValue(const std::string & section, const std::string & field, const T & value) {
126  std::stringstream ss;
127  ss << int(value);
128  setValue(section, field, ss.str());
129  }
130 
131  void setValue(const std::string & section, const std::string & field, const std::string & value) {
132  if (_data.find(section) == _data.end()) {
133  // create new section
134  _allLines[section].push_back("[" + section + "]");
135  _data[section].push_back(make_tuple(section, field, 1, value));
136  _allLines[section].push_back(""); // empty line. will be overritten anyways
137  return;
138  }
139  for (std::tuple<std::string, std::string, size_t, std::string> & t : _data[section]) {
140  if (std::get<SECTION>(t) == section && std::get<FIELD>(t) == field) {
141  std::get<VALUE>(t) = value;
142  return;
143  }
144  }
145  // value not found. Insert at end
146  if (_allLines[section].back() == "" && std::get<INDEX>(_data[section].back()) != _allLines[section].size() - 1) {
147  _data[section].push_back(make_tuple(section, field, _allLines[section].size() - 1, value));
148  } else {
149  _data[section].push_back(make_tuple(section, field, _allLines[section].size(), value));
150  }
151  _allLines[section].push_back(""); // empty line. will be overritten anyways
152  }
153 
157  std::vector<std::string> getAllSections() const;
158 
162  std::vector<std::string> getAllEntries(const std::string & section) const;
163 
168  void removeEntry(const std::string & section, const std::string & entry);
169 
170  private:
174  enum Fields {
175  SECTION,
176  FIELD,
177  INDEX,
178  VALUE
179  };
180 
181  // one vector with lines/values for each section
182  std::map<std::string, std::vector<std::tuple<std::string, std::string, size_t, std::string>>> _data;
183  std::map<std::string, std::vector<std::string>> _allLines;
184  };
185 
189  template<>
190  ClockError CLOCK_INIPARSER_API IniParser::getValue<std::string>(const std::string & section, const std::string & field, std::string & value) const;
191 
192 } /* namespace iniParser */
193 } /* namespace clockUtils */
194 
195 #endif /* __CLOCKUTILS_INIPARSER_INIPARSER_H__ */
196 
0x6 value couldn&#39;t be cast in expected type
std::enable_if<!std::is_enum< T >::value, ClockError >::type getValue(const std::string &section, const std::string &field, T &value) const
fetches value from parsed ini file
Definition: iniParser.h:72
std::enable_if< std::is_enum< T >::value, ClockError >::type getValue(const std::string &section, const std::string &field, T &value) const
Definition: iniParser.h:91
0x0 method call succeeded
void setValue(const std::string &section, const std::string &field, const std::string &value)
Definition: iniParser.h:131
#define CLOCK_INIPARSER_API
this class is used for parsing configuration files
Definition: iniParser.h:44
std::enable_if< std::is_enum< T >::value, void >::type setValue(const std::string &section, const std::string &field, const T &value)
Definition: iniParser.h:125
std::enable_if<!std::is_enum< T >::value, void >::type setValue(const std::string &section, const std::string &field, const T &value)
sets value for a variable
Definition: iniParser.h:118
ClockError
Definition: errors.h:30