00001 #pragma ident "$Id: ConfDataReader.hpp 3140 2012-06-18 15:03:02Z susancummins $"
00002
00008 #ifndef CONFDATAREADER_HPP
00009 #define CONFDATAREADER_HPP
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <string>
00037 #include <map>
00038 #include "FFTextStream.hpp"
00039 #include "StringUtils.hpp"
00040 #include "Matrix.hpp"
00041
00042
00043 namespace gpstk
00044 {
00045
00048
00156 class ConfDataReader : public FFTextStream
00157 {
00158 public:
00159
00161 ConfDataReader()
00162 : issueException(true), fallback2Default(false) {};
00163
00164
00171 ConfDataReader(const char* file)
00172 : FFTextStream(file, std::ios::in), issueException(true),
00173 fallback2Default(false)
00174 { loadData(); };
00175
00176
00183 ConfDataReader(const std::string& file)
00184 : FFTextStream(file.c_str(), std::ios::in), issueException(true),
00185 fallback2Default(false)
00186 { loadData(); };
00187
00188
00190 virtual void open(const char* fn);
00191
00192
00194 virtual void open(const std::string& fn)
00195 { open( fn.c_str() ); };
00196
00197
00204 virtual std::string getValue( std::string variable,
00205 std::string section = "DEFAULT",
00206 std::string defaultVal = "")
00207 throw(ConfigurationException);
00208
00209
00216 virtual double getValueAsDouble( std::string variable,
00217 std::string section = "DEFAULT",
00218 double defaultVal = 0.0)
00219 throw(ConfigurationException)
00220 {
00221 return StringUtils::asDouble(
00222 getValue(variable, section, StringUtils::asString(defaultVal)) );
00223 };
00224
00225
00232 virtual int getValueAsInt( std::string variable,
00233 std::string section = "DEFAULT",
00234 int defaultVal = 0 )
00235 throw(ConfigurationException)
00236 {
00237 return StringUtils::asInt(
00238 getValue(variable, section, StringUtils::asString(defaultVal)) );
00239 };
00240
00241
00248 virtual bool getValueAsBoolean( std::string variable,
00249 std::string section = "DEFAULT",
00250 bool defaultVal = false )
00251 throw(ConfigurationException);
00252
00253
00266 virtual std::string fetchListValue( std::string variableList,
00267 std::string section = "DEFAULT",
00268 std::string defaultVal = "" )
00269 throw(ConfigurationException);
00270
00271
00284 virtual double fetchListValueAsDouble( std::string variableList,
00285 std::string section = "DEFAULT",
00286 double defaultVal = 0.0 )
00287 throw(ConfigurationException)
00288 {
00289 return StringUtils::asDouble(
00290 fetchListValue(variableList,section,StringUtils::asString(defaultVal)));
00291 };
00292
00293
00306 virtual int fetchListValueAsInt( std::string variableList,
00307 std::string section = "DEFAULT",
00308 int defaultVal = 0 )
00309 throw(ConfigurationException)
00310 {
00311 return StringUtils::asInt(
00312 fetchListValue(variableList,section,StringUtils::asString(defaultVal)));
00313 };
00314
00315
00330 virtual bool fetchListValueAsBoolean( std::string variableList,
00331 std::string section = "DEFAULT",
00332 bool defaultVal = false)
00333 throw(ConfigurationException);
00334
00335
00345 virtual int getNumItem( std::string variableList,
00346 std::string section = "DEFAULT" )
00347 throw(ConfigurationException)
00348 { return StringUtils::numWords( getValue( variableList, section ) ); };
00349
00350
00357 virtual std::string getVariableDescription( std::string variable,
00358 std::string section = "DEFAULT" )
00359 throw(ConfigurationException);
00360
00361
00368 virtual std::string getValueDescription( std::string variable,
00369 std::string section = "DEFAULT" )
00370 throw(ConfigurationException);
00371
00372
00376 virtual bool getIssueException( void ) const
00377 { return issueException; };
00378
00379
00386 ConfDataReader& setIssueException(bool issueEx)
00387 { issueException = issueEx; return (*this); }
00388
00389
00393 virtual bool getFallback2Default( void ) const
00394 { return fallback2Default; };
00395
00396
00403 ConfDataReader& setFallback2Default(bool fallback)
00404 { fallback2Default = fallback; return (*this); }
00405
00406
00408 virtual ConfDataReader& clear(void)
00409 { confData.clear(); return (*this); };
00410
00411
00413 virtual std::string getEachSection(void);
00414
00415
00418 virtual void resetSection(void)
00419 { itCurrentSection = confData.begin(); return; };
00420
00421
00428 virtual bool ifExist( std::string variable,
00429 std::string section = "DEFAULT" )
00430 throw(ConfigurationException);
00431
00432
00439 virtual std::string operator()( std::string variable,
00440 std::string section = "DEFAULT" )
00441 throw(ConfigurationException)
00442 { return getValue(variable, section); };
00443
00444
00446 virtual ~ConfDataReader() {}
00447
00448
00449 private:
00450
00451
00454 bool issueException;
00455
00456
00459 bool fallback2Default;
00460
00461
00463 struct variableData
00464 {
00465
00466 variableData() : varComment(""), value(""), valueComment("") {};
00467
00468 std::string varComment;
00469 std::string value;
00470 std::string valueComment;
00471 };
00472
00473
00475 typedef std::map<std::string, variableData> variableMap;
00476
00478 typedef std::map<std::string, variableMap> confMap;
00479
00480
00481
00483 confMap confData;
00484
00485
00487 confMap::const_iterator itCurrentSection;
00488
00489
00494 virtual bool checkName(std::string name);
00495
00496
00498 virtual void loadData(void)
00499 throw(ConfigurationException);
00500
00501
00502 };
00503
00505
00506 }
00507 #endif