Global error option flag for MemoryReader

This commit is contained in:
Jack Andersen 2016-08-08 08:22:03 -10:00
parent 750d23c686
commit b43c4af3a7
2 changed files with 25 additions and 13 deletions

View File

@ -31,7 +31,7 @@ public:
* \param length The length of the existing buffer
* \param takeOwnership Memory will be freed with the reader if set
*/
MemoryReader(const void* data, atUint64 length, bool takeOwnership=false);
MemoryReader(const void* data, atUint64 length, bool takeOwnership=false, bool globalErr=true);
/*! \brief Sets the buffers position relative to the specified position.<br />
* It seeks relative to the current position by default.
@ -89,6 +89,7 @@ protected:
atUint64 m_length = 0;
atUint64 m_position = 0;
bool m_owns = false;
bool m_globalErr = true;
};
class MemoryCopyReader : public MemoryReader

View File

@ -14,14 +14,16 @@ namespace athena
{
namespace io
{
MemoryReader::MemoryReader(const void* data, atUint64 length, bool takeOwnership)
MemoryReader::MemoryReader(const void* data, atUint64 length, bool takeOwnership, bool globalErr)
: m_data(data),
m_length(length),
m_position(0),
m_owns(takeOwnership)
m_owns(takeOwnership),
m_globalErr(globalErr)
{
if (!data)
{
if (m_globalErr)
atError("data cannot be NULL");
setError();
return;
@ -29,6 +31,7 @@ MemoryReader::MemoryReader(const void* data, atUint64 length, bool takeOwnership
if (length == 0)
{
if (m_globalErr)
atError("length cannot be 0");
setError();
return;
@ -46,6 +49,7 @@ MemoryCopyReader::MemoryCopyReader(const void* data, atUint64 length)
{
if (!data)
{
if (m_globalErr)
atError("data cannot be NULL");
setError();
return;
@ -53,6 +57,7 @@ MemoryCopyReader::MemoryCopyReader(const void* data, atUint64 length)
if (length == 0)
{
if (m_globalErr)
atError("length cannot be 0");
setError();
return;
@ -70,6 +75,7 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin)
case SeekOrigin::Begin:
if ((position < 0 || (atInt64)position > (atInt64)m_length))
{
if (m_globalErr)
atError("Position %0.8X outside stream bounds ", position);
setError();
return;
@ -81,6 +87,7 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin)
case SeekOrigin::Current:
if ((((atInt64)m_position + position) < 0 || (m_position + position) > m_length))
{
if (m_globalErr)
atError("Position %0.8X outside stream bounds ", position);
setError();
return;
@ -92,6 +99,7 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin)
case SeekOrigin::End:
if ((((atInt64)m_length - position < 0) || (m_length - position) > m_length))
{
if (m_globalErr)
atError("Position %0.8X outside stream bounds ", position);
setError();
return;
@ -133,6 +141,7 @@ atUint64 MemoryReader::readUBytesToBuf(void* buf, atUint64 length)
{
if (m_position + length > m_length)
{
if (m_globalErr)
atError("Position %0.8X outside stream bounds ", m_position);
setError();
return 0;
@ -151,6 +160,7 @@ void MemoryCopyReader::loadData()
if (!in)
{
if (m_globalErr)
atError("Unable to open file '%s'", m_filepath.c_str());
setError();
return;
@ -174,6 +184,7 @@ void MemoryCopyReader::loadData()
if (ret < 0)
{
if (m_globalErr)
atError("Error reading data from disk");
setError();
return;