#include #include #include #include "CTextParsers.h" CTextParsers g_TextParse; static int g_ini_chartable1[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; } bool CTextParsers::ParseFile_SMC(const char *file, ITextListener_SMC *smc_listener, unsigned int *line, unsigned int *col) { /* :TODO: Implement this */ if (line) { *line = 0; } 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; } /* Preprocess the string before anything */ ptr = buffer; /* First strip beginning whitespace */ while ((*ptr != '\0') && isspace(*ptr)) { 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; 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; 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; }