| 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/CConfigFile.h> |
| 29 |
#include <mrpt/config/config_parser.h> |
| 30 |
#include <mrpt/system/os.h> |
| 31 |
|
| 32 |
#include <fstream> |
| 33 |
#include <iostream> |
| 34 |
|
| 35 |
using namespace mrpt; |
| 36 |
using namespace mrpt::config; |
| 37 |
using namespace std; |
| 38 |
|
| 39 |
struct CConfigFile::Impl |
| 40 |
{ |
| 41 |
std::shared_ptr<CSimpleIniA> ini = std::make_shared<CSimpleIniA>(); |
| 42 |
}; |
| 43 |
|
| 44 |
// copied from mrpt-io to avoid lib dependency: |
| 45 |
namespace |
| 46 |
{ |
| 47 |
std::string local_file_get_contents(const std::string& fileName) |
| 48 |
{ |
| 49 |
// Credits: https://stackoverflow.com/a/2602258/1631514 |
| 50 |
// Note: Add "binary" to make sure the "tellg" file size matches the actual |
| 51 |
// number of read bytes afterwards: |
| 52 |
std::ifstream t(fileName, ios::binary); |
| 53 |
if (!t.is_open()) return {}; |
| 54 |
|
| 55 |
t.seekg(0, std::ios::end); |
| 56 |
std::size_t size = t.tellg(); |
| 57 |
std::string buffer(size, ' '); |
| 58 |
t.seekg(0); |
| 59 |
t.read(&buffer[0], size); |
| 60 |
return buffer; |
| 61 |
} |
| 62 |
} // namespace |
| 63 |
|
| 64 |
/*--------------------------------------------------------------- |
| 65 |
Constructor |
| 66 |
---------------------------------------------------------------*/ |
| 67 |
CConfigFile::CConfigFile(const std::string& fileName) : m_impl(mrpt::make_impl<CConfigFile::Impl>()) |
| 68 |
{ |
| 69 |
MRPT_START |
| 70 |
|
| 71 |
m_file = fileName; |
| 72 |
m_modified = false; |
| 73 |
|
| 74 |
const auto sIn = local_file_get_contents(fileName); |
| 75 |
const auto sOut = mrpt::config::config_parser(sIn); |
| 76 |
m_impl->ini->LoadData(sOut); |
| 77 |
|
| 78 |
MRPT_END |
| 79 |
} |
| 80 |
|
| 81 |
/*--------------------------------------------------------------- |
| 82 |
Constructor |
| 83 |
---------------------------------------------------------------*/ |
| 84 |
CConfigFile::CConfigFile() : m_impl(mrpt::make_impl<CConfigFile::Impl>()) |
| 85 |
{ |
| 86 |
MRPT_START |
| 87 |
|
| 88 |
m_file = ""; |
| 89 |
m_modified = false; |
| 90 |
|
| 91 |
MRPT_END |
| 92 |
} |
| 93 |
|
| 94 |
/*--------------------------------------------------------------- |
| 95 |
setFileName |
| 96 |
---------------------------------------------------------------*/ |
| 97 |
void CConfigFile::setFileName(const std::string& fil_path) |
| 98 |
{ |
| 99 |
MRPT_START |
| 100 |
|
| 101 |
m_file = fil_path; |
| 102 |
m_modified = false; |
| 103 |
|
| 104 |
m_impl->ini->LoadFile(fil_path.c_str()); |
| 105 |
MRPT_END |
| 106 |
} |
| 107 |
|
| 108 |
/*--------------------------------------------------------------- |
| 109 |
writeNow |
| 110 |
---------------------------------------------------------------*/ |
| 111 |
void CConfigFile::writeNow() |
| 112 |
{ |
| 113 |
MRPT_START |
| 114 |
if (m_modified && !m_file.empty()) |
| 115 |
{ |
| 116 |
if (SI_Error err = m_impl->ini->SaveFile(m_file.c_str()); err != SI_OK) |
| 117 |
{ |
| 118 |
THROW_EXCEPTION_FMT( |
| 119 |
"SimpleIni error %i for file='%s'", static_cast<int>(err), m_file.c_str()); |
| 120 |
} |
| 121 |
m_modified = false; |
| 122 |
} |
| 123 |
MRPT_END |
| 124 |
} |
| 125 |
|
| 126 |
void CConfigFile::discardSavingChanges() { m_modified = false; } |
| 127 |
|
| 128 |
CConfigFile::~CConfigFile() |
| 129 |
{ |
| 130 |
try |
| 131 |
{ |
| 132 |
writeNow(); |
| 133 |
} |
| 134 |
catch (const std::exception& e) |
| 135 |
{ |
| 136 |
std::cerr << "[~CConfigFile] Exception:\n" << mrpt::exception_to_str(e); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void CConfigFile::writeString( |
| 141 |
const std::string& section, const std::string& name, const std::string& str) |
| 142 |
{ |
| 143 |
MRPT_START |
| 144 |
|
| 145 |
m_modified = true; |
| 146 |
|
| 147 |
if (0 > m_impl->ini->SetValue(section.c_str(), name.c_str(), str.c_str(), nullptr)) |
| 148 |
THROW_EXCEPTION("Error changing value in INI-style file!"); |
| 149 |
|
| 150 |
MRPT_END |
| 151 |
} |
| 152 |
|
| 153 |
/*--------------------------------------------------------------- |
| 154 |
readString |
| 155 |
---------------------------------------------------------------*/ |
| 156 |
std::string CConfigFile::readString( |
| 157 |
const std::string& section, |
| 158 |
const std::string& name, |
| 159 |
const std::string& defaultStr, |
| 160 |
bool failIfNotFound) const |
| 161 |
{ |
| 162 |
MRPT_START |
| 163 |
const char* defVal = failIfNotFound ? nullptr : defaultStr.c_str(); |
| 164 |
|
| 165 |
const char* aux = m_impl->ini->GetValue( |
| 166 |
section.c_str(), name.c_str(), defVal, |
| 167 |
nullptr); // The memory is managed by the SimpleIni object |
| 168 |
|
| 169 |
if (failIfNotFound && !aux) |
| 170 |
{ |
| 171 |
string tmpStr(format( |
| 172 |
"Value '%s' not found in section '%s' of file '%s' and " |
| 173 |
"failIfNotFound=true.", |
| 174 |
name.c_str(), section.c_str(), m_file.c_str())); |
| 175 |
THROW_EXCEPTION(tmpStr); |
| 176 |
} |
| 177 |
|
| 178 |
// Remove possible comments: "//" |
| 179 |
std::string ret = aux; |
| 180 |
size_t pos; |
| 181 |
if ((pos = ret.find("//")) != string::npos && pos > 0 && isspace(ret[pos - 1])) |
| 182 |
ret = ret.substr(0, pos); |
| 183 |
return ret; |
| 184 |
|
| 185 |
MRPT_END |
| 186 |
} |
| 187 |
|
| 188 |
/*--------------------------------------------------------------- |
| 189 |
getAllSections |
| 190 |
---------------------------------------------------------------*/ |
| 191 |
void CConfigFile::getAllSections(std::vector<std::string>& sections) const |
| 192 |
{ |
| 193 |
CSimpleIniA::TNamesDepend names; |
| 194 |
m_impl->ini->GetAllSections(names); |
| 195 |
|
| 196 |
CSimpleIniA::TNamesDepend::iterator n; |
| 197 |
std::vector<std::string>::iterator s; |
| 198 |
sections.resize(names.size()); |
| 199 |
for (n = names.begin(), s = sections.begin(); n != names.end(); ++n, ++s) *s = n->pItem; |
| 200 |
} |
| 201 |
|
| 202 |
/*--------------------------------------------------------------- |
| 203 |
getAllKeys |
| 204 |
---------------------------------------------------------------*/ |
| 205 |
void CConfigFile::getAllKeys(const string& section, std::vector<std::string>& keys) const |
| 206 |
{ |
| 207 |
CSimpleIniA::TNamesDepend names; |
| 208 |
m_impl->ini->GetAllKeys(section.c_str(), names); |
| 209 |
|
| 210 |
CSimpleIniA::TNamesDepend::iterator n; |
| 211 |
std::vector<std::string>::iterator s; |
| 212 |
keys.resize(names.size()); |
| 213 |
for (n = names.begin(), s = keys.begin(); n != names.end(); ++n, ++s) *s = n->pItem; |
| 214 |
} |
| 215 |
|
| 216 |
void CConfigFile::clear() { m_impl->ini->Reset(); } |