TimeNamedFileStream.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: TimeNamedFileStream.hpp 3143 2012-06-19 16:19:50Z snelsen $"
00002 
00008 #ifndef GPSTK_TIME_NAMED_FILE_STREAM_HPP
00009 #define GPSTK_TIME_NAMED_FILE_STREAM_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 //  Copyright 2004, The University of Texas at Austin
00030 //
00031 //============================================================================
00032 
00033 //============================================================================
00034 //
00035 //This software developed by Applied Research Laboratories at the University of
00036 //Texas at Austin, under contract to an agency or agencies within the U.S.
00037 //Department of Defense. The U.S. Government retains all rights to use,
00038 //duplicate, distribute, disclose, or release this software.
00039 //
00040 //Pursuant to DoD Directive 523024
00041 //
00042 // DISTRIBUTION STATEMENT A: This software has been approved for public
00043 //                           release, distribution is unlimited.
00044 //
00045 //=============================================================================
00046 
00047 #include <string>
00048 
00049 #include "Exception.hpp"
00050 #include "CommonTime.hpp"
00051 #include "SystemTime.hpp"
00052 #include "FFStream.hpp"
00053 #include "TimeString.hpp"
00054 
00055 namespace gpstk
00056 {
00059 
00060    template <class BaseStream>
00061    class TimeNamedFileStream : public BaseStream
00062    {
00063    public:
00064 
00065       TimeNamedFileStream() 
00066          : omode(std::ios::in), debugLevel(0)
00067       {};
00068 
00069       TimeNamedFileStream(
00070          const std::string fs,
00071          std::ios::openmode mode = std::ios::in)
00072          : filespec(fs), omode(mode), debugLevel(0)
00073       {};
00074       
00075       virtual ~TimeNamedFileStream(void) {};
00076 
00077 
00080       virtual void open(const char* fs, std::ios::openmode mode = std::ios::in)
00081       {
00082          setFilespec(fs);
00083          omode = mode;
00084       };
00085 
00086 
00087       void setFilespec(const std::string fs)
00088       { filespec=fs; currentFilename=""; }
00089 
00090 
00091       std::string getFilespec(void) const
00092       { return filespec;}
00093 
00094 
00095       // Get the filename of the current file
00096       std::string getCurrentFilename(void) const 
00097       { return currentFilename; };
00098 
00099 
00100       // Return the time used to generate the current file name
00101       CommonTime getCurrentTime(void) const
00102       { return currentTime; };
00103 
00104 
00105       // Update the file name, returns true if the file name changed
00106       bool updateFileName(const CommonTime& t=SystemTime())
00107       {
00108          bool openedNewFile = false;
00109          const std::string newFilename=printTime(t,filespec);
00110          if (currentFilename.size() == 0 && newFilename.size() > 0)
00111          {
00112             currentFilename = newFilename;
00113             currentTime = t;
00114             BaseStream::open(currentFilename.c_str(), omode);
00115             if (debugLevel)
00116                std::cout << "Opened " << currentFilename << std::endl;
00117             openedNewFile=true;
00118          }
00119          else if (newFilename == currentFilename)
00120          {
00121             currentTime = t;
00122             openedNewFile=false;
00123          }
00124          else
00125          {
00126             if (debugLevel)
00127                std::cout << "Closing " << currentFilename << std::endl;
00128             BaseStream::close();
00129             currentFilename = newFilename;
00130             currentTime = t;
00131             BaseStream::open(currentFilename.c_str(), omode);
00132             if (debugLevel)
00133                std::cout << "Opened " << currentFilename << std::endl;
00134             openedNewFile=true;
00135          }
00136 
00137          return openedNewFile;
00138       };
00139 
00140       int debugLevel;
00141 
00142    private:
00144       std::string filespec;
00145       
00147       std::string currentFilename;
00148 
00150       CommonTime currentTime;
00151 
00152       // The flags to use when opening the files
00153       std::ios::openmode omode;
00154    }; // end class TimeNamedFileStream
00155 
00157 }  // end namespace gpstk
00158 
00159 #endif // GPSTK_TIME_NAMED_FILE_STREAM_HPP

Generated on Tue May 21 03:31:16 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1