Athena IO Library
MemoryWriter.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 MEMORYWRITER_HPP
17 #define MEMORYWRITER_HPP
18 
19 #include "Athena/IStreamWriter.hpp"
20 #include <string>
21 #include <functional>
22 
23 namespace Athena
24 {
25 namespace io
26 {
27 
37 {
38 public:
44  explicit MemoryWriter(atUint8* data = nullptr, atUint64 length = 0x10);
45 
50  MemoryWriter(const std::string& filename, std::function<void(int)> progressFun = nullptr);
51 
52  virtual ~MemoryWriter();
53 
58  void setEndian(Endian endian);
59 
64  Endian endian() const;
65 
70  bool isBigEndian() const;
71 
76  bool isLittleEndian()const;
77 
82  bool isOpen() const;
83 
89  void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
90 
93  inline void seekAlign32() {seek(ROUND_UP_32(m_position), SeekOrigin::Begin);}
94 
95 
100  bool atEnd() const;
101 
102 
107  atUint64 position() const;
108 
113  atUint64 length() const;
114 
124  void setData(const atUint8* data, atUint64 length);
125 
126 
134  atUint8* data() const;
135 
140  void setFilepath(const std::string& filepath);
141 
145  std::string filepath() const;
146 
151  void save(const std::string& filename = "");
152 
157  void seekBit(int bit);
158 
163  void writeBit(bool val);
164 
168  void writeUByte(atUint8 val);
169 
174  void writeByte(atInt8 val);
175 
182  void writeUBytes(atUint8* data, atUint64 len);
183 
190  void writeBytes(atInt8* data, atUint64 len);
191 
198  void writeInt16(atInt16 val);
199 
206  void writeUint16(atUint16);
207 
214  void writeInt32(atInt32);
215 
222  void writeUint32(atUint32);
223 
230  void writeInt64(atInt64);
231 
238  void writeUint64(atUint64);
239 
246  void writeFloat(float);
247 
254  void writeDouble(double);
255 
262  void writeBool(bool);
263 
270  void writeUnicode(const std::string& str);
271 
277  void writeString(const std::string& str);
278 
279  void fill(atUint8 val, atUint64 length);
280  void fill(atInt8 val, atUint64 length);
281 
282  void setProgressCallback(std::function<void(int)> cb);
283 protected:
284  void loadData();
285  atUint8* m_data;
286  atUint64 m_length;
287  std::string m_filepath;
288  atUint64 m_position;
289  atUint64 m_bitPosition;
290  Endian m_endian;
291  std::function<void(int)> m_progressCallback;
292 private:
293  void resize(atUint64 newSize);
294 };
295 }
296 }
297 
298 #ifndef MEMORYWRITER_BASE
299 #define MEMORYWRITER_BASE() \
300  private: \
301  typedef Athena::io::MemoryWriter base
302 #endif // BINARYWRITER_BASE
303 #endif // MEMORYWRITER_HPP
void writeBool(bool)
Writes an bool to the buffer and advances the buffer. It also swaps the bytes depending on the platfo...
void setEndian(Endian endian)
Sets the Endianss of the stream.
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...
bool atEnd() const
Returns whether or not the stream is at the end.
Endian endian() const
Returns the current Endianness of the stream.
std::string filepath() const
Returns the target file.
void save(const std::string &filename="")
Saves the file to the specified file.
bool isBigEndian() const
Returns whether the stream is BigEndian.
void writeUint64(atUint64)
Writes an Uint64 to the buffer and advances the buffer. It also swaps the bytes depending on the plat...
void seekAlign32()
Sets the buffers position relative to the next 32-byte aligned position.
void setFilepath(const std::string &filepath)
Sets the target file.
void writeUnicode(const std::string &str)
Writes an unicode string to the buffer and advances the buffer. It also swaps the bytes depending on ...
void writeInt32(atInt32)
Writes an Int32 to the buffer and advances the buffer. It also swaps the bytes depending on the platf...
bool isLittleEndian() const
Returns whether the stream is LittleEndian.
void seek(atInt64 pos, SeekOrigin origin=SeekOrigin::Current)
Sets the buffers position relative to the specified position. It seeks relative to the current posit...
void writeBytes(atInt8 *data, atUint64 len)
Writes the given buffer with the specified length, buffers can be bigger than the length however it's...
void writeUByte(atUint8 val)
Writes a byte at the current position and advances the position by one byte.
atUint64 length() const
Returns whether or not the stream is at the end.
void writeUBytes(atUint8 *data, atUint64 len)
Writes the given buffer with the specified length, buffers can be bigger than the length however it's...
void writeInt16(atInt16 val)
Writes an Int16 to the buffer and advances the buffer. It also swaps the bytes depending on the platf...
void writeFloat(float)
Writes an float to the buffer and advances the buffer. It also swaps the bytes depending on the platf...
void writeInt64(atInt64)
Writes an Int64 to the buffer and advances the buffer. It also swaps the bytes depending on the platf...
void writeUint32(atUint32)
Writes an Uint32 to the buffer and advances the buffer. It also swaps the bytes depending on the plat...
bool isOpen() const
Retuns whether or not the Stream is open.
atUint64 position() const
Returns the current position in the stream.
void writeBit(bool val)
Writes a bit at the current position and advances the position by one bit.
atUint8 * data() const
Returns a copy of the current buffer. Changes to the copy do not affect the buffer so it's perfectly...
void writeUint16(atUint16)
Writes an Uint16 to the buffer and advances the buffer. It also swaps the bytes depending on the plat...
void writeString(const std::string &str)
Writes an string to the buffer and advances the buffer.
void seekBit(int bit)
Seeks to the specified bit within the current byte.
void writeDouble(double)
Writes an double to the buffer and advances the buffer. It also swaps the bytes depending on the plat...
MemoryWriter(atUint8 *data=nullptr, atUint64 length=0x10)
This constructor takes an existing buffer to write to.
void writeByte(atInt8 val)
Writes a byte at the current position and advances the position by one byte.
std::string m_filepath
Path to the target file.