[Scummvm-cvs-logs] SF.net SVN: scummvm:[34803] scummvm/trunk/common

tanoku at users.sourceforge.net tanoku at users.sourceforge.net
Tue Oct 14 21:00:21 CEST 2008


Revision: 34803
          http://scummvm.svn.sourceforge.net/scummvm/?rev=34803&view=rev
Author:   tanoku
Date:     2008-10-14 19:00:21 +0000 (Tue, 14 Oct 2008)

Log Message:
-----------
Greatly improved XML Parser error messages.

Modified Paths:
--------------
    scummvm/trunk/common/xmlparser.cpp
    scummvm/trunk/common/xmlparser.h

Modified: scummvm/trunk/common/xmlparser.cpp
===================================================================
--- scummvm/trunk/common/xmlparser.cpp	2008-10-14 17:53:52 UTC (rev 34802)
+++ scummvm/trunk/common/xmlparser.cpp	2008-10-14 19:00:21 UTC (rev 34803)
@@ -36,61 +36,71 @@
 
 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] = ' ';
+	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  File <%s>, line %d:\n", _fileName.c_str(), lineCount);
+	fprintf(stderr, "\nParser error: ");
 
-	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(" ");
-
-	printf("^\n");
-	printf("Parser 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;
 }

Modified: scummvm/trunk/common/xmlparser.h
===================================================================
--- scummvm/trunk/common/xmlparser.h	2008-10-14 17:53:52 UTC (rev 34802)
+++ scummvm/trunk/common/xmlparser.h	2008-10-14 19:00:21 UTC (rev 34803)
@@ -105,6 +105,7 @@
  * @see XMLParser::keyCallback()
  */
 class XMLParser {
+	static const int kErrorMessageWidth = 512;
 
 public:
 	/**


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list