Main MRPT website > C++ reference for MRPT 1.5.7
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 /*---------------------------------------------------------------
78  setContent
79  ---------------------------------------------------------------*/
81 {
82  // Load the strings:
83  std::string aux;
84  stringList.getText(aux);
85  THE_INI->Load( aux.c_str(), aux.size() );
86 }
87 
88 /*---------------------------------------------------------------
89  setContent
90  ---------------------------------------------------------------*/
92 {
93  THE_INI->Load( str.c_str(), str.size() );
94 }
95 
96 /*---------------------------------------------------------------
97  getContent
98  ---------------------------------------------------------------*/
100 {
101  ((MRPT_CSimpleIni*)(m_ini.get()))->Save( str);
102 }
103 
104 /*---------------------------------------------------------------
105  Destructor
106  ---------------------------------------------------------------*/
108 {
109  delete THE_INI;
110 }
111 
112 /*---------------------------------------------------------------
113  writeString
114  ---------------------------------------------------------------*/
116 {
117  MRPT_START
118 
119  SI_Error ret = THE_INI->SetValue( section.c_str(),name.c_str(),str.c_str(), NULL );
120  if (ret<0)
121  THROW_EXCEPTION("Error changing value in INI-style file!");
122 
123  MRPT_END
124 }
125 
126 /*---------------------------------------------------------------
127  readString
128  ---------------------------------------------------------------*/
130  const std::string &section,
131  const std::string &name,
132  const std::string &defaultStr,
133  bool failIfNotFound) const
134 {
135  MRPT_START
136  const char *defVal = failIfNotFound ? NULL :defaultStr.c_str();
137 
138  const char *aux = static_cast<const MRPT_CSimpleIni*>(m_ini.get())->GetValue(
139  section.c_str(),
140  name.c_str(),
141  defVal,
142  NULL ); // The memory is managed by the SimpleIni object
143 
144  if (failIfNotFound && !aux )
145  {
146  string tmpStr( format("Value '%s' not found in section '%s' of memory configuration string list and failIfNotFound=true.",name.c_str(),section.c_str() ) );
147  THROW_EXCEPTION(tmpStr);
148  }
149 
150  // Remove possible comments: "//"
151  std::string ret = aux;
152  size_t pos;
153  if ((pos=ret.find("//"))!=string::npos && pos>0 && isspace(ret[pos-1]))
154  ret = ret.substr(0,pos);
155  return ret;
156  MRPT_END
157 }
158 
159 /*---------------------------------------------------------------
160  readString
161  ---------------------------------------------------------------*/
163 {
165  static_cast<const MRPT_CSimpleIni*>(m_ini.get())->GetAllSections(names);
166 
169  sections.resize(names.size());
170  for (n=names.begin(),s=sections.begin(); n!=names.end();++n,++s)
171  *s = n->pItem;
172 }
173 
174 
175 /*---------------------------------------------------------------
176  getAllKeys
177  ---------------------------------------------------------------*/
178 void CConfigFileMemory::getAllKeys( const string &section, vector_string &keys ) const
179 {
181  static_cast<const MRPT_CSimpleIni*>(m_ini.get())->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.
Definition: zip.h:16
#define THROW_EXCEPTION(msg)
GLenum GLsizei n
Definition: glext.h:4618
Scalar * iterator
Definition: eigen_plugins.h:23
STL namespace.
Simple INI file reader.
Definition: SimpleIni.h:110
GLdouble s
Definition: glext.h:3602
std::string readString(const std::string &section, const std::string &name, const std::string &defaultStr, bool failIfNotFound=false) const MRPT_OVERRIDE
A virtual method to read a generic string.
void getAllSections(vector_string &sections) const MRPT_OVERRIDE
Returns a list with all the section names.
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:30
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:2292
#define MRPT_END
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
GLsizei const GLchar ** string
Definition: glext.h:3919
#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:166
GLuint const GLchar * name
Definition: glext.h:3891
void_ptr_noncopy m_ini
The IniFile object.
void getAllKeys(const std::string &section, vector_string &keys) const MRPT_OVERRIDE
Returs a list with all the keys into a section.
void writeString(const std::string &section, const std::string &name, const std::string &str) MRPT_OVERRIDE
A virtual method to write a generic string.
void getText(std::string &outText) const
Returns the whole string list as a single string with &#39; &#39; characters for newlines.
CConfigFileMemory()
Empty constructor. Upon construction, call any of the "setContent" method.
void setContent(const utils::CStringList &stringList)
Changes the contents of the virtual "config file".



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019