mirror of https://github.com/libAthena/athena.git
Added ability to opt-out of file reader/writer errors
This commit is contained in:
parent
b0d56910ce
commit
0a4e8c360e
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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...) \
|
||||||
|
|
|
@ -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,7 +60,8 @@ void FileReader::open()
|
||||||
if (!m_fileHandle)
|
if (!m_fileHandle)
|
||||||
{
|
{
|
||||||
std::string _filename = filename();
|
std::string _filename = filename();
|
||||||
atError("File not found '%s'", _filename.c_str());
|
if (m_globalErr)
|
||||||
|
atError("File not found '%s'", _filename.c_str());
|
||||||
setError();
|
setError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -71,7 +74,8 @@ void FileReader::close()
|
||||||
{
|
{
|
||||||
if (!m_fileHandle)
|
if (!m_fileHandle)
|
||||||
{
|
{
|
||||||
atError("Cannot close an unopened stream");
|
if (m_globalErr)
|
||||||
|
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;
|
||||||
atError("Unable to seek in file");
|
if (m_globalErr)
|
||||||
|
atError("Unable to seek in file");
|
||||||
|
setError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,14 +124,19 @@ 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)
|
||||||
atError("Unable to seek in file");
|
{
|
||||||
|
if (m_globalErr)
|
||||||
|
atError("Unable to seek in file");
|
||||||
|
setError();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
atUint64 FileReader::position() const
|
atUint64 FileReader::position() const
|
||||||
{
|
{
|
||||||
if (!isOpen())
|
if (!isOpen())
|
||||||
{
|
{
|
||||||
atError("File not open");
|
if (m_globalErr)
|
||||||
|
atError("File not open");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,7 +150,8 @@ atUint64 FileReader::length() const
|
||||||
{
|
{
|
||||||
if (!isOpen())
|
if (!isOpen())
|
||||||
{
|
{
|
||||||
atError("File not open");
|
if (m_globalErr)
|
||||||
|
atError("File not open");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,7 +166,8 @@ atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len)
|
||||||
{
|
{
|
||||||
if (!isOpen())
|
if (!isOpen())
|
||||||
{
|
{
|
||||||
atError("File not open for reading");
|
if (m_globalErr)
|
||||||
|
atError("File not open for reading");
|
||||||
setError();
|
setError();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,7 +61,8 @@ void FileWriter::open(bool overwrite)
|
||||||
|
|
||||||
if (!m_fileHandle)
|
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();
|
setError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -72,7 +75,8 @@ void FileWriter::close()
|
||||||
{
|
{
|
||||||
if (!m_fileHandle)
|
if (!m_fileHandle)
|
||||||
{
|
{
|
||||||
atError("Cannot close an unopened stream");
|
if (m_globalErr)
|
||||||
|
atError("Cannot close an unopened stream");
|
||||||
setError();
|
setError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -86,14 +90,16 @@ void FileWriter::seek(atInt64 pos, SeekOrigin origin)
|
||||||
{
|
{
|
||||||
if (!isOpen())
|
if (!isOpen())
|
||||||
{
|
{
|
||||||
atError("Unable to seek in file, not open");
|
if (m_globalErr)
|
||||||
|
atError("Unable to seek in file, not open");
|
||||||
setError();
|
setError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fseeko64(m_fileHandle, pos, (int)origin) != 0)
|
if (fseeko64(m_fileHandle, pos, (int)origin) != 0)
|
||||||
{
|
{
|
||||||
atError("Unable to seek in file");
|
if (m_globalErr)
|
||||||
|
atError("Unable to seek in file");
|
||||||
setError();
|
setError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,14 +122,16 @@ void FileWriter::writeUBytes(const atUint8* data, atUint64 len)
|
||||||
{
|
{
|
||||||
if (!isOpen())
|
if (!isOpen())
|
||||||
{
|
{
|
||||||
atError("File not open for writing");
|
if (m_globalErr)
|
||||||
|
atError("File not open for writing");
|
||||||
setError();
|
setError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fwrite(data, 1, len, m_fileHandle) != len)
|
if (fwrite(data, 1, len, m_fileHandle) != len)
|
||||||
{
|
{
|
||||||
atError("Unable to write to stream");
|
if (m_globalErr)
|
||||||
|
atError("Unable to write to stream");
|
||||||
setError();
|
setError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue