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.

213 lines
4.7 KiB

  1. /****************************************************************************
  2. Lookup.cpp : implementation of various lookup functions
  3. Copyright 2000 Microsoft Corp.
  4. History:
  5. 02-AUG-2000 bhshin remove unused method for Hand Writing team
  6. 17-MAY-2000 bhshin remove unused method for CICERO
  7. 02-FEB-2000 bhshin created
  8. ****************************************************************************/
  9. #include "private.h"
  10. #include "Lookup.h"
  11. #include "Hanja.h"
  12. #include "trie.h"
  13. // LookupHanjaIndex
  14. //
  15. // get the hanja index with code value
  16. // it's needed for just K0/K1 lex
  17. //
  18. // Parameters:
  19. // pLexMap -> (MAPFILE*) ptr to lexicon map struct
  20. // wchHanja -> (WCHAR) hanja unicode
  21. //
  22. // Result:
  23. // (-1 if not found, otherwise return index value)
  24. //
  25. // 02FEB2000 bhshin began
  26. int LookupHanjaIndex(MAPFILE *pLexMap, WCHAR wchHanja)
  27. {
  28. unsigned char *pLex;
  29. LEXICON_HEADER *pLexHeader;
  30. DWORD dwOffset;
  31. DWORD dwIndex;
  32. unsigned short *pIndex;
  33. // parameter validation
  34. if (pLexMap == NULL)
  35. return FALSE;
  36. if (pLexMap->pvData == NULL)
  37. return FALSE;
  38. pLex = (unsigned char*)pLexMap->pvData;
  39. pLexHeader = (LEXICON_HEADER*)pLexMap->pvData;
  40. dwOffset = pLexHeader->rgnHanjaIdx;
  41. if (fIsExtAHanja(wchHanja))
  42. {
  43. dwIndex = (wchHanja - HANJA_EXTA_START);
  44. }
  45. else if (fIsCJKHanja(wchHanja))
  46. {
  47. dwIndex = HANJA_EXTA_END - HANJA_EXTA_START + 1;
  48. dwIndex += (wchHanja - HANJA_CJK_START);
  49. }
  50. else if (fIsCompHanja(wchHanja))
  51. {
  52. dwIndex = HANJA_EXTA_END - HANJA_EXTA_START + 1;
  53. dwIndex += HANJA_CJK_END - HANJA_CJK_START + 1;
  54. dwIndex += (wchHanja - HANJA_COMP_START);
  55. }
  56. else
  57. {
  58. // unknown input
  59. return -1;
  60. }
  61. pIndex = (WCHAR*)(pLex + dwOffset);
  62. return pIndex[dwIndex];
  63. }
  64. // HanjaToHangul
  65. //
  66. // lookup Hanja reading
  67. //
  68. // Parameters:
  69. // pLexMap -> (MAPFILE*) ptr to lexicon map struct
  70. //
  71. // Result:
  72. // (NULL if error, otherwise matched Hangul)
  73. //
  74. // 02FEB2000 bhshin began
  75. WCHAR HanjaToHangul(MAPFILE *pLexMap, WCHAR wchHanja)
  76. {
  77. unsigned char *pLex;
  78. LEXICON_HEADER *pLexHeader;
  79. DWORD dwOffset;
  80. int nIndex;
  81. WCHAR *pwchReading;
  82. // parameter validation
  83. if (pLexMap == NULL)
  84. return FALSE;
  85. if (pLexMap->pvData == NULL)
  86. return FALSE;
  87. pLex = (unsigned char*)pLexMap->pvData;
  88. pLexHeader = (LEXICON_HEADER*)pLexMap->pvData;
  89. dwOffset = pLexHeader->rgnReading;
  90. nIndex = LookupHanjaIndex(pLexMap, wchHanja);
  91. if (nIndex == -1)
  92. {
  93. return NULL; // not found;
  94. }
  95. pwchReading = (WCHAR*)(pLex + dwOffset);
  96. return pwchReading[nIndex];
  97. }
  98. // LookupHangulOfHanja
  99. //
  100. // lookup hangul of input hanja string
  101. //
  102. // Parameters:
  103. // pLexMap -> (MAPFILE*) ptr to lexicon map struct
  104. // lpcwszHanja -> (LPCWSTR) input hanja string
  105. // cchHanja -> (int) length of input hanja
  106. // wzHangul -> (LPWSTR) output hangul string
  107. // cchHangul -> (int) output buffer size
  108. //
  109. // Result:
  110. // (FALSE if error occurs, otherwise return TRUE)
  111. //
  112. // 02FEB2000 bhshin began
  113. BOOL LookupHangulOfHanja(MAPFILE *pLexMap, LPCWSTR lpcwszHanja, int cchHanja,
  114. LPWSTR wzHangul, int cchHangul)
  115. {
  116. WCHAR wchHangul;
  117. if (cchHangul < cchHanja)
  118. return FALSE; // output buffer is too small
  119. for (int i = 0; i < cchHanja; i++)
  120. {
  121. wchHangul = HanjaToHangul(pLexMap, lpcwszHanja[i]);
  122. if (wchHangul == NULL)
  123. return FALSE; // unknown hanja included
  124. wzHangul[i] = wchHangul;
  125. }
  126. wzHangul[i] = L'\0';
  127. return TRUE;
  128. }
  129. // LookupMeaning
  130. //
  131. // lookup hanja meaning
  132. //
  133. // Parameters:
  134. // pLexMap -> (MAPFILE*) ptr to lexicon map struct
  135. // wchHanja -> (WCHAR) input hanja
  136. // wzMean -> (WCHAR*) output meaning buffer
  137. // cchMean -> (int) output meaning buffer size
  138. //
  139. // Result:
  140. // (FALSE if error occurs, otherwise return TRUE)
  141. //
  142. // 09FEB2000 bhshin began
  143. BOOL LookupMeaning(MAPFILE *pLexMap, WCHAR wchHanja, WCHAR *wzMean, int cchMean)
  144. {
  145. unsigned char *pLex;
  146. LEXICON_HEADER *pLexHeader;
  147. int nIndex;
  148. TRIECTRL *lpTrieCtrl;
  149. BOOL fFound;
  150. unsigned short *pidxMean;
  151. int idxMean;
  152. // parameter validation
  153. if (pLexMap == NULL)
  154. return FALSE;
  155. if (pLexMap->pvData == NULL)
  156. return FALSE;
  157. pLex = (unsigned char*)pLexMap->pvData;
  158. pLexHeader = (LEXICON_HEADER*)pLexMap->pvData;
  159. nIndex = LookupHanjaIndex(pLexMap, wchHanja);
  160. if (nIndex == -1)
  161. {
  162. return FALSE; // not found;
  163. }
  164. // meaning
  165. pidxMean = (unsigned short*)(pLex + pLexHeader->rgnMeanIdx);
  166. idxMean = pidxMean[nIndex];
  167. lpTrieCtrl = TrieInit(pLex + pLexHeader->rgnMeaning);
  168. if (lpTrieCtrl == NULL)
  169. return FALSE;
  170. fFound = TrieIndexToWord(lpTrieCtrl, idxMean, wzMean, cchMean);
  171. if (!fFound)
  172. {
  173. wzMean[0] = L'\0';
  174. }
  175. TrieFree(lpTrieCtrl);
  176. return TRUE;
  177. }