2014-04-20 10:28:28 +00:00
|
|
|
// This file is part of libAthena.
|
2013-01-27 01:19:49 +00:00
|
|
|
//
|
2014-04-20 10:28:28 +00:00
|
|
|
// libAthena is free software: you can redistribute it and/or modify
|
2013-01-27 01:19:49 +00:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2014-04-20 10:28:28 +00:00
|
|
|
// libAthena is distributed in the hope that it will be useful,
|
2013-01-27 01:19:49 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2014-04-20 10:28:28 +00:00
|
|
|
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
|
2013-07-21 03:57:20 +00:00
|
|
|
|
2013-01-29 21:46:05 +00:00
|
|
|
#ifndef __BINARYREADER_HPP__
|
|
|
|
#define __BINARYREADER_HPP__
|
|
|
|
|
2014-09-29 03:33:36 +00:00
|
|
|
#include "Athena/Stream.hpp"
|
2013-01-29 21:46:05 +00:00
|
|
|
#include <string>
|
2014-02-22 08:25:29 +00:00
|
|
|
#include <functional>
|
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
|
|
|
|
{
|
2013-01-26 20:19:24 +00:00
|
|
|
/*! \class BinaryReader
|
2013-01-27 20:13:09 +00:00
|
|
|
* \brief A Stream class for reading binary data
|
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
|
|
|
*/
|
|
|
|
class BinaryReader : public Stream
|
|
|
|
{
|
2013-01-26 20:19:24 +00:00
|
|
|
public:
|
|
|
|
/*! \brief This constructor takes an existing buffer to read from.
|
|
|
|
*
|
|
|
|
* \param data The existing buffer
|
|
|
|
* \param length The length of the existing buffer
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
BinaryReader(const atUint8* data, atUint64 length);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
2013-01-26 20:19:24 +00:00
|
|
|
/*! \brief This constructor creates an instance from a file on disk.
|
|
|
|
*
|
|
|
|
* \param filename The file to create the stream from
|
2013-01-29 21:46:05 +00:00
|
|
|
*/
|
2014-04-20 09:14:15 +00:00
|
|
|
BinaryReader(const std::string& filename, std::function<void(int)> progressFun = nullptr);
|
|
|
|
|
2015-01-29 06:31:14 +00:00
|
|
|
virtual ~BinaryReader();
|
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);
|
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;
|
2013-01-26 20:19:24 +00:00
|
|
|
|
2013-09-09 03:36:54 +00:00
|
|
|
/*! \brief Sets the target file
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
* \param filepath The path to write to.
|
|
|
|
*/
|
|
|
|
void setFilepath(const std::string& filepath);
|
|
|
|
|
|
|
|
/*! \brief Returns the target file
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
std::string filepath() const;
|
2013-01-26 20:19:24 +00:00
|
|
|
|
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 Reads a bit at the current position and advances the current position
|
|
|
|
*
|
|
|
|
* \return bool The value at the current position
|
|
|
|
*/
|
2014-02-22 08:25:29 +00:00
|
|
|
bool readBit();
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Reads a byte at the current position and advances the current position
|
|
|
|
*
|
|
|
|
* \return Int8 The value at the current position
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
atInt8 readByte();
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Reads a byte at the current position and advances the current position
|
|
|
|
*
|
|
|
|
* \return Uint8 The value at the current position
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
atUint8 readUByte();
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Reads a byte at the current position and advances the current position.
|
|
|
|
*
|
|
|
|
* \return Uint8* The buffer at the current position from the given length.
|
|
|
|
*/
|
2015-01-25 04:29:08 +00:00
|
|
|
atInt8* readBytes(atUint64 length);
|
2014-04-20 09:14:15 +00:00
|
|
|
|
|
|
|
/*! \brief Reads a byte at the current position and advances the current position.
|
|
|
|
*
|
|
|
|
* \return Int8* The buffer at the current position from the given length.
|
|
|
|
*/
|
2015-01-25 04:29:08 +00:00
|
|
|
atUint8* readUBytes(atUint64 length);
|
2014-02-22 08:25:29 +00:00
|
|
|
|
2013-01-26 20:19:24 +00:00
|
|
|
/*! \brief Reads a Int16 and swaps to proper endianness depending on platform
|
|
|
|
* and Stream settings, and advances the current position
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
*
|
|
|
|
* \return Int16 The value at the current address
|
|
|
|
* \throw IOException when address is out of range
|
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
atInt16 readInt16();
|
2013-01-26 20:19:24 +00:00
|
|
|
|
|
|
|
/*! \brief Reads a Uint16 and swaps to proper endianness depending on platform
|
|
|
|
* and Stream settings, and advances the current position
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
*
|
|
|
|
* \return Uint16 The value at the current address
|
|
|
|
* \throw IOException when address is out of range
|
2013-01-29 21:46:05 +00:00
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
atUint16 readUint16();
|
2013-01-26 20:19:24 +00:00
|
|
|
|
|
|
|
/*! \brief Reads a Int32 and swaps to proper endianness depending on platform
|
|
|
|
* and Stream settings, and advances the current position
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
*
|
|
|
|
* \return Int32 The value at the current address
|
|
|
|
* \throw IOException when address is out of range
|
2013-01-29 21:46:05 +00:00
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
atInt32 readInt32();
|
2013-01-26 20:19:24 +00:00
|
|
|
|
|
|
|
/*! \brief Reads a Uint32 and swaps to proper endianness depending on platform
|
|
|
|
* and Stream settings, and advances the current position
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
*
|
|
|
|
* \return Uint32 The value at the current address
|
|
|
|
* \throw IOException when address is out of range
|
2013-01-29 21:46:05 +00:00
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
atUint32 readUint32();
|
2013-01-26 20:19:24 +00:00
|
|
|
|
|
|
|
/*! \brief Reads a Int64 and swaps to proper endianness depending on platform
|
|
|
|
* and Stream settings, and advances the current position
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
*
|
|
|
|
* \return Int64 The value at the current address
|
|
|
|
* \throw IOException when address is out of range
|
2013-01-29 21:46:05 +00:00
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
atInt64 readInt64();
|
2013-01-26 20:19:24 +00:00
|
|
|
|
|
|
|
/*! \brief Reads a Uint64 and swaps to proper endianness depending on platform
|
|
|
|
* and Stream settings, and advances the current position
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
*
|
|
|
|
* \return Uint64 The value at the current address
|
|
|
|
* \throw IOException when address is out of range
|
2013-01-29 21:46:05 +00:00
|
|
|
*/
|
2014-06-18 04:51:18 +00:00
|
|
|
atUint64 readUint64();
|
2013-01-26 20:19:24 +00:00
|
|
|
|
|
|
|
/*! \brief Reads a float and swaps to proper endianness depending on platform
|
|
|
|
* and Stream settings, and advances the current position
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
*
|
|
|
|
* \return float The value at the current address
|
|
|
|
* \throw IOException when address is out of range
|
2013-01-29 21:46:05 +00:00
|
|
|
*/
|
2014-04-20 09:14:15 +00:00
|
|
|
float readFloat();
|
2013-01-26 20:19:24 +00:00
|
|
|
|
|
|
|
/*! \brief Reads a double and swaps to proper endianness depending on platform
|
|
|
|
* and Stream settings, and advances the current position
|
|
|
|
*
|
|
|
|
* \sa Endian
|
|
|
|
*
|
|
|
|
* \return double The value at the current address
|
|
|
|
* \throw IOException when address is out of range
|
2013-01-29 21:46:05 +00:00
|
|
|
*/
|
2014-04-20 09:14:15 +00:00
|
|
|
double readDouble();
|
|
|
|
|
2013-01-26 20:19:24 +00:00
|
|
|
/*! \brief Reads a bool and advances the current position
|
|
|
|
*
|
|
|
|
* \return bool The value at the current address
|
|
|
|
* \throw IOException when address is out of range
|
2013-01-29 21:46:05 +00:00
|
|
|
*/
|
2014-04-20 09:14:15 +00:00
|
|
|
bool readBool();
|
|
|
|
|
2013-01-26 20:19:24 +00:00
|
|
|
/*! \brief Reads a Unicode string and advances the position in the file
|
|
|
|
*
|
|
|
|
* \return std::string The value at the current address
|
|
|
|
* \throw IOException when address is out of range
|
2013-01-29 21:46:05 +00:00
|
|
|
*/
|
2015-01-29 06:20:57 +00:00
|
|
|
std::string readUnicode(atInt32 maxlen = -1);
|
2013-01-26 20:19:24 +00:00
|
|
|
|
2013-07-21 03:57:20 +00:00
|
|
|
/*! \brief Reads a string and advances the position in the file
|
|
|
|
*
|
|
|
|
* \return std::string The value at the current address
|
|
|
|
* \throw IOException when address is out of range
|
|
|
|
*/
|
2015-01-29 06:20:57 +00:00
|
|
|
std::string readString(atInt32 maxlen = -1);
|
2013-07-21 03:57:20 +00:00
|
|
|
|
2014-02-22 08:25:29 +00:00
|
|
|
void setProgressCallback(std::function<void(int)> cb);
|
2013-01-29 21:46:05 +00:00
|
|
|
protected:
|
2014-02-22 08:25:29 +00:00
|
|
|
void loadData();
|
2014-06-18 04:51:18 +00:00
|
|
|
atUint8* m_data;
|
|
|
|
atUint64 m_length;
|
2013-07-21 03:57:20 +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;
|
2014-02-22 08:25:29 +00:00
|
|
|
std::function<void(int)> m_progressCallback;
|
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
|
|
|
|
2013-07-21 03:57:20 +00:00
|
|
|
#ifndef BINARYREADER_BASE
|
2014-06-03 03:09:40 +00:00
|
|
|
#define BINARYREADER_BASE() \
|
2013-07-21 07:49:07 +00:00
|
|
|
private: \
|
2014-06-03 03:09:40 +00:00
|
|
|
typedef Athena::io::BinaryReader base
|
2013-07-28 01:42:11 +00:00
|
|
|
|
2013-07-21 03:57:20 +00:00
|
|
|
#endif // BINARYREADER_BASE
|
2013-07-22 03:06:54 +00:00
|
|
|
|
2013-07-21 03:57:20 +00:00
|
|
|
#endif // __BINARYREADER_HPP__
|