From efa5493b2d01efed5c9488b3c324469969812296 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sun, 8 Mar 2009 08:17:43 +0000 Subject: SCI: Changed suffix list to be stored as a Common::List; also fixed spelling: suffices -> suffixes svn-id: r39214 --- engines/sci/engine/game.cpp | 4 +- engines/sci/engine/kstring.cpp | 2 +- engines/sci/engine/savegame.cfsml | 3 +- engines/sci/engine/savegame.cpp | 5 +-- engines/sci/engine/scriptconsole.cpp | 20 ++++----- engines/sci/engine/scriptdebug.cpp | 4 +- engines/sci/engine/state.cpp | 2 - engines/sci/engine/state.h | 3 +- engines/sci/scicore/vocabulary.cpp | 81 ++++++++++++++---------------------- engines/sci/scicore/vocabulary.h | 30 +++++++------ 10 files changed, 63 insertions(+), 91 deletions(-) diff --git a/engines/sci/engine/game.cpp b/engines/sci/engine/game.cpp index a220d33f98..572fe9dc90 100644 --- a/engines/sci/engine/game.cpp +++ b/engines/sci/engine/game.cpp @@ -49,7 +49,7 @@ static int _init_vocabulary(EngineState *s) { // initialize vocabulary and relat sciprintf("Initializing vocabulary\n"); if ((s->resmgr->_sciVersion < SCI_VERSION_01_VGA) && (s->parser_words = vocab_get_words(s->resmgr, &(s->parser_words_nr)))) { - s->parser_suffices = vocab_get_suffices(s->resmgr, &(s->parser_suffices_nr)); + vocab_get_suffixes(s->resmgr, s->_parserSuffixes); if ((s->parser_branches = vocab_get_branches(s->resmgr, &(s->parser_branches_nr)))) // Now build a GNF grammar out of this s->parser_rules = vocab_build_gnf(s->parser_branches, s->parser_branches_nr); @@ -78,7 +78,7 @@ static void _free_vocabulary(EngineState *s) { if (s->parser_words) { vocab_free_words(s->parser_words, s->parser_words_nr); - vocab_free_suffices(s->resmgr, s->parser_suffices, s->parser_suffices_nr); + vocab_free_suffixes(s->resmgr, s->_parserSuffixes); vocab_free_branches(s->parser_branches); vocab_free_rule_list(s->parser_rules); } diff --git a/engines/sci/engine/kstring.cpp b/engines/sci/engine/kstring.cpp index 2a2d97b17a..fa61813044 100644 --- a/engines/sci/engine/kstring.cpp +++ b/engines/sci/engine/kstring.cpp @@ -270,7 +270,7 @@ reg_t kParse(EngineState *s, int funct_nr, int argc, reg_t *argv) { words = vocab_tokenize_string(string, &words_nr, s->parser_words, s->parser_words_nr, - s->parser_suffices, s->parser_suffices_nr, + s->_parserSuffixes, &error); s->parser_valid = 0; /* not valid */ diff --git a/engines/sci/engine/savegame.cfsml b/engines/sci/engine/savegame.cfsml index eb5b0d0ffc..ce582ca730 100644 --- a/engines/sci/engine/savegame.cfsml +++ b/engines/sci/engine/savegame.cfsml @@ -1146,8 +1146,7 @@ EngineState *gamestate_restore(EngineState *s, Common::SeekableReadStream *fh) { retval->parser_rules = s->parser_rules; retval->parser_words_nr = s->parser_words_nr; retval->parser_words = s->parser_words; - retval->parser_suffices_nr = s->parser_suffices_nr; - retval->parser_suffices = s->parser_suffices; + retval->_parserSuffixes = s->_parserSuffixes; retval->parser_branches_nr = s->parser_branches_nr; retval->parser_branches = s->parser_branches; diff --git a/engines/sci/engine/savegame.cpp b/engines/sci/engine/savegame.cpp index 5b35336763..ca9ecff0a1 100644 --- a/engines/sci/engine/savegame.cpp +++ b/engines/sci/engine/savegame.cpp @@ -5306,8 +5306,7 @@ EngineState *gamestate_restore(EngineState *s, Common::SeekableReadStream *fh) { retval->parser_rules = s->parser_rules; retval->parser_words_nr = s->parser_words_nr; retval->parser_words = s->parser_words; - retval->parser_suffices_nr = s->parser_suffices_nr; - retval->parser_suffices = s->parser_suffices; + retval->_parserSuffixes = s->_parserSuffixes; retval->parser_branches_nr = s->parser_branches_nr; retval->parser_branches = s->parser_branches; @@ -5383,7 +5382,7 @@ bool get_savegame_metadata(Common::SeekableReadStream* stream, SavegameMetadata* } } // End of auto-generated CFSML data reader code -#line 1195 "engines/sci/engine/savegame.cfsml" +#line 1194 "engines/sci/engine/savegame.cfsml" if (read_eof) return false; diff --git a/engines/sci/engine/scriptconsole.cpp b/engines/sci/engine/scriptconsole.cpp index d2f8417e4a..e2a1e94826 100644 --- a/engines/sci/engine/scriptconsole.cpp +++ b/engines/sci/engine/scriptconsole.cpp @@ -707,31 +707,27 @@ static int c_list_words(EngineState *s) { return 0; } -int c_list_suffices(EngineState *s) { - suffix_t **suffices; - int suffices_nr; - int i; +int c_list_suffixes(EngineState *s) { + SuffixList suffixes; char word_buf[256], alt_buf[256]; - suffices = vocab_get_suffices(s->resmgr, &suffices_nr); - - if (!suffices) { + if (!vocab_get_suffixes(s->resmgr, suffixes)) { sciprintf("No suffix vocabulary.\n"); return 1; } - for (i = 0; i < suffices_nr; i++) { - suffix_t *suf = suffices[i]; - + int i = 0; + for (SuffixList::const_iterator suf = suffixes.begin(); suf != suffixes.end(); ++suf) { strncpy(word_buf, suf->word_suffix, suf->word_suffix_length); word_buf[suf->word_suffix_length] = 0; strncpy(alt_buf, suf->alt_suffix, suf->alt_suffix_length); alt_buf[suf->alt_suffix_length] = 0; sciprintf("%4d: (%03x) -%12s => -%12s (%03x)\n", i, suf->class_mask, word_buf, alt_buf, suf->result_class); + ++i; } - vocab_free_suffices(s->resmgr, suffices, suffices_nr); + vocab_free_suffixes(s->resmgr, suffixes); return 0; } @@ -829,7 +825,7 @@ static int c_list(EngineState *s) { return c_kernelnames(s); else if (!strcmp("suffixes", cmd_params[0].str) || !strcmp("suffices", cmd_params[0].str) || !strcmp("sufficos", cmd_params[0].str)) // sufficos: Accusative Plural of 'suffix' - return c_list_suffices(s); + return c_list_suffixes(s); else if (!strcmp("words", cmd_params[0].str)) return c_list_words(s); else if (strcmp("restypes", cmd_params[0].str) == 0) { diff --git a/engines/sci/engine/scriptdebug.cpp b/engines/sci/engine/scriptdebug.cpp index 62967c9df6..7a579179c8 100644 --- a/engines/sci/engine/scriptdebug.cpp +++ b/engines/sci/engine/scriptdebug.cpp @@ -791,7 +791,7 @@ int c_sim_parse(EngineState *s) { if (openb) *openb = 0; // remove them and the rest - result = vocab_lookup_word(token, strlen(token), s->parser_words, s->parser_words_nr, s->parser_suffices, s->parser_suffices_nr); + result = vocab_lookup_word(token, strlen(token), s->parser_words, s->parser_words_nr, s->_parserSuffixes); if (result) { s->parser_nodes[i].type = 0; @@ -1026,7 +1026,7 @@ int c_parse(EngineState *s) { string = cmd_params[0].str; sciprintf("Parsing '%s'\n", string); words = vocab_tokenize_string(string, &words_nr, s->parser_words, s->parser_words_nr, - s->parser_suffices, s->parser_suffices_nr, &error); + s->_parserSuffixes, &error); if (words) { int i, syntax_fail = 0; diff --git a/engines/sci/engine/state.cpp b/engines/sci/engine/state.cpp index 65262da82f..0c7cbd8411 100644 --- a/engines/sci/engine/state.cpp +++ b/engines/sci/engine/state.cpp @@ -145,8 +145,6 @@ EngineState::EngineState() : _dirseeker(this) { sys_strings = 0; parser_words = 0; parser_words_nr = 0; - parser_suffices = 0; - parser_suffices_nr = 0; parser_branches = 0; parser_rules = 0; parser_branches_nr = 0; diff --git a/engines/sci/engine/state.h b/engines/sci/engine/state.h index 2b4a991118..bc33f6de92 100644 --- a/engines/sci/engine/state.h +++ b/engines/sci/engine/state.h @@ -254,8 +254,7 @@ struct EngineState { /* Parser data: */ word_t **parser_words; int parser_words_nr; - suffix_t **parser_suffices; - int parser_suffices_nr; + SuffixList _parserSuffixes; parse_tree_branch_t *parser_branches; parse_rule_list_t *parser_rules; /* GNF rules used in the parser algorithm */ int parser_branches_nr; diff --git a/engines/sci/scicore/vocabulary.cpp b/engines/sci/scicore/vocabulary.cpp index b7a9401300..3acd189866 100644 --- a/engines/sci/scicore/vocabulary.cpp +++ b/engines/sci/scicore/vocabulary.cpp @@ -154,63 +154,46 @@ const char *vocab_get_any_group_word(int group, word_t **words, int words_nr) { return "{invalid}"; } -suffix_t **vocab_get_suffices(ResourceManager *resmgr, int *suffices_nr) { - int counter = 0; - suffix_t **suffices; +bool vocab_get_suffixes(ResourceManager *resmgr, SuffixList &suffixes) { Resource *resource = resmgr->findResource(kResourceTypeVocab, VOCAB_RESOURCE_SUFFIX_VOCAB, 1); unsigned int seeker = 1; if (!resource) { - fprintf(stderr, "Could not find suffix vocabulary!\n"); - return NULL; // Not critical + warning("Could not find suffix vocabulary"); + return false; // Not critical } - suffices = (suffix_t**)sci_malloc(sizeof(suffix_t *)); - while ((seeker < resource->size - 1) && (resource->data[seeker + 1] != 0xff)) { - char *alt_suffix = (char *) resource->data + seeker; - int alt_len = strlen(alt_suffix); - char *word_suffix; - int word_len; + suffix_t suffix; - suffices = (suffix_t**)sci_realloc(suffices, sizeof(suffix_t *) * (counter + 1)); + suffix.alt_suffix = (char *)resource->data + seeker; + suffix.alt_suffix_length = strlen(suffix.alt_suffix); + seeker += suffix.alt_suffix_length + 1; // Hit end of string - seeker += alt_len + 1; // Hit end of string - word_suffix = (char *)resource->data + seeker + 3; // Beginning of next string +1 (ignore '*') - word_len = strlen(word_suffix); + suffix.class_mask = (int16)READ_BE_UINT16(resource->data + seeker); + seeker += 2; - suffices[counter] = (suffix_t *)sci_malloc(sizeof(suffix_t)); - // allocate enough memory to store the strings + // Beginning of next string - skip leading '*' + seeker++; - suffices[counter]->word_suffix = word_suffix; - suffices[counter]->alt_suffix = alt_suffix; + suffix.word_suffix = (char *)resource->data + seeker; + suffix.word_suffix_length = strlen(suffix.word_suffix); + seeker += suffix.word_suffix_length + 1; - suffices[counter]->alt_suffix_length = alt_len; - suffices[counter]->word_suffix_length = word_len; - suffices[counter]->class_mask = (int16)READ_BE_UINT16(resource->data + seeker); - - seeker += word_len + 4; - suffices[counter]->result_class = (int16)READ_BE_UINT16(resource->data + seeker); + suffix.result_class = (int16)READ_BE_UINT16(resource->data + seeker); seeker += 3; // Next entry - ++counter; + suffixes.push_back(suffix); } - *suffices_nr = counter; - - return suffices; + return true; } -void vocab_free_suffices(ResourceManager *resmgr, suffix_t **suffices, int suffices_nr) { - int i; - +void vocab_free_suffixes(ResourceManager *resmgr, SuffixList &suffixes) { resmgr->unlockResource(resmgr->findResource(kResourceTypeVocab, VOCAB_RESOURCE_SUFFIX_VOCAB, 0), VOCAB_RESOURCE_SUFFIX_VOCAB, kResourceTypeVocab); - for (i = 0; i < suffices_nr; i++) - free(suffices[i]); - - free(suffices); + suffixes.clear(); } void vocab_free_branches(parse_tree_branch_t *parser_branches) { @@ -258,13 +241,13 @@ parse_tree_branch_t *vocab_get_branches(ResourceManager * resmgr, int *branches_ result_word_t *vocab_lookup_word(char *word, int word_len, word_t **words, int words_nr, - suffix_t **suffices, int suffices_nr) { + const SuffixList &suffixes) { word_t *tempword = (word_t*)sci_malloc(sizeof(word_t) + word_len + 256); - // 256: For suffices. Should suffice. + // 256: For suffixes. Should suffice. word_t **dict_word; result_word_t *retval; char *tester; - int i, word_len_tmp; + int word_len_tmp; strncpy(&(tempword->word[0]), word, word_len); tempword->word[word_len] = 0; @@ -286,24 +269,24 @@ result_word_t *vocab_lookup_word(char *word, int word_len, word_t **words, int w return retval; } - // Now try all suffices - for (i = 0; i < suffices_nr; i++) - if (suffices[i]->alt_suffix_length <= word_len) { + // Now try all suffixes + for (SuffixList::const_iterator suffix = suffixes.begin(); suffix != suffixes.end(); ++suffix) + if (suffix->alt_suffix_length <= word_len) { - int suff_index = word_len - suffices[i]->alt_suffix_length; + int suff_index = word_len - suffix->alt_suffix_length; // Offset of the start of the suffix - if (scumm_strnicmp(suffices[i]->alt_suffix, word + suff_index, suffices[i]->alt_suffix_length) == 0) { // Suffix matched! + if (scumm_strnicmp(suffix->alt_suffix, word + suff_index, suffix->alt_suffix_length) == 0) { // Suffix matched! strncpy(&(tempword->word[0]), word, word_len); tempword->word[suff_index] = 0; // Terminate word at suffix start position... - strncat(&(tempword->word[0]), suffices[i]->word_suffix, suffices[i]->word_suffix_length); // ...and append "correct" suffix + strncat(&(tempword->word[0]), suffix->word_suffix, suffix->word_suffix_length); // ...and append "correct" suffix dict_word = (word_t**)bsearch(&tempword, words, words_nr, sizeof(word_t *), _vocab_cmp_words); - if ((dict_word) && ((*dict_word)->w_class & suffices[i]->class_mask)) { // Found it? + if ((dict_word) && ((*dict_word)->w_class & suffix->class_mask)) { // Found it? free(tempword); - retval->w_class = suffices[i]->result_class; // Use suffix class + retval->w_class = suffix->result_class; // Use suffix class retval->group = (*dict_word)->group; return retval; @@ -482,7 +465,7 @@ int vocab_build_simple_parse_tree(parse_tree_node_t *nodes, result_word_t *words #endif result_word_t *vocab_tokenize_string(char *sentence, int *result_nr, word_t **words, int words_nr, - suffix_t **suffices, int suffices_nr, char **error) { + const SuffixList &suffixes, char **error) { char *lastword = sentence; int pos_in_sentence = 0; char c; @@ -508,7 +491,7 @@ result_word_t *vocab_tokenize_string(char *sentence, int *result_nr, word_t **wo if (wordlen) { // Finished a word? lookup_result = - vocab_lookup_word(lastword, wordlen, words, words_nr, suffices, suffices_nr); + vocab_lookup_word(lastword, wordlen, words, words_nr, suffixes); // Look it up if (!lookup_result) { // Not found? diff --git a/engines/sci/scicore/vocabulary.h b/engines/sci/scicore/vocabulary.h index 1652a57b3a..c78603a3ff 100644 --- a/engines/sci/scicore/vocabulary.h +++ b/engines/sci/scicore/vocabulary.h @@ -27,6 +27,7 @@ #define SCI_SCICORE_VOCABULARY_H #include "common/str.h" +#include "common/list.h" #include "sci/scicore/versions.h" @@ -149,6 +150,8 @@ struct suffix_t { }; +typedef Common::List SuffixList; + struct result_word_t { @@ -239,8 +242,7 @@ word_t **vocab_get_words(ResourceManager *resmgr, int *word_counter); */ -void -vocab_free_words(word_t **words, int words_nr); +void vocab_free_words(word_t **words, int words_nr); /* Frees memory allocated by vocab_get_words ** Parameters: (word_t **) words: The words to free ** (int) words_nr: Number of words in the structure @@ -248,19 +250,17 @@ vocab_free_words(word_t **words, int words_nr); */ -suffix_t **vocab_get_suffices(ResourceManager *resmgr, int *suffices_nr); -/* Gets all suffixes from the suffix vocabulary +bool vocab_get_suffixes(ResourceManager *resmgr, SuffixList &suffixes); +/* Loads all suffixes from the suffix vocabulary. ** Parameters: (ResourceManager*) resmgr: Resource manager the resources are ** read from -** (int *) suffices_nr: The variable to store the number of suffices in -** Returns : (suffix_t **): A list of suffixes +** Returns : true on success, false on failure */ -void vocab_free_suffices(ResourceManager *resmgr, suffix_t **suffices, int suffices_nr); -/* Frees suffices_nr suffices +void vocab_free_suffixes(ResourceManager *resmgr, SuffixList &suffixes); +/* Frees all suffixes in the given list. ** Parameters: (ResourceManager *) resmgr: The resource manager to free from -** (suffix_t **) suffices: The suffixes to free -** (int) suffices_nr: Number of entrie sin suffices +** (SuffixList) suffixes: The suffixes to free ** Returns : (void) */ @@ -280,28 +280,26 @@ void vocab_free_branches(parse_tree_branch_t *parser_branches); */ result_word_t *vocab_lookup_word(char *word, int word_len, - word_t **words, int words_nr, suffix_t **suffices, int suffices_nr); + word_t **words, int words_nr, const SuffixList &suffixes); /* Looks up a single word in the words and suffixes list ** Parameters: (char *) word: Pointer to the word to look up ** (int) word_len: Length of the word to look up ** (word_t **) words: List of words ** (int) words_nr: Number of elements in 'words' -** (suffix_t **) suffices: List of suffices -** (int) suffices_nr: Number of entries in 'suffices' +** (SuffixList) suffixes: List of suffixes ** Returns : (result_word_t *) A malloc'd result_word_t, or NULL if the word ** could not be found. */ result_word_t *vocab_tokenize_string(char *sentence, int *result_nr, - word_t **words, int words_nr, suffix_t **suffices, int suffices_nr, char **error); + word_t **words, int words_nr, const SuffixList &suffixes, char **error); /* Tokenizes a string and compiles it into word_ts. ** Parameters: (char *) sentence: The sentence to examine ** (int *) result_nr: The variable to store the resulting number of words in ** (word_t **) words: The words to scan for ** (int) words_nr: Number of words to scan for -** (suffix_t **) suffices: suffixes to scan for -** (int) suffices_nr: Number of suffices to scan for +** (SuffixList) suffixes: suffixes to scan for ** (char **) error: Points to a malloc'd copy of the offending text or to NULL on error ** Returns : (word_t *): A list of word_ts containing the result, or NULL. ** On error, NULL is returned. If *error is NULL, the sentence did not contain any useful words; -- cgit v1.2.3