significant virtual-method refactor to streaming interface

This commit is contained in:
Jack Andersen 2015-07-07 16:53:39 -10:00
parent ce917d4aca
commit 97dfabb265
25 changed files with 1526 additions and 2552 deletions

View File

@ -1,14 +0,0 @@
compiler:
- clang
before_install:
- sudo add-apt-repository --yes "deb http://llvm.org/apt/precise/ llvm-toolchain-precise-3.6 main"
- wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key | sudo apt-key add -;
- sudo add-apt-repository --yes ppa:ubuntu-sdk-team/ppa
- sudo add-apt-repository --yes ppa:ubuntu-toolchain-r/test
- sudo apt-get update -qq
- sudo apt-get install -qq qt5-default clang-3.6
script:
- qmake QMAKE_CXX=clang++-3.6 QMAKE_CC=clang-3.6 Athena.pro
- make

View File

@ -14,7 +14,7 @@ SOURCES += \
$$PWD/src/ec.cpp \
$$PWD/src/md5.cpp \
$$PWD/src/sha1.cpp \
$$PWD/src/aes.c
$$PWD/src/aes.cpp
HEADERS += \
$$PWD/include/Athena/WiiBanner.hpp \
@ -23,7 +23,7 @@ HEADERS += \
$$PWD/include/Athena/WiiSave.hpp \
$$PWD/include/Athena/WiiSaveReader.hpp \
$$PWD/include/Athena/WiiSaveWriter.hpp \
$$PWD/include/aes.h \
$$PWD/include/aes.hpp \
$$PWD/include/bn.h \
$$PWD/include/ec.h \
$$PWD/include/md5.h \

View File

@ -53,10 +53,6 @@ add_library(AthenaCore
include/LZ77/LZLookupTable.hpp
include/LZ77/LZType10.hpp
include/LZ77/LZType11.hpp
include/utf8.h
include/utf8/checked.h
include/utf8/core.h
include/utf8/unchecked.h
include/Athena/FileInfo.hpp
include/Athena/Dir.hpp
include/gekko_support.h
@ -90,7 +86,7 @@ add_library(AthenaWiiSave
src/ec.cpp
src/md5.cpp
src/sha1.cpp
src/aes.c
src/aes.cpp
include/Athena/WiiBanner.hpp
include/Athena/WiiFile.hpp
@ -98,12 +94,13 @@ add_library(AthenaWiiSave
include/Athena/WiiSave.hpp
include/Athena/WiiSaveReader.hpp
include/Athena/WiiSaveWriter.hpp
include/aes.h
include/aes.hpp
include/bn.h
include/ec.h
include/md5.h
include/sha1.h
)
set_source_files_properties(src/aes.cpp PROPERTIES COMPILE_FLAGS -maes)
add_library(AthenaZelda
src/Athena/ALTTPFile.cpp

View File

@ -17,64 +17,20 @@ public:
inline const std::string& filename() const
{return m_filename;}
inline void setEndian(Endian endian)
{m_endian = endian;}
inline Endian endian() const
{return m_endian;}
inline bool isBigEndian() const
{return (m_endian == Endian::BigEndian);}
inline bool isLittleEndian() const
{return (m_endian == Endian::LittleEndian);}
void open();
void close();
inline bool isOpen() const
{return m_fileHandle != NULL;}
bool save();
void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
inline void seekAlign32()
{FileReader::seek(ROUND_UP_32(FileReader::position()), SeekOrigin::Begin);}
bool atEnd() const;
atUint64 position() const;
atUint64 length() const;
void seekBit(int);
bool readBit();
atUint8 readUByte();
inline atInt8 readByte()
{return (atInt8)FileReader::readUByte();}
atUint8* readUBytes(atUint64 len);
inline atInt8* readBytes(atUint64 len)
{return (atInt8*)FileReader::readUBytes(len);}
atUint64 readBytesToBuf(void* buf, atUint64 len)
{return FileReader::readUBytesToBuf(buf, len);}
atUint64 readUBytesToBuf(void* buf, atUint64 len);
atUint16 readUint16();
inline atInt16 readInt16()
{return (atInt16)FileReader::readUint16();}
atUint32 readUint32();
inline atInt32 readInt32()
{return (atInt32)FileReader::readUint32();}
atUint64 readUint64();
inline atInt64 readInt64()
{return (atInt64)FileReader::readUint64();}
double readDouble();
float readFloat();
inline bool readBool()
{return (FileReader::readByte() != 0);}
atVec3f readVec3f();
atVec4f readVec4f();
std::string readString(atInt32 fixedLen = -1);
std::wstring readWString(atInt32 fixedLen = -1);
std::string readUnicode(atInt32 fixedLen = -1);
protected:
std::string m_filename;
FILE* m_fileHandle;
Endian m_endian;
atUint8 m_currentByte;
atUint8 m_bitShift;
bool m_bitValid;
atUint8 m_currentByte;
};
} // io
} // Athena

