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-2018, 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 "config-precomp.h" // Precompiled headers
11 
14 
15 #include "simpleini/SimpleIni.h"
16 
17 using namespace mrpt;
18 using namespace mrpt::config;
19 using namespace mrpt::config::simpleini;
20 using namespace std;
21 
22 #define THE_INI m_impl->m_ini
23 
25 {
27 };
28 
29 CConfigFileMemory::CConfigFileMemory(const std::vector<std::string>& stringList) :
31 {
32  // Load the strings:
33  std::string aux;
34  mrpt::system::stringListAsString(stringList, aux);
35  THE_INI.Load(aux.c_str(), aux.size());
36 }
37 
40 {
41  // Load the strings:
42  THE_INI.Load(str.c_str(), str.size());
43 }
44 
45 /*---------------------------------------------------------------
46  Constructor
47  ---------------------------------------------------------------*/
50 {
51 }
52 
53 void CConfigFileMemory::setContent(const std::vector<std::string>& stringList)
54 {
55  // Load the strings:
56  std::string aux;
57  mrpt::system::stringListAsString(stringList, aux);
58  THE_INI.Load(aux.c_str(), aux.size());
59 }
60 
62 {
63  THE_INI.Load(str.c_str(), str.size());
64 }
65 
67 {
68  m_impl->m_ini.Save(str);
69 }
70 
73  const std::string& section, const std::string& name, const std::string& str)
74 {
76 
77  SI_Error ret =
78  THE_INI.SetValue(section.c_str(), name.c_str(), str.c_str(), nullptr);
79  if (ret < 0) THROW_EXCEPTION("Error changing value in INI-style file!");
80 
81  MRPT_END
82 }
83 
85  const std::string& section, const std::string& name,
86  const std::string& defaultStr, bool failIfNotFound) const
87 {
89  const char* defVal = failIfNotFound ? nullptr : defaultStr.c_str();
90 
91  const char* aux =
92  m_impl->m_ini.GetValue(
93  section.c_str(), name.c_str(), defVal,
94  nullptr); // The memory is managed by the SimpleIni object
95 
96  if (failIfNotFound && !aux)
97  {
98  string tmpStr(
99  format(
100  "Value '%s' not found in section '%s' of memory configuration "
101  "string list and failIfNotFound=true.",
102  name.c_str(), section.c_str()));
103  THROW_EXCEPTION(tmpStr);
104  }
105 
106  // Remove possible comments: "//"
107  std::string ret = aux;
108  size_t pos;
109  if ((pos = ret.find("//")) != string::npos && pos > 0 &&
110  isspace(ret[pos - 1]))
111  ret = ret.substr(0, pos);
112  return ret;
113  MRPT_END
114 }
115 
116 void CConfigFileMemory::getAllSections(std::vector<std::string>& sections) const
117 {
119  m_impl->m_ini.GetAllSections(names);
120 
123  sections.resize(names.size());
124  for (n = names.begin(), s = sections.begin(); n != names.end(); ++n, ++s)
125  *s = n->pItem;
126 }
127 
129  const string& section, std::vector<std::string>& keys) const
130 {
132  m_impl->m_ini.GetAllKeys(section.c_str(), names);
133 
136  keys.resize(names.size());
137  for (n = names.begin(), s = keys.begin(); n != names.end(); ++n, ++s)
138  *s = n->pItem;
139 }
n
GLenum GLsizei n
Definition: glext.h:5074
mrpt::config
Definition: config/CConfigFile.h:16
mrpt::config::CConfigFileMemory::getAllSections
void getAllSections(std::vector< std::string > &sections) const override
Returns a list with all the section names.
Definition: CConfigFileMemory.cpp:116
CConfigFileMemory.h
s
GLdouble s
Definition: glext.h:3676
mrpt::config::CConfigFileMemory::readString
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.
Definition: CConfigFileMemory.cpp:84
string_utils.h
mrpt::config::CConfigFileMemory::getAllKeys
void getAllKeys(const std::string &section, std::vector< std::string > &keys) const override
Returs a list with all the keys into a section.
Definition: CConfigFileMemory.cpp:128
mrpt::config::simpleini
Definition: SimpleIni.h:54
config-precomp.h
mrpt::config::simpleini::CSimpleIniTempl::TNamesDepend
std::list< Entry > TNamesDepend
set of dependent string pointers.
Definition: SimpleIni.h:184
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
name
GLuint const GLchar * name
Definition: glext.h:4054
mrpt::config::CConfigFileMemory::Impl
Definition: CConfigFileMemory.cpp:24
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
mrpt::config::simpleini::SI_Error
SI_Error
Definition: SimpleIni.h:56
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::config::CConfigFileMemory::setContent
void setContent(const std::vector< std::string > &stringList)
Changes the contents of the virtual "config file".
Definition: CConfigFileMemory.cpp:53
mrpt::config::CConfigFileMemory::CConfigFileMemory
CConfigFileMemory()
Empty constructor.
Definition: CConfigFileMemory.cpp:48
mrpt::config::CConfigFileMemory::writeString
void writeString(const std::string &section, const std::string &name, const std::string &str) override
A virtual method to write a generic string.
Definition: CConfigFileMemory.cpp:72
names
std::vector< string > names
Definition: vision_stereo_rectify/test.cpp:23
mrpt::config::CConfigFileMemory::m_impl
mrpt::pimpl< Impl > m_impl
Definition: config/CConfigFileMemory.h:65
THE_INI
#define THE_INI
Definition: CConfigFileMemory.cpp:22
SimpleIni.h
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::make_impl
pimpl< T > make_impl(Args &&... args)
Definition: pimpl.h:18
mrpt::config::simpleini::CSimpleIniTempl
Simple INI file reader.
Definition: SimpleIni.h:114
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::config::CConfigFileMemory
This class implements a config file-like interface over a memory-stored string list.
Definition: config/CConfigFileMemory.h:30
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
mrpt::config::CConfigFileMemory::~CConfigFileMemory
virtual ~CConfigFileMemory()
dtor
Definition: CConfigFileMemory.cpp:71
mrpt::system::stringListAsString
void stringListAsString(const std::vector< std::string > &lst, std::string &out, const std::string &newline="\r\n")
Convert a string list to one single string with new-lines.
Definition: string_utils.cpp:351
mrpt::config::CConfigFileMemory::getContent
std::string getContent() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: config/CConfigFileMemory.h:50
mrpt::config::CConfigFileMemory::Impl::m_ini
MRPT_CSimpleIni m_ini
Definition: CConfigFileMemory.cpp:26



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST