forked from jrywu/DIME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DictionaryParser.cpp
320 lines (268 loc) · 8.57 KB
/
DictionaryParser.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
//
// Derived from Microsoft Sample IME by Jeremy '13,7,17
//
//
#include "Private.h"
#include "DictionaryParser.h"
#include "BaseStructure.h"
//---------------------------------------------------------------------
//
// ctor
//
//---------------------------------------------------------------------
CDictionaryParser::CDictionaryParser(LCID locale, _In_ WCHAR keywordDelimiter)
{
_locale = locale;
_keywordDelimiter = keywordDelimiter;
}
//---------------------------------------------------------------------
//
// dtor
//
//---------------------------------------------------------------------
CDictionaryParser::~CDictionaryParser()
{
}
//---------------------------------------------------------------------
//
// ParseLine
//
// dwBufLen - in character count
//
//---------------------------------------------------------------------
BOOL CDictionaryParser::ParseLine(_In_reads_(dwBufLen) LPCWSTR pwszBuffer, DWORD_PTR dwBufLen, _Out_ CParserStringRange *psrgKeyword,
_Inout_opt_ CDIMEArray<CParserStringRange> *pValue, _In_opt_ BOOL ttsPhraseSearch, _In_opt_ CStringRange *searchText)
{
LPCWSTR pwszKeyWordDelimiter = nullptr;
pwszKeyWordDelimiter = GetToken(pwszBuffer, dwBufLen, _keywordDelimiter, psrgKeyword);
if (!(pwszKeyWordDelimiter))
{
return FALSE; // no delimiter found in line.
}
dwBufLen -= (pwszKeyWordDelimiter - pwszBuffer);
pwszBuffer = pwszKeyWordDelimiter + 1;
dwBufLen--;
// Get value.
if (pValue)
{
if (dwBufLen)
{
CParserStringRange localKeyword;
if(ttsPhraseSearch)
{
do{
pwszKeyWordDelimiter = GetToken(pwszBuffer, dwBufLen,L',', &localKeyword);
CParserStringRange* psrgValue = pValue->Append();
if (!psrgValue)
return FALSE;
PWCHAR pwch = new (std::nothrow) WCHAR[psrgKeyword->GetLength() + localKeyword.GetLength() + 2];
if (!pwch) continue;
if ((pwszKeyWordDelimiter))
{
dwBufLen -= (pwszKeyWordDelimiter - pwszBuffer);
pwszBuffer = pwszKeyWordDelimiter + 1;
dwBufLen--;
StringCchCopyN(pwch, psrgKeyword->GetLength() + localKeyword.GetLength() + 2, localKeyword.Get(),localKeyword.GetLength());
psrgValue->Set(pwch, wcslen(pwch));
RemoveWhiteSpaceFromBegin(psrgValue);
RemoveWhiteSpaceFromEnd(psrgValue);
RemoveStringDelimiter(psrgValue);
}else{ //end of line. append the last item
localKeyword.Set(pwszBuffer, dwBufLen);
RemoveWhiteSpaceFromBegin(&localKeyword);
RemoveWhiteSpaceFromEnd(&localKeyword);
RemoveStringDelimiter(&localKeyword);
StringCchCopyN(pwch, psrgKeyword->GetLength() + localKeyword.GetLength() + 2, localKeyword.Get(),localKeyword.GetLength());
psrgValue->Set(pwch, wcslen(pwch));
}
}while(pwszKeyWordDelimiter);
}else
{
CParserStringRange* psrgValue = pValue->Append();
if (!psrgValue)
return FALSE;
if(searchText == NULL)
{
psrgValue->Set(pwszBuffer, dwBufLen);
}
else
{
DWORD_PTR searchTextLen;
searchTextLen = searchText->GetLength();
localKeyword.Set(pwszBuffer, dwBufLen);
RemoveWhiteSpaceFromBegin(&localKeyword);
RemoveWhiteSpaceFromEnd(&localKeyword);
RemoveStringDelimiter(&localKeyword);
if(searchTextLen >1)
searchTextLen--;//search text always in the form xxx* here. remove the * wildcard in last word.
if(localKeyword.GetLength() == searchTextLen)//the first one is itself, thus dwbuflen = searchTextLen here
psrgValue->Set(localKeyword.Get(), localKeyword.GetLength());
else
psrgValue->Set(localKeyword.Get() + searchTextLen, localKeyword.GetLength() - searchTextLen);
}
RemoveWhiteSpaceFromBegin(psrgValue);
RemoveWhiteSpaceFromEnd(psrgValue);
RemoveStringDelimiter(psrgValue);
}
}
}
return TRUE;
}
//---------------------------------------------------------------------
//
// GetToken
//
// dwBufLen - in character count
//
// return - pointer of delimiter which specified chDelimiter
//
//---------------------------------------------------------------------
_Ret_maybenull_
LPCWSTR CDictionaryParser::GetToken(_In_reads_(dwBufLen) LPCWSTR pwszBuffer, DWORD_PTR dwBufLen, _In_ const WCHAR chDelimiter, _Out_ CParserStringRange *psrgValue)
{
WCHAR ch = '\0';
WCHAR pch = '\0';
psrgValue->Set(pwszBuffer, dwBufLen);
ch = *pwszBuffer;
while ((ch) && (ch != chDelimiter) && dwBufLen)
{
dwBufLen--;
pwszBuffer++;
if ((ch == Global::StringDelimiter) && (pch != L'\\')) // ignore escaped quote \\" which is not a quote acutally
{
while (*pwszBuffer && (*pwszBuffer != Global::StringDelimiter || (*(pwszBuffer-1) == L'\\')&&(*(pwszBuffer-2) != L'\\')) && dwBufLen)
{
dwBufLen--;
pwszBuffer++;
}
if (*pwszBuffer && dwBufLen)
{
dwBufLen--;
pwszBuffer++;
}
else
{
return nullptr;
}
}
ch = *pwszBuffer;
pch = *(pwszBuffer-1);
}
if (*pwszBuffer && dwBufLen)
{
LPCWSTR pwszStart = psrgValue->Get();
psrgValue->Set(pwszStart, pwszBuffer - pwszStart);
RemoveWhiteSpaceFromBegin(psrgValue);
RemoveWhiteSpaceFromEnd(psrgValue);
RemoveStringDelimiter(psrgValue);
return pwszBuffer;
}
RemoveWhiteSpaceFromBegin(psrgValue);
RemoveWhiteSpaceFromEnd(psrgValue);
RemoveStringDelimiter(psrgValue);
return nullptr;
}
//---------------------------------------------------------------------
//
// RemoveWhiteSpaceFromBegin
// RemoveWhiteSpaceFromEnd
// RemoveStringDelimiter
//
//---------------------------------------------------------------------
BOOL CDictionaryParser::RemoveWhiteSpaceFromBegin(_Inout_opt_ CStringRange *pString)
{
DWORD_PTR dwIndexTrace = 0; // in char
if (pString == nullptr)
{
return FALSE;
}
if (SkipWhiteSpace(_locale, pString->Get(), pString->GetLength(), &dwIndexTrace) != S_OK)
{
return FALSE;
}
pString->Set(pString->Get() + dwIndexTrace, pString->GetLength() - dwIndexTrace);
return TRUE;
}
BOOL CDictionaryParser::RemoveWhiteSpaceFromEnd(_Inout_opt_ CStringRange *pString)
{
if (pString == nullptr)
{
return FALSE;
}
DWORD_PTR dwTotalBufLen = pString->GetLength();
LPCWSTR pwszEnd = pString->Get() + dwTotalBufLen - 1;
while (dwTotalBufLen && (IsSpace(_locale, *pwszEnd) || *pwszEnd == L'\r' || *pwszEnd == L'\n'))
{
pwszEnd--;
dwTotalBufLen--;
}
pString->Set(pString->Get(), dwTotalBufLen);
return TRUE;
}
BOOL CDictionaryParser::RemoveStringDelimiter(_Inout_opt_ CStringRange *pString)
{
if (pString == nullptr)
{
return FALSE;
}
if (pString->GetLength() >= 2)
{
if ((*pString->Get() == Global::StringDelimiter) && (*(pString->Get()+pString->GetLength()-1) == Global::StringDelimiter))
{
pString->Set(pString->Get()+1, pString->GetLength()-2);
CParserStringRange localKeyword;
LPCWSTR pwszKeyWordDelimiter = nullptr;
pwszKeyWordDelimiter = GetToken(pString->Get(), pString->GetLength(),L'\\', &localKeyword);//check for backslash '\\' escape code and remove then.
if(pwszKeyWordDelimiter)
{
PWCHAR pwchNoEscape = new (std::nothrow) WCHAR[pString->GetLength() + 1];
if (pwchNoEscape == nullptr) return FALSE;
*pwchNoEscape = L'\0';
const WCHAR *pwch = pString->Get();
DWORD_PTR index = 0;
for(UINT i=0;i < pString->GetLength(); i++)
{
WCHAR wch = pwch[i];
if(wch == L'\\')
{
if(i > 0 && (pwch[i-1] == L'\\'))
{
StringCchCatN(pwchNoEscape, pString->GetLength() + 1, L"\\", 1);
index ++;
}
}
else
{
StringCchCatN(pwchNoEscape, pString->GetLength() + 1, &wch, 1);
index ++;
}
}
pString->Set(pwchNoEscape, index);
}
return TRUE;
}
}
return FALSE;
}
//---------------------------------------------------------------------
//
// GetOneLine
//
// dwBufLen - in character count
//
//---------------------------------------------------------------------
DWORD_PTR CDictionaryParser::GetOneLine(_In_z_ LPCWSTR pwszBuffer, DWORD_PTR dwBufLen)
{
DWORD_PTR dwIndexTrace = 0; // in char
if(pwszBuffer == nullptr) return 0;
while(dwIndexTrace <dwBufLen)
{
if (*(pwszBuffer + dwIndexTrace) == L'\r' || *(pwszBuffer + dwIndexTrace) == L'\n' || *(pwszBuffer + dwIndexTrace) == L'\0')
{
return dwIndexTrace;
}
dwIndexTrace++;
}
return dwIndexTrace;
}