RTFileFrame.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_RTFILEFRAME_HPP
45 #define GNSSTK_RTFILEFRAME_HPP
46 
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <iterator>
50 #include "FileSpec.hpp"
51 #include "FileFilter.hpp"
52 
53 namespace gnsstk
54 {
56 
57 
61  {
62  public:
64  {
65  Dumb = 0,
66  Smart = 1
67  };
68 
74  {
77  };
78  };
79 
80  // forward declaration of the RTFileFrame class
81  template <class FileStream, class FileData>
82  class RTFileFrame;
83 
88  template <class FileStream, class FileData>
90  {
91  public:
94  : rtff(NULL)
95  {}
96 
99  : rtff(right.rtff)
100  {}
101 
103  virtual ~RTFileFrameIterator() {}
104 
107  {
108  rtff = r.rtff;
109  return *this;
110  }
111 
114  {
115  rtff->getRecord();
116  return *this;
117  }
118 
121  {
123  read();
124  return temp;
125  }
126 
128  const FileData& operator*() const
129  {
130  return rtff->lastData;
131  }
132 
134  const FileData* operator->() const
135  {
136  return &(rtff->lastData);
137  }
138 
140  bool operator==(const RTFileFrameIterator& right) const
141  {
142  if (rtff == right.rtff)
143  {
144  if (rtff == NULL)
145  return true;
146 
147  if ((rtff->isOK == right.rtff->isOK) ||
148  (!rtff->isOK && (right.rtff->fileStream == rtff->fileStream)))
149  return true;
150  return false;
151  }
152  else if ((rtff == NULL) && (right.rtff))
153  return !right.rtff->isOK;
154  else if ((right.rtff == NULL) && (rtff))
155  return !rtff->isOK;
156  else
157  return false;
158  }
159 
161  bool operator!=(const RTFileFrameIterator& right) const
162  { return !(*this == right);}
163 
165  friend class RTFileFrame<FileStream, FileData>;
166 
167  protected:
169  void read()
170  { rtff->getRecord(); }
171 
172  private:
175  : rtff(&r)
176  { rtff->getRecord(); }
177 
180  };
181 
228  template <class FileStream, class FileData>
229  class RTFileFrame : public RTFileFrameHelper
230  {
231  public:
241  RTFileFrame(const gnsstk::FileSpec& fnFormat,
244  const RTFileFrameHelper::FileReadingMode frm = AppendedData,
245  const RTFileFrameHelper::GetRecordMode grm = Dumb);
246 
248  ~RTFileFrame();
249 
251  RTFileFrame& setFileReadingMode(const FileReadingMode f);
252 
254  RTFileFrame& setGetRecordMode(const GetRecordMode g);
255 
261  void waitForData(unsigned wait = 0);
262 
265  bool hasFileChanged();
266 
268  bool endOfDataSet();
269 
272 
279 
282  bool getRecord();
283 
285  void openNextDay();
286 
288  const FileData& data() const { return lastData;}
289 
294  std::list<FileData> getList() const;
295 
301 
303  std::string getCurrentFile() const {return currentFileName;}
304 
307 
309  friend class RTFileFrameIterator<FileStream, FileData>;
310 
312  bool openCurrentFile();
313 
315  void closeCurrentFile();
316 
317  protected:
319  FileStream* fileStream;
320 
321  FileData lastData;
323  std::streampos lastPosition;
325  std::string currentFileName;
328 
332  FileReadingMode readMode;
335  GetRecordMode getMode;
337  struct stat fileInfo;
341  bool isOK;
342  };
343 
345 
346  template <class FileStream, class FileData>
349  const gnsstk::CommonTime& beginning,
350  const gnsstk::CommonTime& ending,
353  : fileStream(NULL), fs(fnFormat), startTime(beginning),
354  currentTime(beginning), endTime(ending), readMode(frm), getMode(grm)
355  {
356  // zero out seconds
357  startTime = MJD(floor(MJD(startTime).mjd));
358  endTime = MJD(floor(MJD(endTime).mjd));
359  currentTime = MJD(floor(MJD(curretnTime).mjd));
360 
361  // set up the stream
362  openCurrentFile();
363  }
364 
365  template <class FileStream, class FileData>
367  {
368  if (fileStream)
369  {
370  fileStream->close();
371  delete fileStream;
372  }
373  }
374 
375  template <class FileStream, class FileData>
379  {
380  readMode = f;
381  return *this;
382  }
383 
385  template <class FileStream, class FileData>
389  {
390  getMode = g;
391  return *this;
392  }
393 
394  template <class FileStream, class FileData>
395  void
397  {
398  sleep(wait);
399  if(readMode == AppendedData)
400  {
401  // reopen the file and skip to where we left off - openCurrentFile
402  // will overwrite lastPosition...
403  std::streampos cachedPos = lastPosition;
404  if(openCurrentFile())
405  fileStream->seekg(cachedPos);
406  lastPosition = cachedPos;
407  }
408  else
409  {
410  // start from the beginning
411  openCurrentFile();
412  }
413  }
414 
415  template <class FileStream, class FileData>
416  bool
418  {
419  struct stat tempStat;
420  if (stat(currentFileName.c_str(), &tempStat))
421  return false;
422  if (tempStat.st_mtime != fileInfo.st_mtime)
423  return true;
424  else
425  return false;
426  }
427 
428  template <class FileStream, class FileData>
429  bool
431  {
432  if (currentTime > endTime)
433  return true;
434  else
435  return false;
436  }
437 
438  template <class FileStream, class FileData>
441  {
443  }
444 
445  template <class FileStream, class FileData>
448  {
449  if (readMode == AppendedData)
450  {
451  ;
452  }
453  else // if (readMode == FromTheBeginning)
454  {
455  openCurrentFile();
456  }
457  // this constructor does not read in the first record, so force it
458  // to do so before returning
460  return toReturn;
461  }
462 
463  template <class FileStream, class FileData>
464  bool
466  {
467  if (!isOK)
468  return false;
469 
470  // is the stream still good to read?
471  if (*fileStream >> lastData)
472  {
473  lastPosition = fileStream->tellg();
474  return true;
475  }
476  // the last read failed - try opening the next file until
477  // today's file is reched or else return false
478  else
479  {
480  // read the next file or not?
481  if (getMode == Smart)
482  {
483  if (!endOfDataSet())
484  {
485  // still before today?
486  gnsstk::CommonTime today;
487  today.setYDoySod(today.year(), today.DOY(), 0);
488 
489  if (currentTime < today)
490  {
491  openNextDay();
492  return getRecord();
493  }
494  // we've read through today, so just wait until tomorrow's
495  // data comes in - the user needs to call waitForData()
496  // to continue
497  else
498  isOK = false;
499  }
500  else
501  {
502  isOK = false;
503  }
504  } // if (getmode == smart)
505 
506  // in dumb mode, the user needs to call openNextDay or
507  // openCurrentFile to continue.
508  else
509  {
510  isOK = false;
511  }
512  } // if (isOK)
513  return isOK;
514  }
515 
516  template <class FileStream, class FileData>
517  void
519  {
520  // open a new file for another day, if any.
521  currentTime += gnsstk::CommonTime::SEC_DAY;
522  if (!endOfDataSet())
523  openCurrentFile();
524  }
525 
526  template <class FileStream, class FileData>
527  std::list<FileData>
529  {
531  std::list<FileData> toReturn;
532  inputStream = begin();
533  while (inputStream != end())
534  {
535  toReturn.push_back(data());
536  }
537  return toReturn;
538  }
539 
540  template <class FileStream, class FileData>
543  {
545  FileFilter<FileData> toReturn;
546  inputStream = begin();
547  while (inputStream != end())
548  {
549  toReturn.addData(data());
550  }
551  return toReturn;
552  }
553 
554  template <class FileStream, class FileData>
555  bool
557  {
558  isOK = false;
559  currentFileName = fs.toString(currentTime);
560  stat(currentFileName.c_str(), &fileInfo);
561  lastPosition = 0;
562 
563  if(fileStream)
564  {
565  fileStream->close();
566  delete fileStream;
567  fileStream = NULL;
568  }
569 
570  fileStream = new FileStream;
571  fileStream->open(currentFileName.c_str(), std::ios::in);
572  if (!fileStream->fail())
573  isOK = true;
574  return isOK;
575  }
576 
577  template <class FileStream, class FileData>
578  void
580  {
581  if(fileStream)
582  {
583  fileStream->close();
584  delete fileStream;
585  fileStream = NULL;
586  }
587  isOK = false;
588  }
589 
590 } // namespace gnsstk
591 
592 #endif // GNSSTK_RTFILEFRAME_HPP
gnsstk::RTFileFrameIterator::RTFileFrameIterator
RTFileFrameIterator(const RTFileFrameIterator &right)
copy constructor
Definition: RTFileFrame.hpp:98
gnsstk::RTFileFrame::getMode
GetRecordMode getMode
the GetRecordMode for the reader
Definition: RTFileFrame.hpp:335
gnsstk::RTFileFrameIterator::RTFileFrameIterator
RTFileFrameIterator(gnsstk::RTFileFrame< FileStream, FileData > &r)
constructor for use by gnsstk::RTFileFrame<FileStream, FileData>.
Definition: RTFileFrame.hpp:174
gnsstk::RTFileFrame::lastData
FileData lastData
Definition: RTFileFrame.hpp:321
gnsstk::RTFileFrame::RTFileFrame
RTFileFrame(const gnsstk::FileSpec &fnFormat, const gnsstk::CommonTime &beginning=gnsstk::CommonTime::BEGINNING_OF_TIME, const gnsstk::CommonTime &ending=gnsstk::CommonTime::END_OF_TIME, const RTFileFrameHelper::FileReadingMode frm=AppendedData, const RTFileFrameHelper::GetRecordMode grm=Dumb)
gnsstk::FileFilter
Definition: FileFilter.hpp:76
gnsstk::RTFileFrame::getRecord
bool getRecord()
Definition: RTFileFrame.hpp:465
gnsstk::RTFileFrameIterator::operator->
const FileData * operator->() const
dereference pointer for current object
Definition: RTFileFrame.hpp:134
gnsstk::RTFileFrame::getCurrentTime
gnsstk::CommonTime getCurrentTime() const
returns the current time used for finding files
Definition: RTFileFrame.hpp:306
example6.mjd
mjd
Definition: example6.py:102
gnsstk::RTFileFrame::openNextDay
void openNextDay()
when in Dumb mode, use this function to advance to the next day.
Definition: RTFileFrame.hpp:518
gnsstk::RTFileFrame::data
const FileData & data() const
returns the last read data
Definition: RTFileFrame.hpp:288
gnsstk::RTFileFrameIterator::operator++
RTFileFrameIterator operator++(int)
postfix increment of internal iterator
Definition: RTFileFrame.hpp:120
gnsstk::RTFileFrameHelper::FromTheBeginning
@ FromTheBeginning
reread from the beginning every time.
Definition: RTFileFrame.hpp:76
gnsstk::RTFileFrame::readMode
FileReadingMode readMode
the FileReadingMode for the reader
Definition: RTFileFrame.hpp:333
gnsstk::RTFileFrameIterator::operator*
const FileData & operator*() const
dereference object for current object
Definition: RTFileFrame.hpp:128
gnsstk::RTFileFrameHelper::GetRecordMode
GetRecordMode
Definition: RTFileFrame.hpp:63
gnsstk::RTFileFrame::lastPosition
std::streampos lastPosition
the last get position from the file
Definition: RTFileFrame.hpp:323
gnsstk::RTFileFrameHelper::AppendedData
@ AppendedData
read only appended data.
Definition: RTFileFrame.hpp:75
gnsstk::RTFileFrame::hasFileChanged
bool hasFileChanged()
Definition: RTFileFrame.hpp:417
gnsstk::RTFileFrame::~RTFileFrame
~RTFileFrame()
destructor
Definition: RTFileFrame.hpp:366
NULL
#define NULL
Definition: getopt1.c:64
gnsstk::CommonTime::BEGINNING_OF_TIME
static const GNSSTK_EXPORT CommonTime BEGINNING_OF_TIME
earliest representable CommonTime
Definition: CommonTime.hpp:102
gnsstk::RTFileFrame::fs
gnsstk::FileSpec fs
the file spec for determining file names
Definition: RTFileFrame.hpp:327
FileFilter.hpp
gnsstk::RTFileFrameIterator::read
void read()
gets the next record
Definition: RTFileFrame.hpp:169
gnsstk::RTFileFrame::endOfDataSet
bool endOfDataSet()
Returns true if all data for the given time has been read.
Definition: RTFileFrame.hpp:430
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::FileFilter::addData
FileFilter & addData(const FileData &ffd)
Adds the given data into the filter.
Definition: FileFilter.hpp:401
gnsstk::FileSpec
Definition: FileSpec.hpp:80
example4.temp
temp
Definition: example4.py:35
gnsstk::CommonTime::END_OF_TIME
static const GNSSTK_EXPORT CommonTime END_OF_TIME
latest representable CommonTime
Definition: CommonTime.hpp:104
gnsstk::RTFileFrame::setGetRecordMode
RTFileFrame & setGetRecordMode(const GetRecordMode g)
Allows changing of the GetRecordMode.
Definition: RTFileFrame.hpp:388
gnsstk::RTFileFrameIterator::~RTFileFrameIterator
virtual ~RTFileFrameIterator()
destructor
Definition: RTFileFrame.hpp:103
FileSpec.hpp
gnsstk::RTFileFrameHelper
Definition: RTFileFrame.hpp:60
gnsstk::RTFileFrame::fileStream
FileStream * fileStream
the internal file stream for the internal iterator
Definition: RTFileFrame.hpp:319
gnsstk::RTFileFrameIterator::rtff
gnsstk::RTFileFrame< FileStream, FileData > * rtff
gives access to the RTFileFrame being used
Definition: RTFileFrame.hpp:179
gnsstk::RTFileFrameIterator
Definition: RTFileFrame.hpp:89
gnsstk::RTFileFrame::isOK
bool isOK
Definition: RTFileFrame.hpp:341
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::RTFileFrameHelper::Dumb
@ Dumb
only tries to read once from current file
Definition: RTFileFrame.hpp:65
gnsstk::RTFileFrame::startTime
gnsstk::CommonTime startTime
start time for file searching
Definition: RTFileFrame.hpp:329
gnsstk::RTFileFrame
Definition: RTFileFrame.hpp:82
gnsstk::RTFileFrame::openCurrentFile
bool openCurrentFile()
reopens the file currentFileName
Definition: RTFileFrame.hpp:556
gnsstk::RTFileFrameHelper::Smart
@ Smart
if cannot read, rolls files or waits for data
Definition: RTFileFrame.hpp:66
gnsstk::RTFileFrame::currentTime
gnsstk::CommonTime currentTime
time last used for finding a file
Definition: RTFileFrame.hpp:330
gnsstk::RTFileFrameHelper::FileReadingMode
FileReadingMode
Definition: RTFileFrame.hpp:73
example3.data
data
Definition: example3.py:22
gnsstk::RTFileFrame::setFileReadingMode
RTFileFrame & setFileReadingMode(const FileReadingMode f)
Allows changing of the FileReadingMode.
Definition: RTFileFrame.hpp:378
gnsstk::RTFileFrameIterator::operator!=
bool operator!=(const RTFileFrameIterator &right) const
inequality operator
Definition: RTFileFrame.hpp:161
gnsstk::RTFileFrameIterator::operator=
RTFileFrameIterator & operator=(const RTFileFrameIterator &r)
assignment operator
Definition: RTFileFrame.hpp:106
gnsstk::RTFileFrame::fileInfo
struct stat fileInfo
keeps track of the last modify date for the last file read
Definition: RTFileFrame.hpp:337
gnsstk::RTFileFrame::begin
RTFileFrameIterator< FileStream, FileData > begin()
Definition: RTFileFrame.hpp:447
gnsstk::RTFileFrame::getCurrentFile
std::string getCurrentFile() const
returns the name of the current file begin used
Definition: RTFileFrame.hpp:303
gnsstk::MJD
Definition: MJD.hpp:54
gnsstk::RTFileFrameIterator::RTFileFrameIterator
RTFileFrameIterator()
default constructor
Definition: RTFileFrame.hpp:93
gnsstk::RTFileFrame::waitForData
void waitForData(unsigned wait=0)
Definition: RTFileFrame.hpp:396
gnsstk::RTFileFrame::getList
std::list< FileData > getList() const
Definition: RTFileFrame.hpp:528
gnsstk::RTFileFrame::closeCurrentFile
void closeCurrentFile()
close the file currentFileName
Definition: RTFileFrame.hpp:579
gnsstk::RTFileFrameIterator::operator==
bool operator==(const RTFileFrameIterator &right) const
equality operator
Definition: RTFileFrame.hpp:140
gnsstk::RTFileFrame::currentFileName
std::string currentFileName
the last file to be read
Definition: RTFileFrame.hpp:325
gnsstk::RTFileFrameIterator::operator++
RTFileFrameIterator operator++()
prefix increment of internal iterator
Definition: RTFileFrame.hpp:113
gnsstk::RTFileFrame::getFileFilter
FileFilter< FileData > getFileFilter() const
Definition: RTFileFrame.hpp:542
gnsstk::RTFileFrame::endTime
gnsstk::CommonTime endTime
Definition: RTFileFrame.hpp:331
gnsstk::RTFileFrame::end
RTFileFrameIterator< FileStream, FileData > end()
end of data set iterator, useful for loops
Definition: RTFileFrame.hpp:440


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