Athena IO Library
MemoryReader.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 MEMORYREADER_HPP
17 #define MEMORYREADER_HPP
18 
19 #include "Athena/IStreamReader.hpp"
20 #include <string>
21 #include <functional>
22 
23 namespace Athena
24 {
25 namespace io
26 {
36 {
37 public:
43  MemoryReader(const atUint8* data, atUint64 length);
44 
49  MemoryReader(const std::string& filename, std::function<void(int)> progressFun = nullptr);
50 
51  virtual ~MemoryReader();
52 
57  void setEndian(Endian endian);
58 
63  Endian endian() const;
64 
69  bool isBigEndian() const;
70 
75  bool isLittleEndian()const;
76 
81  bool isOpen() const;
82 
88  void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
89 
92  inline void seekAlign32() {seek(ROUND_UP_32(m_position), SeekOrigin::Begin);}
93 
98  bool atEnd() const;
99 
104  atUint64 position() const;
105 
110  atUint64 length() const;
111 
121  void setData(const atUint8* data, atUint64 length);
122 
123 
131  atUint8* data() const;
132 
138  void setFilepath(const std::string& filepath);
139 
143  std::string filepath() const;
144 
149  void seekBit(int bit);
150 
155  bool readBit();
156 
161  atInt8 readByte();
162 
167  atUint8 readUByte();
168 
173  inline atInt8* readBytes(atUint64 length) {return (atInt8*)readUBytes(length);}
174 
179  atUint8* readUBytes(atUint64 length);
180 
181  atUint64 readBytesToBuf(void* buf, atUint64 len) {return readUBytesToBuf(buf, len);}
182  atUint64 readUBytesToBuf(void* buf, atUint64 len);
183 
192  atInt16 readInt16();
193 
202  atUint16 readUint16();
203 
212  atInt32 readInt32();
213 
222  atUint32 readUint32();
223 
232  atInt64 readInt64();
233 
242  atUint64 readUint64();
243 
252  float readFloat();
253 
262  double readDouble();
263 
269  bool readBool();
270 
276  std::string readUnicode(atInt32 maxlen = -1);
277 
283  std::string readString(atInt32 maxlen = -1);
284 
285  void setProgressCallback(std::function<void(int)> cb);
286 protected:
287  void loadData();
288  atUint8* m_data;
289  atUint64 m_length;
290  std::string m_filepath;
291  atUint64 m_position;
292  atUint64 m_bitPosition;
293  Endian m_endian;
294  std::function<void(int)> m_progressCallback;
295 };
296 
297 } // io
298 } // Athena
299 
300 #ifndef MEMORYREADER_BASE
301 #define MEMORYREADER_BASE() \
302 private: \
303  typedef Athena::io::MemoryReader base
304 
305 #endif // MEMORYREADER_BASE
306 
307 #endif // MEMORYREADER_HPP
bool atEnd() const
Returns whether or not the stream is at the end.
atUint64 position() const
Returns the current position in the stream.
void setEndian(Endian endian)
Sets the Endianss of the stream.
void seek(atInt64 pos, SeekOrigin origin=SeekOrigin::Current)
Sets the buffers position relative to the specified position. It seeks relative to the current posit...
bool isLittleEndian() const
Returns whether the stream is LittleEndian.
atUint16 readUint16()
Reads a Uint16 and swaps to proper endianness depending on platform and Stream settings, and advances the current position.
atInt8 readByte()
Reads a byte at the current position and advances the current position.
void seekAlign32()
Sets the buffers position relative to the next 32-byte aligned position.
atUint8 * readUBytes(atUint64 length)
Reads a byte at the current position and advances the current position.
atUint64 length() const
Returns whether or not the stream is at the end.
std::string readString(atInt32 maxlen=-1)
Reads a string and advances the position in the file.
atUint64 readUint64()
Reads a Uint64 and swaps to proper endianness depending on platform and Stream settings, and advances the current position.
std::string m_filepath
Path to the target file.
void seekBit(int bit)
Seeks to the specified bit within the current byte.
MemoryReader(const atUint8 *data, atUint64 length)
This constructor takes an existing buffer to read from.
double readDouble()
Reads a double and swaps to proper endianness depending on platform and Stream settings, and advances the current position.
atUint32 readUint32()
Reads a Uint32 and swaps to proper endianness depending on platform and Stream settings, and advances the current position.
bool isOpen() const
Retuns whether or not the Stream is open.
atInt64 readInt64()
Reads a Int64 and swaps to proper endianness depending on platform and Stream settings, and advances the current position.
atUint8 * data() const
Returns a copy of the current buffer. Changes to the copy do not affect the buffer so it's perfectly...
float readFloat()
Reads a float and swaps to proper endianness depending on platform and Stream settings, and advances the current position.
bool readBit()
Reads a bit at the current position and advances the current position.
atUint8 readUByte()
Reads a byte at the current position and advances the current position.
void setFilepath(const std::string &filepath)
Sets the target file.
atInt8 * readBytes(atUint64 length)
Reads a byte at the current position and advances the current position.
atInt16 readInt16()
Reads a Int16 and swaps to proper endianness depending on platform and Stream settings, and advances the current position.
bool isBigEndian() const
Returns whether the stream is BigEndian.
bool readBool()
Reads a bool and advances the current position.
Endian endian() const
Returns the current Endianness of the stream.
atInt32 readInt32()
Reads a Int32 and swaps to proper endianness depending on platform and Stream settings, and advances the current position.
std::string filepath() const
Returns the target file.
std::string readUnicode(atInt32 maxlen=-1)
Reads a Unicode string and advances the position in the file.
void setData(const atUint8 *data, atUint64 length)
Sets the buffer to the given one, deleting the current one. BEWARE: As this deletes the current buff...