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.

350 lines
12 KiB

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