From e2aca904ec8c0e0f23124b00b4b1eb525105f95e Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Thu, 18 Feb 2016 22:05:51 -0500 Subject: TITANIC: Implement saving methods for List and ListItem --- engines/titanic/list.h | 42 -------------- engines/titanic/module.mk | 1 + engines/titanic/objects/list.cpp | 88 +++++++++++++++++++++++++++++ engines/titanic/objects/list.h | 82 +++++++++++++++++++++++++++ engines/titanic/objects/saveable_object.cpp | 10 ++-- engines/titanic/objects/saveable_object.h | 6 +- engines/titanic/simple_file.cpp | 19 ++++++- engines/titanic/simple_file.h | 16 ++++-- 8 files changed, 208 insertions(+), 56 deletions(-) delete mode 100644 engines/titanic/list.h create mode 100644 engines/titanic/objects/list.cpp create mode 100644 engines/titanic/objects/list.h (limited to 'engines/titanic') diff --git a/engines/titanic/list.h b/engines/titanic/list.h deleted file mode 100644 index bea3776f0f..0000000000 --- a/engines/titanic/list.h +++ /dev/null @@ -1,42 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - */ - -#ifndef TITANIC_LIST_H -#define TITANIC_LIST_H - -#include "common/scummsys.h" -#include "common/list.h" -#include "titanic/objects/saveable_object.h" - -namespace Titanic { - -class ListItem: public CSaveableObject { -}; - -class List : public CSaveableObject, Common::List { -public: - -}; - -} // End of namespace Titanic - -#endif /* TITANIC_LIST_H */ diff --git a/engines/titanic/module.mk b/engines/titanic/module.mk index 17805e171a..abd351250d 100644 --- a/engines/titanic/module.mk +++ b/engines/titanic/module.mk @@ -13,6 +13,7 @@ MODULE_OBJS := \ titanic.o \ video_surface.o \ objects/file_item.o \ + objects/list.o \ objects/message_target.o \ objects/project_item.o \ objects/saveable_object.o \ diff --git a/engines/titanic/objects/list.cpp b/engines/titanic/objects/list.cpp new file mode 100644 index 0000000000..ddcddb04dd --- /dev/null +++ b/engines/titanic/objects/list.cpp @@ -0,0 +1,88 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "titanic/objects/list.h" + +namespace Titanic { + +void ListItem::save(SimpleFile *file, int indent) const { + // Should always be overriden in descendents, so just write a dummy value + file->writeNumberLine(0, indent); +} + +void ListItem::load(SimpleFile *file) { + // Should always be overriden in descendents, so just read the dummy value + file->readNumber(); +} + +/*------------------------------------------------------------------------*/ + +List::List() { +} + +void List::save(SimpleFile *file, int indent) const { + file->writeNumberLine(0, indent); + saveItems(file, indent); +} + +void List::load(SimpleFile *file) { + file->readNumber(); + loadItems(file); +} + +void List::saveItems(SimpleFile *file, int indent) const { + // Write out number of items + file->writeQuotedLine("L", indent); + file->writeNumberLine(size(), indent); + + // Iterate through writing entries + List::const_iterator i; + for (i = begin(); i != end(); ++i) { + const ListItem *item = *i; + item->saveHeader(file, indent); + item->save(file, indent + 1); + item->saveFooter(file, indent); + } +} + +void List::loadItems(SimpleFile *file) { + file->readBuffer(); + uint count = file->readNumber(); + + for (uint idx = 0; idx < count; ++idx) { + // Validate the class start header + if (!file->IsClassStart()) + error("Unexpected class end"); + + // Get item's class name and use it to instantiate an item + CString className = file->readString(); + CSaveableObject *newItem = CSaveableObject::createInstance(className); + if (!newItem) + error("Could not create instance of %s", className.c_str()); + + // Validate the class end footer + if (file->IsClassStart()) + error("Unexpected class start"); + } +} + +} // End of namespace Titanic diff --git a/engines/titanic/objects/list.h b/engines/titanic/objects/list.h new file mode 100644 index 0000000000..f98912795d --- /dev/null +++ b/engines/titanic/objects/list.h @@ -0,0 +1,82 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef TITANIC_LIST_H +#define TITANIC_LIST_H + +#include "common/scummsys.h" +#include "common/list.h" +#include "titanic/objects/saveable_object.h" + +namespace Titanic { + +class ListItem: public CSaveableObject { +public: + /** + * Return the class name + */ + virtual const char *getClassName() const { return "ListItem"; } + + /** + * Save the data for the class to file + */ + virtual void save(SimpleFile *file, int indent) const; + + /** + * Load the data for the class from file + */ + virtual void load(SimpleFile *file); +}; + +class List : public CSaveableObject, Common::List { +private: + /** + * Write out the contents of the list + */ + void saveItems(SimpleFile *file, int indent) const; + + /** + * Read in the contents of a list + */ + void loadItems(SimpleFile *file); +public: + List(); + + /** + * Return the class name + */ + virtual const char *getClassName() const { return "List"; } + + /** + * Save the data for the class to file + */ + virtual void save(SimpleFile *file, int indent) const; + + /** + * Load the data for the class from file + */ + virtual void load(SimpleFile *file); +}; + +} // End of namespace Titanic + +#endif /* TITANIC_LIST_H */ diff --git a/engines/titanic/objects/saveable_object.cpp b/engines/titanic/objects/saveable_object.cpp index caa5192d19..25bd1645b5 100644 --- a/engines/titanic/objects/saveable_object.cpp +++ b/engines/titanic/objects/saveable_object.cpp @@ -22,10 +22,10 @@ #include "titanic/objects/saveable_object.h" #include "titanic/objects/file_item.h" +#include "titanic/objects/list.h" #include "titanic/objects/message_target.h" #include "titanic/objects/project_item.h" #include "titanic/objects/tree_item.h" -#include "titanic/list.h" namespace Titanic { @@ -60,21 +60,21 @@ CSaveableObject *CSaveableObject::createInstance(const Common::String &name) { return (*_classList)[name](); } -void CSaveableObject::save(SimpleFile *file, int indent) { +void CSaveableObject::save(SimpleFile *file, int indent) const { // Should always be overriden in descendents, so just write a dummy value file->writeNumberLine(0, indent); } void CSaveableObject::load(SimpleFile *file) { - // Should always be overriden in descendents, so just read a dummy value + // Should always be overriden in descendents, so just read the dummy value file->readNumber(); } -void CSaveableObject::saveHeader(SimpleFile *file, int indent) { +void CSaveableObject::saveHeader(SimpleFile *file, int indent) const { file->writeClassStart(getClassName(), indent); } -void CSaveableObject::saveFooter(SimpleFile *file, int indent) { +void CSaveableObject::saveFooter(SimpleFile *file, int indent) const { file->writeClassEnd(indent); } diff --git a/engines/titanic/objects/saveable_object.h b/engines/titanic/objects/saveable_object.h index e99d8c5525..4e7d949191 100644 --- a/engines/titanic/objects/saveable_object.h +++ b/engines/titanic/objects/saveable_object.h @@ -58,7 +58,7 @@ public: /** * Save the data for the class to file */ - virtual void save(SimpleFile *file, int indent); + virtual void save(SimpleFile *file, int indent) const; /** * Load the data for the class from file @@ -69,13 +69,13 @@ public: * Write out a header definition for the class to file * prior to saving the actual data for the class */ - virtual void saveHeader(SimpleFile *file, int indent); + virtual void saveHeader(SimpleFile *file, int indent) const; /** * Writes out a footer for the class after it's data has * been written to file */ - virtual void saveFooter(SimpleFile *file, int indent); + virtual void saveFooter(SimpleFile *file, int indent) const; }; } // End of namespace Titanic diff --git a/engines/titanic/simple_file.cpp b/engines/titanic/simple_file.cpp index 9f371b2009..cf95aca269 100644 --- a/engines/titanic/simple_file.cpp +++ b/engines/titanic/simple_file.cpp @@ -196,6 +196,14 @@ double SimpleFile::readFloat() { return floatValue; } +void SimpleFile::readBuffer(char *buffer, size_t count) { + CString tempString = readString(); + if (buffer) { + strncpy(buffer, tempString.c_str(), count); + buffer[count - 1] = '\0'; + } +} + void SimpleFile::writeLine(const CString &str) { write(str.c_str(), str.size()); write("\r\n", 2); @@ -244,6 +252,12 @@ void SimpleFile::writeQuotedString(const CString &str) { write("\" ", 2); } +void SimpleFile::writeQuotedLine(const CString &str, int indent) { + writeIndent(indent); + writeQuotedString(str); + write("\n", 1); +} + void SimpleFile::writeNumber(int val) { CString str = CString::format("%ld ", val); write(str.c_str(), str.size()); @@ -260,14 +274,15 @@ void SimpleFile::writeIndent(uint indent) { write("\t", 1); } -bool SimpleFile::IsClassEnd() { +bool SimpleFile::IsClassStart() { char c; do { safeRead(&c, 1); } while (Common::isSpace(c)); - return c == '}'; + assert(c == '{' || c == '}'); + return c == '{'; } void SimpleFile::writeClassStart(const CString &classStr, int indent) { diff --git a/engines/titanic/simple_file.h b/engines/titanic/simple_file.h index 17dd82a130..65b0ac6f31 100644 --- a/engines/titanic/simple_file.h +++ b/engines/titanic/simple_file.h @@ -99,13 +99,16 @@ public: */ double readFloat(); + /** + * Read a string and copy it into an optionally passed buffer + */ + void readBuffer(char *buffer = nullptr, size_t count = 0); + /** * Write a string line */ void writeLine(const CString &str); - - /** * Write a string */ @@ -116,6 +119,11 @@ public: */ void writeQuotedString(const CString &str); + /** + * Write a quoted string line + */ + void writeQuotedLine(const CString &str, int indent); + /** * Write a number to file */ @@ -134,9 +142,9 @@ public: /** * 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 + * definition start or end. Returns true if it's a class start */ - bool IsClassEnd(); + bool IsClassStart(); /** * Write the starting header for a class definition -- cgit v1.2.3