TimeNamedFileStream.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: TimeNamedFileStream.hpp 1161 2008-03-27 17:16:22Z ckiesch $"
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "DayTime.hpp"
00051 #include "FFStream.hpp"
00052 
00053 namespace gpstk
00054 {
00057 
00058    template <class BaseStream>
00059    class TimeNamedFileStream : public BaseStream
00060    {
00061    public:
00062 
00063       TimeNamedFileStream() 
00064          : omode(std::ios::in), debugLevel(0)
00065       {};
00066 
00067       TimeNamedFileStream(
00068          const std::string fs,
00069          std::ios::openmode mode = std::ios::in)
00070          : filespec(fs), omode(mode), debugLevel(0)
00071       {};
00072       
00073       virtual ~TimeNamedFileStream(void) {};
00074 
00075 
00078       virtual void open(const char* fs, std::ios::openmode mode = std::ios::in)
00079       {
00080          setFilespec(fs);
00081          omode = mode;
00082       };
00083 
00084 
00085       void setFilespec(const std::string fs)
00086       { filespec=fs; currentFilename=""; }
00087 
00088 
00089       std::string getFilespec(void) const
00090       { return filespec;}
00091 
00092 
00093       // Get the filename of the current file
00094       std::string getCurrentFilename(void) const 
00095       { return currentFilename; };
00096 
00097 
00098       // Return the time used to generate the current file name
00099       DayTime getCurrentTime(void) const
00100       { return currentTime; };
00101 
00102 
00103       // Update the file name, returns true if the file name changed
00104       bool updateFileName(const DayTime& t=DayTime())
00105       {
00106          bool openedNewFile = false;
00107          const std::string newFilename=t.printf(filespec);
00108          if (currentFilename.size() == 0 && newFilename.size() > 0)
00109          {
00110             currentFilename = newFilename;
00111             currentTime = t;
00112             BaseStream::open(currentFilename.c_str(), omode);
00113             if (debugLevel)
00114                std::cout << "Opened " << currentFilename << std::endl;
00115             openedNewFile=true;
00116          }
00117          else if (newFilename == currentFilename)
00118          {
00119             currentTime = t;
00120             openedNewFile=false;
00121          }
00122          else
00123          {
00124             if (debugLevel)
00125                std::cout << "Closing " << currentFilename << std::endl;
00126             BaseStream::close();
00127             currentFilename = newFilename;
00128             currentTime = t;
00129             BaseStream::open(currentFilename.c_str(), omode);
00130             if (debugLevel)
00131                std::cout << "Opened " << currentFilename << std::endl;
00132             openedNewFile=true;
00133          }
00134 
00135          return openedNewFile;
00136       };
00137 
00138       int debugLevel;
00139 
00140    private:
00142       std::string filespec;
00143       
00145       std::string currentFilename;
00146 
00148       DayTime currentTime;
00149 
00150       // The flags to use when opening the files
00151       std::ios::openmode omode;
00152    }; // end class TimeNamedFileStream
00153 
00155 }  // end namespace gpstk
00156 
00157 #endif // GPSTK_TIME_NAMED_FILE_STREAM_HPP

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