fyrenmoo@3129: /** fyrenmoo@3129: * vim: set ts=4 : fyrenmoo@3129: * ============================================================================= fyrenmoo@3129: * SourceMod fyrenmoo@3129: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. fyrenmoo@3129: * ============================================================================= fyrenmoo@3129: * fyrenmoo@3129: * This program is free software; you can redistribute it and/or modify it under fyrenmoo@3129: * the terms of the GNU General Public License, version 3.0, as published by the fyrenmoo@3129: * Free Software Foundation. fyrenmoo@3129: * fyrenmoo@3129: * This program is distributed in the hope that it will be useful, but WITHOUT fyrenmoo@3129: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS fyrenmoo@3129: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more fyrenmoo@3129: * details. fyrenmoo@3129: * fyrenmoo@3129: * You should have received a copy of the GNU General Public License along with fyrenmoo@3129: * this program. If not, see . fyrenmoo@3129: * fyrenmoo@3129: * As a special exception, AlliedModders LLC gives you permission to link the fyrenmoo@3129: * code of this program (as well as its derivative works) to "Half-Life 2," the fyrenmoo@3129: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software fyrenmoo@3129: * by the Valve Corporation. You must obey the GNU General Public License in fyrenmoo@3129: * all respects for all other code used. Additionally, AlliedModders LLC grants fyrenmoo@3129: * this exception to all derivative works. AlliedModders LLC defines further fyrenmoo@3129: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), fyrenmoo@3129: * or . fyrenmoo@3129: * fyrenmoo@3129: * Version: $Id$ fyrenmoo@3129: */ fyrenmoo@3129: fyrenmoo@3129: #include fyrenmoo@3129: #include fyrenmoo@3129: #include fyrenmoo@3129: #include fyrenmoo@3129: #include fyrenmoo@3129: #include fyrenmoo@3129: #include "TextParsers.h" fyrenmoo@3129: //#include "ShareSys.h" fyrenmoo@3129: //#include "sm_stringutil.h" fyrenmoo@3129: //#include "LibrarySys.h" fyrenmoo@3129: fyrenmoo@3129: TextParsers g_TextParser; fyrenmoo@3129: ITextParsers *textparsers = &g_TextParser; fyrenmoo@3129: fyrenmoo@3129: static int g_ini_chartable1[255] = {0}; fyrenmoo@3129: static int g_ws_chartable[255] = {0}; fyrenmoo@3129: fyrenmoo@3129: bool TextParsers::IsWhitespace(const char *stream) fyrenmoo@3129: { fyrenmoo@3129: return g_ws_chartable[(unsigned)*stream] == 1; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: TextParsers::TextParsers() fyrenmoo@3129: { fyrenmoo@3129: g_ini_chartable1[(unsigned)'_'] = 1; fyrenmoo@3129: g_ini_chartable1[(unsigned)'-'] = 1; fyrenmoo@3129: g_ini_chartable1[(unsigned)','] = 1; fyrenmoo@3129: g_ini_chartable1[(unsigned)'+'] = 1; fyrenmoo@3129: g_ini_chartable1[(unsigned)'.'] = 1; fyrenmoo@3129: g_ini_chartable1[(unsigned)'$'] = 1; fyrenmoo@3129: g_ini_chartable1[(unsigned)'?'] = 1; fyrenmoo@3129: g_ini_chartable1[(unsigned)'/'] = 1; fyrenmoo@3129: g_ws_chartable[(unsigned)'\n'] = 1; fyrenmoo@3129: g_ws_chartable[(unsigned)'\v'] = 1; fyrenmoo@3129: g_ws_chartable[(unsigned)'\r'] = 1; fyrenmoo@3129: g_ws_chartable[(unsigned)'\t'] = 1; fyrenmoo@3129: g_ws_chartable[(unsigned)'\f'] = 1; fyrenmoo@3129: g_ws_chartable[(unsigned)' '] = 1; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: unsigned int TextParsers::GetUTF8CharBytes(const char *stream) fyrenmoo@3129: { fyrenmoo@3129: return _GetUTF8CharBytes(stream); fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: /** fyrenmoo@3129: * File streams fyrenmoo@3129: */ fyrenmoo@3129: fyrenmoo@3129: bool FileStreamReader(void *stream, char *buffer, size_t maxlength, unsigned int *read) fyrenmoo@3129: { fyrenmoo@3129: size_t num = fread(buffer, 1, maxlength, (FILE *)stream); fyrenmoo@3129: fyrenmoo@3129: *read = static_cast(num); fyrenmoo@3129: fyrenmoo@3129: if (num == 0 && feof((FILE *)stream)) fyrenmoo@3129: { fyrenmoo@3129: return true; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: return (ferror((FILE *)stream) == 0); fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: SMCError TextParsers::ParseFile_SMC(const char *file, ITextListener_SMC *smc, SMCStates *states) fyrenmoo@3129: { fyrenmoo@3129: FILE *fp = fopen(file, "rt"); fyrenmoo@3129: fyrenmoo@3129: if (!fp) fyrenmoo@3129: { fyrenmoo@3129: if (states != NULL) fyrenmoo@3129: { fyrenmoo@3129: states->line = 0; fyrenmoo@3129: states->col = 0; fyrenmoo@3129: } fyrenmoo@3129: return SMCError_StreamOpen; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: SMCError result = ParseStream_SMC(fp, FileStreamReader, smc, states); fyrenmoo@3129: fyrenmoo@3129: fclose(fp); fyrenmoo@3129: fyrenmoo@3129: return result; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: SMCError TextParsers::ParseSMCFile(const char *file, fyrenmoo@3129: ITextListener_SMC *smc_listener, fyrenmoo@3129: SMCStates *states, fyrenmoo@3129: char *buffer, fyrenmoo@3129: size_t maxsize) fyrenmoo@3129: { fyrenmoo@3129: const char *errstr; fyrenmoo@3129: FILE *fp = fopen(file, "rt"); fyrenmoo@3129: fyrenmoo@3129: if (fp == NULL) fyrenmoo@3129: { fyrenmoo@3129: char error[256] = "unknown"; fyrenmoo@3129: if (states != NULL) fyrenmoo@3129: { fyrenmoo@3129: states->line = 0; fyrenmoo@3129: states->col = 0; fyrenmoo@3129: } fyrenmoo@3129: //g_LibSys.GetPlatformError(error, sizeof(error)); fyrenmoo@3129: UTIL_Format(buffer, maxsize, "File could not be opened: %s", error); fyrenmoo@3129: return SMCError_StreamOpen; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: SMCError result = ParseStream_SMC(fp, FileStreamReader, smc_listener, states); fyrenmoo@3129: fyrenmoo@3129: fclose(fp); fyrenmoo@3129: fyrenmoo@3129: errstr = GetSMCErrorString(result); fyrenmoo@3129: UTIL_Format(buffer, maxsize, "%s", errstr != NULL ? errstr : "Unknown error"); fyrenmoo@3129: fyrenmoo@3129: return result; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: struct RawStream fyrenmoo@3129: { fyrenmoo@3129: const char *stream; fyrenmoo@3129: size_t length; fyrenmoo@3129: size_t pos; fyrenmoo@3129: }; fyrenmoo@3129: fyrenmoo@3129: bool RawStreamReader(void *stream, char *buffer, size_t maxlength, unsigned int *read) fyrenmoo@3129: { fyrenmoo@3129: RawStream *rs = (RawStream *)stream; fyrenmoo@3129: fyrenmoo@3129: if (rs->pos >= rs->length) fyrenmoo@3129: { fyrenmoo@3129: return false; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: size_t remaining = rs->length - rs->pos; fyrenmoo@3129: fyrenmoo@3129: /* Use the smaller of the two */ fyrenmoo@3129: size_t copy = (remaining > maxlength) ? maxlength : remaining; fyrenmoo@3129: fyrenmoo@3129: memcpy(buffer, &rs->stream[rs->pos], copy); fyrenmoo@3129: rs->pos += copy; fyrenmoo@3129: *read = copy; fyrenmoo@3129: assert(rs->pos <= rs->length); fyrenmoo@3129: fyrenmoo@3129: return true; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: SMCError TextParsers::ParseSMCStream(const char *stream, fyrenmoo@3129: size_t length, fyrenmoo@3129: ITextListener_SMC *smc_listener, fyrenmoo@3129: SMCStates *states, fyrenmoo@3129: char *buffer, fyrenmoo@3129: size_t maxsize) fyrenmoo@3129: { fyrenmoo@3129: RawStream rs; fyrenmoo@3129: SMCError result; fyrenmoo@3129: fyrenmoo@3129: rs.stream = stream; fyrenmoo@3129: rs.length = length; fyrenmoo@3129: rs.pos = 0; fyrenmoo@3129: fyrenmoo@3129: result = ParseStream_SMC(&rs, RawStreamReader, smc_listener, states); fyrenmoo@3129: fyrenmoo@3129: const char *errstr = GetSMCErrorString(result); fyrenmoo@3129: UTIL_Format(buffer, maxsize, "%s", errstr != NULL ? errstr : "Unknown error"); fyrenmoo@3129: fyrenmoo@3129: return result; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: /** fyrenmoo@3129: * Raw parsing of streams with helper functions fyrenmoo@3129: */ fyrenmoo@3129: fyrenmoo@3129: struct StringInfo fyrenmoo@3129: { fyrenmoo@3129: StringInfo() : quoted(false), ptr(NULL), end(NULL), special(false) { } fyrenmoo@3129: bool quoted; fyrenmoo@3129: char *ptr; fyrenmoo@3129: char *end; fyrenmoo@3129: bool special; fyrenmoo@3129: }; fyrenmoo@3129: fyrenmoo@3129: const char *FixupString(StringInfo &data) fyrenmoo@3129: { fyrenmoo@3129: if (!data.ptr) fyrenmoo@3129: { fyrenmoo@3129: return NULL; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: if (data.quoted) fyrenmoo@3129: { fyrenmoo@3129: data.ptr++; fyrenmoo@3129: } fyrenmoo@3129: #if defined _DEBUG fyrenmoo@3129: else { fyrenmoo@3129: /* A string will never have beginning whitespace because we ignore it in the stream. fyrenmoo@3129: * Furthermore, if there is trailing whitespace, the end ptr will point to it, so it is valid fyrenmoo@3129: * to overwrite! Lastly, the last character must be whitespace or a comment/invalid character. fyrenmoo@3129: */ fyrenmoo@3129: } fyrenmoo@3129: #endif fyrenmoo@3129: fyrenmoo@3129: /* Do some extra work on strings that have special quoted characters. */ fyrenmoo@3129: if (data.special) fyrenmoo@3129: { fyrenmoo@3129: char *outptr = data.ptr; fyrenmoo@3129: size_t len = data.end - data.ptr; fyrenmoo@3129: if (len >= 2) fyrenmoo@3129: { fyrenmoo@3129: for (size_t i=0; i=0; i--) fyrenmoo@3129: { fyrenmoo@3129: if (info[i].ptr) fyrenmoo@3129: { fyrenmoo@3129: return info[i].ptr; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: return NULL; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: SMCError TextParsers::ParseStream_SMC(void *stream, fyrenmoo@3129: STREAMREADER srdr, fyrenmoo@3129: ITextListener_SMC *smc, fyrenmoo@3129: SMCStates *pStates) fyrenmoo@3129: { fyrenmoo@3129: char *reparse_point = NULL; fyrenmoo@3129: char in_buf[4096]; fyrenmoo@3129: char *parse_point = in_buf; fyrenmoo@3129: char *line_begin = in_buf; fyrenmoo@3129: unsigned int read; fyrenmoo@3129: unsigned int curlevel = 0; fyrenmoo@3129: bool in_quote = false; fyrenmoo@3129: bool ignoring = false; fyrenmoo@3129: bool eol_comment = false; fyrenmoo@3129: bool ml_comment = false; fyrenmoo@3129: unsigned int i; fyrenmoo@3129: SMCError err = SMCError_Okay; fyrenmoo@3129: SMCResult res; fyrenmoo@3129: SMCStates states; fyrenmoo@3129: char c; fyrenmoo@3129: fyrenmoo@3129: StringInfo strings[3]; fyrenmoo@3129: StringInfo emptystring; fyrenmoo@3129: fyrenmoo@3129: states.line = 1; fyrenmoo@3129: states.col = 0; fyrenmoo@3129: fyrenmoo@3129: smc->ReadSMC_ParseStart(); fyrenmoo@3129: fyrenmoo@3129: /** fyrenmoo@3129: * The stream reader reads in as much as it can fill the buffer with. fyrenmoo@3129: * It then processes the buffer. If the buffer cannot be fully processed, for example, fyrenmoo@3129: * a line is left hanging with no newline, then the contents of the buffer is shifted fyrenmoo@3129: * down, and the buffer is filled from the stream reader again. fyrenmoo@3129: * fyrenmoo@3129: * What makes this particularly annoying is that we cache pointers everywhere, so when fyrenmoo@3129: * the shifting process takes place, all those pointers must be shifted as well. fyrenmoo@3129: */ fyrenmoo@3129: while (srdr(stream, parse_point, sizeof(in_buf) - (parse_point - in_buf) - 1, &read)) fyrenmoo@3129: { fyrenmoo@3129: if (!read) fyrenmoo@3129: { fyrenmoo@3129: break; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: /* Check for BOM markings, which is only relevant on the first line. fyrenmoo@3129: * Not worth it, but it could be moved out of the loop. fyrenmoo@3129: */ fyrenmoo@3129: if (states.line == 1 && fyrenmoo@3129: in_buf[0] == (char)0xEF && fyrenmoo@3129: in_buf[1] == (char)0xBB && fyrenmoo@3129: in_buf[2] == (char)0xBF) fyrenmoo@3129: { fyrenmoo@3129: /* Move EVERYTHING down :\ */ fyrenmoo@3129: memmove(in_buf, &in_buf[3], read - 3); fyrenmoo@3129: read -= 3; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: if (reparse_point) fyrenmoo@3129: { fyrenmoo@3129: read += (parse_point - reparse_point); fyrenmoo@3129: parse_point = reparse_point; fyrenmoo@3129: reparse_point = NULL; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: for (i=0; iReadSMC_RawLine(&states, line_begin)) != SMCResult_Continue) fyrenmoo@3129: { fyrenmoo@3129: err = (res == SMCResult_HaltFail) ? SMCError_Custom : SMCError_Okay; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: parse_point[i] = '\n'; fyrenmoo@3129: fyrenmoo@3129: /* Now we check the sanity of our staged strings! */ fyrenmoo@3129: if (strings[2].ptr) fyrenmoo@3129: { fyrenmoo@3129: if (!curlevel) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidProperty1; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: /* Assume the next string is a property and pass the info on. */ fyrenmoo@3129: if ((res=smc->ReadSMC_KeyValue( fyrenmoo@3129: &states, fyrenmoo@3129: FixupString(strings[2]), fyrenmoo@3129: FixupString(strings[1]))) != SMCResult_Continue) fyrenmoo@3129: { fyrenmoo@3129: err = (res == SMCResult_HaltFail) ? SMCError_Custom : SMCError_Okay; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: scrap(strings); fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: /* Change the states for the next line */ fyrenmoo@3129: states.col = 0; fyrenmoo@3129: states.line++; fyrenmoo@3129: line_begin = &parse_point[i+1]; //Note: safe because this gets relocated later fyrenmoo@3129: } fyrenmoo@3129: else if (ignoring) fyrenmoo@3129: { fyrenmoo@3129: if (in_quote) fyrenmoo@3129: { fyrenmoo@3129: /* If i was 0, we could have reparsed, so make sure there's no buffer underrun */ fyrenmoo@3129: if ((&parse_point[i] != in_buf) && c == '"' && parse_point[i-1] != '\\') fyrenmoo@3129: { fyrenmoo@3129: /* If we reached a quote in an ignore phase, fyrenmoo@3129: * we're staging a string and we must rotate it out. fyrenmoo@3129: */ fyrenmoo@3129: in_quote = false; fyrenmoo@3129: ignoring = false; fyrenmoo@3129: /* Set our info */ fyrenmoo@3129: strings[0].end = &parse_point[i]; fyrenmoo@3129: strings[0].quoted = true; fyrenmoo@3129: if (rotate(strings) != NULL) fyrenmoo@3129: { fyrenmoo@3129: /* If we rotated too many strings, there was too much crap on one line */ fyrenmoo@3129: err = SMCError_InvalidTokens; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: else if (c == '\\') fyrenmoo@3129: { fyrenmoo@3129: strings[0].special = true; fyrenmoo@3129: if (i == (read - 1)) fyrenmoo@3129: { fyrenmoo@3129: reparse_point = &parse_point[i]; fyrenmoo@3129: break; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: else if (ml_comment) fyrenmoo@3129: { fyrenmoo@3129: if (c == '*') fyrenmoo@3129: { fyrenmoo@3129: /* Check if we need to get more input first */ fyrenmoo@3129: if (i == read - 1) fyrenmoo@3129: { fyrenmoo@3129: reparse_point = &parse_point[i]; fyrenmoo@3129: break; fyrenmoo@3129: } fyrenmoo@3129: if (parse_point[i+1] == '/') fyrenmoo@3129: { fyrenmoo@3129: ml_comment = false; fyrenmoo@3129: ignoring = false; fyrenmoo@3129: /* We should not be staging anything right now. */ fyrenmoo@3129: assert(strings[0].ptr == NULL); fyrenmoo@3129: /* Advance the input stream so we don't choke on this token */ fyrenmoo@3129: i++; fyrenmoo@3129: states.col++; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: else fyrenmoo@3129: { fyrenmoo@3129: /* Check if we're whitespace or not */ fyrenmoo@3129: if (!g_ws_chartable[(unsigned)c]) fyrenmoo@3129: { fyrenmoo@3129: bool restage = false; fyrenmoo@3129: /* Check various special tokens: fyrenmoo@3129: * ; fyrenmoo@3129: * // fyrenmoo@3129: * / * fyrenmoo@3129: * { fyrenmoo@3129: * } fyrenmoo@3129: */ fyrenmoo@3129: if (c == ';' || c == '/') fyrenmoo@3129: { fyrenmoo@3129: /* If it's a line-based comment (that is, ; or //) fyrenmoo@3129: * we will need to scrap everything until the end of the line. fyrenmoo@3129: */ fyrenmoo@3129: if (c == '/') fyrenmoo@3129: { fyrenmoo@3129: if (i == read - 1) fyrenmoo@3129: { fyrenmoo@3129: /* If we reached the end of the look-ahead, we need to re-check our input. fyrenmoo@3129: * Breaking out will force this to be the new reparse point! fyrenmoo@3129: */ fyrenmoo@3129: reparse_point = &parse_point[i]; fyrenmoo@3129: break; fyrenmoo@3129: } fyrenmoo@3129: if (parse_point[i + 1] == '/') fyrenmoo@3129: { fyrenmoo@3129: /* standard comment */ fyrenmoo@3129: ignoring = true; fyrenmoo@3129: eol_comment = true; fyrenmoo@3129: restage = true; fyrenmoo@3129: } fyrenmoo@3129: else if (parse_point[i+1] == '*') fyrenmoo@3129: { fyrenmoo@3129: /* inline comment - start ignoring */ fyrenmoo@3129: ignoring = true; fyrenmoo@3129: ml_comment = true; fyrenmoo@3129: /* yes, we restage, meaning that: fyrenmoo@3129: * STR/ *stuff* /ING (space because ml comments don't nest in C++) fyrenmoo@3129: * will not generate 'STRING', but rather 'STR' and 'ING'. fyrenmoo@3129: * This should be a rare occurrence and is done here for convenience. fyrenmoo@3129: */ fyrenmoo@3129: restage = true; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: else fyrenmoo@3129: { fyrenmoo@3129: ignoring = true; fyrenmoo@3129: eol_comment = true; fyrenmoo@3129: restage = true; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: else if (c == '{') fyrenmoo@3129: { fyrenmoo@3129: /* If we are staging a string, we must rotate here */ fyrenmoo@3129: if (strings[0].ptr) fyrenmoo@3129: { fyrenmoo@3129: /* We have unacceptable tokens on this line */ fyrenmoo@3129: if (rotate(strings) != NULL) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidSection1; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: /* Sections must always be alone */ fyrenmoo@3129: if (strings[2].ptr != NULL) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidSection1; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: else if (strings[1].ptr == NULL) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidSection2; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: if ((res=smc->ReadSMC_NewSection(&states, FixupString(strings[1]))) fyrenmoo@3129: != SMCResult_Continue) fyrenmoo@3129: { fyrenmoo@3129: err = (res == SMCResult_HaltFail) ? SMCError_Custom : SMCError_Okay; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: strings[1] = emptystring; fyrenmoo@3129: curlevel++; fyrenmoo@3129: } fyrenmoo@3129: else if (c == '}') fyrenmoo@3129: { fyrenmoo@3129: /* Unlike our matching friend, this can be on the same line as something prior */ fyrenmoo@3129: if (rotate(strings) != NULL) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidSection3; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: if (strings[2].ptr) fyrenmoo@3129: { fyrenmoo@3129: if (!curlevel) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidProperty1; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: if ((res=smc->ReadSMC_KeyValue( fyrenmoo@3129: &states, fyrenmoo@3129: FixupString(strings[2]), fyrenmoo@3129: FixupString(strings[1]))) fyrenmoo@3129: != SMCResult_Continue) fyrenmoo@3129: { fyrenmoo@3129: err = (res == SMCResult_HaltFail) ? SMCError_Custom : SMCError_Okay; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: else if (strings[1].ptr) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidSection3; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: else if (!curlevel) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidSection4; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: /* Now it's safe to leave the section */ fyrenmoo@3129: scrap(strings); fyrenmoo@3129: if ((res=smc->ReadSMC_LeavingSection(&states)) != SMCResult_Continue) fyrenmoo@3129: { fyrenmoo@3129: err = (res == SMCResult_HaltFail) ? SMCError_Custom : SMCError_Okay; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: curlevel--; fyrenmoo@3129: } fyrenmoo@3129: else if (c == '"') fyrenmoo@3129: { fyrenmoo@3129: /* If we get a quote mark, we always restage, but we need to do it beforehand */ fyrenmoo@3129: if (strings[0].ptr) fyrenmoo@3129: { fyrenmoo@3129: strings[0].end = &parse_point[i]; fyrenmoo@3129: if (rotate(strings) != NULL) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidTokens; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: strings[0].ptr = &parse_point[i]; fyrenmoo@3129: in_quote = true; fyrenmoo@3129: ignoring = true; fyrenmoo@3129: } fyrenmoo@3129: else if (!strings[0].ptr) fyrenmoo@3129: { fyrenmoo@3129: /* If we have no string, we must start one */ fyrenmoo@3129: strings[0].ptr = &parse_point[i]; fyrenmoo@3129: } fyrenmoo@3129: if (restage && strings[0].ptr) fyrenmoo@3129: { fyrenmoo@3129: strings[0].end = &parse_point[i]; fyrenmoo@3129: if (rotate(strings) != NULL) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidTokens; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: else fyrenmoo@3129: { fyrenmoo@3129: /* If we're eating a string and get whitespace, we need to restage. fyrenmoo@3129: * (Note that if we are quoted, this is being ignored) fyrenmoo@3129: */ fyrenmoo@3129: if (strings[0].ptr) fyrenmoo@3129: { fyrenmoo@3129: /* fyrenmoo@3129: * The specification says the second string in a pair does not need to be quoted. fyrenmoo@3129: * Thus, we check if there's already a string on the stack. fyrenmoo@3129: * If there's a newline, we always rotate so the newline has an empty starter. fyrenmoo@3129: */ fyrenmoo@3129: if (!strings[1].ptr) fyrenmoo@3129: { fyrenmoo@3129: /* There's no string, so we must move this one down and eat up another */ fyrenmoo@3129: strings[0].end = &parse_point[i]; fyrenmoo@3129: rotate(strings); fyrenmoo@3129: } fyrenmoo@3129: else if (!strings[1].quoted) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidTokens; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: /* Advance which token we're on */ fyrenmoo@3129: states.col++; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: if (line_begin != in_buf) fyrenmoo@3129: { fyrenmoo@3129: /* The line buffer has advanced, so it's safe to copy N bytes back to the beginning. fyrenmoo@3129: * What's N? N is the lowest point we're currently relying on. fyrenmoo@3129: */ fyrenmoo@3129: char *stage = lowstring(strings); fyrenmoo@3129: if (!stage || stage > line_begin) fyrenmoo@3129: { fyrenmoo@3129: stage = line_begin; fyrenmoo@3129: } fyrenmoo@3129: unsigned int bytes = read - (stage - parse_point); fyrenmoo@3129: fyrenmoo@3129: /* It is now safe to delete everything before the staged point */ fyrenmoo@3129: memmove(in_buf, stage, bytes); fyrenmoo@3129: fyrenmoo@3129: /* Calculate the number of bytes in the new buffer */ fyrenmoo@3129: bytes = stage - in_buf; fyrenmoo@3129: /* Relocate all the cached pointers to our new base */ fyrenmoo@3129: line_begin -= bytes; fyrenmoo@3129: reloc(strings[0], bytes); fyrenmoo@3129: reloc(strings[1], bytes); fyrenmoo@3129: reloc(strings[2], bytes); fyrenmoo@3129: if (reparse_point) fyrenmoo@3129: { fyrenmoo@3129: reparse_point -= bytes; fyrenmoo@3129: } fyrenmoo@3129: if (parse_point) fyrenmoo@3129: { fyrenmoo@3129: parse_point = &parse_point[read]; fyrenmoo@3129: parse_point -= bytes; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: else if (read == sizeof(in_buf) - 1) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_TokenOverflow; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: /* If we're done parsing and there are tokens left over... */ fyrenmoo@3129: if (curlevel) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidSection5; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: else if (strings[0].ptr || strings[1].ptr) fyrenmoo@3129: { fyrenmoo@3129: err = SMCError_InvalidTokens; fyrenmoo@3129: goto failed; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: smc->ReadSMC_ParseEnd(false, false); fyrenmoo@3129: fyrenmoo@3129: if (pStates != NULL) fyrenmoo@3129: { fyrenmoo@3129: *pStates = states; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: return SMCError_Okay; fyrenmoo@3129: fyrenmoo@3129: failed: fyrenmoo@3129: if (pStates != NULL) fyrenmoo@3129: { fyrenmoo@3129: *pStates = states; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: smc->ReadSMC_ParseEnd(true, (err == SMCError_Custom)); fyrenmoo@3129: fyrenmoo@3129: return err; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: fyrenmoo@3129: /** fyrenmoo@3129: * INI parser fyrenmoo@3129: */ fyrenmoo@3129: fyrenmoo@3129: bool TextParsers::ParseFile_INI(const char *file, ITextListener_INI *ini_listener, unsigned int *line, unsigned int *col) fyrenmoo@3129: { fyrenmoo@3129: FILE *fp = fopen(file, "rt"); fyrenmoo@3129: unsigned int curline = 0; fyrenmoo@3129: unsigned int curtok; fyrenmoo@3129: size_t len; fyrenmoo@3129: fyrenmoo@3129: if (!fp) fyrenmoo@3129: { fyrenmoo@3129: if (line) fyrenmoo@3129: { fyrenmoo@3129: *line = 0; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: return false; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: char buffer[2048]; fyrenmoo@3129: char *ptr, *save_ptr; fyrenmoo@3129: bool in_quote; fyrenmoo@3129: fyrenmoo@3129: while (!feof(fp)) fyrenmoo@3129: { fyrenmoo@3129: curline++; fyrenmoo@3129: curtok = 0; fyrenmoo@3129: buffer[0] = '\0'; fyrenmoo@3129: if (fgets(buffer, sizeof(buffer), fp) == NULL) fyrenmoo@3129: { fyrenmoo@3129: break; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: //:TODO: this will only run once, so find a nice way to move it out of the while loop fyrenmoo@3129: /* If this is the first line, check the first three bytes for BOM */ fyrenmoo@3129: if (curline == 1 && fyrenmoo@3129: buffer[0] == (char)0xEF && fyrenmoo@3129: buffer[1] == (char)0xBB && fyrenmoo@3129: buffer[2] == (char)0xBF) fyrenmoo@3129: { fyrenmoo@3129: /* We have a UTF-8 marked file... skip these bytes */ fyrenmoo@3129: ptr = &buffer[3]; fyrenmoo@3129: } else { fyrenmoo@3129: ptr = buffer; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: /*************************************************** fyrenmoo@3129: * We preprocess the string before parsing tokens! * fyrenmoo@3129: ***************************************************/ fyrenmoo@3129: fyrenmoo@3129: /* First strip beginning whitespace */ fyrenmoo@3129: while (*ptr != '\0' && g_ws_chartable[(unsigned)*ptr] != 0) fyrenmoo@3129: { fyrenmoo@3129: ptr++; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: len = strlen(ptr); fyrenmoo@3129: fyrenmoo@3129: if (!len) fyrenmoo@3129: { fyrenmoo@3129: continue; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: /* Now search for comment characters */ fyrenmoo@3129: in_quote = false; fyrenmoo@3129: save_ptr = ptr; fyrenmoo@3129: for (size_t i=0; i=0 && iReadINI_RawLine(ptr, &curtok)) fyrenmoo@3129: { fyrenmoo@3129: goto event_failed; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: if (*ptr == '[') fyrenmoo@3129: { fyrenmoo@3129: bool invalid_tokens = false; fyrenmoo@3129: bool got_bracket = false; fyrenmoo@3129: bool extra_tokens = false; fyrenmoo@3129: char c; fyrenmoo@3129: bool alnum; fyrenmoo@3129: wchar_t wc; fyrenmoo@3129: fyrenmoo@3129: for (size_t i=1; iReadINI_NewSection(&ptr[1], invalid_tokens, got_bracket, extra_tokens, &curtok)) fyrenmoo@3129: { fyrenmoo@3129: goto event_failed; fyrenmoo@3129: } fyrenmoo@3129: } else { fyrenmoo@3129: char *key_ptr = ptr; fyrenmoo@3129: char *val_ptr = NULL; fyrenmoo@3129: char c; fyrenmoo@3129: size_t first_space = 0; fyrenmoo@3129: bool invalid_tokens = false; fyrenmoo@3129: bool equal_token = false; fyrenmoo@3129: bool quotes = false; fyrenmoo@3129: bool alnum; fyrenmoo@3129: wchar_t wc; fyrenmoo@3129: fyrenmoo@3129: for (size_t i=0; iReadINI_KeyValue(key_ptr, val_ptr, invalid_tokens, equal_token, quotes, &curtok)) fyrenmoo@3129: { fyrenmoo@3129: curtok = 0; fyrenmoo@3129: goto event_failed; fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: if (line) fyrenmoo@3129: { fyrenmoo@3129: *line = curline; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: fclose(fp); fyrenmoo@3129: fyrenmoo@3129: return true; fyrenmoo@3129: fyrenmoo@3129: event_failed: fyrenmoo@3129: if (line) fyrenmoo@3129: { fyrenmoo@3129: *line = curline; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: if (col) fyrenmoo@3129: { fyrenmoo@3129: *col = curtok; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: fclose(fp); fyrenmoo@3129: fyrenmoo@3129: return false; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: const char *TextParsers::GetSMCErrorString(SMCError err) fyrenmoo@3129: { fyrenmoo@3129: static const char *s_errors[] = fyrenmoo@3129: { fyrenmoo@3129: NULL, fyrenmoo@3129: "Stream failed to open", fyrenmoo@3129: "Stream returned read error", fyrenmoo@3129: NULL, fyrenmoo@3129: "Un-quoted section has invalid tokens", fyrenmoo@3129: "Section declared without header", fyrenmoo@3129: "Section declared with unknown tokens", fyrenmoo@3129: "Section ending without a matching section beginning", fyrenmoo@3129: "Section beginning without a matching ending", fyrenmoo@3129: "Line contained too many invalid tokens", fyrenmoo@3129: "Token buffer overflowed", fyrenmoo@3129: "A property was declared outside of a section", fyrenmoo@3129: }; fyrenmoo@3129: fyrenmoo@3129: if (err < SMCError_Okay || err > SMCError_InvalidProperty1) fyrenmoo@3129: { fyrenmoo@3129: return NULL; fyrenmoo@3129: } fyrenmoo@3129: fyrenmoo@3129: return s_errors[err]; fyrenmoo@3129: }