View File

@ -14,62 +14,20 @@ public:
FileWriter(const std::string& filename, bool overwrite = true);
virtual ~FileWriter();
inline void setEndian(Endian endian)
{m_endian = endian;}
inline Endian endian() const
{return m_endian;}
inline bool isBigEndian() const
{return (m_endian == Endian::BigEndian);}
inline bool isLittleEndian() const
{return (m_endian == Endian::LittleEndian);}
void open(bool overwrite = true);
void close();
inline bool isOpen() const
{return m_fileHandle != NULL;}
void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
inline void seekAlign32() {seek(ROUND_UP_32(position()), SeekOrigin::Begin);}
bool atEnd() const;
atUint64 position() const;
atUint64 length() const;
void writeBit(bool val);
void seekBit(int bit);
void writeUByte(atUint8 val);
inline void writeByte(atInt8 val)
{FileWriter::writeUByte(val);}
void writeUBytes(const atUint8* data, atUint64 len);
void writeBytes(const atInt8* data, atUint64 len)
{FileWriter::writeUBytes((atUint8*)data, len);}
void writeUint16(atUint16 val);
inline void writeInt16(atInt16 val)
{FileWriter::writeUint16(val);}
void writeUint32(atUint32 val);
inline void writeInt32(atInt32 val)
{FileWriter::writeUint32(val);}
void writeUint64(atUint64 val);
inline void writeInt64(atInt64 val)
{FileWriter::writeUint64(val);}
void writeDouble(double val);
void writeFloat(float val);
inline void writeBool(bool val)
{FileWriter::writeByte(val);}
void writeVec3f(atVec3f vec);
void writeVec4f(atVec4f vec);
void writeString(const std::string& val, atInt32 fixedLen = -1);
void writeWString(const std::wstring& str, atInt32 fixedLen = -1);
void writeUnicode(const std::string& str, atInt32 fixedLen = -1);
void fill(atInt8 byte, atUint64 len);
inline void fill(atUint8 byte, atUint64 len)
{FileWriter::fill((atInt8)byte, len);}
private:
std::string m_filename;
FILE* m_fileHandle;
Endian m_endian;
atUint8 m_currentByte;
atUint64 m_bytePosition;
atUint8 m_bitShift;
bool m_bitValid;
};
}
} // Athena

View File

@ -1,9 +1,9 @@
#ifndef GLOBAL_HPP
#define GLOBAL_HPP
#include <iostream>
#include "Athena/Types.hpp"
#include "Athena/Utility.hpp"
#include <iostream>
#ifdef _MSC_VER
#pragma warning(disable : 4996)

View File

@ -19,12 +19,10 @@ public:
virtual Endian endian() const = 0;
virtual bool isBigEndian() const = 0;
virtual bool isLittleEndian()const = 0;
virtual bool isOpen() const = 0;
virtual void seek(atInt64, SeekOrigin) = 0;
virtual bool atEnd() const = 0;
virtual atUint64 position() const = 0;
virtual atUint64 length() const = 0;
virtual void seekBit(int) = 0;
};
}
}

View File

