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
321
322
323
324
325
326
327
328
329
330
331
|
#include "main.h"
#include "./xmlServiceParser.h"
#include "../xml/obj_xml.h"
#include "./service.h"
#include "./ifc_omservicehost.h"
#include "./ifc_omstoragehandler.h"
#include "./ifc_omstoragehandlerenum.h"
#include "./ifc_omstorageext.h"
#include <shlwapi.h>
#include <strsafe.h>
typedef void (CALLBACK *TAGCALLBACK)(XmlServiceParser* /*reader*/, OmService* /*editor*/, LPCWSTR /*value*/);
typedef struct __TAGRECORD
{
LPCWSTR name;
TAGCALLBACK callback;
} TAGRECORD;
static void CALLBACK ParserXml_ReadId(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
INT iVal;
if (StrToIntEx(value, STIF_SUPPORT_HEX, &iVal))
{
service->SetId(iVal);
}
}
static void CALLBACK ParserXml_ReadName(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
service->SetName(value, FALSE);
}
static void CALLBACK ParserXml_ReadUrl(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
service->SetUrl(value, FALSE);
}
static void CALLBACK ParserXml_ReadAuth(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
INT iVal;
if (StrToIntEx(value, STIF_SUPPORT_HEX, &iVal))
{
// service->SetAuth(iVal);
}
}
static void CALLBACK ParserXml_ReadIcon(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
service->SetIcon(value, FALSE);
}
static void CALLBACK ParserXml_ReadThumbnail(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
service->SetThumbnail(value, FALSE);
}
static void CALLBACK ParserXml_ReadScreenshot(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
service->SetScreenshot(value, FALSE);
}
static void CALLBACK ParserXml_ReadVersion(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
INT iVal;
if (StrToIntEx(value, STIF_SUPPORT_HEX, &iVal))
service->SetVersion(iVal);
}
static void CALLBACK ParserXml_ReadGeneration(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
INT iVal;
if (StrToIntEx(value, STIF_SUPPORT_HEX, &iVal))
{
service->SetGeneration(iVal);
}
}
static void CALLBACK ParserXml_ReadDescription(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
service->SetDescription(value, FALSE);
}
static void CALLBACK ParserXml_ReadAuthorFirst(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
service->SetAuthorFirst(value, FALSE);
}
static void CALLBACK ParserXml_ReadAuthorLast(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
service->SetAuthorLast(value, FALSE);
}
static void CALLBACK ParserXml_ReadPublished(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
service->SetPublished(value, FALSE);
}
static void CALLBACK ParserXml_ReadUpdated(XmlServiceParser *reader, OmService *service, LPCWSTR value)
{
service->SetUpdated(value, FALSE);
}
static const TAGRECORD szTagTable[] =
{
{ L"id", ParserXml_ReadId },
{ L"name", ParserXml_ReadName },
{ L"version", ParserXml_ReadVersion },
{ L"icon_url", ParserXml_ReadIcon },
{ L"entry_url", ParserXml_ReadUrl },
{ L"generation", ParserXml_ReadGeneration },
{ L"author_comments_long", ParserXml_ReadDescription },
{ L"author_firstname", ParserXml_ReadAuthorFirst},
{ L"author_lastname", ParserXml_ReadAuthorLast },
{ L"date_published", ParserXml_ReadPublished },
{ L"date_updated", ParserXml_ReadUpdated },
{ L"thumbnail", ParserXml_ReadThumbnail },
{ L"screenshot", ParserXml_ReadScreenshot },
};
XmlServiceParser::XmlServiceParser()
: checkList(NULL), checkSize(0), parser(NULL), service(NULL), result(E_UNEXPECTED)
{
}
XmlServiceParser::~XmlServiceParser()
{
Finish(NULL, NULL);
if (NULL != checkList)
free(checkList);
size_t index = handlerList.size();
while (index--)
handlerList[index]->Release();
}
HRESULT XmlServiceParser::RegisterHandlers(ifc_omstoragehandlerenum *handlerEnum)
{
if (E_PENDING == result)
return E_PENDING;
if (NULL != checkList)
{
free(checkList);
checkList = NULL;
checkSize = 0;
}
size_t index = handlerList.size();
while(index--) handlerList[index]->Release();
handlerList.clear();
if (NULL != handlerEnum)
{
ifc_omstoragehandler *handler;
handlerEnum->Reset();
while(S_OK == handlerEnum->Next(1, &handler, NULL))
{
handlerList.push_back(handler);
}
}
return S_OK;
}
HRESULT XmlServiceParser::Initialize(obj_xml *reader, LPCWSTR match, ifc_omservicehost *host)
{
result = E_UNEXPECTED;
if (NULL != parser) return E_UNEXPECTED;
if (NULL == reader) return E_INVALIDARG;
HRESULT hr;
WCHAR szBuffer[1024] = {0};
LPWSTR cursor = szBuffer;
size_t remaining = ARRAYSIZE(szBuffer);
if (NULL == checkList)
{
size_t checkCount = ARRAYSIZE(szTagTable) + handlerList.size();
checkSize = checkCount/8 + ((checkCount%8) ? 1 : 0);
checkList = (BYTE*)calloc(checkSize, sizeof(BYTE));
if (NULL != checkList)
ZeroMemory(checkList, checkSize);
}
hr = StringCchCopyEx(cursor, remaining, match, &cursor, &remaining, STRSAFE_IGNORE_NULLS);
if (FAILED(hr)) return E_UNEXPECTED;
hr = StringCchCopyEx(cursor, remaining, ((cursor != szBuffer) ? L"\f*" : L"*"), &cursor, &remaining, STRSAFE_IGNORE_NULLS);
if (FAILED(hr)) return E_UNEXPECTED;
parser = reader;
parser->AddRef();
parser->xmlreader_registerCallback(szBuffer, this);
hr = OmService::CreateInstance(0, host, &service);
if (FAILED(hr)) return hr;
service->BeginUpdate();
result = E_PENDING;
return S_OK;
}
HRESULT XmlServiceParser::Finish(HRESULT *parserResult, ifc_omservice **ppService)
{
if (NULL == parser)
return E_UNEXPECTED;
if (E_PENDING == result)
result= S_OK;
buffer.Clear();
if (NULL != checkList)
ZeroMemory(checkList, checkSize);
parser->xmlreader_unregisterCallback(this);
parser->Release();
parser = NULL;
if (NULL != service && 0 == service->GetId())
{
if (SUCCEEDED(result))
result = E_UNEXPECTED;
service->Release();
service = NULL;
}
if (NULL != parserResult)
*parserResult = result;
if (NULL != ppService)
{
if (SUCCEEDED(result))
{
*ppService = service;
service->AddRef();
}
else
{
*ppService = NULL;
}
}
if (NULL != service)
{
service->SetModified(0, (UINT)-1);
service->EndUpdate();
service->Release();
service = NULL;
}
result = E_UNEXPECTED;
return S_OK;
}
HRESULT XmlServiceParser::GetActive()
{
return (E_PENDING == result) ? S_OK : S_FALSE;
}
void XmlServiceParser::OnStartElement(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
{
buffer.Clear();
}
void XmlServiceParser::OnEndElement(const wchar_t *xmlpath, const wchar_t *xmltag)
{
size_t i;
for (i = 0; i < ARRAYSIZE(szTagTable); i++)
{
if (NULL == checkList || 0 == (checkList[i/8] & (0x01 << (i%8))) && E_PENDING == result)
{
if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, szTagTable[i].name, -1, xmltag, -1))
{
checkList[i/8] |= (0x01 << (i%8));
szTagTable[i].callback(this, service, buffer.Get());
return;
}
}
}
size_t handlerSize = handlerList.size();
size_t iCheck = ARRAYSIZE(szTagTable);
for (i = 0; i < handlerSize; i++, iCheck++)
{
if (NULL == checkList || 0 == (checkList[iCheck/8] & (0x01 << (iCheck%8))) && E_PENDING == result)
{
LPCWSTR pszKey;
if (SUCCEEDED(handlerList[i]->GetKey(&pszKey)) &&
CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, pszKey, -1, xmltag, -1))
{
checkList[iCheck/8] |= (0x01 << (iCheck%8));
handlerList[i]->Invoke(service, xmltag, buffer.Get());
return;
}
}
}
}
void XmlServiceParser::OnCharData(const wchar_t *xmlpath, const wchar_t *xmltag, const wchar_t *value)
{
buffer.Append(value);
}
void XmlServiceParser::OnError(int linenum, int errcode, const wchar_t *errstr)
{
result = E_FAIL;
if (NULL != service)
{
service->Release();
service = NULL;
}
}
#define CBCLASS XmlServiceParser
START_DISPATCH;
VCB(ONSTARTELEMENT, OnStartElement)
VCB(ONENDELEMENT, OnEndElement)
VCB(ONCHARDATA, OnCharData)
VCB(ONERROR, OnError)
END_DISPATCH;
#undef CBCLASS
|