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.

228 lines
6.1 KiB

  1. /*************************************************
  2. * convlist.c *
  3. * *
  4. * Copyright (C) 1999 Microsoft Inc. *
  5. * *
  6. *************************************************/
  7. #include <windows.h>
  8. #include <immdev.h>
  9. #include "imeattr.h"
  10. #include "imedefs.h"
  11. /**********************************************************************/
  12. /* Conversion() */
  13. /**********************************************************************/
  14. DWORD PASCAL Conversion(
  15. LPCTSTR lpszReading,
  16. LPCANDIDATELIST lpCandList,
  17. DWORD dwBufLen)
  18. {
  19. UINT uMaxCand;
  20. DWORD dwSize = // similar to ClearCand
  21. // header length
  22. sizeof(CANDIDATELIST) +
  23. // candidate string pointers
  24. sizeof(DWORD) * MAXCAND +
  25. // string plus NULL terminator
  26. (sizeof(WCHAR) + sizeof(TCHAR)) * MAXCAND;
  27. PRIVCONTEXT ImcP;
  28. if (!dwBufLen) {
  29. return (dwSize);
  30. }
  31. uMaxCand = dwBufLen - sizeof(CANDIDATELIST);
  32. uMaxCand /= sizeof(DWORD) + sizeof(WCHAR) + sizeof(TCHAR);
  33. if (!uMaxCand) {
  34. // can not even put one string
  35. return (0);
  36. }
  37. lpCandList->dwSize = dwSize;
  38. lpCandList->dwStyle = IME_CAND_READ; // candidate having same reading
  39. lpCandList->dwCount = 0;
  40. lpCandList->dwSelection = 0;
  41. lpCandList->dwPageStart = 0;
  42. lpCandList->dwPageSize = CANDPERPAGE;
  43. lpCandList->dwOffset[0] = sizeof(CANDIDATELIST) + sizeof(DWORD) *
  44. (uMaxCand - 1);
  45. SearchTbl( 0, lpCandList, &ImcP);
  46. return (dwSize);
  47. }
  48. /**********************************************************************/
  49. /* SearchOffset() */
  50. /* Return Value : */
  51. /* the offset in table file which include this uOffset */
  52. /**********************************************************************/
  53. UINT PASCAL SearchOffset(
  54. LPBYTE lpTbl,
  55. UINT uTblSize,
  56. UINT uOffset)
  57. {
  58. int iLo, iMid, iHi;
  59. LPWORD lpwPtr;
  60. iLo = 0;
  61. iHi = uTblSize / sizeof(WORD);
  62. iMid = (iLo + iHi) / 2;
  63. // binary search
  64. for (; iLo <= iHi; ) {
  65. lpwPtr = (LPWORD)lpTbl + iMid;
  66. if (uOffset > *lpwPtr) {
  67. iLo = iMid + 1;
  68. } else if (uOffset < *lpwPtr) {
  69. iHi = iMid - 1;
  70. } else {
  71. break;
  72. }
  73. iMid = (iLo + iHi) / 2;
  74. }
  75. if (iMid > 0) {
  76. iLo = iMid - 1;
  77. } else {
  78. iLo = 0;
  79. }
  80. iHi = uTblSize / sizeof(WORD);
  81. lpwPtr = (LPWORD)lpTbl + iLo;
  82. for (; iLo < iHi; iLo++, lpwPtr++) {
  83. if (*lpwPtr > uOffset) {
  84. return (iLo - 1);
  85. }
  86. }
  87. return (0);
  88. }
  89. /**********************************************************************/
  90. /* ReverseConversion() */
  91. /**********************************************************************/
  92. DWORD PASCAL ReverseConversion(
  93. UINT uCode,
  94. LPCANDIDATELIST lpCandList,
  95. DWORD dwBufLen)
  96. {
  97. UINT uMaxCand;
  98. DWORD dwSize = // similar to ClearCand
  99. // header length
  100. sizeof(CANDIDATELIST) +
  101. // candidate string pointers
  102. sizeof(DWORD) * MAX_COMP +
  103. // string plus NULL terminator
  104. (sizeof(WCHAR) * lpImeL->nMaxKey + sizeof(TCHAR));
  105. UINT uTmpCode;
  106. int i;
  107. if (!dwBufLen) {
  108. return (dwSize);
  109. }
  110. uMaxCand = dwBufLen - sizeof(CANDIDATELIST);
  111. uMaxCand /= sizeof(DWORD) +
  112. (sizeof(WCHAR) * lpImeL->nMaxKey + sizeof(TCHAR));
  113. if (uMaxCand == 0) {
  114. // can not put one string
  115. return (0);
  116. }
  117. lpCandList->dwSize = sizeof(CANDIDATELIST) +
  118. sizeof(DWORD) * uMaxCand +
  119. (sizeof(WCHAR) * lpImeL->nMaxKey + sizeof(TCHAR));
  120. lpCandList->dwStyle = IME_CAND_READ;
  121. lpCandList->dwCount = 0;
  122. lpCandList->dwSelection = 0;
  123. lpCandList->dwPageSize = CANDPERPAGE;
  124. lpCandList->dwOffset[0] = sizeof(CANDIDATELIST) + sizeof(DWORD) *
  125. (uMaxCand - 1);
  126. uTmpCode = uCode;
  127. for (i = lpImeL->nMaxKey - 1; i >= 0; i--) {
  128. UINT uCompChar;
  129. uCompChar = lpImeL->wSeq2CompTbl[(uTmpCode & 0xF) + 1];
  130. *(LPWSTR)((LPBYTE)lpCandList + lpCandList->dwOffset[
  131. lpCandList->dwCount] + sizeof(WCHAR) * i) = (WCHAR)uCompChar;
  132. uTmpCode >>= 4;
  133. }
  134. // null terminator
  135. *(LPTSTR)((LPBYTE)lpCandList + lpCandList->dwOffset[
  136. lpCandList->dwCount] + sizeof(WCHAR) * lpImeL->nMaxKey) = '\0';
  137. // string count ++
  138. lpCandList->dwCount++;
  139. return (dwSize);
  140. }
  141. /**********************************************************************/
  142. /* ImeConversionList() / UniImeConversionList() */
  143. /**********************************************************************/
  144. DWORD WINAPI ImeConversionList(
  145. HIMC hIMC,
  146. LPCTSTR lpszSrc,
  147. LPCANDIDATELIST lpCandList,
  148. DWORD dwBufLen,
  149. UINT uFlag)
  150. {
  151. UINT uCode;
  152. if (!dwBufLen) {
  153. } else if (!lpszSrc) {
  154. return (0);
  155. } else if (!*lpszSrc) {
  156. return (0);
  157. } else if (!lpCandList) {
  158. return (0);
  159. } else if (dwBufLen <= sizeof(CANDIDATELIST)) {
  160. // buffer size can not even put the header information
  161. return (0);
  162. } else {
  163. }
  164. switch (uFlag) {
  165. case GCL_CONVERSION:
  166. return Conversion(
  167. lpszSrc, lpCandList, dwBufLen);
  168. break;
  169. case GCL_REVERSECONVERSION:
  170. if (!dwBufLen) {
  171. return ReverseConversion(
  172. 0, lpCandList, dwBufLen);
  173. }
  174. // only support one DBCS char reverse conversion
  175. if (*(LPTSTR)((LPBYTE)lpszSrc + sizeof(WORD)) != '\0') {
  176. return (0);
  177. }
  178. uCode = *(LPUNAWORD)lpszSrc;
  179. return ReverseConversion(
  180. uCode, lpCandList, dwBufLen);
  181. break;
  182. case GCL_REVERSE_LENGTH:
  183. return sizeof(WCHAR);
  184. break;
  185. default:
  186. return (0);
  187. break;
  188. }
  189. }