2015-03-01 20:42:39 +00:00
|
|
|
#ifndef MEMORYWRITER_HPP
|
|
|
|
#define MEMORYWRITER_HPP
|
2014-04-20 09:14:15 +00:00
|
|
|
|
2015-03-01 21:22:59 +00:00
|
|
|
#include "Athena/IStreamWriter.hpp"
|
2014-04-20 09:14:15 +00:00
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace Athena
|
|
|
|
{
|
|
|
|
namespace io
|
|
|
|
{
|
|
|
|
|
|
|
|
/*! \class BinaryWriter
|
|
|
|
* \brief A Stream class for writing binary data
|
|
|
|
*
|
|
|
|
* A Class for writing binary data to a file or memory stream,
|
|
|
|
* all work is done using a memory buffer, and not written directly to the disk
|
|
|
|
* this allows for fast, flexible code as well as the ability to quickly modify data
|
|
|
|
* \sa Stream
|
|
|
|
*/
|
2015-03-01 21:22:59 +00:00
|
|
|
class MemoryWriter : public IStreamWriter
|
2014-04-20 09:14:15 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/*! \brief This constructor takes an existing buffer to write to.
|
|
|
|
*
|
|
|
|
* \param data The existing buffer
|
|
|
|
* \param length The length of the existing buffer
|
|
|
|
*/
|
2015-05-19 03:24:56 +00:00
|
|
|
explicit MemoryWriter(atUint8* data = nullptr, atUint64 length = 0x10);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief This constructor creates an instance from a file on disk.
|
|
|
|
*
|
|
|
|
* \param filename The file to create the stream from
|
|
|
|
*/
|
2015-03-01 20:42:39 +00:00
|
|
|
MemoryWriter(const std::string& filename, std::function<void(int)> progressFun = nullptr);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
2015-03-01 20:42:39 +00:00
|
|
|
virtual ~MemoryWriter();
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Sets the Endianss of the stream
|
|
|
|
*
|
|
|
|
* \param endian The Endianess to set \sa Endian
|
|
|
|
*/
|
|
|
|
void setEndian(Endian endian);
|
|
|
|
|
|
|
|
/*! \brief Returns the current Endianness of the stream
|
|
|
|
*
|
|
|
|
* \return Endian The current Stream Endianess
|
|
|
|
*/
|
|
|
|
Endian endian() const;
|
|
|
|
|
|
|
|
/*! \brief Returns whether the stream is BigEndian
|
|
|
|
*
|
|
|
|
* \return bool True for BigEndian; False for LittleEndian
|
|
|
|
*/
|
|
|
|
bool isBigEndian() const;
|
|
|
|
|
|
|
|
/*! \brief Returns whether the stream is LittleEndian
|
|
|
|
*
|
|
|
|
* \return bool True for LittleEndian; False for BigEndian
|
|
|
|
*/
|
|
|
|
bool isLittleEndian()const;
|
|
|
|
|
|
|
|
/*! \brief Retuns whether or not the Stream is open.
|
|
|
|
*
|
|
|
|
* \return True if open; False otherwise.
|
|
|
|
*/
|
|
|
|
bool isOpen() const;
|
|
|
|
|
|
|
|
/*! \brief Sets the buffers position relative to the specified position.<br />
|
|
|
|
* It seeks relative to the current position by default.
|
|
|
|
* \param position where in the buffer to seek
|
|
|
|
* \param origin The Origin to seek \sa SeekOrigin
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
|
2015-05-19 03:24:56 +00:00
|
|
|
|
2015-04-11 00:04:13 +00:00
|
|
|
/*! \brief Sets the buffers position relative to the next 32-byte aligned position.<br />
|
|
|
|
*/
|
|
|
|
inline void seekAlign32() {seek(ROUND_UP_32(m_position), SeekOrigin::Begin);}
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*! \brief Returns whether or not the stream is at the end.
|
|
|
|
*
|
|
|
|
* \return bool True if at end; False otherwise.
|
|
|
|
*/
|
|
|
|
bool atEnd() const;
|
|
|
|
|
|
|
|
|
|
|
|
/*! \brief Returns the current position in the stream.
|
|
|
|
*
|
|
|
|
* \return Int64 The current position in the stream.
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
atUint64 position() const;
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Returns whether or not the stream is at the end.
|
|
|
|
*
|
|
|
|
* \return bool True if at end; False otherwise.
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
atUint64 length() const;
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Sets the buffer to the given one, deleting the current one.<br />
|
|
|
|
* <b>BEWARE:</b> As this deletes the current buffer it WILL cause a loss of data
|
|
|
|
* if that was not the intent.<br />
|
|
|
|
* Once you pass the data to setData <b>DO NOT</b> delete the buffer
|
|
|
|
* as Stream now owns the address, this is done to keep memory usage down.
|
|
|
|
* \param data The new buffer.
|
|
|
|
* \param length The length of the new buffer.
|
|
|
|
* \throw IOException
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
void setData(const atUint8* data, atUint64 length);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*! \brief Returns a copy of the current buffer.<br />
|
|
|
|
* Changes to the copy do not affect the buffer so it's perfectly safe to
|
|
|
|
* directly edit the buffer and use setData to set the new information.<br />
|
|
|
|
* However once you pass the data to setData <b>DO NOT</b> delete the buffer
|
|
|
|
* as Stream now owns the address, this is done to keep memory usage down.
|
|
|
|
* \return Uint8* The copy of the buffer.
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
atUint8* data() const;
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Sets the target file
|
|
|
|
*
|
|
|
|
* \param filepath The path to write to.
|
|
|
|
*/
|
|
|
|
void setFilepath(const std::string& filepath);
|
|
|
|
|
|
|
|
/*! \brief
|
|
|
|
* Returns the target file
|
|
|
|
*/
|
|
|
|
std::string filepath() const;
|
|
|
|
|
|
|
|
/*! \brief Saves the file to the specified file.
|
|
|
|
*
|
|
|
|
* \param filename If not empty, the filename to save to
|
|
|
|
*/
|
2015-05-19 03:24:56 +00:00
|
|
|
void save(const std::string& filename = "");
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Seeks to the specified bit within the current byte
|
|
|
|
* \param bit Bit to seek to, range is 0 - 7
|
|
|
|
*/
|
|
|
|
void seekBit(int bit);
|
|
|
|
|
|
|
|
/*! \brief Writes a bit at the current position and advances the position by one bit.
|
|
|
|
* \param val the value to write
|
|
|
|
* \throw IOException
|
|
|
|
*/
|
|
|
|
void writeBit(bool val);
|
|
|
|
|
|
|
|
/*! \brief Writes a byte at the current position and advances the position by one byte.
|
|
|
|
* \param byte The value to write
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
void writeUByte(atUint8 val);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Writes a byte at the current position and advances the position by one byte.
|
|
|
|
* \param byte The value to write
|
|
|
|
* \throw IOException
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
void writeByte(atInt8 val);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Writes the given buffer with the specified length, buffers can be bigger than the length
|
|
|
|
* however it's undefined behavior to try and write a buffer which is smaller than the given length.
|
|
|
|
*
|
|
|
|
* \param data The buffer to write
|
|
|
|
* \param length The amount to write
|
|
|
|
*/
|
2015-07-01 03:01:04 +00:00
|
|
|
void writeUBytes(const atUint8* data, atUint64 len);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Writes the given buffer with the specified length, buffers can be bigger than the length
|
|
|
|
* however it's undefined behavior to try and write a buffer which is smaller than the given length.
|
|
|
|
*
|
|
|
|
* \param data The buffer to write
|
|
|
|
* \param length The amount to write
|
|
|
|
*/
|
2015-07-01 03:01:04 +00:00
|
|
|
void writeBytes(const atInt8* data, atUint64 len);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Writes an Int16 to the buffer and advances the buffer.
|
|
|
|
* It also swaps the bytes depending on the platform and Stream settings.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param val The value to write to the buffer
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
void writeInt16(atInt16 val);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Writes an Uint16 to the buffer and advances the buffer.
|
|
|
|
* It also swaps the bytes depending on the platform and Stream settings
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param val The value to write to the buffer
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
void writeUint16(atUint16);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Writes an Int32 to the buffer and advances the buffer.
|
|
|
|
* It also swaps the bytes depending on the platform and Stream settings.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param val The value to write to the buffer
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
void writeInt32(atInt32);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Writes an Uint32 to the buffer and advances the buffer.
|
|
|
|
* It also swaps the bytes depending on the platform and Stream settings.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param val The value to write to the buffer
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
void writeUint32(atUint32);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Writes an Int64 to the buffer and advances the buffer.
|
|
|
|
* It also swaps the bytes depending on the platform and Stream settings.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param val The value to write to the buffer
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
void writeInt64(atInt64);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Writes an Uint64 to the buffer and advances the buffer.
|
|
|
|
* It also swaps the bytes depending on the platform and Stream settings.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param val The value to write to the buffer
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
void writeUint64(atUint64);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Writes an float to the buffer and advances the buffer.
|
|
|
|
* It also swaps the bytes depending on the platform and Stream settings.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param val The value to write to the buffer
|
|
|
|
*/
|
|
|
|
void writeFloat(float);
|
|
|
|
|
|
|
|
/*! \brief Writes an double to the buffer and advances the buffer.
|
|
|
|
* It also swaps the bytes depending on the platform and Stream settings.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param val The value to write to the buffer
|
|
|
|
*/
|
|
|
|
void writeDouble(double);
|
|
|
|
|
|
|
|
/*! \brief Writes an bool to the buffer and advances the buffer.
|
|
|
|
* It also swaps the bytes depending on the platform and Stream settings.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param val The value to write to the buffer
|
|
|
|
*/
|
|
|
|
void writeBool(bool);
|
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
/*! \brief Writes an atVec3f (12 bytes) to the buffer and advances the buffer.
|
|
|
|
* It also swaps the bytes depending on the platform and Stream settings.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param vec The value to write to the buffer
|
|
|
|
*/
|
|
|
|
void writeVec3f(atVec3f vec);
|
|
|
|
|
|
|
|
/*! \brief Writes an atVec4f (16 bytes) to the buffer and advances the buffer.
|
|
|
|
* It also swaps the bytes depending on the platform and Stream settings.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param vec The value to write to the buffer
|
|
|
|
*/
|
|
|
|
void writeVec4f(atVec4f vec);
|
|
|
|
|
2014-04-20 09:14:15 +00:00
|
|
|
/*! \brief Writes an unicode string to the buffer and advances the buffer.
|
|
|
|
* It also swaps the bytes depending on the platform and Stream settings.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param str The string to write to the buffer
|
2015-06-19 20:12:25 +00:00
|
|
|
* \param fixedLen If not -1, the number of characters to zero-fill string to
|
2014-04-20 09:14:15 +00:00
|
|
|
*/
|
2015-06-19 20:12:25 +00:00
|
|
|
void writeUnicode(const std::string& str, atInt32 fixedLen = -1);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Writes an string to the buffer and advances the buffer.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param str The string to write to the buffer
|
2015-06-19 20:12:25 +00:00
|
|
|
* \param fixedLen If not -1, the number of characters to zero-fill string to
|
2014-04-20 09:14:15 +00:00
|
|
|
*/
|
2015-06-19 20:12:25 +00:00
|
|
|
void writeString(const std::string& str, atInt32 fixedLen = -1);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
2015-06-19 02:55:05 +00:00
|
|
|
/*! \brief Writes an wstring to the buffer and advances the buffer.
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param str The string to write to the buffer
|
2015-06-19 20:12:25 +00:00
|
|
|
* \param fixedLen If not -1, the number of characters to zero-fill string to
|
2015-06-19 02:55:05 +00:00
|
|
|
*/
|
2015-06-19 20:12:25 +00:00
|
|
|
void writeWString(const std::wstring& str, atInt32 fixedLen = -1);
|
2015-06-19 02:55:05 +00:00
|
|
|
|
|
|
|
|
2014-06-18 04:51:18 +00:00
|
|
|
void fill(atUint8 val, atUint64 length);
|
|
|
|
void fill(atInt8 val, atUint64 length);
|
2014-05-15 07:41:54 +00:00
|
|
|
|
2014-04-20 09:14:15 +00:00
|
|
|
void setProgressCallback(std::function<void(int)> cb);
|
|
|
|
protected:
|
|
|
|
void loadData();
|
2014-06-18 04:51:18 +00:00
|
|
|
atUint8* m_data;
|
|
|
|
atUint64 m_length;
|
2014-04-20 09:14:15 +00:00
|
|
|
std::string m_filepath; //!< Path to the target file
|
2014-06-18 04:51:18 +00:00
|
|
|
atUint64 m_position;
|
|
|
|
atUint64 m_bitPosition;
|
2014-04-20 09:14:15 +00:00
|
|
|
Endian m_endian;
|
|
|
|
std::function<void(int)> m_progressCallback;
|
|
|
|
private:
|
2014-06-18 04:51:18 +00:00
|
|
|
void resize(atUint64 newSize);
|
2014-04-20 09:14:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-01 20:42:39 +00:00
|
|
|
#ifndef MEMORYWRITER_BASE
|
|
|
|
#define MEMORYWRITER_BASE() \
|
2014-04-20 09:14:15 +00:00
|
|
|
private: \
|
2015-03-01 20:42:39 +00:00
|
|
|
typedef Athena::io::MemoryWriter base
|
2014-04-20 09:14:15 +00:00
|
|
|
#endif // BINARYWRITER_BASE
|
2015-03-01 20:42:39 +00:00
|
|
|
#endif // MEMORYWRITER_HPP
|