From 3a1e7ccbae87aa5d515af596e5c15c478ed3c4b3 Mon Sep 17 00:00:00 2001 From: Nicola Mettifogo Date: Tue, 9 Feb 2010 01:22:24 +0000 Subject: Moved text parsing to a new class. svn-id: r48013 --- engines/drascula/converse.cpp | 31 +++++++++-------- engines/drascula/drascula.cpp | 36 -------------------- engines/drascula/drascula.h | 20 ++++++++--- engines/drascula/resource.cpp | 56 +++++++++++++++++++++++++++++++ engines/drascula/rooms.cpp | 78 ++++++++++++++++++++++--------------------- 5 files changed, 129 insertions(+), 92 deletions(-) diff --git a/engines/drascula/converse.cpp b/engines/drascula/converse.cpp index 40a1ac80a1..b2a7e217e6 100644 --- a/engines/drascula/converse.cpp +++ b/engines/drascula/converse.cpp @@ -137,7 +137,6 @@ void DrasculaEngine::converse(int index) { if (!stream) error("missing data file %s", fileName); - int size = stream->size(); int game1 = kDialogOptionUnselected, game2 = kDialogOptionUnselected, game3 = kDialogOptionUnselected; @@ -150,19 +149,23 @@ void DrasculaEngine::converse(int index) { 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); - - delete stream; + TextResourceParser p(stream, DisposeAfterUse::YES); + + 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]); diff --git a/engines/drascula/drascula.cpp b/engines/drascula/drascula.cpp index 56fbe48714..7aef05357f 100644 --- a/engines/drascula/drascula.cpp +++ b/engines/drascula/drascula.cpp @@ -604,42 +604,6 @@ bool DrasculaEngine::runCurrentChapter() { } } -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; diff --git a/engines/drascula/drascula.h b/engines/drascula/drascula.h index d32796acd6..34439a7373 100644 --- a/engines/drascula/drascula.h +++ b/engines/drascula/drascula.h @@ -267,6 +267,22 @@ private: }; +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 @@ public: 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); diff --git a/engines/drascula/resource.cpp b/engines/drascula/resource.cpp index efcb1c99ba..01f8d1ed5f 100644 --- a/engines/drascula/resource.cpp +++ b/engines/drascula/resource.cpp @@ -47,5 +47,61 @@ Common::SeekableReadStream *ArchiveMan::open(const Common::String &filename) { 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 diff --git a/engines/drascula/rooms.cpp b/engines/drascula/rooms.cpp index 4d9cb3b4cf..fcf3ff597b 100644 --- a/engines/drascula/rooms.cpp +++ b/engines/drascula/rooms.cpp @@ -1653,65 +1653,67 @@ void DrasculaEngine::enterRoom(int roomIndex) { 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); -- cgit v1.2.3