[Scummvm-cvs-logs] SF.net SVN: scummvm:[48013] scummvm/trunk/engines/drascula

peres001 at users.sourceforge.net peres001 at users.sourceforge.net
Tue Feb 9 02:22:25 CET 2010


Revision: 48013
          http://scummvm.svn.sourceforge.net/scummvm/?rev=48013&view=rev
Author:   peres001
Date:     2010-02-09 01:22:24 +0000 (Tue, 09 Feb 2010)

Log Message:
-----------
Moved text parsing to a new class.

Modified Paths:
--------------
    scummvm/trunk/engines/drascula/converse.cpp
    scummvm/trunk/engines/drascula/drascula.cpp
    scummvm/trunk/engines/drascula/drascula.h
    scummvm/trunk/engines/drascula/resource.cpp
    scummvm/trunk/engines/drascula/rooms.cpp

Modified: scummvm/trunk/engines/drascula/converse.cpp
===================================================================
--- scummvm/trunk/engines/drascula/converse.cpp	2010-02-09 01:07:39 UTC (rev 48012)
+++ scummvm/trunk/engines/drascula/converse.cpp	2010-02-09 01:22:24 UTC (rev 48013)
@@ -137,7 +137,6 @@
 	if (!stream)
 		error("missing data file %s", fileName);
 
-	int size = stream->size();
 	int game1 = kDialogOptionUnselected,
 		game2 = kDialogOptionUnselected,
 		game3 = kDialogOptionUnselected;
@@ -150,20 +149,24 @@
 
 	selectVerb(kVerbNone);
 
-	getStringFromLine(stream, size, phrase1);
-	getStringFromLine(stream, size, phrase2);
-	getStringFromLine(stream, size, phrase3);
-	getStringFromLine(stream, size, phrase4);
-	getStringFromLine(stream, size, sound1);
-	getStringFromLine(stream, size, sound2);
-	getStringFromLine(stream, size, sound3);
-	getStringFromLine(stream, size, sound4);
-	getIntFromLine(stream, size, &answer1);
-	getIntFromLine(stream, size, &answer2);
-	getIntFromLine(stream, size, &answer3);
+	TextResourceParser p(stream, DisposeAfterUse::YES);
 
-	delete stream;
+	p.parseString(phrase1);
+	p.parseString(phrase2);
+	p.parseString(phrase3);
+	p.parseString(phrase4);
+	p.parseString(sound1);
+	p.parseString(sound2);
+	p.parseString(sound3);
+	p.parseString(sound4);
+	p.parseInt(answer1);
+	p.parseInt(answer2);
+	p.parseInt(answer3);
 
+	// no need to delete the stream, since TextResourceParser takes ownership
+	// delete stream;
+
+
 	if (currentChapter == 2 && !strcmp(fileName, "op_5.cal") && flags[38] == 1 && flags[33] == 1) {
 		strcpy(phrase3, _text[405]);
 		strcpy(sound3, "405.als");

Modified: scummvm/trunk/engines/drascula/drascula.cpp
===================================================================
--- scummvm/trunk/engines/drascula/drascula.cpp	2010-02-09 01:07:39 UTC (rev 48012)
+++ scummvm/trunk/engines/drascula/drascula.cpp	2010-02-09 01:22:24 UTC (rev 48013)
@@ -604,43 +604,7 @@
 	}
 }
 
-char *DrasculaEngine::getLine(Common::SeekableReadStream *stream, char *buf, int len) {
-	byte c;
-	char *b;
 
-	for (;;) {
-		b = buf;
-		while (true) {
-			c = ~stream->readByte();
-			if (stream->eos()) break;
-
-			if (c == '\r')
-				continue;
-			if (c == '\n' || b - buf >= (len - 1))
-				break;
-			*b++ = c;
-		}
-		*b = '\0';
-		if (stream->eos() && b == buf)
-			return NULL;
-		if (b != buf)
-			break;
-	}
-	return buf;
-}
-
-void DrasculaEngine::getIntFromLine(Common::SeekableReadStream *stream, int len, int* result) {
-	char buf[256];
-	getLine(stream, buf, len);
-	sscanf(buf, "%d", result);
-}
-
-void DrasculaEngine::getStringFromLine(Common::SeekableReadStream *stream, int len, char* result) {
-	char buf[256];
-	getLine(stream, buf, len);
-	sscanf(buf, "%s", result);
-}
-
 bool DrasculaEngine::verify1() {
 	int l;
 

Modified: scummvm/trunk/engines/drascula/drascula.h
===================================================================
--- scummvm/trunk/engines/drascula/drascula.h	2010-02-09 01:07:39 UTC (rev 48012)
+++ scummvm/trunk/engines/drascula/drascula.h	2010-02-09 01:22:24 UTC (rev 48013)
@@ -267,6 +267,22 @@
 
 };
 
