Athena IO Library
FileReader.hpp
1 #ifndef FILESTREAM_HPP
2 #define FILESTREAM_HPP
3 
4 #include <string>
5 #include <memory>
6 #include <stdio.h>
7 #include "athena/IStreamReader.hpp"
8 
9 namespace athena
10 {
11 namespace io
12 {
13 class FileReader : public IStreamReader
14 {
15 public:
16  FileReader(const std::string& filename, atInt32 cacheSize = (32 * 1024), bool globalErr=true);
17  FileReader(const std::wstring& filename, atInt32 cacheSize = (32 * 1024), bool globalErr=true);
18  virtual ~FileReader();
19 
20  inline std::string filename() const
21  {
22 #if _WIN32
23  return utility::wideToUtf8(m_filename);
24 #else
25  return m_filename;
26 #endif
27  }
28 
29  inline std::wstring wfilename() const
30  {
31 #if _WIN32
32  return m_filename;
33 #else
34  return utility::utf8ToWide(m_filename);
35 #endif
36  }
37 
38  void open();
39  void close();
40  inline bool isOpen() const
41  {return m_fileHandle != NULL;}
42  bool save();
43  void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
44  atUint64 position() const;
45  atUint64 length() const;
46  atUint64 readUBytesToBuf(void* buf, atUint64 len);
47 
48  void setCacheSize(const atInt32 blockSize);
49  FILE* _fileHandle() {return m_fileHandle;}
50 protected:
51 #if _WIN32
52  std::wstring m_filename;
53 #else
54  std::string m_filename;
55 #endif
56  FILE* m_fileHandle;
57  std::unique_ptr<atUint8[]> m_cacheData;
58  atInt32 m_blockSize;
59  atInt32 m_curBlock;
60  atUint64 m_offset;
61  bool m_globalErr;
62 };
63 } // io
64 } // Athena
65 
66 #ifndef FILEREADER_BASE
67 #define FILEREADER_BASE() \
68 private: \
69  typedef athena::io::FileReader base
70 
71 #endif // FILEREADER_BASE
72 
73 #endif // FILESTREAM_HPP
atUint64 position() const
Returns the current position in the stream.
atUint64 length() const
Returns the length of the file.
The IStreamReader class defines a basic API for reading from streams, Implementors are provided with ...
void seek(atInt64 pos, SeekOrigin origin=SeekOrigin::Current)
Sets the buffers position relative to the specified position. It seeks relative to the current posit...
atUint64 readUBytesToBuf(void *buf, atUint64 len)
Attempts to read a fixed length of data into a pre-allocated buffer, this function is client defined ...