OTRANSL.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : OTRANSL.CPP
  21. //Description : Multi-lingual Translation Class
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <ALL.h>
  25. #include <OGAME.h>
  26. #include <OSYS.h>
  27. #include <OFILETXT.h>
  28. #include <OTRANSL.h>
  29. //--------- Define constant ---------//
  30. #define TRANSLATE_FILE_NAME DIR_RES"TRANSLAT.RES"
  31. //------- Format of the translation resource file --------//
  32. //
  33. // <Original Text>|<Translated Text>~
  34. //
  35. // | - separator between the original and the translated text
  36. // ~ - separator between text blocks
  37. //
  38. //--------------------------------------------------------//
  39. //----------- Define static functions --------------//
  40. static int sort_translate_table( const void *a, const void *b );
  41. //--------- Begin of function Translate:Translate --------//
  42. Translate::Translate()
  43. {
  44. memset( this, 0, sizeof(this) );
  45. }
  46. //---------- End of function Translate::Translate --------//
  47. //--------- Begin of function Translate:init --------//
  48. void Translate::init()
  49. {
  50. //---- read the whole file into the buf --------//
  51. if( !m.is_file_exist(TRANSLATE_FILE_NAME) )
  52. return;
  53. File fileTranslate;
  54. fileTranslate.file_open(TRANSLATE_FILE_NAME);
  55. int textBufSize=fileTranslate.file_size();
  56. translate_text_buf = mem_add(textBufSize);
  57. fileTranslate.file_read(translate_text_buf, textBufSize);
  58. fileTranslate.file_close();
  59. //----- count the no. of records ----------//
  60. char* textPtr=translate_text_buf;
  61. int i;
  62. for( i=0, rec_count=0 ; i<textBufSize ; i++, textPtr++ )
  63. {
  64. if( *textPtr == '|' )
  65. rec_count++;
  66. }
  67. //------ allocate the translation table ------//
  68. translate_table = (TranslateRec*) mem_add( sizeof(TranslateRec) * rec_count );
  69. //---- generate the translation table from the translation text ----//
  70. int recNo=1;
  71. textPtr=translate_text_buf;
  72. translate_table[recNo-1].from_text_ptr = textPtr;
  73. // ###### patch begin Gilbert 2/2 #####//
  74. // for( i=0 ; i<textBufSize ; i++, textPtr++ )
  75. for( i=0 ; textPtr-translate_text_buf<textBufSize ; i++, textPtr++ )
  76. // ###### end begin Gilbert 2/2 #####//
  77. {
  78. if( *textPtr == '|' )
  79. {
  80. *textPtr = NULL; // Nullify the sentence
  81. translate_table[recNo-1].to_text_ptr = textPtr+1;
  82. }
  83. if( *textPtr == '~' )
  84. {
  85. *textPtr = NULL; // Nullify the sentence
  86. recNo++;
  87. if( recNo>rec_count ) // end of the file
  88. break;
  89. //------- skip to the next line ----------//
  90. for( ; *textPtr != CHAR_RETURN && *textPtr != CHAR_EOF ; textPtr++ );
  91. if( *textPtr == CHAR_RETURN )
  92. textPtr++;
  93. if( *textPtr == CHAR_LINE_FEED )
  94. textPtr++;
  95. translate_table[recNo-1].from_text_ptr = textPtr;
  96. }
  97. }
  98. //-------- sort the translation table ----------//
  99. qsort( translate_table, rec_count, sizeof(TranslateRec), sort_translate_table );
  100. //-------- create the quick_seek_table ----------//
  101. memset( quick_seek_table, 0, sizeof(quick_seek_table) );
  102. int lastSeekChar=0;
  103. for( i=0 ; i<rec_count ; i++ )
  104. {
  105. if( translate_table[i].from_text_ptr[0] > lastSeekChar )
  106. {
  107. lastSeekChar = translate_table[i].from_text_ptr[0];
  108. quick_seek_table[lastSeekChar] = i+1;
  109. }
  110. }
  111. //----------- set init_flag ------------//
  112. init_flag=1;
  113. }
  114. //---------- End of function Translate::init --------//
  115. //--------- Begin of function Translate:deinit --------//
  116. void Translate::deinit()
  117. {
  118. if( translate_text_buf )
  119. {
  120. mem_del( translate_text_buf );
  121. translate_text_buf = NULL;
  122. }
  123. if( translate_table )
  124. {
  125. mem_del( translate_table );
  126. translate_table = NULL;
  127. }
  128. }
  129. //---------- End of function Translate::deinit --------//
  130. //--------- Begin of function Translate:process --------//
  131. //
  132. // Translate the given text into the foreign language.
  133. //
  134. // <char*> originalText - the original text to be translated
  135. //
  136. // return : <char*> the translated text.
  137. // if not found, return the original text.
  138. //
  139. char* Translate::process(char* originalText)
  140. {
  141. if( !init_flag )
  142. return originalText;
  143. //------ look up the quick_seek_table --------//
  144. int tableRecno = quick_seek_table[ *((unsigned char*)originalText) ];
  145. if( !tableRecno ) // not in the translation table
  146. return originalText;
  147. int i;
  148. for( i=tableRecno ; i<=rec_count ; i++ )
  149. {
  150. if( strcmp(translate_table[i-1].from_text_ptr, originalText)==0 )
  151. return translate_table[i-1].to_text_ptr;
  152. else
  153. {
  154. if( translate_table[i-1].from_text_ptr[0] != originalText[0] ) // all original text begins with this letter has been scanned and none has been found
  155. break;
  156. }
  157. }
  158. return originalText;
  159. }
  160. //---------- End of function Translate::process --------//
  161. //------ Begin of function sort_translate_table ------//
  162. //
  163. static int sort_translate_table( const void *a, const void *b )
  164. {
  165. return strcmp( ((TranslateRec*)a)->from_text_ptr, ((TranslateRec*)b)->from_text_ptr );
  166. }
  167. //------- End of function sort_translate_table ------//
  168. //------ Begin of function Translate::multi_to_win ----------//
  169. void Translate::multi_to_win(char *c, int len)
  170. {
  171. // look up table to convert multilingual char set to windows char set
  172. static unsigned char multi_to_win_table[] =
  173. "ÇüéâäàåçêëèïîìÄÅ"
  174. "ÉæÆôöòûùÿÖÜø£Ø×\x83"
  175. "áíóúñѪº¿\xae¬½¼¡«»"
  176. "?????ÁÂÀ©????¢¥?"
  177. "??????ãÃ???????¤"
  178. "ðÐÊËÈ'ÍÎÏ????¦Ì?"
  179. "ÓßÔÒõÕµÞþÚÛÙýÝ?´"
  180. "·±=¾¶§÷?°¨?¹³²??";
  181. unsigned char *textPtr = (unsigned char *)c;
  182. for( ; len > 0 && *textPtr; --len, ++textPtr )
  183. {
  184. if( *textPtr >= 0x80 && multi_to_win_table[*textPtr - 0x80] != '?' )
  185. *textPtr = multi_to_win_table[*textPtr - 0x80];
  186. }
  187. }
  188. //------ End of function Translate::multi_to_win ----------//