Main MRPT website > C++ reference for MRPT 1.9.9
CConfigFileMemory.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 
13 #include <mrpt/system/os.h>
14 
15 #include "simpleini/SimpleIni.h"
16 
17 using namespace mrpt;
18 using namespace mrpt::utils;
19 using namespace mrpt::utils::simpleini;
20 using namespace std;
21 
22 #define THE_INI static_cast<MRPT_CSimpleIni*>(m_ini.get())
23 
24 /*---------------------------------------------------------------
25  Constructor
26  ---------------------------------------------------------------*/
28 {
29  // Create the object:
30  m_ini = (void*)new MRPT_CSimpleIni();
31 
32  // Load the strings:
33  std::string aux;
34  stringList.getText(aux);
35  THE_INI->Load(aux.c_str(), aux.size());
36 }
37 
38 /*---------------------------------------------------------------
39  Constructor
40  ---------------------------------------------------------------*/
42 {
43  // Create the object:
44  m_ini = (void*)new MRPT_CSimpleIni();
45 
46  // Load the strings:
47  THE_INI->Load(str.c_str(), str.size());
48 }
49 
50 /*---------------------------------------------------------------
51  Constructor
52  ---------------------------------------------------------------*/
54 {
55  // Create the empty object:
56  m_ini = (void*)new MRPT_CSimpleIni();
57 }
58 
59 /** Copy constructor */
61 {
62  // Create the empty object:
63  m_ini = (void*)new MRPT_CSimpleIni();
64  (*this) = o;
65 }
66 
67 /** Copy operator */
69 {
70  std::string str;
71  static_cast<const MRPT_CSimpleIni*>(o.m_ini.get())->Save(str);
72  THE_INI->Load(str.c_str(), str.size());
73  return *this;
74 }
75 
76 /*---------------------------------------------------------------
77  setContent
78  ---------------------------------------------------------------*/
80 {
81  // Load the strings:
82  std::string aux;
83  stringList.getText(aux);
84  THE_INI->Load(aux.c_str(), aux.size());
85 }
86 
87 /*---------------------------------------------------------------
88  setContent
89  ---------------------------------------------------------------*/
91 {
92  THE_INI->Load(str.c_str(), str.size());
93 }
94 
95 /*---------------------------------------------------------------
96  getContent
97  ---------------------------------------------------------------*/
99 {
100  ((MRPT_CSimpleIni*)(m_ini.get()))->Save(str);
101 }
102 
103 /*---------------------------------------------------------------
104  Destructor
105  ---------------------------------------------------------------*/
107 /*---------------------------------------------------------------
108  writeString
109  ---------------------------------------------------------------*/
111  const std::string& section, const std::string& name, const std::string& str)
112 {
113  MRPT_START
114 
115  SI_Error ret =
116  THE_INI->SetValue(section.c_str(), name.c_str(), str.c_str(), nullptr);
117  if (ret < 0) THROW_EXCEPTION("Error changing value in INI-style file!");
118 
119  MRPT_END
120 }
121 
122 /*---------------------------------------------------------------
123  readString
124  ---------------------------------------------------------------*/
126  const std::string& section, const std::string& name,
127  const std::string& defaultStr, bool failIfNotFound) const
128 {
129  MRPT_START
130  const char* defVal = failIfNotFound ? nullptr : defaultStr.c_str();
131 
132  const char* aux =
133  static_cast<const MRPT_CSimpleIni*>(m_ini.get())
134  ->GetValue(
135  section.c_str(), name.c_str(), defVal,
136  nullptr); // The memory is managed by the SimpleIni object
137 
138  if (failIfNotFound && !aux)
139  {
140  string tmpStr(
141  format(
142  "Value '%s' not found in section '%s' of memory configuration "
143  "string list and failIfNotFound=true.",
144  name.c_str(), section.c_str()));
145  THROW_EXCEPTION(tmpStr);
146  }
147 
148  // Remove possible comments: "//"
149  std::string ret = aux;
150  size_t pos;
151  if ((pos = ret.find("//")) != string::npos && pos > 0 &&
152  isspace(ret[pos - 1]))
153  ret = ret.substr(0, pos);
154  return ret;
155  MRPT_END
156 }
157 
158 /*---------------------------------------------------------------
159  readString
160  ---------------------------------------------------------------*/
162 {
164  static_cast<const MRPT_CSimpleIni*>(m_ini.get())->GetAllSections(names);
165 
168  sections.resize(names.size());
169  for (n = names.begin(), s = sections.begin(); n != names.end(); ++n, ++s)
170  *s = n->pItem;
171 }
172 
173 /*---------------------------------------------------------------
174  getAllKeys
175  ---------------------------------------------------------------*/
177  const string& section, vector_string& keys) const
178 {
180  static_cast<const MRPT_CSimpleIni*>(m_ini.get())
181  ->GetAllKeys(section.c_str(), names);
182 
185  keys.resize(names.size());
186  for (n = names.begin(), s = keys.begin(); n != names.end(); ++n, ++s)
187  *s = n->pItem;
188 }
#define THE_INI
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
void getAllSections(vector_string &sections) const override
Returns a list with all the section names.
#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
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.
This class implements a config file-like interface over a memory-stored string list.
std::vector< std::string > vector_string
A type for passing a vector of strings.
Definition: types_simple.h:33
A class for storing a list of text lines.
Definition: CStringList.h:32
CSimpleIniTempl< char, SI_GenericNoCase< char >, MRPT_IniFileParser > MRPT_CSimpleIni
Definition: SimpleIni.h:2346
#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
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CConfigFileMemory & operator=(const CConfigFileMemory &o)
Copy operator.
std::list< Entry > TNamesDepend
set of dependent string pointers.
Definition: SimpleIni.h:183
GLuint const GLchar * name
Definition: glext.h:4054
void_ptr_noncopy m_ini
The IniFile object.
void writeString(const std::string &section, const std::string &name, const std::string &str) override
A virtual method to write a generic string.
void getAllKeys(const std::string &section, vector_string &keys) const override
Returs a list with all the keys into a section.
void getText(std::string &outText) const
Returns the whole string list as a single string with &#39; &#39; characters for newlines.
CConfigFileMemory()
Empty constructor.
void setContent(const utils::CStringList &stringList)
Changes the contents of the virtual "config file".



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