Added ability to opt-out of file reader/writer errors

This commit is contained in:
Jack Andersen 2015-11-22 17:08:48 -10:00
parent b0d56910ce
commit 0a4e8c360e
6 changed files with 62 additions and 39 deletions

View File

@ -13,8 +13,8 @@ namespace io
class FileReader : public IStreamReader class FileReader : public IStreamReader
{ {
public: public:
FileReader(const std::string& filename, atInt32 cacheSize = (32 * 1024)); FileReader(const std::string& filename, atInt32 cacheSize = (32 * 1024), bool globalErr=true);
FileReader(const std::wstring& filename, atInt32 cacheSize = (32 * 1024)); FileReader(const std::wstring& filename, atInt32 cacheSize = (32 * 1024), bool globalErr=true);
virtual ~FileReader(); virtual ~FileReader();
inline std::string filename() const inline std::string filename() const
@ -58,6 +58,7 @@ protected:
atInt32 m_blockSize; atInt32 m_blockSize;
atInt32 m_curBlock; atInt32 m_curBlock;
atUint64 m_offset; atUint64 m_offset;
bool m_globalErr;
}; };
} // io } // io
} // Athena } // Athena

View File

@ -11,8 +11,8 @@ namespace io
class FileWriter : public IStreamWriter class FileWriter : public IStreamWriter
{ {
public: public:
FileWriter(const std::string& filename, bool overwrite = true); FileWriter(const std::string& filename, bool overwrite = true, bool globalErr=true);
FileWriter(const std::wstring& filename, bool overwrite = true); FileWriter(const std::wstring& filename, bool overwrite = true, bool globalErr=true);
virtual ~FileWriter(); virtual ~FileWriter();
inline std::string filename() const inline std::string filename() const
@ -51,6 +51,7 @@ private:
FILE* m_fileHandle; FILE* m_fileHandle;
atUint8 m_currentByte; atUint8 m_currentByte;
atUint64 m_bytePosition; atUint64 m_bytePosition;
bool m_globalErr;
}; };
} }
} // Athena } // Athena

View File

