ConfDataReader.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: ConfDataReader.hpp 2956 2011-10-30 09:48:24Z yanweignss $"
00002 
00008 #ifndef CONFDATAREADER_HPP
00009 #define CONFDATAREADER_HPP
00010 
00011 //============================================================================
00012 //
00013 //  This file is part of GPSTk, the GPS Toolkit.
00014 //
00015 //  The GPSTk is free software; you can redistribute it and/or modify
00016 //  it under the terms of the GNU Lesser General Public License as published
00017 //  by the Free Software Foundation; either version 2.1 of the License, or
00018 //  any later version.
00019 //
00020 //  The GPSTk is distributed in the hope that it will be useful,
00021 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 //  GNU Lesser General Public License for more details.
00024 //
00025 //  You should have received a copy of the GNU Lesser General Public
00026 //  License along with GPSTk; if not, write to the Free Software Foundation,
00027 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 //
00029 //  Dagoberto Salazar - gAGE ( http://www.gage.es ). 2008
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 using namespace std;
00044 
00045 namespace gpstk
00046 {
00047 
00050 
00158    class ConfDataReader : public FFTextStream
00159    {
00160    public:
00161 
00163       ConfDataReader()
00164          : issueException(true), fallback2Default(false) {};
00165 
00166 
00173       ConfDataReader(const char* file)
00174          : FFTextStream(file, std::ios::in), issueException(true),
00175            fallback2Default(false)
00176       { loadData(); };
00177 
00178 
00185       ConfDataReader(const string& file)
00186          : FFTextStream(file.c_str(), std::ios::in), issueException(true),
00187            fallback2Default(false)
00188       { loadData(); };
00189 
00190 
00192       virtual void open(const char* fn);
00193 
00194 
00196       virtual void open(const string& fn)
00197       { open( fn.c_str() ); };
00198 
00199 
00206       virtual string getValue( string variable,
00207                                string section = "DEFAULT",
00208                                string defaultVal = "")
00209          throw(ConfigurationException);
00210 
00211 
00218       virtual double getValueAsDouble( string variable,
00219                                        string section = "DEFAULT",
00220                                        double defaultVal = 0.0)
00221          throw(ConfigurationException)
00222       { 
00223          return StringUtils::asDouble( 
00224                getValue(variable, section, StringUtils::asString(defaultVal)) ); 
00225       };
00226 
00227 
00234       virtual int getValueAsInt( string variable,
00235                                  string section = "DEFAULT",
00236                                  int    defaultVal = 0 )
00237          throw(ConfigurationException)
00238       { 
00239          return StringUtils::asInt( 
00240                   getValue(variable, section, StringUtils::asString(defaultVal)) ); 
00241       };
00242 
00243 
00250       virtual bool getValueAsBoolean( string variable,
00251                                       string section = "DEFAULT", 
00252                                       bool   defaultVal = false )
00253          throw(ConfigurationException);
00254 
00255 
00268       virtual string fetchListValue( string variableList,
00269                                      string section = "DEFAULT",
00270                                      string defaultVal = "" )
00271          throw(ConfigurationException);
00272 
00273 
00286       virtual double fetchListValueAsDouble( string variableList,
00287                                              string section = "DEFAULT",
00288                                              double defaultVal = 0.0 )
00289          throw(ConfigurationException)
00290       { 
00291          return StringUtils::asDouble( 
00292          fetchListValue(variableList,section,StringUtils::asString(defaultVal))); 
00293       };
00294 
00295 
00308       virtual int fetchListValueAsInt( string variableList,
00309                                        string section = "DEFAULT",
00310                                        int    defaultVal = 0 )
00311          throw(ConfigurationException)
00312       { 
00313          return StringUtils::asInt( 
00314          fetchListValue(variableList,section,StringUtils::asString(defaultVal))); 
00315       };
00316 
00317 
00332       virtual bool fetchListValueAsBoolean( string variableList,
00333                                             string section = "DEFAULT",
00334                                             bool   defaultVal = false)
00335          throw(ConfigurationException);
00336 
00337 
00347       virtual int getNumItem( string variableList,
00348                               string section = "DEFAULT" )
00349          throw(ConfigurationException)
00350       { return StringUtils::numWords( getValue( variableList, section ) ); };
00351 
00352 
00359       virtual string getVariableDescription( string variable,
00360                                              string section = "DEFAULT" )
00361          throw(ConfigurationException);
00362 
00363 
00370       virtual string getValueDescription( string variable,
00371                                           string section = "DEFAULT" )
00372          throw(ConfigurationException);
00373 
00374 
00378       virtual bool getIssueException( void ) const
00379       { return issueException; };
00380 
00381 
00388       ConfDataReader& setIssueException(bool issueEx)
00389       { issueException = issueEx; return (*this); }
00390 
00391 
00395       virtual bool getFallback2Default( void ) const
00396       { return fallback2Default; };
00397 
00398 
00405       ConfDataReader& setFallback2Default(bool fallback)
00406       { fallback2Default = fallback; return (*this); }
00407 
00408 
00410       virtual ConfDataReader& clear(void)
00411       { confData.clear(); return (*this); };
00412 
00413 
00415       virtual string getEachSection(void);
00416 
00417 
00420       virtual void resetSection(void)
00421       { itCurrentSection = confData.begin(); return; };
00422 
00423 
00430       virtual bool ifExist( string variable,
00431                             string section = "DEFAULT" )
00432          throw(ConfigurationException);
00433 
00434 
00441       virtual string operator()( string variable,
00442                                  string section = "DEFAULT" )
00443          throw(ConfigurationException)
00444       { return getValue(variable, section); };
00445 
00446 
00448       virtual ~ConfDataReader() {}
00449 
00450 
00451    private:
00452 
00453 
00456       bool issueException;
00457 
00458 
00461       bool fallback2Default;
00462 
00463 
00465       struct variableData
00466       {
00467             // Default constructor initializing the data in the structure
00468          variableData() : varComment(""), value(""), valueComment("") {};
00469 
00470          string varComment;      
00471          string value;           
00472          string valueComment;    
00473       };
00474 
00475 
00477       typedef std::map<string, variableData> variableMap;
00478 
00480       typedef std::map<string, variableMap> confMap;
00481 
00482 
00483 
00485       confMap confData;
00486 
00487 
00489       confMap::const_iterator itCurrentSection;
00490 
00491 
00496       virtual bool checkName(string name);
00497 
00498 
00500       virtual void loadData(void)
00501          throw(ConfigurationException);
00502 
00503 
00504    }; // End of class 'ConfDataReader'
00505 
00507 
00508 }  // End of namespace gpstk
00509 #endif  // CONFDATAREADER_HPP

Generated on Tue May 22 03:30:57 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1