MRPT  2.0.1
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-2020, 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 
37 CConfigFileMemory::CConfigFileMemory(const std::string& str)
38  : m_impl(mrpt::make_impl<CConfigFileMemory::Impl>())
39 {
40  // Load the strings:
41  THE_INI.Load(str.c_str(), str.size());
42 }
43 
44 /*---------------------------------------------------------------
45  Constructor
46  ---------------------------------------------------------------*/
48  : m_impl(mrpt::make_impl<CConfigFileMemory::Impl>())
49 {
50 }
51 
52 void CConfigFileMemory::setContent(const std::vector<std::string>& stringList)
53 {
54  // Load the strings:
55  std::string aux;
56  mrpt::system::stringListAsString(stringList, aux);
57  THE_INI.Load(aux.c_str(), aux.size());
58 }
59 
60 void CConfigFileMemory::setContent(const std::string& str)
61 {
62  THE_INI.Load(str.c_str(), str.size());
63 }
64 
65 void CConfigFileMemory::getContent(std::string& str) const
66 {
67  m_impl->m_ini.Save(str);
68 }
69 
72  const std::string& section, const std::string& name, const std::string& str)
73 {
75 
76  SI_Error ret =
77  THE_INI.SetValue(section.c_str(), name.c_str(), str.c_str(), nullptr);
78  if (ret < 0) THROW_EXCEPTION("Error changing value in INI-style file!");
79 
80  MRPT_END
81 }
82 
84  const std::string& section, const std::string& name,
85  const std::string& defaultStr, bool failIfNotFound) const
86 {
88  const char* defVal = failIfNotFound ? nullptr : defaultStr.c_str();
89 
90  const char* aux = m_impl->m_ini.GetValue(
91  section.c_str(), name.c_str(), defVal,
92  nullptr); // The memory is managed by the SimpleIni object
93 
94  if (failIfNotFound && !aux)
95  {
96  string tmpStr(format(
97  "Value '%s' not found in section '%s' of memory configuration "
98  "string list and failIfNotFound=true.",
99  name.c_str(), section.c_str()));
100  THROW_EXCEPTION(tmpStr);
101  }
102 
103  // Remove possible comments: "//"
104  std::string ret = aux;
105  size_t pos;
106  if ((pos = ret.find("//")) != string::npos && pos > 0 &&
107  isspace(ret[pos - 1]))
108  ret = ret.substr(0, pos);
109  return ret;
110  MRPT_END
111 }
112 
113 void CConfigFileMemory::getAllSections(std::vector<std::string>& sections) const
114 {
116  m_impl->m_ini.GetAllSections(names);
117 
118  MRPT_CSimpleIni::TNamesDepend::iterator n;
119  std::vector<std::string>::iterator s;
120  sections.resize(names.size());
121  for (n = names.begin(), s = sections.begin(); n != names.end(); ++n, ++s)
122  *s = n->pItem;
123 }
124 
126  const string& section, std::vector<std::string>& keys) const
127 {
129  m_impl->m_ini.GetAllKeys(section.c_str(), names);
130 
131  MRPT_CSimpleIni::TNamesDepend::iterator n;
132  std::vector<std::string>::iterator s;
133  keys.resize(names.size());
134  for (n = names.begin(), s = keys.begin(); n != names.end(); ++n, ++s)
135  *s = n->pItem;
136 }
137 
138 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
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
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.
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.
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".
#define MRPT_END
Definition: exceptions.h:245
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 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020