added buffer-copying subclass to MemoryReader/Writer

This commit is contained in:
Jack Andersen
2015-07-07 20:03:08 -10:00
parent 8a397a1622
commit ec38ba088e
19 changed files with 159 additions and 94 deletions

View File

@@ -35,9 +35,9 @@ namespace io
* all work is done using a memory buffer, and not read directly from the disk.
* \sa BinaryReader
*/
class ALTTPFileReader : protected MemoryReader
class ALTTPFileReader : protected MemoryCopyReader
{
MEMORYREADER_BASE();
MEMORYCOPYREADER_BASE();
public:
/*! \brief This constructor takes an existing buffer to read from.

View File

@@ -34,9 +34,9 @@ namespace io
* all work is done using a memory buffer, and not written directly to the disk.
* \sa BinaryReader
*/
class ALTTPFileWriter : protected MemoryWriter
class ALTTPFileWriter : protected MemoryCopyWriter
{
MEMORYWRITER_BASE();
MEMORYCOPYWRITER_BASE();
public:
/*! \brief This constructor takes an existing buffer to write to.

View File

@@ -34,9 +34,9 @@ namespace io
* all work is done using a memory buffer, and not read directly from the disk.
* \sa BinaryReader
*/
class MCFileReader : public MemoryReader
class MCFileReader : public MemoryCopyReader
{
MEMORYREADER_BASE();
MEMORYCOPYREADER_BASE();
public:
/*!
* \brief This constructor takes an existing buffer to read from.

View File

@@ -35,9 +35,9 @@ namespace io
* all work is done using a memory buffer, and not written directly from the disk.
* \sa BinaryWriter
*/
class MCFileWriter : protected MemoryWriter
class MCFileWriter : protected MemoryCopyWriter
{
MEMORYWRITER_BASE();
MEMORYCOPYWRITER_BASE();
public:
/*!
* \brief This constructor takes an existing buffer to write to.

View File

@@ -20,21 +20,13 @@ namespace io
class MemoryReader : public IStreamReader
{
public:
~MemoryReader();
/*! \brief This constructor takes an existing buffer to read from.
/*! \brief This constructor references an existing buffer to read from.
*
* \param data The existing buffer
* \param length The length of the existing buffer
*/
MemoryReader(const atUint8* data, atUint64 length);
/*! \brief This constructor creates an instance from a file on disk.
*
* \param filename The file to create the stream from
*/
MemoryReader(const std::string& filename);
/*! \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
@@ -78,14 +70,44 @@ public:
*/
atUint8* data() const;
/*! \brief Reads a specified number of bytes to user-allocated buffer
* \param buf User-allocated buffer pointer
* \param len Length to read
* \return Number of bytes read
*/
atUint64 readUBytesToBuf(void* buf, atUint64 len);
protected:
const atUint8* m_data;
atUint64 m_length;
atUint64 m_position;
};
class MemoryCopyReader : public MemoryReader
{
public:
/*! \brief This constructor copies an existing buffer to read from.
*
* \param data The existing buffer
* \param length The length of the existing buffer
*/
MemoryCopyReader(const atUint8* data, atUint64 length);
/*! \brief This constructor creates an instance from a file on disk.
*
* \param filename The file to create the stream from
*/
MemoryCopyReader(const std::string& filename)
: MemoryReader(NULL, 0),
m_filepath(filename)
{loadData();}
void setData(const atUint8* data, atUint64 length);
protected:
void loadData();
atUint8* m_data;
atUint64 m_length;
std::unique_ptr<atUint8[]> m_dataCopy;
std::string m_filepath; //!< Path to the target file
atUint64 m_position;
};
} // io
@@ -98,4 +120,11 @@ private: \
#endif // MEMORYREADER_BASE
#ifndef MEMORYCOPYREADER_BASE
#define MEMORYCOPYREADER_BASE() \
private: \
typedef Athena::io::MemoryCopyReader base
#endif // MEMORYCOPYREADER_BASE
#endif // MEMORYREADER_HPP

View File

@@ -21,22 +21,14 @@ namespace io
class MemoryWriter : public IStreamWriter
{
public:
~MemoryWriter();
/*! \brief This constructor takes an existing buffer to write to.
/*! \brief This constructor references an existing buffer to write to in-place.
*
* \param data The existing buffer
* \param length The length of the existing buffer
*/
explicit MemoryWriter(atUint8* data = nullptr, atUint64 length = 0x10);
/*! \brief This constructor creates an instance from a file on disk.
*
* \param filename The file to create the stream from
*/
MemoryWriter(const std::string& filename, std::function<void(int)> progressFun = nullptr);
/*! \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
@@ -70,7 +62,7 @@ public:
* \param length The length of the new buffer.
* \throw IOException
*/
void setData(const atUint8* data, atUint64 length);
void setData(atUint8* data, atUint64 length);
/*! \brief Returns a copy of the current buffer.<br />
@@ -113,11 +105,35 @@ public:
protected:
atUint8* m_data;
atUint64 m_length;
std::string m_filepath; //!< Path to the target file
atUint64 m_position;
std::string m_filepath; //!< Path to the target file
private:
void resize(atUint64 newSize);
};
class MemoryCopyWriter : public MemoryWriter
{
public:
/*! \brief This constructor copies an existing buffer to write to.
*
* \param data The existing buffer
* \param length The length of the existing buffer
*/
explicit MemoryCopyWriter(atUint8* data, atUint64 length);
/*! \brief This constructor creates an instance from a file on disk.
*
* \param filename The file to create the stream from
*/
MemoryCopyWriter(const std::string& filename);
void setData(const atUint8* data, atUint64 length);
protected:
std::unique_ptr<atUint8[]> m_dataCopy;
};
}
}
@@ -126,4 +142,11 @@ private:
private: \
typedef Athena::io::MemoryWriter base
#endif // BINARYWRITER_BASE
#ifndef MEMORYCOPYWRITER_BASE
#define MEMORYCOPYWRITER_BASE() \
private: \
typedef Athena::io::MemoryCopyWriter base
#endif // BINARYWRITER_BASE
#endif // MEMORYWRITER_HPP

