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.

1514 lines
44 KiB

  1. /*************************************************
  2. * conv.c *
  3. * *
  4. * Copyright (C) 1995-1999 Microsoft Inc. *
  5. * *
  6. *************************************************/
  7. #include "conv.h"
  8. DWORD dwBaseWordNum;
  9. HGLOBAL hCreateWord;
  10. HGLOBAL hWordIndex;
  11. HGLOBAL hEncode;
  12. #ifdef UNICODE
  13. TCHAR Title[] = {0x6D4F, 0x89C8, 0x0000};
  14. TCHAR szVer[] = {0x8F93, 0x5165, 0x6CD5, 0x0020, 0x7248, 0x672C, 0x0000};
  15. #else
  16. BYTE Title[] = "����";
  17. BYTE szVer[] = "���뷨 �汾";
  18. #endif
  19. /*****************************************************************************
  20. FUNCTION: ConvConv(HANDLE hWnd,LPCSTR lpSrcFileName, LPCSTR lpszMBFileName)
  21. PURPOSE: Processes MB conversion .
  22. PARAMETERS:
  23. hWnd - window handle of the parent window.
  24. lpSrcFileName - pointer to source text file name.
  25. lpszMBFileName - pointer to destinate .mb file name.
  26. RETURN VALUE:
  27. TRUE - conversion completed
  28. FALSE - conversion not completed
  29. HISTORY:
  30. ****************************************************************************/
  31. BOOL ConvConv(HANDLE hWnd,LPCTSTR lpSrcFileName, LPCTSTR lpszMBFileName)
  32. {
  33. int nTemp;
  34. DWORD dwDBCS;
  35. DWORD dwRuleOffset, dwRuleLen;
  36. DWORD dwDesOffset, dwDesLen;
  37. DWORD i;
  38. int nPages;
  39. TCHAR szDBCS[512];
  40. //dwRuleOffset: Rule segment's offset in source file
  41. MAINID MainID;
  42. LPMAININDEX lpMainIndex;
  43. LPCREATEWORD lpCreateWords;
  44. DESCRIPTION Descript;
  45. LPWORDINDEX lpWordIndex;
  46. LPRULE lpRule;
  47. HANDLE hReConvIndex, hMainIndex;
  48. LPRECONVINDEX lpReConvIndex;
  49. LPENCODEAREA lpEncode;
  50. DispInfo(hWnd,IDS_CONVERTINFO);
  51. nTemp = (int)ConvGetDescript(hWnd,lpSrcFileName,&dwDesOffset,
  52. &dwDesLen,&Descript,FALSE);
  53. if(nTemp == -ERR_FILENOTOPEN || nTemp < -4 )
  54. return FALSE;
  55. hMainIndex = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,sizeof(MAININDEX)*NUMTABLES);
  56. if(!(lpMainIndex = GlobalLock(hMainIndex) ) ) {
  57. ProcessError(ERR_GLOBALLOCK,hWnd,ERR);
  58. return FALSE;
  59. }
  60. ConvInit(&Descript,&MainID,lpMainIndex);
  61. nPages = 1;
  62. dwBaseWordNum = 0;
  63. nTemp = sizeof(WORDINDEX)*GMEM_PAGESIZE;
  64. hWordIndex=GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT, nTemp);
  65. hCreateWord = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,
  66. (DWORD)Descript.wMaxCodes*NUM_OF_ENCODE*sizeof(TCHAR));
  67. if(!hMainIndex||!hCreateWord || !hWordIndex || !hRule) {
  68. ProcessError(ERR_OUTOFMEMORY,hWnd,ERR);
  69. goto err;
  70. }
  71. if(!(lpCreateWords = GlobalLock(hCreateWord) )
  72. ||!(lpWordIndex = (LPWORDINDEX) GlobalLock(hWordIndex))
  73. ||!(lpRule = GlobalLock(hRule)) ) {
  74. ProcessError(ERR_GLOBALLOCK,hWnd,ERR);
  75. goto err;
  76. }
  77. if(ConvGetRule(hWnd,lpSrcFileName,&dwRuleOffset,&dwRuleLen,
  78. lpRule,&Descript)<=0)
  79. goto err;
  80. hSRCFile = Create_File(hWnd,lpSrcFileName,GENERIC_READ,OPEN_EXISTING);
  81. if (hSRCFile == (HANDLE)-1)
  82. return FALSE;
  83. hMBFile = Create_File(hWnd,lpszMBFileName,GENERIC_READ|GENERIC_WRITE,
  84. CREATE_ALWAYS);
  85. if (hMBFile == (HANDLE)-1) {
  86. CloseHandle(hSRCFile);
  87. return FALSE;
  88. }
  89. if(!ConvWriteMainID(hMBFile,&MainID)) {
  90. ProcessError(ERR_WRITEID,hWnd,ERR);
  91. goto err;
  92. }
  93. if(!ConvWriteMainIndex(hMBFile,lpMainIndex)) {
  94. ProcessError(ERR_WRITEMAININDEX,hWnd,ERR);
  95. goto err;
  96. }
  97. if(!ConvWriteDescript(hMBFile, &Descript, lpMainIndex)) {
  98. ProcessError(ERR_WRITEDESCRIPT,hWnd,ERR);
  99. goto err;
  100. }
  101. if(lpMainIndex[TAG_RULE-1].dwLength != 0)
  102. if(!ConvWriteRule( hMBFile,Descript.wNumRules,lpRule, lpMainIndex)) {
  103. ProcessError(ERR_WRITERULE,hWnd,ERR);
  104. goto err;
  105. }
  106. hReConvIndex = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,
  107. (DWORD)Descript.wMaxCodes*NUM_OF_ENCODE*sizeof(TCHAR));
  108. if(!hReConvIndex) {
  109. ProcessError(ERR_OUTOFMEMORY,hWnd,ERR);
  110. goto err;
  111. }
  112. if(!(lpReConvIndex = GlobalLock(hReConvIndex))
  113. || !(lpEncode = GlobalLock(hEncode)) ) {
  114. ProcessError(ERR_GLOBALLOCK,hWnd,ERR);
  115. goto err;
  116. }
  117. DispInfo(hWnd,IDS_READTEXTINFO);
  118. if(!ReadSRCFile(hWnd,&nPages,hSRCFile,lpCreateWords,
  119. lpWordIndex,&dwDBCS,&Descript,lpReConvIndex)) {
  120. goto err;
  121. }
  122. DispInfo(hWnd,IDS_WRCRTINFO);
  123. //****** fill code mapping *****
  124. ConvWriteEncode(hMBFile,lpEncode,lpMainIndex);
  125. if(Descript.wNumRules != 0) {
  126. LoadString(NULL,IDS_ISCHECKCRTWORD,szDBCS, sizeof(szDBCS)/sizeof(TCHAR));
  127. if(ErrMessage(hWnd, szDBCS))
  128. {
  129. if(!CheckCrtData(hWnd,lpCreateWords,lpEncode,Descript.wMaxCodes) ) {
  130. ProcessError(ERR_GB2312NOTENTIRE,hWnd,ERR);
  131. goto err;
  132. }
  133. }
  134. } //******* Debug *******
  135. if(lpMainIndex[TAG_CRTWORDCODE-1].dwLength != 0)
  136. ConvWriteCrtData(hMBFile,lpCreateWords,lpMainIndex);
  137. if(lpMainIndex[TAG_RECONVINDEX-1].dwLength != 0)
  138. ConvWriteReConvIdx(hMBFile,lpReConvIndex,lpMainIndex);
  139. GlobalUnlock(hEncode);
  140. //****95.10.12 auto add code for phrases not exist code
  141. SetCursor (LoadCursor (NULL, IDC_WAIT));
  142. if( Descript.wNumRules != 0)
  143. {
  144. for(i=0; i<dwBaseWordNum; i++)
  145. {
  146. if(lpWordIndex[i].wDBCSLen > 1
  147. && (lstrlen(lpWordIndex[i].szCode) == 0)
  148. ) {
  149. SetFilePointer(hSRCFile,lpWordIndex[i].dwOffset,0,FILE_BEGIN);
  150. nTemp = 2*(DWORD)lpWordIndex[i].wDBCSLen;
  151. ReadFile(hSRCFile,szDBCS,nTemp,&dwDBCS,NULL);
  152. szDBCS[nTemp] = 0;
  153. ConvBatCreate(hWnd,Descript,lpRule,
  154. lpCreateWords,szDBCS,
  155. lpWordIndex[i].szCode);
  156. if(lpWordIndex[i].szCode[0] == TEXT(',') && lpWordIndex[i].szCode[1] == TEXT('z'))
  157. continue;
  158. if(lstrlen(lpWordIndex[i].szCode) == 0)
  159. {
  160. goto err;
  161. }
  162. }
  163. }
  164. }
  165. SetCursor (LoadCursor (NULL, IDC_ARROW));
  166. DispInfo(hWnd,IDS_SORT);
  167. ConvSort(hWnd,lpWordIndex,dwBaseWordNum);
  168. SetFilePointer(hMBFile,lpMainIndex[TAG_BASEDICINDEX-1].dwOffset,0,FILE_BEGIN);
  169. //***** fill index area with null *****
  170. nTemp = Descript.wNumCodes;
  171. nTemp = sizeof(DWORD)*(nTemp+1)*nTemp;
  172. WriteFile(hMBFile,NULL,nTemp,&nTemp,NULL);
  173. //***** fill index area and length area *****
  174. ConvCalc(hWnd,hMBFile,lpWordIndex,lpMainIndex,&Descript);
  175. //***** fill dictionary area *****
  176. WriteMBFile(hWnd,hSRCFile,hMBFile,lpWordIndex,lpMainIndex,&Descript);
  177. MainID.dwNumWords = dwBaseWordNum;
  178. //***** fill changed MainID *****
  179. ConvWriteMainID(hMBFile,&MainID);
  180. GlobalUnlock(hRule);
  181. if(hCreateWord)
  182. GlobalFree(hCreateWord);
  183. GlobalUnlock(hReConvIndex);
  184. GlobalFree(hReConvIndex);
  185. GlobalUnlock(hWordIndex);
  186. GlobalFree(hWordIndex);
  187. CloseHandle(hMBFile);
  188. CloseHandle(hSRCFile);
  189. ProcessError(ERR_CONVEND, hWnd, INFO);
  190. return TRUE;
  191. err:
  192. if(hSRCFile)
  193. CloseHandle(hSRCFile);
  194. if(hMBFile)
  195. CloseHandle(hMBFile);
  196. if(hMainIndex)
  197. GlobalFree(hMainIndex);
  198. if(hWordIndex)
  199. GlobalFree(hWordIndex);
  200. if(hCreateWord)
  201. GlobalFree(hCreateWord);
  202. if(hReConvIndex)
  203. GlobalFree(hReConvIndex);
  204. if(hRule)
  205. GlobalUnlock(hRule);
  206. return FALSE;
  207. }
  208. /*****************************************************************************
  209. FUNCTION: ConvReadFile(HANDLE hWnd,LPCSTR lpSrcFileName, LPCSTR lpDestFile)
  210. PURPOSE: Processes source text file sorting.
  211. PARAMETERS:
  212. hWnd - window handle of the parent window.
  213. lpSrcFileName - pointer to source text file name.
  214. lpDestFile - pointer to destinate sorted text file name.
  215. RETURN VALUE:
  216. TRUE - sorting completed
  217. FALSE - sorting not completed
  218. HISTORY:
  219. ****************************************************************************/
  220. BOOL ConvReadFile(HANDLE hWnd,
  221. LPCTSTR lpSrcFileName,
  222. LPCTSTR lpDestFile)
  223. {
  224. DWORD i,j;
  225. DWORD dwRuleOffset, dwRuleLen;
  226. DWORD dwDesOffset, dwDesLen;
  227. DWORD dwReadBytes;
  228. DWORD dwFstOffset=0;
  229. DWORD dwOffset=0;
  230. TCHAR szStr[512],szDBCS[512];
  231. TCHAR Buffer[MAXREADBUFFER];
  232. BOOL bReadText = FALSE, bSameFile=FALSE;
  233. int nRet;
  234. int nPages;
  235. BOOL IsGenCmb = FALSE;
  236. HANDLE hRule0;
  237. LPCREATEWORD lpCreateWords;
  238. LPWORDINDEX lpWordIndex;
  239. LPRULE lpRule;
  240. DESCRIPTION Descript;
  241. HANDLE hDlgItem;
  242. nRet = (int)ConvGetDescript(hWnd,lpSrcFileName,&dwDesOffset,
  243. &dwDesLen,&Descript,FALSE);
  244. if(nRet == -ERR_FILENOTOPEN)
  245. return FALSE;
  246. if((-nRet)&7)
  247. bReadText = TRUE;
  248. if(Descript.wMaxCodes <= 0)
  249. Descript.wMaxCodes = MAXCODELEN;
  250. nPages = 1;
  251. dwBaseWordNum = 0;
  252. dwReadBytes = sizeof(WORDINDEX)*GMEM_PAGESIZE;
  253. hCreateWord = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,
  254. MAXCODELEN*NUM_OF_ENCODE*sizeof(TCHAR));
  255. hWordIndex=GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT, dwReadBytes);
  256. if(!hWordIndex ) {
  257. ProcessError(ERR_OUTOFMEMORY,hWnd,ERR);
  258. goto err;
  259. }
  260. if(!hCreateWord ) {
  261. ProcessError(ERR_OUTOFMEMORY,hWnd,ERR);
  262. goto err;
  263. }
  264. if(!(lpCreateWords = GlobalLock(hCreateWord) )
  265. ||!(lpWordIndex = (LPWORDINDEX) GlobalLock(hWordIndex))
  266. ) {
  267. ProcessError(ERR_GLOBALLOCK,hWnd,ERR);
  268. goto err;
  269. }
  270. hRule0= GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,
  271. sizeof(RULE)*MAXCODELEN);
  272. if(!(lpRule = GlobalLock(hRule0)) ) {
  273. ProcessError(ERR_GLOBALLOCK,hWnd,ERR);
  274. goto err;
  275. }
  276. if(ConvGetRule(hWnd,lpSrcFileName,&dwRuleOffset,&dwRuleLen,
  277. lpRule,&Descript) == -ERR_FILENOTOPEN)
  278. goto err;
  279. hSRCFile = Create_File(hWnd,lpSrcFileName,GENERIC_READ,OPEN_EXISTING);
  280. if (hSRCFile == (HANDLE)-1)
  281. goto err;
  282. if(lstrcmpi(lpDestFile,lpSrcFileName)==0) {
  283. bSameFile = TRUE;
  284. LoadString(NULL,IDS_OVERWRITE,szStr, sizeof(szStr)/sizeof(TCHAR));
  285. wsprintf(szDBCS,TEXT("\'%s\'\n%s"),lpSrcFileName,szStr);
  286. if(!ErrMessage(hWnd, szDBCS))
  287. {
  288. CloseHandle(hSRCFile);
  289. goto err;
  290. }
  291. #ifdef UNICODE
  292. if(!_waccess(TEXT(TempFile),0))
  293. #else
  294. if(!_access(TEXT(TempFile),0))
  295. #endif
  296. DeleteFile(TEXT(TempFile));
  297. hMBFile = Create_File(hWnd,TEXT(TempFile),GENERIC_WRITE|GENERIC_READ,CREATE_ALWAYS);
  298. }
  299. else
  300. hMBFile = Create_File(hWnd,lpDestFile,GENERIC_WRITE|GENERIC_READ,CREATE_ALWAYS);
  301. if(hMBFile==(HANDLE)-1) {
  302. CloseHandle(hSRCFile);
  303. goto err;
  304. }
  305. CloseHandle(hMBFile);
  306. DispInfo(hWnd,IDS_READTEXTINFO);
  307. SetFilePointer(hSRCFile,0,0,FILE_BEGIN);
  308. szStr[0] = 0;
  309. j = 0;
  310. SetCursor (LoadCursor (NULL, IDC_WAIT));
  311. SetDlgItemText (hWnd,TM_TOTALINFO,TEXT(""));
  312. LoadString(NULL,IDS_SORTWORDS,szStr,sizeof(szStr)/sizeof(TCHAR));
  313. SetDlgItemText (hWnd,TM_TOTAL,szStr);
  314. SetDlgItemInt (hWnd,TM_TOTALNUM,dwBaseWordNum,FALSE);
  315. hDlgItem=GetDlgItem(hWnd,TM_CONVNUM);
  316. InvalidateRect (hWnd,NULL,FALSE);
  317. while(ReadFile(hSRCFile,Buffer,MAXREADBUFFER,&dwReadBytes,NULL))
  318. {
  319. dwReadBytes = dwReadBytes/sizeof(TCHAR);
  320. for(i=0;i<(int)dwReadBytes;i++) {
  321. if(Buffer[i] == 0x0d || Buffer[i] == 0xfeff)
  322. continue;
  323. if(Buffer[i] == TEXT('\n'))
  324. {
  325. szStr[j]=0;
  326. j=0;
  327. if(lstrcmpi(szStr,TEXT(TextSeg))==0) {
  328. bReadText = TRUE;
  329. // lstrcpy(szStr,"");
  330. continue;
  331. }
  332. if(lstrcmpi(szStr,TEXT(RuleSeg))==0
  333. || lstrcmpi(szStr,TEXT(DescriptSeg))==0) {
  334. bReadText = FALSE;
  335. // lstrcpy(szStr,"");
  336. continue;
  337. }
  338. if(bReadText) {
  339. trim(szStr);
  340. if(lstrlen(szStr) != 0) {
  341. nRet = SimpleSplit(hWnd,&nPages,szStr,lpCreateWords,lpWordIndex,Descript.wMaxCodes);
  342. if(nRet == 1)
  343. {
  344. lpWordIndex[dwBaseWordNum-1].dwOffset = dwOffset;
  345. searchPos(lpWordIndex,dwBaseWordNum);
  346. if(dwBaseWordNum%100 == 0)
  347. {
  348. SetDlgItemInt (hWnd,TM_TOTALNUM,dwBaseWordNum,FALSE);
  349. InvalidateRect(hDlgItem,NULL,FALSE);
  350. }
  351. }
  352. }
  353. }
  354. else
  355. lstrcpy(szStr,TEXT(""));
  356. continue;
  357. }
  358. else {
  359. if(j == 0)
  360. dwOffset = dwFstOffset + i*sizeof(TCHAR);
  361. szStr[j]=Buffer[i];
  362. j++;
  363. }
  364. }
  365. if(dwReadBytes*sizeof(TCHAR) < MAXREADBUFFER)
  366. break;
  367. dwFstOffset += MAXREADBUFFER;
  368. };
  369. SetDlgItemInt (hWnd,TM_TOTALNUM,dwBaseWordNum,FALSE);
  370. InvalidateRect(hDlgItem,NULL,FALSE);
  371. if(bSameFile) {
  372. if(dwDesLen != 0)
  373. ConvSaveDescript(TEXT(TempFile),&Descript, 0,0);
  374. if(dwRuleLen != 0)
  375. ConvSaveRule(hWnd,TEXT(TempFile), dwDesLen, 0,
  376. lpRule, Descript.wNumRules);
  377. hMBFile= Create_File(hWnd,TEXT(TempFile),GENERIC_READ|GENERIC_WRITE,OPEN_EXISTING);
  378. }
  379. else {
  380. if(dwDesLen != 0)
  381. ConvSaveDescript(lpDestFile,&Descript, 0,0);
  382. if(dwRuleLen != 0)
  383. ConvSaveRule(hWnd,lpDestFile, dwDesLen, 0,
  384. lpRule, Descript.wNumRules);
  385. hMBFile= Create_File(hWnd,lpDestFile,GENERIC_READ|GENERIC_WRITE,OPEN_EXISTING);
  386. }
  387. SetCursor (LoadCursor (NULL, IDC_ARROW));
  388. if (hMBFile == (HANDLE)-1) {
  389. CloseHandle(hSRCFile);
  390. goto err;
  391. }
  392. SetCursor (LoadCursor (NULL, IDC_WAIT));
  393. DispInfo(hWnd,IDS_WRITETEXTINFO);
  394. {
  395. #ifdef UNICODE
  396. TCHAR cmbStr[] = {0x662F, 0x5426, 0x751F, 0x6210, 0x91CD, 0x7801, 0x8868, 0x0028, 0x0059, 0x002F, 0x004E, 0x0029, 0x003F, 0x0000};
  397. #else
  398. BYTE cmbStr[] = "�Ƿ�����������(Y/N)?";
  399. #endif //UNICODE
  400. TCHAR cmbExt[] = TEXT(".CMB");
  401. LPTSTR lpString;
  402. TCHAR cmbFileName[MAX_PATH];
  403. IsGenCmb = ErrMessage(hWnd, cmbStr);
  404. if(IsGenCmb)
  405. {
  406. lstrcpy(cmbFileName, lpSrcFileName);
  407. if( (lpString = _tcsrchr(cmbFileName, TEXT('.'))) != NULL)
  408. *lpString = 0;
  409. lstrcat(cmbFileName, cmbExt);
  410. hCmbFile= Create_File(hWnd, cmbFileName, GENERIC_READ|GENERIC_WRITE, CREATE_ALWAYS);
  411. if(hCmbFile == (HANDLE)-1)
  412. {
  413. CloseHandle(hSRCFile);
  414. CloseHandle(hMBFile);
  415. goto err;
  416. }
  417. }
  418. }
  419. SetCursor (LoadCursor (NULL, IDC_ARROW));
  420. WriteSortFile(hWnd,hSRCFile,hMBFile,lpWordIndex,lpCreateWords);
  421. GlobalUnlock(hCreateWord);
  422. GlobalFree(hCreateWord);
  423. GlobalFree(hWordIndex);
  424. SetCursor (LoadCursor (NULL, IDC_ARROW));
  425. if(hRule0)
  426. GlobalFree(hRule0);
  427. CloseHandle(hSRCFile);
  428. CloseHandle(hMBFile);
  429. if(hCmbFile > 0)
  430. CloseHandle(hCmbFile);
  431. if(bSameFile) {
  432. DeleteFile(lpSrcFileName);
  433. nRet = MoveFile(TEXT(TempFile),lpSrcFileName);
  434. }
  435. LoadString(NULL,IDS_SORTEND,szStr,sizeof(szStr));
  436. #ifdef UNICODE
  437. {
  438. TCHAR UniTmp[]={0x8BCD, 0x8BED, 0x6392, 0x5E8F, 0x0000};
  439. MessageBox(hWnd,szStr,UniTmp,MB_OK);
  440. }
  441. #else
  442. MessageBox(hWnd,szStr,"��������",MB_OK);
  443. #endif
  444. return TRUE;
  445. err:
  446. if(!_taccess(TEXT(TempFile),0))
  447. DeleteFile(TEXT(TempFile));
  448. if(hWordIndex)
  449. GlobalFree(hWordIndex);
  450. if(hCreateWord)
  451. GlobalFree(hCreateWord);
  452. if(hRule0)
  453. GlobalFree(hRule0);
  454. return FALSE;
  455. }
  456. /*****************************************************************************
  457. FUNCTION: ConvReConv(HANDLE hWnd,LPCSTR lpSrcFileName, LPCSTR lpszMBFileName)
  458. PURPOSE: Processes MB reconversion .
  459. PARAMETERS:
  460. hWnd - window handle of the parent window.
  461. lpSrcFileName - pointer to source .mb file name.
  462. lpszMBFileName - pointer to destinate text file name.
  463. RETURN VALUE:
  464. TRUE - reconversion completed
  465. FALSE - reconversion not completed
  466. HISTORY:
  467. 05-21-95 Yehfew Tie modifyed.
  468. ****************************************************************************/
  469. BOOL ConvReConv(HANDLE hWnd,LPCTSTR lpSrcFileName,LPCTSTR lpMBFileName)
  470. {
  471. DWORD dwDesOffset,dwDesLen;
  472. HANDLE hRule0;
  473. MAINID MainID;
  474. LPRULE lpRule;
  475. DESCRIPTION Descript;
  476. HANDLE hReConvIndex;
  477. LPCREATEWORD lpCreateWords, lpReConvIndex;
  478. MAININDEX MainIndex[NUMTABLES];
  479. hMBFile = Create_File(hWnd,lpMBFileName,GENERIC_READ,OPEN_EXISTING);
  480. if (hMBFile == (HANDLE)-1)
  481. return FALSE;
  482. DispInfo(hWnd,IDS_RECONVINFO);
  483. ConvGetMainID(hMBFile,&MainID);
  484. if(!ConvGetMainIndex(hWnd,hMBFile,MainIndex)) {
  485. CloseHandle(hMBFile);
  486. return FALSE;
  487. }
  488. hSRCFile = Create_File(hWnd,lpSrcFileName,
  489. GENERIC_READ|GENERIC_WRITE,CREATE_ALWAYS);
  490. if (hSRCFile == (HANDLE)-1)
  491. return FALSE;
  492. CloseHandle(hSRCFile);
  493. ConvReadDescript(hMBFile,&Descript, MainIndex);
  494. ConvSaveDescript(lpSrcFileName,&Descript, 0,0);
  495. ConvGetDescript(hWnd,lpSrcFileName,&dwDesOffset,
  496. &dwDesLen,&Descript,FALSE);
  497. hCreateWord = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,
  498. (DWORD)Descript.wMaxCodes*NUM_OF_ENCODE*sizeof(TCHAR));
  499. if(!hCreateWord ) {
  500. ProcessError(ERR_OUTOFMEMORY,hWnd,ERR);
  501. return FALSE;
  502. }
  503. if(!(lpCreateWords = GlobalLock(hCreateWord)) ) {
  504. ProcessError(ERR_GLOBALLOCK,hWnd,ERR);
  505. return FALSE;
  506. }
  507. hReConvIndex = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,
  508. (DWORD)Descript.wMaxCodes*NUM_OF_ENCODE*sizeof(TCHAR));
  509. if(!hReConvIndex ) {
  510. ProcessError(ERR_OUTOFMEMORY,hWnd,ERR);
  511. CloseHandle(hMBFile);
  512. GlobalFree(hCreateWord);
  513. return FALSE;
  514. }
  515. if( !(lpReConvIndex = GlobalLock(hReConvIndex)) ) {
  516. ProcessError(ERR_GLOBALLOCK,hWnd,ERR);
  517. GlobalFree(hReConvIndex);
  518. CloseHandle(hMBFile);
  519. GlobalFree(hCreateWord);
  520. return FALSE;
  521. }
  522. if(Descript.wNumRules != 0) {
  523. hRule0 = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,
  524. sizeof(RULE)*Descript.wNumRules);
  525. if(!hRule0) {
  526. ProcessError(ERR_OUTOFMEMORY,hWnd,ERR);
  527. GlobalFree(hReConvIndex);
  528. CloseHandle(hMBFile);
  529. GlobalFree(hCreateWord);
  530. return FALSE;
  531. }
  532. if(!(lpRule = GlobalLock(hRule0)) ) {
  533. ProcessError(ERR_GLOBALLOCK,hWnd,ERR);
  534. CloseHandle(hMBFile);
  535. GlobalFree(hCreateWord);
  536. GlobalFree(hRule0);
  537. GlobalFree(hReConvIndex);
  538. return FALSE;
  539. }
  540. ConvReadRule(hMBFile,Descript.wNumRules ,lpRule, MainIndex);
  541. ConvSaveRule(hWnd,lpSrcFileName, dwDesLen, 0, lpRule, Descript.wNumRules);
  542. }
  543. ConvGetCrtData(hMBFile, lpCreateWords, MainIndex);
  544. ConvGetReConvIndex(hMBFile, lpReConvIndex,MainIndex);
  545. hSRCFile = Create_File(hWnd,lpSrcFileName,
  546. GENERIC_READ|GENERIC_WRITE,OPEN_EXISTING);
  547. if (hSRCFile == (HANDLE)-1)
  548. return FALSE;
  549. ConvWriteBaseWord(hWnd,hSRCFile,hMBFile,MainID,
  550. MainIndex[TAG_BASEDIC-1].dwOffset, lpCreateWords, lpReConvIndex,Descript.wMaxCodes);
  551. // DispInfo(hWnd,IDS_RECONVEND); bug#57932
  552. CloseHandle(hSRCFile);
  553. CloseHandle(hMBFile);
  554. GlobalFree(hCreateWord);
  555. GlobalFree(hReConvIndex);
  556. if(Descript.wNumRules != 0)
  557. GlobalFree(hRule0);
  558. ProcessError(ERR_RECONVEND, hWnd, INFO);
  559. return TRUE;
  560. }
  561. void ConvInit(LPDESCRIPTION lpDescript,
  562. LPMAINID lpMainID,
  563. LPMAININDEX lpMainIndex)
  564. {
  565. DWORD dwTotal;
  566. int i;
  567. //*** initializes struct MainID ***
  568. lstrcpy(lpMainID->szVersion,TEXT("95.01"));
  569. lpMainID->dwLanguageID = WIN95_PRC;
  570. #ifdef UNICODE
  571. lpMainID->dwEncodeID = UNICODE_IME;
  572. #else
  573. lpMainID->dwEncodeID = GBK_IME;
  574. #endif
  575. lpMainID->dwNumTables = NUMTABLES;
  576. lpMainID->dwNumWords = dwBaseWordNum;
  577. lpMainID->dwFileFlag = 0;
  578. if(lpDescript->byMaxElement > 1 )
  579. lpMainID->dwFileFlag |= FFLG_MULTIELEMENT;
  580. if(lpDescript->wNumRules > 0)
  581. lpMainID->dwFileFlag |= FFLG_RULE;
  582. //*** set tables tag ****
  583. lpMainIndex[TAG_DESCRIPTION-1].dwTag = TAG_DESCRIPTION;
  584. lpMainIndex[TAG_RULE-1].dwTag = TAG_RULE;
  585. lpMainIndex[TAG_ENCODE-1].dwTag = TAG_ENCODE;
  586. lpMainIndex[TAG_CRTWORDCODE-1].dwTag = TAG_CRTWORDCODE;
  587. lpMainIndex[TAG_RECONVINDEX-1].dwTag = TAG_RECONVINDEX;
  588. lpMainIndex[TAG_BASEDICINDEX-1].dwTag = TAG_BASEDICINDEX;
  589. lpMainIndex[TAG_BASEDIC-1].dwTag = TAG_BASEDIC;
  590. //*** set tables length ****
  591. lpMainIndex[TAG_DESCRIPTION-1].dwLength = sizeof(DESCRIPTION);
  592. lpMainIndex[TAG_RULE-1].dwLength= sizeof(RULE)* lpDescript->wNumRules;
  593. lpMainIndex[TAG_ENCODE-1].dwLength =
  594. 2*sizeof(DWORD) + NUMENCODEAREA*sizeof(ENCODEAREA);
  595. lpMainIndex[TAG_CRTWORDCODE-1].dwLength =
  596. (DWORD)NUM_OF_ENCODE*(DWORD)lpDescript->wMaxCodes*sizeof(TCHAR);
  597. lpMainIndex[TAG_RECONVINDEX-1].dwLength =
  598. (DWORD)lpDescript->wMaxCodes*NUM_OF_ENCODE*sizeof(TCHAR);
  599. switch(lpMainID->dwFileFlag) {
  600. case FFLG_RULE|FFLG_SINGLEELEMENT:
  601. lpMainIndex[TAG_RECONVINDEX-1].dwLength = 0;
  602. break;
  603. case FFLG_RULE|FFLG_MULTIELEMENT:
  604. break;
  605. case FFLG_NORULE|FFLG_SINGLEELEMENT:
  606. case FFLG_NORULE|FFLG_MULTIELEMENT:
  607. lpMainIndex[TAG_RULE-1].dwLength = 0;
  608. lpMainIndex[TAG_CRTWORDCODE-1].dwLength = 0;
  609. break;
  610. default:
  611. break;
  612. }
  613. lpMainIndex[TAG_BASEDICINDEX-1].dwLength =( (DWORD)lpDescript->wNumCodes+1)*
  614. (DWORD)lpDescript->wNumCodes*sizeof(DWORD)*2+MAXNUMCODES+CODEMAPOFFSET;
  615. lpMainIndex[TAG_BASEDIC-1].dwLength = 0;
  616. //***** set tables offset *****
  617. dwTotal = sizeof(MAINID) + sizeof(MAININDEX)*NUMTABLES;
  618. lpMainIndex[TAG_DESCRIPTION-1].dwOffset = dwTotal;
  619. dwTotal += lpMainIndex[TAG_DESCRIPTION-1].dwLength;
  620. lpMainIndex[TAG_RULE-1].dwOffset = dwTotal;
  621. dwTotal += lpMainIndex[TAG_RULE-1].dwLength;
  622. lpMainIndex[TAG_ENCODE-1].dwOffset = dwTotal;
  623. dwTotal += lpMainIndex[TAG_ENCODE-1].dwLength;
  624. lpMainIndex[TAG_CRTWORDCODE-1].dwOffset = dwTotal;
  625. dwTotal += lpMainIndex[TAG_CRTWORDCODE-1].dwLength;
  626. lpMainIndex[TAG_RECONVINDEX-1].dwOffset = dwTotal;
  627. dwTotal += lpMainIndex[TAG_RECONVINDEX-1].dwLength;
  628. lpMainIndex[TAG_BASEDICINDEX-1].dwOffset = dwTotal;
  629. dwTotal += lpMainIndex[TAG_BASEDICINDEX-1].dwLength;
  630. lpMainIndex[TAG_BASEDIC-1].dwOffset = dwTotal;
  631. for(i=0; i<NUMTABLES; i++)
  632. lpMainIndex[i].dwCheckSum = lpMainIndex[i].dwTag +
  633. lpMainIndex[i].dwLength +
  634. lpMainIndex[i].dwOffset;
  635. }
  636. BOOL ReadSRCFile(HANDLE hWnd,
  637. LPINT nPages,
  638. HANDLE hFile,
  639. LPCREATEWORD lpCreateWords,
  640. LPWORDINDEX lpWordIndex,
  641. LPINT dwWord, LPDESCRIPTION lpDescript,
  642. LPRECONVINDEX lpReConvIndex)
  643. {
  644. TCHAR szStr[256];
  645. TCHAR Buffer[MAXREADBUFFER];
  646. BOOL bReadText=FALSE;
  647. register int i,j;
  648. int nRet;
  649. DWORD dwReadBytes;
  650. DWORD dwFstOffset=0;
  651. DWORD dwOffset=0;
  652. SetCursor (LoadCursor (NULL, IDC_WAIT));
  653. SetFilePointer(hFile,0,0,FILE_BEGIN);
  654. szStr[0] = 0;
  655. j = 0;
  656. dwLineNo = 0;
  657. dwOffset = 0;
  658. memset(WriteCrtFlag, 0, (NUM_OF_ENCODE+7)/8);
  659. while(ReadFile(hFile,Buffer,MAXREADBUFFER,&dwReadBytes,NULL))
  660. {
  661. dwReadBytes = dwReadBytes/sizeof(TCHAR);
  662. for(i=0;i<(int)dwReadBytes;i++) {
  663. if(Buffer[i] == 0x0d || Buffer[i] == 0xfeff) continue;
  664. if(Buffer[i] == TEXT('\n'))
  665. {
  666. dwLineNo ++;
  667. szStr[j]=0;
  668. j = 0;
  669. if(lstrcmpi(szStr,TEXT("[TEXT]"))==0) {
  670. bReadText = TRUE;
  671. lstrcpy(szStr,TEXT(""));
  672. continue;
  673. }
  674. if(lstrcmpi(szStr,TEXT(RuleSeg))==0
  675. || lstrcmpi(szStr,TEXT(DescriptSeg))==0) {
  676. bReadText = FALSE;
  677. lstrcpy(szStr,TEXT(""));
  678. continue;
  679. }
  680. if(bReadText) {
  681. trim(szStr);
  682. if(lstrlen(szStr) != 0) {
  683. nRet = WordSplit(hWnd,nPages,szStr,lpCreateWords,lpWordIndex,
  684. lpDescript,lpReConvIndex);
  685. if(nRet == 1)
  686. lpWordIndex[dwBaseWordNum-1].dwOffset = dwOffset;
  687. else if(!nRet) {
  688. SetCursor (LoadCursor (NULL, IDC_ARROW));
  689. return FALSE;
  690. }
  691. }
  692. }
  693. else
  694. lstrcpy(szStr,TEXT(""));
  695. continue;
  696. }
  697. else {
  698. if(j == 0)
  699. dwOffset = dwFstOffset + i*sizeof(TCHAR);
  700. szStr[j]=Buffer[i];
  701. j++;
  702. }
  703. }
  704. if(dwReadBytes*sizeof(TCHAR) < MAXREADBUFFER) break;
  705. dwFstOffset += MAXREADBUFFER;
  706. };
  707. SetCursor (LoadCursor (NULL, IDC_ARROW));
  708. if(dwBaseWordNum == 0) {
  709. ProcessError(ERR_TEXTSEG,hWnd,ERR);
  710. return FALSE;
  711. }
  712. else
  713. return TRUE;
  714. }
  715. BOOL ConvCalc(HANDLE hWnd,
  716. HANDLE hMBFile,
  717. LPWORDINDEX lpWordIndex,
  718. LPMAININDEX lpMainIndex,
  719. LPDESCRIPTION lpDescript)
  720. {
  721. DWORD i,j,dwRecLen,dwOffset;
  722. BYTE bySwap;
  723. TCHAR Code[MAXNUMCODES],cc;
  724. DWORD NumX,NumY,NumCodes,FstAdr,dwXStart,dwYStart,dwMaxXLen=0;
  725. HGLOBAL hNumXYWords,hDicIndex;
  726. LPNUMXYWORDS lpNumXYWords;
  727. LPDICINDEX lpDicIndex;
  728. BOOL bStart = FALSE;
  729. NumCodes=lpDescript->wNumCodes;
  730. hNumXYWords = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,sizeof(DWORD)*((NumCodes+1)*NumCodes+1L));
  731. hDicIndex = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,sizeof(DWORD)*(NumCodes+1)*NumCodes);
  732. if(!hNumXYWords || !hDicIndex) {
  733. ProcessError(ERR_OUTOFMEMORY,hWnd,ERR);
  734. return FALSE;
  735. }
  736. if( !(lpNumXYWords = GlobalLock(hNumXYWords))
  737. ||!(lpDicIndex = GlobalLock(hDicIndex)) ) {
  738. ProcessError(ERR_GLOBALLOCK,hWnd,ERR);
  739. GlobalFree(hNumXYWords);
  740. GlobalFree(hDicIndex);
  741. return FALSE;
  742. }
  743. lstrcpy(Code,lpDescript->szUsedCode);
  744. FstAdr = lpMainIndex[TAG_BASEDIC-1].dwOffset;
  745. NumX=0;
  746. NumY=0;
  747. dwXStart=0;
  748. dwYStart=0;
  749. dwOffset=0;
  750. lpNumXYWords[0]=0;
  751. lpDicIndex[0]=0;
  752. for(i =0; i<dwBaseWordNum;i++) {
  753. bySwap =(BYTE) lstrlen(lpWordIndex[i].szCode);
  754. dwRecLen = (DWORD)bySwap*sizeof(TCHAR)+sizeof(TCHAR);
  755. bySwap =(BYTE) lpWordIndex[i].wDBCSLen;
  756. // if the DBCS str length is longer than 126, just get the first 126 characters.
  757. if ( bySwap >= 126 )
  758. bySwap = 126;
  759. dwRecLen += 2*(DWORD)bySwap+sizeof(TCHAR);
  760. while( lpWordIndex[i].szCode[0] != Code[NumX] )
  761. {
  762. if(!bStart && NumY == 0)
  763. {
  764. lpNumXYWords[NumX*(NumCodes+1)+NumY+1] = i;
  765. lpDicIndex[NumX*(NumCodes+1)+NumY+1] = dwOffset;
  766. }
  767. while(NumY < NumCodes-1 )
  768. {
  769. NumY ++;
  770. lpNumXYWords[NumX*(NumCodes+1)+NumY+1] = i;
  771. lpDicIndex[NumX*(NumCodes+1)+NumY+1] = dwOffset;
  772. }
  773. NumX ++;
  774. if(NumX >= NumCodes) break;
  775. lpNumXYWords[NumX*(NumCodes+1)] = i;
  776. lpDicIndex[NumX*(NumCodes+1)] = dwOffset;
  777. NumY = 0;
  778. bStart = FALSE;
  779. }
  780. if(NumX >= NumCodes) break;
  781. cc = lpWordIndex[i].szCode[1];
  782. if(!bStart && cc != 0 && NumY == 0)
  783. {
  784. lpNumXYWords[NumX*(NumCodes+1)+1] = i;
  785. lpDicIndex[NumX*(NumCodes+1)+1] = dwOffset;
  786. bStart = TRUE;
  787. }
  788. while(bStart && cc != 0 && cc != Code[NumY] && NumY < NumCodes - 1 )
  789. {
  790. NumY ++;
  791. lpNumXYWords[NumX*(NumCodes+1)+NumY+1] = i;
  792. lpDicIndex[NumX*(NumCodes+1)+NumY+1] = dwOffset;
  793. }
  794. dwOffset += dwRecLen;
  795. if(i == dwBaseWordNum-1) {
  796. j = NumX*(NumCodes+1) + NumY + 1;
  797. while(j < (NumCodes+1)*NumCodes - 1 )
  798. {
  799. j ++;
  800. lpNumXYWords[j] = i + 1;
  801. lpDicIndex[j] = dwOffset;
  802. }
  803. lpNumXYWords[NumCodes*(NumCodes+1)] = i;
  804. }
  805. }
  806. for(i = 0; i < NumCodes ; i++)
  807. {
  808. for(j = 0; j < NumCodes + 1; j++)
  809. {
  810. if(j == 0)
  811. {
  812. lpNumXYWords[i*(NumCodes+1)] = lpNumXYWords[(i+1)*(NumCodes+1)] -
  813. lpNumXYWords[i*(NumCodes+1)];
  814. }
  815. else
  816. {
  817. lpNumXYWords[i*(NumCodes+1)+j] = lpNumXYWords[i*(NumCodes+1)+j+1] -
  818. lpNumXYWords[i*(NumCodes+1)+j];
  819. lpDicIndex[i*(NumCodes+1)+j] = lpDicIndex[i*(NumCodes+1)+j] -
  820. lpDicIndex[i*(NumCodes+1)];
  821. }
  822. }
  823. }
  824. lpMainIndex[TAG_BASEDIC-1].dwLength = dwOffset;
  825. lpMainIndex[TAG_BASEDIC-1].dwCheckSum = TAG_BASEDIC +
  826. lpMainIndex[TAG_BASEDIC-1].dwOffset+
  827. dwOffset;
  828. for(i=0; i< NumCodes - 1;i++) {
  829. dwRecLen = lpDicIndex[(i+1)*(NumCodes+1)]-lpDicIndex[(NumCodes+1)*i];
  830. dwMaxXLen = (dwRecLen>dwMaxXLen)? dwRecLen:dwMaxXLen;
  831. }
  832. dwRecLen = dwOffset - lpDicIndex[(NumCodes+1)*(NumCodes-1)];
  833. dwMaxXLen = (dwRecLen>dwMaxXLen)? dwRecLen:dwMaxXLen;
  834. ConvWriteMainIndex(hMBFile,lpMainIndex);
  835. ConvWriteCodeIndex(hMBFile,&dwMaxXLen,lpDescript->szUsedCode,lpMainIndex);
  836. ConvWriteDicIndex(hMBFile,lpDicIndex,lpDescript->wNumCodes,lpMainIndex);
  837. ConvWriteNumXYWords(hMBFile,lpNumXYWords,
  838. lpDescript->wNumCodes,lpMainIndex);
  839. GlobalUnlock(hDicIndex);
  840. GlobalUnlock(hNumXYWords);
  841. GlobalFree(hDicIndex);
  842. GlobalFree(hNumXYWords);
  843. return TRUE;
  844. }
  845. BOOL WriteMBFile(HANDLE hWnd,
  846. HANDLE hSRCFile,
  847. HANDLE hMBFile,
  848. LPWORDINDEX lpWordIndex,
  849. LPMAININDEX lpMainIndex,
  850. LPDESCRIPTION lpDescript)
  851. {
  852. DWORD i,dwBytes,NumCodes,FstAdr;
  853. WORD bySwap;
  854. TCHAR szStr[256];
  855. TCHAR Code[MAXNUMCODES];
  856. DWORD dwReadSum;
  857. HANDLE hDlgItem;
  858. SetCursor (LoadCursor (NULL, IDC_WAIT));
  859. SetDlgItemText(hWnd,TM_TOTALINFO,TEXT(""));
  860. LoadString(NULL,IDS_TOTALINFO,szStr,sizeof(szStr)/sizeof(TCHAR));
  861. SetDlgItemText(hWnd,TM_TOTAL,szStr);
  862. LoadString(NULL,IDS_CONVWORDS,szStr,sizeof(szStr)/sizeof(TCHAR));
  863. SetDlgItemText(hWnd,TM_CONVINFO,szStr);
  864. SetDlgItemInt(hWnd,TM_TOTALNUM,dwBaseWordNum,FALSE);
  865. dwReadSum = 0;
  866. SetDlgItemInt(hWnd,TM_CONVNUM,dwReadSum,FALSE);
  867. InvalidateRect(hWnd,NULL,FALSE);
  868. NumCodes=lpDescript->wNumCodes;
  869. lstrncpy(Code,NumCodes,lpDescript->szUsedCode);
  870. FstAdr = lpMainIndex[TAG_BASEDIC-1].dwOffset;
  871. SetFilePointer(hMBFile,FstAdr,0, FILE_BEGIN);
  872. for(i =0; i<dwBaseWordNum;i++) {
  873. SetFilePointer(hSRCFile,lpWordIndex[i].dwOffset,0,FILE_BEGIN);
  874. bySwap =(WORD) lpWordIndex[i].wDBCSLen;
  875. ReadFile(hSRCFile,szStr,2*(DWORD)bySwap,&dwBytes,NULL);
  876. bySwap =(WORD) lstrlen(lpWordIndex[i].szCode);
  877. WriteFile(hMBFile,&bySwap,sizeof(TCHAR),&dwBytes,NULL);
  878. WriteFile(hMBFile,&(lpWordIndex[i].szCode),bySwap*sizeof(TCHAR),&dwBytes,NULL);
  879. bySwap =(WORD) lpWordIndex[i].wDBCSLen;
  880. // if the DBCS str length is longer than 126, just get the first 126 characters.
  881. if ( bySwap >= 126 )
  882. bySwap = 126;
  883. WriteFile(hMBFile,&bySwap,sizeof(TCHAR),&dwBytes,NULL);
  884. WriteFile(hMBFile,szStr,2*bySwap,&dwBytes,NULL);
  885. dwReadSum ++;
  886. if(dwReadSum%100 ==0 || dwReadSum == dwBaseWordNum) {
  887. SetDlgItemInt(hWnd,TM_CONVNUM,dwReadSum,FALSE);
  888. hDlgItem=GetDlgItem(hWnd,TM_CONVNUM);
  889. InvalidateRect(hDlgItem,NULL,FALSE);
  890. }
  891. }
  892. SetCursor (LoadCursor (NULL, IDC_ARROW));
  893. return TRUE;
  894. }
  895. BOOL WriteSortFile(HANDLE hWnd,
  896. HANDLE hSRCFile,
  897. HANDLE hDestFile,
  898. LPWORDINDEX lpWordIndex,
  899. LPCREATEWORD lpCreateWords)
  900. {
  901. DWORD i, k, dwBytes;
  902. WORD wRecLen;
  903. BYTE bySwap;
  904. TCHAR szStr[512],szDBCS[512];
  905. TCHAR szCode[13],szCreate[13];
  906. BOOL IsGenCmb = (hCmbFile)?TRUE:FALSE;
  907. BOOL bExistNext = FALSE;
  908. DWORD dwReadSum;
  909. SetFilePointer(hDestFile,0,0, FILE_END);
  910. dwReadSum = 0;
  911. for(i =0; i<dwBaseWordNum;i++) {
  912. SetFilePointer(hSRCFile,lpWordIndex[i].dwOffset,0,FILE_BEGIN);
  913. wRecLen = 2*lpWordIndex[i].wDBCSLen/sizeof(TCHAR);
  914. ReadFile(hSRCFile,szDBCS,wRecLen*sizeof(TCHAR),&dwBytes,NULL);
  915. szDBCS[wRecLen] = 0;
  916. lstrcpy(szCode,lpWordIndex[i].szCode);
  917. bySwap =(BYTE)lstrlen(lpWordIndex[i].szCode);
  918. if(wRecLen == 2/sizeof(TCHAR)){
  919. k = EncodeToNo(szDBCS);
  920. if((long)k>=0 && k < NUM_OF_ENCODE) {
  921. lstrncpy(szCreate,MAXCODELEN,&lpCreateWords[k*MAXCODELEN]);
  922. szCreate[MAXCODELEN]=0;
  923. if(lstrlen(szCreate))
  924. wsprintf(szStr,TEXT("%s%s %s\r\n"),szDBCS,szCode,szCreate);
  925. else
  926. wsprintf(szStr,TEXT("%s%s\r\n"),szDBCS,szCode);
  927. }
  928. else
  929. wsprintf(szStr,TEXT("%s%s\r\n"),szDBCS,szCode);
  930. }
  931. else
  932. wsprintf(szStr,TEXT("%s%s\r\n"),szDBCS,szCode);
  933. WriteFile(hDestFile,szStr,lstrlen(szStr)*sizeof(TCHAR),&dwBytes,NULL);
  934. if(IsGenCmb)
  935. {
  936. if(i != dwBaseWordNum - 1
  937. && _wcsicmp(lpWordIndex[i].szCode, lpWordIndex[i+1].szCode) == 0)
  938. {
  939. wsprintf(szStr,TEXT("%s%s\r\n"),szCode,szDBCS);
  940. WriteFile(hCmbFile,szStr,lstrlen(szStr)*sizeof(TCHAR),&dwBytes,NULL);
  941. bExistNext = TRUE;
  942. dwReadSum ++;
  943. }
  944. else
  945. {
  946. if(bExistNext)
  947. {
  948. wsprintf(szStr,TEXT("%s%s\r\n"),szCode,szDBCS);
  949. WriteFile(hCmbFile,szStr,lstrlen(szStr)*sizeof(TCHAR),&dwBytes,NULL);
  950. }
  951. bExistNext = FALSE;
  952. }
  953. }
  954. }
  955. if(IsGenCmb)
  956. {
  957. #ifdef UNICODE
  958. TCHAR UniTmp[] = {0x5171, 0x8BA1, 0x91CD, 0x7801, 0x8BCD, 0x6761, 0x6570, 0x0000};
  959. wsprintf(szStr,TEXT("%ws = %d\n"), UniTmp, dwReadSum);
  960. #else
  961. wsprintf(szStr,"�������������� = %ld\n", dwReadSum);
  962. #endif
  963. WriteFile(hCmbFile,szStr,lstrlen(szStr)*sizeof(TCHAR),&dwBytes,NULL);
  964. }
  965. return TRUE;
  966. }
  967. int SimpleSplit(HANDLE hWnd,
  968. LPINT nPages,
  969. LPTSTR szStr,
  970. LPCREATEWORD lpCreateWords,
  971. LPWORDINDEX lpWordIndex,
  972. WORD wMaxCodes)
  973. {
  974. register int i=0;
  975. int nDBCS=0, nCodes=0, nCreate = 0;
  976. TCHAR szDBCS[512],szCode[13],szCreate[128];
  977. if(ParseDBCSstr(hWnd,szStr,szDBCS,szCode,szCreate,wMaxCodes) != TRUE)
  978. {
  979. if(lstrlen(szDBCS) == 0)
  980. {
  981. LoadString(NULL, IDS_NOTEXISTDBCS, szCreate, sizeof(szCreate)/sizeof(TCHAR));
  982. wsprintf(szDBCS,TEXT("\'%s%lu)"),szCreate,dwLineNo);
  983. FatalMessage(hWnd,szDBCS);
  984. }
  985. return FALSE;
  986. }
  987. nDBCS = lstrlen(szDBCS);
  988. DelIllegalCode(szCode);
  989. nCodes = lstrlen(szCode);
  990. nCreate = lstrlen(szCreate);
  991. dwBaseWordNum ++;
  992. lpWordIndex[dwBaseWordNum-1].wDBCSLen =(WORD)(nDBCS*sizeof(TCHAR)/2);
  993. lstrcpy(lpWordIndex[dwBaseWordNum-1].szCode,szCode);
  994. if(nDBCS == 2/sizeof(TCHAR)) {
  995. i = EncodeToNo(szDBCS);
  996. if(i>=0 && i < NUM_OF_ENCODE)
  997. {
  998. if(nCreate != 0)
  999. lstrncpy(lpCreateWords+i*MAXCODELEN,
  1000. MAXCODELEN, szCreate);
  1001. }
  1002. }
  1003. return TRUE;
  1004. }
  1005. int WordSplit(HANDLE hWnd,
  1006. LPINT nPages,
  1007. LPTSTR szStr,
  1008. LPCREATEWORD lpCreateWords,
  1009. LPWORDINDEX lpWordIndex,
  1010. LPDESCRIPTION lpDescript,
  1011. LPRECONVINDEX lpReConvIndex)
  1012. {
  1013. int i, nDBCS=0, nCodes=0, nCreate = 0;
  1014. TCHAR szTemp[512],szTmpStr[256];
  1015. TCHAR szDBCS[512],szCode[13],szCreate[128];
  1016. if(ParseDBCSstr(hWnd,szStr,szDBCS,szCode,szCreate,lpDescript->wMaxCodes) != TRUE)
  1017. {
  1018. if(lstrlen(szDBCS) == 0)
  1019. {
  1020. LoadString(NULL, IDS_NOTEXISTDBCS, szTmpStr, sizeof(szTmpStr)/sizeof(TCHAR));
  1021. wsprintf(szTemp,TEXT("\'%s%lu)"),szTmpStr,dwLineNo);
  1022. FatalMessage(hWnd,szTemp);
  1023. }
  1024. return FALSE;
  1025. }
  1026. if ( lstrlen(szDBCS) > 126 ) {
  1027. // if the string is longer than 126, the rest will be cut.
  1028. // so show a message to end user.
  1029. LoadString(NULL, IDS_DBCSTOOLONG, szTmpStr, sizeof(szTmpStr)/sizeof(TCHAR));
  1030. #ifdef UNICODE
  1031. {
  1032. TCHAR UniTmp[] = {0x0027, 0x0025, 0x0077, 0x0073, 0x0027, 0x0020, 0x0020,
  1033. 0x0020, 0x0028, 0x884C, 0x003A, 0x0025, 0x006C, 0x0075,
  1034. 0x0029, 0x0025, 0x0077, 0x0073, 0x000D, 0x000A, 0x000D,
  1035. 0x000A, 0x9000, 0x51FA, 0x5426, 0x003F, 0x0020, 0x0000};
  1036. wsprintf(szTemp, UniTmp, szDBCS,dwLineNo,szTmpStr);
  1037. }
  1038. #else
  1039. wsprintf(szTemp,"\'%s\'%s(��:%lu)\n�˳���? ",szDBCS,szTmpStr,dwLineNo);
  1040. #endif
  1041. if (ErrMessage(hWnd,szTemp))
  1042. return FALSE;
  1043. }
  1044. if(!CheckCodeLegal(hWnd,szDBCS,szCode,szCreate,lpDescript))
  1045. return FALSE;
  1046. nDBCS = lstrlen(szDBCS);
  1047. nCodes = lstrlen(szCode);
  1048. nCreate = lstrlen(szCreate);
  1049. if(nDBCS == 0)
  1050. return -1;
  1051. if(nDBCS == 2/sizeof(TCHAR))
  1052. {
  1053. i = EncodeToNo(szDBCS);
  1054. if(i < 0 || i >= NUM_OF_ENCODE)
  1055. return FALSE;
  1056. if(nCodes == 0) {
  1057. LoadString(NULL, IDS_CODEEMPTY, szTmpStr, sizeof(szTmpStr)/sizeof(TCHAR));
  1058. #ifdef UNICODE
  1059. {
  1060. TCHAR UniTmp[] = {0x005C, 0x0027, 0x0025, 0x0077, 0x0073, 0x005C, 0x0027, 0x0025, 0x0077, 0x0073, 0x0028, 0x884C, 0x003A, 0x0025, 0x006C, 0x0075, 0x0029, 0x005C, 0x006E, 0x9000, 0x51FA, 0x5426, 0x003F, 0x0020, 0x0000};
  1061. wsprintf(szTemp, UniTmp, szDBCS,szTmpStr,dwLineNo);
  1062. }
  1063. #else
  1064. wsprintf(szTemp,"\'%s\'%s(��:%lu)\n�˳���? ",szDBCS,szTmpStr,dwLineNo);
  1065. #endif \\UNICODE
  1066. if(ErrMessage(hWnd,szTemp))
  1067. return FALSE;
  1068. }
  1069. //**** write create words
  1070. if(lpDescript->wNumRules != 0 && nCreate != 0)
  1071. {
  1072. if(nCreate > lpDescript->wMaxCodes) {
  1073. LoadString(NULL,IDS_DBCSCODE,szTmpStr,sizeof(szTmpStr)/sizeof(TCHAR));
  1074. #ifdef UNICODE
  1075. {
  1076. TCHAR UniTmp[] = {0x005C, 0x0027, 0x0025, 0x0077, 0x0073, 0x0025, 0x0077, 0x0073, 0x005C, 0x0027, 0x0020, 0x0025, 0x0064, 0x0021, 0x0028, 0x884C, 0x003A, 0x0025, 0x006C, 0x0075, 0x0029, 0x005C, 0x006E, 0x9000, 0x51FA, 0x5426, 0x003F, 0x0000};
  1077. wsprintf(szTemp,UniTmp, szDBCS,szTmpStr,
  1078. lpDescript->byMaxElement,dwLineNo);
  1079. }
  1080. #else
  1081. wsprintf(szTemp,"\'%s%s\' %d!(��:%lu)\n�˳���?", szDBCS,szTmpStr,
  1082. lpDescript->byMaxElement,dwLineNo);
  1083. #endif \\UNICODE
  1084. if( ErrMessage(hWnd,szTemp))
  1085. return FALSE;
  1086. }
  1087. lstrncpy(szTmpStr,lpDescript->wMaxCodes,
  1088. lpCreateWords+i*(DWORD)lpDescript->wMaxCodes);
  1089. szTmpStr[lpDescript->wMaxCodes] = 0;
  1090. if(!(WriteCrtFlag[i/8] & (1 << (7 - (i%8))) ) )
  1091. lstrncpy(lpCreateWords+i*(DWORD)lpDescript->wMaxCodes,
  1092. lpDescript->wMaxCodes,
  1093. szCreate);
  1094. WriteCrtFlag[i/8] |= (1 << (7 - (i%8)) );
  1095. }
  1096. }
  1097. else if(nDBCS>2*sizeof(TCHAR) && nCodes==0 && lpDescript->wNumRules==0)
  1098. {
  1099. LoadString(NULL,IDS_CODEEMPTY,szTmpStr,sizeof(szTmpStr)/sizeof(TCHAR));
  1100. #ifdef UNICODE
  1101. {
  1102. TCHAR UniTmp[] = {0x005C, 0x0027, 0x0025, 0x0077, 0x0073, 0x005C, 0x0027, 0x0025, 0x0077, 0x0073, 0x0028, 0x884C, 0x003A, 0x0025, 0x006C, 0x0064, 0x0029, 0x0000};
  1103. wsprintf(szTemp,UniTmp, szDBCS,szTmpStr,dwLineNo);
  1104. }
  1105. #else
  1106. wsprintf(szTemp,"\'%s\'%s(��:%ld)", szDBCS,szTmpStr,dwLineNo);
  1107. #endif
  1108. WarnMessage(hWnd,szTemp);
  1109. return FALSE;
  1110. }
  1111. dwBaseWordNum ++;
  1112. if(dwBaseWordNum >(DWORD)(*nPages)*GMEM_PAGESIZE) {
  1113. GlobalUnlock(hWordIndex);
  1114. hWordIndex = IndexReAlloc(hWordIndex,nPages);
  1115. if(!hWordIndex) {
  1116. ProcessError(ERR_OUTOFMEMORY,hWnd,ERR);
  1117. return FALSE;
  1118. }
  1119. lpWordIndex = (LPWORDINDEX) GlobalLock(hWordIndex);
  1120. if( !lpWordIndex ) {
  1121. ProcessError(ERR_GLOBALLOCK,hWnd,ERR);
  1122. return FALSE;
  1123. }
  1124. }
  1125. lpWordIndex[dwBaseWordNum-1].wDBCSLen =(WORD)(nDBCS*sizeof(TCHAR)/2);
  1126. lstrcpy(lpWordIndex[dwBaseWordNum-1].szCode,szCode);
  1127. ConvReconvIndex(hWnd,szDBCS,lpCreateWords,lpWordIndex[dwBaseWordNum-1],lpDescript,lpReConvIndex);
  1128. return TRUE;
  1129. }
  1130. BOOL ConvWriteBaseWord (HANDLE hWnd,
  1131. HANDLE hSRCFile,
  1132. HANDLE hMBFile,
  1133. MAINID MainID,
  1134. DWORD dwOffset,
  1135. LPCREATEWORD lpCreateWords,
  1136. LPRECONVINDEX lpReConvIndex,
  1137. DWORD MaxCodes)
  1138. {
  1139. TCHAR szCode[13],szCreate[13],szDBCS[512],szReCode[13];
  1140. TCHAR szStr[512];
  1141. static TCHAR ReadBuf[MAXREADBUFFER];
  1142. static TCHAR WriteBuf[2*MAXREADBUFFER];
  1143. DWORD dwReadBytes,dwRecLen,i,j,k,dwReadWords=0;
  1144. DWORD dwFileFlag = MainID.dwFileFlag;
  1145. DWORD dwNumWords = MainID.dwNumWords;
  1146. DWORD dwReadSum;
  1147. HWND hDlgItem;
  1148. SetFilePointer(hMBFile,dwOffset,0,FILE_BEGIN);
  1149. SetFilePointer(hSRCFile,0,0,FILE_END);
  1150. SetDlgItemText(hWnd,TM_TOTALINFO,TEXT(""));
  1151. LoadString(NULL,IDS_TOTALINFO,szStr,sizeof(szStr)/sizeof(TCHAR));
  1152. SetDlgItemText(hWnd,TM_TOTAL,szStr);
  1153. LoadString(NULL,IDS_RECONVWORDS,szStr,sizeof(szStr)/sizeof(TCHAR));
  1154. SetDlgItemText(hWnd,TM_CONVINFO,szStr);
  1155. SetDlgItemInt(hWnd,TM_TOTALNUM,dwNumWords,FALSE);
  1156. dwReadSum = 0;
  1157. SetDlgItemInt(hWnd,TM_CONVNUM,dwReadSum,FALSE);
  1158. InvalidateRect(hWnd,NULL,FALSE);
  1159. i=0;
  1160. do {
  1161. ReadFile(hMBFile,ReadBuf,sizeof(ReadBuf),&dwReadBytes,NULL);
  1162. i=0;
  1163. if(dwReadBytes == 0 ) break;
  1164. WriteBuf[0]=0;
  1165. dwReadBytes = dwReadBytes/sizeof(TCHAR);
  1166. while( i < dwReadBytes-1 ) {
  1167. j = (DWORD)ReadBuf[i]+1;
  1168. dwRecLen = j+(DWORD)ReadBuf[i+j]*2/sizeof(TCHAR)+1;
  1169. if( (dwReadBytes*sizeof(TCHAR) < MAXREADBUFFER) ||
  1170. ((i+j) < (dwReadBytes-1) && (i+dwRecLen) < (dwReadBytes-1) ) ) {
  1171. lstrncpy(szCode,ReadBuf[i],&ReadBuf[i+1]);
  1172. szCode[ReadBuf[i]] = 0;
  1173. lstrncpy(szDBCS,ReadBuf[i+j]*2/sizeof(TCHAR),&ReadBuf[i+j+1]);
  1174. szDBCS[ReadBuf[i+j]*2/sizeof(TCHAR)] = 0;
  1175. if(ReadBuf[i+j]==1
  1176. &&(dwFileFlag&FFLG_RULE)) {
  1177. k = EncodeToNo(szDBCS);
  1178. if((long)k>=0 && k < NUM_OF_ENCODE) {
  1179. lstrncpy(szCreate,MaxCodes,&lpCreateWords[k*MaxCodes]);
  1180. lstrncpy(szReCode,MaxCodes,&lpReConvIndex[k*MaxCodes]);
  1181. szCreate[MaxCodes]=0;
  1182. szReCode[MaxCodes]=0;
  1183. if(lstrlen(szCreate) != 0 && lstrcmpi(szCreate,szCode) != 0)
  1184. wsprintf(szStr,TEXT("%s%s %s\r\n"),szDBCS,szCode,szCreate);
  1185. else
  1186. wsprintf(szStr,TEXT("%s%s\r\n"),szDBCS,szCode);
  1187. }
  1188. else
  1189. wsprintf(szStr,TEXT("%s%s\r\n"),szDBCS,szCode);
  1190. }
  1191. else
  1192. wsprintf(szStr,TEXT("%s%s\r\n"),szDBCS,szCode);
  1193. lstrcat(WriteBuf,szStr);
  1194. dwReadWords ++;
  1195. dwReadSum = dwReadWords;
  1196. if(dwReadSum%100 ==0 || dwReadSum == dwNumWords) {
  1197. SetDlgItemInt(hWnd,TM_CONVNUM,dwReadSum,FALSE);
  1198. hDlgItem=GetDlgItem(hWnd,TM_CONVNUM);
  1199. InvalidateRect(hDlgItem,NULL,FALSE);
  1200. }
  1201. i += dwRecLen;
  1202. }
  1203. else {
  1204. SetFilePointer(hMBFile,(i-dwReadBytes)*sizeof(TCHAR),0,FILE_CURRENT);
  1205. break;
  1206. }
  1207. }
  1208. WriteFile(hSRCFile,WriteBuf,lstrlen(WriteBuf)*sizeof(TCHAR),&k,NULL);
  1209. }while (dwReadBytes == MAXREADBUFFER) ;
  1210. return TRUE;
  1211. }
  1212. BOOL ConvBatCreate (HANDLE hWnd,
  1213. DESCRIPTION Descript,
  1214. LPRULE lpRule,
  1215. LPCREATEWORD lpCreateWord,
  1216. LPTSTR szWordStr,
  1217. LPTSTR lpCode)
  1218. {
  1219. int nWordLen=lstrlen(szWordStr)*sizeof(TCHAR)/2;
  1220. DWORD i,j,k,dwCodeLen;
  1221. DWORD dwNumRules = Descript.wNumRules;
  1222. WORD wMaxCodes = Descript.wMaxCodes;
  1223. TCHAR szDBCS[3],szCode[MAXCODELEN+1];
  1224. BOOL bReturn = FALSE;
  1225. lpCode[0] = 0;
  1226. if(dwNumRules == 0) {
  1227. ProcessError(ERR_NORULE,hWnd,ERR);
  1228. return FALSE;
  1229. }
  1230. for(i=0; i<dwNumRules; i++)
  1231. if( (lpRule[i].byLogicOpra == 0 && nWordLen == lpRule[i].byLength)
  1232. ||(lpRule[i].byLogicOpra == 1 && nWordLen >= lpRule[i].byLength)
  1233. ||(lpRule[i].byLogicOpra == 2 && nWordLen <= lpRule[i].byLength) ) {
  1234. int retCodeLen = 0;
  1235. for(j=0; j<lpRule[i].wNumCodeUnits; j++) {
  1236. k = lpRule[i].CodeUnit[j].wDBCSPosition;
  1237. if(k > (DWORD)nWordLen) k = (DWORD)nWordLen;
  1238. if(lpRule[i].CodeUnit[j].dwDirectMode == 0)
  1239. lstrncpy(szDBCS,2,&szWordStr[2*(k-1)]);
  1240. else
  1241. lstrncpy(szDBCS,2,&szWordStr[2*(nWordLen-k)]);
  1242. szDBCS[2] = 0;
  1243. k = EncodeToNo(szDBCS);
  1244. if((long)k>=0 && k< NUM_OF_ENCODE )
  1245. {
  1246. lstrncpy(szCode,wMaxCodes,&lpCreateWord[wMaxCodes*k]);
  1247. szCode[wMaxCodes] = 0;
  1248. dwCodeLen = lstrlen(szCode);
  1249. k = lpRule[i].CodeUnit[j].wCodePosition;
  1250. if(k == 0)
  1251. {
  1252. if(retCodeLen + dwCodeLen > Descript.wMaxCodes)
  1253. szCode[Descript.wMaxCodes - retCodeLen] = 0;
  1254. lstrcat(lpCode,szCode);
  1255. }
  1256. else
  1257. {
  1258. if(k > dwCodeLen) k = dwCodeLen;
  1259. lpCode[j] = (szCode[k-1] == 0)?((k > 1)? szCode[k-2]:Descript.szUsedCode[0]):szCode[k-1];
  1260. }
  1261. }
  1262. else
  1263. lpCode[j] = (j > 0)?lpCode[j-1]:Descript.szUsedCode[0];
  1264. retCodeLen = lstrlen(lpCode);
  1265. }
  1266. bReturn = TRUE;
  1267. break;
  1268. }
  1269. if(!bReturn)
  1270. ProcessError(ERR_NOTDEFRULE,hWnd,ERR);
  1271. lpCode[wMaxCodes] = 0;
  1272. return bReturn;
  1273. }
  1274. BOOL searchPos(LPWORDINDEX lpWordIndex, DWORD dwNumWords)
  1275. {
  1276. DWORD i, retPos;
  1277. WORDINDEX InsWordIndex = lpWordIndex[dwNumWords-1];
  1278. if(dwNumWords == 1)
  1279. return TRUE;
  1280. retPos = bSearch(lpWordIndex, 0, dwNumWords-2, InsWordIndex);
  1281. if(retPos == dwNumWords - 1)
  1282. return TRUE;
  1283. memmove(&lpWordIndex[retPos+1], &lpWordIndex[retPos],
  1284. sizeof(WORDINDEX)*(dwNumWords - 1 - retPos) );
  1285. lpWordIndex[retPos] = InsWordIndex;
  1286. return TRUE;
  1287. }
  1288. DWORD bSearch(LPWORDINDEX lpWordIndex, DWORD left, DWORD right, WORDINDEX InsWordIndex)
  1289. {
  1290. WORDINDEX MidWordIndex;
  1291. DWORD mid;
  1292. if(left > right)
  1293. return left;
  1294. mid = (left + right)/2;
  1295. MidWordIndex = lpWordIndex[mid];
  1296. if(_wcsicmp(MidWordIndex.szCode, InsWordIndex.szCode) <= 0)
  1297. {
  1298. if(mid == right || _wcsicmp(lpWordIndex[mid+1].szCode, InsWordIndex.szCode) > 0)
  1299. return mid+1;
  1300. else
  1301. return bSearch(lpWordIndex, mid+1, right, InsWordIndex);
  1302. }
  1303. else
  1304. {
  1305. if(mid == left || _wcsicmp(lpWordIndex[mid-1].szCode, InsWordIndex.szCode) <= 0)
  1306. return mid;
  1307. else
  1308. // return bSearch(lpWordIndex, left, mid, InsWordIndex);
  1309. return bSearch(lpWordIndex, left, mid-1, InsWordIndex);
  1310. }
  1311. return 0;
  1312. }
  1313. //***** end 95.10.17