forked from mcneel/rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LBP_XML.cpp
3462 lines (2806 loc) · 64.6 KB
/
LBP_XML.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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include "LBP_XML.h"
#include "LBPUnicodeTextFile.h"
#include "LBPColor.h"
#include "LBPException.h"
#include "LBP_UTF.h"
#include "LBPBase64.h"
#include "LBP_CRC32.h"
#include "LBPLibUtilities.h"
class CXMLCriticalSection
{
public:
CXMLCriticalSection(CCriticalSection& cs) : m_CS(cs) { m_CS.Lock(); }
~CXMLCriticalSection() { m_CS.Unlock(); }
private:
CCriticalSection& m_CS;
};
bool CLBP_XMLNode::m_bAutoTypePropValue = false;
static const wchar_t* StringFromPropType(CLBPVariant::vt vt)
{
switch (vt)
{
case CLBPVariant::vt_Integer: return L"int";
case CLBPVariant::vt_Float: return L"float";
case CLBPVariant::vt_Double: return L"double";
case CLBPVariant::vt_String: return L"string";
case CLBPVariant::vt_Bool: return L"bool";
case CLBPVariant::vt_Matrix: return L"matrix";
case CLBPVariant::vt_Uuid: return L"uuid";
case CLBPVariant::vt_Time: return L"time";
case CLBPVariant::vt_Buffer: return L"buffer";
case CLBPVariant::vt_4_double_color: return L"color";
case CLBPVariant::vt_2_double_array: return L"2da";
case CLBPVariant::vt_3_double_array: return L"3da";
case CLBPVariant::vt_4_double_array: return L"4da";
}
return L"null";
}
static CLBPVariant::vt PropTypeFromString(const CLBPString& s)
{
if (L"int" == s) return CLBPVariant::vt_Integer;
if (L"float" == s) return CLBPVariant::vt_Float;
if (L"double" == s) return CLBPVariant::vt_Double;
if (L"string" == s) return CLBPVariant::vt_String;
if (L"bool" == s) return CLBPVariant::vt_Bool;
if (L"matrix" == s) return CLBPVariant::vt_Matrix;
if (L"uuid" == s) return CLBPVariant::vt_Uuid;
if (L"time" == s) return CLBPVariant::vt_Time;
if (L"buffer" == s) return CLBPVariant::vt_Buffer;
if (L"color" == s) return CLBPVariant::vt_4_double_color;
if (L"2da" == s) return CLBPVariant::vt_2_double_array;
if (L"3da" == s) return CLBPVariant::vt_3_double_array;
if (L"4da" == s) return CLBPVariant::vt_4_double_array;
return CLBPVariant::vt_Null;
}
REFCOUNT_INT g_lNodeCount = 0;
int LBP_GetNodeCount() { return g_lNodeCount; }
REFCOUNT_INT g_lPropertyCount = 0;
int LBP_GetPropertyCount() { return g_lPropertyCount; }
#ifdef _DEBUG
#define LBPXML_PROFILER_START XMLProfiler.StartWatch()
#define LBPXML_PROFILER_STOP XMLProfiler.StopWatch()
#else
#define LBPXML_PROFILER_START
#define LBPXML_PROFILER_STOP
#endif
int g_iWarningsFlagCounter = 0;
bool ThrowWarningExceptions()
{
return g_iWarningsFlagCounter > 0;
}
void AutoTypeVariant(CLBPVariant& v);
CLBP_XMLNode::CLBP_XMLNode(const CLBPString& sName)
{
::InterlockedIncrement(&g_lNodeCount);
SetTagName(sName);
AddEmptyDefaultProperty();
}
CLBP_XMLNode::CLBP_XMLNode(const CLBP_XMLNode& src)
{
::InterlockedIncrement(&g_lNodeCount);
*this = src;
}
CLBP_XMLNode::~CLBP_XMLNode()
{
InternalRemoveAllProperties();
RemoveAllChildren();
::InterlockedDecrement(&g_lNodeCount);
}
CLBP_XMLNode& CLBP_XMLNode::operator=(const CLBP_XMLNode& src)
{
CXMLCriticalSection cs1(m_CS);
CXMLCriticalSection cs2(src.m_CS);
InternalRemoveAllProperties();
RemoveAllChildren();
m_sName = src.m_sName;
// Copy in the properties.
CLBP_XMLProperty* pProperty = NULL;
CPropertyIterator pi = src.GetPropertyIterator();
while (NULL != (pProperty = pi.GetNextProperty()))
{
InternalAddProperty(*pProperty); // This does a copy anyway - no need to call the copy constructor
}
// Copy in the children.
CLBP_XMLNode* pChild = NULL;
CChildIterator ci = src.GetChildIterator();
while (NULL != (pChild = ci.GetNextChild()))
{
AddChildNode(new CLBP_XMLNode(*pChild));
}
return *this;
}
void CLBP_XMLNode::AssignFast(const CLBP_XMLNode& src)
{
*this = src;
}
bool CLBP_XMLNode::MergeFrom(const CLBP_XMLNode& src)
{
CXMLCriticalSection cs1(m_CS);
CXMLCriticalSection cs2(src.m_CS);
if (m_sName.operator != (src.TagName()))
return false;
//Copy in the parameters
CLBP_XMLProperty* pProperty = NULL;
CPropertyIterator pi = src.GetPropertyIterator();
while(NULL != (pProperty = pi.GetNextProperty()))
{
//Replaces any that are already there.
AddProperty(pProperty);
}
//Copy in the children
CLBP_XMLNode* pChild = NULL;
CChildIterator ci = src.GetChildIterator();
BOOL bNeedToMerge = ChildrenCount() != 0;
while (NULL != (pChild = ci.GetNextChild()))
{
CLBPString sName = pChild->TagName();
CLBP_XMLNode* pLocalChildNode = bNeedToMerge ? GetNodeAtPath(pChild->TagName()) : NULL;
if (pLocalChildNode)
{
pLocalChildNode->MergeFrom(*pChild);
}
else
{
AddChildNode(new CLBP_XMLNode(*pChild));
}
}
return true;
}
void CLBP_XMLNode::Clear(void)
{
SetTagName(L"");
RemoveAllProperties();
RemoveAllChildren();
}
void CLBP_XMLNode::AddEmptyDefaultProperty(void)
{
static CLBP_XMLProperty empty_prop(L"");
AddProperty(&empty_prop);
}
void CLBP_XMLNode::RemoveAllChildren(void)
{
CXMLCriticalSection cs(m_CS);
if (NULL != m_pFirstChild)
{
CChildIterator it(this);
CLBP_XMLNode* pNode = NULL;
do
{
pNode = it.GetNextChild();
delete pNode;
}
while (NULL != pNode);
m_pFirstChild = NULL;
m_pLastChild = NULL;
}
}
void CLBP_XMLNode::InternalRemoveAllProperties(void)
{
if (NULL != m_pFirstProperty)
{
CLBP_XMLProperty* pProp = NULL;
CPropertyIterator pit(this);
do
{
pProp = pit.GetNextProperty();
delete pProp;
}
while (NULL != pProp);
m_pFirstProperty = NULL;
}
}
CLBP_XMLNode* CLBP_XMLNode::InternalUnhookChild(CLBP_XMLNode& childNode)
{
CLBP_XMLNode* pChildNode = &childNode;
if (pChildNode->m_pParent != this)
return NULL;
if (m_pFirstChild == pChildNode)
{
if (m_pLastChild == m_pFirstChild)
{
m_pLastChild = pChildNode->m_pNextSibling;
}
m_pFirstChild = pChildNode->m_pNextSibling;
return pChildNode;
}
else
{
//First the child which points to this one
CLBP_XMLNode* pNode = m_pFirstChild;
while (NULL != pNode)
{
if (pNode->m_pNextSibling == pChildNode)
{
pNode->m_pNextSibling = pChildNode->m_pNextSibling;
if (NULL == pNode->m_pNextSibling)
{
m_pLastChild = pNode;
}
return pChildNode;
}
pNode = pNode->m_pNextSibling;
}
}
return NULL;
}
void CLBP_XMLNode::RemoveAllProperties(void)
{
CXMLCriticalSection cs(m_CS);
InternalRemoveAllProperties();
AddEmptyDefaultProperty();
}
const char* CLBP_XMLNode::TagNameMBCS(void) const
{
CXMLCriticalSection cs(m_CS);
return m_sName.AsMBCSString();
}
const WCHAR* CLBP_XMLNode::TagName(void) const
{
CXMLCriticalSection cs(m_CS);
return m_sName.AsWideString();
}
void CLBP_XMLNode::SetTagName(const CLBPString& sName)
{
CXMLCriticalSection cs(m_CS);
ASSERT(!ReadOnly());
if (sName.IsEmpty())
{
m_sName.Empty();
}
else
{
CLBPString& s = m_sName.EmptyStringForWrite();
s = sName;
s.TrimLeft();
s.TrimRight();
}
}
bool CLBP_XMLNode::IsValidXMLName(const CLBPString& sTagName) // Static.
{
const int iLength = sTagName.GetLength();
for (int i = 0; i < iLength; i++)
{
const wchar_t wc = towlower(sTagName[i]);
if ((wc >= L'a') && (wc <= L'z'))
continue;
if (wc == L'_')
continue;
if ((wc >= L'0') && (wc <= L'9'))
continue;
if (i > 0)
{
if (wc == L'-')
continue;
}
OutputDebugString(_T("Invalid XML tag syntax - "));
OutputDebugString(sTagName.T());
OutputDebugString(_T("\n"));
ASSERT(false);
return false;
}
return true;
}
CLBP_XMLNode* CLBP_XMLNode::GetParent(void) const
{
return m_pParent;
}
const CLBP_XMLNode& CLBP_XMLNode::TopmostParent(void) const
{
CXMLCriticalSection cs(m_CS);
const CLBP_XMLNode* p = this;
while (p->m_pParent)
{
p = p->m_pParent;
}
return *p;
}
CLBP_XMLNode* CLBP_XMLNode::AddChildNode(CLBP_XMLNode* pNode)
{
if (NULL == pNode)
return NULL;
CXMLCriticalSection cs(m_CS);
ASSERT(!ReadOnly());
if (NULL == m_pFirstChild)
{
//There are no children - just add one
pNode->m_pNextSibling = NULL;
m_pFirstChild = pNode;
m_pLastChild = pNode;
}
else
{
//There are children - add one to the end
ASSERT(m_pLastChild != NULL);
m_pLastChild->m_pNextSibling = pNode;
pNode->m_pNextSibling = NULL;
m_pLastChild = pNode;
}
pNode->m_pParent = this;
return pNode;
}
CLBP_XMLProperty* CLBP_XMLNode::AddProperty(const CLBP_XMLProperty* pProp)
{
if (NULL == pProp)
return NULL;
return AddProperty(*pProp);
}
CLBP_XMLProperty* CLBP_XMLNode::AddProperty(const CLBP_XMLProperty& prop)
{
CXMLCriticalSection cs(m_CS);
ASSERT(!ReadOnly());
if (ThrowWarningExceptions())
{
const CLBPString sName = prop.Name();
if (sName.Contains(L"\n") || sName.Contains(L"\r"))
{
// The string contain LF or CR codes - this is likely to cause problems but
// is still valid XML.
throw CLBP_XMLException(CLBP_XMLException::eBadCharactersInString, sName);
}
const CLBPString sValue = prop.GetValue();
if (sValue.Contains(L"\n") || sValue.Contains(L"\r"))
{
// The string contain LF or CR codes - this is likely to cause problems but
// is still valid XML.
throw CLBP_XMLException(CLBP_XMLException::eBadCharactersInString, sValue);
}
}
InternalRemoveProperty(prop.Name());
// Copy the property, then add it to the tree
return InternalAddProperty(prop);
}
CLBP_XMLProperty* CLBP_XMLNode::AttachProperty(CLBP_XMLProperty* pProp)
{
if (NULL == pProp)
return NULL;
CXMLCriticalSection cs(m_CS);
ASSERT(!ReadOnly());
InternalRemoveProperty(pProp->Name());
pProp->m_pNextProperty = m_pFirstProperty;
m_pFirstProperty = pProp;
m_pFirstProperty->m_pOwner = this;
return pProp;
}
bool CLBP_XMLNode::RemoveProperty(const CLBPString& sPropertyName)
{
CXMLCriticalSection cs(m_CS);
return InternalRemoveProperty(sPropertyName);
}
CLBP_XMLProperty* CLBP_XMLNode::InternalAddProperty(const CLBP_XMLProperty& prop)
{
CLBP_XMLProperty* pProp = new CLBP_XMLProperty(prop);
pProp->m_pOwner = this;
pProp->m_pNextProperty = m_pFirstProperty;
m_pFirstProperty = pProp;
return pProp;
}
bool CLBP_XMLNode::InternalRemoveProperty(const CLBPString& sName)
{
CLBP_XMLProperty* pPrevious = NULL;
CLBP_XMLProperty* pProp = m_pFirstProperty;
while (NULL != pProp)
{
if (pProp->Name().CompareNoCase(sName.AsWideString()) == 0)
{
if (NULL == pPrevious)
{
m_pFirstProperty = pProp->m_pNextProperty;
}
else
{
pPrevious->m_pNextProperty = pProp->m_pNextProperty;
}
delete pProp;
return true;
}
pPrevious = pProp;
pProp = pProp->m_pNextProperty;
}
return false;
}
void CLBP_XMLNode::Remove(void)
{
CLBP_XMLNode* pParent = GetParent();
if (NULL != pParent)
{
pParent->RemoveChild(this);
}
else
{
delete this;
}
}
CLBP_XMLNode* CLBP_XMLNode::UnhookChild(CLBP_XMLNode* pChildNode)
{
if (NULL == pChildNode)
return NULL;
CXMLCriticalSection cs(m_CS);
ASSERT(!ReadOnly());
return InternalUnhookChild(*pChildNode);
}
bool CLBP_XMLNode::RemoveChild(CLBP_XMLNode* pChildNode)
{
if (NULL == pChildNode)
return NULL;
CXMLCriticalSection cs(m_CS);
ASSERT(!ReadOnly());
CLBP_XMLNode* pChild = InternalUnhookChild(*pChildNode);
delete pChild;
return pChild != NULL;
}
CLBP_XMLNode::CChildIterator CLBP_XMLNode::GetChildIterator() const
{
return CChildIterator(this);
}
CLBP_XMLNode::CPropertyIterator CLBP_XMLNode::GetPropertyIterator(bool bAlphabetized) const
{
return CPropertyIterator(this, bAlphabetized);
}
DWORD CLBP_XMLNode::PropertyCount() const
{
CXMLCriticalSection cs(m_CS);
CPropertyIterator it = GetPropertyIterator();
DWORD iCount = 0;
while (it.GetNextProperty())
{
iCount++;
}
return iCount;
}
DWORD CLBP_XMLNode::ChildrenCount() const
{
CXMLCriticalSection cs(m_CS);
CChildIterator it = GetChildIterator();
DWORD iCount = 0;
while (it.GetNextChild())
{
iCount++;
}
return iCount;
}
bool CLBP_XMLNode::HasDefaultProperty() const
{
return true; // !!GetNamedProperty(L"");
}
int CLBP_XMLNode::GetNestedDepth() const
{
CXMLCriticalSection cs(m_CS);
int iDepth = 0;
const CLBP_XMLNode* pNode = this;
while (NULL != pNode->m_pParent)
{
pNode = pNode->m_pParent;
iDepth++;
}
return iDepth;
}
static bool PrependNodeToStringAndRecurseParents(const CLBP_XMLNode* pNode, CLBPString& s)
{
// Recursive function to pile up the path.
if (NULL == pNode)
return false;
CLBP_XMLNode* pParent = pNode->GetParent();
if (NULL != pParent)
{
//CLBPString sNodeName = pNode->TagName();
//s = sNodeName + L"/" + s;
s.Insert(0, L'/', 1);
s.Insert(0, pNode->TagName());
PrependNodeToStringAndRecurseParents(pParent, s);
return true;
}
return false;
}
CLBPStringBV CLBP_XMLNode::GetPathFromRoot() const
{
CXMLCriticalSection cs(m_CS);
CLBPStringBV sPath(TagName());
PrependNodeToStringAndRecurseParents(GetParent(), sPath);
return sPath;
}
CLBP_XMLProperty& CLBP_XMLNode::GetDefaultProperty(void) const
{
static CLBPString empty(L"");
CLBP_XMLProperty* pProp = GetNamedProperty(empty);
ASSERT(NULL != pProp); // Always created by constructor.
return *pProp;
}
CLBP_XMLProperty* CLBP_XMLNode::GetNamedProperty(const wchar_t* wszName) const
{
CXMLCriticalSection cs(m_CS);
CPropertyIterator it = GetPropertyIterator();
CLBP_XMLProperty* pProp = NULL;
while (NULL != (pProp = it.GetNextProperty()))
{
if (_wcsicmp(wszName, pProp->Name()) == 0)
return pProp;
}
return NULL;
}
CLBP_XMLNode* CLBP_XMLNode::GetNamedChild(const wchar_t* wszName) const
{
CXMLCriticalSection cs(m_CS);
CChildIterator it = GetChildIterator();
CLBP_XMLNode* pNode = NULL;
while (NULL != (pNode = it.GetNextChild()))
{
if (_wcsicmp(wszName, pNode->TagName()) == 0)
return pNode;
}
return NULL;
}
CLBP_XMLNode* CLBP_XMLNode::GetNodeAtPathImpl(const wchar_t* wszPath, bool bCreate)
{
//Forward slash "/" is the separator
if (NULL == wszPath)
return const_cast<CLBP_XMLNode*>(this);
const wchar_t* p = wszPath;
while (*p != 0)
{
if (!iswspace(*p) && *p!=L'/')
break;
p++;
}
//The input string was empty
if (*p == 0)
return const_cast<CLBP_XMLNode*>(this);
WCHAR wsz[260];
wcsncpy(wsz, p, 260);
//Now right trim out the whitespace
const int iLength = (int)wcslen(wsz);
for (int i = iLength - 1; i >= 0; i--)
{
if (iswspace(wsz[i]))
wsz[i] = 0;
else
break;
}
//Check for a resultant empty string
if (*wsz == 0)
return const_cast<CLBP_XMLNode*>(this);
const wchar_t* pstr = wcschr(wsz, L'/');
const int iPos = pstr != NULL ? (int)(pstr-wsz) : -1;
const wchar_t* pNext = NULL;
if (-1 != iPos)
{
//sNext is the rest of the string that we're going to resurse through
pNext = wsz + iPos + 1;
wsz[iPos] = 0;
}
CChildIterator it = GetChildIterator();
CLBP_XMLNode* pChild = NULL;
while (NULL != (pChild = it.GetNextChild()))
{
if (_wcsicmp(wsz, pChild->TagName()) == 0)
{
return pChild->GetNodeAtPathImpl(pNext, bCreate);
}
}
// The child was not found.
if (bCreate)
{
return AddChildNode(new CLBP_XMLNode(wsz))->GetNodeAtPathImpl(pNext, bCreate);
}
return NULL;
}
LPVOID CLBP_XMLNode::LastReadBufferPointer() const
{
return m_pLastReadBufferPointer;
}
void CLBP_XMLNode::SetLastReadBufferPointer(LPVOID p)
{
m_pLastReadBufferPointer = p;
}
DWORD CLBP_XMLNode::ReadFromStreamNoThrow(const WCHAR* wszStream, bool bWarningsAsErrors, bool bValidateTags)
{
// Added a simple check for an empty input stream to stop this thing from outputting first-chance exceptions
// to the output line in normal operation and freaking out Dale.
if (NULL == wszStream || 0 == *wszStream)
{
return LBP_XML_READ_ERROR;
}
try
{
return ReadFromStreamThrow(wszStream, bWarningsAsErrors, bValidateTags);
}
catch(...)
{
return LBP_XML_READ_ERROR;
}
}
DWORD CLBP_XMLNode::ReadFromStreamThrow(const WCHAR* wszStream, bool bWarningsAsErrors, bool bValidateTags)
{
if(bWarningsAsErrors) g_iWarningsFlagCounter++;
Clear();
//Search for the opening tag "<" character
WCHAR* pBuffer = const_cast<WCHAR*>(wszStream);
CLBPString tag;
GetNextTag(tag, pBuffer, bValidateTags);
SetLastReadBufferPointer((LPVOID)wszStream);
if (!tag.IsEmpty())
{
GetPropertiesFromTag(tag);
}
if (tag.GetAtWide(tag.GetLength()-2) != L'/')
{
//This tag either has children, or a default property
CLBPString sDefaultProperty;
BOOL bClosingTag = FALSE;
do
{
WCHAR* pStoreBuffer = pBuffer;
//Get any crap between the tags and store as the default parameter
WCHAR* pStart = pBuffer;
while (*pStart != L'<' && *pStart != 0) pStart++;
//These two lines account for 5% of the time
//CLBPString s(pBuffer, (int)(pStart-pBuffer));
//We ran off the end of the buffer - no idea why, but this is bad, so throw an exception
//See http://mcneel.myjetbrains.com/youtrack/issue/RH-16605 for example
if (0 == *pStart)
{
throw CLBP_XMLException(CLBP_XMLException::eMalformedTagOnRead, tag);
}
ASSERT(*pStart==L'<');
if (NULL!=pStart && *pStart==L'<')
{
sDefaultProperty.Append(pBuffer, (int)(pStart-pBuffer));
}
pBuffer = pStart;
GetNextTag(tag, pBuffer, bValidateTags);
bClosingTag = IsClosingTag(tag);
if (!bClosingTag)
{
CLBP_XMLNode* pNode = new CLBP_XMLNode(L"");
AddChildNode(pNode);
pBuffer = pStoreBuffer + pNode->ReadFromStreamThrow(pStoreBuffer, bWarningsAsErrors, bValidateTags);
}
}
while (!bClosingTag);
sDefaultProperty.TrimLeft();
sDefaultProperty.TrimRight();
sDefaultProperty.URLUnencode();
if (!sDefaultProperty.IsEmpty())
{
CLBP_XMLProperty* pProp = new CLBP_XMLProperty;
AttachProperty(pProp);
const int pos = m_bAutoTypePropValue ? sDefaultProperty.Find(L":") : -1;
if (pos > 0)
{
// The type is encoded in the value.
const CLBPVariant::vt type = PropTypeFromString(sDefaultProperty.Left(pos));
CLBPVariant& v = pProp->GetNonConstValue();
v = sDefaultProperty.Mid(pos+1);
v.SetTypePendingFlag(true);
v.DoAutoTyping(type);
}
else
{
const int iLength = sDefaultProperty.GetLength();
pProp->SetHugeStringValue(sDefaultProperty.ReleaseOwnership(), iLength);
pProp->GetNonConstValue().SetTypePendingFlag(true);
}
}
}
if (bWarningsAsErrors) g_iWarningsFlagCounter--;
TopmostParent().OnNodeReadFromStream(this);
return (DWORD)(pBuffer-wszStream);
}
bool CLBP_XMLNode::GetPropertiesFromTag(const CLBPString& sTag)
{
SetTagName(GetNameFromTag(sTag));
CLBPString tag(sTag);
tag.TrimLeft(L"</ ");
tag.TrimRight(L">/ ");
int iPos = tag.Find(L' ');
if (iPos == -1)
return true; //No properties
tag.TruncateMid(iPos+1);
CLBPString sPropertyName, sPropertyValue;
CLBPVariant vValue;
//We are now at the start of the properties
bool bClear = false;
while (!tag.IsEmpty())
{
iPos = tag.Find(L'=');
if (iPos != -1)
{
sPropertyName = tag.Left(iPos);
sPropertyName.TrimLeft();
//tag = tag.Mid(iPos);
tag.TruncateMid(iPos);
iPos = tag.Find(L'\"');
if (-1 != iPos)
{
//0.061 - this bit
tag.TruncateMid(iPos+1);
iPos = tag.Find(L'\"');
//
if (-1 != iPos)
{
sPropertyValue = tag.Left(iPos);
tag.TruncateMid(iPos+1);
tag.TrimLeft();
sPropertyValue.URLUnencode();
const int pos = m_bAutoTypePropValue ? sPropertyValue.Find(L':') : -1;
if (pos > 0)
{
// The type is encoded in the value.
const CLBPVariant::vt type = PropTypeFromString(sPropertyValue.Left(pos));
vValue = sPropertyValue.Mid(pos+1);
vValue.SetTypePendingFlag(true);
vValue.DoAutoTyping(type);
}
else
{
// This allows for lazy typing of the property
vValue.SetTypePendingFlag(true);
vValue = sPropertyValue;
// AutoTypeVariant(vValue); John commented this out because it made some of his strings turn into ints. But is this right?
}
AttachProperty(new CLBP_XMLProperty(sPropertyName, vValue));
bClear = true;
}
}
}
if (!bClear)
{
throw CLBP_XMLException(CLBP_XMLException::eMalformedTagOnRead, sTag);
}
bClear = false;
tag.TrimLeft();
}
return true;
}
bool CLBP_XMLNode::IsClosingTag(const CLBPString& sTag) const
{
if (sTag.GetAtWide(1) != L'/')
return false;
if (GetNameFromTag(sTag).CompareNoCase(TagName()) == 0)
return true;
return false;
}
CLBPStringBV CLBP_XMLNode::GetNameFromTag(const CLBPString& sTag) // Static.
{
CLBPStringBV tag(sTag);
tag.TrimLeft(L"</ ");
tag.TrimRight(L">/ ");
const int iPos = tag.Find(L' ');
if (iPos != -1)
{
tag.Truncate(iPos);
}
return tag;
}
bool CLBP_XMLNode::RecoverProperty(const CLBPString& tag, int iEqualSign, CLBPString& sProp) // Static.
{
//Move left, looking for a space and ensuring every character is a valid name char
ASSERT(tag.GetAtWide(iEqualSign) == L'=');
int iLeftScan = iEqualSign-1;
CLBPString sName;