PetaVision  Alpha
FileStream.hpp
1 /*
2  * FileStream.hpp
3  *
4  * Created on: Jun 9, 2016
5  * Author: pschultz
6  */
7 
8 #ifndef __FILESTREAM_HPP__
9 #define __FILESTREAM_HPP__
10 
11 #include "PrintStream.hpp"
12 
13 #include <fstream>
14 
15 namespace PV {
16 
17 class FileStream : public PrintStream {
18  public:
19  FileStream(char const *path, std::ios_base::openmode mode, bool verifyWrites = false);
20  virtual ~FileStream();
21  virtual void write(void const *data, long length);
22  virtual void read(void *data, long length);
23  virtual void setOutPos(long pos, std::ios_base::seekdir seekAnchor);
24  virtual void setOutPos(long pos, bool fromBeginning);
25  virtual void setInPos(long pos, std::ios_base::seekdir seekAnchor);
26  virtual void setInPos(long pos, bool fromBeginning);
27  bool readable() { return mMode & std::ios_base::in; }
28  bool writeable() { return mMode & std::ios_base::out; }
29  bool binary() { return mFStream.flags() & std::ios_base::binary; }
30  bool readwrite() { return readable() && writeable(); }
31  long getOutPos();
32  long getInPos();
33  std::string const &getFileName() const { return mFileName; }
34 
35  protected:
36  FileStream() {}
37  void verifyFlags(const char *caller);
38  void openFile(char const *path, std::ios_base::openmode mode, bool verifyWrites);
39 
40  std::fstream mFStream;
41  std::string mFileName;
42 
43  private:
44  std::ios_base::openmode mMode;
45  bool mVerifyWrites = false;
46  int const mMaxAttempts = 5;
47 };
48 
49 } /* namespace PV */
50 
51 #endif