TimeNamedFileStream.hpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
44 #ifndef GNSSTK_TIME_NAMED_FILE_STREAM_HPP
45 #define GNSSTK_TIME_NAMED_FILE_STREAM_HPP
46 
47 #include <string>
48 
49 #include "Exception.hpp"
50 #include "CommonTime.hpp"
51 #include "SystemTime.hpp"
52 #include "FFStream.hpp"
53 #include "TimeString.hpp"
54 #include "FileUtils.hpp"
55 
56 namespace gnsstk
57 {
59 
60 
61  template <class BaseStream>
62  class TimeNamedFileStream : public BaseStream
63  {
64  public:
65 
67  : omode(std::ios::in), debugLevel(0)
68  {}
70  const std::string fs,
71  std::ios::openmode mode = std::ios::in)
72  : debugLevel(0), filespec(fs), omode(mode)
73  {}
74  virtual ~TimeNamedFileStream(void) {};
75 
76 
79  virtual void open(const char* fs, std::ios::openmode mode = std::ios::in)
80  {
81  setFilespec(fs);
82  omode = mode;
83  }
84 
85 
86  void setFilespec(const std::string fs)
87  { filespec=fs; currentFilename=""; }
88 
89 
90  std::string getFilespec(void) const
91  { return filespec; }
92 
93 
94  // Get the filename of the current file
95  std::string getCurrentFilename(void) const
96  { return currentFilename; }
97 
98 
99  // Return the time used to generate the current file name
101  { return currentTime; }
102 
103 
104  // Update the file name, returns true if the file name changed
106  {
107  const std::string newFilename=printTime(t,filespec);
108  if (currentFilename.size() > 0 and newFilename == currentFilename)
109  {
110  currentTime = t;
111  return false;
112  }
113 
114  if (currentFilename.size() > 0)
115  {
116  if (debugLevel)
117  std::cout << "Closing " << currentFilename << std::endl;
118  BaseStream::close();
119  }
120  currentFilename = newFilename;
121  currentTime = t;
122 
123  std::string::size_type i = newFilename.rfind('/');
124  std::string dir(newFilename.substr(0, i));
125  if (dir.size())
126  {
127  if (debugLevel)
128  std::cout << "Creating directory " << dir << std::endl;
129  gnsstk::FileUtils::makeDir(dir, 0755);
130  }
131 
132  BaseStream::open(currentFilename.c_str(), omode);
133  if (debugLevel)
134  std::cout << "Opened " << currentFilename << std::endl;
135 
136  return true;
137  }
138 
140 
141  private:
143  std::string filespec;
144 
146  std::string currentFilename;
147 
150 
151  // The flags to use when opening the files
152  std::ios::openmode omode;
153  }; // end class TimeNamedFileStream
154 
156 } // end namespace gnsstk
157 
158 #endif // GNSSTK_TIME_NAMED_FILE_STREAM_HPP
gnsstk::TimeNamedFileStream::getCurrentTime
CommonTime getCurrentTime(void) const
Definition: TimeNamedFileStream.hpp:100
gnsstk::TimeNamedFileStream::omode
std::ios::openmode omode
Definition: TimeNamedFileStream.hpp:152
gnsstk::TimeNamedFileStream::currentFilename
std::string currentFilename
Name of the current output file.
Definition: TimeNamedFileStream.hpp:146
gnsstk::TimeNamedFileStream::setFilespec
void setFilespec(const std::string fs)
Definition: TimeNamedFileStream.hpp:86
gnsstk::TimeNamedFileStream::getCurrentFilename
std::string getCurrentFilename(void) const
Definition: TimeNamedFileStream.hpp:95
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::TimeNamedFileStream::updateFileName
bool updateFileName(const CommonTime &t=SystemTime())
Definition: TimeNamedFileStream.hpp:105
gnsstk::TimeNamedFileStream
Definition: TimeNamedFileStream.hpp:62
SystemTime.hpp
gnsstk::TimeNamedFileStream::TimeNamedFileStream
TimeNamedFileStream(const std::string fs, std::ios::openmode mode=std::ios::in)
Definition: TimeNamedFileStream.hpp:69
gnsstk::TimeNamedFileStream::filespec
std::string filespec
Pattern on which to create new files.
Definition: TimeNamedFileStream.hpp:143
gnsstk::SystemTime
Definition: SystemTime.hpp:54
gnsstk::CommonTime
Definition: CommonTime.hpp:84
FileUtils.hpp
gnsstk::TimeNamedFileStream::open
virtual void open(const char *fs, std::ios::openmode mode=std::ios::in)
Definition: TimeNamedFileStream.hpp:79
gnsstk::TimeNamedFileStream::~TimeNamedFileStream
virtual ~TimeNamedFileStream(void)
Definition: TimeNamedFileStream.hpp:74
gnsstk::FileUtils::makeDir
int makeDir(const std::string &path, unsigned mode)
Definition: FileUtils.hpp:106
gnsstk::printTime
std::string printTime(const CommonTime &t, const std::string &fmt)
Definition: TimeString.cpp:64
Exception.hpp
CommonTime.hpp
std
Definition: Angle.hpp:142
gnsstk::TimeNamedFileStream::TimeNamedFileStream
TimeNamedFileStream()
Definition: TimeNamedFileStream.hpp:66
gnsstk::TimeNamedFileStream::debugLevel
int debugLevel
Definition: TimeNamedFileStream.hpp:139
FFStream.hpp
gnsstk::TimeNamedFileStream::getFilespec
std::string getFilespec(void) const
Definition: TimeNamedFileStream.hpp:90
gnsstk::TimeNamedFileStream::currentTime
CommonTime currentTime
The time used to generate currentFilename.
Definition: TimeNamedFileStream.hpp:149
TimeString.hpp


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:42