From a7fda281b99d0d243cb75e12c58e12e87687e2fc Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Wed, 22 Jul 2015 13:40:22 -0700 Subject: [PATCH] Add error state --- include/Athena/IStream.hpp | 5 +++++ src/Athena/FileReader.cpp | 3 +++ src/Athena/FileWriter.cpp | 9 +++++++++ src/Athena/MemoryReader.cpp | 10 ++++++++++ src/Athena/MemoryWriter.cpp | 19 +++++++++++++++++++ 5 files changed, 46 insertions(+) diff --git a/include/Athena/IStream.hpp b/include/Athena/IStream.hpp index 2cb72fe..e08541a 100644 --- a/include/Athena/IStream.hpp +++ b/include/Athena/IStream.hpp @@ -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; }; } } diff --git a/src/Athena/FileReader.cpp b/src/Athena/FileReader.cpp index 9adc5ce..3fea0ce 100644 --- a/src/Athena/FileReader.cpp +++ b/src/Athena/FileReader.cpp @@ -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; } diff --git a/src/Athena/FileWriter.cpp b/src/Athena/FileWriter.cpp index 0b5a633..6d97371 100644 --- a/src/Athena/FileWriter.cpp +++ b/src/Athena/FileWriter.cpp @@ -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(); + } } } diff --git a/src/Athena/MemoryReader.cpp b/src/Athena/MemoryReader.cpp index fe1f677..5968ee9 100644 --- a/src/Athena/MemoryReader.cpp +++ b/src/Athena/MemoryReader.cpp @@ -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) diff --git a/src/Athena/MemoryWriter.cpp b/src/Athena/MemoryWriter.cpp index 380e25f..8fec07f 100644 --- a/src/Athena/MemoryWriter.cpp +++ b/src/Athena/MemoryWriter.cpp @@ -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; }