* Add Variadic arguments to THROW_* macros

This commit is contained in:
Phillip Stephens 2014-05-30 17:40:35 -07:00
parent 32f5b9cc03
commit 05db171ac4
9 changed files with 60 additions and 66 deletions

View File

@ -17,6 +17,8 @@
#define __EXCEPTION_HPP__ #define __EXCEPTION_HPP__
#include <string> #include <string>
#include <stdarg.h>
#include "Athena/Utility.hpp"
#define __STRX(x) #x #define __STRX(x) #x
#define __STR(x) __STRX(x) #define __STR(x) __STRX(x)
@ -76,7 +78,10 @@ protected:
}; };
} // error } // error
} // Athena } // Athena
#define THROW_EXCEPTION(msg) \ #define THROW_EXCEPTION(args...) \
do { throw Athena::error::Exception("Exception: " msg, __FILE__, __PRETTY_FUNCTION__, __LINE__); } while(0) do { \
std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::Exception(std::string("Exception: ")+msg, __FILE__, __PRETTY_FUNCTION__, __LINE__); \
} while(0)
#endif #endif

View File

@ -47,7 +47,9 @@ public:
} // error } // error
} // Athena } // Athena
#define THROW_IO_EXCEPTION(msg) \ #define THROW_IO_EXCEPTION(args...) \
do { throw Athena::error::IOException("IOException: " msg, __FILE__, __PRETTY_FUNCTION__, __LINE__); } while(0) do { \
std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::IOException(std::string("IOException: ")+msg, __FILE__, __PRETTY_FUNCTION__, __LINE__); \
} while(0)
#endif #endif

View File

@ -43,7 +43,10 @@ public:
} // error } // error
} // Athena } // Athena
#define THROW_INVALID_DATA_EXCEPTION(msg) \ #define THROW_INVALID_DATA_EXCEPTION(args...) \
do { throw Athena::error::InvalidDataException("InvalidDataException: " msg, __FILE__, __PRETTY_FUNCTION__, __LINE__); } while(0) do { \
std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::InvalidDataException(std::string("InvalidDataException: ")+msg, __FILE__, __PRETTY_FUNCTION__, __LINE__); \
} while(0)
#endif // INVALIDDATAEXCEPTION_HPP #endif // INVALIDDATAEXCEPTION_HPP

View File

@ -17,6 +17,7 @@
#define __INVALID_OPERATION_EXCEPTION_HPP__ #define __INVALID_OPERATION_EXCEPTION_HPP__
#include <string> #include <string>
#include <stdarg.h>
#include "Exception.hpp" #include "Exception.hpp"
namespace Athena namespace Athena
@ -46,7 +47,9 @@ public:
} // error } // error
} // Athena } // Athena
#define THROW_INVALID_OPERATION_EXCEPTION(msg) \ #define THROW_INVALID_OPERATION_EXCEPTION(args...) \
do { throw Athena::error::InvalidOperationException("InvalidOperationException: " msg, __FILE__, __PRETTY_FUNCTION__, __LINE__); } while(0) do { \
std::string msg = Athena::utility::sprintf(args); \
throw Athena::error::InvalidOperationException(std::string("InvalidOperationException: ")+msg, __FILE__, __PRETTY_FUNCTION__, __LINE__); \
} while(0)
#endif // __INVALID_OPERATION_EXCEPTION_HPP__ #endif // __INVALID_OPERATION_EXCEPTION_HPP__

View File

@ -19,6 +19,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <stdarg.h>
#include "Types.hpp" #include "Types.hpp"
namespace Athena namespace Athena
@ -45,7 +46,8 @@ std::vector<std::string> split(const std::string &s, char delim);
std::string join(const std::vector<std::string>& elems, const std::string& delims); std::string join(const std::vector<std::string>& elems, const std::string& delims);
void tolower(std::string& str); void tolower(std::string& str);
void toupper(std::string& str); void toupper(std::string& str);
std::string stdsprintf(const char* fmt, ...); std::string vsprintf(const char* fmt, va_list list);
std::string sprintf(const char* fmt, ...);
bool parseBool(const std::string& boolean, bool* valid = NULL); bool parseBool(const std::string& boolean, bool* valid = NULL);
int countChar(const std::string& str, const char chr, int* lastOccur = NULL); int countChar(const std::string& str, const char chr, int* lastOccur = NULL);

View File

@ -17,7 +17,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#ifndef _LZO_H #ifndef LZO_H
#define LZO_H #define LZO_H
#include "Athena/Types.hpp" #include "Athena/Types.hpp"

View File

