00001 /* 00002 SagaEngine library 00003 Copyright (c) 2002-2006 Skalden Studio AS 00004 00005 This software is provided 'as-is', without any express or implied 00006 warranty. In no event will the authors be held liable for any 00007 damages arising from the use of this software. 00008 00009 Permission is granted to distribute the library under the terms of the 00010 Q Public License version 1.0. Be sure to read and understand the license 00011 before using the library. It should be included here, or you may read it 00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl 00013 00014 The original version of this library can be located at: 00015 http://www.sagaengine.com/ 00016 00017 Rune Myrland 00018 rune@skalden.com 00019 */ 00020 00021 00022 #include "Phrase.hpp" 00023 #include "util/type/String.hpp" 00024 #include "util/error/Log.hpp" 00025 #include "sim/stat/DictionaryEntry.hpp" 00026 #include "sim/config/sim_config.hpp" 00027 #include <cstring> 00028 00029 using namespace se_core; 00030 00031 namespace se_client { 00032 Phrase 00033 ::Phrase() : currentLanguage_(0), phraseCount_(0), supportedLanguageCount_(0) { 00034 languages_ = new unsigned short[ MAX_PHRASES ]; 00035 types_ = new PhraseType[ MAX_PHRASES ]; 00036 namesC_ = new se_core::String*[ MAX_PHRASES ]; 00037 phrasesC_ = new se_core::String*[ MAX_PHRASES ]; 00038 names_ = new const char*[ MAX_PHRASES ]; 00039 phrases_ = new const char*[ MAX_PHRASES ]; 00040 00041 supportedLanguages_ = new Language[ MAX_LANGUAGES ]; 00042 00043 static DictionaryEntry ph1(DE_PHRASE_TYPE, MENU_LABEL, "MENU_LABEL"); 00044 static DictionaryEntry ph2(DE_PHRASE_TYPE, ACTION_LABEL, "ACTION_LABEL"); 00045 static DictionaryEntry ph3(DE_PHRASE_TYPE, THING_LABEL, "THING_LABEL"); 00046 static DictionaryEntry ph4(DE_PHRASE_TYPE, THING_DESCRIPTION, "THING_DESCRIPTION"); 00047 static DictionaryEntry ph5(DE_PHRASE_TYPE, SPEECH, "SPEECH"); 00048 } 00049 00050 00051 Phrase 00052 ::~Phrase() { 00053 while(phraseCount_ > 0) { 00054 --phraseCount_; 00055 delete namesC_[ phraseCount_ ]; 00056 delete phrasesC_[ phraseCount_ ]; 00057 } 00058 delete[] languages_; 00059 delete[] types_; 00060 delete[] namesC_; 00061 delete[] phrasesC_; 00062 delete[] names_; 00063 delete[] phrases_; 00064 00065 delete[] supportedLanguages_; 00066 } 00067 00068 00069 void Phrase 00070 ::setLanguage(unsigned short language) { 00071 currentLanguage_ = language; 00072 } 00073 00074 void Phrase 00075 ::clampToSupportedLanguage() { 00076 for(int i = 0; i < supportedLanguageCount_; ++i) { 00077 if(supportedLanguages_[i].id_ == currentLanguage_) 00078 return; 00079 } 00080 Assert(supportedLanguageCount_ > 0); 00081 currentLanguage_ = supportedLanguages_[0].id_; 00082 } 00083 00084 00085 void Phrase 00086 ::addPhrase(unsigned short language, PhraseType type, String* nameC, String* phraseC) { 00087 Assert(phraseCount_ < MAX_PHRASES); 00088 00089 const char* name = nameC->get(); 00090 const char* phrase = phraseC->get(); 00091 unsigned short cl = currentLanguage_; 00092 currentLanguage_ = language; 00093 unsigned short index = findPhrase(type, name); 00094 00095 for(int i = phraseCount_; i > index; --i) { 00096 languages_[ i ] = languages_[ i - 1 ]; 00097 types_[ i ] = types_[ i - 1 ]; 00098 names_[ i ] = names_[ i - 1 ]; 00099 phrases_[ i ] = phrases_[ i - 1 ]; 00100 00101 namesC_[ i ] = namesC_[ i - 1 ]; 00102 phrasesC_[ i ] = phrasesC_[ i - 1 ]; 00103 } 00104 languages_[ index ] = language; 00105 types_[ index ] = type; 00106 namesC_[ index ] = nameC; 00107 phrasesC_[ index ] = phraseC; 00108 names_[ index ] = name; 00109 phrases_[ index ] = phrase; 00110 ++phraseCount_; 00111 00112 currentLanguage_ = cl; 00113 } 00114 00115 00116 unsigned short Phrase 00117 ::findPhrase(PhraseType type, const char* name) { 00118 unsigned short min = 0; 00119 unsigned short max = phraseCount_; 00120 unsigned short middle; 00121 while(max > min) { 00122 middle = (max + min) / 2; 00123 if(languages_[ middle ] < currentLanguage_ 00124 || (languages_[ middle ] == currentLanguage_ && types_[ middle ] < type) 00125 || (languages_[ middle ] == currentLanguage_ && types_[ middle ] == type && strcmp(names_[middle], name) < 0)) { 00126 min = middle + 1; 00127 } 00128 else { 00129 max = middle; 00130 } 00131 } 00132 return min; 00133 } 00134 00135 00136 const char *Phrase 00137 ::getPhrase(PhraseType type, const char* name) { 00138 int index = findPhrase(type, name); 00139 Assert(index >= 0 && index < phraseCount_); 00140 if(languages_[ index ] == currentLanguage_ && types_[ index ] == type && strcmp(names_[index], name) == 0) { 00141 return phrases_[ index ]; 00142 } 00143 00144 return 0; 00145 } 00146 00147 00148 unsigned short Phrase 00149 ::languageId(const char* name) { 00150 return name[0] * 256 + name[1]; 00151 } 00152 00153 00154 short Phrase 00155 ::typeIdOfName(const char* name) { 00156 if(strcmp(name, "MENU_LABEL") == 0) 00157 return MENU_LABEL; 00158 if(strcmp(name, "THING_LABEL") == 0) 00159 return THING_LABEL; 00160 if(strcmp(name, "ACTION_LABEL") == 0) 00161 return ACTION_LABEL; 00162 if(strcmp(name, "THING_DESCRIPTION") == 0) 00163 return THING_DESCRIPTION; 00164 if(strcmp(name, "SPEECH") == 0) 00165 return SPEECH; 00166 LogFatal(name); 00167 return UNDEFINED; 00168 } 00169 00170 00171 void Phrase 00172 ::addLanguage(unsigned short id, const char* name) { 00173 Assert(supportedLanguageCount_ < MAX_LANGUAGES); 00174 supportedLanguages_[ supportedLanguageCount_ ].name_ = name; 00175 supportedLanguages_[ supportedLanguageCount_ ].id_ = id; 00176 ++supportedLanguageCount_; 00177 } 00178 00179 00180 void Phrase 00181 ::addLanguage(const char* id, const char* name) { 00182 addLanguage(languageId(id), name); 00183 } 00184 }
Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:03 2007 by Doxygen version 1.3.9.1.