* Added progress callback to Binary reader

This commit is contained in:
Antidote 2014-02-22 00:25:29 -08:00
parent 9c38e96403
commit ebca406d71
3 changed files with 125 additions and 31 deletions

View File

@ -18,6 +18,7 @@
#include "Stream.hpp" #include "Stream.hpp"
#include <string> #include <string>
#include <functional>
namespace zelda namespace zelda
{ {
@ -64,6 +65,12 @@ public:
*/ */
std::string filepath() const; std::string filepath() const;
bool readBit();
Int8 readByte();
Uint8 readUByte();
Int8* readBytes(Int64 length);
Uint8* readUBytes(Int64 length);
/*! \brief Reads a Int16 and swaps to proper endianness depending on platform /*! \brief Reads a Int16 and swaps to proper endianness depending on platform
* and Stream settings, and advances the current position * and Stream settings, and advances the current position
* *
@ -163,7 +170,9 @@ public:
*/ */
std::string readString(); std::string readString();
void setProgressCallback(std::function<void(int)> cb);
protected: protected:
void loadData();
/*! \brief Overload of isOpenForWriting in Stream /*! \brief Overload of isOpenForWriting in Stream
* *
* \return false * \return false
@ -180,6 +189,9 @@ protected:
*/ */
void writeBytes(Int8*, Int64); void writeBytes(Int8*, Int64);
std::string m_filepath; //!< Path to the target file std::string m_filepath; //!< Path to the target file
Uint32 m_currentLength;
FILE* m_file;
std::function<void(int)> m_progressCallback;
}; };
} }
} }

View File

@ -49,43 +49,46 @@ BinaryReader::BinaryReader(const std::string& filename)
: m_filepath(filename) : m_filepath(filename)
{ {
Stream::setAutoResizing(false); Stream::setAutoResizing(false);
FILE* in; }
Uint32 length;
in = fopen(filename.c_str(), "rb");
if (!in) bool BinaryReader::readBit()
throw error::FileNotFoundException(filename); {
if (!m_data)
loadData();
fseek(in, 0, SEEK_END); return Stream::readBit();
length = ftell(in); }
fseek(in, 0, SEEK_SET);
#ifdef HW_RVL
m_data = (Uint8*)memalign(32, length);
#else
m_data = new Uint8[length];
#endif
Uint32 done = 0; Int8 BinaryReader::readByte()
Uint32 blocksize = BLOCKSZ; {
do if (!m_data)
{ loadData();
if (blocksize > length - done)
blocksize = length - done;
Int32 ret = fread(m_data + done, 1, blocksize, in); return Stream::readByte();
}
if (ret < 0) Uint8 BinaryReader::readUByte()
throw error::IOException("BinaryReader::BinaryReader -> reading data from disk"); {
else if (ret == 0) if (!m_data)
break; loadData();
done += blocksize; return Stream::readUByte();
} while (done < length); }
fclose(in); Int8* BinaryReader::readBytes(Int64 length)
m_length = length; {
m_position = 0; if (!m_data)
m_bitPosition = 0; loadData();
return Stream::readBytes(length);
}
Uint8* BinaryReader::readUBytes(Int64 length)
{
if (!m_data)
loadData();
return Stream::readUBytes(length);
} }
void BinaryReader::writeByte(Int8) void BinaryReader::writeByte(Int8)
@ -100,6 +103,9 @@ void BinaryReader::writeBytes(Int8*, Int64)
Int16 BinaryReader::readInt16() Int16 BinaryReader::readInt16()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -118,6 +124,9 @@ Int16 BinaryReader::readInt16()
Uint16 BinaryReader::readUInt16() Uint16 BinaryReader::readUInt16()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -136,6 +145,9 @@ Uint16 BinaryReader::readUInt16()
Int32 BinaryReader::readInt32() Int32 BinaryReader::readInt32()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -153,6 +165,9 @@ Int32 BinaryReader::readInt32()
Uint32 BinaryReader::readUInt32() Uint32 BinaryReader::readUInt32()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -171,6 +186,9 @@ Uint32 BinaryReader::readUInt32()
Int64 BinaryReader::readInt64() Int64 BinaryReader::readInt64()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -189,6 +207,9 @@ Int64 BinaryReader::readInt64()
Uint64 BinaryReader::readUInt64() Uint64 BinaryReader::readUInt64()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -206,6 +227,9 @@ Uint64 BinaryReader::readUInt64()
float BinaryReader::readFloat() float BinaryReader::readFloat()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -224,6 +248,9 @@ float BinaryReader::readFloat()
double BinaryReader::readDouble() double BinaryReader::readDouble()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -243,6 +270,9 @@ double BinaryReader::readDouble()
bool BinaryReader::readBool() bool BinaryReader::readBool()
{ {
if (!m_data)
loadData();
if (m_bitPosition > 0) if (m_bitPosition > 0)
{ {
m_bitPosition = 0; m_bitPosition = 0;
@ -258,6 +288,8 @@ bool BinaryReader::readBool()
std::string BinaryReader::readUnicode() std::string BinaryReader::readUnicode()
{ {
if (!m_data)
loadData();
std::string ret; std::string ret;
std::vector<short> tmp; std::vector<short> tmp;
@ -288,6 +320,56 @@ std::string BinaryReader::readString()
return ret; return ret;
} }
void BinaryReader::setProgressCallback(std::function<void (int)> cb)
{
m_progressCallback = cb;
}
void BinaryReader::loadData()
{
FILE* in;
Uint32 length;
in = fopen(m_filepath.c_str(), "rb");
if (!in)
throw error::FileNotFoundException(m_filepath);
fseek(in, 0, SEEK_END);
length = ftell(in);
fseek(in, 0, SEEK_SET);
#ifdef HW_RVL
m_data = (Uint8*)memalign(32, length);
#else
m_data = new Uint8[length];
#endif
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 error::IOException("BinaryReader::BinaryReader -> reading data from disk");
else if (ret == 0)
break;
done += ret;
if (m_progressCallback)
m_progressCallback((int)((float)(done* 100.f)/length));
} while (done < length);
fclose(in);
m_length = length;
m_position = 0;
m_bitPosition = 0;
}
bool BinaryReader::isOpenForWriting() bool BinaryReader::isOpenForWriting()
{ {
return false; return false;

View File

@ -28,7 +28,7 @@ namespace zelda
namespace io namespace io
{ {
const Uint32 Stream::BLOCKSZ = 512; const Uint32 Stream::BLOCKSZ = (32*1024);
Stream::Stream() : Stream::Stream() :
m_bitPosition(0), m_bitPosition(0),