19 #include <mrpt/config.h> 21 #include <yaml-cpp/yaml.h> 37 CConfigFileBase::~CConfigFileBase() =
default;
38 void CConfigFileBase::write(
40 const int name_padding_width,
const int value_padding_width,
50 name_padding_width, value_padding_width, comment);
52 void CConfigFileBase::write(
54 const int name_padding_width,
const int value_padding_width,
60 ((std::abs(
value) > 1e-4f && std::abs(
value) < 1e4f) ||
65 name_padding_width, value_padding_width, comment);
70 void CConfigFileBase::writeString(
72 const int name_padding_width,
const int value_padding_width,
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,
87 if (value_padding_width >= 1)
89 " %*s", -value_padding_width,
100 this->writeString(section, name_pad, value_pad);
106 double CConfigFileBase::read_double(
108 bool failIfNotFound)
const 111 readString(section,
name,
format(
"%.16e", defaultValue), failIfNotFound)
118 float CConfigFileBase::read_float(
120 bool failIfNotFound)
const 123 readString(section,
name,
format(
"%.10e", defaultValue), failIfNotFound)
130 int CConfigFileBase::read_int(
132 bool failIfNotFound)
const 135 readString(section,
name,
format(
"%i", defaultValue), failIfNotFound)
144 bool failIfNotFound)
const 146 string s = readString(
147 section,
name,
format(
"%lu", (
long unsigned int)defaultValue),
155 bool CConfigFileBase::read_bool(
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()));
173 const std::string& defaultValue,
bool failIfNotFound)
const 176 readString(section,
name, defaultValue, failIfNotFound));
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()));
206 bool CConfigFileBase::sectionExists(
const std::string& section_name)
const 208 std::vector<std::string> sects;
209 getAllSections(sects);
210 for (
auto& sect : sects)
216 void CConfigFileBase::setContentFromYAML(
const std::string& yaml_block)
222 YAML::Node root = YAML::Load(yaml_block);
226 using key_values_t = std::map<std::string, std::string>;
227 key_values_t unscoped;
228 std::map<std::string, key_values_t> sections;
230 for (
const auto& sect : root)
232 const auto sectName = sect.first.as<
std::string>();
235 if (sect.second.size() >= 1)
238 for (
const auto& kv : sect.second)
242 sections[sectName].emplace(key,
val);
250 unscoped.emplace(key,
val);
255 for (
const auto& kv : unscoped) this->write(
"", kv.first, kv.second);
257 for (
const auto& sect : sections)
258 for (
const auto& kv : sect.second)
259 this->write(sect.first, kv.first, kv.second);
263 "This method requires building MRPT with yaml-cpp support.");
275 std::vector<std::string> lstSects;
276 getAllSections(lstSects);
282 for (
const auto& sect : lstSects)
284 std::vector<std::string> keys;
285 getAllKeys(sect, keys);
286 for (
const auto& k : keys)
288 const auto val = this->readString(sect, k,
"");
297 std::stringstream ss;
302 "This method requires building MRPT with yaml-cpp support.");
#define THROW_EXCEPTION(msg)
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
GLsizei const GLchar ** string
unsigned __int64 uint64_t
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
GLuint const GLchar * name
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
GLenum GLsizei GLenum format
GLsizei const GLfloat * value
void clear()
Clear the contents of this container.
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.