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 }
n
GLenum GLsizei n
Definition: glext.h:5074
mrpt::config
Definition: config/CConfigFile.h:16
os.h
mrpt::config::CConfigFile::getAllSections
virtual void getAllSections(std::vector< std::string > &sections) const override
Returns a list with all the section names.
Definition: CConfigFile.cpp:146
mrpt::config::CConfigFile::m_impl
mrpt::pimpl< Impl > m_impl
Definition: config/CConfigFile.h:39
mrpt::config::CConfigFile::~CConfigFile
virtual ~CConfigFile()
Destructor.
Definition: CConfigFile.cpp:87
s
GLdouble s
Definition: glext.h:3676
mrpt::config::CConfigFile::Impl
Definition: CConfigFile.cpp:21
mrpt::config::CConfigFile::CConfigFile
CConfigFile()
Constructor, does not open any file.
Definition: CConfigFile.cpp:44
CConfigFile.h
mrpt::config::CConfigFile::readString
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.
Definition: CConfigFile.cpp:111
mrpt::config::simpleini
Definition: SimpleIni.h:54
config-precomp.h
mrpt::config::simpleini::CSimpleIniTempl::TNamesDepend
std::list< Entry > TNamesDepend
set of dependent string pointers.
Definition: SimpleIni.h:184
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::config::CConfigFile::discardSavingChanges
void discardSavingChanges()
Discard saving (current) changes to physical file upon destruction.
Definition: CConfigFile.cpp:83
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
mrpt::config::CConfigFile::setFileName
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
name
GLuint const GLchar * name
Definition: glext.h:4054
mrpt::config::CConfigFile::m_file
std::string m_file
The name of the file.
Definition: config/CConfigFile.h:37
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::config::CConfigFile::writeNow
void writeNow()
Dumps the changes to the physical configuration file now, not waiting until destruction.
Definition: CConfigFile.cpp:72
names
std::vector< string > names
Definition: vision_stereo_rectify/test.cpp:23
mrpt::config::CConfigFile::writeString
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
SimpleIni.h
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::config::CConfigFile
This class allows loading and storing values and vectors of different types from "....
Definition: config/CConfigFile.h:33
mrpt::make_impl
pimpl< T > make_impl(Args &&... args)
Definition: pimpl.h:18
mrpt::config::simpleini::CSimpleIniTempl
Simple INI file reader.
Definition: SimpleIni.h:114
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::config::CConfigFile::getAllKeys
virtual void getAllKeys(const std::string &section, std::vector< std::string > &keys) const override
Returs a list with all the keys into a section.
Definition: CConfigFile.cpp:161
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
mrpt::config::CConfigFile::Impl::m_ini
MRPT_CSimpleIni m_ini
Definition: CConfigFile.cpp:23
mrpt::config::CConfigFile::m_modified
bool m_modified
If modified since load.
Definition: config/CConfigFile.h:42



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST