aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorVicent Marti2008-10-14 19:00:21 +0000
committerVicent Marti2008-10-14 19:00:21 +0000
commitcda308678275bf145cfe40e69ed7870f2c3136bd (patch)
treeb35751ce77711003b0ee989677ceaa1bc0ef1788 /common
parent1c374dcf35ad09c18554c18f57b3d449dac04b74 (diff)
downloadscummvm-rg350-cda308678275bf145cfe40e69ed7870f2c3136bd.tar.gz
scummvm-rg350-cda308678275bf145cfe40e69ed7870f2c3136bd.tar.bz2
scummvm-rg350-cda308678275bf145cfe40e69ed7870f2c3136bd.zip
Greatly improved XML Parser error messages.
svn-id: r34803
Diffstat (limited to 'common')
-rw-r--r--common/xmlparser.cpp98
-rw-r--r--common/xmlparser.h1
2 files changed, 55 insertions, 44 deletions
diff --git a/common/xmlparser.cpp b/common/xmlparser.cpp
index 3939585268..b3fb2e3cc5 100644
--- a/common/xmlparser.cpp
+++ b/common/xmlparser.cpp
@@ -36,61 +36,71 @@ using namespace Graphics;
bool XMLParser::parserError(const char *errorString, ...) {
_state = kParserError;
-
- int original_pos = _stream->pos();
- int pos = original_pos;
- int lineCount = 1;
- int lineStart = 0;
-
- if (_fileName == "Memory Stream") {
- lineStart = MAX(0, original_pos - 35);
- lineCount = 0;
- } else {
- do {
- if (_char == '\n' || _char == '\r') {
- lineCount++;
+
+ 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;
+
+ _stream->seek(0, SEEK_SET);
+
+ while (currentPosition--) {
+ c = _stream->readByte();
- if (lineStart == 0)
- lineStart = MAX(pos + 1, original_pos - 60);
+ if (c == '\n' || c == '\r')
+ lineCount++;
+ }
+
+ _stream->seek(-middle, SEEK_CUR);
+
+ for (int i = 0, j = 0; i < kErrorMessageWidth; ++i, ++j) {
+ c = _stream->readByte();
+
+ if (c == '\n' || c == '\r') {
+ errorBuffer[i++] = ' ';
+ j++;
+
+ while (c && isspace(c)) {
+ c = _stream->readByte();
+ j++;
}
+ }
- _stream->seek(-1, SEEK_CUR);
-
- } while (_stream->pos() > 0);
+ errorBuffer[i] = c;
+ if (!realMiddle && j >= middle)
+ realMiddle = i;
}
-
- char lineStr[70];
- _stream->seek(original_pos - 35, SEEK_SET);
- _stream->readLine_NEW(lineStr, 70);
+
+ 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;
+ }
- for (int i = 0; i < 70; ++i)
- if (lineStr[i] == '\n')
- lineStr[i] = ' ';
-
- printf("\n File <%s>, line %d:\n", _fileName.c_str(), lineCount);
-
- bool startFull = lineStr[0] == '<';
- bool endFull = lineStr[strlen(lineStr) - 1] == '>';
-
- printf("%s%s%s\n", startFull ? "" : "...", lineStr, endFull ? "" : "...");
-
- int cursor = 35;
-
- if (!startFull)
- cursor += 3;
-
- while (cursor--)
- printf(" ");
+ 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] == '>' ? "" : "...");
- printf("^\n");
- printf("Parser error: ");
+ fprintf(stderr, "\nParser error: ");
va_list args;
va_start(args, errorString);
- vprintf(errorString, args);
+ vfprintf(stderr, errorString, args);
va_end(args);
- printf("\n\n");
+ fprintf(stderr, "\n\n");
return false;
}
diff --git a/common/xmlparser.h b/common/xmlparser.h
index 31ba4e594e..89df048395 100644
--- a/common/xmlparser.h
+++ b/common/xmlparser.h
@@ -105,6 +105,7 @@ namespace Common {
* @see XMLParser::keyCallback()
*/
class XMLParser {
+ static const int kErrorMessageWidth = 512;
public:
/**