Athena IO Library
FileReader.hpp
1 // This file is part of libAthena.
2 //
3 // libAthena is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // libAthena is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with libAthena. If not, see <http://www.gnu.org/licenses/>
15 
16 #ifndef FILESTREAM_HPP
17 #define FILESTREAM_HPP
18 
19 #include "Athena/IStreamReader.hpp"
20 #include <string>
21 #include <stdio.h>
22 
23 namespace Athena
24 {
25 namespace io
26 {
27 class FileReader : public IStreamReader
28 {
29 public:
30  FileReader(const std::string& filename);
31  virtual ~FileReader();
32  std::string filename() const;
33 
34  void setEndian(Endian endian);
35  Endian endian() const;
36  bool isBigEndian() const;
37  bool isLittleEndian() const;
38  void open();
39  void close();
40  bool isOpen() const;
41  bool save();
42  void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
43  inline void seekAlign32() {seek(ROUND_UP_32(position()), SeekOrigin::Begin);}
44  bool atEnd() const;
45  atUint64 position() const;
46  atUint64 length() const;
47 
48 
49  void seekBit(int);
50  bool readBit();
51  atUint8 readUByte();
52  atInt8 readByte();
53  atUint8* readUBytes(atUint64 len);
54  atInt8* readBytes(atUint64 len);
55  atUint64 readBytesToBuf(void* buf, atUint64 len) {return readUBytesToBuf(buf, len);}
56  atUint64 readUBytesToBuf(void* buf, atUint64 len);
57  atUint16 readUint16();
58  atInt16 readInt16();
59  atUint32 readUint32();
60  atInt32 readInt32();
61  atUint64 readUint64();
62  atInt64 readInt64();
63  double readDouble();
64  float readFloat();
65  bool readBool();
66  std::string readString(atInt32 maxlen = -1);
67  std::string readUnicode(atInt32 maxlen = -1);
68 protected:
69  std::string m_filename;
70  FILE* m_fileHandle;
71  Endian m_endian;
72  atUint8 m_currentByte;
73  atUint8 m_bitShift;
74  bool m_bitValid;
75 };
76 } // io
77 } // Athena
78 
79 #ifndef FILEREADER_BASE
80 #define FILEREADER_BASE() \
81 private: \
82  typedef Athena::io::FileReader base
83 
84 #endif // FILEREADER_BASE
85 
86 #endif // FILESTREAM_HPP