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.

353 lines
13 KiB

  1. /*************************************************
  2. * gennpsrc.c *
  3. * *
  4. * Copyright (C) 1995-1999 Microsoft Inc. *
  5. * *
  6. *************************************************/
  7. //
  8. // This file is used to Generate a new source Phone Table File.
  9. //
  10. // it will read two files, one for Big5 and one for GB, to generate a new
  11. // Phon code table file.
  12. //
  13. // the two input files are sorted on the external keystroke pattern,
  14. // and both are complete Unicode files, ( there is 0xFEFF
  15. // in its first two bytes),
  16. //
  17. // the two input files contain lots of lines,every line follows below format:
  18. // XXXXTCFRL
  19. // X: Key Code,
  20. // T: Tab, 0x0009
  21. // C: Unicode for this Character
  22. // F: L' ' or L'*'
  23. // R: 0x000D
  24. // L: 0x000A
  25. //
  26. // we will generate a new table source file, if the same pattern exists in both
  27. // Big5 file and GB file, all those lines will be appended to the new file, and// the lines of Big5 will be written first, then GB Lines
  28. //
  29. // the new generated file must be sorted on the pattern.
  30. //
  31. // Created by weibz, March 02, 1998
  32. //
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <windows.h>
  36. #define LINELEN (9 * sizeof(WORD) )
  37. WCHAR Seq2Key[43] = { 0,
  38. L'1', L'Q', L'A', L'Z', L'2', L'W', L'S', L'X',
  39. L'E', L'D', L'C', L'R', L'F', L'V', L'5', L'T',
  40. L'G', L'B', L'Y', L'H', L'N', L'U', L'J', L'M',
  41. L'8', L'I', L'K', L',', L'9', L'O', L'L', L'.',
  42. L'0', L'P', L';', L'/', L'-', L' ', L'6', L'3',
  43. L'4', L'7' };
  44. DWORD GetPattern( WORD *pWord )
  45. {
  46. int i, j;
  47. WORD wValue;
  48. DWORD dwPat, dwSeq;
  49. dwPat = 0;
  50. for (i=0; i<3; i++) {
  51. wValue = *(pWord+i);
  52. dwSeq = 0;
  53. if ( wValue != L' ' ) {
  54. for (j=0; j<43; j++) {
  55. if ( wValue == Seq2Key[j] ) {
  56. dwSeq = j;
  57. break;
  58. }
  59. }
  60. }
  61. dwPat = (dwPat << 6) + dwSeq;
  62. }
  63. // handle the last character, it should be one of five tones.
  64. wValue = *(pWord+3);
  65. dwSeq = 0;
  66. for (j=38; j<43; j++) {
  67. if ( wValue == Seq2Key[j] ) {
  68. dwSeq = j;
  69. break;
  70. }
  71. }
  72. dwPat = (dwPat << 6) + dwSeq;
  73. return dwPat;
  74. }
  75. void _cdecl main( int argc, TCHAR **argv) {
  76. HANDLE hInBig5File, hInGBFile, hOutSrcFile;
  77. HANDLE hInBig5Map, hInGBMap;
  78. LPWORD lpInBig5File, lpInGBFile, lpStartBig5, lpStartGB;
  79. DWORD dwBig5Line, dwGBLine;
  80. DWORD iBig5Line, iGBLine, i;
  81. DWORD dwInFileSize, BytesWritten;
  82. WORD wOutData;
  83. DWORD dwPatternBig5, dwPatternGB;
  84. if ( argc != 4 ) {
  85. printf("Usage: gennpsrc <Big5> <GB File> <New UnicdFile> \n");
  86. return;
  87. }
  88. hInBig5File = CreateFile( argv[1], // pointer to name of the file
  89. GENERIC_READ, // access (read-write) mode
  90. FILE_SHARE_READ, // share mode
  91. NULL, // pointer to security attributes
  92. OPEN_EXISTING, // how to create
  93. FILE_ATTRIBUTE_NORMAL, // file attributes
  94. NULL);
  95. if ( hInBig5File == INVALID_HANDLE_VALUE ) return;
  96. hInGBFile = CreateFile( argv[2], // pointer to name of the file
  97. GENERIC_READ, // access (read-write) mode
  98. FILE_SHARE_READ, // share mode
  99. NULL, // pointer to security attributes
  100. OPEN_EXISTING, // how to create
  101. FILE_ATTRIBUTE_NORMAL, // file attributes
  102. NULL);
  103. if ( hInGBFile == INVALID_HANDLE_VALUE ) {
  104. printf("hInGBFile is INVALID_HANDLE_VALUE\n");
  105. return;
  106. }
  107. hOutSrcFile = CreateFile(argv[3], // pointer to name of the file
  108. GENERIC_WRITE, // access (read-write) mode
  109. FILE_SHARE_WRITE, // share mode
  110. NULL, // pointer to security attributes
  111. CREATE_ALWAYS, // how to create
  112. FILE_ATTRIBUTE_NORMAL, // file attributes
  113. NULL);
  114. if ( hOutSrcFile == INVALID_HANDLE_VALUE ) {
  115. printf("hOutSrcFile is INVALID_HANDLE_VALUE\n");
  116. return;
  117. }
  118. hInBig5Map = CreateFileMapping(hInBig5File, // handle to file to map
  119. NULL, // optional security attributes
  120. PAGE_READONLY, // protection for mapping object
  121. 0, // high-order 32 bits of object size
  122. 0, // low-order 32 bits of object size
  123. NULL); // name of file-mapping object);
  124. if ( !hInBig5Map ) {
  125. printf("hInBig5Map is NULL\n");
  126. return;
  127. }
  128. hInGBMap = CreateFileMapping(hInGBFile, // handle to file to map
  129. NULL, // optional security attributes
  130. PAGE_READONLY, // protection for mapping object
  131. 0, // high-order 32 bits of object size
  132. 0, // low-order 32 bits of object size
  133. NULL); // name of file-mapping object);
  134. if ( !hInGBMap ) {
  135. printf("hInGBMap is NULL\n");
  136. return;
  137. }
  138. lpInBig5File = (LPWORD)MapViewOfFile(hInBig5Map, FILE_MAP_READ, 0, 0, 0);
  139. lpInGBFile = (LPWORD)MapViewOfFile(hInGBMap, FILE_MAP_READ, 0, 0, 0);
  140. lpStartBig5 = lpInBig5File + 1; // skip Unicode header signature 0xFEFF
  141. lpStartGB = lpInGBFile + 1; // skip Unicode header signature 0xFEFF
  142. dwInFileSize = GetFileSize(hInBig5File, NULL) - 2; // sub head two bytes
  143. dwBig5Line = dwInFileSize / LINELEN;
  144. dwInFileSize = GetFileSize(hInGBFile, NULL) - 2;
  145. dwGBLine = dwInFileSize / LINELEN;
  146. wOutData = 0xFEFF;
  147. WriteFile(hOutSrcFile, // handle to file to write to
  148. &wOutData, // pointer to data to write to file
  149. 2, // number of bytes to write
  150. &BytesWritten, // pointer to number of bytes written
  151. NULL); // pointer to structure needed for
  152. // overlapped I/O
  153. iBig5Line=iGBLine=0;
  154. while ((iBig5Line < dwBig5Line) && (iGBLine < dwGBLine)) {
  155. dwPatternBig5 = GetPattern(lpStartBig5);
  156. dwPatternGB = GetPattern(lpStartGB);
  157. if (dwPatternBig5 < dwPatternGB ) {
  158. // in this case, we just keep all lines in Big5 File which have same
  159. // dwpattern to new generated file.
  160. // write lpStartBig5 to OutSrcFile
  161. WriteFile(hOutSrcFile, // handle to file to write to
  162. lpStartBig5, // pointer to data to write to file
  163. LINELEN, // number of bytes to write
  164. &BytesWritten, // pointer to number of bytes written
  165. NULL); // pointer to structure needed for
  166. // overlapped I/O
  167. lpStartBig5 += LINELEN/sizeof(WORD);
  168. iBig5Line ++;
  169. while ( (iBig5Line < dwBig5Line) &&
  170. (GetPattern(lpStartBig5) == dwPatternBig5) ) {
  171. WriteFile(hOutSrcFile, // handle to file to write to
  172. lpStartBig5, // pointer to data to write to file
  173. LINELEN, // number of bytes to write
  174. &BytesWritten, // pointer to number of bytes written
  175. NULL); // pointer to structure needed for
  176. // overlapped I/O
  177. lpStartBig5 += LINELEN/sizeof(WORD);
  178. iBig5Line ++;
  179. }
  180. }
  181. else if ( dwPatternBig5 == dwPatternGB ) {
  182. // in this case, we will put all the lines in BIG5 and then in GB with
  183. // the same dwpattern to the new generated file.
  184. WriteFile(hOutSrcFile, // handle to file to write to
  185. lpStartBig5, // pointer to data to write to file
  186. LINELEN, // number of bytes to write
  187. &BytesWritten, // pointer to number of bytes written
  188. NULL); // pointer to structure needed for
  189. // overlapped I/O
  190. lpStartBig5 += LINELEN/sizeof(WORD);
  191. iBig5Line ++;
  192. while ( (iBig5Line < dwBig5Line) &&
  193. (GetPattern(lpStartBig5) == dwPatternBig5) ) {
  194. WriteFile(hOutSrcFile, // handle to file to write to
  195. lpStartBig5, // pointer to data to write to file
  196. LINELEN, // number of bytes to write
  197. &BytesWritten, // pointer to number of bytes written
  198. NULL); // pointer to structure needed for
  199. // overlapped I/O
  200. lpStartBig5 += LINELEN/sizeof(WORD);
  201. iBig5Line ++;
  202. }
  203. WriteFile(hOutSrcFile, // handle to file to write to
  204. lpStartGB, // pointer to data to write to file
  205. LINELEN, // number of bytes to write
  206. &BytesWritten, // pointer to number of bytes written
  207. NULL); // pointer to structure needed for
  208. // overlapped I/O
  209. lpStartGB += LINELEN/sizeof(WORD);
  210. iGBLine ++;
  211. while ( (iGBLine < dwGBLine) &&
  212. (GetPattern(lpStartGB) == dwPatternGB) ) {
  213. WriteFile(hOutSrcFile, // handle to file to write to
  214. lpStartGB, // pointer to data to write to file
  215. LINELEN, // number of bytes to write
  216. &BytesWritten, // pointer to number of bytes written
  217. NULL); // pointer to structure needed for
  218. // overlapped I/O
  219. lpStartGB += LINELEN/sizeof(WORD);
  220. iGBLine ++;
  221. }
  222. } else {
  223. // in this case, we just put all the lines with same pattern in file
  224. // GB to the new generated file.
  225. WriteFile(hOutSrcFile, // handle to file to write to
  226. lpStartGB, // pointer to data to write to file
  227. LINELEN, // number of bytes to write
  228. &BytesWritten, // pointer to number of bytes written
  229. NULL); // pointer to structure needed for
  230. // overlapped I/O
  231. lpStartGB += LINELEN/sizeof(WORD);
  232. iGBLine ++;
  233. while ( (iGBLine < dwGBLine) &&
  234. (GetPattern(lpStartGB) == dwPatternGB) ) {
  235. WriteFile(hOutSrcFile, // handle to file to write to
  236. lpStartGB, // pointer to data to write to file
  237. LINELEN, // number of bytes to write
  238. &BytesWritten, // pointer to number of bytes written
  239. NULL); // pointer to structure needed for
  240. // overlapped I/O
  241. lpStartGB += LINELEN/sizeof(WORD);
  242. iGBLine ++;
  243. }
  244. }
  245. } // while ...
  246. if ( iBig5Line < dwBig5Line ) {
  247. while ( iBig5Line < dwBig5Line ) {
  248. WriteFile(hOutSrcFile, // handle to file to write to
  249. lpStartBig5, // pointer to data to write to file
  250. LINELEN, // number of bytes to write
  251. &BytesWritten, // pointer to number of bytes written
  252. NULL); // pointer to structure needed for
  253. // overlapped I/O
  254. lpStartBig5 += LINELEN/sizeof(WORD);
  255. iBig5Line ++;
  256. }
  257. }
  258. if ( iGBLine < dwGBLine ) {
  259. while ( iGBLine < dwGBLine ) {
  260. WriteFile(hOutSrcFile, // handle to file to write to
  261. lpStartGB, // pointer to data to write to file
  262. LINELEN, // number of bytes to write
  263. &BytesWritten, // pointer to number of bytes written
  264. NULL); // pointer to structure needed for
  265. // overlapped I/O
  266. lpStartGB += LINELEN/sizeof(WORD);
  267. iGBLine ++;
  268. }
  269. }
  270. UnmapViewOfFile(lpInBig5File);
  271. UnmapViewOfFile(lpInGBFile);
  272. CloseHandle(hInBig5Map);
  273. CloseHandle(hInGBMap);
  274. CloseHandle(hInBig5File);
  275. CloseHandle(hInGBFile);
  276. CloseHandle(hOutSrcFile);
  277. return;
  278. }