Main MRPT website > C++ reference for MRPT 1.5.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-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 
12 #include <mrpt/utils/CConfigFile.h>
13 #include <mrpt/system/os.h>
14 #include "simpleini/SimpleIni.h"
15 
16 using namespace mrpt;
17 using namespace mrpt::utils;
18 using namespace mrpt::utils::simpleini;
19 using namespace std;
20 
21 /*---------------------------------------------------------------
22  Constructor
23  ---------------------------------------------------------------*/
25 {
27 
28  m_file = fileName;
29  m_modified = false;
30  m_ini = (void*) new MRPT_CSimpleIni();
31  static_cast<MRPT_CSimpleIni*>(m_ini.get())->LoadFile(fileName.c_str());
32 
33 
34  MRPT_END
35 }
36 
37 /*---------------------------------------------------------------
38  Constructor
39  ---------------------------------------------------------------*/
41 {
43 
44  m_file = "";
45  m_modified = false;
46  m_ini = (void*) new MRPT_CSimpleIni();
47 
48  MRPT_END
49 }
50 
51 /*---------------------------------------------------------------
52  setFileName
53  ---------------------------------------------------------------*/
55 {
57 
58  m_file = fil_path;
59  m_modified = false;
60 
61  static_cast<MRPT_CSimpleIni*>(m_ini.get())->LoadFile(fil_path.c_str());
62  MRPT_END
63 }
64 
65 /*---------------------------------------------------------------
66  writeNow
67  ---------------------------------------------------------------*/
69 {
71  if (m_modified && !m_file.empty())
72  {
73  static_cast<MRPT_CSimpleIni*>(m_ini.get())->SaveFile( m_file.c_str() );
74  m_modified = false;
75  }
76  MRPT_END
77 }
78 
80 {
81  m_modified = false;
82 }
83 
84 /*---------------------------------------------------------------
85  Destructor
86  ---------------------------------------------------------------*/
88 {
89  writeNow();
90  delete static_cast<MRPT_CSimpleIni*>(m_ini.get());
91 }
92 
93 
94 /*---------------------------------------------------------------
95  writeString
96  ---------------------------------------------------------------*/
97 void CConfigFile::writeString(const std::string &section,const std::string &name, const std::string &str)
98 {
100 
101  m_modified = true;
102 
103  if (0 > static_cast<MRPT_CSimpleIni*>(m_ini.get())->SetValue( section.c_str(),name.c_str(),str.c_str(), NULL ))
104  THROW_EXCEPTION("Error changing value in INI-style file!");
105 
106  MRPT_END
107 
108 }
109 
110 /*---------------------------------------------------------------
111  readString
112  ---------------------------------------------------------------*/
114  const std::string &section,
115  const std::string &name,
116  const std::string &defaultStr,
117  bool failIfNotFound ) const
118 {
119  MRPT_START
120  const char *defVal = failIfNotFound ? NULL :defaultStr.c_str();
121 
122  const char *aux = static_cast<const MRPT_CSimpleIni*>(m_ini.get())->GetValue(
123  section.c_str(),
124  name.c_str(),
125  defVal,
126  NULL ); // The memory is managed by the SimpleIni object
127 
128  if (failIfNotFound && !aux )
129  {
130  string tmpStr( format("Value '%s' not found in section '%s' of file '%s' and failIfNotFound=true.",
131  name.c_str(),
132  section.c_str(),
133  m_file.c_str() ) );
134  THROW_EXCEPTION(tmpStr);
135  }
136 
137  // Remove possible comments: "//"
138  std::string ret = aux;
139  size_t pos;
140  if ((pos=ret.find("//"))!=string::npos && pos>0 && isspace(ret[pos-1]))
141  ret = ret.substr(0,pos);
142  return ret;
143 
144  MRPT_END
145 }
146 
147 /*---------------------------------------------------------------
148  getAllSections
149  ---------------------------------------------------------------*/
151 {
153  static_cast<const MRPT_CSimpleIni*>(m_ini.get())->GetAllSections(names);
154 
157  sections.resize(names.size());
158  for (n=names.begin(),s=sections.begin(); n!=names.end();++n,++s)
159  *s = n->pItem;
160 }
161 
162 
163 /*---------------------------------------------------------------
164  getAllKeys
165  ---------------------------------------------------------------*/
166 void CConfigFile::getAllKeys( const string &section, vector_string &keys ) const
167 {
169  static_cast<const MRPT_CSimpleIni*>(m_ini.get())->GetAllKeys(section.c_str(), names);
170 
173  keys.resize(names.size());
174  for ( n = names.begin(), s = keys.begin(); n!=names.end();++n,++s)
175  *s = n->pItem;
176 }
virtual void getAllKeys(const std::string &section, vector_string &keys) const MRPT_OVERRIDE
Returs a list with all the keys into a section.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
void discardSavingChanges()
Discard saving (current) changes to physical file upon destruction.
Definition: CConfigFile.cpp:79
#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
void writeString(const std::string &section, const std::string &name, const std::string &str) MRPT_OVERRIDE
A virtual method to write a generic string.
Definition: CConfigFile.cpp:97
virtual ~CConfigFile()
Destructor.
Definition: CConfigFile.cpp:87
std::vector< std::string > vector_string
A type for passing a vector of strings.
Definition: types_simple.h:30
CSimpleIniTempl< char, SI_GenericNoCase< char >, MRPT_IniFileParser > MRPT_CSimpleIni
Definition: SimpleIni.h:2292
CConfigFile()
Constructor, does not open any file.
Definition: CConfigFile.cpp:40
#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
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.
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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:54
std::list< Entry > TNamesDepend
set of dependent string pointers.
Definition: SimpleIni.h:166
GLuint const GLchar * name
Definition: glext.h:3891
void writeNow()
Dumps the changes to the physical configuration file now, not waiting until destruction.
Definition: CConfigFile.cpp:68
virtual void getAllSections(vector_string &sections) const MRPT_OVERRIDE
Returns a list with all the section names.



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020