Moved text parsing to a new class.

svn-id: r48013
This commit is contained in:
Nicola Mettifogo 2010-02-09 01:22:24 +00:00
parent 62741adf23
commit 3a1e7ccbae
5 changed files with 128 additions and 91 deletions

View file

@ -137,7 +137,6 @@ void DrasculaEngine::converse(int index) {
if (!stream) if (!stream)
error("missing data file %s", fileName); error("missing data file %s", fileName);
int size = stream->size();
int game1 = kDialogOptionUnselected, int game1 = kDialogOptionUnselected,
game2 = kDialogOptionUnselected, game2 = kDialogOptionUnselected,
game3 = kDialogOptionUnselected; game3 = kDialogOptionUnselected;
@ -150,19 +149,23 @@ void DrasculaEngine::converse(int index) {
selectVerb(kVerbNone); selectVerb(kVerbNone);
getStringFromLine(stream, size, phrase1); TextResourceParser p(stream, DisposeAfterUse::YES);
getStringFromLine(stream, size, phrase2);
getStringFromLine(stream, size, phrase3); p.parseString(phrase1);
getStringFromLine(stream, size, phrase4); p.parseString(phrase2);
getStringFromLine(stream, size, sound1); p.parseString(phrase3);
getStringFromLine(stream, size, sound2); p.parseString(phrase4);
getStringFromLine(stream, size, sound3); p.parseString(sound1);
getStringFromLine(stream, size, sound4); p.parseString(sound2);
getIntFromLine(stream, size, &answer1); p.parseString(sound3);
getIntFromLine(stream, size, &answer2); p.parseString(sound4);
getIntFromLine(stream, size, &answer3); p.parseInt(answer1);
p.parseInt(answer2);
p.parseInt(answer3);
// no need to delete the stream, since TextResourceParser takes ownership
// delete stream;
delete stream;
if (currentChapter == 2 && !strcmp(fileName, "op_5.cal") && flags[38] == 1 && flags[33] == 1) { if (currentChapter == 2 && !strcmp(fileName, "op_5.cal") && flags[38] == 1 && flags[33] == 1) {
strcpy(phrase3, _text[405]); strcpy(phrase3, _text[405]);

View file

@ -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() { bool DrasculaEngine::verify1() {
int l; int l;

View file

@ -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_SAVES 10
#define NUM_FLAGS 50 #define NUM_FLAGS 50
#define DIF_MASK 55 #define DIF_MASK 55
@ -593,10 +609,6 @@ public:
void MusicFadeout(); void MusicFadeout();
void playFile(const char *fname); 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 grr();
void updateAnim(int y, int destX, int destY, int width, int height, int count, byte* src, int delayVal = 3, bool copyRectangle = false); void updateAnim(int y, int destX, int destY, int width, int height, int count, byte* src, int delayVal = 3, bool copyRectangle = false);

View file

@ -47,5 +47,61 @@ Common::SeekableReadStream *ArchiveMan::open(const Common::String &filename) {
return createReadStreamForMember(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 } // End of namespace Drascula

View file

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