-
Notifications
You must be signed in to change notification settings - Fork 0
/
DictionarySearch.cpp
251 lines (222 loc) · 7 KB
/
DictionarySearch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
#include "Private.h"
#include "DictionarySearch.h"
#include "SampleIMEBaseStructure.h"
//+---------------------------------------------------------------------------
//
// ctor
//
//----------------------------------------------------------------------------
CDictionarySearch::CDictionarySearch(LCID locale, _In_ CFile *pFile, _In_ CStringRange *pSearchKeyCode)
: CDictionaryParser(locale)
{
_pFile = pFile;
_pSearchKeyCode = pSearchKeyCode;
_charIndex = 0;
}
//+---------------------------------------------------------------------------
//
// dtor
//
//----------------------------------------------------------------------------
CDictionarySearch::~CDictionarySearch()
{
}
//+---------------------------------------------------------------------------
//
// FindPhrase
//
//----------------------------------------------------------------------------
BOOL CDictionarySearch::FindPhrase(_Out_ CDictionaryResult **ppdret)
{
return FindWorker(FALSE, ppdret, FALSE); // NO WILDCARD
}
//+---------------------------------------------------------------------------
//
// FindPhraseForWildcard
//
//----------------------------------------------------------------------------
BOOL CDictionarySearch::FindPhraseForWildcard(_Out_ CDictionaryResult **ppdret)
{
return FindWorker(FALSE, ppdret, TRUE); // Wildcard
}
//+---------------------------------------------------------------------------
//
// FindConvertedStringForWildcard
//
//----------------------------------------------------------------------------
BOOL CDictionarySearch::FindConvertedStringForWildcard(CDictionaryResult **ppdret)
{
return FindWorker(TRUE, ppdret, TRUE); // Wildcard
}
//+---------------------------------------------------------------------------
//
// FindWorker
//
//----------------------------------------------------------------------------
BOOL CDictionarySearch::FindWorker(BOOL isTextSearch, _Out_ CDictionaryResult **ppdret, BOOL isWildcardSearch)
{
DWORD_PTR dwTotalBufLen = GetBufferInWCharLength(); // in char
if (dwTotalBufLen == 0)
{
return FALSE;
}
const WCHAR *pwch = GetBufferInWChar();
DWORD_PTR indexTrace = 0; // in char
*ppdret = nullptr;
BOOL isFound = FALSE;
DWORD_PTR bufLenOneLine = 0;
TryAgain:
bufLenOneLine = GetOneLine(&pwch[indexTrace], dwTotalBufLen);
if (bufLenOneLine == 0)
{
goto FindNextLine;
}
else
{
CParserStringRange keyword;
DWORD_PTR bufLen = 0;
LPWSTR pText = nullptr;
if (!ParseLine(&pwch[indexTrace], bufLenOneLine, &keyword))
{
return FALSE; // error
}
if (!isTextSearch)
{
// Compare Dictionary key code and input key code
if (!isWildcardSearch)
{
if (CStringRange::Compare(_locale, &keyword, _pSearchKeyCode) != CSTR_EQUAL)
{
if (bufLen)
{
delete[] pText;
}
goto FindNextLine;
}
}
else
{
// Wildcard search
if (!CStringRange::WildcardCompare(_locale, _pSearchKeyCode, &keyword))
{
if (bufLen)
{
delete[] pText;
}
goto FindNextLine;
}
}
}
else
{
// Compare Dictionary converted string and input string
CSampleImeArray<CParserStringRange> convertedStrings;
if (!ParseLine(&pwch[indexTrace], bufLenOneLine, &keyword, &convertedStrings))
{
if (bufLen)
{
delete[] pText;
}
return FALSE;
}
if (convertedStrings.Count() == 1)
{
CStringRange *pTempString = convertedStrings.GetAt(0);
if (!isWildcardSearch)
{
if (CStringRange::Compare(_locale, pTempString, _pSearchKeyCode) != CSTR_EQUAL)
{
if (bufLen)
{
delete[] pText;
}
goto FindNextLine;
}
}
else
{
// Wildcard search
if (!CStringRange::WildcardCompare(_locale, _pSearchKeyCode, pTempString))
{
if (bufLen)
{
delete[] pText;
}
goto FindNextLine;
}
}
}
else
{
if (bufLen)
{
delete[] pText;
}
goto FindNextLine;
}
}
if (bufLen)
{
delete[] pText;
}
// Prepare return's CDictionaryResult
*ppdret = new (std::nothrow) CDictionaryResult();
if (!*ppdret)
{
return FALSE;
}
CSampleImeArray<CParserStringRange> valueStrings;
if (!ParseLine(&pwch[indexTrace], bufLenOneLine, &keyword, &valueStrings))
{
if (*ppdret)
{
delete *ppdret;
*ppdret = nullptr;
}
return FALSE;
}
(*ppdret)->_FindKeyCode = keyword;
(*ppdret)->_SearchKeyCode = *_pSearchKeyCode;
for (UINT i = 0; i < valueStrings.Count(); i++)
{
CStringRange *findPhrase = (*ppdret)->_FindPhraseList.Append();
if (findPhrase)
{
*findPhrase = *valueStrings.GetAt(i);
}
}
// Seek to next line
isFound = TRUE;
}
FindNextLine:
dwTotalBufLen -= bufLenOneLine;
if (dwTotalBufLen == 0)
{
indexTrace += bufLenOneLine;
_charIndex += indexTrace;
if (!isFound && *ppdret)
{
delete *ppdret;
*ppdret = nullptr;
}
return (isFound ? TRUE : FALSE); // End of file
}
indexTrace += bufLenOneLine;
if (pwch[indexTrace] == L'\r' || pwch[indexTrace] == L'\n' || pwch[indexTrace] == L'\0')
{
bufLenOneLine = 1;
goto FindNextLine;
}
if (isFound)
{
_charIndex += indexTrace;
return TRUE;
}
goto TryAgain;
}