-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDictionaryParser.cpp
225 lines (189 loc) · 5.96 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
// 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 "DictionaryParser.h"
#include "SampleIMEBaseStructure.h"
//---------------------------------------------------------------------
//
// ctor
//
//---------------------------------------------------------------------
CDictionaryParser::CDictionaryParser(LCID locale)
{
_locale = locale;
}
//---------------------------------------------------------------------
//
// dtor
//
//---------------------------------------------------------------------
CDictionaryParser::~CDictionaryParser()
{
}
//---------------------------------------------------------------------
//
// ParseLine
//
// dwBufLen - in character count
//
//---------------------------------------------------------------------
BOOL CDictionaryParser::ParseLine(_In_reads_(dwBufLen) LPCWSTR pwszBuffer, DWORD_PTR dwBufLen,
_Out_ CParserStringRange *psrgKeyword,
_Inout_opt_ CSampleImeArray<CParserStringRange> *pValue)
{
LPCWSTR pwszKeyWordDelimiter = nullptr;
pwszKeyWordDelimiter = GetToken(pwszBuffer, dwBufLen, Global::KeywordDelimiter, psrgKeyword);
if (!(pwszKeyWordDelimiter))
{
return FALSE; // End of file
}
dwBufLen -= (pwszKeyWordDelimiter - pwszBuffer);
pwszBuffer = pwszKeyWordDelimiter + 1;
dwBufLen--;
// Get value.
if (pValue)
{
if (dwBufLen)
{
CParserStringRange *psrgValue = pValue->Append();
if (!psrgValue)
{
return FALSE;
}
psrgValue->Set(pwszBuffer, dwBufLen);
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';
psrgValue->Set(pwszBuffer, dwBufLen);
ch = *pwszBuffer;
while ((ch) && (ch != chDelimiter) && dwBufLen)
{
dwBufLen--;
pwszBuffer++;
if (ch == Global::StringDelimiter)
{
while (*pwszBuffer && (*pwszBuffer != Global::StringDelimiter) && dwBufLen)
{
dwBufLen--;
pwszBuffer++;
}
if (*pwszBuffer && dwBufLen)
{
dwBufLen--;
pwszBuffer++;
}
else
{
return nullptr;
}
}
ch = *pwszBuffer;
}
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);
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 (FAILED(FindChar(L'\r', pwszBuffer, dwBufLen, &dwIndexTrace)))
{
if (FAILED(FindChar(L'\0', pwszBuffer, dwBufLen, &dwIndexTrace)))
{
return dwBufLen;
}
}
return dwIndexTrace;
}