From a67c16c6f28ba3f98d30108fb5cbcc94f1da9314 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Sat, 18 Oct 2008 10:11:35 +0000 Subject: Rewrote XMLParser error() function for the 5th time. Bugfix: Ambiguous error message when parsing an unexpected header. svn-id: r34819 --- common/xmlparser.cpp | 72 ++++++++++++++++++++++++---------------------------- common/xmlparser.h | 2 +- 2 files changed, 34 insertions(+), 40 deletions(-) (limited to 'common') diff --git a/common/xmlparser.cpp b/common/xmlparser.cpp index 2440a08803..3671feec2d 100644 --- a/common/xmlparser.cpp +++ b/common/xmlparser.cpp @@ -38,12 +38,9 @@ bool XMLParser::parserError(const char *errorString, ...) { _state = kParserError; const int startPosition = _stream->pos(); - const int middle = kErrorMessageWidth / 2; - int currentPosition = startPosition; - int lineCount = 1, realMiddle = 0; - char errorBuffer[kErrorMessageWidth]; - char c, *errorKeyStart = 0; + int lineCount = 1; + char c = 0; _stream->seek(0, SEEK_SET); @@ -54,46 +51,40 @@ bool XMLParser::parserError(const char *errorString, ...) { lineCount++; } - _stream->seek(-MIN(middle, startPosition), SEEK_CUR); - - for (int i = 0, j = 0; i < kErrorMessageWidth; ++i, ++j) { + assert(_stream->pos() == startPosition); + currentPosition = startPosition; + + int keyOpening = 0; + int keyClosing = 0; + + while (currentPosition-- && keyOpening == 0) { + _stream->seek(-2, SEEK_CUR); c = _stream->readByte(); - - if (c == '\n' || c == '\r') { - errorBuffer[i++] = ' '; - j++; - - while (c && isspace(c)) { - c = _stream->readByte(); - j++; - } - } - - errorBuffer[i] = c; - if (!realMiddle && j >= middle) - realMiddle = i; + + if (c == '<') + keyOpening = currentPosition - 1; + else if (c == '>') + keyClosing = currentPosition; + } + + _stream->seek(startPosition, SEEK_SET); + currentPosition = startPosition; + while (keyClosing == 0 && c && currentPosition++) { + c = _stream->readByte(); + + if (c == '>') + keyClosing = currentPosition; } - - for (int i = realMiddle; i >= 0; --i) - if (errorBuffer[i] == '<') { - errorKeyStart = &errorBuffer[i]; - break; - } - - for (int i = realMiddle; i < kErrorMessageWidth; ++i) - if (errorBuffer[i] == '>') { - errorBuffer[i + 1] = 0; - break; - } fprintf(stderr, "\n File <%s>, line %d:\n", _fileName.c_str(), lineCount); - if (!errorKeyStart) - fprintf(stderr, "...%s%s\n", errorBuffer, errorBuffer[strlen(errorBuffer) - 1] == '>' ? "" : "..."); - else - fprintf(stderr, "%s%s\n", errorKeyStart, errorBuffer[strlen(errorBuffer) - 1] == '>' ? "" : "..."); + currentPosition = (keyClosing - keyOpening); + _stream->seek(keyOpening, SEEK_SET); - fprintf(stderr, "\nParser error: "); + while (currentPosition--) + fprintf(stderr, "%c", _stream->readByte()); + + fprintf(stderr, "\n\nParser error: "); va_list args; va_start(args, errorString); @@ -277,6 +268,9 @@ bool XMLParser::parse() { } else if (_char == '/') { _char = _stream->readByte(); activeClosure = true; + } else if (_char == '?') { + parserError("Unexpected header. There may only be one XML header per file."); + break; } _state = kParserNeedKeyName; diff --git a/common/xmlparser.h b/common/xmlparser.h index fd4788a426..2831eadef8 100644 --- a/common/xmlparser.h +++ b/common/xmlparser.h @@ -406,7 +406,7 @@ protected: _char = _stream->readByte(); } - return isspace(_char) != 0 || _char == '>' || _char == '=' || _char == '/' || _char == '?'; + return isspace(_char) != 0 || _char == '>' || _char == '=' || _char == '/'; } /** -- cgit v1.2.3