DIPTHONG.CPP 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: g:/library/source/rcs/./dipthong.c 1.15 1994/05/20 15:35:17 joe_bostic Exp $ */
  19. /***************************************************************************
  20. ** C O N F I D E N T I A L --- W E S T W O O D A S S O C I A T E S **
  21. ***************************************************************************
  22. * *
  23. * Project Name : Westwood Library *
  24. * *
  25. * File Name : DIPTHONG.C *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : February 23, 1992 *
  30. * *
  31. * Last Update : February 13, 1995 [BWG] *
  32. * *
  33. * DIGRAM or DIATOMIC encoding is the correct term for this method. *
  34. * This is a fixed dictionary digram encoding optimized for English text. *
  35. * *
  36. *-------------------------------------------------------------------------*
  37. * Functions: *
  38. * Extract_String -- Extracts a string pointer from a string data block. *
  39. * UnDip_Text -- Undipthongs a text string into specified buffer. *
  40. * Dip_Text -- Compresses text by using dipthonging. *
  41. * Fixup_Text -- Converts dipthonged foreign text into normal text. *
  42. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  43. //#include "function.h"
  44. //#include "ems.h"
  45. #include <keyboard.h>
  46. #include "dipthong.h"
  47. /***************************************************************************
  48. * Fixup_Text -- Converts dipthonged foreign text into normal text. *
  49. * *
  50. * Takes text that has been processed (or undipped) to hold foriegn *
  51. * language character pairs (needed for Window_Print) and converts it *
  52. * so that Text_Print will print it properly. Typically this would be *
  53. * used after text has been undipped but before it will be Text_Printed.*
  54. * Text that is to be Window_Printed doesn't and mustn't have its text *
  55. * processed by this routine. *
  56. * *
  57. * INPUT: source -- Pointer to the source string to process. *
  58. * *
  59. * dest -- Destination buffer to hold the processed string. *
  60. * *
  61. * OUTPUT: none *
  62. * *
  63. * WARNINGS: This routine will only reduce the size of the string if it *
  64. * modifies it at all. Because of this it is quite legal to *
  65. * pass the same pointers to this routine so that it will *
  66. * modify the string "in place". *
  67. * *
  68. * HISTORY: *
  69. * 08/13/1993 JLB : Created. *
  70. * 10/06/1994 JLB : Handles source string in EMS. *
  71. *=========================================================================*/
  72. void Fixup_Text(char const *source, char *dest)
  73. {
  74. if (source && dest) {
  75. char const *src;
  76. src = source;
  77. while (*src) {
  78. if (*src == KA_EXTEND) {
  79. src++;
  80. *dest++ = (*src++) + 127;
  81. } else {
  82. *dest++ = *src++;
  83. }
  84. }
  85. *dest = '\0';
  86. }
  87. }
  88. /***************************************************************************
  89. * Dip_Text -- Compresses text by using dipthonging. *
  90. * *
  91. * This routine is used to compress text by using dipthonging. Text *
  92. * that is compressed in this fashion usually is reduced in size by *
  93. * approximately 40%. *
  94. * *
  95. * INPUT: source -- Pointer to the source string to compress. *
  96. * *
  97. * dest -- Pointer to the buffer that will hold the dipthong *
  98. * text output. *
  99. * *
  100. * OUTPUT: Returns the number of bytes output into the output buffer. *
  101. * *
  102. * WARNINGS: none *
  103. * *
  104. * HISTORY: *
  105. * 08/13/1993 JLB : Created. *
  106. *=========================================================================*/
  107. int Dip_Text(char const *source, char *dest)
  108. {
  109. unsigned char first, // First character in pair.
  110. next; // Second character in pair.
  111. int len; // Length of output string.
  112. int common, // Common character index.
  113. dipthong; // Dipthong character index.
  114. len = 0; // No output characters YET.
  115. first = *source++;
  116. next = *source;
  117. while (first) {
  118. if (first > 127) {
  119. /*
  120. ** Characters greater than 127 cannot be dipthonged. They must
  121. ** be preceeded with an extended character code.
  122. */
  123. *dest++ = KA_EXTEND;
  124. len++;
  125. first -= 127;
  126. } else {
  127. /*
  128. ** Normal characters can be dipthonged. First see if there is a
  129. ** match in the Common table.
  130. */
  131. for (common = 0; common < 16; common++) {
  132. if (Common[common] == first) {
  133. /*
  134. ** Common character found. See if there is a matching
  135. ** Dipthong character.
  136. */
  137. for (dipthong = 0; dipthong < 8; dipthong++) {
  138. if (Dipthong[common][dipthong] == next) {
  139. first = (unsigned char)(common << 3) | (unsigned char)dipthong | 0x80;
  140. source++;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. /*
  147. ** Output the translated character to the destination buffer.
  148. */
  149. *dest++ = first;
  150. len++;
  151. first = *source++;
  152. next = *source;
  153. }
  154. *dest = '\0';
  155. return(len);
  156. }
  157. /***************************************************************************
  158. * UnDip_Text -- Undipthongs a text string into specified buffer. *
  159. * *
  160. * This routine is used to undipthong a text string and place the *
  161. * undipped text into the buffer specified. Since dipthonged text is *
  162. * compressed, in order for the text to be used it must be undipped *
  163. * first. *
  164. * *
  165. * INPUT: source -- Pointer to the dipped string. *
  166. * *
  167. * dest -- Pointer to the destination buffer. *
  168. * *
  169. * OUTPUT: Returns the number of bytes placed into the destination *
  170. * buffer. *
  171. * *
  172. * WARNINGS: Be sure the destination buffer is big enough to hold the *
  173. * undipped text. *
  174. * *
  175. * HISTORY: *
  176. * 08/13/1993 JLB : Created. *
  177. * 10/06/1994 JLB : Handles source string in EMS. *
  178. *=========================================================================*/
  179. int UnDip_Text(char const *source, char *dest)
  180. {
  181. int c; // Source input character.
  182. int common; // Common character index.
  183. int len; // Length of output string.
  184. char const *src;
  185. len = 0; // Presume no translation.
  186. /*
  187. ** Sweep through the source text and dipthong it.
  188. */
  189. src = source;
  190. c = *src++;
  191. while (c) {
  192. /*
  193. ** Convert a dipthong character into it's component
  194. ** ASCII characters.
  195. */
  196. if (c & 0x80) {
  197. c &= 0x7F;
  198. common = (c & 0x78) >> 3;
  199. *dest++ = Common[common];
  200. len++;
  201. c = Dipthong[common][c & 0x07];
  202. }
  203. *dest++ = (unsigned char)c;
  204. len++;
  205. c = *src++;
  206. }
  207. /*
  208. ** End the output text with a '\0'.
  209. */
  210. *dest++ = '\0';
  211. return(len);
  212. }
  213. /***************************************************************************
  214. * Extract_String -- Extracts a string pointer from a string data block. *
  215. * *
  216. * This routine is used to find a pointer to the specified string *
  217. * inside a string block. String data blocks are created with the *
  218. * TEXTMAKE utility. The data block my reside in XMS or EMS memory, *
  219. * but of course the returned string pointer will also point to *
  220. * such memory. In this case, the string must be placed in real *
  221. * memory before it can be used. *
  222. * *
  223. * INPUT: data -- Pointer to the string data block. *
  224. * *
  225. * string -- The string number to extract (if < 0 then NULL *
  226. * is returned). *
  227. * *
  228. * OUTPUT: Returns with pointer to the string number specified. *
  229. * *
  230. * WARNINGS: none *
  231. * *
  232. * HISTORY: *
  233. * 08/13/1993 JLB : Created. *
  234. * 08/13/1993 JLB : Handles EMS or XMS data pointer. *
  235. *=========================================================================*/
  236. char *Extract_String(void const *data, int string)
  237. {
  238. unsigned short int const *ptr;
  239. unsigned int offset;
  240. if (!data || string < 0) return(NULL);
  241. ptr = (unsigned short int const *)data;
  242. return (((char*)data) + ptr[string]);
  243. }