2015-03-01 20:42:39 +00:00
|
|
|
#ifndef MEMORYREADER_HPP
|
|
|
|
#define MEMORYREADER_HPP
|
2013-01-29 21:46:05 +00:00
|
|
|
|
|
|
|
#include <string>
|
2015-07-10 23:44:31 +00:00
|
|
|
#include <memory>
|
2014-02-22 08:25:29 +00:00
|
|
|
#include <functional>
|
2015-07-10 23:44:31 +00:00
|
|
|
#include "Athena/IStreamReader.hpp"
|
2013-01-26 20:19:24 +00:00
|
|
|
|
2014-04-20 09:14:15 +00:00
|
|
|
namespace Athena
|
2013-07-21 03:57:20 +00:00
|
|
|
{
|
|
|
|
namespace io
|
|
|
|
{
|
2015-07-08 02:53:39 +00:00
|
|
|
/*! \class MemoryReader
|
|
|
|
* \brief A Stream class for reading data from a memory position
|
2013-01-26 20:19:24 +00:00
|
|
|
*
|
|
|
|
* A Class for reading binary data from a file or memory stream,
|
|
|
|
* all work is done using a memory buffer, and not read directly from the disk
|
|
|
|
* this allows for fast, flexible code as well as the ability to quickly modify data
|
|
|
|
* \sa Stream
|
2013-01-29 21:46:05 +00:00
|
|
|
*/
|
2015-03-01 20:42:39 +00:00
|
|
|
class MemoryReader : public IStreamReader
|
2013-01-29 21:46:05 +00:00
|
|
|
{
|
2015-10-17 00:25:36 +00:00
|
|
|
protected:
|
|
|
|
MemoryReader() = default;
|
2013-01-26 20:19:24 +00:00
|
|
|
public:
|
2015-08-23 23:52:01 +00:00
|
|
|
virtual ~MemoryReader();
|
|
|
|
|
2015-07-08 06:03:08 +00:00
|
|
|
/*! \brief This constructor references an existing buffer to read from.
|
2013-01-26 20:19:24 +00:00
|
|
|
*
|
|
|
|
* \param data The existing buffer
|
|
|
|
* \param length The length of the existing buffer
|
2015-08-23 23:52:01 +00:00
|
|
|
* \param takeOwnership Memory will be freed with the reader if set
|
2013-01-26 20:19:24 +00:00
|
|
|
*/
|
2016-02-16 05:48:03 +00:00
|
|
|
MemoryReader(const void* data, atUint64 length, bool takeOwnership=false);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \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);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Returns the current position in the stream.
|
|
|
|
*
|
|
|
|
* \return Int64 The current position in the stream.
|
|
|
|
*/
|
2015-07-01 09:28:40 +00:00
|
|
|
inline atUint64 position() const
|
|
|
|
{return m_position;}
|
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.
|
|
|
|
*/
|
2015-07-01 09:28:40 +00:00
|
|
|
inline atUint64 length() const
|
|
|
|
{return m_length;}
|
|
|
|
|
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.
|
2015-08-23 23:52:01 +00:00
|
|
|
* \param takeOwnership Memory will be freed with the reader if set
|
2014-04-20 09:14:15 +00:00
|
|
|
* \throw IOException
|
|
|
|
*/
|
2015-08-23 23:52:01 +00:00
|
|
|
void setData(const atUint8* data, atUint64 length, bool takeOwnership=false);
|
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;
|
2013-01-26 20:19:24 +00:00
|
|
|
|
2015-07-08 06:03:08 +00:00
|
|
|
/*! \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
|
|
|
|
*/
|
2015-04-11 00:04:13 +00:00
|
|
|
atUint64 readUBytesToBuf(void* buf, atUint64 len);
|
2014-02-22 08:25:29 +00:00
|
|
|
|
2015-07-08 06:03:08 +00:00
|
|
|
protected:
|
2016-02-16 05:48:03 +00:00
|
|
|
const void* m_data = nullptr;
|
2015-10-17 00:25:36 +00:00
|
|
|
atUint64 m_length = 0;
|
|
|
|
atUint64 m_position = 0;
|
|
|
|
bool m_owns = false;
|
2015-07-08 06:03:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2016-02-16 05:48:03 +00:00
|
|
|
MemoryCopyReader(const void* data, atUint64 length);
|
2015-07-08 06:03:08 +00:00
|
|
|
|
|
|
|
/*! \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)
|
2015-10-17 00:25:36 +00:00
|
|
|
: m_filepath(filename)
|
2015-07-08 06:03:08 +00:00
|
|
|
{loadData();}
|
|
|
|
|
|
|
|
void setData(const atUint8* data, atUint64 length);
|
|
|
|
|
2013-01-29 21:46:05 +00:00
|
|
|
protected:
|
2014-02-22 08:25:29 +00:00
|
|
|
void loadData();
|
2015-07-08 06:03:08 +00:00
|
|
|
std::unique_ptr<atUint8[]> m_dataCopy;
|
2015-07-08 02:53:39 +00:00
|
|
|
std::string m_filepath; //!< Path to the target file
|
2013-01-29 21:46:05 +00:00
|
|
|
};
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
} // io
|
|
|
|
} // Athena
|
2013-01-29 21:46:05 +00:00
|
|
|
|
2015-03-01 20:42:39 +00:00
|
|
|
#ifndef MEMORYREADER_BASE
|
|
|
|
#define MEMORYREADER_BASE() \
|
2013-07-21 07:49:07 +00:00
|
|
|
private: \
|
2015-03-01 20:42:39 +00:00
|
|
|
typedef Athena::io::MemoryReader base
|
2013-07-28 01:42:11 +00:00
|
|
|
|
2015-03-01 20:42:39 +00:00
|
|
|
#endif // MEMORYREADER_BASE
|
2013-07-22 03:06:54 +00:00
|
|
|
|
2015-07-08 06:03:08 +00:00
|
|
|
#ifndef MEMORYCOPYREADER_BASE
|
|
|
|
#define MEMORYCOPYREADER_BASE() \
|
|
|
|
private: \
|
|
|
|
typedef Athena::io::MemoryCopyReader base
|
|
|
|
|
|
|
|
#endif // MEMORYCOPYREADER_BASE
|
|
|
|
|
2015-03-01 20:42:39 +00:00
|
|
|
#endif // MEMORYREADER_HPP
|