diff --git a/core/CTextParsers.cpp b/core/CTextParsers.cpp index 063d1f3b..eae4f764 100644 --- a/core/CTextParsers.cpp +++ b/core/CTextParsers.cpp @@ -1,11 +1,14 @@ #include #include +#include #include +#include #include "CTextParsers.h" CTextParsers g_TextParse; static int g_ini_chartable1[255] = {0}; +static int g_ws_chartable[255] = {0}; CTextParsers::CTextParsers() { @@ -17,6 +20,17 @@ CTextParsers::CTextParsers() g_ini_chartable1['$'] = 1; g_ini_chartable1['?'] = 1; g_ini_chartable1['/'] = 1; + g_ws_chartable['\n'] = 1; + g_ws_chartable['\v'] = 1; + g_ws_chartable['\r'] = 1; + g_ws_chartable['\t'] = 1; + g_ws_chartable['\f'] = 1; + g_ws_chartable[' '] = 1; +} + +unsigned int CTextParsers::GetUTF8CharBytes(const char *stream) +{ + return _GetUTF8CharBytes(stream); } bool CTextParsers::ParseFile_SMC(const char *file, ITextListener_SMC *smc_listener, unsigned int *line, unsigned int *col) @@ -50,6 +64,7 @@ bool CTextParsers::ParseFile_INI(const char *file, ITextListener_INI *ini_listen char buffer[2048]; char *ptr, *save_ptr; bool in_quote; + while (!feof(fp)) { curline++; @@ -59,12 +74,26 @@ bool CTextParsers::ParseFile_INI(const char *file, ITextListener_INI *ini_listen { break; } - - /* Preprocess the string before anything */ - ptr = buffer; - + + //:TODO: this will only run once, so find a nice way to move it out of the while loop + /* If this is the first line, check the first three bytes for BOM */ + if (curline == 1 && + buffer[0] == (char)0xEF && + buffer[1] == (char)0xBB && + buffer[2] == (char)0xBF) + { + /* We have a UTF-8 marked file... skip these bytes */ + ptr = &buffer[3]; + } else { + ptr = buffer; + } + + /*************************************************** + * We preprocess the string before parsing tokens! * + ***************************************************/ + /* First strip beginning whitespace */ - while ((*ptr != '\0') && isspace(*ptr)) + while (*ptr != '\0' && g_ws_chartable[*ptr] != 0) { ptr++; } @@ -117,7 +146,7 @@ bool CTextParsers::ParseFile_INI(const char *file, ITextListener_INI *ini_listen /* Lastly, strip ending whitespace off */ for (size_t i=len-1; i>=0 && i #include "sourcemm_api.h" #include "sm_version.h" +#include "CTextParsers.h" SourceMod_Core g_SourceMod;