+class TextResourceParser {
+	Common::SeekableReadStream *_stream;
+	DisposeAfterUse::Flag _dispose;
+	int _maxLen;
+
+	void getLine(char *buf);
+
+public:
+	TextResourceParser(Common::SeekableReadStream *stream, DisposeAfterUse::Flag dispose);
+	~TextResourceParser();
+
+	void parseInt(int &result);
+	void parseString(char *result);
+};
+
+
 #define NUM_SAVES		10
 #define NUM_FLAGS		50
 #define DIF_MASK		55
@@ -593,10 +609,6 @@
 	void MusicFadeout();
 	void playFile(const char *fname);
 
-	char *getLine(Common::SeekableReadStream *stream, char *buf, int len);
-	void getIntFromLine(Common::SeekableReadStream *stream, int len, int* result);
-	void getStringFromLine(Common::SeekableReadStream *stream, int len, char* result);
-
 	void grr();
 	void updateAnim(int y, int destX, int destY, int width, int height, int count, byte* src, int delayVal = 3, bool copyRectangle = false);
 

Modified: scummvm/trunk/engines/drascula/resource.cpp
===================================================================
--- scummvm/trunk/engines/drascula/resource.cpp	2010-02-09 01:07:39 UTC (rev 48012)
+++ scummvm/trunk/engines/drascula/resource.cpp	2010-02-09 01:22:24 UTC (rev 48013)
@@ -47,5 +47,61 @@
 	return createReadStreamForMember(filename);
 }
 
+
+
+
+TextResourceParser::TextResourceParser(Common::SeekableReadStream *stream, DisposeAfterUse::Flag dispose) :
+	_stream(stream), _dispose(dispose) {
+
+	// NOTE: strangely enough, the code before this refactoring used the size of
+	// the stream as a fixed maximum length for the parser. Using an updated 
+	// (size-pos) would make more sense to me, but let's see what the experts say.
+	_maxLen = _stream->size();
+}
+
+TextResourceParser::~TextResourceParser() {
+	if (_dispose == DisposeAfterUse::YES) {
+		delete _stream;
+	}
+}
+
+void TextResourceParser::getLine(char *buf) {
+	byte c;
+	char *b;
+
+	for (;;) {
+		b = buf;
+		while (true) {
+			c = ~_stream->readByte();
+			if (_stream->eos()) break;
+
+			if (c == '\r')
+				continue;
+			if (c == '\n' || b - buf >= (_maxLen - 1))
+				break;
+			*b++ = c;
+		}
+		*b = '\0';
+		if (_stream->eos() && b == buf)
+			return;
+		if (b != buf)
+			break;
+	}
+}
+
+void TextResourceParser::parseInt(int &result) {
+	char buf[256];
+	getLine(buf);
+	sscanf(buf, "%d", &result);
+}
+
+void TextResourceParser::parseString(char* result) {
+	char buf[256];
+	getLine(buf);
+	sscanf(buf, "%s", result);
+}
+
+
+
 } // End of namespace Drascula
 

Modified: scummvm/trunk/engines/drascula/rooms.cpp
===================================================================
--- scummvm/trunk/engines/drascula/rooms.cpp	2010-02-09 01:07:39 UTC (rev 48012)
+++ scummvm/trunk/engines/drascula/rooms.cpp	2010-02-09 01:22:24 UTC (rev 48013)
@@ -1653,65 +1653,67 @@
 	if (!stream) {
 		error("missing data file %s", fileName);
 	}
-	int size = stream->size();
+	
+	TextResourceParser p(stream, DisposeAfterUse::YES);
 
