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