diff options
author | Paul Gilbert | 2016-03-08 21:13:24 -0500 |
---|---|---|
committer | Paul Gilbert | 2016-03-08 21:13:24 -0500 |
commit | 8a404e62aab32a236c77a32804bd934c533d9605 (patch) | |
tree | 2c004dcd0d0b0c717672531b582c65e5cc1abae5 /engines | |
parent | 34ade1f8cbd964314ebb603ffe4c8a76fbdb32c9 (diff) | |
download | scummvm-rg350-8a404e62aab32a236c77a32804bd934c533d9605.tar.gz scummvm-rg350-8a404e62aab32a236c77a32804bd934c533d9605.tar.bz2 scummvm-rg350-8a404e62aab32a236c77a32804bd934c533d9605.zip |
TITANIC: Implemented CTreeItem::findByName
Diffstat (limited to 'engines')
-rw-r--r-- | engines/titanic/core/named_item.cpp | 8 | ||||
-rw-r--r-- | engines/titanic/core/named_item.h | 15 | ||||
-rw-r--r-- | engines/titanic/core/project_item.cpp | 17 | ||||
-rw-r--r-- | engines/titanic/core/project_item.h | 4 | ||||
-rw-r--r-- | engines/titanic/core/tree_item.cpp | 21 | ||||
-rw-r--r-- | engines/titanic/core/tree_item.h | 21 | ||||
-rw-r--r-- | engines/titanic/string.cpp | 16 | ||||
-rw-r--r-- | engines/titanic/string.h | 15 |
8 files changed, 98 insertions, 19 deletions
diff --git a/engines/titanic/core/named_item.cpp b/engines/titanic/core/named_item.cpp index 10d9588590..641efbd249 100644 --- a/engines/titanic/core/named_item.cpp +++ b/engines/titanic/core/named_item.cpp @@ -39,4 +39,12 @@ void CNamedItem::load(SimpleFile *file) { CTreeItem::load(file); } +int CNamedItem::compareTo(const CString &name, int maxLen) const { + if (maxLen) { + return getName().left(maxLen).compareToIgnoreCase(name); + } else { + return getName().compareToIgnoreCase(name); + } +} + } // End of namespace Titanic diff --git a/engines/titanic/core/named_item.h b/engines/titanic/core/named_item.h index c86aae2529..0afbf709c5 100644 --- a/engines/titanic/core/named_item.h +++ b/engines/titanic/core/named_item.h @@ -42,6 +42,21 @@ public: * Load the data for the class from file */ virtual void load(SimpleFile *file); + + /** + * Returns true if the item is a named item + */ + virtual bool isNamedItem() const { return true; } + + /** + * Gets the name of the item, if any + */ + virtual const CString getName() const { return _name; } + + /** + * Compares the name of the item to a passed name + */ + virtual int compareTo(const CString &name, int maxLen) const; }; } // End of namespace Titanic diff --git a/engines/titanic/core/project_item.cpp b/engines/titanic/core/project_item.cpp index 488cdba6c9..de43b5bb41 100644 --- a/engines/titanic/core/project_item.cpp +++ b/engines/titanic/core/project_item.cpp @@ -337,23 +337,8 @@ CDontSaveFileItem *CProjectItem::getDontSaveFileItem() const { return nullptr; } -CRoomItem *CProjectItem::findHiddenRoom() const { +CRoomItem *CProjectItem::findHiddenRoom() { return dynamic_cast<CRoomItem *>(findByName("HiddenRoom")); } -CNamedItem *CProjectItem::findByName(const CString &name, int maxChars) const { - /* - CString nameLower = name; - nameLower.toLowercase(); - - CTreeItem *treeItem = this; - while (treeItem) { - CString nodeName = treeItem->getName(); - - } - */ - return nullptr; -} - - } // End of namespace Titanic diff --git a/engines/titanic/core/project_item.h b/engines/titanic/core/project_item.h index 2ec3a73647..4271559184 100644 --- a/engines/titanic/core/project_item.h +++ b/engines/titanic/core/project_item.h @@ -166,9 +166,7 @@ public: */ CDontSaveFileItem *getDontSaveFileItem() const; - CRoomItem *findHiddenRoom() const; - - CNamedItem *findByName(const CString &name, int maxChars = 0) const; + CRoomItem *findHiddenRoom(); }; } // End of namespace Titanic diff --git a/engines/titanic/core/tree_item.cpp b/engines/titanic/core/tree_item.cpp index 22c4e1ddac..31deab860d 100644 --- a/engines/titanic/core/tree_item.cpp +++ b/engines/titanic/core/tree_item.cpp @@ -23,6 +23,7 @@ #include "titanic/core/tree_item.h" #include "titanic/core/dont_save_file_item.h" #include "titanic/core/file_item.h" +#include "titanic/core/named_item.h" namespace Titanic { @@ -154,4 +155,24 @@ void CTreeItem::detach() { _priorSibling = _nextSibling = _parent = nullptr; } +CNamedItem *CTreeItem::findByName(const CString &name, int maxLen) { + CString nameLower = name; + nameLower.toLowercase(); + + for (CTreeItem *treeItem = this; treeItem; treeItem = scan(treeItem)) { + CString nodeName = treeItem->getName(); + nodeName.toLowercase(); + + if (maxLen) { + if (nodeName.left(maxLen).compareTo(nameLower)) + return dynamic_cast<CNamedItem *>(treeItem); + } else { + if (nodeName.compareTo(nameLower)) + return dynamic_cast<CNamedItem *>(treeItem); + } + } + + return nullptr; +} + } // End of namespace Titanic diff --git a/engines/titanic/core/tree_item.h b/engines/titanic/core/tree_item.h index acfd8bf2f4..afca5254df 100644 --- a/engines/titanic/core/tree_item.h +++ b/engines/titanic/core/tree_item.h @@ -29,6 +29,7 @@ namespace Titanic { class CGameManager; class CDontSaveFileItem; +class CNamedItem; class CTreeItem: public CMessageTarget { private: @@ -62,6 +63,21 @@ public: virtual bool isFileItem() const { return false; } /** + * Returns true if the item is a named item + */ + virtual bool isNamedItem() const { return false; } + + /** + * Gets the name of the item, if any + */ + virtual const CString getName() const { return CString(); } + + /** + * Compares the name of the item to a passed name + */ + virtual int compareTo(const CString &name, int maxLen) const { return false; } + + /** * Get the parent for the given item */ CTreeItem *getParent() const { return _parent; } @@ -131,6 +147,11 @@ public: * Detach the tree item from any other associated tree items */ void detach(); + + /** + * Finds a tree item by name + */ + CNamedItem *findByName(const CString &name, int maxLen = 0); }; } // End of namespace Titanic diff --git a/engines/titanic/string.cpp b/engines/titanic/string.cpp index 55b7f8ca52..128a9a38eb 100644 --- a/engines/titanic/string.cpp +++ b/engines/titanic/string.cpp @@ -20,8 +20,24 @@ * */ +#include "common/algorithm.h" #include "titanic/string.h" namespace Titanic { +CString CString::left(uint count) const { + return (count > size()) ? *this : CString(c_str(), c_str() + count); +} + +CString CString::right(uint count) const { + return (count > size()) ? *this : CString(c_str() - count, c_str()); +} + +CString CString::mid(uint start, uint count) const { + if (start >= size()) + return CString(); + else + return CString(c_str() + start, MIN(count, size() - start)); +} + } // End of namespace Titanic diff --git a/engines/titanic/string.h b/engines/titanic/string.h index 46cbb02ad5..076cce05a2 100644 --- a/engines/titanic/string.h +++ b/engines/titanic/string.h @@ -36,6 +36,21 @@ public: CString(const char *beginP, const char *endP) : Common::String(beginP, endP) {} CString(const String &str) : Common::String(str) {} explicit CString(char c) : Common::String(c) {} + + /** + * Returns the left n characters of the string + */ + CString left(uint count) const; + + /** + * Returns the right n characters of the string + */ + CString right(uint count) const; + + /** + * Returns a substring from within the string + */ + CString mid(uint start, uint count) const; }; } // End of namespace Titanic |