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-2018, 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 "config-precomp.h" // Precompiled headers
11 
13 #include <mrpt/system/os.h>
14 #include "simpleini/SimpleIni.h"
15 
16 using namespace mrpt;
17 using namespace mrpt::config;
18 using namespace mrpt::config::simpleini;
19 using namespace std;
20 
22 {
24 };
25 
26 /*---------------------------------------------------------------
27  Constructor
28  ---------------------------------------------------------------*/
30  m_impl(mrpt::make_impl<CConfigFile::Impl>())
31 {
33 
34  m_file = fileName;
35  m_modified = false;
36  m_impl->m_ini.LoadFile(fileName.c_str());
37 
38  MRPT_END
39 }
40 
41 /*---------------------------------------------------------------
42  Constructor
43  ---------------------------------------------------------------*/
45  m_impl(mrpt::make_impl<CConfigFile::Impl>())
46 {
48 
49  m_file = "";
50  m_modified = false;
51 
52  MRPT_END
53 }
54 
55 /*---------------------------------------------------------------
56  setFileName
57  ---------------------------------------------------------------*/
59 {
61 
62  m_file = fil_path;
63  m_modified = false;
64 
65  m_impl->m_ini.LoadFile(fil_path.c_str());
66  MRPT_END
67 }
68 
69 /*---------------------------------------------------------------
70  writeNow
71  ---------------------------------------------------------------*/
73 {
75  if (m_modified && !m_file.empty())
76  {
77  m_impl->m_ini.SaveFile(m_file.c_str());
78  m_modified = false;
79  }
80  MRPT_END
81 }
82 
84 /*---------------------------------------------------------------
85  Destructor
86  ---------------------------------------------------------------*/
88 {
89  writeNow();
90 }
91 
92 /*---------------------------------------------------------------
93  writeString
94  ---------------------------------------------------------------*/
96  const std::string& section, const std::string& name, const std::string& str)
97 {
99 
100  m_modified = true;
101 
102  if (0 > m_impl->m_ini.SetValue(section.c_str(), name.c_str(), str.c_str(), NULL))
103  THROW_EXCEPTION("Error changing value in INI-style file!");
104 
105  MRPT_END
106 }
107 
108 /*---------------------------------------------------------------
109  readString
110  ---------------------------------------------------------------*/
112  const std::string& section, const std::string& name,
113  const std::string& defaultStr, bool failIfNotFound) const
114 {
115  MRPT_START
116  const char* defVal = failIfNotFound ? NULL : defaultStr.c_str();
117 
118  const char* aux = m_impl->m_ini.GetValue(
119  section.c_str(), name.c_str(), defVal,
120  NULL); // The memory is managed by the SimpleIni object
121 
122  if (failIfNotFound && !aux)
123  {
124  string tmpStr(
125  format(
126  "Value '%s' not found in section '%s' of file '%s' and "
127  "failIfNotFound=true.",
128  name.c_str(), section.c_str(), m_file.c_str()));
129  THROW_EXCEPTION(tmpStr);
130  }
131 
132  // Remove possible comments: "//"
133  std::string ret = aux;
134  size_t pos;
135  if ((pos = ret.find("//")) != string::npos && pos > 0 &&
136  isspace(ret[pos - 1]))
137  ret = ret.substr(0, pos);
138  return ret;
139 
140  MRPT_END
141 }
142 
143 /*---------------------------------------------------------------
144  getAllSections
145  ---------------------------------------------------------------*/
146 void CConfigFile::getAllSections(std::vector<std::string>& sections) const
147 {
149  m_impl->m_ini.GetAllSections(names);
150 
153  sections.resize(names.size());
154  for (n = names.begin(), s = sections.begin(); n != names.end(); ++n, ++s)
155  *s = n->pItem;
156 }
157 
158 /*---------------------------------------------------------------
159  getAllKeys
160  ---------------------------------------------------------------*/
162  const string& section, std::vector<std::string>& keys) const
163 {
165  m_impl->m_ini.GetAllKeys(section.c_str(), names);
166 
169  keys.resize(names.size());
170  for (n = names.begin(), s = keys.begin(); n != names.end(); ++n, ++s)
171  *s = n->pItem;
172 }
mrpt::pimpl< Impl > m_impl
Scalar * iterator
Definition: eigen_plugins.h:26
#define MRPT_START
Definition: exceptions.h:262
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
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:95
virtual void getAllSections(std::vector< std::string > &sections) const override
Returns a list with all the section names.
bool m_modified
If modified since load.
This class allows loading and storing values and vectors of different types from ".ini" files easily.
GLenum GLsizei n
Definition: glext.h:5074
CConfigFile()
Constructor, does not open any file.
Definition: CConfigFile.cpp:44
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.
STL namespace.
GLdouble s
Definition: glext.h:3676
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
GLsizei const GLchar ** string
Definition: glext.h:4101
std::vector< string > names
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void writeNow()
Dumps the changes to the physical configuration file now, not waiting until destruction.
Definition: CConfigFile.cpp:72
#define MRPT_END
Definition: exceptions.h:266
GLuint const GLchar * name
Definition: glext.h:4054
std::string m_file
The name of the file.
pimpl< T > make_impl(Args &&... args)
Definition: pimpl.h:18
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:58
void discardSavingChanges()
Discard saving (current) changes to physical file upon destruction.
Definition: CConfigFile.cpp:83
virtual void getAllKeys(const std::string &section, std::vector< std::string > &keys) const override
Returs a list with all the keys into a section.
std::list< Entry > TNamesDepend
set of dependent string pointers.
Definition: SimpleIni.h:184
virtual ~CConfigFile()
Destructor.
Definition: CConfigFile.cpp:87



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019