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
{
public:
FileReader(const std::string& filename, atInt32 cacheSize = (32 * 1024));
FileReader(const std::wstring& 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), bool globalErr=true);
virtual ~FileReader();
inline std::string filename() const
@ -58,6 +58,7 @@ protected:
atInt32 m_blockSize;
atInt32 m_curBlock;
atUint64 m_offset;
bool m_globalErr;
};
} // io
} // Athena

View File

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

View File

@ -104,12 +104,12 @@ namespace Athena
{
namespace error
{
enum Level
enum class Level
{
LevelMessage,
LevelWarning,
LevelError,
LevelFatal
Message,
Warning,
Error,
Fatal
};
}
enum SeekOrigin
@ -126,7 +126,7 @@ enum Endian
};
} // 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();
void atSetExceptionHandler(atEXCEPTION_HANDLER func);
@ -183,19 +183,19 @@ std::ostream& operator<<(std::ostream& os, const Athena::Endian& endian);
#define atMessage(fmt...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
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)
#define atWarning(fmt...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
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)
#define atError(fmt...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
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)
#define atFatal(fmt...) \

View File

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

View File

@ -13,9 +13,10 @@ namespace Athena
{
namespace io
{
FileWriter::FileWriter(const std::string& filename, bool overwrite)
FileWriter::FileWriter(const std::string& filename, bool overwrite, bool globalErr)
: m_fileHandle(NULL),
m_bytePosition(0)
m_bytePosition(0),
m_globalErr(globalErr)
{
#if _WIN32
m_filename = utility::utf8ToWide(filename);
@ -25,9 +26,10 @@ FileWriter::FileWriter(const std::string& filename, bool 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_bytePosition(0)
m_bytePosition(0),
m_globalErr(globalErr)
{
#if _WIN32
m_filename = filename;
@ -59,7 +61,8 @@ void FileWriter::open(bool overwrite)
if (!m_fileHandle)
{
atError("Unable to open file '%s'", filename().c_str());
if (m_globalErr)
atError("Unable to open file '%s'", filename().c_str());
setError();
return;
}
@ -72,7 +75,8 @@ void FileWriter::close()
{
if (!m_fileHandle)
{
atError("Cannot close an unopened stream");
if (m_globalErr)
atError("Cannot close an unopened stream");
setError();
return;
}
@ -86,14 +90,16 @@ void FileWriter::seek(atInt64 pos, SeekOrigin origin)
{
if (!isOpen())
{
atError("Unable to seek in file, not open");
if (m_globalErr)
atError("Unable to seek in file, not open");
setError();
return;
}
if (fseeko64(m_fileHandle, pos, (int)origin) != 0)
{
atError("Unable to seek in file");
if (m_globalErr)
atError("Unable to seek in file");
setError();
}
}
@ -116,14 +122,16 @@ void FileWriter::writeUBytes(const atUint8* data, atUint64 len)
{
if (!isOpen())
{
atError("File not open for writing");
if (m_globalErr)
atError("File not open for writing");
setError();
return;
}
if (fwrite(data, 1, len, m_fileHandle) != len)
{
atError("Unable to write to stream");
if (m_globalErr)
atError("Unable to write to stream");
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;
switch(level)
switch (level)
{
case Athena::error::LevelWarning:
case Athena::error::Level::Warning:
levelStr = "[WARNING] ";
break;
case Athena::error::LevelError:
case Athena::error::Level::Error:
levelStr = "[ERROR ] ";
break;
case Athena::error::LevelFatal:
case Athena::error::Level::Fatal:
levelStr = "[FATAL ] ";
break;
default: break;