FileFilterFrame.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: FileFilterFrame.hpp 70 2006-08-01 18:36:21Z ehagen $"
00002 
00003 
00004 
00010 #ifndef GPSTK_FILEFILTERFRAME_HPP
00011 #define GPSTK_FILEFILTERFRAME_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 "FileSpec.hpp"
00055 #include "FileFilter.hpp"
00056 #include "FileHunter.hpp"
00057 #include "FileUtils.hpp"
00058 
00059 namespace gpstk
00060 {
00063 
00079    template <class FileStream, class FileData>
00080    class FileFilterFrame : public FileFilter<FileData>
00081    {
00082    public:
00084       FileFilterFrame(const gpstk::DayTime& start = 
00085                          gpstk::DayTime::BEGINNING_OF_TIME,
00086                       const gpstk::DayTime& end = 
00087                          gpstk::DayTime::END_OF_TIME)
00088          throw(gpstk::Exception);
00089 
00091       FileFilterFrame(const std::vector<std::string>& fileList,
00092                       const gpstk::DayTime& start = 
00093                          gpstk::DayTime::BEGINNING_OF_TIME,
00094                       const gpstk::DayTime& end = 
00095                          gpstk::DayTime::END_OF_TIME)
00096          throw(gpstk::Exception);
00097 
00100       FileFilterFrame(const std::string& filename, 
00101                       const gpstk::DayTime& start = 
00102                          gpstk::DayTime::BEGINNING_OF_TIME,
00103                       const gpstk::DayTime& end = 
00104                          gpstk::DayTime::END_OF_TIME)
00105          throw(gpstk::Exception);
00106 
00110       FileFilterFrame(const FileSpec& spec, 
00111                       const gpstk::DayTime& start = 
00112                          gpstk::DayTime::BEGINNING_OF_TIME,
00113                       const gpstk::DayTime& end = 
00114                          gpstk::DayTime::END_OF_TIME,
00115                       const std::vector<FileHunter::FilterPair>& filter = 
00116                       std::vector<FileHunter::FilterPair>())
00117          throw(gpstk::Exception);
00118 
00122       FileFilterFrame& 
00123       newSource(const FileSpec& filespec, 
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 
00133       FileFilterFrame& 
00134       newSource(const std::string& filename, 
00135                 const gpstk::DayTime& start = 
00136                    gpstk::DayTime::BEGINNING_OF_TIME,
00137                 const gpstk::DayTime& end = 
00138                    gpstk::DayTime::END_OF_TIME)
00139          throw(gpstk::Exception);
00140 
00142       FileFilterFrame&
00143       newSource(const std::vector<std::string>& fileList,
00144                 const gpstk::DayTime& start = 
00145                    gpstk::DayTime::BEGINNING_OF_TIME,
00146                 const gpstk::DayTime& end = 
00147                    gpstk::DayTime::END_OF_TIME)
00148          throw(gpstk::Exception);
00149 
00150       virtual ~FileFilterFrame() {}
00151 
00162       bool writeFile(const std::string& outputFile,
00163                      const bool append = false) const
00164          throw(gpstk::Exception);
00165 
00175       bool writeFile(FileStream& stream) const
00176          throw(gpstk::Exception);
00177 
00178    protected:
00180       void init(const std::vector<FileHunter::FilterPair>& filter= 
00181                 std::vector<FileHunter::FilterPair>()) 
00182          throw(gpstk::Exception);
00183 
00184 
00185    protected:   
00187       FileSpec fs;
00189       gpstk::DayTime startTime, endTime;
00190 
00191    };
00192 
00194 
00195    template <class FileStream, class FileData>
00196    FileFilterFrame<FileStream,FileData> :: 
00197    FileFilterFrame(const gpstk::DayTime& start,
00198                    const gpstk::DayTime& end)
00199       throw(gpstk::Exception)
00200          : startTime(start), endTime(end)
00201    {}
00202 
00203    template <class FileStream, class FileData>
00204    FileFilterFrame<FileStream,FileData> :: 
00205    FileFilterFrame(const std::vector<std::string>& fileList,
00206                    const gpstk::DayTime& start,
00207                    const gpstk::DayTime& end)
00208       throw(gpstk::Exception)
00209          : startTime(start), endTime(end)
00210    {
00211       typename std::vector<std::string>::const_iterator itr;
00212       for (itr = fileList.begin(); itr != fileList.end(); itr++)
00213       {
00214          fs.newSpec(*itr);
00215          init();
00216       }
00217    }
00218 
00219    template <class FileStream, class FileData>
00220    FileFilterFrame<FileStream,FileData> :: 
00221    FileFilterFrame(const std::string& filename, 
00222                    const gpstk::DayTime& start,
00223                    const gpstk::DayTime& end)
00224       throw(gpstk::Exception)
00225          : fs(filename), startTime(start), endTime(end)
00226    {
00227       init();
00228    }
00229 
00230    template <class FileStream, class FileData>
00231    FileFilterFrame<FileStream,FileData> :: 
00232    FileFilterFrame(const FileSpec& spec, 
00233                    const gpstk::DayTime& start,
00234                    const gpstk::DayTime& end,
00235                    const std::vector<FileHunter::FilterPair>& filter)
00236       throw(gpstk::Exception)
00237          : fs(spec), startTime(start), endTime(end)
00238    {
00239       init(filter);
00240    }
00241 
00242    template <class FileStream, class FileData>
00243    FileFilterFrame<FileStream, FileData>& 
00244    FileFilterFrame<FileStream,FileData> :: 
00245    newSource(const FileSpec& filespec, 
00246              const gpstk::DayTime& start,
00247              const gpstk::DayTime& end,
00248              const std::vector<FileHunter::FilterPair>& filter)
00249       throw(gpstk::Exception)
00250    {
00251       startTime = start;
00252       endTime = end;
00253 
00254       fs = filespec;
00255       init(filter);
00256       return *this;
00257    }
00258 
00259    template <class FileStream, class FileData>
00260    FileFilterFrame<FileStream, FileData>& 
00261    FileFilterFrame<FileStream,FileData> :: 
00262    newSource(const std::string& filename, 
00263              const gpstk::DayTime& start,
00264              const gpstk::DayTime& end)
00265       throw(gpstk::Exception)
00266    {
00267       startTime = start;
00268       endTime = end;
00269    
00270       fs.newSpec(filename);
00271       init();
00272       return *this;
00273    }
00274 
00275    template <class FileStream, class FileData>
00276    FileFilterFrame<FileStream, FileData>& 
00277    FileFilterFrame<FileStream,FileData> :: 
00278    newSource(const std::vector<std::string>& fileList, 
00279              const gpstk::DayTime& start,
00280              const gpstk::DayTime& end)
00281       throw(gpstk::Exception)
00282    {
00283       startTime = start;
00284       endTime = end;
00285    
00286       typename std::vector<std::string>::const_iterator itr;
00287       for (itr = fileList.begin(); itr != fileList.end(); itr++)
00288       {
00289          fs.newSpec(*itr);
00290          init();
00291       }
00292       return *this;
00293    }
00294 
00295    template <class FileStream, class FileData>
00296    void
00297    FileFilterFrame<FileStream,FileData> :: 
00298    init(const std::vector<FileHunter::FilterPair>& filter)
00299       throw(gpstk::Exception)
00300    {
00301          // find the files
00302       FileHunter fh(fs);
00303    
00304       typename std::vector<FileHunter::FilterPair>::const_iterator itr = 
00305          filter.begin();
00306       while (itr != filter.end())
00307       {
00308          fh.setFilter((*itr).first, (*itr).second);
00309          itr++;
00310       }
00311 
00312       std::vector<std::string> listOfFiles = 
00313          fh.find(startTime, endTime, FileSpec::ascending);
00314 
00315          // for each file, read it into the filter
00316       typename std::vector<std::string>::iterator i;
00317       for(i = listOfFiles.begin(); i != listOfFiles.end(); i++)
00318       {
00319          FileStream s((*i).c_str());
00320 
00321          if (s.good())
00322          {
00323             s.exceptions(std::ios::failbit);
00324          
00325             FileData data;
00326          
00327             while (s >> data)
00328             {
00329                addData(data);
00330             }
00331          }
00332       }
00333    }
00334 
00335    template <class FileStream, class FileData>
00336    bool FileFilterFrame<FileStream,FileData> :: 
00337    writeFile(const std::string& str,
00338              const bool append) const
00339       throw(gpstk::Exception)
00340    {
00341       if (!this->dataVec.empty())
00342       {
00343             // make the directory (if needed)
00344          std::string::size_type pos = str.rfind('/');
00345          if (pos != std::string::npos)
00346             gpstk::FileUtils::makeDir(str.substr(0,pos).c_str(), 0755);      
00347 
00348          std::ios::openmode mode = std::ios::out;
00349          if (append)
00350             mode |= (std::ios::app|std::ios::ate);
00351       
00352          FileStream stream(str.c_str(), mode);
00353          return writeFile(stream);
00354       }
00355 
00356       return true;
00357    }
00358 
00359    template <class FileStream, class FileData>
00360    bool FileFilterFrame<FileStream,FileData> :: 
00361    writeFile(FileStream& stream)
00362       const throw(gpstk::Exception)
00363    {
00364       if (!this->dataVec.empty())
00365       {
00366          stream.exceptions(std::ios::failbit);      
00367       
00368          typename std::list<FileData>::const_iterator index;
00369          for(index = this->dataVec.begin(); index != this->dataVec.end(); index++)
00370             (*index).putRecord(stream);
00371       }
00372 
00373       return true;
00374    }
00375 
00376 }  // namespace gpstk
00377 
00378 #endif // GPSTK_FILEFILTERFRAME_HPP

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