MRPT  1.9.9
CConfigFile.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://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 {
47 
48  m_file = "";
49  m_modified = false;
50 
51  MRPT_END
52 }
53 
54 /*---------------------------------------------------------------
55  setFileName
56  ---------------------------------------------------------------*/
58 {
60 
61  m_file = fil_path;
62  m_modified = false;
63 
64  m_impl->m_ini.LoadFile(fil_path.c_str());
65  MRPT_END
66 }
67 
68 /*---------------------------------------------------------------
69  writeNow
70  ---------------------------------------------------------------*/
72 {
74  if (m_modified && !m_file.empty())
75  {
76  m_impl->m_ini.SaveFile(m_file.c_str());
77  m_modified = false;
78  }
79  MRPT_END
80 }
81 
83 /*---------------------------------------------------------------
84  Destructor
85  ---------------------------------------------------------------*/
87 /*---------------------------------------------------------------
88  writeString
89  ---------------------------------------------------------------*/
91  const std::string& section, const std::string& name, const std::string& str)
92 {
94 
95  m_modified = true;
96 
97  if (0 > m_impl->m_ini.SetValue(
98  section.c_str(), name.c_str(), str.c_str(), nullptr))
99  THROW_EXCEPTION("Error changing value in INI-style file!");
100 
101  MRPT_END
102 }
103 
104 /*---------------------------------------------------------------
105  readString
106  ---------------------------------------------------------------*/
108  const std::string& section, const std::string& name,
109  const std::string& defaultStr, bool failIfNotFound) const
110 {
111  MRPT_START
112  const char* defVal = failIfNotFound ? nullptr : defaultStr.c_str();
113 
114  const char* aux = m_impl->m_ini.GetValue(
115  section.c_str(), name.c_str(), defVal,
116  nullptr); // The memory is managed by the SimpleIni object
117 
118  if (failIfNotFound && !aux)
119  {
120  string tmpStr(format(
121  "Value '%s' not found in section '%s' of file '%s' and "
122  "failIfNotFound=true.",
123  name.c_str(), section.c_str(), m_file.c_str()));
124  THROW_EXCEPTION(tmpStr);
125  }
126 
127  // Remove possible comments: "//"
128  std::string ret = aux;
129  size_t pos;
130  if ((pos = ret.find("//")) != string::npos && pos > 0 &&
131  isspace(ret[pos - 1]))
132  ret = ret.substr(0, pos);
133  return ret;
134 
135  MRPT_END
136 }
137 
138 /*---------------------------------------------------------------
139  getAllSections
140  ---------------------------------------------------------------*/
141 void CConfigFile::getAllSections(std::vector<std::string>& sections) const
142 {
144  m_impl->m_ini.GetAllSections(names);
145 
146  MRPT_CSimpleIni::TNamesDepend::iterator n;
147  std::vector<std::string>::iterator s;
148  sections.resize(names.size());
149  for (n = names.begin(), s = sections.begin(); n != names.end(); ++n, ++s)
150  *s = n->pItem;
151 }
152 
153 /*---------------------------------------------------------------
154  getAllKeys
155  ---------------------------------------------------------------*/
157  const string& section, std::vector<std::string>& keys) const
158 {
160  m_impl->m_ini.GetAllKeys(section.c_str(), names);
161 
162  MRPT_CSimpleIni::TNamesDepend::iterator n;
163  std::vector<std::string>::iterator s;
164  keys.resize(names.size());
165  for (n = names.begin(), s = keys.begin(); n != names.end(); ++n, ++s)
166  *s = n->pItem;
167 }
168 
169 void CConfigFile::clear() { m_impl->m_ini.Reset(); }
mrpt::pimpl< Impl > m_impl
#define MRPT_START
Definition: exceptions.h:241
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
~CConfigFile() override
Destructor.
Definition: CConfigFile.cpp:86
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:90
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:5136
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:3682
GLsizei const GLchar ** string
Definition: glext.h:4116
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:71
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
#define MRPT_END
Definition: exceptions.h:245
GLuint const GLchar * name
Definition: glext.h:4068
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:57
void discardSavingChanges()
Discard saving (current) changes to physical file upon destruction.
Definition: CConfigFile.cpp:82
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:179
void clear() override
Empties the "config file".



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019