configfile.cpp
Go to the documentation of this file.
1 /*****************************************************************
2  *
3  * This file is part of the GMAPPING project
4  *
5  * GMAPPING Copyright (c) 2004 Giorgio Grisetti,
6  * Cyrill Stachniss, and Wolfram Burgard
7  *
8  * This software is licensed under the 3-Clause BSD License
9  * and is copyrighted by Giorgio Grisetti, Cyrill Stachniss,
10  * and Wolfram Burgard.
11  *
12  * Further information on this license can be found at:
13  * https://opensource.org/licenses/BSD-3-Clause
14  *
15  * GMAPPING is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied
17  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18  * PURPOSE.
19  *
20  *****************************************************************/
21 
22 
23 #include <cstdlib>
25 
26 #include <fstream>
27 #include <iostream>
28 
29 #include <string>
30 #include <sstream>
31 #include <iostream>
32 #include <ctype.h>
33 
34 namespace GMapping{
35 using namespace std;
36 
37 AutoVal::AutoVal(const std::string& value) {
38  m_value=value;
39 }
40 
41 AutoVal::AutoVal(const char* c) {
42  m_value=c;
43 }
44 
45 AutoVal::AutoVal(double d) {
46  std::stringstream s;
47  s<<d;
48  m_value=s.str();
49 }
50 
51 AutoVal::AutoVal(bool d) {
52  std::stringstream s;
53  if(d)
54  s << "on";
55  else
56  s << "off";
57  m_value=s.str();
58 }
59 
60 
61 AutoVal::AutoVal(int i) {
62  std::stringstream s;
63  s<<i;
64  m_value=s.str();
65 }
66 
67 AutoVal::AutoVal(unsigned int i) {
68  std::stringstream s;
69  s<<i;
70  m_value=s.str();
71 }
72 
73 AutoVal::AutoVal(const AutoVal& other) {
74  m_value=other.m_value;
75 }
76 
77 AutoVal& AutoVal::operator=(const AutoVal& other) {
78  m_value=other.m_value;
79  return *this;
80 }
81 
82 AutoVal& AutoVal::operator=(double d) {
83  std::stringstream s;
84  s << d;
85  m_value = s.str();
86  return *this;
87 }
88 
89 AutoVal& AutoVal::operator=(bool d) {
90  std::stringstream s;
91  if(d)
92  s << "on";
93  else
94  s << "off";
95  m_value = s.str();
96  return *this;
97 }
98 
99 
100 AutoVal& AutoVal::operator=(int i) {
101  std::stringstream s;
102  s << i;
103  m_value = s.str();
104  return *this;
105 }
106 
107 AutoVal& AutoVal::operator=(unsigned int i) {
108  std::stringstream s;
109  s << i;
110  m_value = s.str();
111  return *this;
112 }
113 
114 
115 AutoVal& AutoVal::operator=(const std::string& s) {
116  m_value=s;
117  return *this;
118 }
119 
120 AutoVal::operator std::string() const {
121  return m_value;
122 }
123 
124 AutoVal::operator double() const {
125  return atof(m_value.c_str());
126 }
127 
128 AutoVal::operator int() const {
129  return atoi(m_value.c_str());
130 }
131 
132 AutoVal::operator unsigned int() const {
133  return (unsigned int) atoi(m_value.c_str());
134 }
135 
136 AutoVal::operator bool() const {
137  // stupid c++ does not allow compareNoCase...
138  if (toLower(m_value)=="on" || atoi(m_value.c_str()) == 1)
139  return true;
140  return false;
141 }
142 
143 std::string AutoVal::toLower(const std::string& source) const {
144  std::string result(source);
145  char c='\0';
146  for (unsigned int i=0; i<result.length(); i++) {
147  c = result[i];
148  c = ::tolower(c);
149  result[i] = c;
150  }
151  return result;
152 }
153 
154 
156 
158 }
159 
160 ConfigFile::ConfigFile(const std::string& configFile) {
161  read(configFile);
162 }
163 
164 ConfigFile::ConfigFile(const char* configFile) {
165  read(configFile);
166 }
167 
168 bool ConfigFile::read(const char* configFile) {
169  return read(std::string(configFile));
170 }
171 
172 bool ConfigFile::read(const std::string& configFile) {
173  std::ifstream file(configFile.c_str());
174 
175  if (!file || file.eof())
176  return false;
177 
178  std::string line;
179  std::string name;
180  std::string val;
181  std::string inSection;
182  while (std::getline(file,line)) {
183 
184  if (! line.length()) continue;
185  // cute the comments
186  if (line[0] == '#') continue;
187  line = truncate(line,"#");
188  line = trim(line);
189  if (! line.length()) continue;
190 
191  if (line[0] == '[') {
192  inSection=trim(line.substr(1,line.find(']')-1));
193  continue;
194  }
195 
196  istringstream lineStream(line);
197  lineStream >> name;
198  lineStream >> val;
199  insertValue(inSection,name,val);
200  }
201  return true;
202 }
203 
204 void ConfigFile::insertValue(const std::string& section, const std::string& entry, const std::string& thevalue) {
205  m_content[toLower(section)+'/'+toLower(entry)]=AutoVal(thevalue);
206 }
207 
208 const AutoVal& ConfigFile::value(const std::string& section, const std::string& entry) const {
209 
210  std::map<std::string,AutoVal>::const_iterator ci =
211  m_content.find(toLower(section) + '/' + toLower(entry));
212  if (ci == m_content.end()) throw "entry does not exist";
213 
214  return ci->second;
215 }
216 
217 const AutoVal& ConfigFile::value(const std::string& section, const std::string& entry, double def) {
218  try {
219  return value(section, entry);
220  } catch(const char *) {
221  return m_content.insert(std::make_pair(section+'/'+entry, AutoVal(def))).first->second;
222  }
223 }
224 
225 const AutoVal& ConfigFile::value(const std::string& section, const std::string& entry, bool def) {
226  try {
227  return value(section, entry);
228  } catch(const char *) {
229  return m_content.insert(std::make_pair(section+'/'+entry, AutoVal(def))).first->second;
230  }
231 }
232 
233 const AutoVal& ConfigFile::value(const std::string& section, const std::string& entry, int def) {
234  try {
235  return value(section, entry);
236  } catch(const char *) {
237  return m_content.insert(std::make_pair(section+'/'+entry, AutoVal(def))).first->second;
238  }
239 }
240 
241 const AutoVal& ConfigFile::value(const std::string& section, const std::string& entry, unsigned int def) {
242  try {
243  return value(section, entry);
244  } catch(const char *) {
245  return m_content.insert(std::make_pair(section+'/'+entry, AutoVal(def))).first->second;
246  }
247 }
248 
249 const AutoVal& ConfigFile::value(const std::string& section, const std::string& entry, const std::string& def) {
250  try {
251  return value(section, entry);
252  } catch(const char *) {
253  return m_content.insert(std::make_pair(section+'/'+entry, AutoVal(def))).first->second;
254  }
255 }
256 
257 const AutoVal& ConfigFile::value(const std::string& section, const std::string& entry, const char* def) {
258  try {
259  return value(section, entry);
260  } catch(const char *) {
261  return m_content.insert(std::make_pair(section+'/'+entry, AutoVal(def))).first->second;
262  }
263 }
264 
265 void ConfigFile::dumpValues(std::ostream& out) {
266 
267  for (std::map<std::string,AutoVal>::const_iterator it = m_content.begin();
268  it != m_content.end(); it++) {
269  out << (std::string) it->first << " " << (std::string)it->second << std::endl;
270  }
271 }
272 
273 
274 std::string ConfigFile::trim(const std::string& source, char const* delims) const {
275  std::string result(source);
276  std::string::size_type index = result.find_last_not_of(delims);
277  if(index != std::string::npos)
278  result.erase(++index);
279 
280  index = result.find_first_not_of(delims);
281  if(index != std::string::npos)
282  result.erase(0, index);
283  else
284  result.erase();
285  return result;
286 }
287 
288 std::string ConfigFile::truncate(const std::string& source, const char* atChar) const {
289  std::string::size_type index = source.find_first_of(atChar);
290 
291  if(index == 0)
292  return "";
293  else if(index == std::string::npos) {
294  return source;
295  }
296  return
297  source.substr(0,index);
298 }
299 
300 std::string ConfigFile::toLower(const std::string& source) const {
301  std::string result(source);
302  char c='\0';
303  for (unsigned int i=0; i<result.length(); i++) {
304  c = result[i];
305  c = ::tolower(c);
306  result[i] = c;
307  }
308  return result;
309 }
310 };
c
unsigned int c
Definition: gfs2stream.cpp:41
GMapping
Definition: configfile.cpp:34
read
rl read(is)
GMapping::ConfigFile::dumpValues
void dumpValues(std::ostream &out)
Definition: configfile.cpp:284
GMapping::AutoVal::m_value
std::string m_value
Definition: configfile.h:101
GMapping::AutoVal::operator=
AutoVal & operator=(const AutoVal &)
Definition: configfile.cpp:96
GMapping::ConfigFile::insertValue
void insertValue(const std::string &section, const std::string &entry, const std::string &thevalue)
Definition: configfile.cpp:223
GMapping::ConfigFile::ConfigFile
ConfigFile()
Definition: configfile.cpp:176
GMapping::ConfigFile::value
const AutoVal & value(const std::string &section, const std::string &entry) const
Definition: configfile.cpp:227
GMapping::AutoVal::toLower
std::string toLower(const std::string &source) const
Definition: configfile.cpp:162
GMapping::ConfigFile::trim
std::string trim(const std::string &source, char const *delims=" \t\r\n") const
Definition: configfile.cpp:293
GMapping::ConfigFile::read
bool read(const std::string &configFile)
Definition: configfile.cpp:191
GMapping::AutoVal
Definition: configfile.h:52
GMapping::ConfigFile::truncate
std::string truncate(const std::string &source, const char *atChar) const
Definition: configfile.cpp:307
GMapping::AutoVal::AutoVal
AutoVal()
Definition: configfile.h:73
configfile.h
GMapping::ConfigFile::toLower
std::string toLower(const std::string &source) const
Definition: configfile.cpp:319


openslam_gmapping
Author(s): Cyrill Stachniss, Udo Frese, Giorgio Grisetti, Wolfram Burgard
autogenerated on Thu Oct 19 2023 02:25:51