From 119cadadc4f7430ddbc39d686cd7434ae4dc71be Mon Sep 17 00:00:00 2001 From: Marrub Date: Fri, 5 Jun 2015 11:14:35 -0400 Subject: [PATCH] informative git log --- .gitignore | 1 + Makefile | 8 +-- parser.lua => lua/parser.lua | 0 lt.c => src/lt.c | 97 ++++++++++++++++++++++++++++++++---- lt.h => src/lt.h | 18 +++++-- 5 files changed, 108 insertions(+), 16 deletions(-) rename parser.lua => lua/parser.lua (100%) rename lt.c => src/lt.c (83%) rename lt.h => src/lt.h (87%) diff --git a/.gitignore b/.gitignore index d78137b..18ad5e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +src/*iconv* bin/* test/* test diff --git a/Makefile b/Makefile index 912e692..f6ec608 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,8 @@ all: mkdir -p bin - mingw32-gcc --std=c99 -g -ggdb -c -o bin/lt.o lt.c - mingw32-gcc -shared -g -ggdb -o bin/LoveToken.dll bin/lt.o -Wl,--out-implib,bin/libLoveToken.a - # cp bin/LoveToken.dll test/LoveToken.dll + mingw32-gcc --std=c99 -g -ggdb -liconv -c -o bin/lt.o src/lt.c -liconv + mingw32-gcc -shared -g -ggdb -liconv -o bin/LoveToken.dll bin/lt.o -Wl,--out-implib,bin/libLoveToken.a -liconv +#mingw32-gcc --std=c99 -g -ggdb -c -o bin/lt.o -DI_FUCKING_HATE_WINDOWS src/lt.c +#mingw32-gcc -shared -g -ggdb -o bin/LoveToken.dll bin/iconv.o bin/lt.o -Wl,--out-implib,bin/libLoveToken.a +#cp bin/LoveToken.dll test/LoveToken.dll diff --git a/parser.lua b/lua/parser.lua similarity index 100% rename from parser.lua rename to lua/parser.lua diff --git a/lt.c b/src/lt.c similarity index 83% rename from lt.c rename to src/lt.c index 1579698..1fa937f 100644 --- a/lt.c +++ b/src/lt.c @@ -21,17 +21,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include -#include -#include #include "lt.h" -FILE *LT_ParseFile; -bool LT_AssertError = false; LT_GarbageList *gbHead, *gbRover; +static FILE *LT_ParseFile; static LT_InitInfo info; +static iconv_t icDesc; +static bool LT_AssertError = false; +static const char *LT_AssertString; static char *tokenTypes[] = { // [marrub] So, this was an interesting bug. This was completely misordered from the enum. @@ -45,10 +43,35 @@ static char *tokenTypes[] = { "TOK_Identi", "TOK_EOF", "TOK_ChrSeq" }; +static void LT_DoConvert(char **str) +{ + size_t i = strlen(*str); + char *strbuf = calloc((i * 6) + 1, 1); + char *strbufOrig = strbuf, *strOrig = *str; + size_t in = i, out = i * 6; + + iconv(icDesc, str, &in, &strbuf, &out); + + *str = strOrig, strbuf = strbufOrig; + + free(*str); + *str = strbuf; +} + void LT_Init(LT_InitInfo initInfo) { info = initInfo; + if(info.doConvert) + { + icDesc = iconv_open(info.toCode, info.fromCode); + + if(icDesc == (iconv_t) -1) + { + LT_Assert(true, "failure opening iconv"); + } + } + gbHead = malloc(sizeof(LT_GarbageList)); gbHead->next = NULL; gbHead->ptr = NULL; @@ -58,6 +81,11 @@ void LT_Init(LT_InitInfo initInfo) void LT_Quit() { + if(info.doConvert) + { + iconv_close(icDesc); + } + gbRover = gbHead; while(gbRover != NULL) @@ -84,12 +112,21 @@ bool LT_Assert(bool assertion, const char *str) if(assertion) { LT_AssertError = true; + LT_AssertString = str; fprintf(stderr, "LT_Assert: %s", str); } return assertion; } +LT_AssertInfo LT_CheckAssert() +{ + LT_AssertInfo ltAssertion; + ltAssertion.failure = LT_AssertError; + ltAssertion.str = LT_AssertString; + return ltAssertion; +} + bool LT_OpenFile(const char *filePath) { LT_ParseFile = fopen(filePath, "r"); @@ -131,11 +168,21 @@ char *LT_ReadNumber() realloc(str, TOKEN_STR_BLOCK_LENGTH * str_blocks++); } - str[i++] = ((info.stripInvalid && (isspace(c) || isprint(c))) || !info.stripInvalid) ? c : ' '; + str[i++] = c; + + if(info.stripInvalid) + { + str[i++] = (isspace(c) || isprint(c)) ? c : ' '; + } } str[i++] = '\0'; + if(info.doConvert) + { + LT_DoConvert(&str); + } + gbRover->next = malloc(sizeof(LT_GarbageList)); gbRover = gbRover->next; gbRover->ptr = realloc(str, i); @@ -188,12 +235,22 @@ char *LT_ReadString(char term) realloc(str, TOKEN_STR_BLOCK_LENGTH * str_blocks++); } - str[i++] = ((info.stripInvalid && (isspace(c) || isprint(c))) || !info.stripInvalid) ? c : ' '; + str[i++] = c; + + if(info.stripInvalid) + { + str[i++] = (isspace(c) || isprint(c)) ? c : ' '; + } } } str[i++] = '\0'; + if(info.doConvert) + { + LT_DoConvert(&str); + } + gbRover->next = malloc(sizeof(LT_GarbageList)); gbRover = gbRover->next; gbRover->ptr = realloc(str, i); @@ -465,12 +522,23 @@ LT_Token LT_GetToken() realloc(str, TOKEN_STR_BLOCK_LENGTH * str_blocks++); } - str[i++] = ((info.stripInvalid && (isspace(c) || isprint(c))) || !info.stripInvalid) ? c : ' '; + str[i++] = c; + + if(info.stripInvalid) + { + str[i++] = (isspace(c) || isprint(c)) ? c : ' '; + } + fread(&c, 1, 1, LT_ParseFile); } str[i++] = '\0'; // [marrub] Completely forgot this line earlier. Really screwed up everything. + if(info.doConvert) + { + LT_DoConvert(&str); + } + gbRover->next = malloc(sizeof(LT_GarbageList)); gbRover = gbRover->next; gbRover->ptr = realloc(str, i); @@ -483,6 +551,17 @@ LT_Token LT_GetToken() return tk; } + tk.string = malloc(2); + tk.string[0] = c; + tk.string[1] = '\0'; + + gbRover->next = malloc(sizeof(LT_GarbageList)); + gbRover = gbRover->next; + gbRover->ptr = tk.string; + gbRover->next = NULL; + + tk.token = tokenTypes[TOK_ChrSeq]; + return tk; } diff --git a/lt.h b/src/lt.h similarity index 87% rename from lt.h rename to src/lt.h index 08a87eb..0901ec4 100644 --- a/lt.h +++ b/src/lt.h @@ -25,8 +25,12 @@ THE SOFTWARE. #define LOVETOKEN_LT_H #include -#include +#include +#include #include +#include + +#include #define TOKEN_STR_BLOCK_LENGTH 512 @@ -38,6 +42,9 @@ typedef struct { bool escapeChars; bool stripInvalid; + bool doConvert; + const char *fromCode; + const char *toCode; } LT_InitInfo; typedef struct @@ -47,11 +54,16 @@ typedef struct int pos; } LT_Token; -extern bool LT_EXPORT LT_AssertError; +typedef struct +{ + bool failure; + const char *str; +} LT_AssertInfo; void LT_EXPORT LT_Init(LT_InitInfo initInfo); void LT_EXPORT LT_Quit(); bool LT_EXPORT LT_Assert(bool assertion, const char *str); +LT_AssertInfo LT_EXPORT LT_CheckAssert(); bool LT_EXPORT LT_OpenFile(const char *filePath); void LT_EXPORT LT_CloseFile(); @@ -69,8 +81,6 @@ typedef struct LT_GarbageList_s void *ptr; } LT_GarbageList; -extern FILE *LT_ParseFile; - enum { TOK_Colon,