FileFilterFrameWithHeader.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: FileFilterFrameWithHeader.hpp 274 2006-10-27 14:24:35Z rickmach $"
00002 
00003 
00004 
00010 #ifndef GPSTK_FILEFILTERFRAMEWITHHEADER_HPP
00011 #define GPSTK_FILEFILTERFRAMEWITHHEADER_HPP
00012 
00013 //============================================================================
00014 //
00015 //  This file is part of GPSTk, the GPS Toolkit.
00016 //
00017 //  The GPSTk is free software; you can redistribute it and/or modify
00018 //  it under the terms of the GNU Lesser General Public License as published
00019 //  by the Free Software Foundation; either version 2.1 of the License, or
00020 //  any later version.
00021 //
00022 //  The GPSTk is distributed in the hope that it will be useful,
00023 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 //  GNU Lesser General Public License for more details.
00026 //
00027 //  You should have received a copy of the GNU Lesser General Public
00028 //  License along with GPSTk; if not, write to the Free Software Foundation,
00029 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00030 //  
00031 //  Copyright 2004, The University of Texas at Austin
00032 //
00033 //============================================================================
00034 
00035 //============================================================================
00036 //
00037 //This software developed by Applied Research Laboratories at the University of
00038 //Texas at Austin, under contract to an agency or agencies within the U.S. 
00039 //Department of Defense. The U.S. Government retains all rights to use,
00040 //duplicate, distribute, disclose, or release this software. 
00041 //
00042 //Pursuant to DoD Directive 523024 
00043 //
00044 // DISTRIBUTION STATEMENT A: This software has been approved for public 
00045 //                           release, distribution is unlimited.
00046 //
00047 //=============================================================================
00048 
00049 
00050 
00051 
00052 
00053 
00054 #include "FileFilterFrame.hpp"
00055 
00056 namespace gpstk
00057 {
00060 
00077    template <class FileStream, class FileData, class FileHeader>
00078    class FileFilterFrameWithHeader :
00079       public FileFilterFrame<FileStream, FileData>
00080    {
00081    public:
00083       FileFilterFrameWithHeader(const gpstk::DayTime& start = 
00084                                    gpstk::DayTime::BEGINNING_OF_TIME,
00085                                  const gpstk::DayTime& end = 
00086                                    gpstk::DayTime::END_OF_TIME)
00087          throw(gpstk::Exception) :
00088             FileFilterFrame<FileStream, FileData>(start, end)
00089          {}
00090 
00092       FileFilterFrameWithHeader(const std::vector<std::string>& fileList,
00093                                  const gpstk::DayTime& start = 
00094                                     gpstk::DayTime::BEGINNING_OF_TIME,
00095                                  const gpstk::DayTime& end = 
00096                                     gpstk::DayTime::END_OF_TIME)
00097          throw(gpstk::Exception) :
00098             FileFilterFrame<FileStream, FileData>(fileList, start, end)
00099          {
00100             std::vector<std::string>::const_iterator itr = fileList.begin();
00101             while (itr != fileList.end())
00102             {
00103                this->fs.newSpec(*itr);
00104                init();
00105                itr++;
00106             }
00107          }
00108 
00111       FileFilterFrameWithHeader(const std::string& filename, 
00112                                  const gpstk::DayTime& start = 
00113                                     gpstk::DayTime::BEGINNING_OF_TIME,
00114                                  const gpstk::DayTime& end = 
00115                                     gpstk::DayTime::END_OF_TIME)
00116          throw(gpstk::Exception) :
00117             FileFilterFrame<FileStream, FileData>(filename, start, end)
00118          {init();}
00119 
00123       FileFilterFrameWithHeader(const FileSpec& spec, 
00124                                  const gpstk::DayTime& start = 
00125                                     gpstk::DayTime::BEGINNING_OF_TIME,
00126                                  const gpstk::DayTime& end = 
00127                                     gpstk::DayTime::END_OF_TIME,
00128                                  const std::vector<FileHunter::FilterPair>& filter = 
00129                                     std::vector<FileHunter::FilterPair>())
00130          throw(gpstk::Exception) :
00131             FileFilterFrame<FileStream, FileData>(spec, start, end, filter)
00132          {init(filter);}
00133 
00137       FileFilterFrameWithHeader& 
00138       newSource(const FileSpec& filespec, 
00139                 const gpstk::DayTime& start = 
00140                    gpstk::DayTime::BEGINNING_OF_TIME,
00141                 const gpstk::DayTime& end = 
00142                    gpstk::DayTime::END_OF_TIME,
00143                 const std::vector<FileHunter::FilterPair>& filter = 
00144                    std::vector<FileHunter::FilterPair>())
00145          throw(gpstk::Exception)
00146          {
00147             FileFilterFrame<FileStream, FileData>::newSource(filespec, start,
00148                                                              end, filter);
00149             init(filter);
00150             return *this;
00151          }
00152 
00154       FileFilterFrameWithHeader& 
00155       newSource(const std::string& filename, 
00156                 const gpstk::DayTime& start = 
00157                    gpstk::DayTime::BEGINNING_OF_TIME,
00158                 const gpstk::DayTime& end = 
00159                    gpstk::DayTime::END_OF_TIME)
00160          throw(gpstk::Exception)
00161          {
00162             FileFilterFrame<FileStream, FileData>::newSource(filename, start,
00163                                                              end);
00164             init();
00165             return *this;
00166          }
00167 
00169       FileFilterFrameWithHeader& 
00170       newSource(const std::vector<std::string>& fileList, 
00171                 const gpstk::DayTime& start = 
00172                    gpstk::DayTime::BEGINNING_OF_TIME,
00173                 const gpstk::DayTime& end = 
00174                    gpstk::DayTime::END_OF_TIME)
00175          throw(gpstk::Exception)
00176          {
00177             FileFilterFrame<FileStream, FileData>::newSource(fileList, start,
00178                                                              end);
00179             typename std::vector<std::string>::const_iterator itr;
00180             for (itr = fileList.begin(); itr != fileList.end(); itr++)
00181             {
00182                this->fs.newSpec(*itr);
00183                init();
00184             }
00185             return *this;
00186          }
00187 
00188       virtual ~FileFilterFrameWithHeader() {}
00189 
00197       bool writeFile(const std::string& outputFile,
00198                      const FileHeader& fh) const
00199          throw(gpstk::Exception);
00200 
00202       template <class Operation>
00203       FileFilterFrameWithHeader& touchHeader(Operation& op)
00204       {
00205          typename std::list<FileHeader>::iterator itr = headerList.begin();
00206 
00207          while (itr != headerList.end())
00208          {
00209             op(*itr);
00210             itr++;
00211          }
00212 
00213          return *this;
00214       }
00215 
00217       std::list<FileHeader>& getHeaderData(void) {return headerList;}
00218 
00220       std::list<FileHeader> getHeaderData(void) const {return headerList;}
00221 
00223       typename std::list<FileHeader>::size_type getHeaderCount(void) const 
00224          { return headerList.size(); }
00225 
00226       typename std::list<FileHeader>::const_iterator beginHeader() const
00227          throw(gpstk::InvalidRequest);
00228 
00229       typename std::list<FileHeader>::const_iterator endHeader() const
00230          throw(gpstk::InvalidRequest);
00231 
00232       typename std::list<FileHeader>::iterator beginHeader() 
00233          throw(gpstk::InvalidRequest);
00234 
00235       typename std::list<FileHeader>::iterator endHeader() 
00236          throw(gpstk::InvalidRequest);
00237 
00238       bool emptyHeader() const
00239          { return headerList.empty(); }
00240 
00241       typename std::list<FileHeader>::size_type sizeHeader()
00242          { return headerList.size(); }
00243 
00244       FileHeader& frontHeader()
00245          throw(gpstk::InvalidRequest);
00246 
00247       const FileHeader& frontHeader() const
00248          throw(gpstk::InvalidRequest);
00249 
00250       FileHeader& backHeader()
00251          throw(gpstk::InvalidRequest);
00252 
00253       const FileHeader& backHeader() const
00254          throw(gpstk::InvalidRequest);
00255 
00256    protected:
00258       void init(const std::vector<FileHunter::FilterPair>& filter= 
00259                 std::vector<FileHunter::FilterPair>()) 
00260          throw(gpstk::Exception);
00261 
00264       inline void chl(const std::string& req) throw(gpstk::InvalidRequest)
00265       {
00266          gpstk::InvalidRequest exc("Header list is empty attempting to"
00267                                     " satisfy "+req+" request.");
00268          if (headerList.empty())
00269          {
00270             GPSTK_THROW(exc);
00271          }
00272       }
00273 
00274    protected:   
00275       std::list<FileHeader> headerList;
00276    };
00277 
00279 
00280    template <class FileStream, class FileData, class FileHeader>
00281    bool FileFilterFrameWithHeader<FileStream,FileData,FileHeader>::
00282    writeFile(const std::string& outputFile,
00283              const FileHeader& fh) const
00284       throw(gpstk::Exception)
00285    {
00286          // make the directory (if needed)
00287       std::string::size_type pos = outputFile.rfind('/');
00288       if (pos != std::string::npos)
00289          gpstk::FileUtils::makeDir(outputFile.substr(0,pos).c_str(), 0755);
00290       
00291       FileStream stream(outputFile.c_str(), std::ios::out|std::ios::trunc);
00292       stream.exceptions(std::ios::failbit);
00293 
00294       stream << fh;
00295 
00296       typename std::list<FileData>::const_iterator index;
00297       for(index = this->dataVec.begin(); index != this->dataVec.end(); index++)
00298          stream << (*index);
00299       return true;
00300    }
00301 
00302     template <class FileStream, class FileData, class FileHeader>
00303    typename std::list<FileHeader>::const_iterator
00304    FileFilterFrameWithHeader<FileStream,FileData,FileHeader>::beginHeader()
00305       const throw(gpstk::InvalidRequest)
00306    {
00307       try { chl("beginHeader"); }
00308       catch(gpstk::InvalidRequest exc)
00309       { GPSTK_RETHROW(exc); }
00310       return headerList.begin();
00311    }
00312 
00313    template <class FileStream, class FileData, class FileHeader>
00314    typename std::list<FileHeader>::const_iterator
00315    FileFilterFrameWithHeader<FileStream,FileData,FileHeader>::endHeader()
00316       const throw(gpstk::InvalidRequest)
00317    {
00318       try { chl("endHeader"); }
00319       catch(gpstk::InvalidRequest exc)
00320       { GPSTK_RETHROW(exc); }
00321       return headerList.end();
00322    }
00323 
00324    template <class FileStream, class FileData, class FileHeader>
00325    typename std::list<FileHeader>::iterator
00326    FileFilterFrameWithHeader<FileStream,FileData,FileHeader>::beginHeader()
00327       throw(gpstk::InvalidRequest)
00328    {
00329       try { chl("beginHeader"); }
00330       catch(gpstk::InvalidRequest exc)
00331       { GPSTK_RETHROW(exc); }
00332       return headerList.begin();
00333    }
00334 
00335 
00336    template <class FileStream, class FileData, class FileHeader>
00337    typename std::list<FileHeader>::iterator
00338    FileFilterFrameWithHeader<FileStream,FileData,FileHeader>::endHeader()
00339       throw(gpstk::InvalidRequest)
00340    {
00341       try { chl("endHeader"); }
00342       catch(gpstk::InvalidRequest exc)
00343       { GPSTK_RETHROW(exc); }
00344       return headerList.end();
00345    }
00346 
00347    template <class FileStream, class FileData, class FileHeader>
00348    FileHeader&
00349    FileFilterFrameWithHeader<FileStream,FileData,FileHeader>::frontHeader()
00350       throw(gpstk::InvalidRequest)
00351    {
00352       try { chl("frontHeader"); }
00353       catch(gpstk::InvalidRequest exc)
00354       { GPSTK_RETHROW(exc); }
00355       return headerList.front();
00356    }
00357 
00358 
00359    template <class FileStream, class FileData, class FileHeader>
00360    const FileHeader&
00361    FileFilterFrameWithHeader<FileStream,FileData,FileHeader>::frontHeader()
00362       const throw(gpstk::InvalidRequest)
00363    {
00364       try { chl("frontHeader"); }
00365       catch(gpstk::InvalidRequest exc)
00366       { GPSTK_RETHROW(exc); }
00367       return headerList.front();
00368    }
00369 
00370 
00371    template <class FileStream, class FileData, class FileHeader>
00372    FileHeader&
00373    FileFilterFrameWithHeader<FileStream,FileData,FileHeader>::backHeader()
00374       throw(gpstk::InvalidRequest)
00375    {
00376       try { chl("backHeader"); }
00377       catch(gpstk::InvalidRequest exc)
00378       { GPSTK_RETHROW(exc); }
00379       return headerList.back();
00380    }
00381 
00382 
00383    template <class FileStream, class FileData, class FileHeader>
00384    const FileHeader&
00385    FileFilterFrameWithHeader<FileStream,FileData,FileHeader>::backHeader()
00386       const throw(gpstk::InvalidRequest)
00387    {
00388       try { chl("backHeader"); }
00389       catch(gpstk::InvalidRequest exc)
00390       { GPSTK_RETHROW(exc); }
00391       return headerList.back();
00392    }
00393 
00394    template <class FileStream, class FileData, class FileHeader>
00395    void
00396    FileFilterFrameWithHeader<FileStream,FileData,FileHeader> :: 
00397    init(const std::vector<FileHunter::FilterPair>& filter)
00398       throw(gpstk::Exception)
00399    {
00400          // find the files
00401       FileHunter fh(this->fs);
00402    
00403       typename std::vector<FileHunter::FilterPair>::const_iterator itr = 
00404          filter.begin();
00405       while (itr != filter.end())
00406       {
00407          fh.setFilter((*itr).first, (*itr).second);
00408          itr++;
00409       }
00410 
00411       std::vector<std::string> listOfFiles = 
00412          fh.find(this->startTime, this->endTime, FileSpec::ascending);
00413 
00414          // for each file, just read the header
00415       typename std::vector<std::string>::iterator i;
00416       for(i = listOfFiles.begin(); i != listOfFiles.end(); i++)
00417       {
00418          FileStream s((*i).c_str());
00419 
00420          if (s.good())
00421          {
00422             s.exceptions(std::ios::failbit);
00423 
00424             FileHeader header;
00425             s >> header;
00426             headerList.push_back(header);
00427          }
00428       }
00429    }  
00430 
00431 } // namespace gpstk
00432 
00433 #endif //GPSTK_FILEFILTERFRAMEWITHHEADER_HPP

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