@ -99,17 +99,17 @@ void BinaryReader::seek(Int64 position, SeekOrigin origin)
{ {
case SeekOrigin::Begin: case SeekOrigin::Begin:
if ((position < 0 || (Int64)position > (Int64)m_length)) if ((position < 0 || (Int64)position > (Int64)m_length))
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", position);
m_position = position; m_position = position;
break; break;
case SeekOrigin::Current: case SeekOrigin::Current:
if ((((Int64)m_position + position) < 0 || (m_position + position) > m_length)) if ((((Int64)m_position + position) < 0 || (m_position + position) > m_length))
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", position);
m_position += position; m_position += position;
break; break;
case SeekOrigin::End: case SeekOrigin::End:
if ((((Int64)m_length - position < 0) || (m_length - position) > m_length)) if ((((Int64)m_length - position < 0) || (m_length - position) > m_length))
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", position);
m_position = m_length - position; m_position = m_length - position;
break; break;
} }
@ -165,7 +165,7 @@ void BinaryReader::seekBit(int bit)
loadData(); loadData();
if (bit < 0 || bit > 7) if (bit < 0 || bit > 7)
THROW_INVALID_OPERATION_EXCEPTION("bit out of range"); THROW_INVALID_OPERATION_EXCEPTION("bit out of range %i %s", bit, (bit < 0 ? "< 0" : "> 7"));
m_bitPosition = bit; m_bitPosition = bit;
} }
@ -175,7 +175,7 @@ bool BinaryReader::readBit()
if (!m_data) if (!m_data)
loadData(); loadData();
if (m_position > m_length) if (m_position > m_length)
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", m_position);
bool ret = (*(Uint8*)(m_data + m_position) & (1 << m_bitPosition)); bool ret = (*(Uint8*)(m_data + m_position) & (1 << m_bitPosition));
@ -200,7 +200,7 @@ Int8 BinaryReader::readByte()
m_position += sizeof(Uint8); m_position += sizeof(Uint8);
} }
if (m_position + 1 > m_length) if (m_position + 1 > m_length)
THROW_IO_EXCEPTION("Stream::readByte -> Position passed stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", m_position);
return *(Int8*)(m_data + m_position++); return *(Int8*)(m_data + m_position++);
} }
@ -219,7 +219,7 @@ Uint8 BinaryReader::readUByte()
m_position += sizeof(Uint8); m_position += sizeof(Uint8);
} }
if (m_position + 1 > m_length) if (m_position + 1 > m_length)
THROW_IO_EXCEPTION("Position passed stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", m_position);
return *(Uint8*)(m_data + m_position++); return *(Uint8*)(m_data + m_position++);
} }
@ -241,7 +241,7 @@ Uint8* BinaryReader::readUBytes(Int64 length)
} }
if (m_position + length > m_length) if (m_position + length > m_length)
THROW_IO_EXCEPTION("Position passed stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", m_position);
Uint8* ret; Uint8* ret;
ret = new Uint8[length]; ret = new Uint8[length];
@ -263,7 +263,7 @@ Int16 BinaryReader::readInt16()
} }
if (m_position + sizeof(Int16) > m_length) if (m_position + sizeof(Int16) > m_length)
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", m_position);
Int16 ret = *(Int16*)(m_data + m_position); Int16 ret = *(Int16*)(m_data + m_position);
m_position += sizeof(Int16); m_position += sizeof(Int16);
@ -289,7 +289,7 @@ Int32 BinaryReader::readInt32()
} }
if (m_position + sizeof(Int32) > m_length) if (m_position + sizeof(Int32) > m_length)
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", m_position);
Int32 ret = *(Int32*)(m_data + m_position); Int32 ret = *(Int32*)(m_data + m_position);
m_position += 4; m_position += 4;
@ -300,23 +300,7 @@ Int32 BinaryReader::readInt32()
Uint32 BinaryReader::readUint32() Uint32 BinaryReader::readUint32()
{ {
if (!m_data) return readInt32();
loadData();
if (m_bitPosition > 0)
{
m_bitPosition = 0;
m_position += sizeof(Uint8);
}
if (m_position + sizeof(Uint32) > m_length)
THROW_IO_EXCEPTION("Position outside stream bounds");
Uint32 ret = *(Uint32*)(m_data + m_position);
m_position += 4;
if ((!utility::isSystemBigEndian() && isBigEndian()) || (utility::isSystemBigEndian() && isLittleEndian()))
ret = utility::swapU32(ret);
return ret;
} }
Int64 BinaryReader::readInt64() Int64 BinaryReader::readInt64()
@ -330,7 +314,7 @@ Int64 BinaryReader::readInt64()
m_position += sizeof(Uint8); m_position += sizeof(Uint8);
} }
if (m_position + sizeof(Int64) > m_length) if (m_position + sizeof(Int64) > m_length)
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", m_position);
Int64 ret = *(Int64*)(m_data + m_position); Int64 ret = *(Int64*)(m_data + m_position);
m_position += 8; m_position += 8;
@ -342,22 +326,7 @@ Int64 BinaryReader::readInt64()
Uint64 BinaryReader::readUint64() Uint64 BinaryReader::readUint64()
{ {
if (!m_data) return readUint64();
loadData();
if (m_bitPosition > 0)
{
m_bitPosition = 0;
m_position += sizeof(Uint8);
}
if (m_position + sizeof(Uint64) > m_length)
THROW_IO_EXCEPTION("Position outside stream bounds");
Uint64 ret = *(Uint64*)(m_data + m_position);
m_position += 8;
if ((!utility::isSystemBigEndian() && isBigEndian()) || (utility::isSystemBigEndian() && isLittleEndian()))
ret = utility::swapU64(ret);
return ret;
} }
float BinaryReader::readFloat() float BinaryReader::readFloat()
@ -371,7 +340,7 @@ float BinaryReader::readFloat()
m_position += sizeof(Uint8); m_position += sizeof(Uint8);
} }
if (m_position + sizeof(float) > m_length) if (m_position + sizeof(float) > m_length)
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", m_position);
float ret = *(float*)(m_data + m_position); float ret = *(float*)(m_data + m_position);
m_position += 4; m_position += 4;
@ -392,7 +361,7 @@ double BinaryReader::readDouble()
m_position += sizeof(Uint8); m_position += sizeof(Uint8);
} }
if (m_position + sizeof(double) > m_length) if (m_position + sizeof(double) > m_length)
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", m_position);
double ret = *(double*)(m_data + m_position); double ret = *(double*)(m_data + m_position);
m_position += 8; m_position += 8;
@ -414,7 +383,7 @@ bool BinaryReader::readBool()
m_position += sizeof(Uint8); m_position += sizeof(Uint8);
} }
if (m_position + sizeof(bool) > m_length) if (m_position + sizeof(bool) > m_length)
THROW_IO_EXCEPTION("Position outside stream bounds"); THROW_IO_EXCEPTION("Position %0.16X outside stream bounds ", m_position);
bool ret = *(bool*)(m_data + m_position); bool ret = *(bool*)(m_data + m_position);
m_position += 1; m_position += 1;

