MRPT  2.0.1
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-2020, 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  ---------------------------------------------------------------*/
29 CConfigFile::CConfigFile(const std::string& fileName)
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  ---------------------------------------------------------------*/
57 void CConfigFile::setFileName(const std::string& fil_path)
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 
85 {
86  try
87  {
88  writeNow();
89  }
90  catch (const std::exception& e)
91  {
92  std::cerr << "[~CConfigFile] Exception:\n" << mrpt::exception_to_str(e);
93  }
94 }
95 
97  const std::string& section, const std::string& name, const std::string& str)
98 {
100 
101  m_modified = true;
102 
103  if (0 > m_impl->m_ini.SetValue(
104  section.c_str(), name.c_str(), str.c_str(), nullptr))
105  THROW_EXCEPTION("Error changing value in INI-style file!");
106 
107  MRPT_END
108 }
109 
110 /*---------------------------------------------------------------
111  readString
112  ---------------------------------------------------------------*/
114  const std::string& section, const std::string& name,
115  const std::string& defaultStr, bool failIfNotFound) const
116 {
117  MRPT_START
118  const char* defVal = failIfNotFound ? nullptr : defaultStr.c_str();
119 
120  const char* aux = m_impl->m_ini.GetValue(
121  section.c_str(), name.c_str(), defVal,
122  nullptr); // The memory is managed by the SimpleIni object
123 
124  if (failIfNotFound && !aux)
125  {
126  string tmpStr(format(
127  "Value '%s' not found in section '%s' of file '%s' and "
128  "failIfNotFound=true.",
129  name.c_str(), section.c_str(), m_file.c_str()));
130  THROW_EXCEPTION(tmpStr);
131  }
132 
133  // Remove possible comments: "//"
134  std::string ret = aux;
135  size_t pos;
136  if ((pos = ret.find("//")) != string::npos && pos > 0 &&
137  isspace(ret[pos - 1]))
138  ret = ret.substr(0, pos);
139  return ret;
140 
141  MRPT_END
142 }
143 
144 /*---------------------------------------------------------------
145  getAllSections
146  ---------------------------------------------------------------*/
147 void CConfigFile::getAllSections(std::vector<std::string>& sections) const
148 {
150  m_impl->m_ini.GetAllSections(names);
151 
152  MRPT_CSimpleIni::TNamesDepend::iterator n;
153  std::vector<std::string>::iterator s;
154  sections.resize(names.size());
155  for (n = names.begin(), s = sections.begin(); n != names.end(); ++n, ++s)
156  *s = n->pItem;
157 }
158 
159 /*---------------------------------------------------------------
160  getAllKeys
161  ---------------------------------------------------------------*/
163  const string& section, std::vector<std::string>& keys) const
164 {
166  m_impl->m_ini.GetAllKeys(section.c_str(), names);
167 
168  MRPT_CSimpleIni::TNamesDepend::iterator n;
169  std::vector<std::string>::iterator s;
170  keys.resize(names.size());
171  for (n = names.begin(), s = keys.begin(); n != names.end(); ++n, ++s)
172  *s = n->pItem;
173 }
174 
175 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:84
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:96
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
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.
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.
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
#define MRPT_END
Definition: exceptions.h:245
std::string m_file
The name of the file.
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
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 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020