@ -1,6 +1,8 @@
#ifndef ISTREAMREADER_HPP
#define ISTREAMREADER_HPP
#include <locale>
#include <codecvt>
#include "IStream.hpp"
namespace Athena
@ -11,38 +13,353 @@ class IStreamReader : public IStream
{
public:
virtual ~IStreamReader() {}
virtual void setEndian(Endian) = 0;
virtual Endian endian() const = 0;
virtual bool isBigEndian() const = 0;
virtual bool isLittleEndian()const = 0;
virtual bool isOpen() const = 0;
virtual void seek(atInt64, SeekOrigin) = 0;
virtual void seekAlign32() = 0;
virtual bool atEnd() const = 0;
virtual atUint64 position() const = 0;
virtual atUint64 length() const = 0;
virtual void seekBit(int) = 0;
virtual bool readBit() = 0;
virtual atUint8 readUByte() = 0;
virtual atInt8 readByte() = 0;
virtual atUint8* readUBytes(atUint64) = 0;
virtual atInt8* readBytes(atUint64) = 0;
virtual atUint64 readUBytesToBuf(void*, atUint64) = 0;
virtual atUint64 readBytesToBuf(void*, atUint64) = 0;
virtual atUint16 readUint16() = 0;
virtual atInt16 readInt16() = 0;
virtual atUint32 readUint32() = 0;
virtual atInt32 readInt32() = 0;
virtual atUint64 readUint64() = 0;
virtual atInt64 readInt64() = 0;
virtual double readDouble() = 0;
virtual float readFloat() = 0;
virtual bool readBool() = 0;
virtual atVec3f readVec3f() = 0;
virtual atVec4f readVec4f() = 0;
virtual std::string readUnicode(atInt32 = -1) = 0;
virtual std::string readString(atInt32 = -1) = 0;
virtual std::wstring readWString(atInt32 = -1) = 0;
/*! \brief Sets the Endianss of the stream
*
* \param endian The Endianess to set \sa Endian
*/
inline void setEndian(Endian endian)
{m_endian = endian;}
/*! \brief Returns the current Endianness of the stream
*
* \return Endian The current Stream Endianess
*/
inline Endian endian() const
{return m_endian;}
/*! \brief Returns whether the stream is BigEndian
*
* \return bool True for BigEndian; False for LittleEndian
*/
inline bool isBigEndian() const
{return (m_endian == Endian::BigEndian);}
/*! \brief Returns whether the stream is LittleEndian
*
* \return bool True for LittleEndian; False for BigEndian
*/
inline bool isLittleEndian()const
{return (m_endian == Endian::LittleEndian);}
/*! \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
*/
virtual void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current)=0;
/*! \brief Sets the buffers position relative to the next 32-byte aligned position.<br />
*/
inline void seekAlign32() {seek(ROUND_UP_32(position()), SeekOrigin::Begin);}
/*! \brief Returns whether or not the stream is at the end.
*
* \return bool True if at end; False otherwise.
*/
inline bool atEnd() const
{return position() >= length();}
/*! \brief Returns the current position in the stream.
*
* \return Int64 The current position in the stream.
*/
virtual atUint64 position() const=0;
/*! \brief Returns whether or not the stream is at the end.
*
* \return bool True if at end; False otherwise.
*/
virtual atUint64 length() const=0;
/*! \brief Reads a byte at the current position and advances the current position
*
* \return Int8 The value at the current position
*/
inline atInt8 readByte() {atInt8 val; readUBytesToBuf(&val, 1); return val;}
/*! \brief Reads a byte at the current position and advances the current position
*
* \return Uint8 The value at the current position
*/
inline atUint8 readUByte() {return readByte();}
/*! \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.
*/
inline atInt8* readBytes(atUint64 length)
{atInt8* buf = new atInt8[length]; readUBytesToBuf(buf, length); return buf;}
/*! \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.
*/
inline atUint8* readUBytes(atUint64 length) {return (atUint8*)readBytes(length);}
inline atUint64 readBytesToBuf(void* buf, atUint64 len) {return readUBytesToBuf(buf, len);}
virtual atUint64 readUBytesToBuf(void* buf, atUint64 len)=0;
/*! \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
*/
inline atInt16 readInt16()
{
atInt16 val;
readUBytesToBuf(&val, 2);
return m_endian == BigEndian ? utility::BigInt16(val) : utility::LittleInt16(val);
}
/*! \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
*/
inline atUint16 readUint16()
{return readInt16();}
/*! \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
*/
inline atInt32 readInt32()
{
atInt32 val;
readUBytesToBuf(&val, 4);
return m_endian == BigEndian ? utility::BigInt32(val) : utility::LittleInt32(val);
}
/*! \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
*/
inline atUint32 readUint32()
{return readInt32();}
/*! \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
*/
inline atInt64 readInt64()
{
atInt64 val;
readUBytesToBuf(&val, 8);
return m_endian == BigEndian ? utility::BigInt64(val) : utility::LittleInt64(val);
}
/*! \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
*/
inline atUint64 readUint64()
{return readInt64();}
/*! \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
*/
inline float readFloat()
{
float val;
readUBytesToBuf(&val, 4);
return m_endian == BigEndian ? utility::BigFloat(val) : utility::LittleFloat(val);
}
/*! \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
*/
inline double readDouble()
{
double val;
readUBytesToBuf(&val, 8);
return m_endian == BigEndian ? utility::BigDouble(val) : utility::LittleDouble(val);
}
/*! \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
*/
inline bool readBool()
{
atUint8 val;
readUBytesToBuf(&val, 1);
return val != 0;
}
/*! \brief Reads an atVec3f (12 bytes) and advances the current position
*
* \return atVec3f The value at the current address
* \throw IOException when address is out of range
*/
inline atVec3f readVec3f()
{
atVec3f val;
readUBytesToBuf(&val, 12);
if (m_endian == BigEndian)
{
utility::BigFloat(val.vec[0]);
utility::BigFloat(val.vec[1]);
utility::BigFloat(val.vec[2]);
}
else
{
utility::LittleFloat(val.vec[0]);
utility::LittleFloat(val.vec[1]);
utility::LittleFloat(val.vec[2]);
}
return val;
}
/*! \brief Reads an atVec4f (16 bytes) and advances the current position
*
* \return atVec4f The value at the current address
* \throw IOException when address is out of range
*/
inline atVec4f readVec4f()
{
atVec4f val;
readUBytesToBuf(&val, 16);
if (m_endian == BigEndian)
{
utility::BigFloat(val.vec[0]);
utility::BigFloat(val.vec[1]);
utility::BigFloat(val.vec[2]);
utility::BigFloat(val.vec[3]);
}
else
{
utility::LittleFloat(val.vec[0]);
utility::LittleFloat(val.vec[1]);
utility::LittleFloat(val.vec[2]);
utility::LittleFloat(val.vec[3]);
}
return val;
}
/*! \brief Reads a Unicode string and advances the position in the file
*
* \param fixedLen If non-negative, this is a fixed-length string read
* \return std::string The value at the current address
* \throw IOException when address is out of range
*/
inline std::string readUnicode(atInt32 fixedLen = -1)
{
std::wstring tmp;
atUint16 chr = readUint16();
atInt32 i;
for (i = 0 ;; ++i)
{
if (fixedLen >= 0 && i >= fixedLen - 1)
break;
if (!chr)
break;
tmp.push_back(chr);
chr = readUint16();
}
if (fixedLen >= 0 && i < fixedLen)
seek(fixedLen - i);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
return conv.to_bytes(tmp);
}
/*! \brief Reads a string and advances the position in the file
*
* \param fixedLen If non-negative, this is a fixed-length string read
* \return std::string The value at the current address
* \throw IOException when address is out of range
*/
inline std::string readString(atInt32 fixedLen = -1)
{
std::string ret;
atUint8 chr = readByte();
atInt32 i;
for (i = 1 ; chr != 0 ; ++i)
{
ret += chr;
if (fixedLen >= 0 && i >= fixedLen)
break;
chr = readByte();
}
if (fixedLen >= 0 && i < fixedLen)
seek(fixedLen - i);
return ret;
}
/*! \brief Reads a wstring and advances the position in the file
*
* \param fixedLen If non-negative, this is a fixed-length string read
* \return std::wstring The value at the current address
* \throw IOException when address is out of range
*/
inline std::wstring readWString(atInt32 fixedLen = -1)
{
std::wstring ret;
atUint16 chr = readUint16();
atInt32 i;
for (i = 1 ; chr != 0 ; ++i)
{
ret += chr;
if (fixedLen >= 0 && i >= fixedLen)
break;
chr = readUint16();
}
if (fixedLen >= 0 && i < fixedLen)
seek(fixedLen - i);
return ret;
}
protected:
Endian m_endian;
};
}
}

