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
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
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
00096 std::string getCurrentFilename(void) const
00097 { return currentFilename; };
00098
00099
00100
00101 CommonTime getCurrentTime(void) const
00102 { return currentTime; };
00103
00104
00105
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
00153 std::ios::openmode omode;
00154 };
00155
00157 }
00158
00159 #endif // GPSTK_TIME_NAMED_FILE_STREAM_HPP