ConfData.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: ConfData.hpp 2956 2011-10-30 09:48:24Z yanweignss $"
00002 
00008 #ifndef GPSTK_CONFDATA_HPP
00009 #define GPSTK_CONFDATA_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 <map>
00035 
00036 #include "DayTime.hpp"
00037 #include "ConfDataSection.hpp"
00038 #include "ConfDataReader.hpp"
00039 #include "ConfDataWriter.hpp"
00040 
00041 
00042 namespace gpstk
00043 {
00044    template <class HEADER_CLASS, class BODY_CLASS>
00045    struct gnssConfData;
00046 
00047    template <class C>
00048    class ConfCallBack
00049    {
00050    public:
00051       typedef void (C::*Callback)(const std::string&);
00052 
00053       ConfCallBack(C& object, Callback method): _pObject(&object), _method(method){}
00054 
00055       ConfCallBack(const ConfCallBack& ra): _pObject(ra._pObject), _method(ra._method){}
00056 
00057       ~ConfCallBack(){}
00058 
00059       ConfCallBack& operator = (const ConfCallBack& ra)
00060       {
00061          _pObject = ra._pObject;
00062          _method  = ra._method;
00063          return *this;
00064       }
00065 
00066       void addSection(const std::string& name){(_pObject->*_method)(name); }
00067 
00068    private:
00069       ConfCallBack();
00070 
00071       C*       _pObject;
00072       Callback _method;
00073    };
00074 
00075    template<class C>
00076    class ConfData   
00077    {
00078    public:
00079       typedef std::map<std::string,ConfDataSection> SectionMap;
00080       typedef SectionMap::iterator Iterator;
00081 
00082    public:
00083       ConfData(const ConfCallBack<C>& callback, const std::string& desc="")
00084          : callBack(callback),comment(desc),variableWidth(20),valuePrecison(6) 
00085       { init();}
00086 
00087       virtual ~ConfData(){}
00088 
00089       ConfData& set_comment(const std::string& desc="")
00090       { comment = desc; return (*this); }
00091 
00092       ConfData& clear(){return init();}
00093 
00094       bool has_section(const std::string& name = Default)
00095       { return (dataMap.find(name)!=dataMap.end()) ? true : false; }
00096 
00097       ConfDataSection* get_section(const std::string& name = Default)
00098       {
00099          SectionMap::iterator it = dataMap.find(name);
00100          return (it!=dataMap.end()) ? &(it->second) : 0;
00101       }
00102 
00103       ConfData& add_section(const std::string& name = Default)
00104       { return add_section(ConfDataSection(),name); }
00105 
00106       ConfData& add_section(const ConfDataItemSet& items,
00107                             const std::string& name = Default)
00108       {
00109          if(dataMap.find(name)==dataMap.end()) dataMap[name] = items;
00110 
00111          return (*this);
00112       }
00113 
00114       ConfData& append_section(ConfDataItemAbstract* item, 
00115                                const std::string& name = Default)
00116       {
00117          if(has_section(name)) get_section(name)->insert(item);    // add to specific section
00118          else                  get_section()->insert(item);        // add to default section
00119          
00120          return (*this);
00121       }
00122 
00123       ConfData& append_section(const ConfDataItemSet& items, 
00124                                const std::string& name = Default)
00125       {
00126          if(has_section(name)) get_section(name)->insert(items);  // add to specific section
00127          else                  get_section()->insert(items);      // add to default section
00128 
00129          return (*this);
00130       }
00131 
00132       Iterator begin() {return dataMap.begin();}
00133 
00134       Iterator end() {return dataMap.end();}
00135 
00136       inline ConfData& save(const std::string& fileName);
00137 
00138       inline ConfData& load(const std::string& fileName);
00139       
00140       void setVariableWidth(const int width = 0)
00141       { variableWidth = width;}
00142 
00143       void setValuePrecision(const int precision = 6)
00144       { valuePrecison = precision;}
00145 
00146    protected:
00147 
00148       ConfData& init()
00149       { dataMap.clear(); return add_section();}
00150 
00151    protected:
00152       SectionMap dataMap;
00153 
00154       std::string comment;
00155 
00156       ConfCallBack<C> callBack;
00157 
00158       int variableWidth;
00159       int valuePrecison;
00160 
00161    public:
00162       static const std::string Default;
00163 
00164    };   // End of class 'ConfData'
00165 
00167 
00168    template<class C>
00169    const std::string ConfData<C>::Default = "DEFAULT";
00170 
00171    template<class C>
00172    inline ConfData<C>& ConfData<C>::save(const std::string& fileName)
00173    {
00174       ConfDataWriter confWriter(fileName);
00175       confWriter.setVariableWidth(variableWidth);
00176       confWriter.setValuePrecision(variableWidth);
00177 
00178       DayTime now;
00179       confWriter.writeCommentLine(comment + " " + now.asString());
00180       confWriter.writeSeparatorLine("=");
00181       confWriter.writeBlankLine();
00182 
00183       // write default
00184       Iterator it = dataMap.find(Default);
00185       if(it!=dataMap.end())
00186       {
00187          for(ConfDataSection::Iterator it2 = it->second.begin();
00188              it2!=it->second.end();
00189              ++it2)
00190          {
00191             ConfDataItemAbstract* item = (*it2);
00192 
00193             confWriter.writeVariable(item->get_var(),
00194                item->get_val(),
00195                item->get_var_comment(),
00196                item->get_val_comment() );
00197          }
00198 
00199          confWriter.writeBlankLine();
00200       }
00201 
00202       // write other section
00203       for(Iterator it = begin(); it!=end(); it++)
00204       {
00205          ConfDataItemSet& section = it->second;
00206 
00207          if(it->first==Default) continue;
00208 
00209          confWriter.writeSection(it->first,section.comment);
00210 
00211          for(ConfDataSection::Iterator it2 = section.begin();
00212              it2!=section.end();
00213              ++it2)
00214          {
00215             ConfDataItemAbstract* item = (*it2);
00216 
00217             confWriter.writeVariable(item->get_var(),
00218                item->get_val(),
00219                item->get_var_comment(),
00220                item->get_val_comment() );
00221          }
00222 
00223          confWriter.writeBlankLine();
00224       }
00225 
00226       confWriter.writeBlankLine();
00227       confWriter.writeEnd();
00228 
00229       confWriter.close();
00230 
00231       return (*this);
00232 
00233    }  // End of method 'ConfData<C>& ConfData<C>::save()'
00234 
00235    template<class C>
00236    inline ConfData<C>& ConfData<C>::load(const std::string& fileName)
00237    {
00238       try
00239       {
00240          ConfDataReader confReader;
00241          confReader.open(fileName);
00242          confReader.setFallback2Default(true);
00243 
00244          // read default section
00245          ConfDataSection* sec = get_section(Default);
00246 
00247          for(ConfDataSection::Iterator it = sec->begin();
00248             it!=sec->end();
00249             ++it)
00250          {
00251             ConfDataItemAbstract* item = (*it);
00252 
00253             string var = item->get_var();
00254 
00255             item->set_val(confReader.getValue(var));
00256             item->set_val_comment(confReader.getValueDescription(var));
00257             item->set_var_comment(confReader.getVariableDescription(var));
00258          }
00259 
00260          // read other section
00261          std::string name;
00262          while ( (name = confReader.getEachSection()) != "" )
00263          {
00264             if(name==Default) continue;      // skip default section
00265 
00266             callBack.addSection(name);
00267 
00268             ConfDataSection* sec = get_section(name);
00269 
00270             for(ConfDataSection::Iterator it = sec->begin();
00271                it!=sec->end();
00272                ++it)
00273             {
00274                ConfDataItemAbstract* item = (*it);
00275 
00276                string var = item->get_var();
00277 
00278                item->set_val(confReader.getValue(var,name));
00279                item->set_val_comment(confReader.getValueDescription(var,name));
00280                item->set_var_comment(confReader.getVariableDescription(var,name));
00281             }
00282          }
00283 
00284          confReader.close();
00285       }
00286       catch(Exception& e)
00287       {
00288          Exception E("Error loading the file '"
00289             + fileName + "', " + string(e.what())); 
00290          GPSTK_THROW(E);
00291       }
00292       catch(exception& e)
00293       {
00294          Exception E("Error loading the file '"
00295             + fileName + "', " + string(e.what())); 
00296          GPSTK_THROW(E);
00297       }
00298       catch(...)
00299       {
00300          Exception E("Error loading the file '"
00301             + fileName + "', " + string("Unknown error.")); 
00302 
00303          GPSTK_THROW(E);
00304       }
00305 
00306       return (*this);
00307 
00308    }  // End of method 'ConfData<C>& ConfData<C>::load()'
00309 
00310 }   // End of namespace gpstk
00311 
00312 
00313 #endif  //GPSTK_CONFDATA_HPP
00314 

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