View File

@ -1,6 +1,8 @@
#ifndef ISTREAMWRITER_HPP
#define ISTREAMWRITER_HPP
#include <locale>
#include <codecvt>
#include "IStream.hpp"
namespace Athena
@ -11,38 +13,365 @@ class IStreamWriter : public IStream
{
public:
virtual ~IStreamWriter() {}
virtual void setEndian(Endian) = 0;
virtual Endian endian() const = 0;
virtual bool isBigEndian() const = 0;
virtual bool isLittleEndian()const = 0;
virtual bool isOpen() const = 0;
virtual void seek(atInt64, SeekOrigin) = 0;
virtual void seekAlign32() = 0;
virtual bool atEnd() const = 0;
virtual atUint64 position() const = 0;
virtual atUint64 length() const = 0;
virtual void seekBit(int) = 0;
virtual void writeBit(bool) = 0;
virtual void writeUByte(atUint8) = 0;
virtual void writeByte(atInt8) = 0;
virtual void writeUBytes(const atUint8*, atUint64) = 0;
virtual void writeBytes(const atInt8*, atUint64) = 0;
virtual void writeUint16(atUint16) = 0;
virtual void writeInt16(atInt16) = 0;
virtual void writeUint32(atUint32) = 0;
virtual void writeInt32(atInt32) = 0;
virtual void writeUint64(atUint64) = 0;
virtual void writeInt64(atInt64) = 0;
virtual void writeDouble(double) = 0;
virtual void writeFloat(float) = 0;
virtual void writeBool(bool) = 0;
virtual void writeVec3f(atVec3f vec) = 0;
virtual void writeVec4f(atVec4f vec) = 0;
virtual void writeString(const std::string&, atInt32 = -1) = 0;
virtual void writeWString(const std::wstring&, atInt32 = -1) = 0;
virtual void writeUnicode(const std::string&, atInt32 = -1) = 0;
virtual void fill(atUint8, atUint64) = 0;
virtual void fill(atInt8, atUint64) = 0;
/*! \brief Sets the Endianss of the stream
*
* \param endian The Endianess to set \sa Endian
*/
inline void setEndian(Endian endian)
{m_endian = endian;}
/*! \brief Returns the current Endianness of the stream
*
* \return Endian The current Stream Endianess
*/
inline Endian endian() const
{return m_endian;}
/*! \brief Returns whether the stream is BigEndian
*
* \return bool True for BigEndian; False for LittleEndian
*/
inline bool isBigEndian() const
{return (m_endian == Endian::BigEndian);}
/*! \brief Returns whether the stream is LittleEndian
*
* \return bool True for LittleEndian; False for BigEndian
*/
inline bool isLittleEndian() const
{return (m_endian == Endian::LittleEndian);}
/*! \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
*/
virtual void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current)=0;
/*! \brief Sets the buffers position relative to the next 32-byte aligned position.<br />
*/
inline void seekAlign32() {seek(ROUND_UP_32(position()), SeekOrigin::Begin);}
/*! \brief Returns whether or not the stream is at the end.
*
* \return bool True if at end; False otherwise.
*/
inline bool atEnd() const {return position() >= length();}
/*! \brief Returns the current position in the stream.
*
* \return Int64 The current position in the stream.
*/
virtual atUint64 position() const=0;
/*! \brief Returns whether or not the stream is at the end.
*
* \return bool True if at end; False otherwise.
*/
virtual atUint64 length() const=0;
/*! \brief Writes a byte at the current position and advances the position by one byte.
* \param byte The value to write
*/
inline void writeUByte(atUint8 val) {writeUBytes(&val, 1);}
/*! \brief Writes a byte at the current position and advances the position by one byte.
* \param byte The value to write
* \throw IOException
*/
inline void writeByte(atInt8 val) {writeUByte(val);}
/*! \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
*/
virtual void writeUBytes(const atUint8* data, atUint64 len)=0;
/*! \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
*/
inline void writeBytes(const atInt8* data, atUint64 len) {writeUBytes((atUint8*)data, len);}
/*! \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
*/
inline void writeInt16(atInt16 val)
{
if (m_endian == BigEndian)
utility::BigInt16(val);
else
utility::LittleInt16(val);
writeUBytes((atUint8*)&val, 2);
}
/*! \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
*/
inline void writeUint16(atUint16 val) {writeInt16(val);}
/*! \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
*/
inline void writeInt32(atInt32 val)
{
if (m_endian == BigEndian)
utility::BigInt32(val);
else
utility::LittleInt32(val);
writeUBytes((atUint8*)&val, 4);
}
/*! \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
*/
inline void writeUint32(atUint32 val) {writeInt32(val);}
/*! \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
*/
inline void writeInt64(atInt64 val)
{
if (m_endian == BigEndian)
utility::BigInt64(val);
else
utility::LittleInt64(val);
writeUBytes((atUint8*)&val, 8);
}
/*! \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
*/
inline void writeUint64(atUint64 val) {writeInt64(val);}
/*! \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
*/
inline void writeFloat(float val)
{
if (m_endian == BigEndian)
utility::BigFloat(val);
else
utility::LittleFloat(val);
writeUBytes((atUint8*)&val, 4);
}
/*! \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
*/
inline void writeDouble(double val)
{
if (m_endian == BigEndian)
utility::BigDouble(val);
else
utility::LittleDouble(val);
writeUBytes((atUint8*)&val, 8);
}
/*! \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
*/
inline void writeBool(bool val) {writeUBytes((atUint8*)&val, 1);}
/*! \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
*/
inline void writeVec3f(atVec3f vec)
{
if (m_endian == BigEndian)
{
utility::BigFloat(vec.vec[0]);
utility::BigFloat(vec.vec[1]);
utility::BigFloat(vec.vec[2]);
}
else
{
utility::LittleFloat(vec.vec[0]);
utility::LittleFloat(vec.vec[1]);
utility::LittleFloat(vec.vec[2]);
}
writeUBytes((atUint8*)&vec, 12);
}
/*! \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
*/
inline void writeVec4f(atVec4f vec)
{
if (m_endian == BigEndian)
{
utility::BigFloat(vec.vec[0]);
utility::BigFloat(vec.vec[1]);
utility::BigFloat(vec.vec[2]);
utility::BigFloat(vec.vec[3]);
}
else
{
utility::LittleFloat(vec.vec[0]);
utility::LittleFloat(vec.vec[1]);
utility::LittleFloat(vec.vec[2]);
utility::LittleFloat(vec.vec[3]);
}
writeUBytes((atUint8*)&vec, 16);
}
/*! \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
* \param fixedLen If not -1, the number of characters to zero-fill string to
*/
inline void writeUnicode(const std::string& str, atInt32 fixedLen = -1)
{
std::string tmpStr = "\xEF\xBB\xBF" + str;
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
std::wstring tmp = conv.from_bytes(tmpStr);
if (fixedLen < 0)
{
for (atUint16 chr : tmp)
{
if (chr != 0xFEFF)
writeUint16(chr);
}
writeUint16(0);
}
else
{
auto it = tmp.begin();
for (atInt32 i=0 ; i<fixedLen ; ++i)
{
atUint16 chr;
if (it == tmp.end())
chr = 0;
else
chr = *it++;
if (chr == 0xFEFF)
{
--i;
continue;
}
writeUint16(chr);
}
}
}
/*! \brief Writes an string to the buffer and advances the buffer.
*
* \sa Endian
* \param str The string to write to the buffer
* \param fixedLen If not -1, the number of characters to zero-fill string to
*/
inline void writeString(const std::string& str, atInt32 fixedLen = -1)
{
if (fixedLen < 0)
{
for (atUint8 c : str)
{
writeUByte(c);
if (c == '\0')
break;
}
writeUByte(0);
}
else
{
auto it = str.begin();
for (atInt32 i=0 ; i<fixedLen ; ++i)
{
atUint8 chr;
if (it == str.end())
chr = 0;
else
chr = *it++;
writeUByte(chr);
}
}
}
/*! \brief Writes an wstring to the buffer and advances the buffer.
*
* \sa Endian
* \param str The string to write to the buffer
* \param fixedLen If not -1, the number of characters to zero-fill string to
*/
inline void writeWString(const std::wstring& str, atInt32 fixedLen = -1)
{
if (fixedLen < 0)
{
for (atUint16 c : str)
{
writeUint16(c);
if (c == L'\0')
break;
}
writeUint16(0);
}
else
{
auto it = str.begin();
for (atInt32 i=0 ; i<fixedLen ; ++i)
{
atUint16 chr;
if (it == str.end())
chr = 0;
else
chr = *it++;
writeUint16(chr);
}
}
}
inline void fill(atUint8 val, atUint64 length)
{for (atUint64 l=0 ; l<length ; ++l) writeUBytes(&val, 1);}
inline void fill(atInt8 val, atUint64 length)
{fill((atUint8)val, length);}
protected:
Endian m_endian;
};
}
}

