Debian Codegraph @

libs/config/src/CConfigFileBase.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 #include <mrpt/config/CConfigFileBase.h>
13 #include <mrpt/containers/yaml.h>
14 #include <mrpt/core/exceptions.h>
15 #include <mrpt/core/format.h>
16 #include <mrpt/system/os.h>
17 #include <mrpt/system/string_utils.h>
18
19 #include <cmath> // abs()
20
21 using namespace std;
22 using namespace mrpt::config;
23 using namespace mrpt::system;
24 using mrpt::format;
25
26 static int MRPT_SAVE_NAME_PADDING = 50;
27 static int MRPT_SAVE_VALUE_PADDING = 20;
28 int mrpt::config::MRPT_SAVE_NAME_PADDING() { return ::MRPT_SAVE_NAME_PADDING; }
29 int mrpt::config::MRPT_SAVE_VALUE_PADDING() { return ::MRPT_SAVE_VALUE_PADDING; }
30
31 CConfigFileBase::~CConfigFileBase() = default;
32 void CConfigFileBase::write(
33 const std::string& section,
34 const std::string& name,
35 double value,
36 const int name_padding_width,
37 const int value_padding_width,
38 const std::string& comment)
39 {
40 writeString(
41 section, name,
42 mrpt::format(
43 ((std::abs(value) > 1e-4 && std::abs(value) < 1e4) || value == .0) ? "%f" : "%e", value),
44 name_padding_width, value_padding_width, comment);
45 }
46 void CConfigFileBase::write(
47 const std::string& section,
48 const std::string& name,
49 float value,
50 const int name_padding_width,
51 const int value_padding_width,
52 const std::string& comment)
53 {
54 writeString(
55 section, name,
56 mrpt::format(
57 ((std::abs(value) > 1e-4f && std::abs(value) < 1e4f) || value == .0f) ? "%f" : "%e",
58 value),
59 name_padding_width, value_padding_width, comment);
60 }
61
62 /** Write a generic string with optional padding and a comment field ("// ...")
63 * at the end of the line. */
64 void CConfigFileBase::writeString(
65 const std::string& section,
66 const std::string& name,
67 const std::string& str,
68 const int name_padding_width,
69 const int value_padding_width,
70 const std::string& comment)
71 {
72 if (name_padding_width < 1 && value_padding_width < 1 && comment.empty())
73 this->writeString(section, name, str); // Default (old) behavior.
74
75 std::string name_pad;
76 if (name_padding_width >= 1)
77 name_pad = mrpt::format(
78 "%*s", -name_padding_width,
79 name.c_str()); // negative width: printf right padding
80 else
81 name_pad = name;
82
83 std::string value_pad;
84 if (value_padding_width >= 1)
85 value_pad = mrpt::format(
86 " %*s", -value_padding_width,
87 str.c_str()); // negative width: printf right padding
88 else
89 value_pad = str;
90
91 if (!comment.empty())
92 {
93 value_pad += std::string(" // ");
94 value_pad += comment;
95 }
96
97 this->writeString(section, name_pad, value_pad);
98 }
99
100 /*---------------------------------------------------------------
101 read_double
102 ---------------------------------------------------------------*/
103 double CConfigFileBase::read_double(
104 const std::string& section,
105 const std::string& name,
106 double defaultValue,
107 bool failIfNotFound) const
108 {
109 return atof(
110 readString(section, name, mrpt::format("%.16e", defaultValue), failIfNotFound).c_str());
111 }
112
113 /*---------------------------------------------------------------
114 read_float
115 ---------------------------------------------------------------*/
116 float CConfigFileBase::read_float(
117 const std::string& section,
118 const std::string& name,
119 float defaultValue,
120 bool failIfNotFound) const
121 {
122 return (float)atof(
123 readString(section, name, mrpt::format("%.10e", defaultValue), failIfNotFound).c_str());
124 }
125
126 /*---------------------------------------------------------------
127 read_int
128 ---------------------------------------------------------------*/
129 int CConfigFileBase::read_int(
130 const std::string& section,
131 const std::string& name,
132 int defaultValue,
133 bool failIfNotFound) const
134 {
135 return atoi(readString(section, name, mrpt::format("%i", defaultValue), failIfNotFound).c_str());
136 }
137
138 /*---------------------------------------------------------------
139 read_uint64_t
140 ---------------------------------------------------------------*/
141 uint64_t CConfigFileBase::read_uint64_t(
142 const std::string& section,
143 const std::string& name,
144 uint64_t defaultValue,
145 bool failIfNotFound) const
146 {
147 string s = readString(
148 section, name, mrpt::format("%lu", (long unsigned int)defaultValue), failIfNotFound);
149 return mrpt::system::os::_strtoull(s.c_str(), nullptr, 0);
150 }
151
152 /*---------------------------------------------------------------
153 read_bool
154 ---------------------------------------------------------------*/
155 bool CConfigFileBase::read_bool(
156 const std::string& section,
157 const std::string& name,
158 bool defaultValue,
159 bool failIfNotFound) const
160 {
161 const string s = mrpt::system::lowerCase(
162 trim(readString(section, name, string(defaultValue ? "1" : "0"), failIfNotFound)));
163 if (s == "true") return true;
164 if (s == "false") return false;
165 if (s == "yes") return true;
166 if (s == "no") return false;
167 return (0 != atoi(s.c_str()));
168 }
169
170 /*---------------------------------------------------------------
171 read_string
172 ---------------------------------------------------------------*/
173 std::string CConfigFileBase::read_string(
174 const std::string& section,
175 const std::string& name,
176 const std::string& defaultValue,
177 bool failIfNotFound) const
178 {
179 return mrpt::system::trim(readString(section, name, defaultValue, failIfNotFound));
180 }
181
182 /*---------------------------------------------------------------
183 read_string_first_word
184 ---------------------------------------------------------------*/
185 std::string CConfigFileBase::read_string_first_word(
186 const std::string& section,
187 const std::string& name,
188 const std::string& defaultValue,
189 bool failIfNotFound) const
190 {
191 string s = readString(section, name, defaultValue, failIfNotFound);
192 std::vector<std::string> auxStrs;
193 mrpt::system::tokenize(s, "[], \t", auxStrs);
194 if (auxStrs.empty())
195 {
196 if (failIfNotFound)
197 {
198 THROW_EXCEPTION(format(
199 "Value '%s' seems to be present in section '%s' but, are "
200 "all whitespaces??",
201 name.c_str(), section.c_str()));
202 }
203 else
204 return "";
205 }
206 else
207 return auxStrs[0];
208 }
209
210 bool CConfigFileBase::sectionExists(const std::string& section_name) const
211 {
212 std::vector<std::string> sects;
213 getAllSections(sects);
214 for (auto& sect : sects)
215 if (!mrpt::system::os::_strcmpi(section_name.c_str(), sect.c_str())) return true;
216 return false;
217 }
218
219 bool CConfigFileBase::keyExists(const std::string& section, const std::string& key) const
220 {
221 std::vector<std::string> keys;
222 getAllKeys(section, keys);
223 for (auto& k : keys)
224 if (!mrpt::system::os::_strcmpi(key.c_str(), k.c_str())) return true;
225 return false;
226 }
227
228 void CConfigFileBase::setContentFromYAML(const std::string& yaml_block)
229 {
230 MRPT_START
231 this->clear();
232
233 mrpt::containers::yaml root;
234 root.loadFromText(yaml_block);
235
236 // Prepare all data here, then insert into the actual INI file at the end,
237 // to ensure that unscoped variables get first.
238 using key_values_t = std::map<std::string, std::string>;
239 key_values_t unscoped;
240 std::map<std::string, key_values_t> sections;
241
242 for (const auto& sect : root.asMap())
243 {
244 const auto sectName = sect.first.as<std::string>();
245 // YAML Type: sect.first.Type()
246
247 if (sect.second.size() > 1)
248 {
249 // A section:
250 for (const auto& kv : sect.second.asMap())
251 {
252 const auto key = kv.first.as<std::string>();
253 const auto val = kv.second.as<std::string>();
254 sections[sectName].emplace(key, val);
255 }
256 }
257 else
258 {
259 // an unscoped key-value:
260 const auto key = sect.first.as<std::string>();
261 const auto val = sect.second.as<std::string>();
262 unscoped.emplace(key, val);
263 }
264 }
265
266 // 1st: unscoped:
267 for (const auto& kv : unscoped) this->write("", kv.first, kv.second);
268
269 for (const auto& sect : sections)
270 for (const auto& kv : sect.second) this->write(sect.first, kv.first, kv.second);
271
272 MRPT_END
273 }
274
275 std::string CConfigFileBase::getContentAsYAML() const
276 {
277 MRPT_START
278
279 mrpt::containers::yaml n;
280
281 // Sections:
282 std::vector<std::string> lstSects;
283 getAllSections(lstSects);
284
285 // Unscoped: "" empty section name:
286 lstSects.insert(lstSects.begin(), std::string());
287
288 // Go thru values:
289 for (const auto& sect : lstSects)
290 {
291 std::vector<std::string> keys;
292 getAllKeys(sect, keys);
293 for (const auto& k : keys)
294 {
295 const auto val = this->readString(sect, k, "");
296 if (sect.empty())
297 n[k] = val;
298 else
299 n[sect][k] = val;
300 }
301 }
302
303 // Convert to string:
304 std::stringstream ss;
305 ss << n;
306 return ss.str();
307
308 MRPT_END
309 }