ConfDataItem.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: ConfDataItem.hpp 2956 2011-10-30 09:48:24Z yanweignss $"
00002 
00008 #ifndef GPSTK_CONFDATAITEM_HPP
00009 #define GPSTK_CONFDATAITEM_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 //  Wei Yan - Chinese Academy of Sciences . 2011
00030 //
00031 //============================================================================
00032 
00033 #include <string>
00034 #include <sstream>
00035 #include <vector>
00036 #include "StringUtils.hpp"
00037 #include "Triple.hpp"
00038 #include "Vector.hpp"
00039 #include "DayTime.hpp"
00040 
00041 namespace gpstk
00042 {
00043 
00047    class ConfDataItemAbstract   
00048    {
00049    public:
00050       
00051       ConfDataItemAbstract(){init();}
00052 
00053       ConfDataItemAbstract(const std::string& var, 
00054                            const std::string& val, 
00055                            const std::string& varComment = "",
00056                            const std::string& valComment = "")
00057       {
00058          this->var = var; this->varcomment = varComment;
00059          this->val = val; this->valcomment = valComment; 
00060       }
00061 
00062       ConfDataItemAbstract(const ConfDataItemAbstract& right)
00063       {
00064          this->var = right.var; this->varcomment = right.varcomment;
00065          this->val = right.val; this->valcomment = right.valcomment;
00066       }
00067 
00068       ConfDataItemAbstract& operator=(const ConfDataItemAbstract& right)
00069       {
00070          this->var = right.var; this->varcomment = right.varcomment;
00071          this->val = right.val; this->valcomment = right.valcomment;
00072 
00073          return (*this);
00074       }
00075       
00076       virtual ~ConfDataItemAbstract(){}
00077       
00078       std::string get_var()
00079       { return var;}
00080 
00081       ConfDataItemAbstract& set_var(std::string s)
00082       { var = s; return (*this); }
00083 
00084       std::string get_var_comment()
00085       { return varcomment;}
00086 
00087       ConfDataItemAbstract& set_var_comment(std::string s)
00088       { varcomment = s; return (*this); }
00089 
00090       virtual std::string get_val()
00091       { return val;}
00092       
00093       virtual ConfDataItemAbstract& set_val(std::string s)
00094       { val = s; return (*this); }
00095 
00096       std::string get_val_comment()
00097       { return valcomment;}
00098 
00099       ConfDataItemAbstract& set_val_comment(std::string s)
00100       { valcomment = s; return (*this); }
00101    
00102    protected:
00103       void init()
00104       { var = varcomment = ""; val = valcomment = ""; }
00105 
00106    protected:
00107       std::string  var, varcomment;
00108       std::string  val, valcomment;
00109 
00110    };   // End of class 'ConfDataItemAbstract'
00111    
00112    template<class DataType = std::string>
00113    class ConfDataItem : public ConfDataItemAbstract
00114    {
00115    public:
00116       ConfDataItem(DataType& dat,
00117                    const std::string& var, 
00118                    const std::string& val, 
00119                    const std::string& varComment = "",
00120                    const std::string& valComment = "") 
00121          : ConfDataItemAbstract(var,val,varComment,valComment),
00122            data(dat)
00123       {
00124       }
00125 
00126       virtual ~ConfDataItem(){}
00127       
00128       std::string get_val()
00129       { return format(data);}
00130 
00131       ConfDataItemAbstract& set_val(std::string s)
00132       { parse(s,data); return (*this); }
00133 
00134       // implement the format functions for every DataType
00135       std::string format(const std::string& d)
00136       { return d;}
00137       
00138       std::string format(const int& d)
00139       { return StringUtils::asString(d); }
00140 
00141       std::string format(const double& d,int p=4)
00142       { return StringUtils::asString(d,p);}
00143 
00144       std::string format(const bool& d)
00145       { return d ? "TRUE" : "FALSE"; }
00146 
00147       std::string format(const DayTime& d)
00148       {
00149          return StringUtils::asString(d.year()) + " "
00150               + StringUtils::asString(d.month()) + " "
00151               + StringUtils::asString(d.day()) + " "
00152               + StringUtils::asString(d.hour()) + " "
00153               + StringUtils::asString(d.minute()) + " "
00154               + StringUtils::asString(d.second(),3);
00155       }
00156 
00157       std::string format(const Triple& d)
00158       { 
00159          return StringUtils::asString(d[0],4)+" "
00160                +StringUtils::asString(d[1],4)+" "
00161                +StringUtils::asString(d[2],4);
00162       }
00163 
00164       std::string format(const std::vector<int>& d)
00165       {
00166          std::string s;
00167 
00168          std::vector<int>::const_iterator it;
00169          for(it = d.begin(); it!=d.end(); ++it)
00170          {
00171             s += StringUtils::asString(*it) + " ";
00172          }
00173 
00174          return StringUtils::strip(s);
00175       }
00176 
00177       std::string format(const std::vector<double>& d)
00178       {
00179          std::string s;
00180 
00181          std::vector<double>::const_iterator it;
00182          for(it = d.begin(); it!=d.end(); ++it)
00183          {
00184             s += StringUtils::asString(*it, 4) + " ";
00185          }
00186 
00187          return StringUtils::strip(s);
00188       }
00189 
00190       std::string format(const std::vector<std::string>& d)
00191       {
00192          std::string s;
00193 
00194          std::vector<std::string>::const_iterator it;
00195          for(it = d.begin(); it!=d.end(); ++it)
00196          {
00197             string ss(*it);
00198             s += StringUtils::strip(ss) + " ";
00199          }
00200 
00201          return StringUtils::strip(s);
00202       }
00203 
00204       // implement the format functions for every DataType
00205       void parse(const std::string& in,std::string& out)
00206       { out = in; }
00207 
00208       void parse(const std::string& in,int& out)
00209       {
00210          if(!StringUtils::isDigitString(in))
00211          {
00212             Exception e("Trying to parsing int from a non-digital string '"
00213                          + in + "'." );
00214             GPSTK_THROW(e);
00215          }
00216 
00217          out = StringUtils::asInt(in);
00218       }
00219 
00220       void parse(const std::string& in,double& out)
00221       { out = StringUtils::asDouble(in); }
00222 
00223       void parse(const std::string& in,bool& out)
00224       {
00225          if(StringUtils::upperCase(in)=="TRUE")
00226          { 
00227             out = true; 
00228          }
00229          else if(StringUtils::upperCase(in)=="FALSE")
00230          {
00231             out = false;
00232          }
00233          else  // it's neither TRUE or FALSE
00234          {
00235             GPSTK_THROW(
00236                Exception("Failed to parse a boolean value from "+in
00237                          +", and it should be 'TRUE' or 'FALSE'.") );
00238 
00239             //out = false;
00240          }
00241       }
00242 
00243       void parse(const std::string& in,DayTime& out)
00244       {
00245          int year,month,day,hour,minute; double second;
00246          istringstream s(in);
00247          s >> year >> month >> day >> hour >> minute >> second;    
00248          out.setYMDHMS(year,month,day,hour,minute,second);
00249       }
00250 
00251       void parse(const std::string& in,Triple& out)
00252       {
00253          istringstream s(in);
00254          s >> out[0] >> out[1] >> out[2];
00255       }
00256 
00257       void parse(const std::string& in,std::vector<int>& out)
00258       {
00259          out.clear();
00260 
00261          std::string ss(in);
00262          std::string firstWord = StringUtils::stripFirstWord(ss);
00263          while(StringUtils::strip(firstWord)!="")
00264          {
00265             out.push_back(StringUtils::asInt(firstWord));
00266 
00267             ss = StringUtils::strip(ss);
00268             firstWord = StringUtils::stripFirstWord(ss);
00269          }
00270       }
00271 
00272       void parse(const std::string& in,std::vector<double>& out)
00273       {
00274          out.clear();
00275 
00276          std::string ss(in);
00277          std::string firstWord = StringUtils::stripFirstWord(ss);
00278          while(StringUtils::strip(firstWord)!="")
00279          {
00280             out.push_back(StringUtils::asDouble(firstWord));
00281 
00282             ss = StringUtils::strip(ss);
00283             firstWord = StringUtils::stripFirstWord(ss);
00284          }
00285       }
00286 
00287       void parse(const std::string& in,std::vector<std::string>& out)
00288       {
00289          out.clear();
00290          
00291          std::string ss(in);
00292          std::string firstWord = StringUtils::stripFirstWord(ss);
00293          while(StringUtils::strip(firstWord)!="")
00294          {
00295             out.push_back(firstWord);
00296             
00297             ss = StringUtils::strip(ss);
00298             firstWord = StringUtils::stripFirstWord(ss);
00299          }
00300       }
00301 
00302    protected:
00303       ConfDataItem(){init();}
00304 
00305       DataType& data;
00306       DataType  data0;
00307    };
00308 
00309 }   // End of namespace gpstk
00310 
00311 /*
00312 bool operator==(const ConfDataItemAbstract& right) const
00313 {
00314 if( (var==right.var) && (val==right.val) )
00315 {
00316 return true;
00317 }
00318 else
00319 {
00320 return false;
00321 }
00322 }
00323 
00324 bool operator!=(const ConfDataItemAbstract& right) const
00325 {
00326 return !(operator==(right)); 
00327 }
00328 
00329 bool operator<(const ConfDataItemAbstract& right) const
00330 {
00331 if(var==right.var)
00332 {
00333 return val < right.val;
00334 }
00335 else
00336 {
00337 return var < right.val;
00338 }
00339 }
00340 
00341 bool operator>(const ConfDataItemAbstract& right) const
00342 {
00343 return (!operator<(right) && !operator==(right)); 
00344 }
00345 
00346 bool operator<=(const ConfDataItemAbstract& right) const
00347 {
00348 return (operator<(right) || operator==(right)); 
00349 }
00350 
00351 bool operator>=(const ConfDataItemAbstract& right) const
00352 {
00353 return !(operator<(right)); 
00354 }
00355 
00356 */
00357 
00358 #endif  //GPSTK_CONFDATAITEM_HPP
00359 

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