Main MRPT website > C++ reference
MRPT logo
CConfigFileBase.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2014, 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 #ifndef CConfigFileBase_H
10 #define CConfigFileBase_H
11 
12 #include <mrpt/utils/utils_defs.h>
13 #include <mrpt/utils/TEnumType.h>
16 
17 /*---------------------------------------------------------------
18  Class
19  ---------------------------------------------------------------*/
20 namespace mrpt
21 {
22 namespace utils
23 {
24  /** This class allows loading and storing values and vectors of different types from a configuration text, which can be implemented as a ".ini" file, a memory-stored string, etc...
25  * This is a virtual class, use only as a pointer to an implementation of one of the derived classes.
26  * \ingroup mrpt_base_grp
27  */
29  {
30  protected:
31  /** A virtual method to write a generic string.
32  */
33  virtual void writeString(const std::string &section,const std::string &name, const std::string &str) = 0;
34 
35  /** Write a generic string with optional padding and a comment field ("// ...") at the end of the line. */
36  void 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);
37 
38  /** A virtual method to read a generic string.
39  * \exception std::exception If the key name is not found and "failIfNotFound" is true. Otherwise the "defaultValue" is returned.
40  */
41  virtual std::string readString(
42  const std::string &section,
43  const std::string &name,
44  const std::string &defaultStr,
45  bool failIfNotFound = false) const = 0;
46 
47  public:
48  /** Virtual destructor...
49  */
50  virtual ~CConfigFileBase()
51  {
52  }
53 
54  /** Returns a list with all the section names.
55  */
56  virtual void getAllSections( vector_string &sections ) const = 0 ;
57 
58  /** Returs a list with all the keys into a section.
59  */
60  virtual void getAllKeys( const std::string section, vector_string &keys ) const = 0;
61 
62  /** Checks if a given section exists (name is case insensitive) */
63  bool sectionExists( const std::string &section_name) const;
64 
65  /** @name Save a configuration parameter. Optionally pads with spaces up to the desired width in number of characters (-1: no fill), and add a final comment field at the end of the line (a "// " prefix is automatically inserted).
66  * @{ */
67  void write(const std::string &section, const std::string &name, double value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment = std::string() );
68  void write(const std::string &section, const std::string &name, float value , const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment = std::string() );
69  void write(const std::string &section, const std::string &name, int value , const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment = std::string() );
70  void write(const std::string &section, const std::string &name, uint32_t value , const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment = std::string() );
71  void write(const std::string &section, const std::string &name, uint64_t value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment = std::string() );
72  void write(const std::string &section, const std::string &name, const std::string &value , const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment = std::string() );
73  void write(const std::string &section, const std::string &name, const std::vector<int> &value , const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment = std::string() );
74  void write(const std::string &section, const std::string &name, const std::vector<unsigned int> &value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment = std::string() );
75  void write(const std::string &section, const std::string &name, const std::vector<float> &value , const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment = std::string() );
76  void write(const std::string &section, const std::string &name, const std::vector<double> &value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment = std::string() );
77  void write(const std::string &section, const std::string &name, const std::vector<bool> &value , const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment = std::string() );
78  /** @} */
79 
80  /** Reads a configuration parameter of type "double"
81  * \exception std::exception If the key name is not found and "failIfNotFound" is true. Otherwise the "defaultValue" is returned.
82  */
83  double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound = false) const;
84 
85  /** Reads a configuration parameter of type "float"
86  * \exception std::exception If the key name is not found and "failIfNotFound" is true. Otherwise the "defaultValue" is returned.
87  */
88  float read_float(const std::string &section, const std::string &name, float defaultValue, bool failIfNotFound = false) const;
89 
90  /** Reads a configuration parameter of type "bool", codified as "1"/"0" or "true"/"false" or "yes"/"no" for true/false, repectively.
91  * \exception std::exception If the key name is not found and "failIfNotFound" is true. Otherwise the "defaultValue" is returned.
92  */
93  bool read_bool(const std::string &section, const std::string &name, bool defaultValue, bool failIfNotFound = false) const;
94 
95  /** Reads a configuration parameter of type "int"
96  * \exception std::exception If the key name is not found and "failIfNotFound" is true. Otherwise the "defaultValue" is returned.
97  */
98  int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound = false) const;
99 
100  /** Reads a configuration parameter of type "uint64_t": As in all other methods, the numeric value can be in decimal or hexadecimal with the prefix "0x"
101  * \exception std::exception If the key name is not found and "failIfNotFound" is true. Otherwise the "defaultValue" is returned.
102  */
103  uint64_t read_uint64_t(const std::string &section, const std::string &name, uint64_t defaultValue, bool failIfNotFound = false ) const;
104 
105  /** Reads a configuration parameter of type "string"
106  * \exception std::exception If the key name is not found and "failIfNotFound" is true. Otherwise the "defaultValue" is returned.
107  */
108  std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound = false) const;
109 
110  /** Reads a configuration parameter of type "string", and keeps only the first word (this can be used to eliminate possible comments at the end of the line)
111  * \exception std::exception If the key name is not found and "failIfNotFound" is true. Otherwise the "defaultValue" is returned.
112  */
113  std::string read_string_first_word(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound = false) const;
114 
115  /** Reads a configuration parameter of type vector, stored in the file as a string: "[v1 v2 v3 ... ]", where spaces could also be commas.
116  * \exception std::exception If the key name is not found and "failIfNotFound" is true. Otherwise the "defaultValue" is returned.
117  */
118  template <class VECTOR_TYPE>
120  const std::string & section,
121  const std::string & name,
122  const VECTOR_TYPE & defaultValue,
123  VECTOR_TYPE & outValues,
124  bool failIfNotFound = false) const
125  {
126  std::string aux ( readString(section, name, "",failIfNotFound ) );
127  // Parse the text into a vector:
128  std::vector<std::string> tokens;
129  mrpt::system::tokenize( aux,"[], \t",tokens);
130 
131  if (tokens.size()==0)
132  {
133  outValues = defaultValue;
134  }
135  else
136  {
137  // Parse to numeric type:
138  const size_t N = tokens.size();
139  outValues.resize( N );
140  for (size_t i=0;i<N;i++)
141  {
142  std::stringstream ss(tokens[i]);
143  ss >> outValues[i];
144  }
145  }
146  }
147 
148 
149  /** Reads a configuration parameter as a matrix written in a matlab-like format - for example: "[2 3 4 ; 7 8 9]"
150  * This template method can be instantiated for matrices of the types: int, long, unsinged int, unsigned long, float, double, long double
151  * \exception std::exception If the key name is not found and "failIfNotFound" is true. Otherwise the "defaultValue" is returned.
152  */
153  template <class MATRIX_TYPE>
155  const std::string &section,
156  const std::string &name,
157  MATRIX_TYPE &outMatrix,
158  const MATRIX_TYPE &defaultMatrix = MATRIX_TYPE(),
159  bool failIfNotFound = false ) const
160  {
161  std::string aux = readString(section, name, "",failIfNotFound );
162  if (aux.empty())
163  outMatrix = defaultMatrix;
164  else
165  {
166  // Parse the text into a vector:
167  if (!outMatrix.fromMatlabStringFormat(aux))
168  THROW_EXCEPTION_CUSTOM_MSG1("Error parsing matrix: '%s'",aux.c_str())
169  }
170  }
171 
172  /** Reads an "enum" value, where the value in the config file can be either a numerical value or the symbolic name, for example:
173  * In the code:
174  * \code
175  * enum my_type_t { type_foo=0, type_bar };
176  * \endcode
177  * In the config file:
178  * \code
179  * [section]
180  * type = type_bar // Use the symbolic name, or
181  * type = 1 // use the numerical value (both lines will be equivalent)
182  * \endcode
183  * Which can be loaded with:
184  * \code
185  * cfgfile.read_enum<my_type_t>("section","type", type_foo );
186  * \endcode
187  *
188  * \note For an enum type to work with this template it is required that it defines a specialization of mrpt::utils::TEnumType
189  */
190  template <typename ENUMTYPE>
191  ENUMTYPE read_enum(const std::string &section, const std::string &name, const ENUMTYPE &defaultValue, bool failIfNotFound = false) const
192  {
193  MRPT_START
194  const std::string sVal = read_string_first_word(section,name,"",failIfNotFound);
195  if (sVal.empty()) return defaultValue;
196  // Text or numeric value?
197  if (::isdigit(sVal[0]))
198  { // Seems a number:
199  return static_cast<ENUMTYPE>(::atoi(&sVal[0]));
200  }
201  else
202  { // Name look-up:
203  try {
205  } catch (std::exception &)
206  {
207  THROW_EXCEPTION(format("Invalid value '%s' for enum type while reading key='%s'.",sVal.c_str(),name.c_str()))
208  }
209  }
210  MRPT_END
211  }
212 
213 
214  }; // End of class def.
215 
216  /** An useful macro for loading variables stored in a INI-like file under a key with the same name that the variable, and assigning the variable the current value if not found in the config file.
217  * The variableType must be the suffix of "read_XXX" functions, i.e. int, bool,...
218  */
219 #define MRPT_LOAD_CONFIG_VAR(variableName,variableType,configFileObject,sectionNameStr) \
220  { variableName = configFileObject.read_##variableType(sectionNameStr,#variableName,variableName); }
221 
222  /** Loads a float variable, stored as radians but entered in the INI-file as degrees:
223  */
224 #define MRPT_LOAD_CONFIG_VAR_DEGREES(variableName,configFileObject,sectionNameStr) \
225  { variableName = DEG2RAD( configFileObject.read_float(sectionNameStr,#variableName, RAD2DEG(variableName)) ); }
226 
227 #define MRPT_LOAD_CONFIG_VAR_CAST(variableName,variableType,variableTypeCast,configFileObject,sectionNameStr) \
228  { variableName = static_cast<variableTypeCast>(configFileObject.read_##variableType(sectionNameStr,#variableName,variableName)); }
229 
230 
231 #define MRPT_LOAD_HERE_CONFIG_VAR(variableName,variableType,targetVariable,configFileObject,sectionNameStr) \
232  targetVariable = configFileObject.read_##variableType(sectionNameStr,#variableName,targetVariable,false);
233 
234 #define MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT(variableName,variableType,targetVariable,configFileObject,sectionNameStr) \
235  { try { \
236  targetVariable = configFileObject.read_##variableType(sectionNameStr,#variableName,targetVariable,true); \
237  } catch (std::exception &) \
238  { \
239  THROW_EXCEPTION( format( "Value for '%s' not found in config file", static_cast<const char*>(#variableName ) )); \
240  } }\
241 
242 
243 #define MRPT_LOAD_CONFIG_VAR_NO_DEFAULT(variableName,variableType,configFileObject,sectionNameStr) \
244  { try { \
245  variableName = configFileObject.read_##variableType(sectionNameStr,#variableName,variableName,true); \
246  } catch (std::exception &) \
247  { \
248  THROW_EXCEPTION( format( "Value for '%s' not found in config file", static_cast<const char*>(#variableName ) )); \
249  } }\
250 
251 #define MRPT_LOAD_CONFIG_VAR_CAST_NO_DEFAULT(variableName,variableType,variableTypeCast,configFileObject,sectionNameStr) \
252  { try { \
253  variableName = static_cast<variableTypeCast>(configFileObject.read_##variableType(sectionNameStr,#variableName,variableName,true)); \
254  } catch (std::exception &) \
255  { \
256  THROW_EXCEPTION( format( "Value for '%s' not found in config file", static_cast<const char*>(#variableName ) )); \
257  } }\
258 
259 
260 #define MRPT_LOAD_HERE_CONFIG_VAR_CAST(variableName,variableType,variableTypeCast,targetVariable,configFileObject,sectionNameStr) \
261  targetVariable = static_cast<variableTypeCast>(configFileObject.read_##variableType(sectionNameStr,#variableName,targetVariable));
262 
263 #define MRPT_LOAD_HERE_CONFIG_VAR_CAST_NO_DEFAULT(variableName,variableType,variableTypeCast,targetVariable,configFileObject,sectionNameStr) \
264  { try { \
265  targetVariable = static_cast<variableTypeCast>(configFileObject.read_##variableType(sectionNameStr,#variableName,targetVariable,true)); \
266  } catch (std::exception &) \
267  { \
268  THROW_EXCEPTION( format( "Value for '%s' not found in config file", static_cast<const char*>(#variableName ) )); \
269  } }\
270 
271 
272 #define MRPT_SAVE_CONFIG_VAR(variableName,configFileObject,sectionNameStr) \
273  { configFileObject.write(sectionNameStr,#variableName,variableName); }
274 
275 #define MRPT_SAVE_CONFIG_VAR_DEGREES(variableName,configFileObject,sectionNameStr) \
276  { configFileObject.write(sectionNameStr,#variableName, RAD2DEG(variableName)); }
277 
278 
279  } // End of namespace
280 } // end of namespace
281 #endif
static ENUMTYPE name2value(const std::string &name)
Gives the numerical name for a given enum text name.
Definition: TEnumType.h:35
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
#define THROW_EXCEPTION(msg)
std::vector< std::string > vector_string
A type for passing a vector of strings.
This class allows loading and storing values and vectors of different types from a configuration text...
virtual ~CConfigFileBase()
Virtual destructor...
void read_matrix(const std::string &section, const std::string &name, MATRIX_TYPE &outMatrix, const MATRIX_TYPE &defaultMatrix=MATRIX_TYPE(), bool failIfNotFound=false) const
Reads a configuration parameter as a matrix written in a matlab-like format - for example: "[2 3 4 ; ...
#define MRPT_END
void read_vector(const std::string &section, const std::string &name, const VECTOR_TYPE &defaultValue, VECTOR_TYPE &outValues, bool failIfNotFound=false) const
Reads a configuration parameter of type vector, stored in the file as a string: "[v1 v2 v3 ...
ENUMTYPE read_enum(const std::string &section, const std::string &name, const ENUMTYPE &defaultValue, bool failIfNotFound=false) const
Reads an "enum" value, where the value in the config file can be either a numerical value or the symb...
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void BASE_IMPEXP tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens) MRPT_NO_THROWS
Tokenizes a string according to a set of delimiting characters.
#define THROW_EXCEPTION_CUSTOM_MSG1(msg, param1)



Page generated by Doxygen 1.8.14 for MRPT 1.1.0 SVN: at lun oct 28 00:54:49 CET 2019 Hosted on:
SourceForge.net Logo