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/resource.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'engines/drascula/resource.cpp') 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 -- cgit v1.2.3