marrub
/
LoveToken
Archived
1
0
Fork 0

vararg for assert

master
Marrub 2015-06-07 04:04:23 -04:00
parent 407caaf6f1
commit baa854169c
3 changed files with 18 additions and 11 deletions

View File

@ -56,7 +56,7 @@ void LT_Init(LT_Config initCfg);
void LT_SetConfig(LT_Config newCfg); void LT_SetConfig(LT_Config newCfg);
void LT_Quit(); void LT_Quit();
bool LT_Assert(bool assertion, const char *str); bool LT_Assert(bool assertion, const char *str, ...);
LT_AssertInfo LT_CheckAssert(); LT_AssertInfo LT_CheckAssert();
bool LT_OpenFile(const char *filePath); bool LT_OpenFile(const char *filePath);

View File

@ -29,6 +29,7 @@ THE SOFTWARE.
#include <errno.h> #include <errno.h>
#include <iconv.h> #include <iconv.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
/* /*
* Variables * Variables
@ -308,14 +309,24 @@ void LT_Quit()
gbHead = NULL; gbHead = NULL;
} }
bool LT_Assert(bool assertion, const char *str) bool LT_Assert(bool assertion, const char *fmt, ...)
{ {
if(assertion) if(assertion)
{ {
char ftString[16];
char asBuffer[512];
va_list va;
assertError = true; assertError = true;
assertString = malloc(512); assertString = malloc(512);
snprintf(assertString, 512, ":%ld:%s", ftell(parseFile), str); snprintf(ftString, 16, ":%ld:", ftell(parseFile));
va_start(va, fmt);
vsnprintf(asBuffer, 512, fmt, va);
va_end(va);
snprintf(assertString, 512, "%s%s", ftString, asBuffer);
LT_SetGarbage(LT_ReAlloc(assertString, strlen(assertString) + 1)); LT_SetGarbage(LT_ReAlloc(assertString, strlen(assertString) + 1));
} }
@ -343,11 +354,7 @@ bool LT_OpenFile(const char *filePath)
if(parseFile == NULL) if(parseFile == NULL)
{ {
char *errorStr = LT_Alloc(256); LT_Assert(true, "LT_OpenFile: %s", strerror(errno));
snprintf(errorStr, 256, "LT_OpenFile: %s", strerror(errno));
LT_Assert(true, errorStr);
return false; return false;
} }
@ -358,7 +365,7 @@ void LT_SetPos(int newPos)
{ {
if(fseek(parseFile, newPos, SEEK_SET) != 0) if(fseek(parseFile, newPos, SEEK_SET) != 0)
{ {
LT_Assert(ferror(parseFile), "LT_SetPos: Failed to set position"); LT_Assert(ferror(parseFile), "LT_SetPos: %s", strerror(errno));
} }
} }
@ -553,7 +560,7 @@ char *LT_Escaper(char *str, size_t pos, char escape)
break; break;
default: default:
LT_Assert(true, "LT_Escaper: Unknown escape character"); LT_Assert(true, "LT_Escaper: Unknown escape character '%c'", escape);
break; break;
} }

View File

@ -113,7 +113,7 @@ void LT_EXPORT LT_Init(LT_Config initCfg);
void LT_EXPORT LT_SetConfig(LT_Config newCfg); void LT_EXPORT LT_SetConfig(LT_Config newCfg);
void LT_EXPORT LT_Quit(); void LT_EXPORT LT_Quit();
bool LT_EXPORT LT_Assert(bool assertion, const char *str); bool LT_EXPORT LT_Assert(bool assertion, const char *fmt, ...);
void LT_EXPORT LT_Error(int type); // [marrub] C use ONLY void LT_EXPORT LT_Error(int type); // [marrub] C use ONLY
LT_AssertInfo LT_EXPORT LT_CheckAssert(); LT_AssertInfo LT_EXPORT LT_CheckAssert();