Remove and replace exceptions

This commit is contained in:
Phillip Stephens 2015-07-21 22:37:22 -07:00
parent 602f6b6e15
commit 8ea5071e0f
33 changed files with 425 additions and 588 deletions

View File

@ -51,16 +51,10 @@ add_library(AthenaCore
include/Athena/Types.hpp
include/Athena/Utility.hpp
include/Athena/Global.hpp
include/Athena/Exception.hpp
include/Athena/FileNotFoundException.hpp
include/Athena/IOException.hpp
include/Athena/InvalidDataException.hpp
include/Athena/InvalidOperationException.hpp
include/Athena/FileReader.hpp
include/Athena/FileWriter.hpp
include/Athena/MemoryReader.hpp
include/Athena/MemoryWriter.hpp
include/Athena/NotImplementedException.hpp
include/Athena/Checksums.hpp
include/Athena/Compression.hpp
include/LZ77/LZBase.hpp

View File

@ -1,7 +1,7 @@
# PKGBUILD for libAthena
_pkgname=libathena
pkgname=$_pkgname-git
pkgver=2.0.0.9.gf3ba881
pkgver=2.1.0
pkgrel=1
pkgdesc="Basic cross platform IO library"
arch=('i686' 'x86_64')

View File

@ -17,7 +17,7 @@
#ifndef ALTTP_FILE_HPP
#define ALTTP_FILE_HPP
#include "Athena/Types.hpp"
#include "Athena/Global.hpp"
#include <vector>
namespace Athena

View File

@ -17,7 +17,7 @@
#ifndef ALTTP_QUEST_HPP
#define ALTTP_QUEST_HPP
#include "Types.hpp"
#include "Athena/Global.hpp"
#include <string>
#include <vector>
#include "Athena/ALTTPStructs.hpp"

View File

@ -1,116 +0,0 @@
#ifndef EXCEPTION_HPP
#define EXCEPTION_HPP
#include <string>
#include <stdarg.h>
#include "Athena/Utility.hpp"
#include "Athena/Global.hpp"
#define __STRX(x) #x
#define __STR(x) __STRX(x)
#define __LINE_STRING__ __STR(__LINE__)
namespace Athena
{
namespace error
{
/*! \class Exception
* \brief The baseclass for all Exceptions.
*
* <b>Do Not</b> use Exception directly, instead create an appropriate
* Exception class and inherit from this baseclass.
*/
class Exception
{
public:
/*! \brief The constructor for an Exception
* \param message The error message to throw
*/
inline Exception(const std::string& message, const std::string& file, const std::string& function, const int line) :
m_message(message),
m_file(file),
m_function(function),
m_line(line),
m_exceptionName("Exception")
{
}
/*! \brief Returns the Error message of the exception
* \return std::string The error message
*/
inline std::string message() const
{
return m_exceptionName + (m_message.empty() ? "" : ": " + m_message);
}
inline std::string file() const
{
return m_file;
}
inline std::string function() const
{
return m_function;
}
inline int line() const
{
return m_line;
}
inline std::string formattedMessage() const
{
return Athena::utility::sprintf("%s : %s (%i) %s", m_file.c_str(), m_function.c_str(), m_line, message().c_str());
}
protected:
std::string m_message; //!< The error message string
std::string m_file;
std::string m_function;
int m_line;
std::string m_exceptionName;
};
} // error
} // Athena
#ifdef _MSC_VER
#define THROW_EXCEPTION(args,...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); return; \
} else { std::string msg = Athena::utility::sprintf(__VA_ARGS__); \
throw Athena::error::Exception(std::string("Exception: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#elif defined(__GNUC__)
#define THROW_EXCEPTION(args...) \
do { \
if (atGetExceptionHandler()) { atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return; \
} else { \
std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::Exception(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif
#ifdef _MSC_VER
#define THROW_EXCEPTION_RETURN(ret, args,...) \
do { \
if (atGetExceptionHandler()) \
{ \
atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, __VA_ARGS__); \
return ret; \
} else { \
std::string msg = Athena::utility::sprintf(__VA_ARGS__); \
throw Athena::error::Exception(std::string("Exception: ")+msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#elif defined(__GNUC__)
#define THROW_EXCEPTION_RETURN(ret, args...) \
do { \
if (atGetExceptionHandler()) { atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return ret; \
} else { \
std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::Exception(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif
#endif // EXCEPTION_HPP

View File

@ -1,68 +0,0 @@
#ifndef FILENOTFOUNDEXCEPTION_HPP
#define FILENOTFOUNDEXCEPTION_HPP
#include "Athena/Exception.hpp"
namespace Athena
{
namespace error
{
/*! \class FileNotFoundException
* \brief An excpeption thrown when a file could not be found at the given path.
*
* This should only be thrown when the Stream is unable to open a file.<br />
* <br />
* It is <b>NOT</b> appropriate to use <b>throw new</b> so avoid doing so,
* keeping things on the stack as much as possible is very important for speed.
*/
class FileNotFoundException : public Exception
{
public:
/*! \brief The constructor for an FileNotFoundException
* \param filename The path of the offending file.
*/
inline FileNotFoundException(const std::string& filename, const std::string& file, const std::string& function, const int line) :
Exception(std::string("FileNotFoundException: Could not find file \"") + filename + std::string("\", please check that it exists."), file, function, line),
m_filename(filename)
{
m_exceptionName = "FileNotFoundException";
}
/*! \brief Returns the path of the offending file.
* \return std::string The filename of the file including the path.
*/
inline std::string filename() const { return m_filename; }
private:
std::string m_filename;
};
} // error
} // Athena
#ifndef THROW_FILE_NOT_FOUND_EXCEPTION
#define THROW_FILE_NOT_FOUND_EXCEPTION(msg) \
do { \
if (atGetExceptionHandler()) \
{ \
atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, msg); \
return; \
} \
else \
throw Athena::error::FileNotFoundException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} while(0)
#endif
#ifndef THROW_FILE_NOT_FOUND_EXCEPTION_RETURN
#define THROW_FILE_NOT_FOUND_EXCEPTION_RETURN(ret, msg) \
do { \
if (atGetExceptionHandler()) \
{ \
atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, msg); \
return ret; \
} \
else \
throw Athena::error::FileNotFoundException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} while(0)
#endif
#endif // FILENOTFOUNDEXCEPTION_HPP

View File

@ -51,21 +51,6 @@ typedef struct _stat64 stat64_t;
typedef struct stat64 stat64_t;
#endif
#ifndef aDebug
#define aDebug() \
std::cout << __FILE__ << "(" << __LINE__ << ") " << AT_PRETTY_FUNCTION << ": "
#endif
#ifndef aError
#define aError() \
std::cerr << __FILE__ << "(" << __LINE__ << ") " << AT_PRETTY_FUNCTION << ": "
#endif
#ifndef aPrint
#define aPrint() std::cout
#endif
#define aEnd() '\n'
#ifndef BLOCKSZ
#define BLOCKSZ 512
#endif
@ -75,6 +60,16 @@ typedef struct stat64 stat64_t;
namespace Athena
{
namespace error
{
enum Level
{
MESSAGE,
WARNING,
ERROR,
FATAL
};
}
enum SeekOrigin
{
Begin,
@ -91,7 +86,6 @@ enum Endian
#ifndef ATHENA_NO_SAKURA
namespace Sakura
{
template <typename T>
class Vector2D
{
@ -118,11 +112,83 @@ typedef Vector2D<float> Vector2Df;
#endif // ATHENA_NO_SAKURA
} // Athena
typedef void (*atEXCEPTION_HANDLER)(const std::string& file, const std::string& function, int line, const std::string&, ...);
typedef void (*atEXCEPTION_HANDLER)(const Athena::error::Level& level, const std::string& file, const std::string& function, int line, const char* fmt, ...);
atEXCEPTION_HANDLER atGetExceptionHandler();
void atSetExceptionHandler(atEXCEPTION_HANDLER func);
std::ostream& operator<<(std::ostream& os, const Athena::SeekOrigin& origin);
std::ostream& operator<<(std::ostream& os, const Athena::Endian& endian);
#ifdef _MSC_VER
#ifndef NDEBUG
#define atDebug(fmt, ...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(Athena::error::MESSAGE, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
} while(0)
#else
#define atDebug(fmt, ...)
#endif
#define atMessage(fmt, ...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(Athena::error::MESSAGE, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
} while(0)
#define atWarning(fmt, ...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(Athena::error::WARNING, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
} while(0)
#define atError(fmt, ...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(Athena::error::ERROR, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
} while(0)
#define atFatal(fmt, ...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(Athena::error::FATAL, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \
} while(0)
#elif defined(__GNUC__)
#ifndef NDEBUG
#define atDebug(fmt...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(Athena::error::MESSAGE, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while(0)
#else // _MSC_VER
#define atDebug(fmt, ...)
#endif // NDEBUG
#define atMessage(fmt...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(Athena::error::MESSAGE, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while(0)
#define atWarning(fmt...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(Athena::error::WARNING, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while(0)
#define atError(fmt...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(Athena::error::ERROR, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while(0)
#define atFatal(fmt...) \
do { atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \
__handler(Athena::error::FATAL, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \
} while(0)
#endif // defined(__GNUC__)
#endif // GLOBAL_HPP

View File

@ -1,75 +0,0 @@
#ifndef IOEXCEPTION_HPP
#define IOEXCEPTION_HPP
#include "Athena/Exception.hpp"
namespace Athena
{
namespace error
{
/*! \class IOException
* \brief An excpeption thrown on inappropriate IO calls.
*
* This should only be thrown when the library tries to write to a buffer
* e.g when the position is greater than the position and the stream
* is not set to autoresize.<br />
* <br />
* It is <b>NOT</b> appropriate to use <b>throw new</b> so avoid doing so,
* keeping things on the stack as much as possible is very important for speed.
*/
class IOException : public Exception
{
public:
/*! \brief The constructor for an IOException
* \param message The error message to throw
*/
inline IOException(const std::string& message, const std::string& file, const std::string& function, const int line) :
Exception(message, file, function, line)
{
m_exceptionName = "IOException";
}
};
} // error
} // Athena
#ifdef _MSC_VER
#define THROW_IO_EXCEPTION(args, ...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args, __VA_ARGS__); return; \
} else { \
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \
throw Athena::error::IOException(std::string("IOException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while (0)
#elif defined(__GNUC__)
#define THROW_IO_EXCEPTION(args...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return; \
} else { std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::IOException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif
#ifdef _MSC_VER
#define THROW_IO_EXCEPTION_RETURN(ret, args, ...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args, __VA_ARGS__); return ret; \
} else { \
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \
throw Athena::error::IOException(std::string("IOException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while (0)
#elif defined(__GNUC__)
#define THROW_IO_EXCEPTION_RETURN(ret, args...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return ret; \
} else { std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::IOException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif
#endif // IOEXCEPTION_HPP

View File

@ -2,7 +2,6 @@
#define STREAM_HPP
#include "Global.hpp"
#include "Athena/NotImplementedException.hpp"
namespace Athena
{

View File

@ -1,65 +0,0 @@
#ifndef INVALIDDATAEXCEPTION_HPP
#define INVALIDDATAEXCEPTION_HPP
#include "Athena/Exception.hpp"
#include <sstream>
namespace Athena
{
namespace error
{
/*! \class InvalidDataException
* \brief An exception thrown on Invalid Data calls.
*
* This should only be thrown when the library tries to
* e.g pass a NULL pointer to a function which requires a valid pointer.
* <br />
* It is <b>NOT</b> appropriate to use <b>throw new</b> so avoid doing so,
* keeping things on the stack as much as possible is very important for speed.
*/
class InvalidDataException : public Exception
{
public:
inline InvalidDataException(const std::string& error, const std::string& file, const std::string& function, const int line)
: Exception(("InvalidDataException") + error, file, function, line)
{
m_exceptionName = "InvalidDataException";
}
};
} // error
} // Athena
#ifdef _MSC_VER
#define THROW_INVALID_DATA_EXCEPTION(args, ...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args, __VA_ARGS__); return; } \
else { std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \
throw Athena::error::InvalidDataException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#elif defined(__GNUC__)
#define THROW_INVALID_DATA_EXCEPTION(args...) \
do { if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return; } \
else { std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::InvalidDataException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif
#ifdef _MSC_VER
#define THROW_INVALID_DATA_EXCEPTION_RETURN(ret, args, ...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args, __VA_ARGS__); return ret; } \
else { std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \
throw Athena::error::InvalidDataException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#elif defined(__GNUC__)
#define THROW_INVALID_DATA_EXCEPTION_RETURN(ret, args...) \
do { if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return ret; } \
else { std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::InvalidDataException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif
#endif // INVALIDDATAEXCEPTION_HPP

View File

@ -1,76 +0,0 @@
#ifndef INVALID_OPERATION_EXCEPTION_HPP
#define INVALID_OPERATION_EXCEPTION_HPP
#include <string>
#include <stdarg.h>
#include "Athena/Exception.hpp"
namespace Athena
{
namespace error
{
/*! \class InvalidOperationException
* \brief An excpeption thrown on Invalid Operations calls.
*
* This should only be thrown when the library tries to
* e.g pass a NULL pointer to a function which requires a valid pointer.
* <br />
* It is <b>NOT</b> appropriate to use <b>throw new</b> so avoid doing so,
* keeping things on the stack as much as possible is very important for speed.
*/
class InvalidOperationException : public Exception
{
public:
/*! \brief The constructor for an InvalidOperationException
* \param error The error message to throw
*/
inline InvalidOperationException(const std::string& message, const std::string& file, const std::string& function, const int line) :
Exception(message, file, function, line)
{
m_exceptionName = "InvalidOperationException";
}
};
} // error
} // Athena
#ifdef _MSC_VER
#define THROW_INVALID_OPERATION_EXCEPTION(args, ...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args, __VA_ARGS__); return; \
} else { \
std::string msg = Athena::utility::sprintf(args , __VA_ARGS__); \
\
throw Athena::error::InvalidOperationException(std::string("InvalidOperationException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
\
} \
} while (0)
#elif defined (__GNUC__)
#define THROW_INVALID_OPERATION_EXCEPTION(args...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return; \
} else { std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::InvalidOperationException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif
#ifdef _MSC_VER
#define THROW_INVALID_OPERATION_EXCEPTION_RETURN(ret, args, ...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args, __VA_ARGS__); return ret; \
} else { \
std::string msg = Athena::utility::sprintf(args, __VA_ARGS__); \
\
throw Athena::error::InvalidOperationException(std::string("InvalidOperationException: ") + msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while (0)
#elif defined(__GNUC__)
#define THROW_INVALID_OPERATION_EXCEPTION_RETURN(ret, args...) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, args); return ret; \
} else { std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::InvalidOperationException(msg, __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif
#endif // INVALID_OPERATION_EXCEPTION_HPP

View File

@ -1,37 +0,0 @@
#ifndef NOTIMPLEMENTEDEXCEPTION_HPP
#define NOTIMPLEMENTEDEXCEPTION_HPP
#include "Athena/Exception.hpp"
namespace Athena
{
namespace error
{
class NotImplementedException : public Exception
{
public:
NotImplementedException(const std::string& message, const std::string& file, const std::string& function, const int line) :
Exception(message, file, function, line)
{
m_exceptionName = "NotImplementedException";
}
};
} // error
} // Athena
#define THROW_NOT_IMPLEMENTED_EXCEPTION() \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, "NotImplementedException"); return; \
} else { \
throw Athena::error::NotImplementedException(std::string(), __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#define THROW_NOT_IMPLEMENTED_EXCEPTION_RETURN(ret) \
do { \
if (atGetExceptionHandler()) {atGetExceptionHandler()(__FILE__, AT_PRETTY_FUNCTION, __LINE__, "NotImplementedException"); return ret; \
} else { \
throw Athena::error::NotImplementedException(std::string(), __FILE__, AT_PRETTY_FUNCTION, __LINE__); \
} \
} while(0)
#endif // NOTIMPLEMENTEDEXCEPTION_HPP

View File

@ -17,8 +17,6 @@
#include "Athena/ALTTPFile.hpp"
#include "Athena/ALTTPQuest.hpp"
#include "Athena/InvalidOperationException.hpp"
namespace Athena
{
ALTTPFile::ALTTPFile()
@ -33,7 +31,10 @@ ALTTPFile::ALTTPFile(std::vector<ALTTPQuest*> quests, std::vector<ALTTPQuest*> b
void ALTTPFile::setQuest(atUint32 id, ALTTPQuest* val)
{
if (id > m_quests.size())
THROW_INVALID_OPERATION_EXCEPTION("index out of range");
{
atWarning("index out of range");
return;
}
m_quests[id] = val;
}
@ -45,7 +46,10 @@ std::vector<ALTTPQuest*> ALTTPFile::questList() const
ALTTPQuest* ALTTPFile::quest(atUint32 id) const
{
if (id > m_quests.size())
THROW_INVALID_OPERATION_EXCEPTION_RETURN(nullptr, "index out of range");
{
atWarning("index out of range");
return nullptr;
}
return m_quests[id];
}

View File

@ -247,7 +247,7 @@ ALTTPDungeonItemFlags ALTTPFileReader::readDungeonFlags()
flags.HyruleCastle = (flagsByte >> 6) & 1;
flags.SewerPassage = (flagsByte >> 7) & 1;
aDebug() << std::hex << flags.flags1 << " " << flags.flags2 << std::dec << std::endl;
atDebug("%x %x", flags.flags1, flags.flags2);
return flags;
}

View File

@ -15,7 +15,6 @@
// along with libAthena. If not, see <http://www.gnu.org/licenses/>
#include "Athena/ALTTPQuest.hpp"
#include "Athena/InvalidOperationException.hpp"
#include <iostream>
namespace Athena
@ -72,7 +71,10 @@ std::vector<ALTTPOverworldEvent*> ALTTPQuest::overworldEvents() const
ALTTPOverworldEvent* ALTTPQuest::overworldEvent(atUint32 id) const
{
if (id > m_overworldEvents.size() - 1)
THROW_INVALID_OPERATION_EXCEPTION_RETURN(nullptr, "index out of range");
{
atWarning("index out of range");
return nullptr;
}
return m_overworldEvents[id];
}
@ -303,7 +305,10 @@ void ALTTPQuest::setDungeonKeys(std::vector<atUint8> val)
void ALTTPQuest::setDungeonKeys(atUint32 id, atUint8 val)
{
if (id > m_dungeonKeys.size() - 1)
THROW_INVALID_OPERATION_EXCEPTION("index out of range");
{
atWarning("index out of range");
return;
}
m_dungeonKeys[id] = val;
}
@ -311,7 +316,10 @@ void ALTTPQuest::setDungeonKeys(atUint32 id, atUint8 val)
atUint8 ALTTPQuest::dungeonKeys(atUint32 id) const
{
if (id > m_dungeonKeys.size() - 1)
THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "index out of range");
{
atWarning("index out of range");
return 0;
}
return m_dungeonKeys[id];
}
@ -400,7 +408,10 @@ void ALTTPQuest::setOldManFlags(std::vector<atUint8> flags)
void ALTTPQuest::setOldManFlag(atUint32 id, atUint8 val)
{
if (id > m_oldManFlags.size() - 1)
THROW_INVALID_OPERATION_EXCEPTION("index out of range");
{
atWarning("index out of range");
return;
}
m_oldManFlags[id] = val;
}
@ -408,8 +419,10 @@ void ALTTPQuest::setOldManFlag(atUint32 id, atUint8 val)
atUint8 ALTTPQuest::oldManFlag(atUint32 id)
{
if (id > m_oldManFlags.size() - 1)
THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "index out of range");
{
atWarning("index out of range");
return 0;
}
return m_oldManFlags[id];
}
@ -436,7 +449,10 @@ void ALTTPQuest::setUnknown1(std::vector<atUint8> flags)
void ALTTPQuest::setUnknown1(atUint32 id, atUint8 val)
{
if (id > m_unknown1.size())
THROW_INVALID_OPERATION_EXCEPTION("index out of range");
{
atWarning("index out of range");
return;
}
m_unknown1[id] = val;
}
@ -444,7 +460,10 @@ void ALTTPQuest::setUnknown1(atUint32 id, atUint8 val)
atUint8 ALTTPQuest::unknown1(atUint32 id)
{
if (id > m_unknown1.size())
THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "index out of range");
{
atWarning("index out of range");
return 0;
}
return m_unknown1[id];
}
@ -462,7 +481,10 @@ void ALTTPQuest::setPlayerName(std::vector<atUint16> playerName)
void ALTTPQuest::setPlayerName(const std::string& playerName)
{
if (playerName == std::string() || playerName.size() > 6)
THROW_INVALID_OPERATION_EXCEPTION("index out of range");
{
atWarning("index out of range");
return;
}
m_playerName.clear();
@ -675,7 +697,10 @@ void ALTTPQuest::setDungeonDeathTotals(std::vector<atUint16> val)
void ALTTPQuest::setDungeonDeathTotal(atUint32 id, atUint16 val)
{
if (id > m_dungeonDeathTotals.size())
THROW_INVALID_OPERATION_EXCEPTION("index out of range");
{
atWarning("index out of range");
return;
}
m_dungeonDeathTotals[id] = val;
}
@ -683,7 +708,10 @@ void ALTTPQuest::setDungeonDeathTotal(atUint32 id, atUint16 val)
atUint16 ALTTPQuest::dungeonDeathTotal(atUint32 id) const
{
if (id > m_dungeonDeathTotals.size())
THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "index out of range");
{
atWarning("index out of range");
return 0;
}
return m_dungeonDeathTotals[id];
}

View File

@ -1,5 +1,4 @@
#include "Athena/Compression.hpp"
#include "Athena/Exception.hpp"
#include <lzo/lzo1x.h>
#include <iostream>
#include <zlib.h>

View File

@ -1,8 +1,4 @@
#include "Athena/FileReader.hpp"
#include "Athena/FileNotFoundException.hpp"
#include "Athena/InvalidDataException.hpp"
#include "Athena/InvalidOperationException.hpp"
#include "Athena/IOException.hpp"
#if _WIN32
#include "win32_largefilewrapper.h"
@ -35,7 +31,10 @@ void FileReader::open()
m_fileHandle = fopen(m_filename.c_str(), "rb");
if (!m_fileHandle)
THROW_FILE_NOT_FOUND_EXCEPTION(m_filename);
{
atError("File not found '%s'", m_filename.c_str());
return;
}
// ensure we're at the beginning of the file
rewind(m_fileHandle);
@ -44,7 +43,10 @@ void FileReader::open()
void FileReader::close()
{
if (!m_fileHandle)
THROW_INVALID_OPERATION_EXCEPTION("Cannot close an unopened stream");
{
atError("Cannot close an unopened stream");
return;
}
fclose(m_fileHandle);
m_fileHandle = NULL;
@ -72,7 +74,8 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin)
if (m_offset > length())
{
oldOff = m_offset;
THROW_INVALID_OPERATION_EXCEPTION("Unable to seek in file");
atError("Unable to seek in file");
return;
}
size_t block = m_offset / m_blockSize;
@ -84,13 +87,16 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin)
}
}
else if (fseeko64(m_fileHandle, pos, (int)origin) != 0)
THROW_INVALID_OPERATION_EXCEPTION("Unable to seek in file");
atError("Unable to seek in file");
}
atUint64 FileReader::position() const
{
if (!isOpen())
THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "File not open");
{
atError("File not open");
return 0;
}
if (m_blockSize > 0)
return m_offset;
@ -101,7 +107,10 @@ atUint64 FileReader::position() const
atUint64 FileReader::length() const
{
if (!isOpen())
THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "File not open");
{
atError("File not open");
return 0;
}
return utility::fileSize(m_filename);
}
@ -109,7 +118,10 @@ atUint64 FileReader::length() const
atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len)
{
if (!isOpen())
THROW_INVALID_OPERATION_EXCEPTION_RETURN(0, "File not open for reading");
{
atError("File not open for reading");
return 0;
}
if (m_blockSize <= 0)
return fread(buf, 1, len, m_fileHandle);

View File

@ -1,8 +1,4 @@
#include "Athena/FileWriter.hpp"
#include "Athena/FileNotFoundException.hpp"
#include "Athena/InvalidDataException.hpp"
#include "Athena/InvalidOperationException.hpp"
#include "Athena/IOException.hpp"
#if _WIN32
#include "win32_largefilewrapper.h"
@ -36,7 +32,10 @@ void FileWriter::open(bool overwrite)
m_fileHandle = fopen(m_filename.c_str(), "r+b");
if (!m_fileHandle)
THROW_FILE_NOT_FOUND_EXCEPTION(m_filename);
{
atError("Unable to open file '%s'", m_filename.c_str());
return;
}
// ensure we're at the beginning of the file
rewind(m_fileHandle);
@ -45,7 +44,10 @@ void FileWriter::open(bool overwrite)
void FileWriter::close()
{
if (!m_fileHandle)
THROW_INVALID_OPERATION_EXCEPTION("Cannot close an unopened stream");
{
atError("Cannot close an unopened stream");
return;
}
fclose(m_fileHandle);
m_fileHandle = NULL;
@ -55,7 +57,7 @@ void FileWriter::close()
void FileWriter::seek(atInt64 pos, SeekOrigin origin)
{
if (fseeko64(m_fileHandle, pos, (int)origin) != 0)
THROW_IO_EXCEPTION("Unable to seek in file");
atError("Unable to seek in file");
}
atUint64 FileWriter::position() const
@ -71,10 +73,13 @@ atUint64 FileWriter::length() const
void FileWriter::writeUBytes(const atUint8* data, atUint64 len)
{
if (!isOpen())
THROW_INVALID_OPERATION_EXCEPTION("File not open for writing");
{
atError("File not open for writing");
return;
}
if (fwrite(data, 1, len, m_fileHandle) != len)
THROW_IO_EXCEPTION("Unable to write to stream");
atError("Unable to write to stream");
}
}

View File

@ -1,4 +1,7 @@
#include "Athena/Global.hpp"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
std::ostream& operator<<(std::ostream& os, const Athena::SeekOrigin& origin)
{
@ -38,7 +41,31 @@ std::ostream& operator<<(std::ostream& os, const Athena::Endian& endian)
}
static atEXCEPTION_HANDLER g_atExceptionHandler = nullptr;
static void __defaultExceptionHandler(const Athena::error::Level& level, const std::string& file, const std::string& function, int line, const char* fmt, ...)
{
std::string levelStr;
switch(level)
{
case Athena::error::WARNING:
levelStr = "[WARNING] ";
break;
case Athena::error::ERROR:
levelStr = "[ERROR ] ";
break;
case Athena::error::FATAL:
levelStr = "[FATAL ] ";
break;
default: break;
}
va_list vl;
va_start(vl, fmt);
std::string msg = Athena::utility::vsprintf(fmt, vl);
va_end(vl);
std::cerr << levelStr << " " << file << " " << function << "(" << line << "): " << msg << std::endl;
}
static atEXCEPTION_HANDLER g_atExceptionHandler = __defaultExceptionHandler;
atEXCEPTION_HANDLER atGetExceptionHandler()
{
@ -48,5 +75,8 @@ atEXCEPTION_HANDLER atGetExceptionHandler()
void atSetExceptionHandler(atEXCEPTION_HANDLER func)
{
g_atExceptionHandler = func;
if (func)
g_atExceptionHandler = func;
else
g_atExceptionHandler = __defaultExceptionHandler;
}

View File

@ -1,8 +1,4 @@
#include "Athena/MemoryReader.hpp"
#include "Athena/IOException.hpp"
#include "Athena/FileNotFoundException.hpp"
#include "Athena/InvalidDataException.hpp"
#include "Athena/InvalidOperationException.hpp"
#include <stdio.h>
#include <stdlib.h>
@ -24,20 +20,32 @@ MemoryReader::MemoryReader(const atUint8* data, atUint64 length)
m_position(0)
{
if (!data)
THROW_INVALID_DATA_EXCEPTION("data cannot be NULL");
{
atError("data cannot be NULL");
return;
}
if (length == 0)
THROW_INVALID_OPERATION_EXCEPTION("length cannot be 0");
{
atError("length cannot be 0");
return;
}
}
MemoryCopyReader::MemoryCopyReader(const atUint8* data, atUint64 length)
: MemoryReader(data, length)
{
if (!data)
THROW_INVALID_DATA_EXCEPTION("data cannot be NULL");
{
atError("data cannot be NULL");
return;
}
if (length == 0)
THROW_INVALID_OPERATION_EXCEPTION("length cannot be 0");
{
atError("length cannot be 0");
return;
}
m_dataCopy.reset(new atUint8[m_length]);
m_data = m_dataCopy.get();
@ -50,21 +58,30 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin)
{
case SeekOrigin::Begin:
if ((position < 0 || (atInt64)position > (atInt64)m_length))
THROW_IO_EXCEPTION("Position %0.8X outside stream bounds ", position);
{
atError("Position %0.8X outside stream bounds ", position);
return;
}
m_position = position;
break;
case SeekOrigin::Current:
if ((((atInt64)m_position + position) < 0 || (m_position + position) > m_length))
THROW_IO_EXCEPTION("Position %0.8X outside stream bounds ", position);
{
atError("Position %0.8X outside stream bounds ", position);
return;
}
m_position += position;
break;
case SeekOrigin::End:
if ((((atInt64)m_length - position < 0) || (m_length - position) > m_length))
THROW_IO_EXCEPTION("Position %0.8X outside stream bounds ", position);
{
atError("Position %0.8X outside stream bounds ", position);
return;
}
m_position = m_length - position;
break;
@ -98,7 +115,10 @@ atUint8* MemoryReader::data() const
atUint64 MemoryReader::readUBytesToBuf(void* buf, atUint64 length)
{
if (m_position + length > m_length)
THROW_IO_EXCEPTION_RETURN(0, "Position %0.8X outside stream bounds ", m_position);
{
atError("Position %0.8X outside stream bounds ", m_position);
return 0;
}
memcpy(buf, (const atUint8*)(m_data + m_position), length);
m_position += length;
@ -112,7 +132,10 @@ void MemoryCopyReader::loadData()
in = fopen(m_filepath.c_str(), "rb");
if (!in)
THROW_FILE_NOT_FOUND_EXCEPTION(m_filepath);
{
atError("Unable to open file '%s'", m_filepath.c_str());
return;
}
rewind(in);
@ -131,7 +154,10 @@ void MemoryCopyReader::loadData()
atInt64 ret = fread(m_dataCopy.get() + done, 1, blocksize, in);
if (ret < 0)
THROW_IO_EXCEPTION("Error reading data from disk");
{
atError("Error reading data from disk");
return;
}
else if (ret == 0)
break;

View File

@ -1,8 +1,4 @@
#include "Athena/MemoryWriter.hpp"
#include "Athena/IOException.hpp"
#include "Athena/InvalidOperationException.hpp"
#include "Athena/InvalidDataException.hpp"
#include "Athena/FileNotFoundException.hpp"
#include <stdio.h>
#include <string.h>
@ -24,10 +20,16 @@ MemoryWriter::MemoryWriter(atUint8* data, atUint64 length)
m_position(0)
{
if (!data)
THROW_INVALID_DATA_EXCEPTION("data cannot be NULL");
{
atError("data cannot be NULL");
return;
}
if (length == 0)
THROW_INVALID_OPERATION_EXCEPTION("length cannot be 0");
{
atError("length cannot be 0");
return;
}
}
MemoryCopyWriter::MemoryCopyWriter(atUint8* data, atUint64 length)
@ -37,7 +39,10 @@ MemoryCopyWriter::MemoryCopyWriter(atUint8* data, atUint64 length)
m_position = 0;
if (length == 0)
THROW_INVALID_OPERATION_EXCEPTION("length cannot be 0");
{
atError("length cannot be 0");
return;
}
m_dataCopy.reset(new atUint8[length]);
m_data = m_dataCopy.get();
@ -55,7 +60,10 @@ MemoryCopyWriter::MemoryCopyWriter(const std::string& filename)
m_data = m_dataCopy.get();
if (!m_data)
THROW_IO_EXCEPTION("Could not allocate memory!");
{
atError("Could not allocate memory!");
return;
}
memset(m_data, 0, m_length);
}
@ -66,30 +74,48 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin)
{
case SeekOrigin::Begin:
if (position < 0)
THROW_IO_EXCEPTION("Position outside stream bounds");
{
atError("Position outside stream bounds");
return;
}
if ((atUint64)position > m_length)
THROW_IO_EXCEPTION("data exceeds available buffer space");
{
atError("data exceeds available buffer space");
return;
}
m_position = position;
break;
case SeekOrigin::Current:
if ((((atInt64)m_position + position) < 0))
THROW_IO_EXCEPTION("Position outside stream bounds");
{
atError("Position outside stream bounds");
return;
}
if (m_position + position > m_length)
THROW_IO_EXCEPTION("data exceeds available buffer space");
{
atError("data exceeds available buffer space");
return;
}
m_position += position;
break;
case SeekOrigin::End:
if (((atInt64)m_length - position) < 0)
THROW_IO_EXCEPTION("Position outside stream bounds");
{
atError("Position outside stream bounds");
return;
}
if ((atUint64)position > m_length)
THROW_IO_EXCEPTION("data exceeds available buffer space");
{
atError("data exceeds available buffer space");
return;
}
m_position = m_length - position;
break;
@ -102,7 +128,10 @@ void MemoryCopyWriter::seek(atInt64 position, SeekOrigin origin)
{
case SeekOrigin::Begin:
if (position < 0)
THROW_IO_EXCEPTION("Position outside stream bounds");
{
atError("Position outside stream bounds");
return;
}
if ((atUint64)position > m_length)
resize(position);
@ -112,7 +141,10 @@ void MemoryCopyWriter::seek(atInt64 position, SeekOrigin origin)
case SeekOrigin::Current:
if ((((atInt64)m_position + position) < 0))
THROW_IO_EXCEPTION("Position outside stream bounds");
{
atError("Position outside stream bounds");
return;
}
if (m_position + position > m_length)
resize(m_position + position);
@ -122,7 +154,10 @@ void MemoryCopyWriter::seek(atInt64 position, SeekOrigin origin)
case SeekOrigin::End:
if (((atInt64)m_length - position) < 0)
THROW_IO_EXCEPTION("Position outside stream bounds");
{
atError("Position outside stream bounds");
return;
}
if ((atUint64)position > m_length)
resize(position);
@ -160,7 +195,10 @@ atUint8* MemoryWriter::data() const
void MemoryWriter::save(const std::string& filename)
{
if (filename.empty() && m_filepath.empty())
THROW_INVALID_OPERATION_EXCEPTION("No file specified, cannot save.");
{
atError("No file specified, cannot save.");
return;
}
if (!filename.empty())
m_filepath = filename;
@ -168,7 +206,10 @@ void MemoryWriter::save(const std::string& filename)
FILE* out = fopen(m_filepath.c_str(), "wb");
if (!out)
THROW_FILE_NOT_FOUND_EXCEPTION(m_filepath);
{
atError("Unable to open file '%s'", m_filepath.c_str());
return;
}
atUint64 done = 0;
atUint64 blocksize = BLOCKSZ;
@ -181,7 +222,10 @@ void MemoryWriter::save(const std::string& filename)
atInt64 ret = fwrite(m_data + done, 1, blocksize, out);
if (ret < 0)
THROW_IO_EXCEPTION("Error writing data to disk");
{
atError("Error writing data to disk");
return;
}
else if (ret == 0)
break;
@ -195,10 +239,16 @@ void MemoryWriter::save(const std::string& filename)