-
Notifications
You must be signed in to change notification settings - Fork 0
/
vkxml2pas.pas
5617 lines (5380 loc) · 196 KB
/
vkxml2pas.pas
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
(******************************************************************************
* zlib license *
*============================================================================*
* *
* Copyright (C) 2016-2021, Benjamin Rosseaux ([email protected]) *
* *
* This software is provided 'as-is', without any express or implied *
* warranty. In no event will the authors be held liable for any damages *
* arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not *
* claim that you wrote the original software. If you use this software *
* in a product, an acknowledgement in the product documentation would be *
* appreciated but is not required. *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* 3. This notice may not be removed or altered from any source distribution. *
* *
******************************************************************************)
program vkxml2pas; //this is done with pascalabc in mind as it requires certain workarounds
{$ifdef fpc}
{$mode delphi}
{$ifdef cpui386}
{$define cpu386}
{$endif}
{$ifdef cpu386}
{$asmmode intel}
{$endif}
{$ifdef cpuamd64}
{$asmmode intel}
{$endif}
{$ifdef fpc_little_endian}
{$define little_endian}
{$else}
{$ifdef fpc_big_endian}
{$define big_endian}
{$endif}
{$endif}
{$ifdef fpc_has_internal_sar}
{$define HasSAR}
{$endif}
{-$pic off}
{$define CAN_INLINE}
{$ifdef FPC_HAS_TYPE_EXTENDED}
{$define HAS_TYPE_EXTENDED}
{$else}
{$undef HAS_TYPE_EXTENDED}
{$endif}
{$ifdef FPC_HAS_TYPE_DOUBLE}
{$define HAS_TYPE_DOUBLE}
{$else}
{$undef HAS_TYPE_DOUBLE}
{$endif}
{$ifdef FPC_HAS_TYPE_SINGLE}
{$define HAS_TYPE_SINGLE}
{$else}
{$undef HAS_TYPE_SINGLE}
{$endif}
{$else}
{$if CompilerVersion>=15.0}
{$realcompatibility off}
{$localsymbols on}
{$define little_endian}
{$ifndef cpu64}
{$define cpu32}
{$endif}
{$define delphi}
{$undef HasSAR}
{$define UseDIV}
{$define HAS_TYPE_EXTENDED}
{$define HAS_TYPE_DOUBLE}
{$define HAS_TYPE_SINGLE}
{$elseif}
{$define pascalabc}
{$ifend}
{$endif}
{$ifdef cpu386}
{$define cpux86}
{$endif}
{$ifdef cpuamd64}
{$define cpux86}
{$endif}
{$ifdef Win32}
{$define Windows}
{$endif}
{$ifdef Win64}
{$define Windows}
{$endif}
{$ifdef WinCE}
{$define Windows}
{$endif}
{$ifdef Windows}
{$define Win}
{$endif}
{$ifdef sdl20}
{$define sdl}
{$endif}
{$rangechecks off}
{$extendedsyntax on}
{$writeableconst on}
{$hints off}
{$booleval off}
{$typedaddress off}
{$stackframes off}
{$varstringchecks on}
{$typeinfo on}
{$overflowchecks off}
{$longstrings on}
{$openstrings on}
{$ifndef HAS_TYPE_DOUBLE}
{$error No double floating point precision}
{$endif}
{$ifdef fpc}
{$define CAN_INLINE}
{$else}
{$ifndef pascalabc}
{$undef CAN_INLINE}
{$ifdef ver180}
{$define CAN_INLINE}
{$else}
{$ifdef conditionalexpressions}
{$if CompilerVersion>=18.0}
{$define CAN_INLINE}
{$ifend}
{$endif}
{$endif}
{$endif}
{$endif}
{$ifdef windows}
{$apptype console}
{$endif}
{$ifdef pascalabc}
{$apptype console}
{$reference System.dll}
{$zerobasedstrings on}
{$endif}
{$undef UNICODE}
//pascalabc doesn't need this part
{$ifndef pascalabc}
uses SysUtils,Classes,Contnrs;
// On Windows, Vulkan commands use the stdcall convention
// On Android/ARMv7a, Vulkan functions use the armeabi-v7a-hard calling convention, even if the application's native code is compiled with the armeabi-v7a calling convention.
// On other platforms, use the default calling convention
const CallingConventions='{$ifdef Windows}stdcall;{$else}{$ifdef Android}{$ifdef cpuarm}hardfloat;{$else}cdecl;{$endif}{$else}cdecl;{$endif}{$endif}';
{$else}
uses vkxml2pasutilsabc; //reimplementation of some functions, classes, types
//{$reference vkxml2pasutilsabc.dll} //imitate functions that are too hard
{$endif}
const CommentPadding=80;
{$ifndef pascalabc} //pabc doesn't have {$if}
{$if defined(fpc)}
{$undef OldDelphi}
{$elseif}
{$if defined(conditionalexpressions)}
{$if CompilerVersion>=23.0}
{$undef OldDelphi}
type qword=uint64;
ptruint=NativeUInt;
ptrint=NativeInt;
{$elseif} //pabc skips {$elseif} and allows you to compile
{$define OldDelphi}
{$ifend}
{$elseif}
{$define OldDelphi}
{$ifend}
{$ifend}
{$endif}
{$ifdef OldDelphi}
type qword=int64;
{$ifdef cpu64}
ptruint=qword;
ptrint=int64;
{$else}
ptruint=longword;
ptrint=longint;
{$endif}
{$endif}
{$ifdef pascalabc}
type qword=uint64;
ptruint=NativeUInt;
ptrint=NativeInt;
{$endif}
function UTF32CharToUTF8(CharValue:longword):ansistring;
var Data:array[0..{$ifdef strictutf8}3{$else}5{$endif}] of ansichar;
ResultLen:longint;
begin
if CharValue=0 then begin
result:=#0;
end else begin
if CharValue<=$7f then begin
Data[0]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte(CharValue));
ResultLen:=1;
end else if CharValue<=$7ff then begin
Data[0]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($c0 or ((CharValue shr 6) and $1f)));
Data[1]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or (CharValue and $3f)));
ResultLen:=2;
{$ifdef strictutf8}
end else if CharValue<=$d7ff then begin
Data[0]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($e0 or ((CharValue shr 12) and $0f)));
Data[1]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or ((CharValue shr 6) and $3f)));
Data[2]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or (CharValue and $3f)));
ResultLen:=3;
end else if CharValue<=$dfff then begin
Data[0]:=#$ef; // $fffd
Data[1]:=#$bf;
Data[2]:=#$bd;
ResultLen:=3;
{$endif}
end else if CharValue<=$ffff then begin
Data[0]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($e0 or ((CharValue shr 12) and $0f)));
Data[1]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or ((CharValue shr 6) and $3f)));
Data[2]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or (CharValue and $3f)));
ResultLen:=3;
end else if CharValue<=$1fffff then begin
Data[0]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($f0 or ((CharValue shr 18) and $07)));
Data[1]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or ((CharValue shr 12) and $3f)));
Data[2]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or ((CharValue shr 6) and $3f)));
Data[3]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or (CharValue and $3f)));
ResultLen:=4;
{$ifndef strictutf8}
end else if CharValue<=$3ffffff then begin
Data[0]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($f8 or ((CharValue shr 24) and $03)));
Data[1]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or ((CharValue shr 18) and $3f)));
Data[2]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or ((CharValue shr 12) and $3f)));
Data[3]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or ((CharValue shr 6) and $3f)));
Data[4]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or (CharValue and $3f)));
ResultLen:=5;
end else if CharValue<=$7fffffff then begin
Data[0]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($fc or ((CharValue shr 30) and $01)));
Data[1]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or ((CharValue shr 24) and $3f)));
Data[2]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or ((CharValue shr 18) and $3f)));
Data[3]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or ((CharValue shr 12) and $3f)));
Data[4]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or ((CharValue shr 6) and $3f)));
Data[5]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}(byte($80 or (CharValue and $3f)));
ResultLen:=6;
{$endif}
end else begin
//that part looked identical to another at the top of the function but the block bellow decided not to compile in pabc so it got changed:
Data[0]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}($ef); // $fffd
Data[1]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}($bf);
Data[2]:={$ifndef pascalabc}ansichar{$else}chransi{$endif}($bd);
ResultLen:=3;
end;
SetString(result,pansichar(@Data[0]),ResultLen);
end;
end;
type TXMLClass=class
public
Previous,Next:TXMLClass;
Core:pointer;
constructor Create; overload; {$ifndef pascalabc}virtual;{$endif} //"a constructor can't be virtual"-(c)PascalABC
destructor Destroy; {$ifndef pascalabc}override;{$endif} //here pabc claims the destructor isn't overriden so i ifndef'ed
end;
const MaxListSize=2147483647 div {$ifndef pascalabc}SizeOf(TXMLClass){$else}8{$endif};
type PEngineListClasses=^TXMLClasses;
TXMLClasses=array[0..MaxListSize-1] of TXMLClass;
TXMLClassList=class(TXMLClass)
private
InternalList:PEngineListClasses;
InternalCount,InternalCapacity:longint;
function GetItem(Index:longint):TXMLClass;
procedure SetItem(Index:longint;Value:TXMLClass);
function GetItemPointer(Index:longint):TXMLClass;
public
ClearWithContentDestroying:boolean;
CapacityMinimium:longint;
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Clear;
procedure ClearNoFree;
procedure ClearWithFree;
function Add(Item:TXMLClass):longint;
function Append(Item:TXMLClass):longint;
function AddList(List:TXMLClassList):longint;
function AppendList(List:TXMLClassList):longint;
function NewClass:TXMLClass;
procedure Insert(Index:longint;Item:TXMLClass);
procedure Delete(Index:longint);
procedure DeleteClass(Index:longint);
function Remove(Item:TXMLClass):longint;
function RemoveClass(Item:TXMLClass):longint;
function Find(Item:TXMLClass):longint;
function IndexOf(Item:TXMLClass):longint;
procedure Exchange(Index1,Index2:longint);
procedure SetCapacity(NewCapacity:longint);
procedure SetOptimalCapacity(TargetCapacity:longint);
procedure SetCount(NewCount:longint);
function Push(Item:TXMLClass):longint;
function Pop(var Item:TXMLClass):boolean; overload;
function Pop:TXMLClass; overload;
function Last:TXMLClass;
property Count:longint read InternalCount;
property Capacity:longint read InternalCapacity write SetCapacity;
property Item[Index:longint]:TXMLClass read GetItem write SetItem; default;
property Items[Index:longint]:TXMLClass read GetItem write SetItem;
property PItems[Index:longint]:TXMLClass read GetItemPointer;
end;
TXMLClassLinkedList=class(TXMLClass)
public
ClearWithContentDestroying:boolean;
First,Last:TXMLClass;
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Clear;
procedure ClearNoFree;
procedure ClearWithFree;
procedure Add(Item:TXMLClass);
procedure Append(Item:TXMLClass);
procedure AddLinkedList(List:TXMLClassLinkedList);
procedure AppendLinkedList(List:TXMLClassLinkedList);
procedure Remove(Item:TXMLClass);
procedure RemoveClass(Item:TXMLClass);
procedure Push(Item:TXMLClass);
function Pop(var Item:TXMLClass):boolean; overload;
function Pop:TXMLClass; overload;
function Count:longint;
end;
TXMLStringTreeData=ptrint;
PXMLStringTreeNode=^TXMLStringTreeNode;
TXMLStringTreeNode=record
TheChar:ansichar;
Data:TXMLStringTreeData;
DataExist:boolean;
Prevoius,Next,Up,Down:PXMLStringTreeNode;
end;
TXMLStringTree=class
private
Root:PXMLStringTreeNode;
function CreateStringTreeNode(AChar:ansichar):PXMLStringTreeNode;
procedure DestroyStringTreeNode(Node:PXMLStringTreeNode);
public
constructor Create;
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Clear;
procedure DumpTree;
procedure DumpList;
procedure AppendTo(DestStringTree:TXMLStringTree);
procedure Optimize(DestStringTree:TXMLStringTree);
{$ifndef pascalabc}
function Add(Content:ansistring;Data:TXMLStringTreeData;Replace:boolean=false):boolean;
{$else}
function Add(Content:ansistring;Data:TXMLStringTreeData;Replace:boolean:=false):boolean;
{$endif}
function Delete(Content:ansistring):boolean;
function Find(Content:ansistring;var Data:TXMLStringTreeData):boolean;
function FindEx(Content:ansistring;var Data:TXMLStringTreeData;var Len:longint):boolean;
end;
TXMLString={$ifdef UNICODE}widestring{$else}ansistring{$endif};
TXMLChar={$ifdef UNICODE}widechar{$else}ansichar{$endif};
TXMLParameter=class(TXMLClass)
public
Name:ansistring;
Value:TXMLString;
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Assign(From:TXMLParameter); virtual;
end;
TXMLItemList=class;
TXMLTag=class;
TXMLItem=class(TXMLClass)
public
Items:TXMLItemList;
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Clear; virtual;
procedure Add(Item:TXMLItem);
procedure Assign(From:TXMLItem); virtual;
function FindTag(const TagName:ansistring):TXMLTag;
end;
TXMLItemList=class(TXMLClassList)
private
function GetItem(Index:longint):TXMLItem;
procedure SetItem(Index:longint;Value:TXMLItem);
public
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
function NewClass:TXMLItem;
function FindTag(const TagName:ansistring):TXMLTag;
property Item[Index:longint]:TXMLItem read GetItem write SetItem; {$ifndef pascalabc}default;{$endif}
property Items[Index:longint]:TXMLItem read GetItem write SetItem;
end;
TXMLText=class(TXMLItem)
public
Text:TXMLString;
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Assign(From:TXMLItem); override;
procedure SetText(AText:ansistring);
end;
TXMLCommentTag=class(TXMLItem)
public
Text:ansistring;
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Assign(From:TXMLItem); override;
procedure SetText(AText:ansistring);
end;
TXMLTag=class(TXMLItem)
public
Name:ansistring;
Parameter:array of TXMLParameter;
IsAloneTag:boolean;
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Clear; override;
procedure Assign(From:TXMLItem); override;
function FindParameter(ParameterName:ansistring):TXMLParameter;
{$ifndef pascalabc}
function GetParameter(ParameterName:ansistring;bydefault:ansistring=''):ansistring; //default is reserved in pascalabc, replaced with bydefault
{$else}
function GetParameter(ParameterName:ansistring;bydefault:ansistring:=''):ansistring; //default is reserved in pascalabc, replaced with bydefault
{$endif}
function AddParameter(AParameter:TXMLParameter):boolean; overload;
function AddParameter(Name:ansistring;Value:TXMLString):boolean; overload;
function RemoveParameter(AParameter:TXMLParameter):boolean; overload;
function RemoveParameter(ParameterName:ansistring):boolean; overload;
end;
TXMLProcessTag=class(TXMLTag)
public
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Assign(From:TXMLItem); override;
end;
TXMLScriptTag=class(TXMLItem)
public
Text:ansistring;
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Assign(From:TXMLItem); override;
procedure SetText(AText:ansistring);
end;
TXMLCDataTag=class(TXMLItem)
public
Text:ansistring;
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Assign(From:TXMLItem); override;
procedure SetText(AText:ansistring);
end;
TXMLDOCTYPETag=class(TXMLItem)
public
Text:ansistring;
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Assign(From:TXMLItem); override;
procedure SetText(AText:ansistring);
end;
TXMLExtraTag=class(TXMLItem)
public
Text:ansistring;
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Assign(From:TXMLItem); override;
procedure SetText(AText:ansistring);
end;
TXML=class(TXMLClass)
private
function ReadXMLText:ansistring;
procedure WriteXMLText(Text:ansistring);
public
Root:TXMLItem;
AutomaticAloneTagDetection:boolean;
FormatIndent:boolean;
FormatIndentText:boolean;
constructor Create; {$ifndef pascalabc}override;{$endif}
destructor Destroy; {$ifndef pascalabc}override;{$endif}
procedure Assign(From:TXML);
function Parse(Stream:TStream):boolean;
function Read(Stream:TStream):boolean;
{$ifndef pascalabc}
function Write(Stream:TStream;IdentSize:longint=2):boolean;
{$else}
function Write(Stream:TStream;IdentSize:longint:=2):boolean;
{$endif}
property Text:ansistring read ReadXMLText write WriteXMLText;
end;
{$ifndef pascalabc}
function NextPowerOfTwo(Value:longint;const MinThreshold:longint=0):longint;
{$else}
function NextPowerOfTwo(Value:longint;MinThreshold:longint:=0):longint;
{$endif}
begin
result:=(Value or MinThreshold)-1;
result:=result or (result shr 1);
result:=result or (result shr 2);
result:=result or (result shr 4);
result:=result or (result shr 8);
result:=result or (result shr 16);
inc(result);
end;
const EntityChars:array[1..102,1..2] of TXMLString=(('"',#34),('&',#38),(''',''''),
('<',#60),('>',#62),('€',#128),(' ',#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));
type TEntitiesCharLookUpItem=record
IsEntity:boolean;
Entity:ansistring;
end;
TEntitiesCharLookUpTable=array[0..{$ifdef UNICODE}65535{$else}255{$endif}] of TEntitiesCharLookUpItem;
var EntitiesCharLookUp:TEntitiesCharLookUpTable;
EntityStringTree:TXMLStringTree;
{$ifndef pascalabc}const{$else}var{$endif} EntityInitialized:boolean=false;
procedure InitializeEntites;
var EntityCounter:longint;
begin
if not EntityInitialized then begin
EntityInitialized:=true;
EntityStringTree:=TXMLStringTree.Create;
FillChar(EntitiesCharLookUp,SizeOf(TEntitiesCharLookUpTable),#0);
for EntityCounter:=low(EntityChars) to high(EntityChars) do begin
EntityStringTree.Add(EntityChars[EntityCounter,1],EntityCounter,true);
with EntitiesCharLookUp[ord(EntityChars[EntityCounter,2][1])] do begin
IsEntity:=true;
Entity:=EntityChars[EntityCounter,1];
end;
end;
end;
end;
procedure FinalizeEntites;
begin
if assigned(EntityStringTree) then begin
EntityStringTree.Destroy;
EntityStringTree:=nil;
end;
EntityInitialized:=false;
end;
{$ifndef pascalabc}
function ConvertToEntities(AString:TXMLString;IdentLevel:longint=0):ansistring;
{$else}
function ConvertToEntities(AString:TXMLString;IdentLevel:longint:=0):ansistring;
{$endif}
var Counter,IdentCounter:longint;
c:TXMLChar;
begin
result:='';
for Counter:=1 to length(AString) do begin
c:=AString[Counter];
if c=#13 then begin
if ((Counter+1)<=length(AString)) and (AString[Counter+1]=#10) then begin
continue;
end;
c:=#10;
end;
if EntitiesCharLookUp[ord(c)].IsEntity then begin
result:=result+EntitiesCharLookUp[ord(c)].Entity;
end else if (c=#9) or (c=#10) or (c=#13) or ((c>=#32) and (c<=#127)) then begin
result:=result+c;
if c=#10 then begin
for IdentCounter:=1 to IdentLevel do begin
result:=result+' ';
end;
end;
end else begin
{$ifdef UNICODE}
if c<#255 then begin
result:=result+'&#'+INTTOSTR(ord(c))+';';
end else begin
result:=result+'&#x'+IntToHex(ord(c),4)+';';
end;
{$else}
result:=result+'&#'+INTTOSTR(byte(c))+';';
{$endif}
end;
end;
end;
constructor TXMLClass.Create;
begin
inherited Create;
Previous:=nil;
Next:=nil;
Core:=nil;
end;
destructor TXMLClass.Destroy;
begin
inherited Destroy;
end;
constructor TXMLClassList.Create;
begin
inherited Create;
ClearWithContentDestroying:=false;
InternalCount:=0;
InternalCapacity:=0;
InternalList:=nil;
CapacityMinimium:=0;
Clear;
end;
destructor TXMLClassList.Destroy;
begin
Clear;
if assigned(InternalList) and (InternalCapacity<>0) then begin
FreeMem(InternalList);
end;
inherited Destroy;
end;
procedure TXMLClassList.Clear;
begin
if ClearWithContentDestroying then begin
ClearWithFree;
end else begin
ClearNoFree;
end;
end;
procedure TXMLClassList.ClearNoFree;
begin
SetCount(0);
end;
procedure TXMLClassList.ClearWithFree;
var Counter:longint;
begin
for Counter:=0 to InternalCount-1 do begin
if assigned(InternalList^[Counter]) then begin
try
InternalList^[Counter].Destroy;
except
end;
end;
end;
SetCount(0);
end;
procedure TXMLClassList.SetCapacity(NewCapacity:longint);
begin
if (InternalCapacity<>NewCapacity) and
((NewCapacity>=0) and (NewCapacity<MaxListSize)) then begin
ReallocMem(InternalList,NewCapacity*SizeOf(TXMLClass));
if InternalCapacity<NewCapacity then begin
FillChar(InternalList^[InternalCapacity],(NewCapacity-InternalCapacity)*SizeOf(TXMLClass),#0);
end;
InternalCapacity:=NewCapacity;
end;
end;
procedure TXMLClassList.SetOptimalCapacity(TargetCapacity:longint);
var CapacityMask:longint;
begin
if (TargetCapacity>=0) and (TargetCapacity<MaxListSize) then begin
if TargetCapacity<256 then begin
CapacityMask:=15;
end else if TargetCapacity<1024 then begin
CapacityMask:=255;
end else if TargetCapacity<4096 then begin
CapacityMask:=1023;
end else if TargetCapacity<16384 then begin
CapacityMask:=4095;
end else if TargetCapacity<65536 then begin
CapacityMask:=16383;
end else begin
CapacityMask:=65535;
end;
SetCapacity((TargetCapacity+CapacityMask+CapacityMinimium) and not CapacityMask);
end;
end;
procedure TXMLClassList.SetCount(NewCount:longint);
begin
if (NewCount>=0) and (NewCount<MaxListSize) then begin
SetOptimalCapacity(NewCount);
if InternalCount<NewCount then begin
FillChar(InternalList^[InternalCount],(NewCount-InternalCount)*SizeOf(TXMLClass),#0);
end;
InternalCount:=NewCount;
end;
end;
function TXMLClassList.Add(Item:TXMLClass):longint;
begin
result:=InternalCount;
SetCount(result+1);
InternalList^[result]:=Item;
end;
function TXMLClassList.Append(Item:TXMLClass):longint;
begin
result:=Add(Item);
end;
function TXMLClassList.AddList(List:TXMLClassList):longint;
var Counter,Index:longint;
begin
result:=-1;
for Counter:=0 to List.Count-1 do begin
Index:=Add(List[Counter]);
if Counter=0 then begin
result:=Index;
end;
end;
end;
function TXMLClassList.AppendList(List:TXMLClassList):longint;
begin
result:=AddList(List);
end;
function TXMLClassList.NewClass:TXMLClass;
var Item:TXMLClass;
begin
Item:=TXMLClass.Create;
Add(Item);
result:=Item;
end;
procedure TXMLClassList.Insert(Index:longint;Item:TXMLClass);
var Counter:longint;
begin
if (Index>=0) and (Index<InternalCount) then begin
SetCount(InternalCount+1);
for Counter:=InternalCount-1 downto Index do begin
InternalList^[Counter+1]:=InternalList^[Counter];
end;
InternalList^[Index]:=Item;
end else if Index=InternalCount then begin
Add(Item);
end else if Index>InternalCount then begin
SetCount(Index);
Add(Item);
end;
end;
procedure TXMLClassList.Delete(Index:longint);
var i,j:longint;
begin
if (Index>=0) and (Index<InternalCount) then begin
j:=InternalCount-1;
i:=Index;
Move(InternalList^[i+1],InternalList^[i],(j-i)*SizeOf(TXMLClass));
SetCount(j);
end;
end;
procedure TXMLClassList.DeleteClass(Index:longint);
var i,j:longint;
begin
if (Index>=0) and (Index<InternalCount) then begin
j:=InternalCount-1;
i:=Index;
if assigned(InternalList^[i]) then begin
InternalList^[i].Free;
InternalList^[i]:=nil;
end;
Move(InternalList^[i+1],InternalList^[i],(j-i)*SizeOf(TXMLClass));
SetCount(j);
end;
end;
function TXMLClassList.Remove(Item:TXMLClass):longint;
var i,j,k:longint;
begin
result:=-1;
k:=InternalCount;
j:=-1;
for i:=0 to k-1 do begin
if InternalList^[i]=Item then begin
j:=i;
break;
end;
end;
if j>=0 then begin
dec(k);
Move(InternalList^[j+1],InternalList^[j],(k-j)*SizeOf(TXMLClass));
SetCount(k);
result:=j;
end;
end;
function TXMLClassList.RemoveClass(Item:TXMLClass):longint;
var i,j,k:longint;
begin
result:=-1;
k:=InternalCount;
j:=-1;
for i:=0 to k-1 do begin
if InternalList^[i]=Item then begin
j:=i;
break;
end;
end;
if j>=0 then begin
dec(k);
Move(InternalList^[j+1],InternalList^[j],(k-j)*SizeOf(TXMLClass));
SetCount(k);
Item.Free;
result:=j;
end;
end;
function TXMLClassList.Find(Item:TXMLClass):longint;
var i:longint;
begin
result:=-1;
for i:=0 to InternalCount-1 do begin
if InternalList^[i]=Item then begin
result:=i;
exit;
end;
end;
end;
function TXMLClassList.IndexOf(Item:TXMLClass):longint;
var i:longint;
begin
result:=-1;
for i:=0 to InternalCount-1 do begin
if InternalList^[i]=Item then begin
result:=i;
exit;
end;
end;
end;
procedure TXMLClassList.Exchange(Index1,Index2:longint);
var TempPointer:TXMLClass;
begin
if (Index1>=0) and (Index1<InternalCount) and (Index2>=0) and (Index2<InternalCount) then begin
TempPointer:=InternalList^[Index1];
InternalList^[Index1]:=InternalList^[Index2];
InternalList^[Index2]:=TempPointer;
end;
end;
function TXMLClassList.Push(Item:TXMLClass):longint;
begin
result:=Add(Item);
end;
function TXMLClassList.Pop(var Item:TXMLClass):boolean;
begin
result:=InternalCount>0;
if result then begin
Item:=InternalList^[InternalCount-1];
Delete(InternalCount-1);
end;
end;
function TXMLClassList.Pop:TXMLClass;
begin
if InternalCount>0 then begin
result:=InternalList^[InternalCount-1];
Delete(InternalCount-1);
end else begin
result:=nil;
end;
end;
function TXMLClassList.Last:TXMLClass;
begin
if InternalCount>0 then begin
result:=InternalList^[InternalCount-1];
end else begin
result:=nil;
end;
end;
function TXMLClassList.GetItem(Index:longint):TXMLClass;
begin
if (Index>=0) and (Index<InternalCount) then begin
result:=InternalList^[Index];
end else begin
result:=nil;
end;
end;
procedure TXMLClassList.SetItem(Index:longint;Value:TXMLClass);
begin
if (Index>=0) and (Index<InternalCount) then begin
InternalList^[Index]:=Value;
end;
end;
function TXMLClassList.GetItemPointer(Index:longint):TXMLClass;
begin
if (Index>=0) and (Index<InternalCount) then begin
result:=@InternalList^[Index];
end else begin
result:=nil;
end;
end;
constructor TXMLClassLinkedList.Create;
begin
inherited Create;
ClearWithContentDestroying:=false;
ClearNoFree;
end;
destructor TXMLClassLinkedList.Destroy;
begin
Clear;
inherited Destroy;
end;
procedure TXMLClassLinkedList.Clear;
begin
if ClearWithContentDestroying then begin
ClearWithFree;
end else begin
ClearNoFree;
end;
end;
procedure TXMLClassLinkedList.ClearNoFree;
var Current,Next:TXMLClass;
begin
Current:=First;
while assigned(Current) do begin
Next:=Current.Next;
Remove(Current);
Current:=Next;
end;
First:=nil;
Last:=nil;
end;
procedure TXMLClassLinkedList.ClearWithFree;
var Current,Next:TXMLClass;
begin
Current:=First;
while assigned(Current) do begin
Next:=Current.Next;
RemoveClass(Current);
Current:=Next;
end;
First:=nil;
Last:=nil;
end;
procedure TXMLClassLinkedList.Add(Item:TXMLClass);
begin
Item.Next:=nil;