Main MRPT website > C++ reference for MRPT 1.5.6
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 
22 
23 CConfigFileBase::~CConfigFileBase()
24 {
25 }
26 
27 void CConfigFileBase::write(const std::string &section, const std::string &name, double value, const int name_padding_width, const int value_padding_width, const std::string &comment )
28 {
29  writeString(section, name, format(((std::abs(value)>1e-4 && std::abs(value)<1e4 ) || value==.0)? "%f" : "%e",value), name_padding_width,value_padding_width,comment);
30 }
31 void CConfigFileBase::write(const std::string &section, const std::string &name, float value, const int name_padding_width, const int value_padding_width, const std::string &comment )
32 {
33  writeString(section, name, format(((std::abs(value)>1e-4f && std::abs(value)<1e4f ) || value==.0f) ? "%f" : "%e",value), name_padding_width,value_padding_width,comment);
34 }
35 
36 /** Write a generic string with optional padding and a comment field ("// ...") at the end of the line. */
37 void CConfigFileBase::writeString(const std::string &section,const std::string &name, const std::string &str, const int name_padding_width, const int value_padding_width, const std::string &comment)
38 {
39  if (name_padding_width<1 && value_padding_width<1 && comment.empty())
40  this->writeString(section,name,str); // Default (old) behavior.
41 
42  std::string name_pad;
43  if (name_padding_width>=1)
44  name_pad = mrpt::format("%*s",-name_padding_width, name.c_str()); // negative width: printf right padding
45  else name_pad = name;
46 
47  std::string value_pad;
48  if (value_padding_width>=1)
49  value_pad = mrpt::format(" %*s",-value_padding_width, str.c_str()); // negative width: printf right padding
50  else value_pad = str;
51 
52  if (!comment.empty())
53  {
54  value_pad += std::string(" // ");
55  value_pad += comment;
56  }
57 
58  this->writeString(section,name_pad,value_pad);
59 }
60 
61 
62 /*---------------------------------------------------------------
63  read_double
64  ---------------------------------------------------------------*/
65 double CConfigFileBase::read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound ) const
66 {
67  return atof( readString(section,name,format("%.16e",defaultValue),failIfNotFound).c_str());
68 }
69 
70 /*---------------------------------------------------------------
71  read_float
72  ---------------------------------------------------------------*/
73 float CConfigFileBase::read_float(const std::string &section, const std::string &name, float defaultValue, bool failIfNotFound ) const
74 {
75  return (float)atof( readString(section,name,format("%.10e",defaultValue),failIfNotFound).c_str());
76 }
77 
78 /*---------------------------------------------------------------
79  read_int
80  ---------------------------------------------------------------*/
81 int CConfigFileBase::read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound ) const
82 {
83  return atoi( readString(section,name,format("%i",defaultValue),failIfNotFound).c_str());
84 }
85 
86 
87 /*---------------------------------------------------------------
88  read_uint64_t
89  ---------------------------------------------------------------*/
90 uint64_t CConfigFileBase::read_uint64_t(const std::string &section, const std::string &name, uint64_t defaultValue, bool failIfNotFound ) const
91 {
92  string s = readString(section,name,format("%lu",(long unsigned int)defaultValue),failIfNotFound);
93  return mrpt::system::os::_strtoull(s.c_str(),NULL, 0);
94 }
95 
96 /*---------------------------------------------------------------
97  read_bool
98  ---------------------------------------------------------------*/
99 bool CConfigFileBase::read_bool(const std::string &section, const std::string &name, bool defaultValue, bool failIfNotFound ) const
100 {
101  const string s = mrpt::system::lowerCase(trim(readString(section,name,string(defaultValue ? "1":"0"),failIfNotFound)));
102  if (s=="true") return true;
103  if (s=="false") return false;
104  if (s=="yes") return true;
105  if (s=="no") return false;
106  return (0 != atoi(s.c_str()));
107 }
108 
109 /*---------------------------------------------------------------
110  read_string
111  ---------------------------------------------------------------*/
112 std::string CConfigFileBase::read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound ) const
113 {
114  return mrpt::system::trim(readString(section, name, defaultValue,failIfNotFound ));
115 }
116 
117 /*---------------------------------------------------------------
118  read_string_first_word
119  ---------------------------------------------------------------*/
120 std::string CConfigFileBase::read_string_first_word(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound ) const
121 {
122  string s = readString(section, name, defaultValue,failIfNotFound );
123  vector_string auxStrs;
124  mrpt::system::tokenize(s,"[], \t", auxStrs);
125  if (auxStrs.empty())
126  {
127  if (failIfNotFound)
128  {
129  THROW_EXCEPTION( format("Value '%s' seems to be present in section '%s' but, are all whitespaces??",
130  name.c_str(),
131  section.c_str() ) );
132  }
133  else return "";
134  }
135  else return auxStrs[0];
136 }
137 
138 /** Checks if a given section exists (name is case insensitive) */
139 bool CConfigFileBase::sectionExists( const std::string &section_name) const
140 {
141  vector_string sects;
142  getAllSections(sects);
143  for (vector_string::iterator s=sects.begin();s!=sects.end();++s)
144  if (!mrpt::system::os::_strcmpi(section_name.c_str(), s->c_str() ))
145  return true;
146  return false;
147 }
148 
int BASE_IMPEXP MRPT_SAVE_VALUE_PADDING
Default padding sizes for macros MRPT_SAVE_CONFIG_VAR_COMMENT(), etc.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
#define THROW_EXCEPTION(msg)
Scalar * iterator
Definition: eigen_plugins.h:23
STL namespace.
GLdouble s
Definition: glext.h:3602
std::vector< std::string > vector_string
A type for passing a vector of strings.
Definition: types_simple.h:30
std::string BASE_IMPEXP lowerCase(const std::string &str)
Returns an lower-case version of a string.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
GLsizei const GLchar ** string
Definition: glext.h:3919
unsigned __int64 uint64_t
Definition: rptypes.h:52
int BASE_IMPEXP _strcmpi(const char *str1, const char *str2) MRPT_NO_THROWS
An OS-independent version of strcmpi.
Definition: os.cpp:320
GLuint const GLchar * name
Definition: glext.h:3891
uint64_t BASE_IMPEXP _strtoull(const char *nptr, char **endptr, int base)
An OS-independent version of strtoull.
Definition: os.cpp:472
std::string BASE_IMPEXP trim(const std::string &str)
Removes leading and trailing spaces.
GLenum GLsizei GLenum format
Definition: glext.h:3513
GLsizei const GLfloat * value
Definition: glext.h:3929
void BASE_IMPEXP tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) MRPT_NO_THROWS
Tokenizes a string according to a set of delimiting characters.
int BASE_IMPEXP MRPT_SAVE_NAME_PADDING



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019