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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019