diff options
-rw-r--r-- | engines/titanic/compressed_file.cpp | 34 | ||||
-rw-r--r-- | engines/titanic/compressed_file.h | 11 | ||||
-rw-r--r-- | engines/titanic/simple_file.cpp | 115 | ||||
-rw-r--r-- | engines/titanic/simple_file.h | 71 |
4 files changed, 192 insertions, 39 deletions
diff --git a/engines/titanic/compressed_file.cpp b/engines/titanic/compressed_file.cpp index 7c0d9872d3..ea760167d7 100644 --- a/engines/titanic/compressed_file.cpp +++ b/engines/titanic/compressed_file.cpp @@ -20,7 +20,6 @@ * */ -#include "common/util.h" #include "titanic/compressed_file.h" namespace Titanic { @@ -106,28 +105,25 @@ CompressedFile::CompressedFile() : SimpleFile() { CompressedFile::~CompressedFile() { } -void CompressedFile::open(const Common::String &name, FileMode mode) { - SimpleFile::open(name, mode); +void CompressedFile::open(const Common::String &name) { + SimpleFile::open(name); - if (mode == FILE_READ) { - _decompressor.load(); - _fileMode = 2; - } else if (mode == FILE_WRITE) { - _decompressor.load(); - _fileMode = 1; - } + _decompressor.load(); + _fileMode = 2; } -void CompressedFile::open(Common::SeekableReadStream *stream, FileMode mode) { - SimpleFile::open(stream, mode); +void CompressedFile::open(Common::SeekableReadStream *stream) { + SimpleFile::open(stream); - if (mode == FILE_READ) { - _decompressor.load(); - _fileMode = 2; - } else if (mode == FILE_WRITE) { - _decompressor.load(); - _fileMode = 1; - } + _decompressor.load(); + _fileMode = 2; +} + +void CompressedFile::open(Common::OutSaveFile *stream) { + SimpleFile::open(stream); + + _decompressor.load(); + _fileMode = 1; } void CompressedFile::close() { diff --git a/engines/titanic/compressed_file.h b/engines/titanic/compressed_file.h index 814a1ab812..56e2c23343 100644 --- a/engines/titanic/compressed_file.h +++ b/engines/titanic/compressed_file.h @@ -97,12 +97,17 @@ public: /** * Open a file for access */ - virtual void open(const Common::String &name, FileMode mode = FILE_READ); + virtual void open(const Common::String &name); /** - * Set up a stream for access + * Set up a stream for read access */ - virtual void open(Common::SeekableReadStream *stream, FileMode mode = FILE_READ); + virtual void open(Common::SeekableReadStream *stream); + + /** + * Set up a stream for write access + */ + virtual void open(Common::OutSaveFile *stream); /** * Close the file diff --git a/engines/titanic/simple_file.cpp b/engines/titanic/simple_file.cpp index 038603c2d1..5848fbee67 100644 --- a/engines/titanic/simple_file.cpp +++ b/engines/titanic/simple_file.cpp @@ -20,41 +20,63 @@ * */ +#include "common/util.h" #include "titanic/simple_file.h" namespace Titanic { -SimpleFile::SimpleFile(): _stream(nullptr) { +SimpleFile::SimpleFile(): _inStream(nullptr), _outStream(nullptr), _lineCount(1) { } SimpleFile::~SimpleFile() { _file.close(); } -void SimpleFile::open(const Common::String &name, FileMode mode) { - assert(mode == FILE_READ); +void SimpleFile::open(const Common::String &name) { + close(); + if (!_file.open(name)) error("Could not find file - %s", name.c_str()); + _inStream = &_file; } -void SimpleFile::open(Common::SeekableReadStream *stream, FileMode mode) { +void SimpleFile::open(Common::SeekableReadStream *stream) { + close(); + _inStream = stream; +} + +void SimpleFile::open(Common::OutSaveFile *stream) { close(); - _stream = stream; + _outStream = stream; } void SimpleFile::close() { _file.close(); - _stream = nullptr; + if (_outStream) + _outStream->finalize(); + + _inStream = nullptr; + _outStream = nullptr; } void SimpleFile::safeRead(void *dst, size_t count) { - assert(_stream); if (unsafeRead(dst, count) != count) error("Could not read %d bytes", count); } size_t SimpleFile::unsafeRead(void *dst, size_t count) { - return _stream->read(dst, count); + assert(_inStream); + return _inStream->read(dst, count); +} + +size_t SimpleFile::write(const void *src, size_t count) { + assert(_outStream); + return _outStream->write(src, count); +} + +bool SimpleFile::eof() const { + assert(_inStream); + return _inStream->pos() >= _inStream->size(); } CString SimpleFile::readString() { @@ -174,4 +196,81 @@ double SimpleFile::readFloat() { return floatValue; } +void SimpleFile::writeLine(const CString &str) { + write(str.c_str(), str.size()); + write("\r\n", 2); +} + +void SimpleFile::writeString(const CString &str) { + if (str.empty()) + return; + + const char *msgP = str.c_str(); + char c; + + while (c = *msgP++) { + switch (c) { + case '\r': + write("\\r", 2); + break; + case '\n': + write("\\n", 2); + break; + case '\t': + write("\\t", 2); + break; + case '\"': + write("\\\"", 2); + break; + case '\\': + write("\\\\", 2); + break; + case '{': + write("\\{", 2); + break; + case '}': + write("\\}", 2); + break; + default: + write(&c, 1); + break; + } + } +} + +void SimpleFile::writeQuotedString(const CString &str) { + write("\"", 1); + writeString(str); + write("\" ", 2); +} + +void SimpleFile::writeIndent(uint indent) { + for (uint idx = 0; idx < indent; ++idx) + write("\t", 1); +} + +bool SimpleFile::IsClassEnd() { + char c; + + do { + safeRead(&c, 1); + } while (Common::isSpace(c)); + + return c == '}'; +} + +void SimpleFile::writeClassStart(const CString &classStr, int indent) { + write("\n", 1); + writeIndent(indent); + write("{\n", 2); + writeIndent(indent + 1); + writeQuotedString(classStr); + write("\n", 1); +} + +void SimpleFile::writeClassEnd(int indent) { + writeIndent(indent); + write("}\n", 2); +} + } // End of namespace Titanic diff --git a/engines/titanic/simple_file.h b/engines/titanic/simple_file.h index 6720d9455f..45d700018a 100644 --- a/engines/titanic/simple_file.h +++ b/engines/titanic/simple_file.h @@ -26,19 +26,20 @@ #include "common/scummsys.h" #include "common/file.h" #include "common/queue.h" +#include "common/savefile.h" #include "titanic/string.h" namespace Titanic { -enum FileMode { FILE_READ = 1, FILE_WRITE = 2 }; - class Decompressor; class DecompressorData; class SimpleFile { protected: Common::File _file; - Common::SeekableReadStream *_stream; + Common::SeekableReadStream *_inStream; + Common::OutSaveFile *_outStream; + int _lineCount; public: SimpleFile(); virtual ~SimpleFile(); @@ -46,12 +47,17 @@ public: /** * Open a file for access */ - virtual void open(const Common::String &name, FileMode mode = FILE_READ); + virtual void open(const Common::String &name); + + /** + * Set up a stream for read access + */ + virtual void open(Common::SeekableReadStream *stream); /** - * Set up a stream for access + * Set up a stream for write access */ - virtual void open(Common::SeekableReadStream *stream, FileMode mode = FILE_READ); + virtual void open(Common::OutSaveFile *stream); /** * Close the file @@ -69,19 +75,66 @@ public: virtual size_t unsafeRead(void *dst, size_t count); /** + * Write out data + */ + virtual size_t write(const void *src, size_t count); + + /** + * Return true if the end of the file has been reached + */ + bool eof() const; + + /** * Read a string from the file */ - virtual CString readString(); + CString readString(); /** * Read a number from the file */ - virtual int readNumber(); + int readNumber(); /** * Read a floating point number from the file */ - virtual double readFloat(); + double readFloat(); + + /** + * Write a line + */ + void writeLine(const CString &str); + + /** + * Write a string + */ + void writeString(const CString &str); + + /** + * Write a quoted string + */ + void writeQuotedString(const CString &str); + + /** + * Write out a number of tabs to form an indent in the output + */ + void writeIndent(uint indent); + + /** + * Validates that the following non-space character is either + * an opening or closing squiggly bracket denoting a class + * definition start or end. Returns true if it's a class end + */ + bool IsClassEnd(); + + /** + * Write the starting header for a class definition + */ + void writeClassStart(const CString &classStr, int indent); + + /** + * Write out the ending footer for a class definition + */ + void writeClassEnd(int indent); }; } // End of namespace Titanic |