19 #include <mrpt/config.h> 21 #include <yaml-cpp/yaml.h> 37 CConfigFileBase::~CConfigFileBase() =
default;
38 void CConfigFileBase::write(
39 const std::string& section,
const std::string& name,
double value,
40 const int name_padding_width,
const int value_padding_width,
41 const std::string& comment)
46 ((std::abs(value) > 1e-4 && std::abs(value) < 1e4) || value == .0)
50 name_padding_width, value_padding_width, comment);
52 void CConfigFileBase::write(
53 const std::string& section,
const std::string& name,
float value,
54 const int name_padding_width,
const int value_padding_width,
55 const std::string& comment)
60 ((std::abs(value) > 1e-4f && std::abs(value) < 1e4f) ||
65 name_padding_width, value_padding_width, comment);
70 void CConfigFileBase::writeString(
71 const std::string& section,
const std::string& name,
const std::string& str,
72 const int name_padding_width,
const int value_padding_width,
73 const std::string& comment)
75 if (name_padding_width < 1 && value_padding_width < 1 && comment.empty())
76 this->writeString(section, name, str);
79 if (name_padding_width >= 1)
81 "%*s", -name_padding_width,
86 std::string value_pad;
87 if (value_padding_width >= 1)
89 " %*s", -value_padding_width,
96 value_pad += std::string(
" // ");
100 this->writeString(section, name_pad, value_pad);
106 double CConfigFileBase::read_double(
107 const std::string& section,
const std::string& name,
double defaultValue,
108 bool failIfNotFound)
const 111 readString(section, name,
format(
"%.16e", defaultValue), failIfNotFound)
118 float CConfigFileBase::read_float(
119 const std::string& section,
const std::string& name,
float defaultValue,
120 bool failIfNotFound)
const 123 readString(section, name,
format(
"%.10e", defaultValue), failIfNotFound)
130 int CConfigFileBase::read_int(
131 const std::string& section,
const std::string& name,
int defaultValue,
132 bool failIfNotFound)
const 135 readString(section, name,
format(
"%i", defaultValue), failIfNotFound)
142 uint64_t CConfigFileBase::read_uint64_t(
143 const std::string& section,
const std::string& name, uint64_t defaultValue,
144 bool failIfNotFound)
const 146 string s = readString(
147 section, name,
format(
"%lu", (
long unsigned int)defaultValue),
155 bool CConfigFileBase::read_bool(
156 const std::string& section,
const std::string& name,
bool defaultValue,
157 bool failIfNotFound)
const 160 section, name,
string(defaultValue ?
"1" :
"0"), failIfNotFound)));
161 if (s ==
"true")
return true;
162 if (s ==
"false")
return false;
163 if (s ==
"yes")
return true;
164 if (s ==
"no")
return false;
165 return (0 != atoi(s.c_str()));
171 std::string CConfigFileBase::read_string(
172 const std::string& section,
const std::string& name,
173 const std::string& defaultValue,
bool failIfNotFound)
const 176 readString(section, name, defaultValue, failIfNotFound));
182 std::string CConfigFileBase::read_string_first_word(
183 const std::string& section,
const std::string& name,
184 const std::string& defaultValue,
bool failIfNotFound)
const 186 string s = readString(section, name, defaultValue, failIfNotFound);
187 std::vector<std::string> auxStrs;
194 "Value '%s' seems to be present in section '%s' but, are " 196 name.c_str(), section.c_str()));
205 bool CConfigFileBase::sectionExists(
const std::string& section_name)
const 207 std::vector<std::string> sects;
208 getAllSections(sects);
209 for (
auto&
sect : sects)
215 bool CConfigFileBase::keyExists(
216 const std::string& section,
const std::string& key)
const 218 std::vector<std::string> keys;
219 getAllKeys(section, keys);
225 void CConfigFileBase::setContentFromYAML(
const std::string& yaml_block)
231 YAML::Node root = YAML::Load(yaml_block);
235 using key_values_t = std::map<std::string, std::string>;
236 key_values_t unscoped;
237 std::map<std::string, key_values_t> sections;
239 for (
const auto&
sect : root)
241 const auto sectName =
sect.first.as<std::string>();
244 if (
sect.second.size() >= 1)
247 for (
const auto& kv :
sect.second)
249 const auto key = kv.first.as<std::string>();
250 const auto val = kv.second.as<std::string>();
251 sections[sectName].emplace(key,
val);
257 const auto key =
sect.first.as<std::string>();
258 const auto val =
sect.second.as<std::string>();
259 unscoped.emplace(key,
val);
264 for (
const auto& kv : unscoped) this->write(
"", kv.first, kv.second);
266 for (
const auto&
sect : sections)
267 for (
const auto& kv :
sect.second)
268 this->write(
sect.first, kv.first, kv.second);
272 "This method requires building MRPT with yaml-cpp support.");
277 std::string CConfigFileBase::getContentAsYAML()
const 284 std::vector<std::string> lstSects;
285 getAllSections(lstSects);
288 lstSects.insert(lstSects.begin(), std::string());
291 for (
const auto&
sect : lstSects)
293 std::vector<std::string> keys;
294 getAllKeys(
sect, keys);
295 for (
const auto& k : keys)
297 const auto val = this->readString(
sect, k,
"");
306 std::stringstream ss;
311 "This method requires building MRPT with yaml-cpp support.");
#define THROW_EXCEPTION(msg)
std::string std::string format(std::string_view fmt, ARGS &&... args)
int MRPT_SAVE_NAME_PADDING()
Default padding sizes for macros MRPT_SAVE_CONFIG_VAR_COMMENT(), etc.
void tokenize(const std::string &inString, const std::string &inDelimiters, OUT_CONTAINER &outTokens, bool skipBlankTokens=true) noexcept
Tokenizes a string according to a set of delimiting characters.
std::string lowerCase(const std::string &str)
Returns an lower-case version of a string.
int MRPT_SAVE_VALUE_PADDING()
static std::string & trim(std::string &s)
static int MRPT_SAVE_VALUE_PADDING
uint64_t _strtoull(const char *nptr, char **endptr, int base)
An OS-independent version of strtoull.
std::string trim(const std::string &str)
Removes leading and trailing spaces.
static int MRPT_SAVE_NAME_PADDING
void clear()
Clear the contents of this container.
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.