-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIMarkup.cpp
More file actions
461 lines (413 loc) · 13.5 KB
/
UIMarkup.cpp
File metadata and controls
461 lines (413 loc) · 13.5 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "StdAfx.h"
#ifndef TRACE
#define TRACE
#endif
///////////////////////////////////////////////////////////////////////////////////////
//
//
//
CMarkupNode::CMarkupNode() : m_pOwner(NULL)
{
}
CMarkupNode::CMarkupNode(CMarkup* pOwner, int iPos) : m_pOwner(pOwner), m_iPos(iPos), m_nAttributes(0)
{
}
CMarkupNode CMarkupNode::GetSibling()
{
if( m_pOwner == NULL ) return CMarkupNode();
ULONG iPos = m_pOwner->m_pElements[m_iPos].iNext;
if( iPos == 0 ) return CMarkupNode();
return CMarkupNode(m_pOwner, iPos);
}
bool CMarkupNode::HasSiblings() const
{
if( m_pOwner == NULL ) return false;
ULONG iPos = m_pOwner->m_pElements[m_iPos].iNext;
return iPos > 0;
}
CMarkupNode CMarkupNode::GetChild()
{
if( m_pOwner == NULL ) return CMarkupNode();
ULONG iPos = m_pOwner->m_pElements[m_iPos].iChild;
if( iPos == 0 ) return CMarkupNode();
return CMarkupNode(m_pOwner, iPos);
}
CMarkupNode CMarkupNode::GetChild(LPCTSTR pstrName)
{
if( m_pOwner == NULL ) return CMarkupNode();
ULONG iPos = m_pOwner->m_pElements[m_iPos].iChild;
while( iPos != 0 ) {
if( _tcscmp(m_pOwner->m_pstrXML + m_pOwner->m_pElements[iPos].iStart, pstrName) == 0 ) {
return CMarkupNode(m_pOwner, iPos);
}
iPos = m_pOwner->m_pElements[iPos].iNext;
}
return CMarkupNode();
}
bool CMarkupNode::HasChildren() const
{
if( m_pOwner == NULL ) return false;
return m_pOwner->m_pElements[m_iPos].iChild != 0;
}
CMarkupNode CMarkupNode::GetParent()
{
if( m_pOwner == NULL ) return CMarkupNode();
ULONG iPos = m_pOwner->m_pElements[m_iPos].iParent;
if( iPos == 0 ) return CMarkupNode();
return CMarkupNode(m_pOwner, iPos);
}
bool CMarkupNode::IsValid() const
{
return m_pOwner != NULL;
}
LPCTSTR CMarkupNode::GetName() const
{
if( m_pOwner == NULL ) return NULL;
return m_pOwner->m_pstrXML + m_pOwner->m_pElements[m_iPos].iStart;
}
LPCTSTR CMarkupNode::GetValue() const
{
if( m_pOwner == NULL ) return NULL;
return m_pOwner->m_pstrXML + m_pOwner->m_pElements[m_iPos].iData;
}
LPCTSTR CMarkupNode::GetAttributeName(int iIndex)
{
if( m_pOwner == NULL ) return NULL;
if( m_nAttributes == 0 ) _MapAttributes();
if( iIndex < 0 || iIndex >= m_nAttributes ) return _T("");
return m_pOwner->m_pstrXML + m_aAttributes[iIndex].iName;
}
LPCTSTR CMarkupNode::GetAttributeValue(int iIndex)
{
if( m_pOwner == NULL ) return NULL;
if( m_nAttributes == 0 ) _MapAttributes();
if( iIndex < 0 || iIndex >= m_nAttributes ) return _T("");
return m_pOwner->m_pstrXML + m_aAttributes[iIndex].iValue;
}
LPCTSTR CMarkupNode::GetAttributeValue(LPCTSTR pstrName)
{
if( m_pOwner == NULL ) return NULL;
if( m_nAttributes == 0 ) _MapAttributes();
for( int i = 0; i < m_nAttributes; i++ ) {
if( _tcscmp(m_pOwner->m_pstrXML + m_aAttributes[i].iName, pstrName) == 0 ) return m_pOwner->m_pstrXML + m_aAttributes[i].iValue;
}
return _T("");
}
bool CMarkupNode::GetAttributeValue(int iIndex, LPTSTR pstrValue, SIZE_T cchMax)
{
if( m_pOwner == NULL ) return false;
if( m_nAttributes == 0 ) _MapAttributes();
if( iIndex < 0 || iIndex >= m_nAttributes ) return false;
_tcsncpy(pstrValue, m_pOwner->m_pstrXML + m_aAttributes[iIndex].iValue, cchMax);
return true;
}
bool CMarkupNode::GetAttributeValue(LPCTSTR pstrName, LPTSTR pstrValue, SIZE_T cchMax)
{
if( m_pOwner == NULL ) return false;
if( m_nAttributes == 0 ) _MapAttributes();
for( int i = 0; i < m_nAttributes; i++ ) {
if( _tcscmp(m_pOwner->m_pstrXML + m_aAttributes[i].iName, pstrName) == 0 ) {
_tcsncpy(pstrValue, m_pOwner->m_pstrXML + m_aAttributes[i].iValue, cchMax);
return true;
}
}
return false;
}
int CMarkupNode::GetAttributeCount()
{
if( m_pOwner == NULL ) return 0;
if( m_nAttributes == 0 ) _MapAttributes();
return m_nAttributes;
}
bool CMarkupNode::HasAttributes()
{
if( m_pOwner == NULL ) return false;
if( m_nAttributes == 0 ) _MapAttributes();
return m_nAttributes > 0;
}
bool CMarkupNode::HasAttribute(LPCTSTR pstrName)
{
if( m_pOwner == NULL ) return false;
if( m_nAttributes == 0 ) _MapAttributes();
for( int i = 0; i < m_nAttributes; i++ ) {
if( _tcscmp(m_pOwner->m_pstrXML + m_aAttributes[i].iName, pstrName) == 0 ) return true;
}
return false;
}
void CMarkupNode::_MapAttributes()
{
m_nAttributes = 0;
LPCTSTR pstr = m_pOwner->m_pstrXML + m_pOwner->m_pElements[m_iPos].iStart;
LPCTSTR pstrEnd = m_pOwner->m_pstrXML + m_pOwner->m_pElements[m_iPos].iData;
pstr += _tcslen(pstr) + 1;
while( pstr < pstrEnd ) {
m_pOwner->_SkipWhitespace(pstr);
m_aAttributes[m_nAttributes].iName = pstr - m_pOwner->m_pstrXML;
pstr += _tcslen(pstr) + 1;
if( *pstr++ != '\"' && *pstr++ != '\'' ) return;
m_aAttributes[m_nAttributes++].iValue = pstr - m_pOwner->m_pstrXML;
if( m_nAttributes >= MAX_XML_ATTRIBUTES ) return;
pstr += _tcslen(pstr) + 1;
}
}
///////////////////////////////////////////////////////////////////////////////////////
//
//
//
CMarkup::CMarkup(LPCTSTR pstrXML)
{
m_pstrXML = NULL;
m_pElements = NULL;
m_nElements = 0;
m_bPreserveWhitespace = false;
if( pstrXML != NULL ) Load(pstrXML);
}
CMarkup::~CMarkup()
{
Release();
}
bool CMarkup::IsValid() const
{
return m_pElements != NULL;
}
void CMarkup::SetPreserveWhitespace(bool bPreserve)
{
m_bPreserveWhitespace = bPreserve;
}
bool CMarkup::Load(LPCTSTR pstrXML)
{
Release();
SIZE_T cbLen = (_tcslen(pstrXML) + 1) * sizeof(TCHAR);
m_pstrXML = static_cast<LPTSTR>(malloc(cbLen));
::CopyMemory(m_pstrXML, pstrXML, cbLen);
bool bRes = _Parse();
if( !bRes ) Release();
return bRes;
}
bool CMarkup::LoadFromFile(LPCTSTR pstrFilename)
{
Release();
HANDLE hFile = ::CreateFile(pstrFilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( hFile == INVALID_HANDLE_VALUE ) return _Failed(_T("Error opening file"));
DWORD dwSize = ::GetFileSize(hFile, NULL);
if( dwSize == 0 ) return _Failed(_T("File is empty"));
DWORD dwRead = 0;
#ifdef _UNICODE
// BUG: We don't support UNICODE file loading yet.
::CloseHandle(hFile);
return false;
#else
m_pstrXML = static_cast<LPTSTR>(malloc(dwSize + 1));
::ReadFile(hFile, m_pstrXML, dwSize, &dwRead, NULL);
::CloseHandle(hFile);
m_pstrXML[dwSize] = '\0';
#endif // _UNICODE
if( dwRead != dwSize ) {
Release();
return _Failed(_T("Could not read file"));
}
bool bRes = _Parse();
if( !bRes ) Release();
return bRes;
}
void CMarkup::Release()
{
if( m_pstrXML != NULL ) free(m_pstrXML);
if( m_pElements != NULL ) free(m_pElements);
m_pstrXML = NULL;
m_pElements = NULL;
m_nElements;
}
void CMarkup::GetLastErrorMessage(LPTSTR pstrMessage, SIZE_T cchMax) const
{
_tcsncpy(pstrMessage, m_szErrorMsg, cchMax);
}
void CMarkup::GetLastErrorLocation(LPTSTR pstrSource, SIZE_T cchMax) const
{
_tcsncpy(pstrSource, m_szErrorXML, cchMax);
}
CMarkupNode CMarkup::GetRoot()
{
if( m_nElements == 0 ) return CMarkupNode();
return CMarkupNode(this, 1);
}
bool CMarkup::_Parse()
{
_ReserveElement(); // Reserve index 0 for errors
::ZeroMemory(m_szErrorMsg, sizeof(m_szErrorMsg));
::ZeroMemory(m_szErrorXML, sizeof(m_szErrorXML));
LPTSTR pstrXML = m_pstrXML;
return _Parse(pstrXML, 0);
}
bool CMarkup::_Parse(LPTSTR& pstrText, ULONG iParent)
{
ULONG iPrevious = 0;
for( ; ; )
{
if( *pstrText == '\0' && iParent <= 1 ) return true;
if( *pstrText != '<' ) return _Failed(_T("Expected start tag"), pstrText);
if( pstrText[1] == '/' ) return true;
*pstrText++ = '\0';
// Skip comment or processing directive
if( *pstrText == '!' || *pstrText == '?' ) {
TCHAR chEnd = *pstrText == '!' ? '-' : '?';
while( *pstrText != '\0' && !(*pstrText == chEnd && *(pstrText + 1) == '>') ) pstrText = ::CharNext(pstrText);
if( *pstrText != '\0' ) pstrText += 2;
_SkipWhitespace(pstrText);
continue;
}
// Fill out element structure
XMLELEMENT* pEl = _ReserveElement();
ULONG iPos = pEl - m_pElements;
pEl->iStart = pstrText - m_pstrXML;
pEl->iParent = iParent;
pEl->iNext = pEl->iChild = 0;
if( iPrevious != 0 ) m_pElements[iPrevious].iNext = iPos;
else if( iParent > 0 ) m_pElements[iParent].iChild = iPos;
iPrevious = iPos;
// Parse name
LPCTSTR pstrName = pstrText;
_SkipIdentifier(pstrText);
LPTSTR pstrNameEnd = pstrText;
if( *pstrText == '\0' ) return _Failed(_T("Error parsing element name"), pstrText);
// Parse attributes
if( !_ParseAttributes(pstrText) ) return false;
_SkipWhitespace(pstrText);
if( pstrText[0] == '/' && pstrText[1] == '>' )
{
pEl->iData = pstrText - m_pstrXML;
*pstrText = '\0';
pstrText += 2;
}
else
{
if( *pstrText != '>' ) return _Failed(_T("Expected start-tag closing"), pstrText);
// Parse node data
pEl->iData = ++pstrText - m_pstrXML;
LPTSTR pstrDest = pstrText;
if( !_ParseData(pstrText, pstrDest, '<') ) return false;
// Determine type of next element
if( *pstrText == '\0' && iParent <= 1 ) return true;
if( *pstrText != '<' ) return _Failed(_T("Expected end-tag start"), pstrText);
if( pstrText[0] == '<' && pstrText[1] != '/' )
{
if( !_Parse(pstrText, iPos) ) return false;
}
if( pstrText[0] == '<' && pstrText[1] == '/' )
{
*pstrDest = '\0';
*pstrText = '\0';
pstrText += 2;
SIZE_T cchName = pstrNameEnd - pstrName;
if( _tcsncmp(pstrText, pstrName, cchName) != 0 ) return _Failed(_T("Unmatched closing tag"), pstrText);
if( pstrText[cchName] != '>' ) return _Failed(_T("Unmatched closing tag"), pstrText);
pstrText += cchName + 1;
}
}
*pstrNameEnd = '\0';
_SkipWhitespace(pstrText);
}
}
CMarkup::XMLELEMENT* CMarkup::_ReserveElement()
{
if( m_nElements == 0 ) m_nReservedElements = 0;
if( m_nElements >= m_nReservedElements ) {
m_nReservedElements += (m_nReservedElements / 2) + 500;
m_pElements = static_cast<XMLELEMENT*>(realloc(m_pElements, m_nReservedElements * sizeof(XMLELEMENT)));
}
return &m_pElements[m_nElements++];
}
void CMarkup::_SkipWhitespace(LPCTSTR& pstr) const
{
while( *pstr != '\0' && *pstr <= ' ' ) pstr++;
}
void CMarkup::_SkipWhitespace(LPTSTR& pstr) const
{
while( *pstr != '\0' && *pstr <= ' ' ) pstr++;
}
void CMarkup::_SkipIdentifier(LPCTSTR& pstr) const
{
while( *pstr != '\0' && (*pstr == '_' || *pstr == ':' || _istalnum(*pstr)) ) pstr++;
}
void CMarkup::_SkipIdentifier(LPTSTR& pstr) const
{
while( *pstr != '\0' && (*pstr == '_' || *pstr == ':' || _istalnum(*pstr)) ) pstr++;
}
bool CMarkup::_ParseAttributes(LPTSTR& pstrText)
{
if( *pstrText == '>' ) return true;
*pstrText++ = '\0';
_SkipWhitespace(pstrText);
while( *pstrText != '\0' && *pstrText != '>' && *pstrText != '/' ) {
_SkipIdentifier(pstrText);
if( *pstrText != '=' ) return _Failed(_T("Error while parsing attributes"), pstrText);
*pstrText++ = '\0';
TCHAR chQuote = *pstrText++;
if( chQuote != '\"' && chQuote != '\'' ) return _Failed(_T("Expected attribute value"), pstrText);
LPTSTR pstrDest = pstrText;
if( !_ParseData(pstrText, pstrDest, chQuote) ) return false;
if( *pstrText == '\0' ) return _Failed(_T("Error while parsing attribute string"), pstrText);
*pstrDest = '\0';
*pstrText++ = '\0';
_SkipWhitespace(pstrText);
}
return true;
}
bool CMarkup::_ParseData(LPTSTR& pstrText, LPTSTR& pstrDest, char cEnd)
{
while( *pstrText != '\0' && *pstrText != cEnd ) {
if( *pstrText == '&' ) {
_ParseMetaChar(++pstrText, pstrDest);
}
if( *pstrText == ' ' ) {
*pstrDest++ = *pstrText++;
if( !m_bPreserveWhitespace ) _SkipWhitespace(pstrText);
}
else {
*pstrDest++ = *pstrText++;
#ifdef _MBCS
if( ::IsDBCSLeadByte(*(pstrText - 1)) ) *pstrDest++ = *pstrText++;
#endif // _MBCS
}
}
// Make sure that MapAttributes() works correctly when it parses
// over a value that has been transformed.
LPTSTR pstrFill = pstrDest + 1;
while( pstrFill < pstrText ) *pstrFill++ = ' ';
return true;
}
void CMarkup::_ParseMetaChar(LPTSTR& pstrText, LPTSTR& pstrDest)
{
if( pstrText[0] == 'a' && pstrText[1] == 'm' && pstrText[2] == 'p' && pstrText[3] == ';' ) {
*pstrDest++ = '&';
pstrText += 4;
}
else if( pstrText[0] == 'l' && pstrText[1] == 't' && pstrText[2] == ';' ) {
*pstrDest++ = '<';
pstrText += 3;
}
else if( pstrText[0] == 'g' && pstrText[1] == 't' && pstrText[2] == ';' ) {
*pstrDest++ = '>';
pstrText += 3;
}
else if( pstrText[0] == 'q' && pstrText[1] == 'u' && pstrText[2] == 'o' && pstrText[3] == 't' && pstrText[4] == ';' ) {
*pstrDest++ = '\"';
pstrText += 5;
}
else if( pstrText[0] == 'a' && pstrText[1] == 'p' && pstrText[2] == 'o' && pstrText[3] == 's' && pstrText[4] == ';' ) {
*pstrDest++ = '\'';
pstrText += 5;
}
else {
*pstrDest++ = '&';
}
}
bool CMarkup::_Failed(LPCTSTR pstrError, LPCTSTR pstrLocation)
{
// Register last error
TRACE("XML Error: %s", pstrError);
TRACE(pstrLocation);
_tcsncpy(m_szErrorMsg, pstrError, (sizeof(m_szErrorMsg) / sizeof(m_szErrorMsg[0])) - 1);
_tcsncpy(m_szErrorXML, pstrLocation != NULL ? pstrLocation : _T(""), lengthof(m_szErrorXML) - 1);
return false; // Always return 'false'
}