mirror of
https://github.com/libAthena/athena.git
synced 2025-12-09 13:38:03 +00:00
* Fix bug in BinaryReader where it wasn't correctly reading in all the data
This commit is contained in:
@@ -12,11 +12,11 @@
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
|
||||
#ifndef __BINARYREADER_HPP__
|
||||
#define __BINARYREADER_HPP__
|
||||
|
||||
#include "Stream.hpp"
|
||||
#include <string>
|
||||
#ifndef __BINARYREADER_HPP__
|
||||
#define __BINARYREADER_HPP__
|
||||
|
||||
#include "Stream.hpp"
|
||||
#include <string>
|
||||
|
||||
/*! \class BinaryReader
|
||||
* \brief A Stream class for reading binary data
|
||||
@@ -25,9 +25,9 @@
|
||||
* 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
|
||||
*/
|
||||
class BinaryReader : public Stream
|
||||
{
|
||||
*/
|
||||
class BinaryReader : public Stream
|
||||
{
|
||||
public:
|
||||
/*! \brief This constructor takes an existing buffer to read from.
|
||||
*
|
||||
@@ -38,12 +38,12 @@ public:
|
||||
/*! \brief This constructor takes an existing Stream to read from.
|
||||
*
|
||||
* \param stream The stream to read data from
|
||||
*/
|
||||
*/
|
||||
BinaryReader(const Stream& stream);
|
||||
/*! \brief This constructor creates an instance from a file on disk.
|
||||
*
|
||||
* \param filename The file to create the stream from
|
||||
*/
|
||||
*/
|
||||
BinaryReader(const std::string& filename);
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ public:
|
||||
*
|
||||
* \return Uint16 The value at the current address
|
||||
* \throw IOException when address is out of range
|
||||
*/
|
||||
*/
|
||||
Uint16 readUInt16();
|
||||
|
||||
/*! \brief Reads a Int32 and swaps to proper endianness depending on platform
|
||||
@@ -74,7 +74,7 @@ public:
|
||||
*
|
||||
* \return Int32 The value at the current address
|
||||
* \throw IOException when address is out of range
|
||||
*/
|
||||
*/
|
||||
Int32 readInt32();
|
||||
|
||||
/*! \brief Reads a Uint32 and swaps to proper endianness depending on platform
|
||||
@@ -84,7 +84,7 @@ public:
|
||||
*
|
||||
* \return Uint32 The value at the current address
|
||||
* \throw IOException when address is out of range
|
||||
*/
|
||||
*/
|
||||
Uint32 readUInt32();
|
||||
|
||||
/*! \brief Reads a Int64 and swaps to proper endianness depending on platform
|
||||
@@ -94,7 +94,7 @@ public:
|
||||
*
|
||||
* \return Int64 The value at the current address
|
||||
* \throw IOException when address is out of range
|
||||
*/
|
||||
*/
|
||||
Int64 readInt64();
|
||||
|
||||
/*! \brief Reads a Uint64 and swaps to proper endianness depending on platform
|
||||
@@ -104,7 +104,7 @@ public:
|
||||
*
|
||||
* \return Uint64 The value at the current address
|
||||
* \throw IOException when address is out of range
|
||||
*/
|
||||
*/
|
||||
Uint64 readUInt64();
|
||||
|
||||
/*! \brief Reads a float and swaps to proper endianness depending on platform
|
||||
@@ -114,7 +114,7 @@ public:
|
||||
*
|
||||
* \return float The value at the current address
|
||||
* \throw IOException when address is out of range
|
||||
*/
|
||||
*/
|
||||
float readFloat();
|
||||
|
||||
/*! \brief Reads a double and swaps to proper endianness depending on platform
|
||||
@@ -124,26 +124,26 @@ public:
|
||||
*
|
||||
* \return double The value at the current address
|
||||
* \throw IOException when address is out of range
|
||||
*/
|
||||
*/
|
||||
double readDouble();
|
||||
/*! \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
|
||||
*/
|
||||
*/
|
||||
bool readBool();
|
||||
/*! \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
|
||||
*/
|
||||
*/
|
||||
std::string readUnicode();
|
||||
|
||||
private:
|
||||
protected:
|
||||
/*! \brief Overload of isOpenForWriting in Stream
|
||||
*
|
||||
* \return false
|
||||
*/
|
||||
*/
|
||||
bool isOpenForWriting();
|
||||
/*! \brief Overload of writeByte in Stream
|
||||
*
|
||||
@@ -153,9 +153,9 @@ private:
|
||||
/*! \brief Overload of writeBytes in Stream
|
||||
*
|
||||
* \throw IOException
|
||||
*/
|
||||
void writeBytes(Int8*, Int64);
|
||||
std::string m_filename;
|
||||
};
|
||||
|
||||
#endif
|
||||
*/
|
||||
void writeBytes(Int8*, Int64);
|
||||
std::string m_filename;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -13,36 +13,37 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
#ifndef __EXCEPTION_HPP__
|
||||
#define __EXCEPTION_HPP__
|
||||
|
||||
#include <string>
|
||||
#ifndef __EXCEPTION_HPP__
|
||||
#define __EXCEPTION_HPP__
|
||||
|
||||
#include <string>
|
||||
|
||||
/*! \class Exception
|
||||
* \brief The baseclass for all Exceptions.
|
||||
*
|
||||
* <b>Do Not</b> use Exception directly, instead create an appropriate
|
||||
* Exception class and inherit from this baseclass.
|
||||
*/
|
||||
class Exception
|
||||
{
|
||||
*/
|
||||
class Exception
|
||||
{
|
||||
public:
|
||||
/*! \brief The constructor for an Exception
|
||||
* \param message The error message to throw
|
||||
*/
|
||||
Exception(const std::string& message) :
|
||||
m_message(message)
|
||||
{};
|
||||
*/
|
||||
inline Exception(const std::string& message) :
|
||||
m_message(message)
|
||||
{
|
||||
};
|
||||
|
||||
/*! \brief Returns the Error message of the exception
|
||||
* \return std::string The error message
|
||||
*/
|
||||
std::string message() const
|
||||
{
|
||||
return m_message;
|
||||
};
|
||||
protected:
|
||||
std::string m_message;
|
||||
};
|
||||
|
||||
#endif
|
||||
*/
|
||||
inline std::string message() const
|
||||
{
|
||||
return m_message;
|
||||
};
|
||||
protected:
|
||||
std::string m_message;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -13,10 +13,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
#ifndef __FILENOTFOUNDEXCEPTION_HPP__
|
||||
#define __FILENOTFOUNDEXCEPTION_HPP__
|
||||
|
||||
#include "Exception.hpp"
|
||||
#ifndef __FILENOTFOUNDEXCEPTION_HPP__
|
||||
#define __FILENOTFOUNDEXCEPTION_HPP__
|
||||
|
||||
#include "Exception.hpp"
|
||||
|
||||
/*! \class FileNotFoundException
|
||||
* \brief An excpeption thrown when a file could not be found at the given path.
|
||||
@@ -25,24 +25,24 @@
|
||||
* <br />
|
||||
* It is <b>NOT</b> appropriate to use <b>throw new</b> so avoid doing so,
|
||||
* keeping things on the stack as much as possible is very important for speed.
|
||||
*/
|
||||
class FileNotFoundException : public Exception
|
||||
{
|
||||
*/
|
||||
class FileNotFoundException : public Exception
|
||||
{
|
||||
public:
|
||||
/*! \brief The constructor for an FileNotFoundException
|
||||
* \param filename The path of the offending file.
|
||||
*/
|
||||
FileNotFoundException(const std::string& filename) :
|
||||
Exception("FileNotFoundException:\nCouldn't not find \"" + filename + "\", please check that it exists."),
|
||||
m_filename(filename)
|
||||
{}
|
||||
*/
|
||||
inline FileNotFoundException(const std::string& filename) :
|
||||
Exception("FileNotFoundException:\nCouldn't not find \"" + filename + "\", please check that it exists."),
|
||||
m_filename(filename)
|
||||
{}
|
||||
|
||||
/*! \brief Returns the path of the offending file.
|
||||
* \return std::string The filename of the file including the path.
|
||||
*/
|
||||
std::string filename() const { return m_filename; }
|
||||
private:
|
||||
std::string m_filename;
|
||||
};
|
||||
|
||||
#endif
|
||||
*/
|
||||
inline std::string filename() const { return m_filename; }
|
||||
private:
|
||||
std::string m_filename;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -13,11 +13,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
|
||||
|
||||
#ifndef __INVALID_OPERATION_EXCEPTION_HPP__
|
||||
#define __INVALID_OPERATION_EXCEPTION_HPP__
|
||||
|
||||
#include <string>
|
||||
#include <Exception.hpp>
|
||||
#ifndef __INVALID_OPERATION_EXCEPTION_HPP__
|
||||
#define __INVALID_OPERATION_EXCEPTION_HPP__
|
||||
|
||||
#include <string>
|
||||
#include <Exception.hpp>
|
||||
|
||||
/*! \class InvalidOperationException
|
||||
* \brief An excpeption thrown on Invalid Operations calls.
|
||||
@@ -27,16 +27,16 @@
|
||||
* <br />
|
||||
* It is <b>NOT</b> appropriate to use <b>throw new</b> so avoid doing so,
|
||||
* keeping things on the stack as much as possible is very important for speed.
|
||||
*/
|
||||
class InvalidOperationException : public Exception
|
||||
{
|
||||
*/
|
||||
class InvalidOperationException : public Exception
|
||||
{
|
||||
public:
|
||||
/*! \brief The constructor for an InvalidOperationException
|
||||
* \param error The error message to throw
|
||||
*/
|
||||
InvalidOperationException(const std::string& error)
|
||||
: Exception("InvalidOperationException:\n" + error)
|
||||
{
|
||||
}
|
||||
};
|
||||
#endif // __INVALID_OPERATION_EXCEPTION_HPP__
|
||||
*/
|
||||
inline InvalidOperationException(const std::string& error)
|
||||
: Exception("InvalidOperationException:\n" + error)
|
||||
{
|
||||
}
|
||||
};
|
||||
#endif // __INVALID_OPERATION_EXCEPTION_HPP__
|
||||
|
||||
@@ -37,7 +37,34 @@
|
||||
* return 0;
|
||||
* }
|
||||
* \endcode
|
||||
* \section example_sec BinaryReader example
|
||||
* \code
|
||||
* #include "BinaryReader.hpp"
|
||||
* #include "FileNotFoundException.hpp"
|
||||
* #include "Exception.hpp"
|
||||
* int main()
|
||||
* {
|
||||
* try
|
||||
* {
|
||||
* BinaryReader writer("test.bin");
|
||||
* std::cout << reader.readByte() << std::endl;
|
||||
* std::cout << reader.readInt32() << std::endl;
|
||||
* }
|
||||
* catch (FileNotFoundException e)
|
||||
* {
|
||||
* std::cout << e.message() << std::endl;
|
||||
* }
|
||||
* catch (Exception e)
|
||||
* {
|
||||
* std::cout << e.message() << std::endl;
|
||||
* }
|
||||
* catch(...)
|
||||
* {
|
||||
* }
|
||||
* return 0;
|
||||
* }
|
||||
* \endcode
|
||||
* \section Credits
|
||||
* Chibi Zelda: AnimeWaterFall on Deviantart
|
||||
* Chibi Zelda: <a href="http://animewaterfall.deviantart.com/art/Chibi-Zelda-331611090">AnimeWaterFall</a> on Deviantart
|
||||
*/
|
||||
#endif // __MAINPAGE_HPP__
|
||||
|
||||
@@ -12,10 +12,10 @@
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
|
||||
#ifndef __STREAM_HPP__
|
||||
#define __STREAM_HPP__
|
||||
|
||||
#include "Types.hpp"
|
||||
#ifndef __STREAM_HPP__
|
||||
#define __STREAM_HPP__
|
||||
|
||||
#include "Types.hpp"
|
||||
|
||||
/*! \class Stream
|
||||
* \brief Stream is the main class all streams inherit from
|
||||
@@ -28,10 +28,11 @@
|
||||
* <br />
|
||||
* Stream can also be used by itself though it's not advised as it's not feature filled.<br />
|
||||
* It's highly suggested to use of the more advanced Streams such as BinaryReader.
|
||||
*/
|
||||
class Stream
|
||||
{
|
||||
*/
|
||||
class Stream
|
||||
{
|
||||
public:
|
||||
//! \brief Default buffer block size.
|
||||
static const Uint32 BLOCKSZ;
|
||||
|
||||
/*! \enum Endian
|
||||
@@ -47,50 +48,50 @@ public:
|
||||
|
||||
/*! \enum SeekOrigin
|
||||
* \brief Specifies how to seek in a stream.
|
||||
*/
|
||||
*/
|
||||
enum SeekOrigin
|
||||
{
|
||||
Beginning = 0, //!< Tells the Stream to seek from the Beginning of the buffer.
|
||||
Current, //!< Tells the Stream to seek from the Current position of the buffer.
|
||||
End //!< Tells the Stream to seek from the End of the buffer.
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
/*! \brief The default constructor
|
||||
*/
|
||||
*/
|
||||
Stream();
|
||||
/*! \brief This constructor takes an existing buffer to read from.
|
||||
*
|
||||
* \param bytes The existing buffer
|
||||
* \param length The length of the existing buffer
|
||||
*/
|
||||
*/
|
||||
Stream(const Uint8* bytes, Uint64 length);
|
||||
/*! \brief This constructor creates a buffer from the given length.
|
||||
*
|
||||
* \param length The length of the existing buffer
|
||||
*/
|
||||
*/
|
||||
Stream(Int64 length);
|
||||
/*! \brief This constructor takes an existing Stream to read from.
|
||||
*
|
||||
* \param stream The stream to read data from
|
||||
*/
|
||||
*/
|
||||
Stream(Stream* stream);
|
||||
|
||||
/*! \brief The destructor cleans up memory and sets everything back
|
||||
* to the default settings.
|
||||
*/
|
||||
virtual ~Stream();
|
||||
*/
|
||||
virtual ~Stream();
|
||||
|
||||
/*! \brief Writes a bit at the current position and advances the position by one bit.
|
||||
* \param val the value to write
|
||||
* \throw IOException
|
||||
*/
|
||||
*/
|
||||
virtual 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
|
||||
* \throw IOException
|
||||
*/
|
||||
*/
|
||||
virtual void writeByte(Int8 byte);
|
||||
|
||||
/*! \brief Writes the given buffer with the specified length, buffers can be bigger than the length
|
||||
@@ -99,27 +100,27 @@ public:
|
||||
* \param data The buffer to write
|
||||
* \param length The amount to write
|
||||
* \throw IOException
|
||||
*/
|
||||
*/
|
||||
virtual void writeBytes(Int8* data, Int64 length);
|
||||
|
||||
/*! \brief Reads a bit at the current position and advances the current position
|
||||
*
|
||||
* \return bool The value at the current position
|
||||
* \throw IOException
|
||||
*/
|
||||
*/
|
||||
virtual bool readBit();
|
||||
/*! \brief Reads a byte at the current position and advances the current position
|
||||
*
|
||||
* \return Int8 The value at the current position
|
||||
* \throw IOException
|
||||
*/
|
||||
*/
|
||||
virtual Int8 readByte();
|
||||
|
||||
/*! \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.
|
||||
* \throw IOException
|
||||
*/
|
||||
*/
|
||||
virtual Int8* readBytes(Int64 length);
|
||||
|
||||
/*! \brief Sets the buffers position relative to the specified position.<br />
|
||||
@@ -127,15 +128,15 @@ public:
|
||||
* \param position where in the buffer to seek
|
||||
* \param origin The Origin to seek \sa SeekOrigin
|
||||
* \throw IOException
|
||||
*/
|
||||
*/
|
||||
void seek(Int64 position, SeekOrigin origin = Current);
|
||||
|
||||
/*! \brief Resizes the buffer to the given length.<br />
|
||||
* The new size must be greater than the current length to avoid loss of data.
|
||||
* \param newSize The amount to resize by.
|
||||
* \throw IOException
|
||||
*/
|
||||
void resize(Uint64 newSize);
|
||||
*/
|
||||
void resize(Uint64 newSize);
|
||||
|
||||
/*! \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
|
||||
@@ -161,44 +162,44 @@ public:
|
||||
/*! \brief Returns the length of the Stream.
|
||||
*
|
||||
* \return Int64 The length of the stream.
|
||||
*/
|
||||
*/
|
||||
Int64 length();
|
||||
|
||||
/*! \brief Returns the current position in the stream.
|
||||
*
|
||||
* \return Int64 The current position in the stream.
|
||||
*/
|
||||
*/
|
||||
Int64 position();
|
||||
|
||||
/*! \brief Returns whether or not the stream is at the end.
|
||||
*
|
||||
* \return bool True if at end; False otherwise.
|
||||
*/
|
||||
*/
|
||||
bool atEnd();
|
||||
|
||||
/*! \brief Sets whether the Stream resizes when the at the end of the buffer.
|
||||
*
|
||||
* \param val True for resizing; False for no resizing.
|
||||
*/
|
||||
*/
|
||||
void setAutoResizing(bool val);
|
||||
|
||||
/*! \brief Retuns whether or not the Stream currenty autoresizes.
|
||||
*
|
||||
* \return True for resizing; False otherwise.
|
||||
*/
|
||||
bool autoResizing() const;
|
||||
*/
|
||||
bool autoResizing() const;
|
||||
|
||||
|
||||
/*! \brief Retuns whether or not the Stream is open for reading.
|
||||
*
|
||||
* \return True if open for reading; False otherwise.
|
||||
*/
|
||||
*/
|
||||
virtual bool isOpenForReading() const;
|
||||
|
||||
/*! \brief Retuns whether or not the Stream is open for writing
|
||||
*
|
||||
* \return True if open for writing; False otherwise.
|
||||
*/
|
||||
*/
|
||||
virtual bool isOpenForWriting() const;
|
||||
|
||||
/*! \brief Sets the Endianss of the stream
|
||||
@@ -225,14 +226,14 @@ public:
|
||||
* \return bool True for LittleEndian; False for BigEndian
|
||||
*/
|
||||
bool isLittleEndian() const;
|
||||
|
||||
protected:
|
||||
Uint32 m_bitPosition; //!< The current position in the current byte
|
||||
Uint64 m_position; //!< The current position in the Stream
|
||||
|
||||
protected:
|
||||
Uint32 m_bitPosition; //!< The current position in the current byte
|
||||
Uint64 m_position; //!< The current position in the Stream
|
||||
Uint64 m_length; //!< The length of the Stream
|
||||
Endian m_endian; //!< The Endianess of the Stream
|
||||
Uint8* m_data; //!< The Stream buffer
|
||||
bool m_autoResize; //!< Whether the stream is autoresizing
|
||||
};
|
||||
|
||||
#endif // __STREAM_HPP__
|
||||
Endian m_endian; //!< The Endianess of the Stream
|
||||
Uint8* m_data; //!< The Stream buffer
|
||||
bool m_autoResize; //!< Whether the stream is autoresizing
|
||||
};
|
||||
|
||||
#endif // __STREAM_HPP__
|
||||
|
||||
Reference in New Issue
Block a user