Debian Codegraph @

libs/config/src/CConfigFilePrefixer.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/CConfigFilePrefixer.h>
13
14 using namespace mrpt::config;
15 using namespace std;
16
17 CConfigFilePrefixer::CConfigFilePrefixer() = default;
18 CConfigFilePrefixer::CConfigFilePrefixer(
19 const CConfigFileBase& o, const std::string& prefix_sections, const std::string& prefix_keys) :
20 m_bound_object(const_cast<CConfigFileBase*>(&o)),
21 m_prefix_sections(prefix_sections),
22 m_prefix_keys(prefix_keys)
23 {
24 }
25 CConfigFilePrefixer::~CConfigFilePrefixer()
26 {
27 // Nothing to free manually
28 }
29
30 void CConfigFilePrefixer::bind(const CConfigFileBase& o)
31 {
32 m_bound_object = const_cast<CConfigFileBase*>(&o);
33 }
34
35 void CConfigFilePrefixer::setPrefixes(
36 const std::string& prefix_sections, const std::string& prefix_keys)
37 {
38 m_prefix_sections = prefix_sections;
39 m_prefix_keys = prefix_keys;
40 }
41
42 std::string CConfigFilePrefixer::getSectionPrefix() const { return m_prefix_sections; }
43 std::string CConfigFilePrefixer::getKeyPrefix() const { return m_prefix_keys; }
44 CConfigFileBase* CConfigFilePrefixer::getBoundConfigFileBase() const { return m_bound_object; }
45
46 void CConfigFilePrefixer::ensureIsBound() const
47 {
48 ASSERTMSG_(m_bound_object, "You must first bind CConfigFilePrefixer to an existing object!");
49 }
50
51 void CConfigFilePrefixer::getAllSections(std::vector<std::string>& sections) const
52 {
53 ensureIsBound();
54 m_bound_object->getAllSections(sections);
55 for (auto& section : sections) section = m_prefix_sections + section;
56 }
57
58 void CConfigFilePrefixer::getAllKeys(
59 const std::string& section, std::vector<std::string>& keys) const
60 {
61 ensureIsBound();
62 m_bound_object->getAllKeys(section, keys);
63 for (auto& key : keys) key = m_prefix_keys + key;
64 }
65 void CConfigFilePrefixer::clear()
66 {
67 ensureIsBound();
68 m_bound_object->clear();
69 }
70
71 void CConfigFilePrefixer::writeString(
72 const std::string& section, const std::string& name, const std::string& str)
73 {
74 ensureIsBound();
75 m_bound_object->writeString(m_prefix_sections + section, m_prefix_keys + name, str);
76 }
77
78 std::string CConfigFilePrefixer::readString(
79 const std::string& section,
80 const std::string& name,
81 const std::string& defaultStr,
82 bool failIfNotFound) const
83 {
84 ensureIsBound();
85 return m_bound_object->readString(
86 m_prefix_sections + section, m_prefix_keys + name, defaultStr, failIfNotFound);
87 }