View File

@ -9,8 +9,8 @@ namespace Athena
{
namespace io
{
/*! \class BinaryReader
* \brief A Stream class for reading binary data
/*! \class MemoryReader
* \brief A Stream class for reading data from a memory position
*
* 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
@ -20,6 +20,8 @@ namespace io
class MemoryReader : public IStreamReader
{
public:
~MemoryReader();
/*! \brief This constructor takes an existing buffer to read from.
*
* \param data The existing buffer
@ -31,45 +33,7 @@ public:
*
* \param filename The file to create the stream from
*/
MemoryReader(const std::string& filename, std::function<void(int)> progressFun = nullptr);
virtual ~MemoryReader();
/*! \brief Sets the Endianss of the stream
*
* \param endian The Endianess to set \sa Endian
*/
inline void setEndian(Endian endian)
{m_endian = endian;}
/*! \brief Returns the current Endianness of the stream
*
* \return Endian The current Stream Endianess
*/
inline Endian endian() const
{return m_endian;}
/*! \brief Returns whether the stream is BigEndian
*
* \return bool True for BigEndian; False for LittleEndian
*/
inline bool isBigEndian() const
{return (m_endian == Endian::BigEndian);}
/*! \brief Returns whether the stream is LittleEndian
*
* \return bool True for LittleEndian; False for BigEndian
*/
inline bool isLittleEndian()const
{return (m_endian == Endian::LittleEndian);}
/*! \brief Retuns whether or not the Stream is open.
*
* \return True if open; False otherwise.
*/
inline bool isOpen() const
{return m_data != nullptr;}
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.
@ -78,17 +42,6 @@ public:
*/
void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
/*! \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);}
/*! \brief Returns whether or not the stream is at the end.
*
* \return bool True if at end; False otherwise.
*/
inline bool atEnd() const
{return m_position >= m_length;}
/*! \brief Returns the current position in the stream.
*
* \return Int64 The current position in the stream.
@ -113,7 +66,7 @@ public:
* \param length The length of the new buffer.
* \throw IOException
*/
void setData(const atUint8* data, atUint64 length);
void setData(const atUint8* data, atUint64 length);
/*! \brief Returns a copy of the current buffer.<br />
@ -125,199 +78,14 @@ public:
*/
atUint8* data() const;
/*! \brief Sets the target file
*
* \sa Endian
* \param filepath The path to write to.
*/
inline void setFilepath(const std::string& filepath)
{m_filepath = filepath;}
/*! \brief Returns the target file
*
*/
inline std::string filepath() const
{return m_filepath;}
/*!
* \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
*/
bool readBit();
/*! \brief Reads a byte at the current position and advances the current position
*
* \return Int8 The value at the current position
*/
atInt8 readByte();
/*! \brief Reads a byte at the current position and advances the current position
*
* \return Uint8 The value at the current position
*/
atUint8 readUByte();
/*! \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.
*/
inline atInt8* readBytes(atUint64 length) {return (atInt8*)readUBytes(length);}
/*! \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.
*/
atUint8* readUBytes(atUint64 length);
atUint64 readBytesToBuf(void* buf, atUint64 len) {return readUBytesToBuf(buf, len);}
atUint64 readUBytesToBuf(void* buf, atUint64 len);
/*! \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
*/
atInt16 readInt16();
/*! \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
*/
inline atUint16 readUint16()
{return MemoryReader::readInt16();}
/*! \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
*/
atInt32 readInt32();
/*! \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
*/
inline atUint32 readUint32()
{return MemoryReader::readInt32();}
/*! \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
*/
atInt64 readInt64();
/*! \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
*/
inline atUint64 readUint64()
{return MemoryReader::readInt64();}
/*! \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
*/
float readFloat();
/*! \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
*/
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 an atVec3f (12 bytes) and advances the current position
*
* \return atVec3f The value at the current address
* \throw IOException when address is out of range
*/
atVec3f readVec3f();
/*! \brief Reads an atVec4f (16 bytes) and advances the current position
*
* \return atVec4f The value at the current address
* \throw IOException when address is out of range
*/
atVec4f readVec4f();
/*! \brief Reads a Unicode string and advances the position in the file
*
* \param fixedLen If non-negative, this is a fixed-length string read
* \return std::string The value at the current address
* \throw IOException when address is out of range
*/
std::string readUnicode(atInt32 fixedLen = -1);
/*! \brief Reads a string and advances the position in the file
*
* \param fixedLen If non-negative, this is a fixed-length string read
* \return std::string The value at the current address
* \throw IOException when address is out of range
*/
std::string readString(atInt32 fixedLen = -1);
/*! \brief Reads a wstring and advances the position in the file
*
* \param fixedLen If non-negative, this is a fixed-length string read
* \return std::wstring The value at the current address
* \throw IOException when address is out of range
*/
std::wstring readWString(atInt32 fixedLen = -1);
inline void setProgressCallback(std::function<void(int)> cb)
{m_progressCallback = cb;}
protected:
void loadData();
atUint8* m_data;
atUint64 m_length;
std::string m_filepath; //!< Path to the target file
std::string m_filepath; //!< Path to the target file
atUint64 m_position;
atUint64 m_bitPosition;
Endian m_endian;
std::function<void(int)> m_progressCallback;
};
} // io

