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.

448 lines
13 KiB

  1. /*++
  2. Copyright (c) 1995-1999 Microsoft Corporation, All Rights Reserved
  3. Module Name:
  4. REGWORD.C - register word into dictionary of IME
  5. ++*/
  6. #include <windows.h>
  7. #include <immdev.h>
  8. #include <imedefs.h>
  9. #include <regstr.h>
  10. #ifdef EUDC
  11. /**********************************************************************/
  12. /* ImeRegsisterWord */
  13. /* Return Value: */
  14. /* TRUE - successful, FALSE - failure */
  15. /**********************************************************************/
  16. BOOL WINAPI ImeRegisterWord(
  17. LPCTSTR lpszReading,
  18. DWORD dwStyle,
  19. LPCTSTR lpszString)
  20. {
  21. HIMCC hPrivate;
  22. if (!lpszString || (lpszString[0] ==TEXT('\0'))) {
  23. return (FALSE);
  24. }
  25. if (!lpszReading || (lpszReading[0] == TEXT('\0'))) {
  26. return (FALSE);
  27. }
  28. // only handle word not string now, should consider string later?
  29. if (*(LPCTSTR)((LPBYTE)lpszString + sizeof(WORD)) != TEXT('\0')) {
  30. return (FALSE);
  31. }
  32. hPrivate = (HIMCC)ImmCreateIMCC(sizeof(PRIVCONTEXT));
  33. if (hPrivate != (HIMCC)NULL){
  34. StartEngine(hPrivate);
  35. AddZCItem(hPrivate, (LPTSTR)lpszReading, (LPTSTR)lpszString);
  36. EndEngine(hPrivate);
  37. ImmDestroyIMCC(hPrivate);
  38. return (TRUE);
  39. }
  40. else
  41. return (FALSE);
  42. }
  43. /**********************************************************************/
  44. /* ImeUnregsisterWord */
  45. /* Return Value: */
  46. /* TRUE - successful, FALSE - failure */
  47. /**********************************************************************/
  48. BOOL WINAPI ImeUnregisterWord(
  49. LPCTSTR lpszReading,
  50. DWORD dwStyle,
  51. LPCTSTR lpszString)
  52. {
  53. BOOL fRet;
  54. HANDLE hUsrDicMem, hUsrDicFile;
  55. LPTSTR lpUsrDicStart, lpCurr, lpUsrDicLimit, lpUsrDic;
  56. LPWORD lpwStart;
  57. WORD wNum_EMB;
  58. PSECURITY_ATTRIBUTES psa;
  59. TCHAR szReading[MAXCODE];
  60. int i;
  61. fRet = FALSE;
  62. if (!lpszString || !lpszReading) {
  63. return (fRet);
  64. }
  65. if (!(dwStyle & IME_REGWORD_STYLE_EUDC)) {
  66. return (fRet);
  67. }
  68. // only handle word not string now, should consider string later?
  69. if (*(LPCTSTR)((LPBYTE)lpszString + sizeof(WORD)) != TEXT('\0')) {
  70. return (fRet);
  71. }
  72. if (!MBIndex.EUDCData.szEudcDictName[0]) {
  73. return (fRet);
  74. }
  75. psa = CreateSecurityAttributes();
  76. hUsrDicFile = CreateFile(MBIndex.EUDCData.szEudcDictName,
  77. GENERIC_READ | GENERIC_WRITE,
  78. FILE_SHARE_READ|FILE_SHARE_WRITE,
  79. psa,
  80. OPEN_EXISTING,
  81. FILE_ATTRIBUTE_NORMAL,
  82. (HANDLE)NULL);
  83. if (hUsrDicFile == INVALID_HANDLE_VALUE) {
  84. FreeSecurityAttributes(psa);
  85. return FALSE;
  86. }
  87. hUsrDicMem = CreateFileMapping(hUsrDicFile,
  88. psa,
  89. PAGE_READWRITE,
  90. 0,
  91. sizeof(EMB_Head)*MAXNUMBER_EMB+2,
  92. MBIndex.EUDCData.szEudcMapFileName);
  93. if (!hUsrDicMem) {
  94. CloseHandle(hUsrDicFile);
  95. FreeSecurityAttributes(psa);
  96. return (fRet);
  97. }
  98. FreeSecurityAttributes(psa);
  99. lpUsrDic = MapViewOfFile(hUsrDicMem, FILE_MAP_WRITE, 0, 0, 0);
  100. if (!lpUsrDic) {
  101. CloseHandle(hUsrDicFile);
  102. CloseHandle(hUsrDicMem);
  103. return (fRet);
  104. }
  105. lpwStart = (LPWORD)lpUsrDic;
  106. wNum_EMB = *lpwStart;
  107. // skip the first two bytes which contain the numeber of EMB records.
  108. lpUsrDicStart =(LPTSTR) ( (LPBYTE)lpUsrDic + sizeof(WORD) );
  109. lpUsrDicLimit=lpUsrDicStart+(sizeof(EMB_Head)*wNum_EMB)/sizeof(TCHAR);
  110. for (i=0; i<MAXCODE; i++)
  111. szReading[i] = TEXT('\0');
  112. for (i=0; i<lstrlen(lpszReading); i++)
  113. szReading[i] = lpszReading[i];
  114. for (lpCurr = lpUsrDicStart; lpCurr < lpUsrDicLimit;
  115. lpCurr += sizeof(EMB_Head) / sizeof(TCHAR) ) {
  116. EMB_Head *lpEMB_Head;
  117. lpEMB_Head = (EMB_Head *)lpCurr;
  118. // find the internal code, if this record contains a phrase, skip it.
  119. if ( lpEMB_Head->C_Char[sizeof(WORD)/sizeof(TCHAR)] != TEXT('\0') )
  120. continue;
  121. if (memcmp((LPBYTE)lpszString, (LPBYTE)(lpEMB_Head->C_Char), 2) != 0)
  122. continue;
  123. if (memcmp((LPBYTE)szReading,(LPBYTE)(lpEMB_Head->W_Code),
  124. MAXCODE*sizeof(TCHAR) ) == 0 )
  125. break;
  126. }
  127. if (lpCurr < lpUsrDicLimit) {
  128. // we found a record matching the requested lpszReading and lpszString
  129. LPTSTR lpCurrNext;
  130. wNum_EMB --;
  131. *lpwStart = wNum_EMB;
  132. lpCurrNext = lpCurr + sizeof(EMB_Head)/sizeof(TCHAR);
  133. // move every next EMB record ahead.
  134. while (lpCurrNext < lpUsrDicLimit) {
  135. for (i=0; i<sizeof(EMB_Head)/sizeof(TCHAR); i++)
  136. *lpCurr++ = *lpCurrNext++;
  137. }
  138. // put zero to the last EMB record.
  139. for (i=0; i<sizeof(EMB_Head)/sizeof(TCHAR); i++)
  140. *lpCurr++ = TEXT('\0');
  141. }
  142. UnmapViewOfFile(lpUsrDic);
  143. CloseHandle(hUsrDicMem);
  144. CloseHandle(hUsrDicFile);
  145. return (TRUE);
  146. }
  147. /**********************************************************************/
  148. /* ImeGetRegsisterWordStyle */
  149. /* Return Value: */
  150. /* number of styles copied/required */
  151. /**********************************************************************/
  152. UINT WINAPI ImeGetRegisterWordStyle(
  153. UINT nItem,
  154. LPSTYLEBUF lpStyleBuf)
  155. {
  156. if (!nItem) {
  157. return (1);
  158. }
  159. // invalid case
  160. if (!lpStyleBuf) {
  161. return (0);
  162. }
  163. lpStyleBuf->dwStyle = IME_REGWORD_STYLE_EUDC;
  164. LoadString(hInst, IDS_EUDC, lpStyleBuf->szDescription,
  165. sizeof(lpStyleBuf->szDescription)/sizeof(TCHAR));
  166. return (1);
  167. }
  168. /**********************************************************************/
  169. /* ImeEnumRegisterWord */
  170. /* Return Value: */
  171. /* the last value return by the callback function */
  172. /**********************************************************************/
  173. UINT WINAPI ImeEnumRegisterWord(
  174. REGISTERWORDENUMPROC lpfnRegisterWordEnumProc,
  175. LPCTSTR lpszReading,
  176. DWORD dwStyle,
  177. LPCTSTR lpszString,
  178. LPVOID lpData)
  179. {
  180. HANDLE hUsrDicMem;
  181. HANDLE hUsrDicFile;
  182. LPTSTR lpUsrDicStart, lpCurr, lpUsrDicLimit, lpUsrDic;
  183. UINT uRet;
  184. int i;
  185. LPWORD lpwStart;
  186. WORD wNum_EMB;
  187. PSECURITY_ATTRIBUTES psa;
  188. TCHAR szReading[MAXCODE];
  189. TCHAR szString[MAXINPUTWORD];
  190. uRet = 0;
  191. if (dwStyle != IME_REGWORD_STYLE_EUDC) {
  192. return (uRet);
  193. }
  194. if (lpszString && (*(LPCTSTR)((LPBYTE)lpszString+sizeof(WORD))!=TEXT('\0')) )
  195. return (uRet);
  196. if (!MBIndex.EUDCData.szEudcDictName[0]) {
  197. return (uRet);
  198. }
  199. psa = CreateSecurityAttributes();
  200. hUsrDicFile = CreateFile(MBIndex.EUDCData.szEudcDictName,
  201. GENERIC_READ | GENERIC_WRITE,
  202. FILE_SHARE_READ|FILE_SHARE_WRITE,
  203. psa,
  204. OPEN_EXISTING,
  205. FILE_ATTRIBUTE_NORMAL,
  206. (HANDLE)NULL);
  207. if (hUsrDicFile == INVALID_HANDLE_VALUE) {
  208. FreeSecurityAttributes(psa);
  209. return (uRet);
  210. }
  211. hUsrDicMem = CreateFileMapping(hUsrDicFile,
  212. psa,
  213. PAGE_READWRITE,
  214. 0,
  215. sizeof(EMB_Head)*MAXNUMBER_EMB+2,
  216. MBIndex.EUDCData.szEudcMapFileName);
  217. if (!hUsrDicMem) {
  218. CloseHandle(hUsrDicFile);
  219. FreeSecurityAttributes(psa);
  220. return (uRet);
  221. }
  222. FreeSecurityAttributes(psa);
  223. lpUsrDic = MapViewOfFile(hUsrDicMem, FILE_MAP_WRITE, 0, 0, 0);
  224. if (!lpUsrDic) {
  225. CloseHandle(hUsrDicMem);
  226. CloseHandle(hUsrDicFile);
  227. return (uRet);
  228. }
  229. lpwStart = (LPWORD)lpUsrDic;
  230. wNum_EMB = *lpwStart;
  231. lpUsrDicStart = (LPTSTR)( (LPBYTE)lpUsrDic + sizeof(WORD) );
  232. lpUsrDicLimit = lpUsrDicStart+(sizeof(EMB_Head)*wNum_EMB)/sizeof(TCHAR);
  233. for (i=0; i<MAXCODE; i++)
  234. szReading[i] = TEXT('\0');
  235. for (i=0; i<MAXINPUTWORD; i++)
  236. szString[i] = TEXT('\0');
  237. if ( lpszReading )
  238. for (i=0; i<lstrlen(lpszReading); i++)
  239. szReading[i] = lpszReading[i];
  240. if ( lpszString )
  241. for (i=0; i<lstrlen(lpszString); i++)
  242. szString[i]=lpszString[i];
  243. for (lpCurr = lpUsrDicStart; lpCurr < lpUsrDicLimit;
  244. lpCurr += (sizeof(EMB_Head)*wNum_EMB)/sizeof(TCHAR)) {
  245. TCHAR szBufReading[MAXCODE];
  246. TCHAR szBufString[MAXINPUTWORD];
  247. EMB_Head *lpEMB_Head;
  248. BOOL fMatched;
  249. lpEMB_Head = (EMB_Head *)lpCurr;
  250. for ( i=0; i<MAXCODE; i++)
  251. szBufReading[i] = lpEMB_Head->W_Code[i];
  252. for ( i=0; i<MAXINPUTWORD; i++)
  253. szBufString[i] = lpEMB_Head->C_Char[i];
  254. // Because here we handle only EUDC chars, if it is a phrase,
  255. // just skip it.
  256. fMatched = FALSE;
  257. if ( szBufString[sizeof(WORD)/sizeof(TCHAR)] != TEXT('\0') )
  258. continue;
  259. if ( !lpszReading && !lpszString) {
  260. fMatched = TRUE;
  261. }
  262. else {
  263. // if lpszReading is NULL, enumerate all availible reading strings
  264. // matching with the specified lpszString
  265. if ( !lpszReading) {
  266. if (memcmp((LPBYTE)szBufString,(LPBYTE)szString, 2) ==0) {
  267. fMatched = TRUE;
  268. }
  269. }
  270. if ( !lpszString ) {
  271. if (memcmp((LPBYTE)szBufReading, (LPBYTE)szReading,
  272. MAXCODE*sizeof(TCHAR) ) == 0 ) {
  273. fMatched = TRUE;
  274. }
  275. }
  276. if ( lpszReading && lpszString) {
  277. if ( (memcmp((LPBYTE)szBufString,(LPBYTE)szString, 2) ==0) &&
  278. (memcmp((LPBYTE)szBufReading, (LPBYTE)szReading,
  279. MAXCODE*sizeof(TCHAR) ) == 0 ) ) {
  280. fMatched = TRUE;
  281. }
  282. }
  283. }
  284. if ( fMatched == TRUE ) {
  285. uRet=(*lpfnRegisterWordEnumProc)((const unsigned short *)szBufReading,
  286. IME_REGWORD_STYLE_EUDC,
  287. (const unsigned short *)szBufString,
  288. lpData);
  289. if (!uRet) break;
  290. }
  291. }
  292. UnmapViewOfFile(lpUsrDic);
  293. CloseHandle(hUsrDicMem);
  294. CloseHandle(hUsrDicFile);
  295. return (uRet);
  296. }
  297. #else
  298. /**********************************************************************/
  299. /* ImeRegsisterWord */
  300. /* Return Value: */
  301. /* TRUE - successful, FALSE - failure */
  302. /**********************************************************************/
  303. BOOL WINAPI ImeRegisterWord(
  304. LPCTSTR lpszReading,
  305. DWORD dwStyle,
  306. LPCTSTR lpszString)
  307. {
  308. return (FALSE);
  309. }
  310. /**********************************************************************/
  311. /* ImeUnregsisterWord */
  312. /* Return Value: */
  313. /* TRUE - successful, FALSE - failure */
  314. /**********************************************************************/
  315. BOOL WINAPI ImeUnregisterWord(
  316. LPCTSTR lpszReading,
  317. DWORD dwStyle,
  318. LPCTSTR lpszString)
  319. {
  320. return (FALSE);
  321. }
  322. /**********************************************************************/
  323. /* ImeGetRegsisterWordStyle */
  324. /* Return Value: */
  325. /* number of styles copied/required */
  326. /**********************************************************************/
  327. UINT WINAPI ImeGetRegisterWordStyle(
  328. UINT nItem,
  329. LPSTYLEBUF lpStyleBuf)
  330. {
  331. return (FALSE);
  332. }
  333. /**********************************************************************/
  334. /* ImeEnumRegisterWord */
  335. /* Return Value: */
  336. /* the last value return by the callback function */
  337. /**********************************************************************/
  338. UINT WINAPI ImeEnumRegisterWord(
  339. REGISTERWORDENUMPROC lpfnRegisterWordEnumProc,
  340. LPCTSTR lpszReading,
  341. DWORD dwStyle,
  342. LPCTSTR lpszString,
  343. LPVOID lpData)
  344. {
  345. return (FALSE);
  346. }
  347. #endif //EUDC