mirror of https://github.com/libAthena/athena.git
* Nearly done with the documentation
* Changed BinaryReader/Writer to read and write in 512 byte chunks by default
This commit is contained in:
parent
ad0505a501
commit
506556e1ae
|
@ -3,7 +3,14 @@
|
|||
|
||||
#include "Stream.hpp"
|
||||
#include <string>
|
||||
|
||||
|
||||
/*! \class BinaryWriter
|
||||
*
|
||||
* 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
|
||||
* this allows for fast, flexible code as well as the ability to quickly modify data
|
||||
* \sa Stream
|
||||
*/
|
||||
class BinaryWriter : public Stream
|
||||
{
|
||||
public:
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
class Stream
|
||||
{
|
||||
public:
|
||||
static const Uint32 BLOCKSZ;
|
||||
|
||||
/*! \enum Endian
|
||||
* \brief Allows the user to specify the Endianness of the stream buffer.<br />
|
||||
|
@ -46,7 +47,7 @@ public:
|
|||
Stream();
|
||||
/*! \brief This constructor takes an existing buffer to read from.
|
||||
*
|
||||
* \param data The existing buffer
|
||||
* \param bytes The existing buffer
|
||||
* \param length The length of the existing buffer
|
||||
*/
|
||||
Stream(const Uint8* bytes, Uint64 length);
|
||||
|
@ -120,7 +121,9 @@ public:
|
|||
|
||||
/*! \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.
|
||||
* 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
|
||||
|
@ -133,31 +136,85 @@ public:
|
|||
* 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
|
||||
* \return Uint8* The copy of the buffer.
|
||||
*/
|
||||
Uint8* data() const;
|
||||
|
||||
/*! \brief Returns the length of the Stream.
|
||||
Int64 length();
|
||||
Int64 position();
|
||||
bool atEnd();
|
||||
void setAutoResizing(bool val);
|
||||
/*! \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;
|
||||
|
||||
virtual bool isOpenForReading();
|
||||
|
||||
|
||||
/*! \brief Retuns whether or not the Stream is open for reading.
|
||||
*
|
||||
* \return True if open for reading; False otherwise.
|
||||
*/
|
||||
virtual bool isOpenForReading();
|
||||
|
||||
/*! \brief Retuns whether or not the Stream is open for writing
|
||||
*
|
||||
* \return True if open for writing; False otherwise.
|
||||
*/
|
||||
virtual bool isOpenForWriting();
|
||||
|
||||
/*! \brief Sets the Endianss of the stream
|
||||
*
|
||||
* \param endian The Endianess to set \sa Endian
|
||||
*/
|
||||
void setEndianess(Endian endian);
|
||||
|
||||
/*! \brief Returns the current Endianness of the stream
|
||||
*
|
||||
* \return Endian The current Stream Endianess
|
||||
*/
|
||||
Endian endianness() const;
|
||||
|
||||
|
||||
/*! \brief Returns whether the stream is BigEndian
|
||||
*
|
||||
* \return bool True for BigEndian; False for LittleEndian
|
||||
*/
|
||||
bool isBigEndian() const;
|
||||
bool isLittleEndian() const;
|
||||
|
||||
/*! \brief Returns whether the stream is LittleEndian
|
||||
*
|
||||
* \return bool True for LittleEndian; False for BigEndian
|
||||
*/
|
||||
bool isLittleEndian() const;
|
||||
|
||||
protected:
|
||||
Uint32 m_bitPosition;
|
||||
Uint64 m_position;
|
||||
Uint64 m_length;
|
||||
Endian m_endian;
|
||||
Uint8* m_data;
|
||||
bool m_autoResize;
|
||||
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__
|
||||
|
|
|
@ -78,3 +78,83 @@
|
|||
<string.h>
|
||||
<stdlib.h>
|
||||
|
||||
1359243487 source:/home/antidote/wiiking2_editor/libzelda/src/BinaryWriter.cpp
|
||||
"BinaryWriter.hpp"
|
||||
"IOException.hpp"
|
||||
"FileNotFoundException.hpp"
|
||||
"utility.hpp"
|
||||
"utf8.h"
|
||||
<stdio.h>
|
||||
<string.h>
|
||||
<vector>
|
||||
<iostream>
|
||||
|
||||
1359245518 /home/antidote/wiiking2_editor/libzelda/include/BinaryWriter.hpp
|
||||
"Stream.hpp"
|
||||
<string>
|
||||
|
||||
1359243334 /home/antidote/wiiking2_editor/libzelda/include/Stream.hpp
|
||||
"Types.hpp"
|
||||
|
||||
1358872986 /home/antidote/wiiking2_editor/libzelda/include/Types.hpp
|
||||
<limits.h>
|
||||
|
||||
1358872986 /home/antidote/wiiking2_editor/libzelda/include/IOException.hpp
|
||||
"Exception.hpp"
|
||||
|
||||
1358872986 /home/antidote/wiiking2_editor/libzelda/include/Exception.hpp
|
||||
<string>
|
||||
|
||||
1358872986 /home/antidote/wiiking2_editor/libzelda/include/FileNotFoundException.hpp
|
||||
"Exception.hpp"
|
||||
|
||||
1359171946 /home/antidote/wiiking2_editor/libzelda/include/utility.hpp
|
||||
<string>
|
||||
"Types.hpp"
|
||||
|
||||
1358872986 /home/antidote/wiiking2_editor/libzelda/include/utf8.h
|
||||
"utf8/checked.h"
|
||||
"utf8/unchecked.h"
|
||||
|
||||
1358872986 /home/antidote/wiiking2_editor/libzelda/include/utf8/checked.h
|
||||
"core.h"
|
||||
<stdexcept>
|
||||
|
||||
1358872986 /home/antidote/wiiking2_editor/libzelda/include/utf8/core.h
|
||||
<iterator>
|
||||
|
||||
1358872986 /home/antidote/wiiking2_editor/libzelda/include/utf8/unchecked.h
|
||||
"core.h"
|
||||
|
||||
1359243354 source:/home/antidote/wiiking2_editor/libzelda/src/Stream.cpp
|
||||
"Stream.hpp"
|
||||
"IOException.hpp"
|
||||
"InvalidOperationException.hpp"
|
||||
<string.h>
|
||||
<sstream>
|
||||
|
||||
1358872986 /home/antidote/wiiking2_editor/libzelda/include/InvalidOperationException.hpp
|
||||
<string>
|
||||
<Exception.hpp>
|
||||
|
||||
1359219210 source:/home/antidote/wiiking2_editor/libzelda/src/utility.cpp
|
||||
"utility.hpp"
|
||||
<iostream>
|
||||
<string.h>
|
||||
<stdlib.h>
|
||||
|
||||
1359243404 source:/home/antidote/wiiking2_editor/libzelda/src/BinaryReader.cpp
|
||||
"BinaryReader.hpp"
|
||||
"IOException.hpp"
|
||||
"FileNotFoundException.hpp"
|
||||
"utility.hpp"
|
||||
"utf8.h"
|
||||
<stdio.h>
|
||||
<stdlib.h>
|
||||
<vector>
|
||||
<iostream>
|
||||
|
||||
1359220647 /home/antidote/wiiking2_editor/libzelda/include/BinaryReader.hpp
|
||||
"Stream.hpp"
|
||||
<string>
|
||||
|
||||
|
|
|
@ -6,7 +6,8 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
BinaryReader::BinaryReader(const Stream& stream) :
|
||||
Stream(stream)
|
||||
|
@ -21,9 +22,8 @@ BinaryReader::BinaryReader(const Uint8* data, Uint64 length) :
|
|||
}
|
||||
|
||||
BinaryReader::BinaryReader(const std::string& filename)
|
||||
: m_bitPosition(0),
|
||||
m_filename(filename)
|
||||
{
|
||||
: m_filename(filename)
|
||||
{
|
||||
Stream::setAutoResizing(false);
|
||||
FILE* in;
|
||||
int length;
|
||||
|
@ -35,9 +35,26 @@ BinaryReader::BinaryReader(const std::string& filename)
|
|||
fseek(in, 0, SEEK_END);
|
||||
length = ftell(in);
|
||||
fseek(in, 0, SEEK_SET);
|
||||
m_data = new Uint8[length];
|
||||
m_data = new Uint8[length];
|
||||
|
||||
Uint32 done = 0;
|
||||
Uint32 blocksize = BLOCKSZ;
|
||||
do
|
||||
{
|
||||
if (blocksize > length - done)
|
||||
blocksize = length - done;
|
||||
|
||||
Int32 ret = fread(m_data + done, 1, blocksize, in);
|
||||
|
||||
if (ret < 0)
|
||||
throw IOException("Error readin data from disk");
|
||||
else if (ret == 0)
|
||||
break;
|
||||
|
||||
done += blocksize;
|
||||
std::cout << "Read " << done << " bytes" << std::endl;
|
||||
}while (done < m_length);
|
||||
|
||||
fread(m_data, 1, length, in);
|
||||
fclose(in);
|
||||
m_length = length;
|
||||
m_position = 0;
|
||||
|
|
|
@ -7,7 +7,8 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
BinaryWriter::BinaryWriter(const Uint8* data, Uint64 length)
|
||||
: Stream(data, length)
|
||||
|
@ -39,9 +40,26 @@ void BinaryWriter::save(const std::string& filename)
|
|||
|
||||
FILE* out = fopen(m_filename.c_str(), "wb");
|
||||
if (!out)
|
||||
throw FileNotFoundException(m_filename);
|
||||
throw FileNotFoundException(m_filename);
|
||||
|
||||
Uint32 done = 0;
|
||||
Uint32 blocksize = BLOCKSZ;
|
||||
do
|
||||
{
|
||||
if (blocksize > m_length - done)
|
||||
blocksize = m_length - done;
|
||||
|
||||
Int32 ret = fwrite(m_data + done, 1, blocksize, out);
|
||||
|
||||
if (ret < 0)
|
||||
throw IOException("Error writing data to disk");
|
||||
else if (ret == 0)
|
||||
break;
|
||||
|
||||
done += blocksize;
|
||||
std::cout << "Wrote " << done << " bytes" << std::endl;
|
||||
}while (done < m_length);
|
||||
|
||||
fwrite(m_data, 1, m_length, out);
|
||||
fclose(out);
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
#include "InvalidOperationException.hpp"
|
||||
#include <string.h>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
const Uint32 Stream::BLOCKSZ = 512;
|
||||
|
||||
Stream::Stream() :
|
||||
m_bitPosition(0),
|
||||
|
|
Loading…
Reference in New Issue