View File

@ -10,8 +10,8 @@ namespace Athena
namespace io
{
/*! \class BinaryWriter
* \brief A Stream class for writing binary data
/*! \class MemoryWriter
* \brief A Stream class for writing data to a memory position
*
* 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
@ -21,6 +21,8 @@ namespace io
class MemoryWriter : public IStreamWriter
{
public:
~MemoryWriter();
/*! \brief This constructor takes an existing buffer to write to.
*
* \param data The existing buffer
@ -34,43 +36,6 @@ public:
*/
MemoryWriter(const std::string& filename, std::function<void(int)> progressFun = nullptr);
virtual ~MemoryWriter();
/*! \brief Sets the Endianss of the stream
*
* \param endian The Endianess to set \sa Endian
*/
inline void setEndian(Endian endian)
{m_endian = endian;}
/*! \brief Returns the current Endianness of the stream
*
* \return Endian The current Stream Endianess
*/
inline Endian endian() const
{return m_endian;}
/*! \brief Returns whether the stream is BigEndian
*
* \return bool True for BigEndian; False for LittleEndian
*/
inline bool isBigEndian() const
{return (m_endian == Endian::BigEndian);}
/*! \brief Returns whether the stream is LittleEndian
*
* \return bool True for LittleEndian; False for BigEndian
*/
inline bool isLittleEndian() const
{return (m_endian == Endian::LittleEndian);}
/*! \brief Retuns whether or not the Stream is open.
*
* \return True if open; False otherwise.
*/
inline bool isOpen() const
{return m_data != nullptr;}
/*! \brief Sets the buffers position relative to the specified position.<br />
* It seeks relative to the current position by default.
@ -79,18 +44,6 @@ public:
*/
void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
/*! \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);}
/*! \brief Returns whether or not the stream is at the end.
*
* \return bool True if at end; False otherwise.
*/
inline bool atEnd() const
{return m_position >= m_length;}
/*! \brief Returns the current position in the stream.
*
@ -106,6 +59,8 @@ public:
inline atUint64 length() const
{return m_length;}
inline bool isOpen() const {return true;}
/*! \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 />