CARMA C++
FileHandler.h
Go to the documentation of this file.
1 // $Id: FileHandler.h,v 1.1 2010/12/13 21:06:30 eml Exp $
2 
3 #ifndef SZA_UTIL_FILEHANDLER_H
4 #define SZA_UTIL_FILEHANDLER_H
5 
16 #include <iostream>
17 #include <unistd.h>
18 #include <vector>
19 
20 namespace sza {
21  namespace util {
22 
23  class FileHandler {
24  public:
25 
29  FileHandler();
30  FileHandler(std::string path);
31 
35  FileHandler(const FileHandler& objToBeCopied);
36 
40  FileHandler(FileHandler& objToBeCopied);
41 
45  void operator=(const FileHandler& objToBeAssigned);
46 
50  void operator=(FileHandler& objToBeAssigned);
51 
55  friend std::ostream& operator<<(std::ostream& os, FileHandler& obj);
56 
60  virtual ~FileHandler();
61 
62  void setTo(std::string path);
63  virtual void openForRead(bool memMap=false);
64  void close();
65  void advanceByNbytes(off_t bytes);
66  void setToBeginning();
67  off_t setToEnd();
68  unsigned getFileSizeInBytes();
69 
70  int getFd();
71 
72  off_t getCurrentOffset();
73  void read(void* buf, size_t nByte);
74 
75  void memoryMap();
76  void loadFile();
77 
78  protected:
79 
80  std::string path_;
81  bool pathIsSet_;
82 
83  int fd_;
84 
85  off_t currentOffset_;
86  size_t sizeInBytes_;
87 
88  //------------------------------------------------------------
89  // If the file is memory-mapped, memMap_ will be set to true,
90  // and mptrHead_ will point to the head of the memory mapped space
91  //------------------------------------------------------------
92 
93  bool memMap_;
94  unsigned char* mptrHead_;
95 
96  bool loadFile_;
97  std::vector<unsigned char> fbuf_;
98 
99  void checkFd();
100 
101  }; // End class FileHandler
102 
103  } // End namespace util
104 } // End namespace sza
105 
106 
107 
108 #endif // End #ifndef SZA_UTIL_FILEHANDLER_H