Main MRPT website > C++ reference for MRPT 1.9.9
CConfigFileBase.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "config-precomp.h" // Precompiled headers
11 
14 #include <mrpt/system/os.h>
15 #include <mrpt/core/format.h>
16 #include <mrpt/core/exceptions.h>
17 #include <cmath> // abs()
18 
19 using namespace std;
20 using namespace mrpt::config;
21 using namespace mrpt::system;
22 using mrpt::format;
23 
24 static int MRPT_SAVE_NAME_PADDING = 50;
25 static int MRPT_SAVE_VALUE_PADDING = 20;
28 {
30 }
31 
32 CConfigFileBase::~CConfigFileBase() {}
33 void CConfigFileBase::write(
34  const std::string& section, const std::string& name, double value,
35  const int name_padding_width, const int value_padding_width,
36  const std::string& comment)
37 {
38  writeString(
39  section, name,
40  format(
41  ((std::abs(value) > 1e-4 && std::abs(value) < 1e4) || value == .0)
42  ? "%f"
43  : "%e",
44  value),
45  name_padding_width, value_padding_width, comment);
46 }
47 void CConfigFileBase::write(
48  const std::string& section, const std::string& name, float value,
49  const int name_padding_width, const int value_padding_width,
50  const std::string& comment)
51 {
52  writeString(
53  section, name,
54  format(
55  ((std::abs(value) > 1e-4f && std::abs(value) < 1e4f) ||
56  value == .0f)
57  ? "%f"
58  : "%e",
59  value),
60  name_padding_width, value_padding_width, comment);
61 }
62 
63 /** Write a generic string with optional padding and a comment field ("// ...")
64  * at the end of the line. */
65 void CConfigFileBase::writeString(
66  const std::string& section, const std::string& name, const std::string& str,
67  const int name_padding_width, const int value_padding_width,
68  const std::string& comment)
69 {
70  if (name_padding_width < 1 && value_padding_width < 1 && comment.empty())
71  this->writeString(section, name, str); // Default (old) behavior.
72 
73  std::string name_pad;
74  if (name_padding_width >= 1)
75  name_pad = mrpt::format(
76  "%*s", -name_padding_width,
77  name.c_str()); // negative width: printf right padding
78  else
79  name_pad = name;
80 
81  std::string value_pad;
82  if (value_padding_width >= 1)
83  value_pad = mrpt::format(
84  " %*s", -value_padding_width,
85  str.c_str()); // negative width: printf right padding
86  else
87  value_pad = str;
88 
89  if (!comment.empty())
90  {
91  value_pad += std::string(" // ");
92  value_pad += comment;
93  }
94 
95  this->writeString(section, name_pad, value_pad);
96 }
97 
98 /*---------------------------------------------------------------
99  read_double
100  ---------------------------------------------------------------*/
101 double CConfigFileBase::read_double(
102  const std::string& section, const std::string& name, double defaultValue,
103  bool failIfNotFound) const
104 {
105  return atof(
106  readString(section, name, format("%.16e", defaultValue), failIfNotFound)
107  .c_str());
108 }
109 
110 /*---------------------------------------------------------------
111  read_float
112  ---------------------------------------------------------------*/
113 float CConfigFileBase::read_float(
114  const std::string& section, const std::string& name, float defaultValue,
115  bool failIfNotFound) const
116 {
117  return (float)atof(
118  readString(section, name, format("%.10e", defaultValue), failIfNotFound)
119  .c_str());
120 }
121 
122 /*---------------------------------------------------------------
123  read_int
124  ---------------------------------------------------------------*/
125 int CConfigFileBase::read_int(
126  const std::string& section, const std::string& name, int defaultValue,
127  bool failIfNotFound) const
128 {
129  return atoi(
130  readString(section, name, format("%i", defaultValue), failIfNotFound)
131  .c_str());
132 }
133 
134 /*---------------------------------------------------------------
135  read_uint64_t
136  ---------------------------------------------------------------*/
137 uint64_t CConfigFileBase::read_uint64_t(
138  const std::string& section, const std::string& name, uint64_t defaultValue,
139  bool failIfNotFound) const
140 {
141  string s = readString(
142  section, name, format("%lu", (long unsigned int)defaultValue),
143  failIfNotFound);
144  return mrpt::system::os::_strtoull(s.c_str(), nullptr, 0);
145 }
146 
147 /*---------------------------------------------------------------
148  read_bool
149  ---------------------------------------------------------------*/
150 bool CConfigFileBase::read_bool(
151  const std::string& section, const std::string& name, bool defaultValue,
152  bool failIfNotFound) const
153 {
154  const string s = mrpt::system::lowerCase(
155  trim(
156  readString(
157  section, name, string(defaultValue ? "1" : "0"),
158  failIfNotFound)));
159  if (s == "true") return true;
160  if (s == "false") return false;
161  if (s == "yes") return true;
162  if (s == "no") return false;
163  return (0 != atoi(s.c_str()));
164 }
165 
166 /*---------------------------------------------------------------
167  read_string
168  ---------------------------------------------------------------*/
169 std::string CConfigFileBase::read_string(
170  const std::string& section, const std::string& name,
171  const std::string& defaultValue, bool failIfNotFound) const
172 {
173  return mrpt::system::trim(
174  readString(section, name, defaultValue, failIfNotFound));
175 }
176 
177 /*---------------------------------------------------------------
178  read_string_first_word
179  ---------------------------------------------------------------*/
180 std::string CConfigFileBase::read_string_first_word(
181  const std::string& section, const std::string& name,
182  const std::string& defaultValue, bool failIfNotFound) const
183 {
184  string s = readString(section, name, defaultValue, failIfNotFound);
185  std::vector<std::string> auxStrs;
186  mrpt::system::tokenize(s, "[], \t", auxStrs);
187  if (auxStrs.empty())
188  {
189  if (failIfNotFound)
190  {
192  format(
193  "Value '%s' seems to be present in section '%s' but, are "
194  "all whitespaces??",
195  name.c_str(), section.c_str()));
196  }
197  else
198  return "";
199  }
200  else
201  return auxStrs[0];
202 }
203 
204 /** Checks if a given section exists (name is case insensitive) */
205 bool CConfigFileBase::sectionExists(const std::string& section_name) const
206 {
207  std::vector<std::string> sects;
208  getAllSections(sects);
209  for (std::vector<std::string>::iterator s = sects.begin(); s != sects.end();
210  ++s)
211  if (!mrpt::system::os::_strcmpi(section_name.c_str(), s->c_str()))
212  return true;
213  return false;
214 }
mrpt::config
Definition: config/CConfigFile.h:16
os.h
MRPT_SAVE_VALUE_PADDING
static int MRPT_SAVE_VALUE_PADDING
Definition: CConfigFileBase.cpp:25
format
GLenum GLsizei GLenum format
Definition: glext.h:3531
exceptions.h
trim
static std::string & trim(std::string &s)
Definition: CListOfClasses.cpp:61
s
GLdouble s
Definition: glext.h:3676
MRPT_SAVE_NAME_PADDING
static int MRPT_SAVE_NAME_PADDING
Definition: CConfigFileBase.cpp:24
string_utils.h
format.h
config-precomp.h
mrpt::system::os::_strtoull
uint64_t _strtoull(const char *nptr, char **endptr, int base)
An OS-independent version of strtoull.
Definition: os.cpp:464
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
mrpt::system::tokenize
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.
mrpt::system::lowerCase
std::string lowerCase(const std::string &str)
Returns an lower-case version of a string.
Definition: string_utils.cpp:26
name
GLuint const GLchar * name
Definition: glext.h:4054
CConfigFileBase.h
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
uint64_t
unsigned __int64 uint64_t
Definition: rptypes.h:50
mrpt::config::MRPT_SAVE_VALUE_PADDING
int MRPT_SAVE_VALUE_PADDING()
Definition: CConfigFileBase.cpp:27
value
GLsizei const GLfloat * value
Definition: glext.h:4117
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::system::trim
std::string trim(const std::string &str)
Removes leading and trailing spaces.
Definition: string_utils.cpp:270
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
mrpt::system::os::_strcmpi
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.
Definition: os.cpp:320
mrpt::config::MRPT_SAVE_NAME_PADDING
int MRPT_SAVE_NAME_PADDING()
Default padding sizes for macros MRPT_SAVE_CONFIG_VAR_COMMENT(), etc.
Definition: CConfigFileBase.cpp:26
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST