Debian Codegraph @

libs/config/src/CConfigFileMemory.cpp

raw
1 /* +------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | https://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2026, 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
12 // Fix to SimpleIni bug: not able to build with C++17
13 #include <functional>
14 #ifdef _MSC_VER
15 namespace std
16 {
17 template <typename T1, typename T2, typename RET>
18 using binary_function = std::function<RET(T1, T2)>;
19 }
20 #endif
21
22 #include <mrpt/config.h>
23 #if MRPT_HAS_SIMPLEINI_SYSTEM
24 // Enforce using libuci instead of copyrighted ConvertUTF.h
25 #define SI_CONVERT_ICU 1
26 #endif
27 #include <SimpleIni.h>
28 #include <mrpt/config/CConfigFileMemory.h>
29 #include <mrpt/config/config_parser.h>
30 #include <mrpt/system/string_utils.h>
31
32 using namespace mrpt;
33 using namespace mrpt::config;
34 using namespace std;
35
36 struct CConfigFileMemory::Impl
37 {
38 std::shared_ptr<CSimpleIniA> ini = std::make_shared<CSimpleIniA>();
39 };
40
41 CConfigFileMemory::CConfigFileMemory(const std::vector<std::string>& stringList) :
42 m_impl(mrpt::make_impl<CConfigFileMemory::Impl>())
43 {
44 // Load the strings:
45 setContent(stringList);
46 }
47
48 CConfigFileMemory::CConfigFileMemory(const std::string& str) :
49 m_impl(mrpt::make_impl<CConfigFileMemory::Impl>())
50 {
51 setContent(str);
52 }
53
54 /*---------------------------------------------------------------
55 Constructor
56 ---------------------------------------------------------------*/
57 CConfigFileMemory::CConfigFileMemory() : m_impl(mrpt::make_impl<CConfigFileMemory::Impl>()) {}
58
59 void CConfigFileMemory::setContent(const std::vector<std::string>& stringList)
60 {
61 // Load the strings:
62 std::string aux;
63 mrpt::system::stringListAsString(stringList, aux);
64 const auto sOut = mrpt::config::config_parser(aux);
65 m_impl->ini->LoadData(sOut);
66 }
67
68 void CConfigFileMemory::setContent(const std::string& str)
69 {
70 const auto sOut = mrpt::config::config_parser(str);
71 m_impl->ini->LoadData(sOut);
72 }
73
74 void CConfigFileMemory::getContent(std::string& str) const { m_impl->ini->Save(str); }
75
76 CConfigFileMemory::~CConfigFileMemory() = default;
77 void CConfigFileMemory::writeString(
78 const std::string& section, const std::string& name, const std::string& str)
79 {
80 MRPT_START
81
82 SI_Error ret = m_impl->ini->SetValue(section.c_str(), name.c_str(), str.c_str(), nullptr);
83 if (ret < 0) THROW_EXCEPTION("Error changing value in INI-style file!");
84
85 MRPT_END
86 }
87
88 std::string CConfigFileMemory::readString(
89 const std::string& section,
90 const std::string& name,
91 const std::string& defaultStr,
92 bool failIfNotFound) const
93 {
94 MRPT_START
95 const char* defVal = failIfNotFound ? nullptr : defaultStr.c_str();
96
97 const char* aux = m_impl->ini->GetValue(
98 section.c_str(), name.c_str(), defVal,
99 nullptr); // The memory is managed by the SimpleIni object
100
101 if (failIfNotFound && !aux)
102 {
103 string tmpStr(format(
104 "Value '%s' not found in section '%s' of memory configuration "
105 "string list and failIfNotFound=true.",
106 name.c_str(), section.c_str()));
107 THROW_EXCEPTION(tmpStr);
108 }
109
110 // Remove possible comments: "//"
111 std::string ret = aux;
112 size_t pos;
113 if ((pos = ret.find("//")) != string::npos && pos > 0 && isspace(ret[pos - 1]))
114 ret = ret.substr(0, pos);
115 return ret;
116 MRPT_END
117 }
118
119 void CConfigFileMemory::getAllSections(std::vector<std::string>& sections) const
120 {
121 CSimpleIniA::TNamesDepend names;
122 m_impl->ini->GetAllSections(names);
123
124 CSimpleIniA::TNamesDepend::iterator n;
125 std::vector<std::string>::iterator s;
126 sections.resize(names.size());
127 for (n = names.begin(), s = sections.begin(); n != names.end(); ++n, ++s) *s = n->pItem;
128 }
129
130 void CConfigFileMemory::getAllKeys(const string& section, std::vector<std::string>& keys) const
131 {
132 CSimpleIniA::TNamesDepend names;
133 m_impl->ini->GetAllKeys(section.c_str(), names);
134
135 CSimpleIniA::TNamesDepend::iterator n;
136 std::vector<std::string>::iterator s;
137 keys.resize(names.size());
138 for (n = names.begin(), s = keys.begin(); n != names.end(); ++n, ++s) *s = n->pItem;
139 }
140
141 void CConfigFileMemory::clear() { m_impl->ini->Reset(); }