00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CConfigFileBase_H
00029 #define CConfigFileBase_H
00030
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/utils/TEnumType.h>
00033 #include <mrpt/math/CMatrixTemplate.h>
00034 #include <mrpt/system/string_utils.h>
00035
00036
00037
00038
00039 namespace mrpt
00040 {
00041 namespace utils
00042 {
00043
00044
00045
00046 class BASE_IMPEXP CConfigFileBase
00047 {
00048 protected:
00049
00050
00051 virtual void writeString(const std::string §ion,const std::string &name, const std::string &str) = 0;
00052
00053
00054
00055
00056 virtual std::string readString(
00057 const std::string §ion,
00058 const std::string &name,
00059 const std::string &defaultStr,
00060 bool failIfNotFound = false) const = 0;
00061
00062 public:
00063
00064
00065 virtual ~CConfigFileBase()
00066 {
00067 }
00068
00069
00070
00071 virtual void getAllSections( vector_string §ions ) const = 0 ;
00072
00073
00074
00075 virtual void getAllKeys( const std::string section, vector_string &keys ) const = 0;
00076
00077
00078 bool sectionExists( const std::string §ion_name) const;
00079
00080
00081
00082 void write(const std::string §ion, const std::string &name, double value);
00083
00084
00085
00086 void write(const std::string §ion, const std::string &name, float value);
00087
00088
00089
00090 void write(const std::string §ion, const std::string &name, int value);
00091
00092
00093
00094 void write(const std::string §ion, const std::string &name, unsigned int value);
00095
00096 #if MRPT_WORD_SIZE>32
00097
00098
00099 void write(const std::string §ion, const std::string &name, size_t value);
00100 #endif
00101
00102
00103
00104 void write(const std::string §ion, const std::string &name, const std::string &value);
00105
00106
00107
00108 void write(const std::string §ion, const std::string &name, const std::vector<int> &value);
00109
00110
00111
00112 void write(const std::string §ion, const std::string &name, const std::vector<unsigned int> &value);
00113
00114
00115
00116 void write(const std::string §ion, const std::string &name, const std::vector<float> &value);
00117
00118
00119
00120 void write(const std::string §ion, const std::string &name, const std::vector<double> &value);
00121
00122
00123
00124 void write(const std::string §ion, const std::string &name, const std::vector<bool> &value);
00125
00126
00127
00128
00129 double read_double(const std::string §ion, const std::string &name, double defaultValue, bool failIfNotFound = false) const;
00130
00131
00132
00133
00134 float read_float(const std::string §ion, const std::string &name, float defaultValue, bool failIfNotFound = false) const;
00135
00136
00137
00138
00139 bool read_bool(const std::string §ion, const std::string &name, bool defaultValue, bool failIfNotFound = false) const;
00140
00141
00142
00143
00144 int read_int(const std::string §ion, const std::string &name, int defaultValue, bool failIfNotFound = false) const;
00145
00146
00147
00148
00149 uint64_t read_uint64_t(const std::string §ion, const std::string &name, uint64_t defaultValue, bool failIfNotFound = false ) const;
00150
00151
00152
00153
00154 std::string read_string(const std::string §ion, const std::string &name, const std::string &defaultValue, bool failIfNotFound = false) const;
00155
00156
00157
00158
00159 std::string read_string_first_word(const std::string §ion, const std::string &name, const std::string &defaultValue, bool failIfNotFound = false) const;
00160
00161
00162
00163
00164 template <class VECTOR_TYPE>
00165 void read_vector(
00166 const std::string & section,
00167 const std::string & name,
00168 const VECTOR_TYPE & defaultValue,
00169 VECTOR_TYPE & outValues,
00170 bool failIfNotFound = false) const
00171 {
00172 std::string aux ( readString(section, name, "",failIfNotFound ) );
00173
00174 std::vector<std::string> tokens;
00175 mrpt::system::tokenize( aux,"[], \t",tokens);
00176
00177 if (tokens.size()==0)
00178 {
00179 outValues = defaultValue;
00180 }
00181 else
00182 {
00183
00184 const size_t N = tokens.size();
00185 outValues.resize( N );
00186 for (size_t i=0;i<N;i++)
00187 {
00188 std::stringstream ss(tokens[i]);
00189 ss >> outValues[i];
00190 }
00191 }
00192 }
00193
00194
00195
00196
00197
00198
00199 template <class MATRIX_TYPE>
00200 void read_matrix(
00201 const std::string §ion,
00202 const std::string &name,
00203 MATRIX_TYPE &outMatrix,
00204 const MATRIX_TYPE &defaultMatrix = MATRIX_TYPE(),
00205 bool failIfNotFound = false ) const
00206 {
00207 std::string aux = readString(section, name, "",failIfNotFound );
00208 if (aux.empty())
00209 outMatrix = defaultMatrix;
00210 else
00211 {
00212
00213 if (!outMatrix.fromMatlabStringFormat(aux))
00214 THROW_EXCEPTION_CUSTOM_MSG1("Error parsing matrix: '%s'",aux.c_str())
00215 }
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 template <typename ENUMTYPE>
00237 ENUMTYPE read_enum(const std::string §ion, const std::string &name, const ENUMTYPE &defaultValue, bool failIfNotFound = false) const
00238 {
00239 MRPT_START
00240 const std::string sVal = read_string_first_word(section,name,"",failIfNotFound);
00241 if (sVal.empty()) return defaultValue;
00242
00243 if (::isdigit(sVal[0]))
00244 {
00245 return static_cast<ENUMTYPE>(::atoi(&sVal[0]));
00246 }
00247 else
00248 {
00249 try {
00250 return mrpt::utils::TEnumType<ENUMTYPE>::name2value(sVal);
00251 } catch (std::exception &)
00252 {
00253 THROW_EXCEPTION(format("Invalid value '%s' for enum type while reading key='%s'.",sVal.c_str(),name.c_str()))
00254 }
00255 }
00256 MRPT_END
00257 }
00258
00259
00260 };
00261
00262
00263
00264
00265 #define MRPT_LOAD_CONFIG_VAR(variableName,variableType,configFileObject,sectionNameStr) \
00266 { variableName = configFileObject.read_##variableType(sectionNameStr,#variableName,variableName); }
00267
00268
00269
00270 #define MRPT_LOAD_CONFIG_VAR_DEGREES(variableName,configFileObject,sectionNameStr) \
00271 { variableName = DEG2RAD( configFileObject.read_float(sectionNameStr,#variableName, RAD2DEG(variableName)) ); }
00272
00273 #define MRPT_LOAD_CONFIG_VAR_CAST(variableName,variableType,variableTypeCast,configFileObject,sectionNameStr) \
00274 { variableName = static_cast<variableTypeCast>(configFileObject.read_##variableType(sectionNameStr,#variableName,variableName)); }
00275
00276
00277 #define MRPT_LOAD_HERE_CONFIG_VAR(variableName,variableType,targetVariable,configFileObject,sectionNameStr) \
00278 targetVariable = configFileObject.read_##variableType(sectionNameStr,#variableName,targetVariable,false);
00279
00280 #define MRPT_LOAD_HERE_CONFIG_VAR_NO_DEFAULT(variableName,variableType,targetVariable,configFileObject,sectionNameStr) \
00281 { try { \
00282 targetVariable = configFileObject.read_##variableType(sectionNameStr,#variableName,targetVariable,true); \
00283 } catch (std::exception &) \
00284 { \
00285 THROW_EXCEPTION( format( "Value for '%s' not found in config file", static_cast<const char*>(#variableName ) )); \
00286 } }\
00287
00288
00289 #define MRPT_LOAD_CONFIG_VAR_NO_DEFAULT(variableName,variableType,configFileObject,sectionNameStr) \
00290 { try { \
00291 variableName = configFileObject.read_##variableType(sectionNameStr,#variableName,variableName,true); \
00292 } catch (std::exception &) \
00293 { \
00294 THROW_EXCEPTION( format( "Value for '%s' not found in config file", static_cast<const char*>(#variableName ) )); \
00295 } }\
00296
00297 #define MRPT_LOAD_CONFIG_VAR_CAST_NO_DEFAULT(variableName,variableType,variableTypeCast,configFileObject,sectionNameStr) \
00298 { try { \
00299 variableName = static_cast<variableTypeCast>(configFileObject.read_##variableType(sectionNameStr,#variableName,variableName,true)); \
00300 } catch (std::exception &) \
00301 { \
00302 THROW_EXCEPTION( format( "Value for '%s' not found in config file", static_cast<const char*>(#variableName ) )); \
00303 } }\
00304
00305
00306 #define MRPT_LOAD_HERE_CONFIG_VAR_CAST(variableName,variableType,variableTypeCast,targetVariable,configFileObject,sectionNameStr) \
00307 targetVariable = static_cast<variableTypeCast>(configFileObject.read_##variableType(sectionNameStr,#variableName,targetVariable));
00308
00309 #define MRPT_LOAD_HERE_CONFIG_VAR_CAST_NO_DEFAULT(variableName,variableType,variableTypeCast,targetVariable,configFileObject,sectionNameStr) \
00310 { try { \
00311 targetVariable = static_cast<variableTypeCast>(configFileObject.read_##variableType(sectionNameStr,#variableName,targetVariable,true)); \
00312 } catch (std::exception &) \
00313 { \
00314 THROW_EXCEPTION( format( "Value for '%s' not found in config file", static_cast<const char*>(#variableName ) )); \
00315 } }\
00316
00317
00318 #define MRPT_SAVE_CONFIG_VAR(variableName,configFileObject,sectionNameStr) \
00319 { configFileObject.write(sectionNameStr,#variableName,variableName); }
00320
00321 #define MRPT_SAVE_CONFIG_VAR_DEGREES(variableName,configFileObject,sectionNameStr) \
00322 { configFileObject.write(sectionNameStr,#variableName, RAD2DEG(variableName)); }
00323
00324
00325 }
00326 }
00327 #endif