Greatly improved XML Parser error messages.
svn-id: r34803
This commit is contained in:
parent
1c374dcf35
commit
cda3086782
2 changed files with 59 additions and 48 deletions
|
@ -37,60 +37,70 @@ using namespace Graphics;
|
||||||
bool XMLParser::parserError(const char *errorString, ...) {
|
bool XMLParser::parserError(const char *errorString, ...) {
|
||||||
_state = kParserError;
|
_state = kParserError;
|
||||||
|
|
||||||
int original_pos = _stream->pos();
|
const int startPosition = _stream->pos();
|
||||||
int pos = original_pos;
|
const int middle = kErrorMessageWidth / 2;
|
||||||
int lineCount = 1;
|
|
||||||
int lineStart = 0;
|
|
||||||
|
|
||||||
if (_fileName == "Memory Stream") {
|
int currentPosition = startPosition;
|
||||||
lineStart = MAX(0, original_pos - 35);
|
int lineCount = 1, realMiddle = 0;
|
||||||
lineCount = 0;
|
char errorBuffer[kErrorMessageWidth];
|
||||||
} else {
|
char c, *errorKeyStart = 0;
|
||||||
do {
|
|
||||||
if (_char == '\n' || _char == '\r') {
|
_stream->seek(0, SEEK_SET);
|
||||||
|
|
||||||
|
while (currentPosition--) {
|
||||||
|
c = _stream->readByte();
|
||||||
|
|
||||||
|
if (c == '\n' || c == '\r')
|
||||||
lineCount++;
|
lineCount++;
|
||||||
|
|
||||||
if (lineStart == 0)
|
|
||||||
lineStart = MAX(pos + 1, original_pos - 60);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_stream->seek(-1, SEEK_CUR);
|
_stream->seek(-middle, SEEK_CUR);
|
||||||
|
|
||||||
} while (_stream->pos() > 0);
|
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++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char lineStr[70];
|
errorBuffer[i] = c;
|
||||||
_stream->seek(original_pos - 35, SEEK_SET);
|
if (!realMiddle && j >= middle)
|
||||||
_stream->readLine_NEW(lineStr, 70);
|
realMiddle = i;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 70; ++i)
|
for (int i = realMiddle; i >= 0; --i)
|
||||||
if (lineStr[i] == '\n')
|
if (errorBuffer[i] == '<') {
|
||||||
lineStr[i] = ' ';
|
errorKeyStart = &errorBuffer[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
printf("\n File <%s>, line %d:\n", _fileName.c_str(), lineCount);
|
for (int i = realMiddle; i < kErrorMessageWidth; ++i)
|
||||||
|
if (errorBuffer[i] == '>') {
|
||||||
|
errorBuffer[i + 1] = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
bool startFull = lineStr[0] == '<';
|
fprintf(stderr, "\n File <%s>, line %d:\n", _fileName.c_str(), lineCount);
|
||||||
bool endFull = lineStr[strlen(lineStr) - 1] == '>';
|
|
||||||
|
|
||||||
printf("%s%s%s\n", startFull ? "" : "...", lineStr, endFull ? "" : "...");
|
if (!errorKeyStart)
|
||||||
|
fprintf(stderr, "...%s%s\n", errorBuffer, errorBuffer[strlen(errorBuffer) - 1] == '>' ? "" : "...");
|
||||||
|
else
|
||||||
|
fprintf(stderr, "%s%s\n", errorKeyStart, errorBuffer[strlen(errorBuffer) - 1] == '>' ? "" : "...");
|
||||||
|
|
||||||
int cursor = 35;
|
fprintf(stderr, "\nParser error: ");
|
||||||
|
|
||||||
if (!startFull)
|
|
||||||
cursor += 3;
|
|
||||||
|
|
||||||
while (cursor--)
|
|
||||||
printf(" ");
|
|
||||||
|
|
||||||
printf("^\n");
|
|
||||||
printf("Parser error: ");
|
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, errorString);
|
va_start(args, errorString);
|
||||||
vprintf(errorString, args);
|
vfprintf(stderr, errorString, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
printf("\n\n");
|
fprintf(stderr, "\n\n");
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,6 +105,7 @@ namespace Common {
|
||||||
* @see XMLParser::keyCallback()
|
* @see XMLParser::keyCallback()
|
||||||
*/
|
*/
|
||||||
class XMLParser {
|
class XMLParser {
|
||||||
|
static const int kErrorMessageWidth = 512;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue