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 }
#define THE_INI
This class implements a config file-like interface over a memory-stored string list.
Scalar * iterator
Definition: eigen_plugins.h:26
#define MRPT_START
Definition: exceptions.h:262
CConfigFileMemory()
Empty constructor.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
GLenum GLsizei n
Definition: glext.h:5074
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:3676
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:4101
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 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:266
GLuint const GLchar * name
Definition: glext.h:4054
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:180



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020