Leaked source code of windows server 2003
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.

1673 lines
50 KiB

  1. /*************************************************
  2. * basefunc.c *
  3. * *
  4. * Copyright (C) 1995-1999 Microsoft Inc. *
  5. * *
  6. *************************************************/
  7. #include <io.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #include <windows.h>
  12. #include <string.h>
  13. #include <winbase.h>
  14. #include <commdlg.h>
  15. #include "conv.h"
  16. #include "propshet.h"
  17. #define MAX_VALUE_NAME 256
  18. #ifdef UNICODE
  19. TCHAR szCaption[] = {0x8F93, 0x5165, 0x6CD5, 0x751F, 0x6210, 0x5668, 0x0000};
  20. #else
  21. BYTE szCaption[] = "���뷨������"
  22. #endif
  23. /**** delete all spaces of string *****/
  24. void DelSpace(LPTSTR szStr)
  25. {
  26. TCHAR szStr1[512];
  27. int len,count=0;
  28. register int i=0;
  29. len = lstrlen(szStr);
  30. if ( len > (ARRAYSIZE(szStr1) -1) )
  31. len = ARRAYSIZE(szStr1) - 1;
  32. do {
  33. if(szStr[i] == 32||szStr[i] == TEXT('\t')) continue;
  34. szStr1[count] = szStr[i];
  35. if(szStr1[count] >= TEXT('A') && szStr1[count] <= TEXT('Z'))
  36. szStr1[count] |= 0x20;
  37. count ++;
  38. } while(i++<len);
  39. szStr1[count] = 0;
  40. lstrcpy(szStr,szStr1);
  41. }
  42. //***** start
  43. int GetEntryString(LPTSTR szSrcStr,
  44. LPTSTR szEntryName,
  45. LPTSTR szDefaultStr,
  46. LPTSTR szGetStr,
  47. DWORD dwLength
  48. )
  49. {
  50. LPTSTR lpStr;
  51. TCHAR szName[256];
  52. GetEntryName(szSrcStr, szName);
  53. lpStr = _tcschr(szSrcStr,TEXT('='));
  54. if(lpStr == NULL)
  55. {
  56. lstrcpy(szGetStr, szDefaultStr);
  57. return TRUE;
  58. }
  59. else if(lstrcmpi(szEntryName, szName) != 0)
  60. {
  61. lstrcpy(szGetStr, szDefaultStr);
  62. return FALSE;
  63. }
  64. else
  65. {
  66. lstrcpy(szGetStr, lpStr+1);
  67. return TRUE;
  68. }
  69. }
  70. int GetEntryInt(LPTSTR szSrcStr,
  71. LPTSTR szEntryName,
  72. int nDefault,
  73. LPINT fnPrivateInt
  74. )
  75. {
  76. LPTSTR lpStr;
  77. TCHAR szName[256];
  78. GetEntryName(szSrcStr, szName);
  79. lpStr = _tcschr(szSrcStr,TEXT('='));
  80. if(lpStr == NULL)
  81. {
  82. *fnPrivateInt = nDefault;
  83. return TRUE;
  84. }
  85. else if( lstrcmpi(szEntryName, szName) != 0)
  86. {
  87. *fnPrivateInt = nDefault;
  88. return FALSE;
  89. }
  90. else
  91. {
  92. StringCchCopy(szName, ARRAYSIZE(szName), lpStr+1);
  93. *fnPrivateInt = _ttoi(szName);
  94. return TRUE;
  95. }
  96. }
  97. void GetEntryName(LPTSTR szSrcStr, LPTSTR szEntryName)
  98. {
  99. LPTSTR lpStr;
  100. lstrcpy(szEntryName,szSrcStr);
  101. if((lpStr = _tcschr(szEntryName,TEXT('='))) == NULL)
  102. szEntryName[0] = 0;
  103. else
  104. *lpStr = 0;
  105. }
  106. //***** end 95.10.11
  107. BOOL ParseDBCSstr(HWND hWnd,
  108. TCHAR *szInputStr,
  109. LPTSTR szDBCS,
  110. LPTSTR szCodeStr,
  111. LPTSTR szCreateStr,
  112. WORD wMaxCodes)
  113. {
  114. int i, len, nDBCS = 0, nCodes = 0;
  115. TCHAR szStr[512], szTmpStr[256];
  116. #ifdef UNICODE
  117. len = lstrlen(szInputStr);
  118. for(i=0; i<len-1; i++) {
  119. if(szInputStr[i] > 0x100)
  120. nDBCS++;
  121. else
  122. break;
  123. }
  124. #else
  125. len = lstrlen(szInputStr);
  126. for(i=0; i<len-1; i+= 2) {
  127. if( ((UCHAR)szInputStr[i] < 0 || (UCHAR)szInputStr[i] > (UCHAR)0x80) &&
  128. ((UCHAR)szInputStr[i+1] >= 0x40 && (UCHAR)szInputStr[i+1] <= (UCHAR)0xfe &&
  129. (UCHAR)szInputStr[i+1] != (UCHAR)0x7f) )
  130. nDBCS += 2;
  131. else
  132. break;
  133. }
  134. #endif
  135. if(nDBCS == 0)
  136. {
  137. /* LoadString(NULL, IDS_NOTEXISTDBCS, szTmpStr, sizeof(szTmpStr));
  138. wsprintf(szStr,"\'%s%lu)",szTmpStr,dwLineNo);
  139. FatalMessage(hWnd,szStr);*/
  140. return -1;
  141. }
  142. lstrncpy(szDBCS,nDBCS,szInputStr);
  143. szDBCS[nDBCS] = 0;
  144. lstrcpy(szStr,&szInputStr[nDBCS]);
  145. trim(szStr);
  146. len = lstrlen(szStr);
  147. if(len > 0)
  148. {
  149. for(i = 0; i<len; i++)
  150. {
  151. if((int)szStr[i] == 32 || szStr[i] == TEXT('\t'))
  152. break;
  153. nCodes++;
  154. }
  155. }
  156. else
  157. nCodes = 0;
  158. if(nCodes > wMaxCodes) {
  159. LoadString(NULL, IDS_DBCSCODELEN, szTmpStr, sizeof(szTmpStr)/sizeof(TCHAR));
  160. #ifdef UNICODE
  161. {
  162. TCHAR UniTmp[] = {0x884C, 0x0000};
  163. StringCchPrintf(szStr, ARRAYSIZE(szStr), TEXT("\'%ws\'%ws%d!(%ws:%lu)"),
  164. szDBCS,szTmpStr,wMaxCodes,UniTmp,dwLineNo);
  165. }
  166. #else
  167. StringCchPrintf(szStr,ARRAYSIZE(szStr), "\'%s\'%s%d!(��:%lu)",
  168. szDBCS,szTmpStr,wMaxCodes,dwLineNo);
  169. #endif
  170. FatalMessage(hWnd,szStr);
  171. return FALSE;
  172. //szStr[wMaxCodes] = 0;
  173. }
  174. lstrncpy(szCodeStr,nCodes, szStr);
  175. szCodeStr[nCodes] = 0;
  176. DelSpace(szCodeStr);
  177. lstrcpy(szCreateStr,&szStr[nCodes]);
  178. szCreateStr[MAXCODELEN] = 0;
  179. DelSpace(szCreateStr);
  180. return TRUE;
  181. }
  182. /**** delete spaces of string's head and tail *****/
  183. void trim(LPTSTR szStr)
  184. {
  185. register int i=0;
  186. UINT len ;
  187. while(szStr[i] == 32 || szStr[i] == TEXT('\t'))
  188. i++;
  189. lstrcpy(szStr,&szStr[i]);
  190. len = lstrlen(szStr);
  191. if(len == 0) return;
  192. i = 1;
  193. while(szStr[len-i] == 32
  194. || szStr[len-i] == TEXT('\r')
  195. || szStr[len-i] == TEXT('\n')
  196. || szStr[len-i] == TEXT('\t')
  197. || szStr[len-i] == 0)
  198. i++;
  199. szStr[len-i+1] = 0;
  200. len = lstrlen(szStr);
  201. for(i=0; i<(int)len; i++)
  202. {
  203. if(szStr[i] > 0x100)
  204. continue;
  205. if(szStr[i] >= TEXT('A') && szStr[i] <= TEXT('Z'))
  206. szStr[i] |= 0x20;
  207. }
  208. }
  209. void fnsplit(LPCTSTR szFullPath, LPTSTR szFileName)
  210. {
  211. LPTSTR lpString;
  212. #ifdef UNICODE
  213. if((lpString=wcsrchr(szFullPath,TEXT('\\')))!=NULL)
  214. #else
  215. if((lpString=strrchr(szFullPath,TEXT('\\')))!=NULL)
  216. #endif
  217. lstrcpy(szFileName,lpString+1);
  218. else
  219. lstrcpy(szFileName,szFullPath);
  220. }
  221. BOOL ErrMessage(HANDLE hWnd,LPTSTR lpText)
  222. {
  223. int RetValue;
  224. RetValue = MessageBox(hWnd,
  225. lpText,
  226. szCaption,
  227. MB_ICONEXCLAMATION|MB_YESNO|MB_DEFBUTTON2);
  228. if(RetValue == IDYES)
  229. return TRUE;
  230. else
  231. return FALSE;
  232. }
  233. VOID WarnMessage(HANDLE hWnd,LPTSTR lpText)
  234. {
  235. MessageBox(hWnd,lpText,szCaption,MB_ICONEXCLAMATION|MB_OK);
  236. }
  237. VOID InfoMessage(HANDLE hWnd,LPTSTR lpText)
  238. {
  239. MessageBox(hWnd,lpText,szCaption,MB_ICONINFORMATION|MB_OK);
  240. }
  241. VOID FatalMessage(HANDLE hWnd,LPTSTR lpText)
  242. {
  243. MessageBox(hWnd,lpText,szCaption,MB_ICONSTOP|MB_OK);
  244. }
  245. void lstrncpy(LPTSTR lpDest,int nCount,LPTSTR lpSrc)
  246. {
  247. register int i;
  248. BOOL bEnd = FALSE;
  249. for(i=0; i<nCount; i++) {
  250. if(lpSrc[i] == 0)
  251. bEnd = TRUE;
  252. if(bEnd)
  253. lpDest[i] = 0;
  254. else
  255. lpDest[i] = lpSrc[i];
  256. }
  257. }
  258. void lstrncpy0(LPTSTR lpDest,int nCount,LPTSTR lpSrc)
  259. {
  260. register int i;
  261. BOOL bEnd = FALSE;
  262. for(i=0; i<nCount; i++)
  263. lpDest[i] = lpSrc[i];
  264. }
  265. /**************************************************************************
  266. * HANDLE CreateMapFile(HANDLE hWnd,char *MapFileName)
  267. *
  268. * Purpose: Create a Map file to map named share memory
  269. *
  270. * Inputs: hWnd - parent window's handle
  271. * *MapFileName - pointer to map file name
  272. *
  273. * Returns: MapFileHandle - a handle to the file
  274. * or NULL if failure
  275. *
  276. * Calls: CreateFile, ErrorOut
  277. *
  278. * History:
  279. *
  280. *
  281. \**************************************************************************/
  282. HANDLE CreateMapFile(HANDLE hWnd,TCHAR *MapFileName)
  283. {
  284. HANDLE MapFileHandle;
  285. MapFileHandle= CreateFile(MapFileName,
  286. GENERIC_READ | GENERIC_WRITE,
  287. FILE_SHARE_READ | FILE_SHARE_WRITE,
  288. NULL,
  289. CREATE_ALWAYS,
  290. FILE_ATTRIBUTE_NORMAL /* | STANDARD_RIGHTS_REQUIRED |
  291. FILE_MAP_WRITE | FILE_MAP_READ */,
  292. NULL);
  293. if (MapFileHandle == (HANDLE)-1)
  294. {
  295. ErrorOut(hWnd,TEXT("CreateFile"));
  296. return(NULL);
  297. }
  298. else
  299. return(MapFileHandle);
  300. }
  301. /**************************************************************************
  302. * HANDLE CreateMap(HANDLE hWnd,HANDLE *FileToBeMapped, char MapName[128] )
  303. *
  304. * Purpose: Create File Mapping object using the open file handle
  305. *
  306. * Inputs: hWnd - parent window's handle
  307. * *FileToBeMapped - pointer to the file handle
  308. *
  309. * Returns: MapHandle - handle to the file mapping object
  310. * or NULL if failure
  311. *
  312. * Calls: CreateFileMapping, ErrorOut
  313. *
  314. * History:
  315. *
  316. *
  317. \**************************************************************************/
  318. HANDLE CreateMap(HANDLE hWnd,HANDLE *FileToBeMapped, TCHAR MapName[128])
  319. {
  320. HANDLE MapHandle;
  321. MapHandle= CreateFileMapping(*FileToBeMapped,
  322. NULL,
  323. PAGE_READWRITE,
  324. 0,
  325. 4096,
  326. MapName);
  327. if (MapHandle == NULL)
  328. {
  329. ErrorOut(hWnd,TEXT("CreateFileMapping"));
  330. return(NULL);
  331. }
  332. else
  333. return(MapHandle);
  334. }
  335. /**************************************************************************
  336. * LPVOID MapView(HANDLE *hMap)
  337. *
  338. * Purpose: Map the file mapping object into address space
  339. *
  340. * Inputs: *hMap - pointer to the mapping object
  341. *
  342. * Returns: MappedPointer - pointer to the address space that the
  343. * object is mapped into
  344. * or NULL if failure
  345. *
  346. * Calls: MapViewOfFile, ErrorOut
  347. *
  348. * History:
  349. *
  350. *
  351. \**************************************************************************/
  352. LPVOID MapView(HANDLE hWnd,HANDLE *hMap)
  353. {
  354. LPVOID MappedPointer;
  355. MappedPointer= MapViewOfFile(*hMap,
  356. FILE_MAP_WRITE | FILE_MAP_READ,
  357. 0,
  358. 0,
  359. 4096);
  360. if (MappedPointer == NULL)
  361. {
  362. ErrorOut(hWnd,TEXT("MapViewOfFile"));
  363. return(NULL);
  364. }
  365. else
  366. return(MappedPointer);
  367. }
  368. /************************************************************************
  369. * void ErrorOut(HANDLE ghwndMain,char errstring[128])
  370. *
  371. * Purpose: Print out an meainful error code by means of
  372. * GetLastError and printf
  373. *
  374. * Inputs: ghwndMain - WinMain's HANDLE
  375. * errstring - the action that failed, passed by the
  376. * calling proc.
  377. *
  378. * Returns: none
  379. *
  380. * Calls: GetLastError
  381. *
  382. * History:
  383. *
  384. *
  385. \************************************************************************/
  386. void ErrorOut(HANDLE ghwndMain,TCHAR errstring[128])
  387. {
  388. DWORD Error;
  389. TCHAR str[80];
  390. Error= GetLastError();
  391. StringCchPrintf((LPTSTR) str, ARRAYSIZE(str), TEXT("Error on %s = %d\n"), errstring, Error);
  392. MessageBox(ghwndMain, (LPTSTR)str, TEXT("Error"), MB_OK);
  393. }
  394. /*************************************************************************
  395. * HANDLE OpenMap(HANDLE hWnd,char MapName[128])
  396. *
  397. * Purpose: Open the mapping object pointed to by MapName
  398. *
  399. * Inputs: hWnd - parent window's handle
  400. * * MapName - pointer to map file name
  401. *
  402. * Returns: handle to mapped object or NULL if failure
  403. *
  404. * Calls: OpenFileMapping, ErrorOut
  405. *
  406. * History:
  407. *
  408. *
  409. \*************************************************************************/
  410. HANDLE OpenMap(HANDLE hWnd,TCHAR MapName[128])
  411. {
  412. HANDLE hAMap;
  413. hAMap= OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE,
  414. TRUE,
  415. MapName);
  416. if (hAMap == NULL)
  417. {
  418. ErrorOut(hWnd,TEXT("OpenFileMapping"));
  419. return(NULL);
  420. }
  421. else
  422. return(hAMap);
  423. }
  424. /*************************************************************************
  425. * HANDLE Create_File(HANDLE hWnd,
  426. LPSTR lpFileName,
  427. DWORD fdwAccess,
  428. DWORD fdwCreate)
  429. *
  430. * Purpose: Open the object pointed to by lpFileName
  431. *
  432. * Inputs: hWnd - parent window's handle
  433. * lpFileName - pointer to file name
  434. * fdwAccess - access(read-write)mode
  435. * fdwCreate - how to create
  436. *
  437. * Returns: handle to object or NULL if failure
  438. *
  439. * History:
  440. *
  441. *
  442. \*************************************************************************/
  443. HANDLE Create_File(HANDLE hWnd,
  444. LPTSTR lpFileName,
  445. DWORD fdwAccess,
  446. DWORD fdwCreate)
  447. {
  448. HANDLE hFile;
  449. TCHAR szStr[256],szStr1[256];
  450. int RetValue;
  451. RetValue=CREATE_ALWAYS;
  452. if(fdwCreate == CREATE_ALWAYS) {
  453. if(_taccess(lpFileName,0)==0) {
  454. LoadString(NULL,IDS_OVERWRITE,szStr, sizeof(szStr)/sizeof(TCHAR));
  455. StringCchPrintf(szStr1,ARRAYSIZE(szStr1), TEXT("\'%s\' %s"),lpFileName,szStr);
  456. RetValue = MessageBox(hWnd,
  457. szStr1,
  458. szCaption,
  459. MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2);
  460. if(RetValue != IDYES)
  461. //*if(!SaveFileAs(hWnd,lpFileName))
  462. return (HANDLE)-1;
  463. }
  464. }
  465. else if(fdwCreate == OPEN_EXISTING)
  466. {
  467. if(_taccess(lpFileName,0)) {
  468. return (HANDLE)-1;
  469. }
  470. }
  471. hFile = CreateFile(lpFileName,fdwAccess,FILE_SHARE_READ,NULL,
  472. fdwCreate,0,NULL);
  473. if (hFile == (HANDLE)-1) {
  474. LoadString(NULL,IDS_FILEOPEN,szStr, sizeof(szStr)/sizeof(TCHAR));
  475. StringCchPrintf(szStr1, ARRAYSIZE(szStr1), TEXT("\'%s\' %s\n"),lpFileName,szStr);
  476. FatalMessage(hWnd,szStr1);
  477. }
  478. return hFile;
  479. }
  480. BOOL SaveFileAs(HWND hwnd, LPTSTR szFilename) {
  481. OPENFILENAME ofn;
  482. TCHAR szFile[256], szFileTitle[256];
  483. static TCHAR *szFilter;
  484. // szFilter = "�����ļ�(*.*)\0\0";
  485. szFilter = TEXT("All files (*.*)\0\0");
  486. lstrcpy(szFile, TEXT("*.*\0"));
  487. ofn.lStructSize = sizeof(OPENFILENAME);
  488. ofn.hwndOwner = hwnd;
  489. ofn.lpstrFilter = szFilter;
  490. ofn.lpstrCustomFilter = (LPTSTR) NULL;
  491. ofn.nMaxCustFilter = 0L;
  492. ofn.nFilterIndex = 0L;
  493. ofn.lpstrFile = szFilename;
  494. ofn.nMaxFile = sizeof(szFile)/sizeof(TCHAR);
  495. ofn.lpstrFileTitle = szFileTitle;
  496. ofn.nMaxFileTitle = sizeof(szFileTitle)/sizeof(TCHAR);
  497. ofn.lpstrInitialDir = NULL;
  498. ofn.lpstrTitle = TEXT("Save file As");
  499. ofn.Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT;
  500. ofn.nFileOffset = 0;
  501. ofn.nFileExtension = 0;
  502. ofn.lpstrDefExt = (LPTSTR)NULL;
  503. if (!GetSaveFileName(&ofn))
  504. return 0L;
  505. return 1L;
  506. }
  507. /****************************************************************************
  508. *
  509. * FUNCTION: ProcessCDError(DWORD)
  510. *
  511. * PURPOSE: Processes errors from the conversion functions.
  512. *
  513. * COMMENTS:
  514. *
  515. * This function is called whenever a conversion function
  516. * fails. The string is loaded and displayed for the user.
  517. *
  518. * RETURN VALUES:
  519. * void.
  520. *
  521. * HISTORY:
  522. * 03-04-95 Yehfew Tie Created.
  523. *
  524. ****************************************************************************/
  525. void ProcessError(DWORD dwErrorCode, HWND hWnd,DWORD ErrorLevel)
  526. {
  527. WORD wStringID;
  528. TCHAR buf[256];
  529. switch(dwErrorCode)
  530. {
  531. case ERR_MBNAME: wStringID=IDS_MBNAME; break;
  532. case ERR_FILENOTEXIST: wStringID=IDS_FILENOTEXIST; break;
  533. case ERR_FILENOTOPEN: wStringID=IDS_FILEOPEN; break;
  534. case ERR_OUTOFMEMORY: wStringID=IDS_MEMORY; break;
  535. case ERR_GLOBALLOCK: wStringID=IDS_GLOBALLOCK; break;
  536. case ERR_IMEUSE: wStringID=IDS_IMEUSE; break;
  537. case ERR_MAXCODES: wStringID=IDS_MAXCODES; break;
  538. case ERR_ELEMENT: wStringID=IDS_ELEMENT; break;
  539. case ERR_USEDCODE: wStringID=IDS_USEDCODE; break;
  540. case ERR_WILDCHAR: wStringID=IDS_WILDCHAR; break;
  541. case ERR_RULEHEADER: wStringID=IDS_RULEHEAD; break;
  542. case ERR_RULELOGICOPRA: wStringID=IDS_RULELOGIC; break;
  543. case ERR_RULEWORDLEN: wStringID=IDS_RULEWORDLEN; break;
  544. case ERR_RULEEQUAL: wStringID=IDS_RULEEQUAL; break;
  545. case ERR_RULEDIRECTMODE: wStringID=IDS_RULEDIRECT; break;
  546. case ERR_RULEDBCSPOS: wStringID=IDS_RULEDBCSPOS; break;
  547. case ERR_RULECODEPOS: wStringID=IDS_RULECODEPOS; break;
  548. case ERR_NORULE: wStringID=IDS_NORULE; break;
  549. case ERR_NOTDEFRULE: wStringID=IDS_NOTDEFRULE; break;
  550. case ERR_RULENUM: wStringID=IDS_RULENUM; break;
  551. case ERR_DBCSCODE: wStringID=IDS_DBCSCODE; break;
  552. case ERR_CODEUNITNOTEXIST: wStringID=IDS_CODEUNIT; break;
  553. case ERR_CREATECODE: wStringID=IDS_CREATECODE; break;
  554. case ERR_CREATENOTEXIST: wStringID=IDS_CRTCODEEMPTY; break;
  555. case ERR_CODEEMPTY: wStringID=IDS_CODEEMPTY; break;
  556. // case ERR_SINGLECODEWORDDOUBLE: wStringID=IDS_SCODEREP; break;
  557. case ERR_SBCS_IN_DBCS: wStringID=IDS_SBCSINDBCS; break;
  558. case ERR_GB2312NOTENTIRE: wStringID=IDS_GB2312; break;
  559. case ERR_USERWORDLEN: wStringID=IDS_USERWORDLEN; break;
  560. case ERR_WRITEID: wStringID=IDS_WRID; break;
  561. case ERR_WRITEMAININDEX: wStringID=IDS_WRMAININDEX; break;
  562. case ERR_WRITEDESCRIPT: wStringID=IDS_WRDESCRIPT; break;
  563. case ERR_WRITERULE: wStringID=IDS_WRRULE; break;
  564. case ERR_READID: wStringID=IDS_READID; break;
  565. case ERR_READMAININDEX: wStringID=IDS_RDMAININDEX; break;
  566. case ERR_READDESCRIPT: wStringID=IDS_RDDESCRIPT; break;
  567. case ERR_READRULE: wStringID=IDS_RDRULE; break;
  568. case ERR_DESCRIPTSEG: wStringID=IDS_DESCRIPTSEG; break;
  569. case ERR_RULESEG: wStringID=IDS_RULESEG; break;
  570. case ERR_TEXTSEG: wStringID=IDS_TEXTSEG; break;
  571. case ERR_TOOMANYUSERWORD: wStringID=IDS_TOOMANYUSERWORD; break;
  572. case ERR_OVERWRITE: wStringID=IDS_OVERWRITE; break;
  573. case ERR_IMENAMEENTRY: wStringID=IDS_IMENAMEENTRY; break;
  574. case ERR_MAXCODESENTRY: wStringID=IDS_MAXCODESENTRY; break;
  575. case ERR_ELEMENTENTRY: wStringID=IDS_ELEMENTENTRY; break;
  576. case ERR_USEDCODEENTRY: wStringID=IDS_USEDCODEENTRY; break;
  577. case ERR_NUMRULEENTRY: wStringID=IDS_NUMRULESENTRY; break;
  578. case ERR_CONVEND: wStringID=IDS_CONVEND; break;
  579. case ERR_RECONVEND: wStringID=IDS_RECONVEND; break;
  580. case ERR_SORTEND: wStringID=IDS_SORTEND; break;
  581. case ERR_VERSION: wStringID=IDS_VERSIONEMPTY; break;
  582. case ERR_GROUP: wStringID=IDS_GROUP; break;
  583. case 0: //User may have hit CANCEL or we got a *very* random error
  584. return;
  585. default:
  586. wStringID=IDS_UNKNOWNERROR;
  587. }
  588. LoadString(NULL, wStringID, buf, sizeof(buf)/sizeof(TCHAR));
  589. switch(ErrorLevel) {
  590. case INFO:
  591. MessageBox(hWnd, buf, szCaption, MB_OK|MB_ICONINFORMATION);
  592. break;
  593. case WARNING:
  594. MessageBox(hWnd, buf, szCaption, MB_OK|MB_ICONEXCLAMATION);
  595. break;
  596. case ERR:
  597. default:
  598. MessageBox(hWnd, buf, szCaption, MB_OK|MB_ICONSTOP);
  599. break;
  600. }
  601. return;
  602. }
  603. HANDLE IndexReAlloc(HANDLE hMem,LPINT nPages)
  604. {
  605. HANDLE hReMem;
  606. DWORD dwSize;
  607. *nPages++;
  608. dwSize = (DWORD)(*nPages)*GMEM_PAGESIZE*sizeof(WORDINDEX);
  609. GlobalUnlock(hMem);
  610. hReMem = GlobalReAlloc(hMem, dwSize, GMEM_MODIFY|GMEM_MOVEABLE);
  611. return hReMem;
  612. }
  613. BOOL ConvSort(HANDLE hWnd,LPWORDINDEX lpWordIndex,int nCount)
  614. {
  615. int i;
  616. TCHAR szStr[256];
  617. HANDLE hDlgItem;
  618. SetCursor (LoadCursor (NULL, IDC_WAIT));
  619. SetDlgItemText (hWnd,TM_TOTALINFO,TEXT(""));
  620. LoadString (NULL,IDS_TOTALINFO,szStr,sizeof(szStr)/sizeof(TCHAR));
  621. SetDlgItemText (hWnd,TM_TOTAL,szStr);
  622. LoadString(NULL,IDS_SORTWORDS,szStr,sizeof(szStr)/sizeof(TCHAR));
  623. SetDlgItemText (hWnd,TM_CONVINFO,szStr);
  624. SetDlgItemInt (hWnd,TM_TOTALNUM, nCount, FALSE);
  625. i = 0;
  626. SetDlgItemInt (hWnd,TM_CONVNUM,i,FALSE);
  627. InvalidateRect (hWnd,NULL,FALSE);
  628. hDlgItem = GetDlgItem(hWnd, TM_CONVNUM);
  629. for(i=1 ; i<= nCount; i++)
  630. {
  631. searchPos(lpWordIndex, i);
  632. if(i%100 == 0 || i == nCount)
  633. {
  634. SetDlgItemInt (hWnd,TM_CONVNUM,i,FALSE);
  635. InvalidateRect(hDlgItem,NULL,FALSE);
  636. }
  637. }
  638. SetCursor (LoadCursor (NULL, IDC_ARROW));
  639. return TRUE;
  640. }
  641. /******** Quick sort structure function ********/
  642. void qSort(LPWORDINDEX item, DWORD left,DWORD right)
  643. {
  644. int i,j,k,mid;
  645. WORDINDEX MidWord,SwapWord;
  646. if ( left > right ) return;
  647. i=(int)left;
  648. j=(int)right;
  649. mid = (i+j)/2;
  650. MidWord = item[mid];
  651. do {
  652. while( i < (int)right)
  653. {
  654. k = lstrcmpi(MidWord.szCode,item[i].szCode);
  655. if ( ( k > 0 ) || ( k==0 && i< mid ) )
  656. i++;
  657. else
  658. break;
  659. }
  660. while( j > (int)left )
  661. {
  662. k=lstrcmpi(MidWord.szCode,item[j].szCode);
  663. if ( ( k < 0 ) || (k == 0 && j > mid) )
  664. j--;
  665. else
  666. break;
  667. }
  668. if(i <= j) {
  669. SwapWord = item[i];
  670. item[i] = item[j];
  671. item[j] = SwapWord;
  672. i++;
  673. j--;
  674. }
  675. } while(i <= j);
  676. if((int)left < j) qSort(item,left,j);
  677. if(i < (int)right) qSort(item,i,right);
  678. }
  679. /******** Quick sort char function ********/
  680. void qSortChar(LPTSTR item, DWORD left,DWORD right)
  681. {
  682. int i,j,mid;
  683. TCHAR MidChar,SwapChar;
  684. if ( left > right ) return ;
  685. i=(int)left;
  686. j=(int)right;
  687. mid = (i+j)/2;
  688. MidChar = item[mid];
  689. do {
  690. while( ( MidChar > item[i] && i < (int)right)
  691. ||(MidChar == item[i] && i != mid) )
  692. i++;
  693. while( (MidChar <item[j] && j > (int)left)
  694. ||(MidChar == item[j] && j!=mid) )
  695. j--;
  696. if(i <= j) {
  697. SwapChar = item[i];
  698. item[i] = item[j];
  699. item[j] = SwapChar;
  700. i++;
  701. j--;
  702. }
  703. } while(i <= j);
  704. if((int)left < j) qSortChar(item,left,j);
  705. if(i < (int)right) qSortChar(item,i,right);
  706. }
  707. DWORD EncodeToNo(LPTSTR szDBCS)
  708. {
  709. WORD wCode;
  710. LPENCODEAREA lpEncode;
  711. DWORD dwNo = 0xffffffff, i;
  712. lpEncode = (LPENCODEAREA) GlobalLock(hEncode);
  713. #ifdef UNICODE
  714. wCode = szDBCS[0];
  715. #else
  716. wCode = (WORD)((UCHAR)szDBCS[0])*256 + (WORD)(UCHAR)szDBCS[1];
  717. #endif
  718. for( i = NUMENCODEAREA -1; (long)i>=0; i--) {
  719. if(wCode >= lpEncode[i].StartEncode) {
  720. dwNo = lpEncode[i].PreCount;
  721. dwNo += wCode - lpEncode[i].StartEncode;
  722. break;
  723. }
  724. }
  725. if(dwNo > NUM_OF_ENCODE)
  726. dwNo = 0xffffffff;
  727. GlobalUnlock(hEncode);
  728. return dwNo;
  729. }
  730. DWORD EncodeToGBNo(UCHAR szDBCS[3])
  731. {
  732. DWORD dwNo;
  733. if(szDBCS[0] < 0xa1 || szDBCS[1] < 0xa1 || szDBCS[1] > 0xfe)
  734. {
  735. dwNo = 0xffffffff;
  736. return dwNo;
  737. }
  738. dwNo = (DWORD)(szDBCS[0]-0xa0-16) ;
  739. dwNo = dwNo * 94 + (DWORD)(szDBCS[1]-0xa0-1) - ((dwNo > 39)?5:0);
  740. return dwNo;
  741. }
  742. void NoToEncode(DWORD dwNo,LPBYTE szDBCS, DWORD dwNumArea,
  743. LPENCODEAREA lpEncode)
  744. {
  745. DWORD Value,i;
  746. for( i =dwNumArea-1; (long)i>=0; i--) {
  747. if(dwNo >= lpEncode[i].PreCount) {
  748. Value = dwNo-lpEncode[i].PreCount;
  749. Value += lpEncode[i].StartEncode;
  750. #ifdef UNICODE
  751. szDBCS[0] = (UCHAR)(Value&0xff);
  752. szDBCS[1] = (UCHAR)((Value>>8)&0xff);
  753. #else
  754. szDBCS[0] = (UCHAR)((Value>>8)&0xff);
  755. szDBCS[1] = (UCHAR)(Value&0xff);
  756. #endif
  757. break;
  758. }
  759. }
  760. }
  761. void NoToGB2312Code(DWORD dwNo,LPBYTE szDBCS, DWORD dwNumArea)
  762. {
  763. DWORD Value;
  764. szDBCS[0] = 0;
  765. szDBCS[1] = 0;
  766. if(dwNo > GB2312WORDNUM)
  767. return;
  768. Value = dwNo + ((dwNo >= 3755)?5:0);
  769. szDBCS[0] = (BYTE)(Value/94 +16 +0xa0);
  770. szDBCS[1] = (BYTE)(Value%94 + 0xa1);
  771. }
  772. void RuleToText(LPRULE lpRule, LPTSTR szStr)
  773. {
  774. DWORD dwTemp;
  775. int nCount=0,i;
  776. szStr[0]=TEXT('c');
  777. dwTemp = lpRule->byLogicOpra;
  778. szStr[1]=(dwTemp==0)?TEXT('e'):(dwTemp==1)?TEXT('a'):TEXT('b');
  779. dwTemp = lpRule->byLength;
  780. szStr[2]=(dwTemp<10)?TEXT('0')+(TCHAR)dwTemp:TEXT('a')+(TCHAR)dwTemp-10;
  781. szStr[3]=TEXT('=');
  782. nCount = lpRule->wNumCodeUnits;
  783. for(i=0; i< nCount; i++) {
  784. dwTemp = lpRule->CodeUnit[i].dwDirectMode;
  785. szStr[4+i*4] = (dwTemp==0)?TEXT('p'):TEXT('n');
  786. dwTemp = lpRule->CodeUnit[i].wDBCSPosition;
  787. szStr[4+i*4+1] = (dwTemp<10)?TEXT('0')+(TCHAR)dwTemp:TEXT('a')+(TCHAR)dwTemp-10;
  788. dwTemp = lpRule->CodeUnit[i].wCodePosition;
  789. szStr[4+i*4+2] = (dwTemp<10)?TEXT('0')+(TCHAR)dwTemp:TEXT('a')+(TCHAR)dwTemp-10;
  790. szStr[4+i*4+3] = TEXT('+');
  791. }
  792. szStr[4+4*nCount-1] = 0;
  793. lstrcat(szStr,TEXT("\r\n"));
  794. }
  795. void MoveFileBlock(HANDLE hFile,DWORD dwOffset,DWORD dwSize, DWORD dwDirect)
  796. //** if (dwDirect==0) move block to file begin, else move to file end
  797. {
  798. BYTE *Buffer;
  799. static BYTE space[MAXREADBUFFER];
  800. DWORD i,dwReadBytes,dwFilePtr;
  801. Buffer = (BYTE *)LocalAlloc(LMEM_FIXED, MAXREADBUFFER * sizeof(BYTE));
  802. if (!Buffer)
  803. return;
  804. SetFilePointer(hFile,dwOffset,0,FILE_BEGIN);
  805. if(dwDirect == 0) {
  806. do {
  807. if (!ReadFile(hFile,Buffer,MAXREADBUFFER,&dwReadBytes,NULL))
  808. break;
  809. SetFilePointer(hFile, (0-dwReadBytes-dwSize),0,FILE_CURRENT);
  810. WriteFile(hFile,Buffer,dwReadBytes,&dwReadBytes,NULL);
  811. SetFilePointer(hFile, dwSize,0,FILE_CURRENT);
  812. }while(dwReadBytes == MAXREADBUFFER);
  813. SetFilePointer(hFile, 0-dwSize,0,FILE_CURRENT);
  814. for(i=0;i<dwSize;i++)
  815. //#60639 10/18/96
  816. space[i] = (BYTE)0;
  817. WriteFile(hFile,space,dwSize,&dwReadBytes,NULL);
  818. }
  819. else {
  820. dwFilePtr = SetFilePointer(hFile,0,0,FILE_END);
  821. while(dwFilePtr > dwOffset) {
  822. if(dwFilePtr > dwOffset+MAXREADBUFFER)
  823. dwReadBytes = MAXREADBUFFER;
  824. else
  825. dwReadBytes = dwFilePtr - dwOffset;
  826. dwFilePtr = SetFilePointer(hFile,(0-dwReadBytes),0,FILE_CURRENT);
  827. if (!ReadFile(hFile,Buffer,dwReadBytes,&dwReadBytes,NULL))
  828. break;
  829. SetFilePointer(hFile, (dwSize-dwReadBytes),0,FILE_CURRENT);
  830. WriteFile(hFile,Buffer,dwReadBytes,&dwReadBytes,NULL);
  831. SetFilePointer(hFile, (0-dwSize-dwReadBytes),0,FILE_CURRENT);
  832. }
  833. }
  834. LocalFree(Buffer);
  835. }
  836. BOOL Copy_File(LPCTSTR SrcFile,LPCTSTR DestFile)
  837. {
  838. BYTE *Buffer;
  839. HANDLE hSrcFile, hDestFile;
  840. DWORD dwReadBytes;
  841. Buffer = (BYTE *)LocalAlloc(LMEM_FIXED, MAXREADBUFFER * sizeof(BYTE));
  842. if (!Buffer)
  843. return FALSE;
  844. hSrcFile = Create_File(GetFocus(),(LPTSTR)SrcFile,GENERIC_READ,OPEN_EXISTING);
  845. hDestFile = CreateFile((LPTSTR)DestFile,GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,0,NULL);
  846. if(hSrcFile == (HANDLE)-1)
  847. {
  848. LocalFree(Buffer);
  849. return FALSE;
  850. }
  851. if(hDestFile == (HANDLE)-1)
  852. {
  853. CloseHandle(hSrcFile);
  854. LocalFree(Buffer);
  855. return FALSE;
  856. }
  857. SetFilePointer(hSrcFile,0,0,FILE_BEGIN);
  858. do {
  859. if (!ReadFile(hSrcFile,Buffer,MAXREADBUFFER,&dwReadBytes,NULL))
  860. {
  861. CloseHandle(hSrcFile);
  862. CloseHandle(hDestFile);
  863. LocalFree(Buffer);
  864. return FALSE;
  865. }
  866. WriteFile(hDestFile,Buffer,dwReadBytes,&dwReadBytes,NULL);
  867. }while(dwReadBytes == MAXREADBUFFER);
  868. CloseHandle(hSrcFile);
  869. CloseHandle(hDestFile);
  870. LocalFree(Buffer);
  871. return TRUE;
  872. }
  873. /****************************************************************************
  874. *
  875. * FUNCTION: CheckCrtData(HANDLE hWnd,
  876. * LPCREATEWORD lpCreateWords,
  877. * LPENCODEAREA lpEncode,
  878. * DWORD dwMaxCodes)
  879. *
  880. * PURPOSE: check whether create word data is entired or not.
  881. *
  882. * RETURN VALUES:
  883. * TRUE or FALSE.
  884. *
  885. * HISTORY:
  886. *
  887. *
  888. ****************************************************************************/
  889. BOOL CheckCrtData(HANDLE hWnd,
  890. LPCREATEWORD lpCreateWords,
  891. LPENCODEAREA lpEncode,
  892. DWORD dwMaxCodes)
  893. {
  894. DWORD i;
  895. TCHAR szDBCS[3],szCreate[13];
  896. TCHAR szTemp[128],szTmpStr[128];
  897. BOOL bErr = FALSE;
  898. #ifdef UNICODE
  899. //check CJK Unified Ideograph subset only
  900. for (i=0x250; i< 0x250+NUM_OF_CJK_CHINESE; i++) {
  901. #else
  902. for (i=0; i< NUM_OF_ENCODE ; i++) {
  903. #endif
  904. lstrncpy(szCreate,dwMaxCodes,&lpCreateWords[i*dwMaxCodes]);
  905. szCreate[dwMaxCodes] = 0;
  906. if(lstrlen(szCreate) == 0) {
  907. NoToEncode(i, (LPBYTE)szDBCS, NUMENCODEAREA, lpEncode);
  908. //NoToGB2312Code(i,szDBCS,NUMENCODEAREA);
  909. szDBCS[1] =0; //#62550
  910. LoadString(NULL, IDS_WORDNOTEXIST, szTmpStr, sizeof(szTmpStr)/sizeof(TCHAR));
  911. StringCchPrintf(szTemp,ARRAYSIZE(szTemp), TEXT("\'%s\' %s"),szDBCS,szTmpStr);
  912. if(ErrMessage(hWnd,szTemp))
  913. return FALSE;
  914. bErr = TRUE;
  915. }
  916. }
  917. return (!bErr) ;
  918. }
  919. void DispInfo(HANDLE hWnd,WORD wStrID)
  920. {
  921. TCHAR text[80];
  922. LoadString(NULL,wStrID,text,sizeof(text)/sizeof(TCHAR));
  923. SetDlgItemText(hWnd,TM_TOTALINFO,text);
  924. SetDlgItemText(hWnd,TM_TOTAL,TEXT(""));
  925. SetDlgItemText(hWnd,TM_TOTALNUM,TEXT(""));
  926. SetDlgItemText(hWnd,TM_CONVINFO,TEXT(""));
  927. SetDlgItemText(hWnd,TM_CONVNUM,TEXT(""));
  928. InvalidateRect(hWnd,NULL,FALSE);
  929. }
  930. BOOL WriteEMBToFile(LPCTSTR path_name,LPEMB_Head EMB_Table) {
  931. HANDLE hFile;
  932. DWORD byte_t_write;
  933. TCHAR szStr[256],szStr1[256];
  934. hFile = CreateFile(path_name,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL);
  935. if(hFile==INVALID_HANDLE_VALUE) {
  936. LoadString(NULL,IDS_FILEOPEN,szStr, sizeof(szStr)/sizeof(TCHAR));
  937. StringCchPrintf(szStr1, ARRAYSIZE(szStr1), TEXT("\'%s\' %s\n"),path_name,szStr);
  938. FatalMessage(GetFocus(),szStr1);
  939. return(0);
  940. }
  941. WriteFile(hFile,&EMB_Count,sizeof(EMB_Count),&byte_t_write,NULL);
  942. WriteFile(hFile,EMB_Table,EMB_Count*sizeof(EMB_Head), &byte_t_write, NULL);
  943. GlobalUnlock(HmemEMB_Table);
  944. SetEndOfFile(hFile);
  945. CloseHandle(hFile);
  946. return (1);
  947. }
  948. BOOL ReadEMBFromFile(LPCTSTR path_name, LPEMB_Head EMB_Table) {
  949. HANDLE hFile;
  950. DWORD byte_t_write;
  951. hFile = CreateFile(path_name,GENERIC_READ,
  952. FILE_SHARE_READ,NULL,OPEN_ALWAYS,0,NULL);
  953. if(hFile==INVALID_HANDLE_VALUE) {
  954. ProcessError(ERR_IMEUSE, GetFocus(), ERR);
  955. return FALSE;
  956. }
  957. /* if(hFile == INVALID_HANDLE_VALUE) {
  958. ProcessError(ERR_FILENOTOPEN,GetFocus(),ERR);
  959. return(0);
  960. }*/
  961. SetFilePointer(hFile,0,0,FILE_BEGIN);
  962. EMB_Count = 0;
  963. if (!ReadFile(hFile,&EMB_Count,sizeof(EMB_Count),&byte_t_write,NULL))
  964. {
  965. CloseHandle(hFile);
  966. return (0);
  967. }
  968. if(EMB_Count > 1000) {
  969. CloseHandle(hFile);
  970. return (0);
  971. }
  972. HmemEMB_Table = GlobalAlloc(GMEM_DISCARDABLE,(EMB_Count+1)*sizeof(EMB_Head));
  973. EMB_Table = GlobalLock(HmemEMB_Table);
  974. if (!ReadFile(hFile,EMB_Table,EMB_Count*sizeof(EMB_Head), &byte_t_write, NULL))
  975. {
  976. GlobalUnlock(HmemEMB_Table);
  977. SetEndOfFile(hFile);
  978. CloseHandle(hFile);
  979. return (0);
  980. }
  981. GlobalUnlock(HmemEMB_Table);
  982. SetEndOfFile(hFile);
  983. CloseHandle(hFile);
  984. return (1);
  985. }
  986. int AddZCItem(LPCTSTR path_name,LPEMB_Head EMB_Table,LPTSTR wai_code,LPTSTR cCharStr) { //string must end by '\0'
  987. int i;
  988. if(EMB_Count >= 1000)
  989. return FALSE;
  990. for(i=0; i<EMB_Count;i++) {
  991. #ifdef UNICODE
  992. if(wcsncmp(wai_code,EMB_Table[i].W_Code,MAXCODELEN) == 0 &&
  993. wcsncmp(cCharStr,EMB_Table[i].C_Char,USER_WORD_SIZE) == 0)
  994. {
  995. return FALSE;
  996. }
  997. if(wcsncmp(wai_code,EMB_Table[i].W_Code,MAXCODELEN) < 0 )
  998. break;
  999. #else
  1000. if(strncmp(wai_code,EMB_Table[i].W_Code,MAXCODELEN) == 0 &&
  1001. strncmp(cCharStr,EMB_Table[i].C_Char,USER_WORD_SIZE) == 0)
  1002. {
  1003. return FALSE;
  1004. }
  1005. if(strncmp(wai_code,EMB_Table[i].W_Code,MAXCODELEN) < 0 )
  1006. break;
  1007. #endif
  1008. }
  1009. EMB_Count ++;
  1010. memmove(&EMB_Table[i+1],&EMB_Table[i], (EMB_Count-i-1)*sizeof(EMB_Head));
  1011. lstrncpy(EMB_Table[i].W_Code,MAXCODELEN,wai_code);
  1012. lstrncpy(EMB_Table[i].C_Char,USER_WORD_SIZE,cCharStr);
  1013. // GlobalUnlock(HmemEMB_Table);
  1014. WriteEMBToFile(path_name,EMB_Table);
  1015. return TRUE;
  1016. }
  1017. void DelSelCU(LPCTSTR path_name,LPEMB_Head EMB_Table, int item) {
  1018. memcpy(EMB_Table+item,EMB_Table+item+1,(EMB_Count-item-1)*sizeof(EMB_Head));
  1019. EMB_Count --;
  1020. WriteEMBToFile(path_name,EMB_Table);
  1021. }
  1022. /****************************************************************************
  1023. *
  1024. * FUNCTION: ReadUserWord(HWND hWnd,LPSTR lpFileName,LPDWORD fdwUserWords)
  1025. *
  1026. * PURPOSE: read user words from file pointed by lpFileName.
  1027. *
  1028. * INPUTS: hWnd - parent window's handle
  1029. * lpFileName - pointer to file name
  1030. fdwUserWord - pointer to number of user words
  1031. *
  1032. * RETURN VALUES:
  1033. * TRUE or FALSE.
  1034. *
  1035. * HISTORY:
  1036. *
  1037. *
  1038. ****************************************************************************/
  1039. BOOL ReadUserWord(HWND hWnd,LPTSTR lpFileName,LPDWORD fdwUserWords,WORD wMaxCodes)
  1040. {
  1041. HANDLE hFile;
  1042. TCHAR *Buffer;
  1043. DWORD dwReadBytes;
  1044. TCHAR szStr[256];
  1045. int nRet;
  1046. register int i,j;
  1047. Buffer = (TCHAR *)LocalAlloc(LMEM_FIXED, MAXREADBUFFER * sizeof(TCHAR));;
  1048. if (!Buffer)
  1049. return (-ERR_OUTOFMEMORY);
  1050. *fdwUserWords = 0;
  1051. hFile = Create_File(hWnd,lpFileName,GENERIC_READ,OPEN_EXISTING);
  1052. if (hFile == (HANDLE)-1)
  1053. {
  1054. LocalFree(Buffer);
  1055. return FALSE;
  1056. }
  1057. SendDlgItemMessage(hWnd,IDC_LIST,LB_RESETCONTENT,0,0L);
  1058. SetFilePointer(hFile,0,0,FILE_BEGIN);
  1059. SetCursor (LoadCursor (NULL, IDC_WAIT));
  1060. dwLineNo = 0;
  1061. while(ReadFile(hFile,Buffer,MAXREADBUFFER,&dwReadBytes,NULL))
  1062. {
  1063. lstrcpy(szStr,TEXT(""));
  1064. j=0;
  1065. dwReadBytes = dwReadBytes/sizeof(TCHAR);
  1066. for(i=0;i<(int)dwReadBytes;i++) {
  1067. if(Buffer[i] == 0x0d || Buffer[i] == 0xfeff)
  1068. continue;
  1069. else if(Buffer[i] == TEXT('\n')) {
  1070. dwLineNo ++;
  1071. szStr[j]=0;
  1072. j=0;
  1073. if(lstrlen(szStr) == 0) continue;
  1074. nRet = CheckUserWord(hWnd,szStr,wMaxCodes);
  1075. if(nRet == FALSE) {
  1076. CloseHandle(hFile);
  1077. SetCursor (LoadCursor (NULL, IDC_ARROW));
  1078. LocalFree(Buffer);
  1079. return FALSE;
  1080. }
  1081. else if(nRet == -1)
  1082. {
  1083. szStr[0]=0;
  1084. continue;
  1085. }
  1086. SendDlgItemMessage(hWnd,IDC_LIST,LB_ADDSTRING,0,(LPARAM)szStr);
  1087. (*fdwUserWords)++;
  1088. if((*fdwUserWords) >= 1000) {
  1089. ProcessError(ERR_TOOMANYUSERWORD,hWnd,WARNING);
  1090. CloseHandle(hFile);
  1091. SetCursor (LoadCursor (NULL, IDC_ARROW));
  1092. LocalFree(Buffer);
  1093. return FALSE;
  1094. }
  1095. szStr[0]=0;
  1096. }
  1097. else {
  1098. szStr[j]=Buffer[i];
  1099. j++;
  1100. }
  1101. } /*** for (i=0;...) ****/
  1102. if(j)
  1103. SetFilePointer(hFile,0-j,0,FILE_CURRENT);
  1104. if(dwReadBytes*sizeof(TCHAR) < MAXREADBUFFER) break;
  1105. };
  1106. CloseHandle(hFile);
  1107. SetCursor (LoadCursor (NULL, IDC_ARROW));
  1108. LocalFree(Buffer);
  1109. return TRUE;
  1110. }
  1111. int CheckUserWord(HWND hWnd,LPTSTR szUserWord,WORD wMaxCodes)
  1112. {
  1113. int retValue;
  1114. TCHAR szTemp[256];
  1115. TCHAR szDBCS[256],szCode[30];
  1116. retValue = ParseDBCSstr(hWnd,szUserWord, szDBCS,szCode,szTemp,wMaxCodes);
  1117. if(retValue != TRUE)
  1118. return retValue;
  1119. if(lstrlen(szDBCS) == 0 || lstrlen(szCode) == 0)
  1120. return -1;
  1121. else
  1122. return retValue;
  1123. }
  1124. BOOL CheckUserDBCS(HWND hWnd,LPTSTR szDBCS)
  1125. {
  1126. int nDBCS=0,i;
  1127. static TCHAR szTmpStr[256],szTemp[256];
  1128. nDBCS=lstrlen(szDBCS);
  1129. if(nDBCS == 0)
  1130. return FALSE;
  1131. #ifndef UNICODE
  1132. if(nDBCS%2 == 1) {
  1133. MessageBeep((UINT)-1);
  1134. for(i=0; i <nDBCS; i += 2) {
  1135. if((BYTE)szDBCS[i] < 0x81)
  1136. memcpy(&szDBCS[i],&szDBCS[i+1], nDBCS-i);
  1137. }
  1138. return FALSE;
  1139. }
  1140. #endif
  1141. return TRUE;
  1142. }
  1143. BOOL CheckCodeLegal(HWND hWnd,LPTSTR szDBCS,LPTSTR szCode,LPTSTR szCreate, LPDESCRIPTION lpDescript)
  1144. {
  1145. int len = lstrlen(szCode);
  1146. int i;
  1147. TCHAR szTemp[256],szTmpStr[256];
  1148. if(len==0) return TRUE;
  1149. if(len > lpDescript->wMaxCodes) {
  1150. LoadString(NULL, IDS_DBCSCODELEN, szTmpStr, sizeof(szTmpStr)/sizeof(TCHAR));
  1151. #ifdef UNICODE
  1152. {
  1153. TCHAR UniTmp[] = {0x884C, 0x0000};
  1154. StringCchPrintf(szTemp, ARRAYSIZE(szTemp), TEXT("\'%ws\'%ws %d(%ws:%ld)!"), szDBCS,szTmpStr,(int)lpDescript->wMaxCodes, UniTmp, dwLineNo);
  1155. }
  1156. #else
  1157. StringCchPrintf(szTemp, ARRAYSIZE(szTemp), "\'%s\'%s %d(��:%ld)!", szDBCS,szTmpStr,(int)lpDescript->wMaxCodes, dwLineNo);
  1158. #endif
  1159. FatalMessage(hWnd,szTemp);
  1160. return FALSE;
  1161. }
  1162. for(i=0; i<len; i++) {
  1163. if(_tcschr(lpDescript->szUsedCode,szCode[i]) == NULL) {
  1164. LoadString(NULL, IDS_DBCSCODE, szTmpStr, sizeof(szTmpStr)/sizeof(TCHAR));
  1165. #ifdef UNICODE
  1166. {
  1167. TCHAR UniTmp[] = {0x884C, 0x0000};
  1168. StringCchPrintf(szTemp, ARRAYSIZE(szTemp),TEXT("\'%ws%ws\' %ws(%ws:%ld) "), szDBCS,szCode,szTmpStr, UniTmp, dwLineNo);
  1169. }
  1170. #else
  1171. StringCchPrintf(szTemp, ARRAYSIZE(szTemp),"\'%s%s\' %s(��:%ld) ", szDBCS,szCode,szTmpStr, dwLineNo);
  1172. #endif
  1173. FatalMessage(hWnd,szTemp);
  1174. return FALSE;
  1175. }
  1176. }
  1177. len = lstrlen(szCreate);
  1178. if(lpDescript->byMaxElement == 1 || len == 0)
  1179. return TRUE;
  1180. for(i=0; i<len; i++) {
  1181. if(_tcschr(lpDescript->szUsedCode,szCreate[i]) == NULL) {
  1182. LoadString(NULL, IDS_DBCSCODE, szTmpStr, sizeof(szTmpStr)/sizeof(TCHAR));
  1183. #ifdef UNICODE
  1184. {
  1185. TCHAR UniTmp[] = {0x884C, 0x0000};
  1186. StringCchPrintf(szTemp, ARRAYSIZE(szTemp), TEXT("\'%ws%ws %ws\' %ws(%ws:%ld) "), szDBCS,szCode,szCreate,szTmpStr, UniTmp, dwLineNo);
  1187. }
  1188. #else
  1189. StringCchPrintf(szTemp, ARRAYSIZE(szTemp), "\'%s%s %s\' %s(��:%ld) ", szDBCS,szCode,szCreate,szTmpStr, dwLineNo);
  1190. #endif
  1191. FatalMessage(hWnd,szTemp);
  1192. return FALSE;
  1193. }
  1194. }
  1195. return TRUE;
  1196. }
  1197. void DelIllegalCode(TCHAR *szCode)
  1198. {
  1199. static TCHAR collection[48]=
  1200. TEXT("`1234567890-=\\[];',./abcdefghijklmnopqrstuvwxyz");
  1201. int i,len = lstrlen(szCode), j;
  1202. TCHAR szStr[512];
  1203. if(len==0)
  1204. return ;
  1205. j = 0;
  1206. for(i=0; i<len;i++)
  1207. if(_tcschr(collection,szCode[i]) != NULL)
  1208. {
  1209. szStr[j] = szCode[i];
  1210. j ++;
  1211. }
  1212. szStr[j] = 0;
  1213. lstrcpy(szCode,szStr);
  1214. return ;
  1215. }
  1216. BOOL CheckCodeCollection(HWND hWnd,LPTSTR szUsedCode)
  1217. {
  1218. static TCHAR collection[48]=
  1219. TEXT("`1234567890-=\\[];',./abcdefghijklmnopqrstuvwxyz");
  1220. int i,len = lstrlen(szUsedCode);
  1221. if(len==0) {
  1222. ProcessError(ERR_USEDCODE,hWnd,ERR);
  1223. return FALSE;
  1224. }
  1225. qSortChar(szUsedCode,0,len-1);
  1226. for(i=0; i<len-1 ;i++)
  1227. {
  1228. if(szUsedCode[i] == szUsedCode[i+1]
  1229. || _tcschr(collection,szUsedCode[len-1])== NULL
  1230. || _tcschr(collection,szUsedCode[i])== NULL) {
  1231. ProcessError(ERR_USEDCODE,hWnd,ERR);
  1232. return FALSE;
  1233. }
  1234. }
  1235. return TRUE;
  1236. }
  1237. // QueryKey - enumerates the subkeys of a given key and the associated
  1238. // values and then copies the information about the keys and values
  1239. // into a pair of edit controls and list boxes.
  1240. // hDlg - dialog box that contains the edit controls and list boxes
  1241. // hKey - key whose subkeys and values are to be enumerated
  1242. //
  1243. BOOL QueryKey(HWND hDlg, HANDLE hKey)
  1244. {
  1245. TCHAR achKey[MAX_PATH];
  1246. TCHAR achClass[MAX_PATH] = TEXT(""); /* buffer for class name */
  1247. DWORD cchClassName = MAX_PATH; /* length of class string */
  1248. DWORD cSubKeys; /* number of subkeys */
  1249. DWORD cbMaxSubKey; /* longest subkey size */
  1250. DWORD cchMaxClass; /* longest class string */
  1251. DWORD cValues; /* number of values for key */
  1252. DWORD cchMaxValue; /* longest value name */
  1253. DWORD cbMaxValueData; /* longest value data */
  1254. DWORD cbSecurityDescriptor; /* size of security descriptor */
  1255. FILETIME ftLastWriteTime; /* last write time */
  1256. DWORD i, j;
  1257. DWORD retCode;
  1258. DWORD dwcValueName = MAX_VALUE_NAME;
  1259. // Get the class name and the value count.
  1260. RegQueryInfoKey(hKey, /* key handle */
  1261. achClass, /* buffer for class name */
  1262. &cchClassName, /* length of class string */
  1263. NULL, /* reserved */
  1264. &cSubKeys, /* number of subkeys */
  1265. &cbMaxSubKey, /* longest subkey size */
  1266. &cchMaxClass, /* longest class string */
  1267. &cValues, /* number of values for this key */
  1268. &cchMaxValue, /* longest value name */
  1269. &cbMaxValueData, /* longest value data */
  1270. &cbSecurityDescriptor, /* security descriptor */
  1271. &ftLastWriteTime); /* last write time */
  1272. // Enumerate the child keys, looping until RegEnumKey fails. Then
  1273. // get the name of each child key and copy it into the list box.
  1274. SetCursor(LoadCursor(NULL, IDC_WAIT));
  1275. j = 0;
  1276. for (i = 0, retCode = ERROR_SUCCESS;
  1277. retCode == ERROR_SUCCESS; i++) {
  1278. retCode = RegEnumKey(hKey, i, achKey, MAX_PATH);
  1279. if (retCode == (DWORD)ERROR_SUCCESS) {
  1280. SendMessage(GetDlgItem(hDlg, IDC_LIST),
  1281. LB_ADDSTRING, 0,
  1282. (LPARAM)achKey);
  1283. }
  1284. }
  1285. SetCursor(LoadCursor (NULL, IDC_ARROW));
  1286. return TRUE;
  1287. }
  1288. BOOL CreateMbKey(PHKEY phKey,LPCTSTR FileName,LPCTSTR KeyName)
  1289. {
  1290. HKEY hkResult;
  1291. DWORD i;
  1292. DWORD Value=1;
  1293. #ifdef UNICODE
  1294. static TCHAR ValueName[][12]= {
  1295. 0x7801, 0x8868, 0x6587, 0x4EF6, 0x540D,0x0000,
  1296. 0x8BCD, 0x8BED, 0x8054, 0x60F3, 0x0000,
  1297. 0x8BCD, 0x8BED, 0x8F93, 0x5165, 0x0000,
  1298. 0x9010, 0x6E10, 0x63D0, 0x793A, 0x0000,
  1299. 0x5916, 0x7801, 0x63D0, 0x793A, 0x0000,
  1300. '<','S','P','A','C','E','>', 0x0000,
  1301. '<','E','N','T','E','R','>', 0x0000,
  1302. 0x5149, 0x6807, 0x8DDF, 0x968F, 0x0000
  1303. };
  1304. #else
  1305. static TCHAR ValueName[][12]= {
  1306. "�����ļ���",
  1307. "��������",
  1308. "��������",
  1309. "������ʾ",
  1310. "������ʾ",
  1311. "<SPACE>",
  1312. "<ENTER>",
  1313. "��������"
  1314. };
  1315. #endif \\UNICODE
  1316. if(!RegOpenKey(*phKey,KeyName,&hkResult))
  1317. return FALSE;
  1318. if(RegCreateKey(*phKey,KeyName,&hkResult))
  1319. return FALSE;
  1320. RegSetValueEx(hkResult,ValueName[0],0,REG_SZ,(BYTE*)FileName,(lstrlen(FileName)+1) * sizeof(TCHAR) );
  1321. for(i=1;i<6;i++)
  1322. RegSetValueEx(hkResult,ValueName[i],0,REG_DWORD,(LPSTR)&Value,sizeof(DWORD));
  1323. RegSetValueEx(hkResult,ValueName[7],0,REG_DWORD,(LPSTR)&Value,sizeof(DWORD));
  1324. Value = 0;
  1325. RegSetValueEx(hkResult,ValueName[5],0,REG_DWORD,(LPSTR)&Value,sizeof(DWORD));
  1326. RegSetValueEx(hkResult,ValueName[6],0,REG_DWORD,(LPSTR)&Value,sizeof(DWORD));
  1327. RegCloseKey(hkResult);
  1328. return TRUE;
  1329. }
  1330. BOOL SetRegValue(HKEY hKey,LPDWORD Value)
  1331. {
  1332. DWORD i;
  1333. #ifdef UNICODE
  1334. static TCHAR ValueName[][12]= {
  1335. 0x8BCD, 0x8BED, 0x8054, 0x60F3, 0x0000,
  1336. 0x8BCD, 0x8BED, 0x8F93, 0x5165, 0x0000,
  1337. 0x9010, 0x6E10, 0x63D0, 0x793A, 0x0000,
  1338. 0x5916, 0x7801, 0x63D0, 0x793A, 0x0000,
  1339. '<','S','P','A','C','E','>', 0x0000,
  1340. '<','E','N','T','E','R','>', 0x0000,
  1341. 0x5149, 0x6807, 0x8DDF, 0x968F, 0x0000
  1342. };
  1343. #else
  1344. static TCHAR ValueName[][12]= {
  1345. "��������",
  1346. "��������",
  1347. "������ʾ",
  1348. "������ʾ",
  1349. "<SPACE>",
  1350. "<ENTER>",
  1351. "��������"
  1352. };
  1353. #endif \\UNICODE
  1354. for(i=0;i<7;i++)
  1355. RegSetValueEx(hKey,ValueName[i],0,REG_DWORD,(LPSTR)&Value[i],sizeof(DWORD));
  1356. return TRUE;
  1357. }
  1358. BOOL GetRegValue(HWND hDlg,HKEY hKey,LPDWORD Value)
  1359. {
  1360. #ifdef UNICODE
  1361. static TCHAR ValueName[][12]= {
  1362. 0x8BCD, 0x8BED, 0x8054, 0x60F3, 0x0000,
  1363. 0x8BCD, 0x8BED, 0x8F93, 0x5165, 0x0000,
  1364. 0x9010, 0x6E10, 0x63D0, 0x793A, 0x0000,
  1365. 0x5916, 0x7801, 0x63D0, 0x793A, 0x0000,
  1366. '<','S','P','A','C','E','>', 0x0000,
  1367. '<','E','N','T','E','R','>', 0x0000,
  1368. 0x5149, 0x6807, 0x8DDF, 0x968F, 0x0000
  1369. };
  1370. #else
  1371. static TCHAR ValueName[][12]= {
  1372. "��������",
  1373. "��������",
  1374. "������ʾ",
  1375. "������ʾ",
  1376. "<SPACE>",
  1377. "<ENTER>",
  1378. "��������"
  1379. };
  1380. #endif \\UNICODE
  1381. DWORD i,j,retValue,dwcValueName;
  1382. TCHAR Buf[80];
  1383. SetCursor (LoadCursor (NULL, IDC_WAIT));
  1384. for (j = 0, retValue = ERROR_SUCCESS; j < 7; j++)
  1385. {
  1386. dwcValueName = MAX_VALUE_NAME;
  1387. i=sizeof(DWORD);
  1388. retValue = RegQueryValueEx (hKey, ValueName[j],
  1389. NULL,
  1390. NULL, //&dwType,
  1391. (LPSTR)&Value[j], //&bData,
  1392. &i); //&bcData);
  1393. if (retValue != (DWORD)ERROR_SUCCESS &&
  1394. retValue != ERROR_INSUFFICIENT_BUFFER)
  1395. {
  1396. wsprintf (Buf, TEXT("Line:%d 0 based index = %d, retValue = %d, ValueLen = %d"),
  1397. __LINE__, j, retValue, dwcValueName);
  1398. MessageBox (hDlg, Buf, TEXT("Debug"), MB_OK);
  1399. }
  1400. }// end for(;;)
  1401. SetCursor (LoadCursor (NULL, IDC_ARROW));
  1402. return TRUE;
  1403. }
  1404. LPTSTR _tcschr(LPTSTR string, TCHAR c)
  1405. {
  1406. #ifdef UNICODE
  1407. return (wcschr(string, c));
  1408. #else
  1409. return (strchr(string, c));
  1410. #endif
  1411. }
  1412. LPTSTR _tcsrchr(LPTSTR string, TCHAR c)
  1413. {
  1414. #ifdef UNICODE
  1415. return (wcsrchr(string, c));
  1416. #else
  1417. return (strrchr(string, c));
  1418. #endif
  1419. }
  1420. LPTSTR _tcsstr(LPTSTR string1, LPTSTR string2)
  1421. {
  1422. #ifdef UNICODE
  1423. return (wcsstr(string1, string2));
  1424. #else
  1425. return (strstr(string1, string2));
  1426. #endif
  1427. }
  1428. LPTSTR _tcsupr(LPTSTR string)
  1429. {
  1430. #ifdef UNICODE
  1431. return (_wcsupr(string));
  1432. #else
  1433. return (_strupr(string));
  1434. #endif
  1435. }
  1436. int _ttoi(LPTSTR string)
  1437. {
  1438. #ifdef UNICODE
  1439. return (_wtoi(string));
  1440. #else
  1441. return (atoi(string));
  1442. #endif
  1443. }
  1444. int _taccess(LPTSTR szFileName, int mode)
  1445. {
  1446. char szDbcsName[256];
  1447. #ifdef UNICODE
  1448. WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, szFileName, -1, szDbcsName,
  1449. sizeof(szDbcsName), NULL, NULL);
  1450. #else
  1451. StringCbCopy(szDbcsName, sizeof(szDbcsName), szFileName);
  1452. #endif
  1453. return (_access(szDbcsName, mode));
  1454. }