View File

@ -86,6 +86,11 @@ bool FileWriter::isOpen() const
return m_fileHandle != NULL; return m_fileHandle != NULL;
} }
bool FileWriter::save()
{
return true;
}
void FileWriter::seek(Int64 pos, SeekOrigin origin) void FileWriter::seek(Int64 pos, SeekOrigin origin)
{ {
if (fseeko64(m_fileHandle, pos, (int)origin) != 0) if (fseeko64(m_fileHandle, pos, (int)origin) != 0)

View File

@ -151,26 +151,31 @@ void toupper(std::string& str)
std::transform(str.begin(), str.end(), str.begin(), ::toupper); std::transform(str.begin(), str.end(), str.begin(), ::toupper);
} }
std::string sprintf(const char* fmt, ...) std::string vsprintf(const char* fmt, va_list list)
{ {
int size = 512; int size = 512;
char* buffer = 0; char* buffer = 0;
buffer = new char[size]; buffer = new char[size];
va_list vl; int nsize = vsnprintf(buffer, size, fmt, list);
va_start(vl, fmt); while(size<=nsize)
int nsize = vsnprintf(buffer, size, fmt, vl);
if(size<=nsize)
{ //fail delete buffer and try again { //fail delete buffer and try again
delete[] buffer; delete[] buffer;
buffer = 0; buffer = 0;
buffer = new char[nsize+1]; //+1 for /0 buffer = new char[nsize+1]; //+1 for /0
nsize = vsnprintf(buffer, size, fmt, vl); nsize = vsnprintf(buffer, size, fmt, list);
} }
std::string ret(buffer); std::string ret(buffer);
va_end(vl);
delete[] buffer; delete[] buffer;
return ret; return ret;
} }
std::string sprintf(const char* fmt, ...)
{
va_list vl;
va_start(vl, fmt);
std::string ret = vsprintf(fmt, vl);
va_end(vl);
return ret;
}
bool parseBool(const std::string& boolean, bool* valid) bool parseBool(const std::string& boolean, bool* valid)
{ {