Main MRPT website > C++ reference for MRPT 1.9.9
CConfigFile.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/utils/CConfigFile.h>
13 #include <mrpt/system/os.h>
14 #include "simpleini/SimpleIni.h"
15 
16 using namespace mrpt;
17 using namespace mrpt::utils;
18 using namespace mrpt::utils::simpleini;
19 using namespace std;
20 
21 /*---------------------------------------------------------------
22  Constructor
23  ---------------------------------------------------------------*/
25 {
27 
28  m_file = fileName;
29  m_modified = false;
30  m_ini = (void*)new MRPT_CSimpleIni();
31  static_cast<MRPT_CSimpleIni*>(m_ini.get())->LoadFile(fileName.c_str());
32 
33  MRPT_END
34 }
35 
36 /*---------------------------------------------------------------
37  Constructor
38  ---------------------------------------------------------------*/
40 {
42 
43  m_file = "";
44  m_modified = false;
45  m_ini = (void*)new MRPT_CSimpleIni();
46 
47  MRPT_END
48 }
49 
50 /*---------------------------------------------------------------
51  setFileName
52  ---------------------------------------------------------------*/
54 {
56 
57  m_file = fil_path;
58  m_modified = false;
59 
60  static_cast<MRPT_CSimpleIni*>(m_ini.get())->LoadFile(fil_path.c_str());
61  MRPT_END
62 }
63 
64 /*---------------------------------------------------------------
65  writeNow
66  ---------------------------------------------------------------*/
68 {
70  if (m_modified && !m_file.empty())
71  {
72  static_cast<MRPT_CSimpleIni*>(m_ini.get())->SaveFile(m_file.c_str());
73  m_modified = false;
74  }
75  MRPT_END
76 }
77 
78 void CConfigFile::discardSavingChanges() { m_modified = false; }
79 /*---------------------------------------------------------------
80  Destructor
81  ---------------------------------------------------------------*/
83 {
84  writeNow();
85  delete static_cast<MRPT_CSimpleIni*>(m_ini.get());
86 }
87 
88 /*---------------------------------------------------------------
89  writeString
90  ---------------------------------------------------------------*/
92  const std::string& section, const std::string& name, const std::string& str)
93 {
95 
96  m_modified = true;
97 
98  if (0 > static_cast<MRPT_CSimpleIni*>(m_ini.get())
99  ->SetValue(section.c_str(), name.c_str(), str.c_str(), NULL))
100  THROW_EXCEPTION("Error changing value in INI-style file!");
101 
102  MRPT_END
103 }
104 
105 /*---------------------------------------------------------------
106  readString
107  ---------------------------------------------------------------*/
109  const std::string& section, const std::string& name,
110  const std::string& defaultStr, bool failIfNotFound) const
111 {
112  MRPT_START
113  const char* defVal = failIfNotFound ? NULL : defaultStr.c_str();
114 
115  const char* aux =
116  static_cast<const MRPT_CSimpleIni*>(m_ini.get())
117  ->GetValue(
118  section.c_str(), name.c_str(), defVal,
119  NULL); // The memory is managed by the SimpleIni object
120 
121  if (failIfNotFound && !aux)
122  {
123  string tmpStr(
124  format(
125  "Value '%s' not found in section '%s' of file '%s' and "
126  "failIfNotFound=true.",
127  name.c_str(), section.c_str(), m_file.c_str()));
128  THROW_EXCEPTION(tmpStr);
129  }
130 
131  // Remove possible comments: "//"
132  std::string ret = aux;
133  size_t pos;
134  if ((pos = ret.find("//")) != string::npos && pos > 0 &&
135  isspace(ret[pos - 1]))
136  ret = ret.substr(0, pos);
137  return ret;
138 
139  MRPT_END
140 }
141 
142 /*---------------------------------------------------------------
143  getAllSections
144  ---------------------------------------------------------------*/
146 {
148  static_cast<const MRPT_CSimpleIni*>(m_ini.get())->GetAllSections(names);
149 
152  sections.resize(names.size());
153  for (n = names.begin(), s = sections.begin(); n != names.end(); ++n, ++s)
154  *s = n->pItem;
155 }
156 
157 /*---------------------------------------------------------------
158  getAllKeys
159  ---------------------------------------------------------------*/
160 void CConfigFile::getAllKeys(const string& section, vector_string& keys) const
161 {
163  static_cast<const MRPT_CSimpleIni*>(m_ini.get())
164  ->GetAllKeys(section.c_str(), names);
165 
168  keys.resize(names.size());
169  for (n = names.begin(), s = keys.begin(); n != names.end(); ++n, ++s)
170  *s = n->pItem;
171 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
void discardSavingChanges()
Discard saving (current) changes to physical file upon destruction.
Definition: CConfigFile.cpp:78
#define THROW_EXCEPTION(msg)
GLenum GLsizei n
Definition: glext.h:5074
Scalar * iterator
Definition: eigen_plugins.h:26
STL namespace.
Simple INI file reader.
Definition: SimpleIni.h:113
GLdouble s
Definition: glext.h:3676
virtual ~CConfigFile()
Destructor.
Definition: CConfigFile.cpp:82
std::vector< std::string > vector_string
A type for passing a vector of strings.
Definition: types_simple.h:33
CSimpleIniTempl< char, SI_GenericNoCase< char >, MRPT_IniFileParser > MRPT_CSimpleIni
Definition: SimpleIni.h:2346
CConfigFile()
Constructor, does not open any file.
Definition: CConfigFile.cpp:39
#define MRPT_END
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
GLsizei const GLchar ** string
Definition: glext.h:4101
void writeString(const std::string &section, const std::string &name, const std::string &str) override
A virtual method to write a generic string.
Definition: CConfigFile.cpp:91
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void setFileName(const std::string &fil_path)
Associate this object with the given file, so future read/write operations will be applied to that fi...
Definition: CConfigFile.cpp:53
std::list< Entry > TNamesDepend
set of dependent string pointers.
Definition: SimpleIni.h:183
GLuint const GLchar * name
Definition: glext.h:4054
void writeNow()
Dumps the changes to the physical configuration file now, not waiting until destruction.
Definition: CConfigFile.cpp:67
virtual void getAllKeys(const std::string &section, vector_string &keys) const override
Returs a list with all the keys into a section.
virtual void getAllSections(vector_string &sections) const override
Returns a list with all the section names.
std::string readString(const std::string &section, const std::string &name, const std::string &defaultStr, bool failIfNotFound=false) const override
A virtual method to read a generic string.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019