From 4fbd5b7c0a9a387edd9d7c80c3d439f4f77a4d7e Mon Sep 17 00:00:00 2001 From: Marrub Date: Wed, 29 Jul 2015 08:30:16 -0400 Subject: [PATCH] KAY NEVERMIND TODAY I LEARNED MSVC HAS ABSOLUTELY NOTHING AND IS HORRIBLE IN EVERY POSSIBLE WAY IT COULD BE. --- COMPILING.txt | 2 ++ lua/tokenizer.lua | 28 ++++++++++++----------- src/lt.c | 32 ++++++++++++++++---------- src/lt.h | 58 +++++++++++++++++++++++++++-------------------- 4 files changed, 70 insertions(+), 50 deletions(-) diff --git a/COMPILING.txt b/COMPILING.txt index 7cecbc7..7909ca6 100644 --- a/COMPILING.txt +++ b/COMPILING.txt @@ -4,5 +4,7 @@ You can compile with the LT_NO_ICONV definition to disable iconv. Compile lt.c to an object file and statically or dynamically link it with your application. That's it. Don't forget to include lt.h. +If you don't want to export it to a DLL/SO/whatever, define LT_NO_EXPORT. + Also, compiling with GDCC ( http://github.com/DavidPH/GDCC ) works. It will automatically omit iconv and use some specialized functions to work. diff --git a/lua/tokenizer.lua b/lua/tokenizer.lua index 00a71e6..e6eec5c 100644 --- a/lua/tokenizer.lua +++ b/lua/tokenizer.lua @@ -28,11 +28,13 @@ local tokenizer = {} local loveToken = ffi.load("LoveToken") ffi.cdef([[ +typedef int LT_BOOL; + typedef struct { - bool escapeChars; - bool stripInvalid; - bool doConvert; + LT_BOOL escapeChars; + LT_BOOL stripInvalid; + LT_BOOL doConvert; const char *fromCode; const char *toCode; const char *stringChars; @@ -41,33 +43,33 @@ typedef struct typedef struct { - char *token; + const char *token; char *string; int pos; } LT_Token; typedef struct { - bool failure; + LT_BOOL failure; const char *str; } LT_AssertInfo; void LT_Init(LT_Config initCfg); void LT_SetConfig(LT_Config newCfg); -void LT_Quit(); +void LT_Quit(void); -bool LT_Assert(bool assertion, const char *str, ...); -LT_AssertInfo LT_CheckAssert(); +LT_BOOL LT_Assert(LT_BOOL assertion, const char *fmt, ...); +LT_AssertInfo LT_CheckAssert(void); -bool LT_OpenFile(const char *filePath); +LT_BOOL LT_OpenFile(const char *filePath); void LT_SetPos(int newPos); -void LT_CloseFile(); +void LT_CloseFile(void); -char *LT_ReadNumber(); +char *LT_ReadNumber(void); char *LT_ReadString(char term); char *LT_Escaper(char *str, size_t pos, char escape); -LT_Token LT_GetToken(); -void LT_SkipWhite(); +LT_Token LT_GetToken(void); +void LT_SkipWhite(void); ]]) local pReturn diff --git a/src/lt.c b/src/lt.c index edfaa80..c823803 100644 --- a/src/lt.c +++ b/src/lt.c @@ -251,7 +251,7 @@ void LT_Init(LT_Config initCfg) if(cfg.stringChars != NULL) { - int i; + unsigned i; stringChars = LT_Alloc(6); @@ -276,7 +276,7 @@ void LT_Init(LT_Config initCfg) if(cfg.charChars != NULL) { - int i; + unsigned i; charChars = LT_Alloc(6); @@ -338,7 +338,7 @@ void LT_SetConfig(LT_Config newCfg) if(cfg.stringChars != NULL) { - int i; + unsigned i; stringChars = LT_Alloc(6); @@ -363,7 +363,7 @@ void LT_SetConfig(LT_Config newCfg) if(cfg.charChars != NULL) { - int i; + unsigned i; charChars = LT_Alloc(6); @@ -430,13 +430,13 @@ LT_BOOL LT_Assert(LT_BOOL assertion, const char *fmt, ...) assertError = LT_TRUE; assertString = malloc(512); - snprintf(ftString, 16, ":%ld:", ftell(parseFile)); + sprintf(ftString, ":%ld:", ftell(parseFile)); va_start(va, fmt); - vsnprintf(asBuffer, 512, fmt, va); + vsprintf(asBuffer, fmt, va); va_end(va); - snprintf(assertString, 512, "%s%s", ftString, asBuffer); + sprintf(assertString, "%s%s", ftString, asBuffer); LT_SetGarbage(assertString); } @@ -606,6 +606,8 @@ char *LT_ReadString(char term) char *LT_Escaper(char *str, size_t pos, char escape) { + unsigned i; + switch(escape) { case '\\': case '\'': case '"': str[pos] = escape; break; @@ -617,7 +619,7 @@ char *LT_Escaper(char *str, size_t pos, char escape) case 't': str[pos] = '\t'; break; case 'v': str[pos] = '\v'; break; case 'x': // [marrub] THIS ONE IS FUN - for(unsigned int i = 0;;) + for(i = 0;;) { int c = fgetc(parseFile); @@ -653,9 +655,11 @@ char *LT_Escaper(char *str, size_t pos, char escape) case '4': case '5': case '6': case '7': { int c = escape; - unsigned int i = 0; + unsigned n; - for(unsigned int n = 2; n != 0; n--) + i = 0; + + for(n = 2; n != 0; n--) { switch(c) { @@ -914,7 +918,9 @@ LT_Token LT_GetToken() if(stringChars[0] != '\0') { - for(size_t i = 0; i < 6;) + unsigned i; + + for(i = 0; i < 6;) { char cc = stringChars[i++]; @@ -933,7 +939,9 @@ LT_Token LT_GetToken() if(charChars[0] != '\0') { - for(size_t i = 0; i < 6;) + unsigned i; + + for(i = 0; i < 6;) { char cc = charChars[i++]; diff --git a/src/lt.h b/src/lt.h index 0238e7e..bc96fae 100644 --- a/src/lt.h +++ b/src/lt.h @@ -41,12 +41,20 @@ THE SOFTWARE. // [marrub] When using in FFI, remove this from the declarations. // Also make sure to redefine this if your platform is not supported. // (OSX shouldn't need this at all) -#if defined(_MSC_VER) - #define LT_EXPORT __declspec(dllexport) -#elseif defined(_GCC) - #define LT_EXPORT __attribute__((visibility("default"))) +#ifndef LT_NO_EXPORT + #if defined(_MSC_VER) + #define LT_DLLEXPORT __declspec(dllexport) + #define LT_EXPORT + #elif defined(_GCC) + #define LT_EXPORT __attribute__((visibility("default"))) + #define LT_DLLEXPORT + #else + #define LT_EXPORT + #define LT_DLLEXPORT + #endif #else #define LT_EXPORT + #define LT_DLLEXPORT #endif #ifdef __GDCC__ @@ -81,12 +89,14 @@ enum * Types */ +typedef int LT_BOOL; + typedef struct { - bool escapeChars; - bool stripInvalid; + LT_BOOL escapeChars; + LT_BOOL stripInvalid; #ifndef LT_NO_ICONV - bool doConvert; + LT_BOOL doConvert; const char *fromCode; const char *toCode; #endif @@ -103,7 +113,7 @@ typedef struct typedef struct { - bool failure; + LT_BOOL failure; const char *str; } LT_AssertInfo; @@ -113,8 +123,6 @@ typedef struct LT_GarbageList_s void *ptr; } LT_GarbageList; // [marrub] Don't include this into FFI declarations. -typedef int LT_BOOL; - /* * Functions */ @@ -123,27 +131,27 @@ typedef int LT_BOOL; extern "C" { #endif -void LT_EXPORT LT_Init(LT_Config initCfg); -void LT_EXPORT LT_SetConfig(LT_Config newCfg); -void LT_EXPORT LT_Quit(void); +LT_DLLEXPORT void LT_EXPORT LT_Init(LT_Config initCfg); +LT_DLLEXPORT void LT_EXPORT LT_SetConfig(LT_Config newCfg); +LT_DLLEXPORT void LT_EXPORT LT_Quit(void); -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); +LT_DLLEXPORT LT_BOOL LT_EXPORT LT_Assert(LT_BOOL assertion, const char *fmt, ...); +LT_DLLEXPORT void LT_EXPORT LT_Error(int type); // [marrub] C use ONLY +LT_DLLEXPORT LT_AssertInfo LT_EXPORT LT_CheckAssert(void); #ifndef __GDCC__ -bool LT_EXPORT LT_OpenFile(const char *filePath); +LT_DLLEXPORT LT_BOOL LT_EXPORT LT_OpenFile(const char *filePath); #else -bool LT_EXPORT LT_OpenFile(__str filePath); +LT_DLLEXPORT LT_BOOL LT_EXPORT LT_OpenFile(__str filePath); #endif -void LT_EXPORT LT_SetPos(int newPos); -void LT_EXPORT LT_CloseFile(void); +LT_DLLEXPORT void LT_EXPORT LT_SetPos(int newPos); +LT_DLLEXPORT void LT_EXPORT LT_CloseFile(void); -char *LT_EXPORT LT_ReadNumber(void); -char *LT_EXPORT LT_ReadString(char term); -char *LT_EXPORT LT_Escaper(char *str, size_t pos, char escape); -LT_Token LT_EXPORT LT_GetToken(void); -void LT_EXPORT LT_SkipWhite(void); +LT_DLLEXPORT char *LT_EXPORT LT_ReadNumber(void); +LT_DLLEXPORT char *LT_EXPORT LT_ReadString(char term); +LT_DLLEXPORT char *LT_EXPORT LT_Escaper(char *str, size_t pos, char escape); +LT_DLLEXPORT LT_Token LT_EXPORT LT_GetToken(void); +LT_DLLEXPORT void LT_EXPORT LT_SkipWhite(void); #ifdef __cplusplus }