Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

331 lines
9.9 KiB

  1. /*************************************************
  2. * convphon.c *
  3. * *
  4. * Copyright (C) 1995-1999 Microsoft Inc. *
  5. * *
  6. *************************************************/
  7. #include <stdio.h>
  8. #include <windows.h>
  9. WCHAR Seq2Key[43] = { 0,
  10. L'1', L'Q', L'A', L'Z', L'2', L'W', L'S', L'X',
  11. L'E', L'D', L'C', L'R', L'F', L'V', L'5', L'T',
  12. L'G', L'B', L'Y', L'H', L'N', L'U', L'J', L'M',
  13. L'8', L'I', L'K', L',', L'9', L'O', L'L', L'.',
  14. L'0', L'P', L';', L'/', L'-', L' ', L'6', L'3',
  15. L'4', L'7' };
  16. WORD Seq2DBCS[43] = { 0,
  17. 0xa374, 0xa375, 0xa376, 0xa377, 0xa378, 0xa379,
  18. 0xa37a, 0xa37b, 0xa37c, 0xa37d, 0xa37e, 0xa3a1,
  19. 0xa3a2, 0xa3a3, 0xa3a4, 0xa3a5, 0xa3a6, 0xa3a7,
  20. 0xa3a8, 0xa3a9, 0xa3aa, 0xa3b8, 0xa3b9, 0xa3ba,
  21. 0xa3ab, 0xa3ac, 0xa3ad, 0xa3ae, 0xa3af, 0xa3b0,
  22. 0xa3b1, 0xa3b2, 0xa3b3, 0xa3b4, 0xa3b5, 0xa3b6,
  23. 0xa3b7, 0xa3bc, 0xa3bd, 0xa3be, 0xa3bf, 0xa3bb };
  24. // the index (position) of bo, po, mo, and fo.
  25. // only 0 to 3 is a valid value
  26. const char cIndexTable[] = {
  27. // ' ' ! " # $ % & '
  28. 3, -1, -1, -1, -1, -1, -1, -1,
  29. // ( ) * + , - . /
  30. -1, -1, -1, -1, 2, 2, 2, 2,
  31. // 0 1 2 3 4 5 6 7
  32. 2, 0, 0, 3, 3, 0, 3, 3,
  33. // 8 9 : ; < = > ?
  34. 2, 2, -1, 2, -1, -1, -1, -1,
  35. // @ A B C D E F G
  36. -1, 0, 0, 0, 0, 0, 0, 0,
  37. // H I J K L M N O
  38. 0, 2, 1, 2, 2, 1, 0, 2,
  39. // P Q R S T U V W
  40. 2, 0, 0, 0, 0, 1, 0, 0,
  41. // X Y Z [ \ ] ^ _ `
  42. 0, 0, 0, -1, -1, -1, -1, -1, -1
  43. };
  44. void _cdecl main( int argc, TCHAR **argv) {
  45. HANDLE hInFile, hOutFile;
  46. HANDLE hInMap;
  47. LPBYTE lpInFile;
  48. WORD OutLine[20];
  49. DWORD dwInFileSize, i, NumberOfBytesWritten;
  50. DWORD iLine;
  51. if ( argc != 3 ) {
  52. printf("Usage: convphon File1 File2\n");
  53. return;
  54. }
  55. hInFile = CreateFile( argv[1], // pointer to name of the file
  56. GENERIC_READ, // access (read-write) mode
  57. FILE_SHARE_READ, // share mode
  58. NULL, // pointer to security attributes
  59. OPEN_EXISTING, // how to create
  60. FILE_ATTRIBUTE_NORMAL, // file attributes
  61. NULL);
  62. if ( hInFile == INVALID_HANDLE_VALUE ) return;
  63. dwInFileSize = GetFileSize(hInFile, NULL);
  64. hOutFile=CreateFile( argv[2], // pointer to name of the file
  65. GENERIC_WRITE, // access (read-write) mode
  66. FILE_SHARE_WRITE, // share mode
  67. NULL, // pointer to security attributes
  68. CREATE_ALWAYS, // how to create
  69. FILE_ATTRIBUTE_NORMAL, // file attributes
  70. NULL);
  71. if ( hOutFile == INVALID_HANDLE_VALUE ) {
  72. printf("hOutFile is INVALID_HANDLE_VALUE\n");
  73. return;
  74. }
  75. hInMap = CreateFileMapping(hInFile, // handle to file to map
  76. NULL, // optional security attributes
  77. PAGE_READONLY, // protection for mapping object
  78. 0, // high-order 32 bits of object size
  79. 0, // low-order 32 bits of object size
  80. NULL); // name of file-mapping object);
  81. if ( !hInMap ) {
  82. printf("hInMap is NULL\n");
  83. return;
  84. }
  85. lpInFile = (LPBYTE) MapViewOfFile(hInMap, FILE_MAP_READ, 0, 0, 0);
  86. OutLine[0] = 0xFEFF;
  87. WriteFile(hOutFile, // handle to file to write to
  88. OutLine, // pointer to data to write to file
  89. 2, // number of bytes to write
  90. &NumberOfBytesWritten, // pointer to number of bytes written
  91. NULL); // pointer to structure needed for
  92. // overlapped I/O
  93. i = 0;
  94. iLine = 1;
  95. while ( i < dwInFileSize ) {
  96. WORD CharCode;
  97. BOOL bFirstCode;
  98. WCHAR LastKey;
  99. DWORD iStart, iTmp;
  100. CharCode = 0;
  101. bFirstCode = TRUE;
  102. if ( i >= dwInFileSize) break;
  103. for (iStart=0; iStart<4; iStart++) {
  104. BYTE ThisByte;
  105. ThisByte = lpInFile[i+iStart];
  106. if ( (ThisByte >= '0') && (ThisByte <= '9') )
  107. ThisByte = ThisByte - '0';
  108. else
  109. if ( (ThisByte >= 'A') && (ThisByte <= 'F') )
  110. ThisByte = ThisByte - 'A' + 10;
  111. else
  112. if ( (ThisByte >= 'a') && (ThisByte <= 'f') )
  113. ThisByte = ThisByte - 'a' + 10;
  114. else
  115. {
  116. printf("Line Num %d data error ThisByte is %c\n", iLine, ThisByte);
  117. return;
  118. }
  119. CharCode = CharCode * 16 + ThisByte;
  120. }
  121. i += 4;
  122. while ( lpInFile[i] == 0x20 ) i++;
  123. iStart = 0;
  124. while ((lpInFile[i]!=0x0D) && (i<dwInFileSize)) {
  125. WORD dbcsCode;
  126. if (lpInFile[i] != 0x20){//must be serial DBCS which are Phonetic
  127. if ( i+1 >= dwInFileSize ) {
  128. printf(" No Tail Byte, Error!\n");
  129. return;
  130. }
  131. dbcsCode = lpInFile[i] * 256 + lpInFile[i+1];
  132. i += 2;
  133. // try to get the Key for this Code, First search Seq2DBCS table to
  134. // get its sequent code, and then get this sequent code's Key.
  135. for (iTmp=0; iTmp<43; iTmp++ ) {
  136. if ( Seq2DBCS[iTmp] == dbcsCode ) {
  137. OutLine[iStart] = Seq2Key[iTmp];
  138. iStart ++;
  139. break;
  140. }
  141. }
  142. }
  143. else // it is 0x20
  144. {
  145. while ( lpInFile[i] == 0x20 ) i++;
  146. LastKey = OutLine[iStart-1];
  147. if ( (LastKey != L' ') && // First Tone
  148. (LastKey != L'3') && // Third Tone
  149. (LastKey != L'4') && // Forth Tone
  150. (LastKey != L'6') && // Second Tone
  151. (LastKey != L'7') ) { // Fifth Tone
  152. OutLine[iStart] = L' '; // assume it is first tone
  153. iStart ++;
  154. }
  155. if ( iStart < 4 ) {
  156. WCHAR tmpBuf[4];
  157. int cIndex;
  158. for ( iTmp=0; iTmp<4; iTmp++ )
  159. tmpBuf[iTmp] = 0x0020;
  160. for (iTmp=0; iTmp < iStart; iTmp++) {
  161. cIndex = cIndexTable[ (OutLine[iTmp] - L' ') ];
  162. tmpBuf[cIndex] = OutLine[iTmp];
  163. }
  164. for (iTmp=0; iTmp < 4; iTmp++)
  165. {
  166. OutLine[iTmp] = tmpBuf[iTmp];
  167. }
  168. }
  169. iStart = 4;
  170. OutLine[iStart++] = 0x0009; // Add Tab
  171. OutLine[iStart++] = CharCode; // Add char code
  172. if ( bFirstCode == TRUE ) {
  173. OutLine[iStart++] = L' ';
  174. bFirstCode = FALSE;
  175. }
  176. else
  177. OutLine[iStart++] = L'*';
  178. OutLine[iStart++] = 0x000D;
  179. OutLine[iStart++] = 0x000A;
  180. WriteFile(hOutFile, // handle to file to write to
  181. OutLine, // pointer to data to write to file
  182. iStart * sizeof(WORD), // number of bytes to write
  183. &NumberOfBytesWritten, // number of bytes written
  184. NULL); // pointer to structure needed for
  185. // overlapped I/O
  186. iStart = 0;
  187. }
  188. } // lpInFile[i] != 0x0D
  189. if ( lpInFile[i] == 0x0D ) {
  190. iLine ++;
  191. if ( lpInFile[i+1] == 0x0A )
  192. i = i + 2;
  193. else
  194. i++;
  195. }
  196. if ( iStart > 0 ) {
  197. LastKey = OutLine[iStart-1];
  198. if ( (LastKey != L' ') && // First Tone
  199. (LastKey != L'3') && // Third Tone
  200. (LastKey != L'4') && // Forth Tone
  201. (LastKey != L'6') && // Second Tone
  202. (LastKey != L'7') ) { // Fifth Tone
  203. OutLine[iStart] = L' '; // assume it is first tone
  204. iStart ++;
  205. }
  206. if ( iStart < 4 ) {
  207. WCHAR tmpBuf[4];
  208. int cIndex;
  209. for ( iTmp=0; iTmp<4; iTmp++ )
  210. tmpBuf[iTmp] = 0x0020;
  211. for (iTmp=0; iTmp < iStart; iTmp++) {
  212. cIndex = cIndexTable[ (OutLine[iTmp] - L' ') ];
  213. tmpBuf[cIndex] = OutLine[iTmp];
  214. }
  215. for (iTmp=0; iTmp < 4; iTmp++)
  216. {
  217. OutLine[iTmp] = tmpBuf[iTmp];
  218. }
  219. }
  220. iStart = 4;
  221. OutLine[iStart++] = 0x0009; // Add Tab
  222. OutLine[iStart++] = CharCode; // Add char code
  223. if ( bFirstCode == TRUE )
  224. OutLine[iStart++] = L' ';
  225. else
  226. OutLine[iStart++] = L'*';
  227. OutLine[iStart++] = 0x000D;
  228. OutLine[iStart++] = 0x000A;
  229. WriteFile(hOutFile, // handle to file to write to
  230. OutLine, // pointer to data to write to file
  231. iStart * sizeof(WORD), // number of bytes to write
  232. &NumberOfBytesWritten, // number of bytes written
  233. NULL); // pointer to structure needed for
  234. // overlapped I/O
  235. iStart = 0;
  236. }
  237. } // while i<dwInFileSize
  238. UnmapViewOfFile(lpInFile);
  239. CloseHandle(hInMap);
  240. CloseHandle(hInFile);
  241. CloseHandle(hOutFile);
  242. return;
  243. }