View File

@@ -24,9 +24,9 @@ namespace Athena
class SkywardSwordFile;
namespace io
{
class SkywardSwordFileReader : public MemoryReader
class SkywardSwordFileReader : public MemoryCopyReader
{
MEMORYREADER_BASE();
MEMORYCOPYREADER_BASE();
public:
SkywardSwordFileReader(atUint8* data, atUint64 length);

View File

@@ -26,9 +26,9 @@ class SkywardSwordFile;
namespace io
{
class SkywardSwordFileWriter : public MemoryWriter
class SkywardSwordFileWriter : public MemoryCopyWriter
{
MEMORYWRITER_BASE();
MEMORYCOPYWRITER_BASE();
public:
SkywardSwordFileWriter(atUint8* data, atUint64 len);
SkywardSwordFileWriter(const std::string& filename);

View File

@@ -29,9 +29,9 @@ class SpriteFile;
namespace io
{
class SpriteFileReader : public MemoryReader
class SpriteFileReader : public MemoryCopyReader
{
MEMORYREADER_BASE();
MEMORYCOPYREADER_BASE();
public:
SpriteFileReader(atUint8* data, atUint64 length);
SpriteFileReader(const std::string& filepath);

View File

@@ -29,9 +29,9 @@ class SpriteFile;
namespace io
{
class SpriteFileWriter : public MemoryWriter
class SpriteFileWriter : public MemoryCopyWriter
{
MEMORYWRITER_BASE();
MEMORYCOPYWRITER_BASE();
public:
SpriteFileWriter(atUint8* data, atUint64 length);

View File

@@ -37,9 +37,9 @@ namespace io
* all work is done using a memory buffer, and not read directly from the disk.
* \sa BinaryReader
*/
class WiiSaveReader : protected MemoryReader
class WiiSaveReader : protected MemoryCopyReader
{
MEMORYREADER_BASE();
MEMORYCOPYREADER_BASE();
public:
/*! \brief This constructor takes an existing buffer to read from.
*

View File

@@ -36,9 +36,9 @@ namespace io
* all work is done using a memory buffer, and not written directly to the disk.
* \sa BinaryReader
*/
class WiiSaveWriter : protected MemoryWriter
class WiiSaveWriter : protected MemoryCopyWriter
{
MEMORYWRITER_BASE();
MEMORYCOPYWRITER_BASE();
public:
/*! \brief This constructor creates an instance from a file on disk.
*

View File

@@ -29,9 +29,9 @@ namespace io
/*!
* \brief The ZQuestFileReader class
*/
class ZQuestFileReader : protected MemoryReader
class ZQuestFileReader : protected MemoryCopyReader
{
MEMORYREADER_BASE();
MEMORYCOPYREADER_BASE();
public:
/*!

View File

@@ -29,9 +29,9 @@ namespace io
/*!
* \brief The ZQuestFileWriter class
*/
class ZQuestFileWriter : protected MemoryWriter
class ZQuestFileWriter : protected MemoryCopyWriter
{
MEMORYWRITER_BASE();
MEMORYCOPYWRITER_BASE();
public:
/*!