From 5eff934ef0ee21da68debafcbc8c1af297bd1523 Mon Sep 17 00:00:00 2001 From: Marrub Date: Thu, 11 Jun 2015 09:42:36 -0400 Subject: [PATCH] gdcc support added --- COMPILING.txt | 8 +++- README.txt | 1 + src/lt.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++- src/lt.h | 10 ++++ 4 files changed, 139 insertions(+), 4 deletions(-) diff --git a/COMPILING.txt b/COMPILING.txt index 1a43586..b8c3f41 100644 --- a/COMPILING.txt +++ b/COMPILING.txt @@ -1,2 +1,6 @@ -Compiling LoveToken is near trivial since it only needs iconv and C99. -Build an object file from lt.c and link it with iconv into a dll/so/etc. +Compiling LoveToken is near trivial since it only needs C99 (and optionally iconv). +Build an object file from lt.c (and link it with iconv if you want conversion) into a dll/so/etc. +You can compile with the NO_ICONV definition to skip iconv requirements. + +Also, compiling with GDCC ( http://github.com/DavidPH/GDCC ) is now allowed. +It will automatically omit iconv and use some specialized functions to work. diff --git a/README.txt b/README.txt index 3447d16..1774c27 100644 --- a/README.txt +++ b/README.txt @@ -1,3 +1,4 @@ LoveToken is a tokenizer made for usage with LOVE2D's (LuaJIT) FFI. It also works just as well in C or C++, or really anything that can load C functions. See COMPILING.txt for info on compiling. +Supported platforms (known): Microsoft Windows, Linux, ZDoom, Mac OS X diff --git a/src/lt.c b/src/lt.c index 4282955..74be4a7 100644 --- a/src/lt.c +++ b/src/lt.c @@ -27,10 +27,34 @@ THE SOFTWARE. #include #include #include -#include #include #include +#ifdef __GDCC__ + #include +#else + #ifndef NO_ICONV + #include + #endif +#endif + +#ifdef __GDCC__ +#define fopen LT_FOpen +#define ftell LT_FTell +#define fgetc LT_FGetC +#define ungetc LT_UnGetC +#define fseek LT_FSeek +#define fclose LT_FClose +#define FILE LT_File + +typedef struct +{ + __str langId; + __str data; + int pos; +} LT_File; +#endif + /* * Variables */ @@ -38,10 +62,14 @@ THE SOFTWARE. static LT_GarbageList *gbHead, *gbRover; static FILE *parseFile; static LT_Config cfg; + +#ifndef NO_ICONV static iconv_t icDesc; +#endif + static bool assertError = false; static char *assertString; -static char *stringChars = "\"", *charChars = "'"; +static char *stringChars = (char *)"\"", *charChars = (char *)"'"; static const char *errors[] = { "LT_Error: Syntax error", @@ -67,6 +95,7 @@ const char *LT_TkNames[] = { * Functions */ +#ifndef NO_ICONV static void LT_DoConvert(char **str) { size_t i = strlen(*str); @@ -81,6 +110,7 @@ static void LT_DoConvert(char **str) free(*str); *str = strbuf; } +#endif static void *LT_Alloc(size_t size) { @@ -108,24 +138,93 @@ static void *LT_ReAlloc(void *ptr, size_t newSize) static void *LT_SetGarbage(void *p) { +#ifndef __GDCC__ gbRover->next = LT_Alloc(sizeof(LT_GarbageList)); gbRover = gbRover->next; gbRover->ptr = p; gbRover->next = NULL; return gbRover->ptr; +#else + return p; +#endif } +#ifdef __GDCC__ +#define StrParam(...) \ + ( \ + ACS_BeginStrParam(), \ + __nprintf(__VA_ARGS__), \ + ACS_EndStrParam() \ + ) +#define StrParamL(...) (StrParam("%LS", StrParam(__VA_ARGS__))) + +LT_File *LT_FOpen(__str languageId, const char *mode) +{ + LT_File *file = LT_Alloc(sizeof(LT_File)); + + file->langId = languageId; + file->data = StrParamL("%S", languageId); + file->pos = 0; + + return file; +} + +int LT_FTell(LT_File *file) +{ + return file->pos; +} + +int LT_FGetC(LT_File *file) +{ + int c = ACS_GetChar(file->data, file->pos++); + return c < 1 ? EOF : c; +} + +int LT_UnGetC(int ch, LT_File *file) +{ + int c = ACS_GetChar(file->data, file->pos--); + return c < 1 ? EOF : c; +} + +int LT_FSeek(LT_File *file, long int offset, int whence) +{ + switch(whence) + { + case SEEK_SET: + file->pos = offset; + return 0; + case SEEK_CUR: + file->pos += offset; + return 0; + case SEEK_END: + file->pos = ACS_StrLen(file->data) + offset; + return 0; + } + + return 1; +} + +int LT_FClose(LT_File *file) +{ + free(file); + return 0; +} +#endif + void LT_Init(LT_Config initCfg) { +#ifndef __GDCC__ gbHead = LT_Alloc(sizeof(LT_GarbageList)); gbHead->next = NULL; gbHead->ptr = NULL; gbRover = gbHead; +#endif cfg = initCfg; +#ifndef NO_ICONV if(cfg.doConvert && cfg.fromCode != NULL && cfg.toCode != NULL) { icDesc = iconv_open(cfg.toCode, cfg.fromCode); @@ -144,6 +243,7 @@ void LT_Init(LT_Config initCfg) { cfg.stripInvalid = false; } +#endif if(cfg.stringChars != NULL) { @@ -200,6 +300,7 @@ void LT_SetConfig(LT_Config newCfg) { cfg = newCfg; +#ifndef NO_ICONV if(cfg.doConvert && cfg.fromCode != NULL && cfg.toCode != NULL) { if(icDesc != NULL) @@ -229,6 +330,7 @@ void LT_SetConfig(LT_Config newCfg) { cfg.stripInvalid = false; } +#endif if(cfg.stringChars != NULL) { @@ -283,11 +385,14 @@ void LT_SetConfig(LT_Config newCfg) void LT_Quit() { +#ifndef NO_ICONV if(cfg.doConvert) { iconv_close(icDesc); } +#endif +#ifndef __GDCC__ gbRover = gbHead; while(gbRover != NULL) @@ -307,6 +412,7 @@ void LT_Quit() gbRover = NULL; gbHead = NULL; +#endif } bool LT_Assert(bool assertion, const char *fmt, ...) @@ -348,7 +454,11 @@ LT_AssertInfo LT_CheckAssert() return ltAssertion; } +#ifndef __GDCC__ bool LT_OpenFile(const char *filePath) +#else +bool LT_OpenFile(__str filePath) +#endif { parseFile = fopen(filePath, "r"); @@ -363,10 +473,14 @@ bool LT_OpenFile(const char *filePath) void LT_SetPos(int newPos) { +#ifndef __GDCC__ if(fseek(parseFile, newPos, SEEK_SET) != 0) { LT_Assert(ferror(parseFile), "LT_SetPos: %s", strerror(errno)); } +#else + fseek(parseFile, newPos, SEEK_SET); +#endif } void LT_CloseFile() @@ -408,10 +522,12 @@ char *LT_ReadNumber() str[i++] = '\0'; +#ifndef NO_ICONV if(cfg.doConvert) { LT_DoConvert(&str); } +#endif return LT_SetGarbage(LT_ReAlloc(str, i)); } @@ -474,10 +590,12 @@ char *LT_ReadString(char term) str[i++] = '\0'; +#ifndef NO_ICONV if(cfg.doConvert) { LT_DoConvert(&str); } +#endif return LT_SetGarbage(LT_ReAlloc(str, i)); } @@ -856,10 +974,12 @@ LT_Token LT_GetToken() str[i++] = '\0'; // [marrub] Completely forgot this line earlier. Really screwed up everything. +#ifndef NO_ICONV if(cfg.doConvert) { LT_DoConvert(&str); } +#endif ungetc(c, parseFile); diff --git a/src/lt.h b/src/lt.h index 34f538b..777ddbc 100644 --- a/src/lt.h +++ b/src/lt.h @@ -50,6 +50,10 @@ THE SOFTWARE. #define LT_EXPORT #endif +#ifdef __GDCC__ + #define NO_ICONV +#endif + enum { TOK_Colon, TOK_Comma, TOK_Div, TOK_Mod, TOK_Mul, @@ -79,9 +83,11 @@ typedef struct { bool escapeChars; bool stripInvalid; +#ifndef NO_ICONV bool doConvert; const char *fromCode; const char *toCode; +#endif const char *stringChars; const char *charChars; } LT_Config; @@ -117,7 +123,11 @@ bool LT_EXPORT LT_Assert(bool assertion, const char *fmt, ...); void LT_EXPORT LT_Error(int type); // [marrub] C use ONLY LT_AssertInfo LT_EXPORT LT_CheckAssert(void); +#ifndef __GDCC__ bool LT_EXPORT LT_OpenFile(const char *filePath); +#else +bool LT_EXPORT LT_OpenFile(__str filePath); +#endif void LT_EXPORT LT_SetPos(int newPos); void LT_EXPORT LT_CloseFile(void);