MRPT  1.9.9
CConfigFileMemory.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-2019, 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 
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 
21 #define THE_INI m_impl->m_ini
22 
24 {
26 };
27 
28 CConfigFileMemory::CConfigFileMemory(const std::vector<std::string>& stringList)
29  : m_impl(mrpt::make_impl<CConfigFileMemory::Impl>())
30 {
31  // Load the strings:
32  std::string aux;
33  mrpt::system::stringListAsString(stringList, aux);
34  THE_INI.Load(aux.c_str(), aux.size());
35 }
36 
38  : m_impl(mrpt::make_impl<CConfigFileMemory::Impl>())
39 {
40  // Load the strings:
41  THE_INI.Load(str.c_str(), str.size());
42  THE_INI.Reset();
43 }
44 
45 /*---------------------------------------------------------------
46  Constructor
47  ---------------------------------------------------------------*/
49  : m_impl(mrpt::make_impl<CConfigFileMemory::Impl>())
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 = m_impl->m_ini.GetValue(
92  section.c_str(), name.c_str(), defVal,
93  nullptr); // The memory is managed by the SimpleIni object
94 
95  if (failIfNotFound && !aux)
96  {
97  string tmpStr(format(
98  "Value '%s' not found in section '%s' of memory configuration "
99  "string list and failIfNotFound=true.",
100  name.c_str(), section.c_str()));
101  THROW_EXCEPTION(tmpStr);
102  }
103 
104  // Remove possible comments: "//"
105  std::string ret = aux;
106  size_t pos;
107  if ((pos = ret.find("//")) != string::npos && pos > 0 &&
108  isspace(ret[pos - 1]))
109  ret = ret.substr(0, pos);
110  return ret;
111  MRPT_END
112 }
113 
114 void CConfigFileMemory::getAllSections(std::vector<std::string>& sections) const
115 {
117  m_impl->m_ini.GetAllSections(names);
118 
119  MRPT_CSimpleIni::TNamesDepend::iterator n;
120  std::vector<std::string>::iterator s;
121  sections.resize(names.size());
122  for (n = names.begin(), s = sections.begin(); n != names.end(); ++n, ++s)
123  *s = n->pItem;
124 }
125 
127  const string& section, std::vector<std::string>& keys) const
128 {
130  m_impl->m_ini.GetAllKeys(section.c_str(), names);
131 
132  MRPT_CSimpleIni::TNamesDepend::iterator n;
133  std::vector<std::string>::iterator s;
134  keys.resize(names.size());
135  for (n = names.begin(), s = keys.begin(); n != names.end(); ++n, ++s)
136  *s = n->pItem;
137 }
138 
139 void CConfigFileMemory::clear() { m_impl->m_ini.Reset(); }
#define THE_INI
This class implements a config file-like interface over a memory-stored string list.
#define MRPT_START
Definition: exceptions.h:241
CConfigFileMemory()
Empty constructor.
~CConfigFileMemory() override
dtor
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
GLenum GLsizei n
Definition: glext.h:5136
STL namespace.
void stringListAsString(const std::vector< std::string > &lst, std::string &out, const std::string &newline="\")
Convert a string list to one single string with new-lines.
GLdouble s
Definition: glext.h:3682
void getAllKeys(const std::string &section, std::vector< std::string > &keys) const override
Returs a list with all the keys into a section.
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.
GLsizei const GLchar ** string
Definition: glext.h:4116
void getAllSections(std::vector< std::string > &sections) const override
Returns a list with all the section names.
std::vector< string > names
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void clear() override
Empties the virtual "config file".
void setContent(const std::vector< std::string > &stringList)
Changes the contents of the virtual "config file".
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
#define MRPT_END
Definition: exceptions.h:245
GLuint const GLchar * name
Definition: glext.h:4068
void writeString(const std::string &section, const std::string &name, const std::string &str) override
A virtual method to write a generic string.
pimpl< T > make_impl(Args &&... args)
Definition: pimpl.h:18
std::list< Entry > TNamesDepend
set of dependent string pointers.
Definition: SimpleIni.h:179



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019