#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() { g_ini_chartable1['_'] = 1; g_ini_chartable1['-'] = 1; g_ini_chartable1[','] = 1; g_ini_chartable1['+'] = 1; g_ini_chartable1['.'] = 1; 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, bool strict) { /* This beast is a lot more rigorous than our INI friend. */ return false; } bool CTextParsers::ParseFile_INI(const char *file, ITextListener_INI *ini_listener, unsigned int *line, unsigned int *col) { FILE *fp = fopen(file, "rt"); unsigned int curline = 0; unsigned int curtok; size_t len; if (!fp) { if (line) { *line = 0; } return false; } char buffer[2048]; char *ptr, *save_ptr; bool in_quote; while (!feof(fp)) { curline++; curtok = 0; buffer[0] = '\0'; if (fgets(buffer, sizeof(buffer), fp) == NULL) { break; } //: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' && g_ws_chartable[*ptr] != 0) { ptr++; } len = strlen(ptr); if (!len) { continue; } /* Now search for comment characters */ in_quote = false; save_ptr = ptr; for (size_t i=0; i=0 && iReadINI_RawLine(ptr, &curtok)) { goto event_failed; } if (*ptr == '[') { bool invalid_tokens = false; bool got_bracket = false; bool extra_tokens = false; char c; bool alnum; wchar_t wc; for (size_t i=1; iReadINI_NewSection(&ptr[1], invalid_tokens, got_bracket, extra_tokens, &curtok)) { goto event_failed; } } else { char *key_ptr = ptr; char *val_ptr = NULL; char c; size_t first_space = 0; bool invalid_tokens = false; bool equal_token = false; bool quotes = false; bool alnum; wchar_t wc; for (size_t i=0; iReadINI_KeyValue(key_ptr, val_ptr, invalid_tokens, equal_token, quotes, &curtok)) { curtok = 0; goto event_failed; } } } if (line) { *line = curline; } fclose(fp); return true; event_failed: if (line) { *line = curline; } if (col) { *col = curtok; } fclose(fp); return false; }