-	getIntFromLine(stream, size, &roomNumber);
-	getIntFromLine(stream, size, &roomMusic);
-	getStringFromLine(stream, size, roomDisk);
-	getIntFromLine(stream, size, &palLevel);
+	p.parseInt(roomNumber);
+	p.parseInt(roomMusic);
+	p.parseString(roomDisk);
+	p.parseInt(palLevel);
 
 	if (currentChapter == 2)
-		getIntFromLine(stream, size, &martin);
+		p.parseInt(martin);
 
 	if (currentChapter == 2 && martin != 0) {
 		curWidth = martin;
-		getIntFromLine(stream, size, &curHeight);
-		getIntFromLine(stream, size, &feetHeight);
-		getIntFromLine(stream, size, &stepX);
-		getIntFromLine(stream, size, &stepY);
+		p.parseInt(curHeight);
+		p.parseInt(feetHeight);
+		p.parseInt(stepX);
+		p.parseInt(stepY);
 
-		getStringFromLine(stream, size, pant1);
-		getStringFromLine(stream, size, pant2);
-		getStringFromLine(stream, size, pant3);
-		getStringFromLine(stream, size, pant4);
+		p.parseString(pant1);
+		p.parseString(pant2);
+		p.parseString(pant3);
+		p.parseString(pant4);
 
 		strcpy(menuBackground, pant4);
 	}
 
-	getIntFromLine(stream, size, &numRoomObjs);
+	p.parseInt(numRoomObjs);
 
 	for (l = 0; l < numRoomObjs; l++) {
-		getIntFromLine(stream, size, &objectNum[l]);
-		getStringFromLine(stream, size, objName[l]);
-		getIntFromLine(stream, size, &x1[l]);
-		getIntFromLine(stream, size, &y1[l]);
-		getIntFromLine(stream, size, &x2[l]);
-		getIntFromLine(stream, size, &y2[l]);
-		getIntFromLine(stream, size, &roomObjX[l]);
-		getIntFromLine(stream, size, &roomObjY[l]);
-		getIntFromLine(stream, size, &trackObj[l]);
-		getIntFromLine(stream, size, &visible[l]);
-		getIntFromLine(stream, size, &isDoor[l]);
+		p.parseInt(objectNum[l]);
+		p.parseString(objName[l]);
+		p.parseInt(x1[l]);
+		p.parseInt(y1[l]);
+		p.parseInt(x2[l]);
+		p.parseInt(y2[l]);
+		p.parseInt(roomObjX[l]);
+		p.parseInt(roomObjY[l]);
+		p.parseInt(trackObj[l]);
+		p.parseInt(visible[l]);
+		p.parseInt(isDoor[l]);
 		if (isDoor[l] != 0) {
-			getStringFromLine(stream, size, _targetSurface[l]);
-			getIntFromLine(stream, size, &_destX[l]);
-			getIntFromLine(stream, size, &_destY[l]);
-			getIntFromLine(stream, size, &trackCharacter_alkeva[l]);
-			getIntFromLine(stream, size, &roomExits[l]);
+			p.parseString(_targetSurface[l]);
+			p.parseInt(_destX[l]);
+			p.parseInt(_destY[l]);
+			p.parseInt(trackCharacter_alkeva[l]);
+			p.parseInt(roomExits[l]);
 			updateDoor(l);
 		}
 	}
 
-	getIntFromLine(stream, size, &floorX1);
-	getIntFromLine(stream, size, &floorY1);
-	getIntFromLine(stream, size, &floorX2);
-	getIntFromLine(stream, size, &floorY2);
+	p.parseInt(floorX1);
+	p.parseInt(floorY1);
+	p.parseInt(floorX2);
+	p.parseInt(floorY2);
 
 	if (currentChapter != 2) {
-		getIntFromLine(stream, size, &upperLimit);
-		getIntFromLine(stream, size, &lowerLimit);
+		p.parseInt(upperLimit);
+		p.parseInt(lowerLimit);
 	}
-	delete stream;
+	// no need to delete the stream, since TextResourceParser takes ownership
+	// delete stream;
 
 	if (currentChapter == 2 && martin != 0) {
 		loadPic(pant2, extraSurface);


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