diff options
-rw-r--r-- | engines/titanic/true_talk/tt_node.cpp | 12 | ||||
-rw-r--r-- | engines/titanic/true_talk/tt_node.h | 5 | ||||
-rw-r--r-- | engines/titanic/true_talk/tt_string.cpp | 4 | ||||
-rw-r--r-- | engines/titanic/true_talk/tt_string.h | 1 | ||||
-rw-r--r-- | engines/titanic/true_talk/tt_string_node.cpp | 11 | ||||
-rw-r--r-- | engines/titanic/true_talk/tt_string_node.h | 5 | ||||
-rw-r--r-- | engines/titanic/true_talk/tt_synonym.cpp | 26 | ||||
-rw-r--r-- | engines/titanic/true_talk/tt_synonym.h | 9 | ||||
-rw-r--r-- | engines/titanic/true_talk/tt_vocab.cpp | 24 | ||||
-rw-r--r-- | engines/titanic/true_talk/tt_vocab.h | 2 | ||||
-rw-r--r-- | engines/titanic/true_talk/tt_word.cpp | 32 | ||||
-rw-r--r-- | engines/titanic/true_talk/tt_word.h | 13 |
12 files changed, 93 insertions, 51 deletions
diff --git a/engines/titanic/true_talk/tt_node.cpp b/engines/titanic/true_talk/tt_node.cpp index 8b175a0906..22695ad379 100644 --- a/engines/titanic/true_talk/tt_node.cpp +++ b/engines/titanic/true_talk/tt_node.cpp @@ -46,6 +46,18 @@ void TTnode::detach() { _nextP->_priorP = _priorP; } +void TTnode::deleteSiblings() { + // Detach current node from prior one, if there is one + if (_priorP) + _priorP->_nextP = nullptr; + + // Iterate through the linked chain of nodes, deleting each in turn + for (TTnode *curP = _nextP, *nextP = nullptr; nextP; curP = nextP) { + nextP = curP->_nextP; + delete curP; + } +} + TTnode *TTnode::getTail() { if (_nextP == nullptr) return this; diff --git a/engines/titanic/true_talk/tt_node.h b/engines/titanic/true_talk/tt_node.h index 668b909bd5..f8d1bc6766 100644 --- a/engines/titanic/true_talk/tt_node.h +++ b/engines/titanic/true_talk/tt_node.h @@ -44,6 +44,11 @@ public: void detach(); /** + * Delete any sibling chain attached to this node + */ + void deleteSiblings(); + + /** * Returns the final node at the end of the linked list of nodes */ TTnode *getTail(); diff --git a/engines/titanic/true_talk/tt_string.cpp b/engines/titanic/true_talk/tt_string.cpp index 70d6fe3fcf..df93a5669c 100644 --- a/engines/titanic/true_talk/tt_string.cpp +++ b/engines/titanic/true_talk/tt_string.cpp @@ -95,6 +95,10 @@ TTstring &TTstring::operator+=(char c) { return *this; } +bool TTstring::operator==(const TTstring &str) { + return _data && str._data && _data->_string == str._data->_string; +} + void TTstring::save(SimpleFile *file) const { file->writeFormat("%s", c_str()); } diff --git a/engines/titanic/true_talk/tt_string.h b/engines/titanic/true_talk/tt_string.h index cae1a5dc2f..434c6fe829 100644 --- a/engines/titanic/true_talk/tt_string.h +++ b/engines/titanic/true_talk/tt_string.h @@ -57,6 +57,7 @@ public: TTstring &operator+=(const char *str); TTstring &operator+=(const TTstring &str); TTstring &operator+=(char c); + bool operator==(const TTstring &str); const char &operator[](int index) { return *(c_str() + index); diff --git a/engines/titanic/true_talk/tt_string_node.cpp b/engines/titanic/true_talk/tt_string_node.cpp index 1c0b5b9a90..2bb0c5a74b 100644 --- a/engines/titanic/true_talk/tt_string_node.cpp +++ b/engines/titanic/true_talk/tt_string_node.cpp @@ -54,4 +54,15 @@ void TTstringNode::initialize(TTstringNode *oldNode) { delete oldNode; } +TTstringNode *TTstringNode::findByName(const TTstring &str, int mode) { + for (TTstringNode *nodeP = this; nodeP; nodeP = static_cast<TTstringNode *>(nodeP->_nextP)) { + if (nodeP->_mode == mode || (mode == 3 && nodeP->_mode < 3)) { + if (nodeP->_string == str) + return nodeP; + } + } + + return nullptr; +} + } // End of namespace Titanic diff --git a/engines/titanic/true_talk/tt_string_node.h b/engines/titanic/true_talk/tt_string_node.h index 31013a950e..ced162b439 100644 --- a/engines/titanic/true_talk/tt_string_node.h +++ b/engines/titanic/true_talk/tt_string_node.h @@ -47,6 +47,11 @@ public: int _field1C; public: TTstringNode(); + + /** + * Find a string node in the linked chain by name + */ + TTstringNode *findByName(const TTstring &str, int mode); }; } // End of namespace Titanic diff --git a/engines/titanic/true_talk/tt_synonym.cpp b/engines/titanic/true_talk/tt_synonym.cpp index 0e5ae39e82..636b4438f1 100644 --- a/engines/titanic/true_talk/tt_synonym.cpp +++ b/engines/titanic/true_talk/tt_synonym.cpp @@ -42,32 +42,16 @@ TTsynonym::TTsynonym(int mode, const char *str, FileHandle file) : _file = file; } -TTsynonym *TTsynonym::findByName(TTsynonym *start, const TTstring &str, int mode) { - for (; start; start = static_cast<TTsynonym *>(start->_nextP)) { - if (start->_mode == mode || (mode == 3 && start->_mode < 3)) { - if (!strcmp(start->_string.c_str(), str.c_str())) - start; - } - } - - return nullptr; -} - -TTsynonym *TTsynonym::copy(TTsynonym *src) { +TTsynonym *TTsynonym::copyFrom(const TTsynonym *src) { if (src->_field1C) { _field1C = 5; - return this; } else { _field1C = 0; - if (src == this) - return this; - - _string = src->_string; - TTsynonym *newNode = new TTsynonym(src); - initialize(newNode); - - return this; + if (src != this) + _string = src->_string; } + + return this; } int TTsynonym::save(SimpleFile *file) { diff --git a/engines/titanic/true_talk/tt_synonym.h b/engines/titanic/true_talk/tt_synonym.h index 288e9809ff..e95a6d4faa 100644 --- a/engines/titanic/true_talk/tt_synonym.h +++ b/engines/titanic/true_talk/tt_synonym.h @@ -40,14 +40,9 @@ public: TTsynonym(int mode, const char *str, FileHandle file); /** - * Copy the synonym + * Copies data from one synonym to another */ - TTsynonym *copy(TTsynonym *src); - - /** - * Scan for a synonym with a given string - */ - static TTsynonym *findByName(TTsynonym *start, const TTstring &str, int mode); + TTsynonym *copyFrom(const TTsynonym *src); /** * Save data for the synonym to file diff --git a/engines/titanic/true_talk/tt_vocab.cpp b/engines/titanic/true_talk/tt_vocab.cpp index 707cbafff4..fe8b622d70 100644 --- a/engines/titanic/true_talk/tt_vocab.cpp +++ b/engines/titanic/true_talk/tt_vocab.cpp @@ -33,7 +33,7 @@ namespace Titanic { TTvocab::TTvocab(int val): _headP(nullptr), _tailP(nullptr), _word(nullptr), - _fieldC(0), _field10(0), _field18(val) { + _fieldC(0), _field10(0), _vocabMode(val) { _field14 = load("STVOCAB.TXT"); } @@ -160,8 +160,8 @@ TTword *TTvocab::findWord(const TTstring &str) { TTword *word = _headP; while (word && !flag) { - if (_field18 != 3 || strcmp(word->c_str(), str)) { - if (word->scanCopy(str, tempNode, _field18)) + if (_vocabMode != 3 || strcmp(word->c_str(), str)) { + if (word->findSynByName(str, tempNode, _vocabMode)) flag = true; else word = word->_nextP; @@ -177,18 +177,20 @@ TTword *TTvocab::findWord(const TTstring &str) { TTword *TTvocab::getPrimeWord(TTstring &str, TTword **words) { TTsynonym *synonym = new TTsynonym(); char c = str.charAt(0); - TTword *vocabList = _headP; TTword *returnWord = nullptr; if (!Common::isDigit(c)) { returnWord = new TTword(str, 3, 300); - } else if (!vocabList) { - // No vocab present. Should never happen } else { TTword *foundWord = nullptr; - while (!foundWord && vocabList) { - if (_field18 == 3 && !strcmp(str.c_str(), vocabList->c_str())) { - + for (TTword *vocabP = _headP; vocabP && !foundWord; vocabP = vocabP->_nextP) { + if (_vocabMode == 3 && !strcmp(str.c_str(), vocabP->c_str())) { + foundWord = vocabP->copy(); + foundWord->_nextP = nullptr; + foundWord->setSyn(nullptr); + } else { + vocabP->findSynByName(str, synonym, _vocabMode); + // TODO } } @@ -196,8 +198,8 @@ TTword *TTvocab::getPrimeWord(TTstring &str, TTword **words) { } - if (words) - *words = vocabList; +// if (words) +// *words = vocabList; delete synonym; return returnWord; diff --git a/engines/titanic/true_talk/tt_vocab.h b/engines/titanic/true_talk/tt_vocab.h index 40e9458cb7..c417c7bf36 100644 --- a/engines/titanic/true_talk/tt_vocab.h +++ b/engines/titanic/true_talk/tt_vocab.h @@ -37,7 +37,7 @@ private: int _fieldC; int _field10; int _field14; - int _field18; + int _vocabMode; private: /** * Load the vocab data diff --git a/engines/titanic/true_talk/tt_word.cpp b/engines/titanic/true_talk/tt_word.cpp index 90c7cf1a9d..a09af4ab34 100644 --- a/engines/titanic/true_talk/tt_word.cpp +++ b/engines/titanic/true_talk/tt_word.cpp @@ -100,6 +100,15 @@ int TTword::readSyn(SimpleFile *file) { return 0; } +void TTword::setSyn(TTsynonym *synP) { + if (_synP) { + _synP->deleteSiblings(); + delete _synP; + } + + _synP = synP; +} + void TTword::appendNode(TTsynonym *node) { if (_synP) _synP->addNode(node); @@ -143,17 +152,20 @@ bool TTword::testFileHandle(FileHandle file) const { return true; } -TTword *TTword::scanCopy(const TTstring &str, TTsynonym *node, int mode) { - if (_synP) { - TTsynonym *strNode = TTsynonym::findByName(_synP, str, mode); - if (strNode) { - node->copy(strNode); - node->_priorP = nullptr; - node->_nextP = nullptr; - } - } +bool TTword::findSynByName(const TTstring &str, TTsynonym *dest, int mode) const { + if (!_synP) + return false; + + const TTsynonym *synP = static_cast<const TTsynonym *>(_synP->findByName(str, mode)); + if (synP) { + dest->copyFrom(synP); + dest->_priorP = nullptr; + dest->_nextP = nullptr; - return nullptr; + return true; + } else { + return false; + } } TTword *TTword::copy() { diff --git a/engines/titanic/true_talk/tt_word.h b/engines/titanic/true_talk/tt_word.h index 7e692ffeea..885409ccb0 100644 --- a/engines/titanic/true_talk/tt_word.h +++ b/engines/titanic/true_talk/tt_word.h @@ -64,6 +64,11 @@ public: int readSyn(SimpleFile *file); /** + * Set a new synonym for the word + */ + void setSyn(TTsynonym *synP); + + /** * Either sets the first synonym for a word, or adds it to an existing one */ void appendNode(TTsynonym *node); @@ -73,7 +78,13 @@ public: */ int load(SimpleFile *file, int mode); - TTword *scanCopy(const TTstring &str, TTsynonym *node, int mode); + /** + * Finds a synonym in the word by name, if one exists + * @param str Name to search for + * @param dest Destination synonym instance to copy match into + * @returns Returns true if a match was found + */ + bool findSynByName(const TTstring &str, TTsynonym *dest, int mode) const; const char *c_str() const { return _string.c_str(); } operator const char *() const { return c_str(); } |