@ -104,12 +104,12 @@ namespace Athena
{ {
namespace error namespace error
{ {
enum Level enum class Level
{ {
LevelMessage, Message,
LevelWarning, Warning,
LevelError, Error,
LevelFatal Fatal
}; };
} }
enum SeekOrigin enum SeekOrigin
@ -126,7 +126,7 @@ enum Endian
}; };
} // Athena } // Athena
typedef void (*atEXCEPTION_HANDLER)(const Athena::error::Level& level, const char* file, const char* function, int line, const char* fmt, ...); typedef void (*atEXCEPTION_HANDLER)(Athena::error::Level level, const char* file, const char* function, int line, const char* fmt, ...);
atEXCEPTION_HANDLER atGetExceptionHandler(); atEXCEPTION_HANDLER atGetExceptionHandler();
void atSetExceptionHandler(atEXCEPTION_HANDLER func); void atSetExceptionHandler(atEXCEPTION_HANDLER func);
@ -183,19 +183,19 @@ std::ostream& operator<<(std::ostream& os, const Athena::Endian& endian);
#define atMessage(fmt...) \ #define atMessage(fmt...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \ if (__handler) \
__handler(Athena::error::LevelMessage, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \ __handler(Athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while(0) } while(0)
#define atWarning(fmt...) \ #define atWarning(fmt...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \ if (__handler) \
__handler(Athena::error::LevelWarning, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \ __handler(Athena::error::Level::Warning, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while(0) } while(0)
#define atError(fmt...) \ #define atError(fmt...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \ if (__handler) \
__handler(Athena::error::LevelError, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \ __handler(Athena::error::Level::Error, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while(0) } while(0)
#define atFatal(fmt...) \ #define atFatal(fmt...) \

View File

@ -13,10 +13,11 @@ namespace Athena
{ {
namespace io namespace io
{ {
FileReader::FileReader(const std::string& filename, atInt32 cacheSize) FileReader::FileReader(const std::string& filename, atInt32 cacheSize, bool globalErr)
: m_fileHandle(nullptr), : m_fileHandle(nullptr),
m_cacheData(nullptr), m_cacheData(nullptr),
m_offset(0) m_offset(0),
m_globalErr(globalErr)
{ {
#if _WIN32 #if _WIN32
m_filename = utility::utf8ToWide(filename); m_filename = utility::utf8ToWide(filename);
@ -27,10 +28,11 @@ FileReader::FileReader(const std::string& filename, atInt32 cacheSize)
setCacheSize(cacheSize); setCacheSize(cacheSize);
} }
FileReader::FileReader(const std::wstring& filename, atInt32 cacheSize) FileReader::FileReader(const std::wstring& filename, atInt32 cacheSize, bool globalErr)
: m_fileHandle(nullptr), : m_fileHandle(nullptr),
m_cacheData(nullptr), m_cacheData(nullptr),
m_offset(0) m_offset(0),
m_globalErr(globalErr)
{ {
#if _WIN32 #if _WIN32
m_filename = filename; m_filename = filename;
@ -58,6 +60,7 @@ void FileReader::open()
if (!m_fileHandle) if (!m_fileHandle)
{ {
std::string _filename = filename(); std::string _filename = filename();
if (m_globalErr)
atError("File not found '%s'", _filename.c_str()); atError("File not found '%s'", _filename.c_str());
setError(); setError();
return; return;
@ -71,6 +74,7 @@ void FileReader::close()
{ {
if (!m_fileHandle) if (!m_fileHandle)
{ {
if (m_globalErr)
atError("Cannot close an unopened stream"); atError("Cannot close an unopened stream");
setError(); setError();
return; return;
@ -105,7 +109,9 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin)
if (m_offset > length()) if (m_offset > length())
{ {
oldOff = m_offset; oldOff = m_offset;
if (m_globalErr)
atError("Unable to seek in file"); atError("Unable to seek in file");
setError();
return; return;
} }
@ -118,13 +124,18 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin)
} }
} }
else if (fseeko64(m_fileHandle, pos, (int)origin) != 0) else if (fseeko64(m_fileHandle, pos, (int)origin) != 0)
{
if (m_globalErr)
atError("Unable to seek in file"); atError("Unable to seek in file");
setError();
}
} }
atUint64 FileReader::position() const atUint64 FileReader::position() const
{ {
if (!isOpen()) if (!isOpen())
{ {
if (m_globalErr)
atError("File not open"); atError("File not open");
return 0; return 0;
} }
@ -139,6 +150,7 @@ atUint64 FileReader::length() const
{ {
if (!isOpen()) if (!isOpen())
{ {
if (m_globalErr)
atError("File not open"); atError("File not open");
return 0; return 0;
} }
@ -154,6 +166,7 @@ atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len)
{ {
if (!isOpen()) if (!isOpen())
{ {
if (m_globalErr)
atError("File not open for reading"); atError("File not open for reading");
setError(); setError();
return 0; return 0;

View File

@ -13,9 +13,10 @@ namespace Athena
{ {
namespace io namespace io
{ {
FileWriter::FileWriter(const std::string& filename, bool overwrite) FileWriter::FileWriter(const std::string& filename, bool overwrite, bool globalErr)
: m_fileHandle(NULL), : m_fileHandle(NULL),
m_bytePosition(0) m_bytePosition(0),
m_globalErr(globalErr)
{ {
#if _WIN32 #if _WIN32
m_filename = utility::utf8ToWide(filename); m_filename = utility::utf8ToWide(filename);
@ -25,9 +26,10 @@ FileWriter::FileWriter(const std::string& filename, bool overwrite)
open(overwrite); open(overwrite);
} }
FileWriter::FileWriter(const std::wstring& filename, bool overwrite) FileWriter::FileWriter(const std::wstring& filename, bool overwrite, bool globalErr)
: m_fileHandle(NULL), : m_fileHandle(NULL),
m_bytePosition(0) m_bytePosition(0),
m_globalErr(globalErr)
{ {
#if _WIN32 #if _WIN32
m_filename = filename; m_filename = filename;
@ -59,6 +61,7 @@ void FileWriter::open(bool overwrite)
if (!m_fileHandle) if (!m_fileHandle)
{ {
if (m_globalErr)
atError("Unable to open file '%s'", filename().c_str()); atError("Unable to open file '%s'", filename().c_str());
setError(); setError();
return; return;
@ -72,6 +75,7 @@ void FileWriter::close()
{ {
if (!m_fileHandle) if (!m_fileHandle)
{ {
if (m_globalErr)
atError("Cannot close an unopened stream"); atError("Cannot close an unopened stream");
setError(); setError();
return; return;
@ -86,6 +90,7 @@ void FileWriter::seek(atInt64 pos, SeekOrigin origin)
{ {
if (!isOpen()) if (!isOpen())
{ {
if (m_globalErr)
atError("Unable to seek in file, not open"); atError("Unable to seek in file, not open");
setError(); setError();
return; return;
@ -93,6 +98,7 @@ void FileWriter::seek(atInt64 pos, SeekOrigin origin)
if (fseeko64(m_fileHandle, pos, (int)origin) != 0) if (fseeko64(m_fileHandle, pos, (int)origin) != 0)
{ {
if (m_globalErr)
atError("Unable to seek in file"); atError("Unable to seek in file");
setError(); setError();
} }
@ -116,6 +122,7 @@ void FileWriter::writeUBytes(const atUint8* data, atUint64 len)
{ {
if (!isOpen()) if (!isOpen())
{ {
if (m_globalErr)
atError("File not open for writing"); atError("File not open for writing");
setError(); setError();
return; return;
@ -123,6 +130,7 @@ void FileWriter::writeUBytes(const atUint8* data, atUint64 len)
if (fwrite(data, 1, len, m_fileHandle) != len) if (fwrite(data, 1, len, m_fileHandle) != len)
{ {
if (m_globalErr)
atError("Unable to write to stream"); atError("Unable to write to stream");
setError(); setError();
} }

View File

@ -41,18 +41,18 @@ std::ostream& operator<<(std::ostream& os, const Athena::Endian& endian)
} }
static void __defaultExceptionHandler(const Athena::error::Level& level, const char* file, const char* function, int line, const char* fmt, ...) static void __defaultExceptionHandler(Athena::error::Level level, const char* file, const char* function, int line, const char* fmt, ...)
{ {
std::string levelStr; std::string levelStr;
switch(level) switch (level)
{ {
case Athena::error::LevelWarning: case Athena::error::Level::Warning:
levelStr = "[WARNING] "; levelStr = "[WARNING] ";
break; break;
case Athena::error::LevelError: case Athena::error::Level::Error:
levelStr = "[ERROR ] "; levelStr = "[ERROR ] ";
break; break;
case Athena::error::LevelFatal: case Athena::error::Level::Fatal:
levelStr = "[FATAL ] "; levelStr = "[FATAL ] ";
break; break;
default: break; default: break;