* Nearly done with the documentation

* Changed BinaryReader/Writer to read and write in 512 byte chunks by
default
This commit is contained in:
Antidote 2013-01-26 16:13:19 -08:00
parent ad0505a501
commit 506556e1ae
6 changed files with 209 additions and 27 deletions

View File

@ -4,6 +4,13 @@
#include "Stream.hpp" #include "Stream.hpp"
#include <string> #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 class BinaryWriter : public Stream
{ {
public: public:

View File

@ -18,6 +18,7 @@
class Stream class Stream
{ {
public: public:
static const Uint32 BLOCKSZ;
/*! \enum Endian /*! \enum Endian
* \brief Allows the user to specify the Endianness of the stream buffer.<br /> * \brief Allows the user to specify the Endianness of the stream buffer.<br />
@ -46,7 +47,7 @@ public:
Stream(); Stream();
/*! \brief This constructor takes an existing buffer to read from. /*! \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 * \param length The length of the existing buffer
*/ */
Stream(const Uint8* bytes, Uint64 length); Stream(const Uint8* bytes, Uint64 length);
@ -120,7 +121,9 @@ public:
/*! \brief Sets the buffer to the given one, deleting the current one.<br /> /*! \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 * <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 data The new buffer.
* \param length The length of the new buffer. * \param length The length of the new buffer.
* \throw IOException * \throw IOException
@ -133,31 +136,85 @@ public:
* directly edit the buffer and use setData to set the new information.<br /> * 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 * 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. * 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; Uint8* data() const;
/*! \brief Returns the length of the Stream. /*! \brief Returns the length of the Stream.
*
* \return Int64 The length of the stream.
*/
Int64 length(); Int64 length();
/*! \brief Returns the current position in the stream.
*
* \return Int64 The current position in the stream.
*/
Int64 position(); Int64 position();
/*! \brief Returns whether or not the stream is at the end.
*
* \return bool True if at end; False otherwise.
*/
bool atEnd(); 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); 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(); 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(); virtual bool isOpenForWriting();
/*! \brief Sets the Endianss of the stream
*
* \param endian The Endianess to set \sa Endian
*/
void setEndianess(Endian endian); void setEndianess(Endian endian);
/*! \brief Returns the current Endianness of the stream
*
* \return Endian The current Stream Endianess
*/
Endian endianness() const; Endian endianness() const;
/*! \brief Returns whether the stream is BigEndian
*
* \return bool True for BigEndian; False for LittleEndian
*/
bool isBigEndian() const; bool isBigEndian() const;
/*! \brief Returns whether the stream is LittleEndian
*
* \return bool True for LittleEndian; False for BigEndian
*/
bool isLittleEndian() const; bool isLittleEndian() const;
protected: protected:
Uint32 m_bitPosition; Uint32 m_bitPosition; //!< The current position in the current byte
Uint64 m_position; Uint64 m_position; //!< The current position in the Stream
Uint64 m_length; Uint64 m_length; //!< The length of the Stream
Endian m_endian; Endian m_endian; //!< The Endianess of the Stream
Uint8* m_data; Uint8* m_data; //!< The Stream buffer
bool m_autoResize; bool m_autoResize; //!< Whether the stream is autoresizing
}; };
#endif // __STREAM_HPP__ #endif // __STREAM_HPP__

View File

@ -78,3 +78,83 @@
<string.h> <string.h>
<stdlib.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>

View File

@ -7,6 +7,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <vector> #include <vector>
#include <iostream>
BinaryReader::BinaryReader(const Stream& stream) : BinaryReader::BinaryReader(const Stream& stream) :
Stream(stream) Stream(stream)
@ -21,8 +22,7 @@ BinaryReader::BinaryReader(const Uint8* data, Uint64 length) :
} }
BinaryReader::BinaryReader(const std::string& filename) BinaryReader::BinaryReader(const std::string& filename)
: m_bitPosition(0), : m_filename(filename)
m_filename(filename)
{ {
Stream::setAutoResizing(false); Stream::setAutoResizing(false);
FILE* in; FILE* in;
@ -37,7 +37,24 @@ BinaryReader::BinaryReader(const std::string& filename)
fseek(in, 0, SEEK_SET); fseek(in, 0, SEEK_SET);
m_data = new Uint8[length]; m_data = new Uint8[length];
fread(m_data, 1, length, in); 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);
fclose(in); fclose(in);
m_length = length; m_length = length;
m_position = 0; m_position = 0;

View File

@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
BinaryWriter::BinaryWriter(const Uint8* data, Uint64 length) BinaryWriter::BinaryWriter(const Uint8* data, Uint64 length)
: Stream(data, length) : Stream(data, length)
{} {}
@ -41,7 +42,24 @@ void BinaryWriter::save(const std::string& filename)
if (!out) if (!out)
throw FileNotFoundException(m_filename); throw FileNotFoundException(m_filename);
fwrite(m_data, 1, m_length, out); 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);
fclose(out); fclose(out);
} }

View File

@ -4,6 +4,9 @@
#include <string.h> #include <string.h>
#include <sstream> #include <sstream>
const Uint32 Stream::BLOCKSZ = 512;
Stream::Stream() : Stream::Stream() :
m_bitPosition(0), m_bitPosition(0),
m_position(0), m_position(0),