Debian Codegraph @

libs/config/src/CLoadableOptions.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/CConfigFile.h>
13 #include <mrpt/config/CConfigFileMemory.h>
14 #include <mrpt/config/CLoadableOptions.h>
15 #include <mrpt/core/format.h>
16
17 #include <iostream>
18
19 using namespace mrpt::config;
20
21 const int LOADABLEOPTS_COLUMN_WIDTH = 41; // Until the "=" in each row.
22
23 void CLoadableOptions::loadFromConfigFileName(
24 const std::string& config_file, const std::string& section)
25 {
26 CConfigFile f(config_file);
27 this->loadFromConfigFile(f, section);
28 }
29
30 void CLoadableOptions::saveToConfigFile(
31 [[maybe_unused]] CConfigFileBase& target, [[maybe_unused]] const std::string& section) const
32 {
33 throw std::logic_error("The child class does not implement this method.");
34 }
35
36 void CLoadableOptions::saveToConfigFileName(
37 const std::string& config_file, const std::string& section) const
38 {
39 CConfigFile f(config_file);
40 this->saveToConfigFile(f, section);
41 }
42
43 void CLoadableOptions::dumpToConsole() const { dumpToTextStream(std::cout); }
44 void CLoadableOptions::dumpVar_int(std::ostream& out, const char* varName, int v)
45 {
46 out << mrpt::format("%-*s= %i\n", LOADABLEOPTS_COLUMN_WIDTH, varName, v);
47 }
48
49 void CLoadableOptions::dumpVar_float(std::ostream& out, const char* varName, float v)
50 {
51 out << mrpt::format("%-*s= %f\n", LOADABLEOPTS_COLUMN_WIDTH, varName, v);
52 }
53
54 void CLoadableOptions::dumpVar_double(std::ostream& out, const char* varName, double v)
55 {
56 out << mrpt::format("%-*s= %f\n", LOADABLEOPTS_COLUMN_WIDTH, varName, v);
57 }
58
59 void CLoadableOptions::dumpVar_bool(std::ostream& out, const char* varName, bool v)
60 {
61 out << mrpt::format("%-*s= %s\n", LOADABLEOPTS_COLUMN_WIDTH, varName, v ? "YES" : "NO");
62 }
63
64 void CLoadableOptions::dumpVar_string(std::ostream& out, const char* varName, const std::string& v)
65 {
66 out << mrpt::format("%-*s= %s\n", LOADABLEOPTS_COLUMN_WIDTH, varName, v.c_str());
67 }
68
69 void CLoadableOptions::dumpToTextStream(std::ostream& out) const
70 {
71 CConfigFileMemory cfg;
72 this->saveToConfigFile(cfg, "");
73 out << cfg.getContent();
74 }