Add error state

This commit is contained in:
Phillip Stephens 2015-07-22 13:40:22 -07:00
parent 8ea5071e0f
commit d9c4d3b79b
5 changed files with 46 additions and 0 deletions

View File

@ -12,6 +12,7 @@ std::ostream& operator<<(std::ostream& os, Endian& endian);
class IStream
{
public:
IStream() : m_hasError(false) {}
virtual ~IStream() {}
virtual void setEndian(Endian) = 0;
@ -22,6 +23,10 @@ public:
virtual bool atEnd() const = 0;
virtual atUint64 position() const = 0;
virtual atUint64 length() const = 0;
bool hasError() const { return m_hasError; }
protected:
void setError() { m_hasError = true; }
bool m_hasError;
};
}
}

View File

@ -33,6 +33,7 @@ void FileReader::open()
if (!m_fileHandle)
{
atError("File not found '%s'", m_filename.c_str());
setError();
return;
}
@ -45,6 +46,7 @@ void FileReader::close()
if (!m_fileHandle)
{
atError("Cannot close an unopened stream");
setError();
return;
}
@ -120,6 +122,7 @@ atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len)
if (!isOpen())
{
atError("File not open for reading");
setError();
return 0;
}

View File

@ -34,6 +34,7 @@ void FileWriter::open(bool overwrite)
if (!m_fileHandle)
{
atError("Unable to open file '%s'", m_filename.c_str());
setError();
return;
}
@ -46,6 +47,7 @@ void FileWriter::close()
if (!m_fileHandle)
{
atError("Cannot close an unopened stream");
setError();
return;
}
@ -57,7 +59,10 @@ void FileWriter::close()
void FileWriter::seek(atInt64 pos, SeekOrigin origin)
{
if (fseeko64(m_fileHandle, pos, (int)origin) != 0)
{
atError("Unable to seek in file");
setError();
}
}
atUint64 FileWriter::position() const
@ -75,11 +80,15 @@ void FileWriter::writeUBytes(const atUint8* data, atUint64 len)
if (!isOpen())
{
atError("File not open for writing");
setError();
return;
}
if (fwrite(data, 1, len, m_fileHandle) != len)
{
atError("Unable to write to stream");
setError();
}
}
}

View File

@ -22,12 +22,14 @@ MemoryReader::MemoryReader(const atUint8* data, atUint64 length)
if (!data)
{
atError("data cannot be NULL");
setError();
return;
}
if (length == 0)
{
atError("length cannot be 0");
setError();
return;
}
}
@ -38,12 +40,14 @@ MemoryCopyReader::MemoryCopyReader(const atUint8* data, atUint64 length)
if (!data)
{
atError("data cannot be NULL");
setError();
return;
}
if (length == 0)
{
atError("length cannot be 0");
setError();
return;
}
@ -60,6 +64,7 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin)
if ((position < 0 || (atInt64)position > (atInt64)m_length))
{
atError("Position %0.8X outside stream bounds ", position);
setError();
return;
}
@ -70,6 +75,7 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin)
if ((((atInt64)m_position + position) < 0 || (m_position + position) > m_length))
{
atError("Position %0.8X outside stream bounds ", position);
setError();
return;
}
@ -80,6 +86,7 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin)
if ((((atInt64)m_length - position < 0) || (m_length - position) > m_length))
{
atError("Position %0.8X outside stream bounds ", position);
setError();
return;
}
@ -117,6 +124,7 @@ atUint64 MemoryReader::readUBytesToBuf(void* buf, atUint64 length)
if (m_position + length > m_length)
{
atError("Position %0.8X outside stream bounds ", m_position);
setError();
return 0;
}
@ -134,6 +142,7 @@ void MemoryCopyReader::loadData()
if (!in)
{
atError("Unable to open file '%s'", m_filepath.c_str());
setError();
return;
}
@ -156,6 +165,7 @@ void MemoryCopyReader::loadData()
if (ret < 0)
{
atError("Error reading data from disk");
setError();
return;
}
else if (ret == 0)

View File

@ -22,12 +22,14 @@ MemoryWriter::MemoryWriter(atUint8* data, atUint64 length)
if (!data)
{
atError("data cannot be NULL");
setError();
return;
}
if (length == 0)
{
atError("length cannot be 0");
setError();
return;
}
}
@ -41,6 +43,7 @@ MemoryCopyWriter::MemoryCopyWriter(atUint8* data, atUint64 length)
if (length == 0)
{
atError("length cannot be 0");
setError();
return;
}
@ -62,6 +65,7 @@ MemoryCopyWriter::MemoryCopyWriter(const std::string& filename)
if (!m_data)
{
atError("Could not allocate memory!");
setError();
return;
}
@ -76,12 +80,14 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin)
if (position < 0)
{
atError("Position outside stream bounds");
setError();
return;
}
if ((atUint64)position > m_length)
{
atError("data exceeds available buffer space");
setError();
return;
}
@ -92,12 +98,14 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin)
if ((((atInt64)m_position + position) < 0))
{
atError("Position outside stream bounds");
setError();
return;
}
if (m_position + position > m_length)
{
atError("data exceeds available buffer space");
setError();
return;
}
@ -108,12 +116,14 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin)
if (((atInt64)m_length - position) < 0)
{
atError("Position outside stream bounds");
setError();
return;
}
if ((atUint64)position > m_length)
{
atError("data exceeds available buffer space");
setError();
return;
}
@ -130,6 +140,7 @@ void MemoryCopyWriter::seek(atInt64 position, SeekOrigin origin)
if (position < 0)
{
atError("Position outside stream bounds");
setError();
return;
}
@ -143,6 +154,7 @@ void MemoryCopyWriter::seek(atInt64 position, SeekOrigin origin)
if ((((atInt64)m_position + position) < 0))
{
atError("Position outside stream bounds");
setError();
return;
}
@ -156,6 +168,7 @@ void MemoryCopyWriter::seek(atInt64 position, SeekOrigin origin)
if (((atInt64)m_length - position) < 0)
{
atError("Position outside stream bounds");
setError();
return;
}
@ -197,6 +210,7 @@ void MemoryWriter::save(const std::string& filename)
if (filename.empty() && m_filepath.empty())
{
atError("No file specified, cannot save.");
setError();
return;
}
@ -208,6 +222,7 @@ void MemoryWriter::save(const std::string& filename)
if (!out)
{
atError("Unable to open file '%s'", m_filepath.c_str());
setError();
return;
}
@ -224,6 +239,7 @@ void MemoryWriter::save(const std::string& filename)
if (ret < 0)
{
atError("Error writing data to disk");
setError();
return;
}
else if (ret == 0)
@ -241,12 +257,14 @@ void MemoryWriter::writeUBytes(const atUint8* data, atUint64 length)
if (!data)
{
atError("data cannnot be NULL");
setError();
return;
}
if (m_position + length > m_length)
{
atError("data length exceeds available buffer space");
setError();
return;
}
@ -260,6 +278,7 @@ void MemoryCopyWriter::writeUBytes(const atUint8* data, atUint64 length)
if (!data)
{
atError("data cannnot be NULL");
setError();
return;
}