ConfDataReader.hpp

Go to the documentation of this file.
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 //  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110, 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 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             // Default constructor initializing the data in the structure
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    }; // End of class 'ConfDataReader'
00503 
00505 
00506 }  // End of namespace gpstk
00507 #endif  // CONFDATAREADER_HPP

Generated on Thu May 23 03:31:05 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1