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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
|
/**
****************************************************************************
* <P> XML.c - implementation file for basic XML parser written in ANSI C++
* for portability. It works by using recursion and a node tree for breaking
* down the elements of an XML document. </P>
*
* @version V2.43
* @author Frank Vanden Berghen
*
* NOTE:
*
* If you add "#define STRICT_PARSING", on the first line of this file
* the parser will see the following XML-stream:
* <a><b>some text</b><b>other text </a>
* as an error. Otherwise, this tring will be equivalent to:
* <a><b>some text</b><b>other text</b></a>
*
* NOTE:
*
* If you add "#define APPROXIMATE_PARSING" on the first line of this file
* the parser will see the following XML-stream:
* <data name = "n1">
* <data name = "n2">
* <data name = "n3" />
* as equivalent to the following XML-stream:
* <data name = "n1" />
* <data name = "n2" />
* <data name = "n3" />
* This can be useful for badly-formed XML-streams but prevent the use
* of the following XML-stream (problem is: tags at contiguous levels
* have the same names):
* <data name = "n1">
* <data name = "n2">
* <data name = "n3" />
* </data>
* </data>
*
* NOTE:
*
* If you add "#define _XMLPARSER_NO_MESSAGEBOX_" on the first line of this file
* the "openFileHelper" function will always display error messages inside the
* console instead of inside a message-box-window. Message-box-windows are
* available on windows 9x/NT/2000/XP/Vista only.
*
* Copyright (c) 2002, Business-Insight
* <a href = "http://www.Business-Insight.com">Business-Insight</a>
* All rights reserved.
* See the file "AFPL-license.txt" about the licensing terms
*
****************************************************************************
*/
#include "..\..\core\commonheaders.h"
#include "xmlParser.h"
#include <memory.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
XMLCSTR XMLNode::getVersion() { return _CXML("v2.43"); }
void freeXMLString(XMLSTR t) {if(t)free(t);}
static XMLNode::XMLCharEncoding characterEncoding = XMLNode::char_encoding_UTF8;
static char guessWideCharChars = 1, dropWhiteSpace = 0, removeCommentsInMiddleOfText = 1;
inline int mmin(const int t1, const int t2) { return t1 < t2 ? t1 : t2; }
// You can modify the initialization of the variable "XMLClearTags" below
// to change the clearTags that are currently recognized by the library.
// The number on the second columns is the length of the string inside the
// first column.
// The "<!DOCTYPE" declaration must be the second in the list.
// The "<!--" declaration must be the third in the list.
// All ClearTag Strings must start with the '<' character.
typedef struct { XMLCSTR lpszOpen; int openTagLen; XMLCSTR lpszClose;} ALLXMLClearTag;
static ALLXMLClearTag XMLClearTags[] =
{
{ _CXML("<![CDATA["), 9, _CXML("]]>") },
{ _CXML("<!DOCTYPE"), 9, _CXML(">") },
{ _CXML("<!--") , 4, _CXML("-->") },
{ _CXML("<PRE>") , 5, _CXML("</PRE>") },
// { _CXML("<Script>") , 8, _CXML("</Script>")},
{ NULL , 0, NULL }
};
// You can modify the initialization of the variable "XMLEntities" below
// to change the character entities that are currently recognized by the library.
// The number on the second columns is the length of the string inside the
// first column. Additionally, the syntaxes " " and " " are recognized.
typedef struct { XMLCSTR s; int l; XMLCHAR c;} XMLCharacterEntity;
static XMLCharacterEntity XMLEntities[] =
{
{ _CXML("&"), 5, _CXML('&')},
{ _CXML("<"), 4, _CXML('<')},
{ _CXML(">"), 4, _CXML('>')},
{ _CXML("""), 6, _CXML('\"')},
{ _CXML("'"), 6, _CXML('\'')},
{ NULL , 0, '\0' }
};
// When rendering the XMLNode to a string (using the "createXMLString" function),
// you can ask for a beautiful formatting. This formatting is using the
// following indentation character:
#define INDENTCHAR _CXML('\t')
// The following function parses the XML errors into a user friendly string.
// You can edit this to change the output language of the library to something else.
XMLCSTR XMLNode::getError(XMLError xerror)
{
switch (xerror)
{
case eXMLErrorNone: return _CXML("No error");
case eXMLErrorMissingEndTag: return _CXML("Warning: Unmatched end tag");
case eXMLErrorNoXMLTagFound: return _CXML("Warning: No XML tag found");
case eXMLErrorEmpty: return _CXML("Error: No XML data");
case eXMLErrorMissingTagName: return _CXML("Error: Missing start tag name");
case eXMLErrorMissingEndTagName: return _CXML("Error: Missing end tag name");
case eXMLErrorUnmatchedEndTag: return _CXML("Error: Unmatched end tag");
case eXMLErrorUnmatchedEndClearTag: return _CXML("Error: Unmatched clear tag end");
case eXMLErrorUnexpectedToken: return _CXML("Error: Unexpected token found");
case eXMLErrorNoElements: return _CXML("Error: No elements found");
case eXMLErrorFileNotFound: return _CXML("Error: File not found");
case eXMLErrorFirstTagNotFound: return _CXML("Error: First Tag not found");
case eXMLErrorUnknownCharacterEntity:return _CXML("Error: Unknown character entity");
case eXMLErrorCharacterCodeAbove255: return _CXML("Error: Character code above 255 is forbidden in MultiByte char mode.");
case eXMLErrorCharConversionError: return _CXML("Error: unable to convert between WideChar and MultiByte chars");
case eXMLErrorCannotOpenWriteFile: return _CXML("Error: unable to open file for writing");
case eXMLErrorCannotWriteFile: return _CXML("Error: cannot write into file");
case eXMLErrorBase64DataSizeIsNotMultipleOf4: return _CXML("Warning: Base64-string length is not a multiple of 4");
case eXMLErrorBase64DecodeTruncatedData: return _CXML("Warning: Base64-string is truncated");
case eXMLErrorBase64DecodeIllegalCharacter: return _CXML("Error: Base64-string contains an illegal character");
case eXMLErrorBase64DecodeBufferTooSmall: return _CXML("Error: Base64 decode output buffer is too small");
};
return _CXML("Unknown");
}
/////////////////////////////////////////////////////////////////////////
// Here start the abstraction layer to be OS-independent //
/////////////////////////////////////////////////////////////////////////
// Here is an abstraction layer to access some common string manipulation functions.
// The abstraction layer is currently working for gcc, Microsoft Visual Studio 6.0,
// Microsoft Visual Studio .NET, CC (sun compiler) and Borland C++.
// If you plan to "port" the library to a new system/compiler, all you have to do is
// to edit the following lines.
#ifdef XML_NO_WIDE_CHAR
char myIsTextWideChar(const void *b, int len) { return FALSE; }
#else
#if defined (UNDER_CE) || !defined(_XMLWINDOWS)
char myIsTextWideChar(const void *b, int len) // inspired by the Wine API: RtlIsTextUnicode
{
#ifdef sun
// for SPARC processors: wchar_t* buffers must always be alligned, otherwise it's a char* buffer.
if ((((unsigned long)b)%sizeof(wchar_t)) != 0) return FALSE;
#endif
const wchar_t *s = (const wchar_t*)b;
// buffer too small:
if (len<sizeof(wchar_t)) return FALSE;
// odd length test
if (len&1) return FALSE;
/* only checks the first 256 characters */
len = mmin(256, len/sizeof(wchar_t));
// Check for the special byte order:
if (*((unsigned short*)s) == 0xFFFE) return TRUE; // IS_TEXT_UNICODE_REVERSE_SIGNATURE;
if (*((unsigned short*)s) == 0xFEFF) return TRUE; // IS_TEXT_UNICODE_SIGNATURE
// checks for ASCII characters in the UNICODE stream
int i, stats = 0;
for (i=0; i<len; i++) if (s[i] <= (unsigned short)255) stats++;
if (stats>len/2) return TRUE;
// Check for UNICODE NULL chars
for (i=0; i<len; i++) if (!s[i]) return TRUE;
return FALSE;
}
#else
char myIsTextWideChar(const void *b, int l) { return (char)IsTextUnicode((CONST LPVOID)b, l, NULL); }
#endif
#endif
#ifdef _XMLWINDOWS
// for Microsoft Visual Studio 6.0 and Microsoft Visual Studio .NET and Borland C++ Builder 6.0
#ifdef _XMLWIDECHAR
wchar_t *myMultiByteToWideChar(const char *s, XMLNode::XMLCharEncoding ce)
{
int i;
if (ce == XMLNode::char_encoding_UTF8) i = (int)MultiByteToWideChar(CP_UTF8, 0 , s, -1, NULL, 0);
else i = (int)MultiByteToWideChar(CP_ACP , MB_PRECOMPOSED, s, -1, NULL, 0);
if (i<0) return NULL;
wchar_t *d = (wchar_t *)malloc((i+1)*sizeof(XMLCHAR));
if (ce == XMLNode::char_encoding_UTF8) i = (int)MultiByteToWideChar(CP_UTF8, 0 , s, -1, d, i);
else i = (int)MultiByteToWideChar(CP_ACP , MB_PRECOMPOSED, s, -1, d, i);
d[i] = 0;
return d;
}
static inline FILE *xfopen(XMLCSTR filename, XMLCSTR mode) { return _wfopen(filename, mode); }
static inline size_t xstrlen(XMLCSTR c) { return wcslen(c); }
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return _wcsnicmp(c1, c2, l);}
static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncmp(c1, c2, l);}
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return _wcsicmp(c1, c2); }
static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)wcsstr(c1, c2); }
static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)wcscpy(c1, c2); }
#else
char *myWideCharToMultiByte(const wchar_t *s)
{
UINT codePage = CP_ACP; if (characterEncoding == XMLNode::char_encoding_UTF8) codePage = CP_UTF8;
int i = (int)WideCharToMultiByte(codePage, // code page
0, // performance and mapping flags
s, // wide-character string
-1, // number of chars in string
NULL, // buffer for new string
0, // size of buffer
NULL, // default for unmappable chars
NULL // set when default char used
);
if (i<0) return NULL;
char *d = (char*)malloc(i+1);
WideCharToMultiByte(codePage, // code page
0, // performance and mapping flags
s, // wide-character string
-1, // number of chars in string
d, // buffer for new string
i, // size of buffer
NULL, // default for unmappable chars
NULL // set when default char used
);
d[i] = 0;
return d;
}
static inline FILE *xfopen(XMLCSTR filename, XMLCSTR mode) { return fopen(filename, mode); }
static inline size_t xstrlen(XMLCSTR c) { return strlen(c); }
#ifdef __BORLANDC__
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return strnicmp(c1, c2, l);}
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return stricmp(c1, c2); }
#else
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return _strnicmp(c1, c2, l);}
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return _stricmp(c1, c2); }
#endif
static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncmp(c1, c2, l);}
static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)strstr(c1, c2); }
static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)strcpy(c1, c2); }
#endif
#else
// for gcc and CC
#ifdef XML_NO_WIDE_CHAR
char *myWideCharToMultiByte(const wchar_t *s) { return NULL; }
#else
char *myWideCharToMultiByte(const wchar_t *s)
{
const wchar_t *ss = s;
int i = (int)wcsrtombs(NULL, &ss, 0, NULL);
if (i<0) return NULL;
char *d = (char *)malloc(i+1);
wcsrtombs(d, &s, i, NULL);
d[i] = 0;
return d;
}
#endif
#ifdef _XMLWIDECHAR
wchar_t *myMultiByteToWideChar(const char *s, XMLNode::XMLCharEncoding ce)
{
const char *ss = s;
int i = (int)mbsrtowcs(NULL, &ss, 0, NULL);
if (i<0) return NULL;
wchar_t *d = (wchar_t *)malloc((i+1)*sizeof(wchar_t));
mbsrtowcs(d, &s, i, NULL);
d[i] = 0;
return d;
}
int xstrlen(XMLCSTR c) { return wcslen(c); }
#ifdef sun
// for CC
#include <widec.h>
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncasecmp(c1, c2, l);}
static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncmp(c1, c2, l);}
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return wscasecmp(c1, c2); }
#else
static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncmp(c1, c2, l);}
#ifdef __linux__
// for gcc/linux
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncasecmp(c1, c2, l);}
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return wcscasecmp(c1, c2); }
#else
#include <wctype.h>
// for gcc/non-linux (MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, AIX 4.3.2, HP-UX 11, IRIX 6.5, OSF/1 5.1, Cygwin, mingw)
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2)
{
wchar_t left, right;
do
{
left = towlower(*c1++); right = towlower(*c2++);
} while (left&&(left == right));
return (int)left-(int)right;
}
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l)
{
wchar_t left, right;
while (l--)
{
left = towlower(*c1++); right = towlower(*c2++);
if ((!left) || (left != right)) return (int)left-(int)right;
}
return 0;
}
#endif
#endif
static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)wcsstr(c1, c2); }
static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)wcscpy(c1, c2); }
static inline FILE *xfopen(XMLCSTR filename, XMLCSTR mode)
{
char *filenameAscii = myWideCharToMultiByte(filename);
FILE *f;
if (mode[0] == _CXML('r')) f = fopen(filenameAscii, "rb");
else f = fopen(filenameAscii, "wb");
free(filenameAscii);
return f;
}
#else
static inline FILE *xfopen(XMLCSTR filename, XMLCSTR mode) { return fopen(filename, mode); }
static inline int xstrlen(XMLCSTR c) { return strlen(c); }
static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncasecmp(c1, c2, l);}
static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncmp(c1, c2, l);}
static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return strcasecmp(c1, c2); }
static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)strstr(c1, c2); }
static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)strcpy(c1, c2); }
#endif
static inline int _strnicmp(const char *c1, const char *c2, int l) { return strncasecmp(c1, c2, l);}
#endif
///////////////////////////////////////////////////////////////////////////////
// the "xmltoc, xmltob, xmltoi, xmltol, xmltof, xmltoa" functions //
///////////////////////////////////////////////////////////////////////////////
// These 6 functions are not used inside the XMLparser.
// There are only here as "convenience" functions for the user.
// If you don't need them, you can delete them without any trouble.
#ifdef _XMLWIDECHAR
#ifdef _XMLWINDOWS
// for Microsoft Visual Studio 6.0 and Microsoft Visual Studio .NET and Borland C++ Builder 6.0
char xmltob(XMLCSTR t, char v) { if (t&&(*t)) return (char)_wtoi(t); return v; }
int xmltoi(XMLCSTR t, int v) { if (t&&(*t)) return _wtoi(t); return v; }
long xmltol(XMLCSTR t, long v) { if (t&&(*t)) return _wtol(t); return v; }
double xmltof(XMLCSTR t, double v) { if (t&&(*t)) swscanf(t, L"%lf", &v); /*v = _wtof(t);*/ return v; }
#else
#ifdef sun
// for CC
#include <widec.h>
char xmltob(XMLCSTR t, char v) { if (t) return (char)wstol(t, NULL, 10); return v; }
int xmltoi(XMLCSTR t, int v) { if (t) return (int)wstol(t, NULL, 10); return v; }
long xmltol(XMLCSTR t, long v) { if (t) return wstol(t, NULL, 10); return v; }
#else
// for gcc
char xmltob(XMLCSTR t, char v) { if (t) return (char)wcstol(t, NULL, 10); return v; }
int xmltoi(XMLCSTR t, int v) { if (t) return (int)wcstol(t, NULL, 10); return v; }
long xmltol(XMLCSTR t, long v) { if (t) return wcstol(t, NULL, 10); return v; }
#endif
double xmltof(XMLCSTR t, double v) { if (t&&(*t)) swscanf(t, L"%lf", &v); /*v = _wtof(t);*/ return v; }
#endif
#else
char xmltob(XMLCSTR t, char v) { if (t&&(*t)) return (char)atoi(t); return v; }
int xmltoi(XMLCSTR t, int v) { if (t&&(*t)) return atoi(t); return v; }
long xmltol(XMLCSTR t, long v) { if (t&&(*t)) return atol(t); return v; }
double xmltof(XMLCSTR t, double v) { if (t&&(*t)) return atof(t); return v; }
#endif
XMLCSTR xmltoa(XMLCSTR t, XMLCSTR v) { if (t) return t; return v; }
XMLCHAR xmltoc(XMLCSTR t, const XMLCHAR v) { if (t&&(*t)) return *t; return v; }
/////////////////////////////////////////////////////////////////////////
// the "openFileHelper" function //
/////////////////////////////////////////////////////////////////////////
// Since each application has its own way to report and deal with errors, you should modify & rewrite
// the following "openFileHelper" function to get an "error reporting mechanism" tailored to your needs.
XMLNode XMLNode::openFileHelper(XMLCSTR filename, XMLCSTR tag)
{
// guess the value of the global parameter "characterEncoding"
// (the guess is based on the first 200 bytes of the file).
FILE *f = xfopen(filename, _CXML("rb"));
if (f)
{
char bb[205];
int l = (int)fread(bb, 1, 200, f);
setGlobalOptions(guessCharEncoding(bb, l), guessWideCharChars, dropWhiteSpace, removeCommentsInMiddleOfText);
fclose(f);
}
// parse the file
XMLResults pResults;
XMLNode xnode = XMLNode::parseFile(filename, tag, &pResults);
// display error message (if any)
if (pResults.error != eXMLErrorNone)
{
// create message
char message[2000], *s1 = (char*)"", *s3 = (char*)""; XMLCSTR s2 = _CXML("");
if (pResults.error == eXMLErrorFirstTagNotFound) { s1 = (char*)"First Tag should be '"; s2 = tag; s3 = (char*)"'.\n"; }
mir_snprintf(message, SIZEOF(message),
#ifdef _XMLWIDECHAR
"XML Parsing error inside file '%S'.\n%S\nAt line %i, column %i.\n%s%S%s"
#else
"XML Parsing error inside file '%s'.\n%s\nAt line %i, column %i.\n%s%s%s"
#endif
, filename, XMLNode::getError(pResults.error), pResults.nLine, pResults.nColumn, s1, s2, s3);
// display message
#if defined(_XMLWINDOWS) && !defined(UNDER_CE) && !defined(_XMLPARSER_NO_MESSAGEBOX_)
MessageBoxA(NULL, message, "XML Parsing error", MB_OK|MB_ICONERROR|MB_TOPMOST);
#else
printf("%s", message);
#endif
exit(255);
}
return xnode;
}
/////////////////////////////////////////////////////////////////////////
// Here start the core implementation of the XMLParser library //
/////////////////////////////////////////////////////////////////////////
// You should normally not change anything below this point.
#ifndef _XMLWIDECHAR
// If "characterEncoding = ascii" then we assume that all characters have the same length of 1 byte.
// If "characterEncoding = UTF8" then the characters have different lengths (from 1 byte to 4 bytes).
// If "characterEncoding = ShiftJIS" then the characters have different lengths (from 1 byte to 2 bytes).
// This table is used as lookup-table to know the length of a character (in byte) based on the
// content of the first byte of the character.
// (note: if you modify this, you must always have XML_utf8ByteTable[0] = 0).
static const char XML_utf8ByteTable[256] =
{
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x00
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x10
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x30
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x50
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x70 End of ASCII range
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x80 0x80 to 0xc1 invalid
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x90
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xa0
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xb0
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xc0 0xc2 to 0xdf 2 byte
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xd0
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xe0 0xe0 to 0xef 3 byte
4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // 0xf0 0xf0 to 0xf4 4 byte, 0xf5 and higher invalid
};
static const char XML_legacyByteTable[256] =
{
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
static const char XML_sjisByteTable[256] =
{
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x00
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x10
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x30
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x50
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x70
1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0x80 0x81 to 0x9F 2 bytes
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0x90
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xa0
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xb0
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xc0
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0xd0
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xe0 0xe0 to 0xef 2 bytes
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // 0xf0
};
static const char XML_gb2312ByteTable[256] =
{
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x00
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x10
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x30
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x50
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x70
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x80
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x90
1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xa0 0xa1 to 0xf7 2 bytes
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xb0
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xc0
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xd0
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xe0
2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1 // 0xf0
};
static const char XML_gbk_big5_ByteTable[256] =
{
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x00
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x10
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x30
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x50
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x70
1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0x80 0x81 to 0xfe 2 bytes
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0x90
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xa0
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xb0
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xc0
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xd0
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xe0
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 // 0xf0
};
static const char *XML_ByteTable = (const char *)XML_utf8ByteTable; // the default is "characterEncoding = XMLNode::encoding_UTF8"
#endif
XMLNode XMLNode::emptyXMLNode;
XMLClear XMLNode::emptyXMLClear = { NULL, NULL, NULL};
XMLAttribute XMLNode::emptyXMLAttribute = { NULL, NULL};
// Enumeration used to decipher what type a token is
typedef enum XMLTokenTypeTag
{
eTokenText = 0,
eTokenQuotedText,
eTokenTagStart, /* "<" */
eTokenTagEnd, /* "</" */
eTokenCloseTag, /* ">" */
eTokenEquals, /* " = " */
eTokenDeclaration, /* "<?" */
eTokenShortHandClose, /* "/>" */
eTokenClear,
eTokenError
} XMLTokenType;
// Main structure used for parsing XML
typedef struct XML
{
XMLCSTR lpXML;
XMLCSTR lpszText;
int nIndex, nIndexMissigEndTag;
enum XMLError error;
XMLCSTR lpEndTag;
int cbEndTag;
XMLCSTR lpNewElement;
int cbNewElement;
int nFirst;
} XML;
typedef struct
{
ALLXMLClearTag *pClr;
XMLCSTR pStr;
} NextToken;
// Enumeration used when parsing attributes
typedef enum Attrib
{
eAttribName = 0,
eAttribEquals,
eAttribValue
} Attrib;
// Enumeration used when parsing elements to dictate whether we are currently
// inside a tag
typedef enum XMLStatus
{
eInsideTag = 0,
eOutsideTag
} XMLStatus;
XMLError XMLNode::writeToFile(XMLCSTR filename, const char *encoding, char nFormat) const
{
if (!d) return eXMLErrorNone;
FILE *f = xfopen(filename, _CXML("wb"));
if (!f) return eXMLErrorCannotOpenWriteFile;
#ifdef _XMLWIDECHAR
unsigned char h[2] = { 0xFF, 0xFE };
if (!fwrite(h, 2, 1, f))
{
fclose(f);
return eXMLErrorCannotWriteFile;
}
if ((!isDeclaration())&&((d->lpszName) || (!getChildNode().isDeclaration())))
{
if ( fputws(L"<?xml version = \"1.0\" encoding = \"utf-16\"?>\n", f) == EOF)
{
fclose(f);
return eXMLErrorCannotWriteFile;
}
}
#else
if ((!isDeclaration())&&((d->lpszName) || (!getChildNode().isDeclaration())))
{
if (characterEncoding == char_encoding_UTF8)
{
// header so that windows recognize the file as UTF-8:
unsigned char h[3] = {0xEF, 0xBB, 0xBF};
if (!fwrite(h, 3, 1, f))
{
fclose(f);
return eXMLErrorCannotWriteFile;
}
encoding = "utf-8";
} else if (characterEncoding == char_encoding_ShiftJIS) encoding = "SHIFT-JIS";
if (!encoding) encoding = "ISO-8859-1";
if (fprintf(f, "<?xml version = \"1.0\" encoding = \"%s\"?>\n", encoding)<0)
{
fclose(f);
return eXMLErrorCannotWriteFile;
}
} else
{
if (characterEncoding == char_encoding_UTF8)
{
unsigned char h[3] = {0xEF, 0xBB, 0xBF};
if (!fwrite(h, 3, 1, f))
{
fclose(f);
return eXMLErrorCannotWriteFile;
}
}
}
#endif
int i;
XMLSTR t = createXMLString(nFormat, &i);
if (!fwrite(t, sizeof(XMLCHAR)*i, 1, f))
{
free(t);
fclose(f);
return eXMLErrorCannotWriteFile;
}
if (fclose(f) != 0)
{
free(t);
return eXMLErrorCannotWriteFile;
}
free(t);
return eXMLErrorNone;
}
// Duplicate a given string.
XMLSTR stringDup(XMLCSTR lpszData, int cbData)
{
if (lpszData == NULL) return NULL;
XMLSTR lpszNew;
if (cbData == -1) cbData = (int)xstrlen(lpszData);
lpszNew = (XMLSTR)malloc((cbData+1) * sizeof(XMLCHAR));
if (lpszNew)
{
memcpy(lpszNew, lpszData, (cbData) * sizeof(XMLCHAR));
lpszNew[cbData] = (XMLCHAR)NULL;
}
return lpszNew;
}
XMLSTR ToXMLStringTool::toXMLUnSafe(XMLSTR dest, XMLCSTR source)
{
XMLSTR dd = dest;
XMLCHAR ch;
XMLCharacterEntity *entity;
while ((ch = *source))
{
entity = XMLEntities;
do
{
if (ch == entity->c) {xstrcpy(dest, entity->s); dest+=entity->l; source++; goto out_of_loop1; }
entity++;
} while (entity->s);
#ifdef _XMLWIDECHAR
*(dest++) = *(source++);
#else
switch(XML_ByteTable[(unsigned char)ch])
{
case 4: *(dest++) = *(source++);
case 3: *(dest++) = *(source++);
case 2: *(dest++) = *(source++);
case 1: *(dest++) = *(source++);
}
#endif
out_of_loop1:
;
}
*dest = 0;
return dd;
}
// private (used while rendering):
int ToXMLStringTool::lengthXMLString(XMLCSTR source)
{
int r = 0;
XMLCharacterEntity *entity;
XMLCHAR ch;
while ((ch = *source))
{
entity = XMLEntities;
do
{
if (ch == entity->c) { r+=entity->l; source++; goto out_of_loop1; }
entity++;
} while (entity->s);
#ifdef _XMLWIDECHAR
r++; source++;
#else
ch = XML_ByteTable[(unsigned char)ch]; r+=ch; source+=ch;
#endif
out_of_loop1:
;
}
return r;
}
ToXMLStringTool::~ToXMLStringTool() { freeBuffer(); }
void ToXMLStringTool::freeBuffer() { if (buf) free(buf); buf = NULL; buflen = 0; }
XMLSTR ToXMLStringTool::toXML(XMLCSTR source)
{
if (!source)
{
if (buflen<1) { buflen = 1; buf = (XMLSTR)malloc(sizeof(XMLCHAR)); }
*buf = 0;
return buf;
}
int l = lengthXMLString(source)+1;
if (l>buflen) { freeBuffer(); buflen = l; buf = (XMLSTR)malloc(l*sizeof(XMLCHAR)); }
return toXMLUnSafe(buf, source);
}
// private:
XMLSTR fromXMLString(XMLCSTR s, int lo, XML *pXML)
{
// This function is the opposite of the function "toXMLString". It decodes the escape
// sequences &, ", ', <, > and replace them by the characters
// &, ", ', <, >. This function is used internally by the XML Parser. All the calls to
// the XML library will always gives you back "decoded" strings.
//
// in: string (s) and length (lo) of string
// out: new allocated string converted from xml
if (!s) return NULL;
int ll = 0, j;
XMLSTR d;
XMLCSTR ss = s;
XMLCharacterEntity *entity;
while ((lo>0)&&(*s))
{
if (*s == _CXML('&'))
{
if ((lo>2)&&(s[1] == _CXML('#')))
{
s+=2; lo-=2;
if ((*s == _CXML('X')) || (*s == _CXML('x'))) { s++; lo--; }
while ((*s)&&(*s != _CXML(';'))&&((lo--)>0)) s++;
if (*s != _CXML(';'))
{
pXML->error = eXMLErrorUnknownCharacterEntity;
return NULL;
}
s++; lo--;
} else
{
entity = XMLEntities;
do
{
if ((lo>=entity->l)&&(xstrnicmp(s, entity->s, entity->l) == 0)) { s+=entity->l; lo-=entity->l; break; }
entity++;
} while (entity->s);
if (!entity->s)
{
pXML->error = eXMLErrorUnknownCharacterEntity;
return NULL;
}
}
} else
{
#ifdef _XMLWIDECHAR
s++; lo--;
#else
j = XML_ByteTable[(unsigned char)*s]; s+=j; lo-=j; ll+=j-1;
#endif
}
ll++;
}
d = (XMLSTR)malloc((ll+1)*sizeof(XMLCHAR));
s = d;
while (ll-->0)
{
if (*ss == _CXML('&'))
{
if (ss[1] == _CXML('#'))
{
ss+=2; j = 0;
if ((*ss == _CXML('X')) || (*ss == _CXML('x')))
{
ss++;
while (*ss != _CXML(';'))
{
if ((*ss>=_CXML('0'))&&(*ss <= _CXML('9'))) j = (j<<4)+*ss-_CXML('0');
else if ((*ss>=_CXML('A'))&&(*ss <= _CXML('F'))) j = (j<<4)+*ss-_CXML('A')+10;
else if ((*ss>=_CXML('a'))&&(*ss <= _CXML('f'))) j = (j<<4)+*ss-_CXML('a')+10;
else { free((void*)s); pXML->error = eXMLErrorUnknownCharacterEntity;return NULL;}
ss++;
}
} else
{
while (*ss != _CXML(';'))
{
if ((*ss>=_CXML('0'))&&(*ss <= _CXML('9'))) j = (j*10)+*ss-_CXML('0');
else { free((void*)s); pXML->error = eXMLErrorUnknownCharacterEntity;return NULL;}
ss++;
}
}
#ifndef _XMLWIDECHAR
if (j>255) { free((void*)s); pXML->error = eXMLErrorCharacterCodeAbove255;return NULL;}
#endif
(*d++) = (XMLCHAR)j; ss++;
} else
{
entity = XMLEntities;
do
{
if (xstrnicmp(ss, entity->s, entity->l) == 0) { *(d++) = entity->c; ss+=entity->l; break; }
entity++;
} while (entity->s);
}
} else
{
#ifdef _XMLWIDECHAR
*(d++) = *(ss++);
#else
switch(XML_ByteTable[(unsigned char)*ss])
{
case 4: *(d++) = *(ss++); ll--;
case 3: *(d++) = *(ss++); ll--;
case 2: *(d++) = *(ss++); ll--;
case 1: *(d++) = *(ss++);
}
#endif
}
}
*d = 0;
#ifndef _XMLWIDECHAR
if (characterEncoding != XMLNode::char_encoding_legacy)
Utf8Decode((XMLSTR)s, NULL);
#endif
return (XMLSTR)s;
}
#define XML_isSPACECHAR(ch) ((ch == _CXML('\n')) || (ch == _CXML(' ')) || (ch == _CXML('\t')) || (ch == _CXML('\r')))
// private:
char myTagCompare(XMLCSTR cclose, XMLCSTR copen)
// !!!! WARNING strange convention&:
// return 0 if equals
// return 1 if different
{
if (!cclose) return 1;
int l = (int)xstrlen(cclose);
if (xstrnicmp(cclose, copen, l) != 0) return 1;
const XMLCHAR c = copen[l];
if (XML_isSPACECHAR(c) ||
(c == _CXML('/')) ||
(c == _CXML('<')) ||
(c == _CXML('>')) ||
(c == _CXML('='))) return 0;
return 1;
}
// Obtain the next character from the string.
static inline XMLCHAR getNextChar(XML *pXML)
{
XMLCHAR ch = pXML->lpXML[pXML->nIndex];
#ifdef _XMLWIDECHAR
if (ch != 0) pXML->nIndex++;
#else
pXML->nIndex+=XML_ByteTable[(unsigned char)ch];
#endif
return ch;
}
// Find the next token in a string.
// pcbToken contains the number of characters that have been read.
static NextToken GetNextToken(XML *pXML, int *pcbToken, enum XMLTokenTypeTag *pType)
{
NextToken result;
XMLCHAR ch;
XMLCHAR chTemp;
int indexStart, nFoundMatch, nIsText = FALSE;
result.pClr = NULL; // prevent warning
// Find next non-white space character
do { indexStart = pXML->nIndex; ch = getNextChar(pXML); } while XML_isSPACECHAR(ch);
if (ch)
{
// Cache the current string pointer
result.pStr = &pXML->lpXML[indexStart];
// check for standard tokens
switch(ch)
{
// Check for quotes
case _CXML('\''):
case _CXML('\"'):
// Type of token
*pType = eTokenQuotedText;
chTemp = ch;
// Set the size
nFoundMatch = FALSE;
// Search through the string to find a matching quote
while ((ch = getNextChar(pXML)))
{
if (ch == chTemp) { nFoundMatch = TRUE; break; }
if (ch == _CXML('<')) break;
}
// If we failed to find a matching quote
if (nFoundMatch == FALSE)
{
pXML->nIndex = indexStart+1;
nIsText = TRUE;
break;
}
// 4.02.2002
// if (FindNonWhiteSpace(pXML)) pXML->nIndex--;
break;
// Equals (used with attribute values)
case _CXML('='):
*pType = eTokenEquals;
break;
// Close tag
case _CXML('>'):
*pType = eTokenCloseTag;
break;
// Check for tag start and tag end
case _CXML('<'):
{
// First check whether the token is in the clear tag list (meaning it
// does not need formatting).
ALLXMLClearTag *ctag = XMLClearTags;
do
{
if (!xstrncmp(ctag->lpszOpen, result.pStr, ctag->openTagLen))
{
result.pClr = ctag;
pXML->nIndex+=ctag->openTagLen-1;
*pType = eTokenClear;
return result;
}
ctag++;
} while (ctag->lpszOpen);
// Peek at the next character to see if we have an end tag '</',
// or an xml declaration '<?'
chTemp = pXML->lpXML[pXML->nIndex];
// If we have a tag end...
if (chTemp == _CXML('/'))
{
// Set the type and ensure we point at the next character
getNextChar(pXML);
*pType = eTokenTagEnd;
}
// If we have an XML declaration tag
else if (chTemp == _CXML('?'))
{
// Set the type and ensure we point at the next character
getNextChar(pXML);
*pType = eTokenDeclaration;
}
// Otherwise we must have a start tag
else
{
*pType = eTokenTagStart;
}
break;
}
// Check to see if we have a short hand type end tag ('/>').
case _CXML('/'):
// Peek at the next character to see if we have a short end tag '/>'
chTemp = pXML->lpXML[pXML->nIndex];
// If we have a short hand end tag...
if (chTemp == _CXML('>'))
{
// Set the type and ensure we point at the next character
getNextChar(pXML);
*pType = eTokenShortHandClose;
break;
}
// If we haven't found a short hand closing tag then drop into the
// text process
// Other characters
default:
nIsText = TRUE;
}
// If this is a TEXT node
if (nIsText)
{
// Indicate we are dealing with text
*pType = eTokenText;
while ((ch = getNextChar(pXML)))
{
if XML_isSPACECHAR(ch)
{
indexStart++; break;
} else if (ch == _CXML('/'))
{
// If we find a slash then this maybe text or a short hand end tag
// Peek at the next character to see it we have short hand end tag
ch = pXML->lpXML[pXML->nIndex];
// If we found a short hand end tag then we need to exit the loop
if (ch == _CXML('>')) { pXML->nIndex--; break; }
} else if ((ch == _CXML('<')) || (ch == _CXML('>')) || (ch == _CXML('=')))
{
pXML->nIndex--; break;
}
}
}
*pcbToken = pXML->nIndex-indexStart;
} else
{
// If we failed to obtain a valid character
*pcbToken = 0;
*pType = eTokenError;
result.pStr = NULL;
}
return result;
}
XMLCSTR XMLNode::updateName_WOSD(XMLSTR lpszName)
{
if (!d) { free(lpszName); return NULL; }
if (d->lpszName&&(lpszName != d->lpszName)) free((void*)d->lpszName);
d->lpszName = lpszName;
return lpszName;
}
// private:
XMLNode::XMLNode(struct XMLNodeDataTag *p) { d = p; (p->ref_count)++; }
XMLNode::XMLNode(XMLNodeData *pParent, XMLSTR lpszName, char isDeclaration)
{
d = (XMLNodeData*)malloc(sizeof(XMLNodeData));
d->ref_count = 1;
d->lpszName = NULL;
d->nChild = 0;
d->nText = 0;
d->nClear = 0;
d->nAttribute = 0;
d->isDeclaration = isDeclaration;
d->pParent = pParent;
d->pChild = NULL;
d->pText = NULL;
d->pClear = NULL;
d->pAttribute = NULL;
d->pOrder = NULL;
d->pInnerText = NULL;
updateName_WOSD(lpszName);
d->lpszNS = NULL;
if (lpszName && pParent && pParent->lpszName && !pParent->isDeclaration) {
TCHAR* p = _tcschr(lpszName, ':');
if (p) {
*p = 0;
d->lpszNS = d->lpszName;
d->lpszName = p+1;
}
}
}
XMLNode XMLNode::createXMLTopNode_WOSD(XMLSTR lpszName, char isDeclaration) { return XMLNode(NULL, lpszName, isDeclaration); }
XMLNode XMLNode::createXMLTopNode(XMLCSTR lpszName, char isDeclaration) { return XMLNode(NULL, stringDup(lpszName), isDeclaration); }
#define MEMORYINCREASE 50
static inline void myFree(void *p) { if (p) free(p); }
static inline void *myRealloc(void *p, int newsize, int memInc, int sizeofElem)
{
if (p == NULL) { if (memInc) return malloc(memInc*sizeofElem); return malloc(sizeofElem); }
if ((memInc == 0) || ((newsize%memInc) == 0)) p = realloc(p, (newsize+memInc)*sizeofElem);
// if (!p)
// {
// printf("XMLParser Error: Not enough memory! Aborting...\n"); exit(220);
// }
return p;
}
// private:
XMLElementPosition XMLNode::findPosition(XMLNodeData *d, int index, XMLElementType xxtype)
{
if (index<0) return -1;
int i=0, j = (int)((index<<2)+xxtype), *o = d->pOrder; while (o[i] != j) i++; return i;
}
// private:
// update "order" information when deleting a content of a XMLNode
int XMLNode::removeOrderElement(XMLNodeData *d, XMLElementType t, int index)
{
int n = d->nChild+d->nText+d->nClear, *o = d->pOrder, i = findPosition(d, index, t);
memmove(o+i, o+i+1, (n-i)*sizeof(int));
for (;i<n;i++)
if ((o[i]&3) == (int)t) o[i]-=4;
// We should normally do:
// d->pOrder = (int)realloc(d->pOrder, n*sizeof(int));
// but we skip reallocation because it's too time consuming.
// Anyway, at the end, it will be free'd completely at once.
return i;
}
void *XMLNode::addToOrder(int memoryIncrease, int *_pos, int nc, void *p, int size, XMLElementType xtype)
{
// in: *_pos is the position inside d->pOrder ("-1" means "EndOf")
// out: *_pos is the index inside p
p = myRealloc(p, (nc+1), memoryIncrease, size);
int n = d->nChild+d->nText+d->nClear;
d->pOrder = (int*)myRealloc(d->pOrder, n+1, memoryIncrease*3, sizeof(int));
int pos = *_pos, *o = d->pOrder;
if ((pos<0) || (pos>=n)) { *_pos = nc; o[n] = (int)((nc<<2)+xtype); return p; }
int i = pos;
memmove(o+i+1, o+i, (n-i)*sizeof(int));
while ((pos<n)&&((o[pos]&3) != (int)xtype)) pos++;
if (pos == n) { *_pos = nc; o[n] = (int)((nc<<2)+xtype); return p; }
o[i] = o[pos];
for (i = pos+1;i <= n;i++) if ((o[i]&3) == (int)xtype) o[i]+=4;
*_pos = pos = o[pos]>>2;
memmove(((char*)p)+(pos+1)*size, ((char*)p)+pos*size, (nc-pos)*size);
return p;
}
// Add a child node to the given element.
XMLNode XMLNode::addChild_priv(int memoryIncrease, XMLSTR lpszName, char isDeclaration, int pos)
{
if (!lpszName) return emptyXMLNode;
d->pChild = (XMLNode*)addToOrder(memoryIncrease, &pos, d->nChild, d->pChild, sizeof(XMLNode), eNodeChild);
d->pChild[pos].d = NULL;
d->pChild[pos] = XMLNode(d, lpszName, isDeclaration);
d->nChild++;
return d->pChild[pos];
}
// Add an attribute to an element.
XMLAttribute *XMLNode::addAttribute_priv(int memoryIncrease, XMLSTR lpszName, XMLSTR lpszValuev)
{
if (!lpszName) return &emptyXMLAttribute;
if (!d) { myFree(lpszName); myFree(lpszValuev); return &emptyXMLAttribute; }
int nc = d->nAttribute;
d->pAttribute = (XMLAttribute*)myRealloc(d->pAttribute, (nc+1), memoryIncrease, sizeof(XMLAttribute));
XMLAttribute *pAttr = d->pAttribute+nc;
pAttr->lpszName = lpszName;
pAttr->lpszValue = lpszValuev;
d->nAttribute++;
TCHAR* p = _tcschr(lpszName, ':');
if (p)
if (!mir_tstrcmp(p+1, d->lpszNS) || (d->pParent && !mir_tstrcmp(p+1, d->pParent->lpszNS)))
*p = 0;
return pAttr;
}
// Add text to the element.
XMLCSTR XMLNode::addText_priv(int memoryIncrease, XMLSTR lpszValue, int pos)
{
if (!lpszValue) return NULL;
if (!d) { myFree(lpszValue); return NULL; }
invalidateInnerText();
d->pText = (XMLCSTR*)addToOrder(memoryIncrease, &pos, d->nText, d->pText, sizeof(XMLSTR), eNodeText);
d->pText[pos] = lpszValue;
d->nText++;
return lpszValue;
}
// Add clear (unformatted) text to the element.
XMLClear *XMLNode::addClear_priv(int memoryIncrease, XMLSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, int pos)
{
if (!lpszValue) return &emptyXMLClear;
if (!d) { myFree(lpszValue); return &emptyXMLClear; }
invalidateInnerText();
d->pClear = (XMLClear *)addToOrder(memoryIncrease, &pos, d->nClear, d->pClear, sizeof(XMLClear), eNodeClear);
XMLClear *pNewClear = d->pClear+pos;
pNewClear->lpszValue = lpszValue;
if (!lpszOpen) lpszOpen = XMLClearTags->lpszOpen;
if (!lpszClose) lpszClose = XMLClearTags->lpszClose;
pNewClear->lpszOpenTag = lpszOpen;
pNewClear->lpszCloseTag = lpszClose;
d->nClear++;
return pNewClear;
}
// private:
// Parse a clear (unformatted) type node.
char XMLNode::parseClearTag(void *px, void *_pClear)
{
XML *pXML = (XML *)px;
ALLXMLClearTag pClear = *((ALLXMLClearTag*)_pClear);
int cbTemp = 0;
XMLCSTR lpszTemp = NULL;
XMLCSTR lpXML = &pXML->lpXML[pXML->nIndex];
static XMLCSTR docTypeEnd = _CXML("]>");
// Find the closing tag
// Seems the <!DOCTYPE need a better treatment so lets handle it
if (pClear.lpszOpen == XMLClearTags[1].lpszOpen)
{
XMLCSTR pCh = lpXML;
while (*pCh)
{
if (*pCh == _CXML('<')) { pClear.lpszClose = docTypeEnd; lpszTemp = xstrstr(lpXML, docTypeEnd); break; }
else if (*pCh == _CXML('>')) { lpszTemp = pCh; break; }
#ifdef _XMLWIDECHAR
pCh++;
#else
pCh+=XML_ByteTable[(unsigned char)(*pCh)];
#endif
}
} else lpszTemp = xstrstr(lpXML, pClear.lpszClose);
if (lpszTemp)
{
// Cache the size and increment the index
cbTemp = (int)(lpszTemp - lpXML);
pXML->nIndex += cbTemp+(int)xstrlen(pClear.lpszClose);
// Add the clear node to the current element
addClear_priv(MEMORYINCREASE, cbTemp?stringDup(lpXML, cbTemp):NULL, pClear.lpszOpen, pClear.lpszClose, -1);
return 0;
}
// If we failed to find the end tag
pXML->error = eXMLErrorUnmatchedEndClearTag;
return 1;
}
void XMLNode::exactMemory(XMLNodeData *d)
{
if (d->pOrder) d->pOrder = (int*)realloc(d->pOrder, (d->nChild+d->nText+d->nClear)*sizeof(int));
if (d->pChild) d->pChild = (XMLNode*)realloc(d->pChild, d->nChild*sizeof(XMLNode));
if (d->pAttribute) d->pAttribute = (XMLAttribute*)realloc(d->pAttribute, d->nAttribute*sizeof(XMLAttribute));
if (d->pText) d->pText = (XMLCSTR*)realloc(d->pText, d->nText*sizeof(XMLSTR));
if (d->pClear) d->pClear = (XMLClear *)realloc(d->pClear, d->nClear*sizeof(XMLClear));
}
char XMLNode::maybeAddTxT(void *pa, XMLCSTR tokenPStr)
{
XML *pXML = (XML *)pa;
XMLCSTR lpszText = pXML->lpszText;
if (!lpszText) return 0;
if (dropWhiteSpace) while (XML_isSPACECHAR(*lpszText)&&(lpszText != tokenPStr)) lpszText++;
int cbText = (int)(tokenPStr - lpszText);
if (!cbText) { pXML->lpszText = NULL; return 0; }
if (dropWhiteSpace) { cbText--; while ((cbText)&&XML_isSPACECHAR(lpszText[cbText])) cbText--; cbText++; }
if (!cbText) { pXML->lpszText = NULL; return 0; }
XMLSTR lpt = fromXMLString(lpszText, cbText, pXML);
if (!lpt) return 1;
pXML->lpszText = NULL;
if (removeCommentsInMiddleOfText && d->nText && d->nClear)
{
// if the previous insertion was a comment (<!-- -->) AND
// if the previous previous insertion was a text then, delete the comment and append the text
size_t n = d->nChild+d->nText+d->nClear-1;
int *o = d->pOrder;
if (((o[n]&3) == eNodeClear)&&((o[n-1]&3) == eNodeText))
{
int i = o[n]>>2;
if (d->pClear[i].lpszOpenTag == XMLClearTags[2].lpszOpen)
{
deleteClear(i);
i = o[n-1]>>2;
n = xstrlen(d->pText[i]);
size_t n2 = xstrlen(lpt)+1;
d->pText[i] = (XMLSTR)realloc((void*)d->pText[i], (n+n2)*sizeof(XMLCHAR));
if (!d->pText[i]) {
free(lpt);
return 1;
}
memcpy((void*)(d->pText[i]+n), lpt, n2*sizeof(XMLCHAR));
free(lpt);
return 0;
}
}
}
addText_priv(MEMORYINCREASE, lpt, -1);
return 0;
}
// private:
// Recursively parse an XML element.
int XMLNode::ParseXMLElement(void *pa)
{
XML *pXML = (XML *)pa;
int cbToken;
enum XMLTokenTypeTag xtype;
NextToken token;
XMLCSTR lpszTemp = NULL;
int cbTemp = 0;
char nDeclaration;
XMLNode pNew;
enum XMLStatus status; // inside or outside a tag
enum Attrib attrib = eAttribName;
assert(pXML);
// If this is the first call to the function
if (pXML->nFirst)
{
// Assume we are outside of a tag definition
pXML->nFirst = FALSE;
status = eOutsideTag;
} else
{
// If this is not the first call then we should only be called when inside a tag.
status = eInsideTag;
}
// Iterate through the tokens in the document
for (;;)
{
// Obtain the next token
token = GetNextToken(pXML, &cbToken, &xtype);
if (xtype != eTokenError)
{
// Check the current status
switch(status)
{
// If we are outside of a tag definition
case eOutsideTag:
// Check what type of token we obtained
switch(xtype)
{
// If we have found text or quoted text
case eTokenText:
case eTokenCloseTag: /* '>' */
case eTokenShortHandClose: /* '/>' */
case eTokenQuotedText:
case eTokenEquals:
break;
// If we found a start tag '<' and declarations '<?'
case eTokenTagStart:
case eTokenDeclaration:
// Cache whether this new element is a declaration or not
nDeclaration = (xtype == eTokenDeclaration);
// If we have node text then add this to the element
if (maybeAddTxT(pXML, token.pStr)) return FALSE;
// Find the name of the tag
token = GetNextToken(pXML, &cbToken, &xtype);
// Return an error if we couldn't obtain the next token or
// it wasnt text
if (xtype != eTokenText)
{
pXML->error = eXMLErrorMissingTagName;
return FALSE;
}
// If we found a new element which is the same as this
// element then we need to pass this back to the caller..
#ifdef APPROXIMATE_PARSING
if (d->lpszName &&
myTagCompare(d->lpszName, token.pStr) == 0)
{
// Indicate to the caller that it needs to create a
// new element.
pXML->lpNewElement = token.pStr;
pXML->cbNewElement = cbToken;
return TRUE;
} else
#endif
{
// If the name of the new element differs from the name of
// the current element we need to add the new element to
// the current one and recurse
pNew = addChild_priv(MEMORYINCREASE, stringDup(token.pStr, cbToken), nDeclaration, -1);
while (!pNew.isEmpty())
{
// Callself to process the new node. If we return
// FALSE this means we dont have any more
// processing to do...
if (!pNew.ParseXMLElement(pXML)) return FALSE;
else
{
// If the call to recurse this function
// evented in a end tag specified in XML then
// we need to unwind the calls to this
// function until we find the appropriate node
// (the element name and end tag name must
// match)
if (pXML->cbEndTag)
{
// If we are back at the root node then we
// have an unmatched end tag
if (!d->lpszName)
{
pXML->error = eXMLErrorUnmatchedEndTag;
return FALSE;
}
// If the end tag matches the name of this
// element then we only need to unwind
// once more...
if (myTagCompare(d->lpszName, pXML->lpEndTag) == 0)
{
pXML->cbEndTag = 0;
}
return TRUE;
} else
if (pXML->cbNewElement)
{
// If the call indicated a new element is to
// be created on THIS element.
// If the name of this element matches the
// name of the element we need to create
// then we need to return to the caller
// and let it process the element.
if (myTagCompare(d->lpszName, pXML->lpNewElement) == 0)
{
return TRUE;
}
// Add the new element and recurse
pNew = addChild_priv(MEMORYINCREASE, stringDup(pXML->lpNewElement, pXML->cbNewElement), 0, -1);
pXML->cbNewElement = 0;
}
else
{
// If we didn't have a new element to create
pNew = emptyXMLNode;
}
}
}
}
break;
// If we found an end tag
case eTokenTagEnd:
// If we have node text then add this to the element
if (maybeAddTxT(pXML, token.pStr)) return FALSE;
// Find the name of the end tag
token = GetNextToken(pXML, &cbTemp, &xtype);
// The end tag should be text
if (xtype != eTokenText)
{
pXML->error = eXMLErrorMissingEndTagName;
return FALSE;
}
lpszTemp = token.pStr;
// After the end tag we should find a closing tag
token = GetNextToken(pXML, &cbToken, &xtype);
if (xtype != eTokenCloseTag)
{
pXML->error = eXMLErrorMissingEndTagName;
return FALSE;
}
pXML->lpszText = pXML->lpXML+pXML->nIndex;
// We need to return to the previous caller. If the name
// of the tag cannot be found we need to keep returning to
// caller until we find a match
if (!d->lpszNS) {
if (myTagCompare(d->lpszName, lpszTemp) != 0)
#ifdef STRICT_PARSING
{
LBL_Error:
pXML->error = eXMLErrorUnmatchedEndTag;
pXML->nIndexMissigEndTag = pXML->nIndex;
return FALSE;
}
#else
{
LBL_Error:
pXML->error = eXMLErrorMissingEndTag;
pXML->nIndexMissigEndTag = pXML->nIndex;
pXML->lpEndTag = lpszTemp;
pXML->cbEndTag = cbTemp;
}
#endif
}
else {
const TCHAR* p = _tcschr(lpszTemp, ':');
if (!p)
goto LBL_Error;
if (myTagCompare(d->lpszName, p+1) != 0)
goto LBL_Error;
}
// Return to the caller
exactMemory(d);
return TRUE;
// If we found a clear (unformatted) token
case eTokenClear:
// If we have node text then add this to the element
if (maybeAddTxT(pXML, token.pStr)) return FALSE;
if (parseClearTag(pXML, token.pClr)) return FALSE;
pXML->lpszText = pXML->lpXML+pXML->nIndex;
break;
default:
break;
}
break;
// If we are inside a tag definition we need to search for attributes
case eInsideTag:
// Check what part of the attribute (name, equals, value) we
// are looking for.
switch(attrib)
{
// If we are looking for a new attribute
case eAttribName:
// Check what the current token type is
switch(xtype)
{
// If the current type is text...
// Eg. 'attribute'
case eTokenText:
// Cache the token then indicate that we are next to
// look for the equals
lpszTemp = token.pStr;
cbTemp = cbToken;
attrib = eAttribEquals;
break;
// If we found a closing tag...
// Eg. '>'
case eTokenCloseTag:
// We are now outside the tag
status = eOutsideTag;
pXML->lpszText = pXML->lpXML+pXML->nIndex;
break;
// If we found a short hand '/>' closing tag then we can
// return to the caller
case eTokenShortHandClose:
exactMemory(d);
pXML->lpszText = pXML->lpXML+pXML->nIndex;
return TRUE;
// Errors...
case eTokenQuotedText: /* '"SomeText"' */
case eTokenTagStart: /* '<' */
case eTokenTagEnd: /* '</' */
case eTokenEquals: /* '=' */
case eTokenDeclaration: /* '<?' */
case eTokenClear:
pXML->error = eXMLErrorUnexpectedToken;
return FALSE;
default: break;
}
break;
// If we are looking for an equals
case eAttribEquals:
// Check what the current token type is
switch(xtype)
{
// If the current type is text...
// Eg. 'Attribute AnotherAttribute'
case eTokenText:
// Add the unvalued attribute to the list
addAttribute_priv(MEMORYINCREASE, stringDup(lpszTemp, cbTemp), NULL);
// Cache the token then indicate. We are next to
// look for the equals attribute
lpszTemp = token.pStr;
cbTemp = cbToken;
break;
// If we found a closing tag 'Attribute >' or a short hand
// closing tag 'Attribute />'
case eTokenShortHandClose:
case eTokenCloseTag:
// If we are a declaration element '<?' then we need
// to remove extra closing '?' if it exists
pXML->lpszText = pXML->lpXML+pXML->nIndex;
if (d->isDeclaration &&
(lpszTemp[cbTemp-1]) == _CXML('?'))
{
cbTemp--;
if (d->pParent && d->pParent->pParent) xtype = eTokenShortHandClose;
}
if (cbTemp)
{
// Add the unvalued attribute to the list
addAttribute_priv(MEMORYINCREASE, stringDup(lpszTemp, cbTemp), NULL);
}
// If this is the end of the tag then return to the caller
if (xtype == eTokenShortHandClose)
{
exactMemory(d);
return TRUE;
}
// We are now outside the tag
status = eOutsideTag;
break;
// If we found the equals token...
// Eg. 'Attribute = '
case eTokenEquals:
// Indicate that we next need to search for the value
// for the attribute
attrib = eAttribValue;
break;
// Errors...
case eTokenQuotedText: /* 'Attribute "InvalidAttr"'*/
case eTokenTagStart: /* 'Attribute <' */
case eTokenTagEnd: /* 'Attribute </' */
case eTokenDeclaration: /* 'Attribute <?' */
case eTokenClear:
pXML->error = eXMLErrorUnexpectedToken;
return FALSE;
default: break;
}
break;
// If we are looking for an attribute value
case eAttribValue:
// Check what the current token type is
switch(xtype)
{
// If the current type is text or quoted text...
// Eg. 'Attribute = "Value"' or 'Attribute = Value' or
// 'Attribute = 'Value''.
case eTokenText:
case eTokenQuotedText:
// If we are a declaration element '<?' then we need
// to remove extra closing '?' if it exists
if (d->isDeclaration &&
(token.pStr[cbToken-1]) == _CXML('?'))
{
cbToken--;
}
if (cbTemp)
{
// Add the valued attribute to the list
if (xtype == eTokenQuotedText) { token.pStr++; cbToken-=2; }
XMLSTR attrVal = (XMLSTR)token.pStr;
if (attrVal)
{
attrVal = fromXMLString(attrVal, cbToken, pXML);
if (!attrVal) return FALSE;
}
addAttribute_priv(MEMORYINCREASE, stringDup(lpszTemp, cbTemp), attrVal);
}
// Indicate we are searching for a new attribute
attrib = eAttribName;
break;
// Errors...
case eTokenTagStart: /* 'Attr = <' */
case eTokenTagEnd: /* 'Attr = </' */
case eTokenCloseTag: /* 'Attr = >' */
case eTokenShortHandClose: /* "Attr = />" */
case eTokenEquals: /* 'Attr = = ' */
case eTokenDeclaration: /* 'Attr = <?' */
case eTokenClear:
pXML->error = eXMLErrorUnexpectedToken;
return FALSE;
break;
default: break;
}
}
}
}
// If we failed to obtain the next token
else
{
if ((!d->isDeclaration)&&(d->pParent))
{
#ifdef STRICT_PARSING
pXML->error = eXMLErrorUnmatchedEndTag;
#else
pXML->error = eXMLErrorMissingEndTag;
#endif
pXML->nIndexMissigEndTag = pXML->nIndex;
}
maybeAddTxT(pXML, pXML->lpXML+pXML->nIndex);
return FALSE;
}
}
}
// Count the number of lines and columns in an XML string.
static void CountLinesAndColumns(XMLCSTR lpXML, int nUpto, XMLResults *pResults)
{
XMLCHAR ch;
assert(lpXML);
assert(pResults);
struct XML xml = { lpXML, lpXML, 0, 0, eXMLErrorNone, NULL, 0, NULL, 0, TRUE };
pResults->nLine = 1;
pResults->nColumn = 1;
while (xml.nIndex<nUpto)
{
ch = getNextChar(&xml);
if (ch != _CXML('\n')) pResults->nColumn++;
else
{
pResults->nLine++;
pResults->nColumn = 1;
}
}
}
// Parse XML and return the root element.
XMLNode XMLNode::parseString(XMLCSTR lpszXML, XMLCSTR tag, XMLResults *pResults)
{
if (!lpszXML)
{
if (pResults)
{
pResults->error = eXMLErrorNoElements;
pResults->nLine = 0;
pResults->nColumn = 0;
}
return emptyXMLNode;
}
XMLNode xnode(NULL, NULL, FALSE);
struct XML xml = { lpszXML, lpszXML, 0, 0, eXMLErrorNone, NULL, 0, NULL, 0, TRUE };
// Create header element
xnode.ParseXMLElement(&xml);
enum XMLError error = xml.error;
if (!xnode.nChildNode()) error = eXMLErrorNoXMLTagFound;
if ((xnode.nChildNode() == 1)&&(xnode.nElement() == 1)) xnode = xnode.getChildNode(); // skip the empty node
// If no error occurred
if ((error == eXMLErrorNone) || (error == eXMLErrorMissingEndTag) || (error == eXMLErrorNoXMLTagFound))
{
XMLCSTR name = xnode.getName();
if (tag&&(*tag)&&((!name) || (xstricmp(name, tag))))
{
xnode = xnode.getChildNode(tag);
if (xnode.isEmpty())
{
if (pResults)
{
pResults->error = eXMLErrorFirstTagNotFound;
pResults->nLine = 0;
pResults->nColumn = 0;
pResults->nChars = xml.nIndex;
}
return emptyXMLNode;
}
}
} else
{
// Cleanup: this will destroy all the nodes
xnode = emptyXMLNode;
}
// If we have been given somewhere to place results
if (pResults)
{
pResults->error = error;
// If we have an error
if (error != eXMLErrorNone)
{
if (error == eXMLErrorMissingEndTag) xml.nIndex = xml.nIndexMissigEndTag;
// Find which line and column it starts on.
CountLinesAndColumns(xml.lpXML, xml.nIndex, pResults);
}
pResults->nChars = xml.nIndex;
}
return xnode;
}
XMLNode XMLNode::parseFile(XMLCSTR filename, XMLCSTR tag, XMLResults *pResults)
{
if (pResults) { pResults->nLine = 0; pResults->nColumn = 0; }
FILE *f = xfopen(filename, _CXML("rb"));
if (f == NULL) { if (pResults) pResults->error = eXMLErrorFileNotFound; return emptyXMLNode; }
fseek(f, 0, SEEK_END);
int l = (int)ftell(f), headerSz = 0;
if (!l) { if (pResults) pResults->error = eXMLErrorEmpty; fclose(f); return emptyXMLNode; }
fseek(f, 0, SEEK_SET);
unsigned char *buf = (unsigned char*)malloc(l+4);
l = (int)fread(buf, 1, l, f);
fclose(f);
buf[l] = 0;buf[l+1] = 0;buf[l+2] = 0;buf[l+3] = 0;
#ifdef _XMLWIDECHAR
if (guessWideCharChars)
{
if (!myIsTextWideChar(buf, l))
{
XMLNode::XMLCharEncoding ce = XMLNode::char_encoding_legacy;
if ((buf[0] == 0xef)&&(buf[1] == 0xbb)&&(buf[2] == 0xbf)) { headerSz = 3; ce = XMLNode::char_encoding_UTF8; }
XMLSTR b2 = myMultiByteToWideChar((const char*)(buf+headerSz), ce);
if (!b2)
{
// todo: unable to convert
}
free(buf); buf = (unsigned char*)b2; headerSz = 0;
} else
{
if ((buf[0] == 0xef)&&(buf[1] == 0xff)) headerSz = 2;
if ((buf[0] == 0xff)&&(buf[1] == 0xfe)) headerSz = 2;
}
} else
{
if ((buf[0] == 0xef)&&(buf[1] == 0xff)) headerSz = 2;
if ((buf[0] == 0xff)&&(buf[1] == 0xfe)) headerSz = 2;
if ((buf[0] == 0xef)&&(buf[1] == 0xbb)&&(buf[2] == 0xbf)) headerSz = 3;
}
#else
if (guessWideCharChars)
{
if (myIsTextWideChar(buf, l))
{
if ((buf[0] == 0xef)&&(buf[1] == 0xff)) headerSz = 2;
if ((buf[0] == 0xff)&&(buf[1] == 0xfe)) headerSz = 2;
char *b2 = myWideCharToMultiByte((const wchar_t*)(buf+headerSz));
free(buf); buf = (unsigned char*)b2; headerSz = 0;
} else
{
if ((buf[0] == 0xef)&&(buf[1] == 0xbb)&&(buf[2] == 0xbf)) headerSz = 3;
}
} else
{
if ((buf[0] == 0xef)&&(buf[1] == 0xff)) headerSz = 2;
if ((buf[0] == 0xff)&&(buf[1] == 0xfe)) headerSz = 2;
if ((buf[0] == 0xef)&&(buf[1] == 0xbb)&&(buf[2] == 0xbf)) headerSz = 3;
}
#endif
if (!buf) { if (pResults) pResults->error = eXMLErrorCharConversionError; return emptyXMLNode; }
XMLNode x = parseString((XMLSTR)(buf+headerSz), tag, pResults);
free(buf);
return x;
}
static inline void charmemset(XMLSTR dest, XMLCHAR c, int l) { while (l--) *(dest++) = c; }
// private:
// Creates an user friendly XML string from a given element with
// appropriate white space and carriage returns.
//
// This recurses through all subnodes then adds contents of the nodes to the
// string.
int XMLNode::CreateXMLStringR(XMLNodeData *pEntry, XMLSTR lpszMarker, int nFormat)
{
int nResult = 0;
int cb = nFormat<0?0:nFormat;
int cbElement;
int nChildFormat = -1;
int nElementI;
int i, j;
assert(pEntry);
nElementI = pEntry->nChild+pEntry->nText+pEntry->nClear;
if ((nFormat>=0)&&(nElementI == 1)&&(pEntry->nText == 1)&&(!pEntry->isDeclaration)) nFormat = -2;
#define LENSTR(lpsz) (lpsz ? xstrlen(lpsz) : 0)
// If the element has no name then assume this is the head node.
cbElement = (int)LENSTR(pEntry->lpszName);
if (cbElement)
{
// "<elementname "
if (lpszMarker)
{
if (cb) charmemset(lpszMarker, INDENTCHAR, cb);
nResult = cb;
lpszMarker[nResult++] = _CXML('<');
if (pEntry->isDeclaration) lpszMarker[nResult++] = _CXML('?');
xstrcpy(&lpszMarker[nResult], pEntry->lpszName);
nResult+=cbElement;
lpszMarker[nResult++] = _CXML(' ');
} else
{
nResult+=cbElement+2+cb;
if (pEntry->isDeclaration) nResult++;
}
// Enumerate attributes and add them to the string
XMLAttribute *pAttr = pEntry->pAttribute;
for (i=0; i<pEntry->nAttribute; i++)
{
// "Attrib
cb = (int)LENSTR(pAttr->lpszName);
if (cb)
{
if (lpszMarker) xstrcpy(&lpszMarker[nResult], pAttr->lpszName);
nResult += cb;
// "Attrib = Value "
if (pAttr->lpszValue)
{
cb = (int)ToXMLStringTool::lengthXMLString(pAttr->lpszValue);
if (lpszMarker)
{
lpszMarker[nResult] = _CXML('=');
lpszMarker[nResult+1] = _CXML('"');
if (cb) ToXMLStringTool::toXMLUnSafe(&lpszMarker[nResult+2], pAttr->lpszValue);
lpszMarker[nResult+cb+2] = _CXML('"');
}
nResult+=cb+3;
}
if (lpszMarker) lpszMarker[nResult] = _CXML(' ');
nResult++;
}
pAttr++;
}
if (pEntry->isDeclaration)
{
if (lpszMarker)
{
lpszMarker[nResult-1] = _CXML('?');
lpszMarker[nResult] = _CXML('>');
}
nResult++;
if (nFormat != -1)
{
if (lpszMarker) lpszMarker[nResult] = _CXML('\n');
nResult++;
}
} else
// If there are child nodes we need to terminate the start tag
if (nElementI)
{
if (lpszMarker) lpszMarker[nResult-1] = _CXML('>');
if (nFormat>=0)
{
if (lpszMarker) lpszMarker[nResult] = _CXML('\n');
nResult++;
}
} else nResult--;
}
// Calculate the child format for when we recurse. This is used to
// determine the number of spaces used for prefixes.
if (nFormat != -1)
{
if (cbElement&&(!pEntry->isDeclaration)) nChildFormat = nFormat+1;
else nChildFormat = nFormat;
}
// Enumerate through remaining children
for (i=0; i<nElementI; i++)
{
j = pEntry->pOrder[i];
switch((XMLElementType)(j&3))
{
// Text nodes
case eNodeText:
{
// "Text"
XMLCSTR pChild = pEntry->pText[j>>2];
cb = (int)ToXMLStringTool::lengthXMLString(pChild);
if (cb)
{
if (nFormat>=0)
{
if (lpszMarker)
{
charmemset(&lpszMarker[nResult], INDENTCHAR, nFormat+1);
ToXMLStringTool::toXMLUnSafe(&lpszMarker[nResult+nFormat+1], pChild);
lpszMarker[nResult+nFormat+1+cb] = _CXML('\n');
}
nResult+=cb+nFormat+2;
} else
{
if (lpszMarker) ToXMLStringTool::toXMLUnSafe(&lpszMarker[nResult], pChild);
nResult += cb;
}
}
break;
}
// Clear type nodes
case eNodeClear:
{
XMLClear *pChild = pEntry->pClear+(j>>2);
// "OpenTag"
cb = (int)LENSTR(pChild->lpszOpenTag);
if (cb)
{
if (nFormat != -1)
{
if (lpszMarker)
{
charmemset(&lpszMarker[nResult], INDENTCHAR, nFormat+1);
xstrcpy(&lpszMarker[nResult+nFormat+1], pChild->lpszOpenTag);
}
nResult+=cb+nFormat+1;
}
else
{
if (lpszMarker)xstrcpy(&lpszMarker[nResult], pChild->lpszOpenTag);
nResult += cb;
}
}
// "OpenTag Value"
cb = (int)LENSTR(pChild->lpszValue);
if (cb)
{
if (lpszMarker) xstrcpy(&lpszMarker[nResult], pChild->lpszValue);
nResult += cb;
}
// "OpenTag Value CloseTag"
cb = (int)LENSTR(pChild->lpszCloseTag);
if (cb)
{
if (lpszMarker) xstrcpy(&lpszMarker[nResult], pChild->lpszCloseTag);
nResult += cb;
}
if (nFormat != -1)
{
if (lpszMarker) lpszMarker[nResult] = _CXML('\n');
nResult++;
}
break;
}
// Element nodes
case eNodeChild:
{
// Recursively add child nodes
nResult += CreateXMLStringR(pEntry->pChild[j>>2].d, lpszMarker ? lpszMarker + nResult : 0, nChildFormat);
break;
}
default: break;
}
}
if ((cbElement)&&(!pEntry->isDeclaration))
{
// If we have child entries we need to use long XML notation for
// closing the element - "<elementname>blah blah blah</elementname>"
if (nElementI)
{
// "</elementname>\0"
if (lpszMarker)
{
if (nFormat >=0)
{
charmemset(&lpszMarker[nResult], INDENTCHAR, nFormat);
nResult+=nFormat;
}
lpszMarker[nResult] = _CXML('<'); lpszMarker[nResult+1] = _CXML('/');
nResult += 2;
xstrcpy(&lpszMarker[nResult], pEntry->lpszName);
nResult += cbElement;
lpszMarker[nResult] = _CXML('>');
if (nFormat == -1) nResult++;
else
{
lpszMarker[nResult+1] = _CXML('\n');
nResult+=2;
}
} else
{
if (nFormat>=0) nResult+=cbElement+4+nFormat;
else if (nFormat == -1) nResult+=cbElement+3;
else nResult+=cbElement+4;
}
} else
{
// If there are no children we can use shorthand XML notation -
// "<elementname/>"
// "/>\0"
if (lpszMarker)
{
lpszMarker[nResult] = _CXML('/'); lpszMarker[nResult+1] = _CXML('>');
if (nFormat != -1) lpszMarker[nResult+2] = _CXML('\n');
}
nResult += nFormat == -1 ? 2 : 3;
}
}
return nResult;
}
#undef LENSTR
// Create an XML string
// @param int nFormat - 0 if no formatting is required
// otherwise nonzero for formatted text
// with carriage returns and indentation.
// @param int *pnSize - [out] pointer to the size of the
// returned string not including the
// NULL terminator.
// @return XMLSTR - Allocated XML string, you must free
// this with free().
XMLSTR XMLNode::createXMLString(int nFormat, int *pnSize) const
{
if (!d) { if (pnSize) *pnSize = 0; return NULL; }
XMLSTR lpszResult = NULL;
int cbStr;
// Recursively Calculate the size of the XML string
if (!dropWhiteSpace) nFormat = 0;
nFormat = nFormat ? 0 : -1;
cbStr = CreateXMLStringR(d, 0, nFormat);
// Alllocate memory for the XML string + the NULL terminator and
// create the recursively XML string.
lpszResult = (XMLSTR)malloc((cbStr+1)*sizeof(XMLCHAR));
CreateXMLStringR(d, lpszResult, nFormat);
lpszResult[cbStr] = _CXML('\0');
if (pnSize) *pnSize = cbStr;
return lpszResult;
}
int XMLNode::detachFromParent(XMLNodeData *d)
{
XMLNode *pa = d->pParent->pChild;
int i=0;
while (((void*)(pa[i].d)) != ((void*)d)) i++;
d->pParent->nChild--;
if (d->pParent->nChild) memmove(pa+i, pa+i+1, (d->pParent->nChild-i)*sizeof(XMLNode));
else { free(pa); d->pParent->pChild = NULL; }
return removeOrderElement(d->pParent, eNodeChild, i);
}
XMLNode::~XMLNode()
{
if (!d) return;
d->ref_count--;
emptyTheNode(0);
}
void XMLNode::deleteNodeContent()
{
if (!d) return;
if (d->pParent) { detachFromParent(d); d->pParent = NULL; d->ref_count--; }
emptyTheNode(1);
}
void XMLNode::emptyTheNode(char force)
{
XMLNodeData *dd = d; // warning: must stay this way!
if ((dd->ref_count == 0) || force)
{
if (d->pParent) detachFromParent(d);
int i;
XMLNode *pc;
for (i=0; i<dd->nChild; i++)
{
pc = dd->pChild+i;
pc->d->pParent = NULL;
pc->d->ref_count--;
pc->emptyTheNode(force);
}
myFree(dd->pChild);
for (i=0; i<dd->nText; i++) free((void*)dd->pText[i]);
myFree(dd->pText);
for (i=0; i<dd->nClear; i++) free((void*)dd->pClear[i].lpszValue);
myFree(dd->pClear);
for (i=0; i<dd->nAttribute; i++)
{
free((void*)dd->pAttribute[i].lpszName);
if (dd->pAttribute[i].lpszValue) free((void*)dd->pAttribute[i].lpszValue);
}
myFree(dd->pAttribute);
myFree(dd->pOrder);
myFree(dd->pInnerText);
if (dd->lpszNS)
myFree((void*)dd->lpszNS);
else
myFree((void*)dd->lpszName);
dd->nChild = 0; dd->nText = 0; dd->nClear = 0; dd->nAttribute = 0;
dd->pChild = NULL; dd->pText = NULL; dd->pClear = NULL; dd->pAttribute = NULL;
dd->pOrder = NULL; dd->pInnerText = NULL; dd->lpszNS = dd->lpszName = NULL; dd->pParent = NULL;
}
if (dd->ref_count == 0)
{
free(dd);
d = NULL;
}
}
void XMLNode::invalidateInnerText()
{
if (!d) return;
myFree(d->pInnerText);
d->pInnerText = NULL;
}
XMLNode& XMLNode::operator = (const XMLNode& A)
{
// shallow copy
if (this != &A)
{
if (d) { d->ref_count--; emptyTheNode(0); }
d = A.d;
if (d) (d->ref_count) ++;
}
return *this;
}
XMLNode::XMLNode(const XMLNode &A)
{
// shallow copy
d = A.d;
if (d) (d->ref_count)++;
}
XMLNode XMLNode::deepCopy() const
{
if (!d) return XMLNode::emptyXMLNode;
XMLNode x(NULL, stringDup(d->lpszName), d->isDeclaration);
XMLNodeData *p = x.d;
int n = d->nAttribute;
if (n)
{
p->nAttribute = n; p->pAttribute = (XMLAttribute*)malloc(n*sizeof(XMLAttribute));
while (n--)
{
p->pAttribute[n].lpszName = stringDup(d->pAttribute[n].lpszName);
p->pAttribute[n].lpszValue = stringDup(d->pAttribute[n].lpszValue);
}
}
if (d->pOrder)
{
n = (d->nChild+d->nText+d->nClear)*sizeof(int); p->pOrder = (int*)malloc(n); memcpy(p->pOrder, d->pOrder, n);
}
n = d->nText;
if (n)
{
p->nText = n; p->pText = (XMLCSTR*)malloc(n*sizeof(XMLCSTR));
while (n--) p->pText[n] = stringDup(d->pText[n]);
}
n = d->nClear;
if (n)
{
p->nClear = n; p->pClear = (XMLClear*)malloc(n*sizeof(XMLClear));
while (n--)
{
p->pClear[n].lpszCloseTag = d->pClear[n].lpszCloseTag;
p->pClear[n].lpszOpenTag = d->pClear[n].lpszOpenTag;
p->pClear[n].lpszValue = stringDup(d->pClear[n].lpszValue);
}
}
n = d->nChild;
if (n)
{
p->nChild = n; p->pChild = (XMLNode*)malloc(n*sizeof(XMLNode));
while (n--)
{
p->pChild[n].d = NULL;
p->pChild[n] = d->pChild[n].deepCopy();
p->pChild[n].d->pParent = p;
}
}
return x;
}
XMLNode XMLNode::addChild(XMLNode childNode, int pos)
{
XMLNodeData *dc = childNode.d;
if ((!dc) || (!d)) return childNode;
if (!dc->lpszName)
{
// this is a root node: todo: correct fix
int j = pos;
while (dc->nChild)
{
addChild(dc->pChild[0], j);
if (pos>=0) j++;
}
return childNode;
}
if (dc->pParent) { if ((detachFromParent(dc) <= pos)&&(dc->pParent == d)) pos--; } else dc->ref_count++;
dc->pParent = d;
// int nc = d->nChild;
// d->pChild = (XMLNode*)myRealloc(d->pChild, (nc+1), memoryIncrease, sizeof(XMLNode));
d->pChild = (XMLNode*)addToOrder(0, &pos, d->nChild, d->pChild, sizeof(XMLNode), eNodeChild);
d->pChild[pos].d = dc;
d->nChild++;
return childNode;
}
void XMLNode::deleteAttribute(int i)
{
if ((!d) || (i<0) || (i>=d->nAttribute)) return;
d->nAttribute--;
XMLAttribute *p = d->pAttribute+i;
free((void*)p->lpszName);
if (p->lpszValue) free((void*)p->lpszValue);
if (d->nAttribute) memmove(p, p+1, (d->nAttribute-i)*sizeof(XMLAttribute)); else { free(p); d->pAttribute = NULL; }
}
void XMLNode::deleteAttribute(XMLAttribute *a) { if (a) deleteAttribute(a->lpszName); }
void XMLNode::deleteAttribute(XMLCSTR lpszName)
{
int j = 0;
getAttribute(lpszName, &j);
if (j) deleteAttribute(j-1);
}
XMLAttribute *XMLNode::updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName, int i)
{
if (!d) { if (lpszNewValue) free(lpszNewValue); if (lpszNewName) free(lpszNewName); return NULL; }
if (i>=d->nAttribute)
{
if (lpszNewName) return addAttribute_WOSD(lpszNewName, lpszNewValue);
return NULL;
}
XMLAttribute *p = d->pAttribute+i;
if (p->lpszValue&&p->lpszValue != lpszNewValue) free((void*)p->lpszValue);
p->lpszValue = lpszNewValue;
if (lpszNewName&&p->lpszName != lpszNewName) { free((void*)p->lpszName); p->lpszName = lpszNewName; };
return p;
}
XMLAttribute *XMLNode::updateAttribute_WOSD(XMLAttribute *newAttribute, XMLAttribute *oldAttribute)
{
if (oldAttribute) return updateAttribute_WOSD((XMLSTR)newAttribute->lpszValue, (XMLSTR)newAttribute->lpszName, oldAttribute->lpszName);
return addAttribute_WOSD((XMLSTR)newAttribute->lpszName, (XMLSTR)newAttribute->lpszValue);
}
XMLAttribute *XMLNode::updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName, XMLCSTR lpszOldName)
{
int j = 0;
getAttribute(lpszOldName, &j);
if (j) return updateAttribute_WOSD(lpszNewValue, lpszNewName, j-1);
else
{
if (lpszNewName) return addAttribute_WOSD(lpszNewName, lpszNewValue);
else return addAttribute_WOSD(stringDup(lpszOldName), lpszNewValue);
}
}
int XMLNode::indexText(XMLCSTR lpszValue) const
{
if (!d) return -1;
int i, l = d->nText;
if (!lpszValue) { if (l) return 0; return -1; }
XMLCSTR *p = d->pText;
for (i=0; i<l; i++) if (lpszValue == p[i]) return i;
return -1;
}
void XMLNode::deleteText(int i)
{
if ((!d) || (i<0) || (i>=d->nText)) return;
invalidateInnerText();
d->nText--;
XMLCSTR *p = d->pText+i;
free((void*)*p);
if (d->nText) memmove(p, p+1, (d->nText-i)*sizeof(XMLCSTR)); else { free(p); d->pText = NULL; }
removeOrderElement(d, eNodeText, i);
}
void XMLNode::deleteText(XMLCSTR lpszValue) { deleteText(indexText(lpszValue)); }
XMLCSTR XMLNode::updateText_WOSD(XMLSTR lpszNewValue, int i)
{
if (!d) { if (lpszNewValue) free(lpszNewValue); return NULL; }
if (i>=d->nText) return addText_WOSD(lpszNewValue);
invalidateInnerText();
XMLCSTR *p = d->pText+i;
if (*p != lpszNewValue) { free((void*)*p); *p = lpszNewValue; }
return lpszNewValue;
}
XMLCSTR XMLNode::updateText_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue)
{
if (!d) { if (lpszNewValue) free(lpszNewValue); return NULL; }
int i = indexText(lpszOldValue);
if (i>=0) return updateText_WOSD(lpszNewValue, i);
return addText_WOSD(lpszNewValue);
}
void XMLNode::deleteClear(int i)
{
if ((!d) || (i<0) || (i>=d->nClear)) return;
invalidateInnerText();
d->nClear--;
XMLClear *p = d->pClear+i;
free((void*)p->lpszValue);
if (d->nClear) memmove(p, p+1, (d->nClear-i)*sizeof(XMLClear)); else { free(p); d->pClear = NULL; }
removeOrderElement(d, eNodeClear, i);
}
int XMLNode::indexClear(XMLCSTR lpszValue) const
{
if (!d) return -1;
int i, l = d->nClear;
if (!lpszValue) { if (l) return 0; return -1; }
XMLClear *p = d->pClear;
for (i=0; i<l; i++) if (lpszValue == p[i].lpszValue) return i;
return -1;
}
void XMLNode::deleteClear(XMLCSTR lpszValue) { deleteClear(indexClear(lpszValue)); }
void XMLNode::deleteClear(XMLClear *a) { if (a) deleteClear(a->lpszValue); }
XMLClear *XMLNode::updateClear_WOSD(XMLSTR lpszNewContent, int i)
{
if (!d) { if (lpszNewContent) free(lpszNewContent); return NULL; }
if (i>=d->nClear) return addClear_WOSD(lpszNewContent);
invalidateInnerText();
XMLClear *p = d->pClear+i;
if (lpszNewContent != p->lpszValue) { free((void*)p->lpszValue); p->lpszValue = lpszNewContent; }
return p;
}
XMLClear *XMLNode::updateClear_WOSD(XMLSTR lpszNewContent, XMLCSTR lpszOldValue)
{
if (!d) { if (lpszNewContent) free(lpszNewContent); return NULL; }
int i = indexClear(lpszOldValue);
if (i>=0) return updateClear_WOSD(lpszNewContent, i);
return addClear_WOSD(lpszNewContent);
}
XMLClear *XMLNode::updateClear_WOSD(XMLClear *newP, XMLClear *oldP)
{
if (oldP) return updateClear_WOSD((XMLSTR)newP->lpszValue, (XMLSTR)oldP->lpszValue);
return NULL;
}
int XMLNode::nChildNode(XMLCSTR name) const
{
if (!d) return 0;
int i, j = 0, n = d->nChild;
XMLNode *pc = d->pChild;
for (i=0; i<n; i++)
{
if (xstricmp(pc->d->lpszName, name) == 0) j++;
pc++;
}
return j;
}
XMLNode XMLNode::getChildNode(XMLCSTR name, int *j) const
{
if (!d) return emptyXMLNode;
int i=0, n = d->nChild;
if (j) i = *j;
XMLNode *pc = d->pChild+i;
for (; i<n; i++)
{
if (!xstricmp(pc->d->lpszName, name))
{
if (j) *j = i+1;
return *pc;
}
pc++;
}
return emptyXMLNode;
}
XMLNode XMLNode::getChildNode(XMLCSTR name, int j) const
{
if (!d) return emptyXMLNode;
if (j>=0)
{
int i=0;
while (j-->0) getChildNode(name, &i);
return getChildNode(name, &i);
}
int i = d->nChild;
while (i--) if (!xstricmp(name, d->pChild[i].d->lpszName)) break;
if (i<0) return emptyXMLNode;
return getChildNode(i);
}
XMLNode XMLNode::getNextNode() const
{
if (!d) return emptyXMLNode;
XMLNodeDataTag *par = d->pParent;
if (!par) return emptyXMLNode;
int i, n = par->nChild;
for (i=0; i<n; i++)
if (par->pChild[i].d == d)
break;
return XMLNode(par).getChildNode(d->lpszName, &++i);
}
XMLNode XMLNode::getChildNodeByPath(XMLCSTR _path, char createMissing, XMLCHAR sep)
{
XMLSTR path = stringDup(_path);
XMLNode x = getChildNodeByPathNonConst(path, createMissing, sep);
if (path) free(path);
return x;
}
XMLNode XMLNode::getChildNodeByPathNonConst(XMLSTR path, char createIfMissing, XMLCHAR sep)
{
if ((!path) || (!(*path))) return *this;
XMLNode xn, xbase = *this;
XMLCHAR *tend1, sepString[2]; sepString[0] = sep; sepString[1] = 0;
tend1 = xstrstr(path, sepString);
while (tend1)
{
*tend1 = 0;
xn = xbase.getChildNode(path);
if (xn.isEmpty())
{
if (createIfMissing) xn = xbase.addChild(path);
else { *tend1 = sep; return XMLNode::emptyXMLNode; }
}
*tend1 = sep;
xbase = xn;
path = tend1+1;
tend1 = xstrstr(path, sepString);
}
xn = xbase.getChildNode(path);
if (xn.isEmpty()&&createIfMissing) xn = xbase.addChild(path);
return xn;
}
XMLElementPosition XMLNode::positionOfText (int i) const { if (i>=d->nText) i = d->nText-1; return findPosition(d, i, eNodeText); }
XMLElementPosition XMLNode::positionOfClear (int i) const { if (i>=d->nClear) i = d->nClear-1; return findPosition(d, i, eNodeClear); }
XMLElementPosition XMLNode::positionOfChildNode(int i) const { if (i>=d->nChild) i = d->nChild-1; return findPosition(d, i, eNodeChild); }
XMLElementPosition XMLNode::positionOfText (XMLCSTR lpszValue) const { return positionOfText (indexText (lpszValue)); }
XMLElementPosition XMLNode::positionOfClear(XMLCSTR lpszValue) const { return positionOfClear(indexClear(lpszValue)); }
XMLElementPosition XMLNode::positionOfClear(XMLClear *a) const { if (a) return positionOfClear(a->lpszValue); return positionOfClear(); }
XMLElementPosition XMLNode::positionOfChildNode(XMLNode x) const
{
if ((!d) || (!x.d)) return -1;
XMLNodeData *dd = x.d;
XMLNode *pc = d->pChild;
int i = d->nChild;
while (i--) if (pc[i].d == dd) return findPosition(d, i, eNodeChild);
return -1;
}
XMLElementPosition XMLNode::positionOfChildNode(XMLCSTR name, int count) const
{
if (!name) return positionOfChildNode(count);
int j = 0;
do { getChildNode(name, &j); if (j<0) return -1; } while (count--);
return findPosition(d, j-1, eNodeChild);
}
XMLNode XMLNode::getChildNodeWithAttribute(XMLCSTR name, XMLCSTR attributeName, XMLCSTR attributeValue, int *k) const
{
int i=0, j;
if (k) i = *k;
XMLNode x;
XMLCSTR t;
do
{
x = getChildNode(name, &i);
if (!x.isEmpty())
{
if (attributeValue)
{
j = 0;
do
{
t = x.getAttribute(attributeName, &j);
if (t&&(xstricmp(attributeValue, t) == 0)) { if (k) *k = i; return x; }
} while (t);
} else
{
if (x.isAttributeSet(attributeName)) { if (k) *k = i; return x; }
}
}
} while (!x.isEmpty());
return emptyXMLNode;
}
// Find an attribute on an node.
XMLCSTR XMLNode::getAttribute(XMLCSTR lpszAttrib, int *j) const
{
if (!d) return NULL;
int i=0, n = d->nAttribute;
if (j) i = *j;
XMLAttribute *pAttr = d->pAttribute+i;
for (; i<n; i++)
{
if (xstricmp(pAttr->lpszName, lpszAttrib) == 0)
{
if (j) *j = i+1;
return pAttr->lpszValue;
}
pAttr++;
}
return NULL;
}
char XMLNode::isAttributeSet(XMLCSTR lpszAttrib) const
{
if (!d) return FALSE;
int i, n = d->nAttribute;
XMLAttribute *pAttr = d->pAttribute;
for (i=0; i<n; i++)
{
if (xstricmp(pAttr->lpszName, lpszAttrib) == 0)
{
return TRUE;
}
pAttr++;
}
return FALSE;
}
XMLCSTR XMLNode::getAttribute(XMLCSTR name, int j) const
{
if (!d) return NULL;
int i=0;
while (j-->0) getAttribute(name, &i);
return getAttribute(name, &i);
}
XMLNodeContents XMLNode::enumContents(int i) const
{
XMLNodeContents c;
if (!d) { c.etype = eNodeNULL; return c; }
if (i<d->nAttribute)
{
c.etype = eNodeAttribute;
c.attrib = d->pAttribute[i];
return c;
}
i-=d->nAttribute;
c.etype = (XMLElementType)(d->pOrder[i]&3);
i = (d->pOrder[i])>>2;
switch (c.etype)
{
case eNodeChild: c.child = d->pChild[i]; break;
case eNodeText: c.text = d->pText[i]; break;
case eNodeClear: c.clear = d->pClear[i]; break;
default: break;
}
return c;
}
XMLCSTR XMLNode::getInnerText() const
{
if (!d) return NULL;
if (nText() <= 1 && nClear() == 0) return getText();
if (d->pInnerText) return d->pInnerText;
int count = nElement();
size_t length = 1;
for (int i=0; i < count; i++)
{
XMLNodeContents c = enumContents(i);
switch (c.etype)
{
case eNodeText:
length += xstrlen(c.text);
break;
case eNodeClear:
length += xstrlen(c.clear.lpszValue);
break;
}
}
XMLCHAR *buf = (XMLCHAR *)malloc(sizeof(XMLCHAR) * length);
XMLCHAR *pos = buf;
for (int i=0; i < count; i++)
{
XMLNodeContents c = enumContents(i);
switch (c.etype)
{
case eNodeText:
xstrcpy(pos, c.text);
pos += xstrlen(c.text);
break;
case eNodeClear:
xstrcpy(pos, c.clear.lpszValue);
pos += xstrlen(c.clear.lpszValue);
break;
}
}
return d->pInnerText = buf;
}
XMLCSTR XMLNode::getName() const { if (!d) return NULL; return d->lpszName; }
int XMLNode::nText() const { if (!d) return 0; return d->nText; }
int XMLNode::nChildNode() const { if (!d) return 0; return d->nChild; }
int XMLNode::nAttribute() const { if (!d) return 0; return d->nAttribute; }
int XMLNode::nClear() const { if (!d) return 0; return d->nClear; }
int XMLNode::nElement() const { if (!d) return 0; return d->nAttribute+d->nChild+d->nText+d->nClear; }
XMLClear XMLNode::getClear (int i) const { if ((!d) || (i>=d->nClear)) return emptyXMLClear; return d->pClear[i]; }
XMLAttribute XMLNode::getAttribute (int i) const { if ((!d) || (i>=d->nAttribute)) return emptyXMLAttribute; return d->pAttribute[i]; }
XMLCSTR XMLNode::getAttributeName (int i) const { if ((!d) || (i>=d->nAttribute)) return NULL; return d->pAttribute[i].lpszName; }
XMLCSTR XMLNode::getAttributeValue(int i) const { if ((!d) || (i>=d->nAttribute)) return NULL; return d->pAttribute[i].lpszValue; }
XMLCSTR XMLNode::getText (int i) const { if ((!d) || (i>=d->nText)) return NULL; return d->pText[i]; }
XMLNode XMLNode::getChildNode (int i) const { if ((!d) || (i>=d->nChild)) return emptyXMLNode; return d->pChild[i]; }
XMLNode XMLNode::getParentNode () const { if ((!d) || (!d->pParent)) return emptyXMLNode; return XMLNode(d->pParent); }
char XMLNode::isDeclaration () const { if (!d) return 0; return d->isDeclaration; }
char XMLNode::isEmpty () const { return (d == NULL); }
XMLNode XMLNode::emptyNode () { return XMLNode::emptyXMLNode; }
XMLNode XMLNode::addChild(XMLCSTR lpszName, char isDeclaration, XMLElementPosition pos)
{ return addChild_priv(0, stringDup(lpszName), isDeclaration, pos); }
XMLNode XMLNode::addChild_WOSD(XMLSTR lpszName, char isDeclaration, XMLElementPosition pos)
{ return addChild_priv(0, lpszName, isDeclaration, pos); }
XMLAttribute *XMLNode::addAttribute(XMLCSTR lpszName, XMLCSTR lpszValue)
{ return addAttribute_priv(0, stringDup(lpszName), stringDup(lpszValue)); }
XMLAttribute *XMLNode::addAttribute_WOSD(XMLSTR lpszName, XMLSTR lpszValuev)
{ return addAttribute_priv(0, lpszName, lpszValuev); }
XMLCSTR XMLNode::addText(XMLCSTR lpszValue, XMLElementPosition pos)
{ return addText_priv(0, stringDup(lpszValue), pos); }
XMLCSTR XMLNode::addText_WOSD(XMLSTR lpszValue, XMLElementPosition pos)
{ return addText_priv(0, lpszValue, pos); }
XMLClear *XMLNode::addClear(XMLCSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, XMLElementPosition pos)
{ return addClear_priv(0, stringDup(lpszValue), lpszOpen, lpszClose, pos); }
XMLClear *XMLNode::addClear_WOSD(XMLSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, XMLElementPosition pos)
{ return addClear_priv(0, lpszValue, lpszOpen, lpszClose, pos); }
XMLCSTR XMLNode::updateName(XMLCSTR lpszName)
{ return updateName_WOSD(stringDup(lpszName)); }
XMLAttribute *XMLNode::updateAttribute(XMLAttribute *newAttribute, XMLAttribute *oldAttribute)
{ return updateAttribute_WOSD(stringDup(newAttribute->lpszValue), stringDup(newAttribute->lpszName), oldAttribute->lpszName); }
XMLAttribute *XMLNode::updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName, int i)
{ return updateAttribute_WOSD(stringDup(lpszNewValue), stringDup(lpszNewName), i); }
XMLAttribute *XMLNode::updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName, XMLCSTR lpszOldName)
{ return updateAttribute_WOSD(stringDup(lpszNewValue), stringDup(lpszNewName), lpszOldName); }
XMLCSTR XMLNode::updateText(XMLCSTR lpszNewValue, int i)
{ return updateText_WOSD(stringDup(lpszNewValue), i); }
XMLCSTR XMLNode::updateText(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue)
{ return updateText_WOSD(stringDup(lpszNewValue), lpszOldValue); }
XMLClear *XMLNode::updateClear(XMLCSTR lpszNewContent, int i)
{ return updateClear_WOSD(stringDup(lpszNewContent), i); }
XMLClear *XMLNode::updateClear(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue)
{ return updateClear_WOSD(stringDup(lpszNewValue), lpszOldValue); }
XMLClear *XMLNode::updateClear(XMLClear *newP, XMLClear *oldP)
{ return updateClear_WOSD(stringDup(newP->lpszValue), oldP->lpszValue); }
char XMLNode::setGlobalOptions(XMLCharEncoding _characterEncoding, char _guessWideCharChars,
char _dropWhiteSpace, char _removeCommentsInMiddleOfText)
{
guessWideCharChars = _guessWideCharChars; dropWhiteSpace = _dropWhiteSpace; removeCommentsInMiddleOfText = _removeCommentsInMiddleOfText;
#ifdef _XMLWIDECHAR
if (_characterEncoding) characterEncoding = _characterEncoding;
#else
switch(_characterEncoding)
{
case char_encoding_UTF8: characterEncoding = _characterEncoding; XML_ByteTable = XML_utf8ByteTable; break;
case char_encoding_legacy: characterEncoding = _characterEncoding; XML_ByteTable = XML_legacyByteTable; break;
case char_encoding_ShiftJIS: characterEncoding = _characterEncoding; XML_ByteTable = XML_sjisByteTable; break;
case char_encoding_GB2312: characterEncoding = _characterEncoding; XML_ByteTable = XML_gb2312ByteTable; break;
case char_encoding_Big5:
case char_encoding_GBK: characterEncoding = _characterEncoding; XML_ByteTable = XML_gbk_big5_ByteTable; break;
default: return 1;
}
#endif
return 0;
}
XMLNode::XMLCharEncoding XMLNode::guessCharEncoding(void *buf, int l, char useXMLEncodingAttribute)
{
#ifdef _XMLWIDECHAR
return (XMLCharEncoding)0;
#else
if (l<25) return (XMLCharEncoding)0;
if (guessWideCharChars&&(myIsTextWideChar(buf, l))) return (XMLCharEncoding)0;
unsigned char *b = (unsigned char*)buf;
if ((b[0] == 0xef)&&(b[1] == 0xbb)&&(b[2] == 0xbf)) return char_encoding_UTF8;
// Match utf-8 model ?
XMLCharEncoding bestGuess = char_encoding_UTF8;
int i=0;
while (i<l)
switch (XML_utf8ByteTable[b[i]])
{
case 4: i++; if ((i<l)&&(b[i]& 0xC0) != 0x80) { bestGuess = char_encoding_legacy; i = l; } // 10bbbbbb ?
case 3: i++; if ((i<l)&&(b[i]& 0xC0) != 0x80) { bestGuess = char_encoding_legacy; i = l; } // 10bbbbbb ?
case 2: i++; if ((i<l)&&(b[i]& 0xC0) != 0x80) { bestGuess = char_encoding_legacy; i = l; } // 10bbbbbb ?
case 1: i++; break;
case 0: i = l;
}
if (!useXMLEncodingAttribute) return bestGuess;
// if encoding is specified and different from utf-8 than it's non-utf8
// otherwise it's utf-8
char bb[201];
l = mmin(l, 200);
memcpy(bb, buf, l); // copy buf into bb to be able to do "bb[l] = 0"
bb[l] = 0;
b = (unsigned char*)strstr(bb, "encoding");
if (!b) return bestGuess;
b+=8; while XML_isSPACECHAR(*b) b++; if (*b != '=') return bestGuess;
b++; while XML_isSPACECHAR(*b) b++; if ((*b != '\'')&&(*b != '"')) return bestGuess;
b++; while XML_isSPACECHAR(*b) b++;
if ((xstrnicmp((char*)b, "utf-8", 5) == 0) ||
(xstrnicmp((char*)b, "utf8", 4) == 0))
{
if (bestGuess == char_encoding_legacy) return char_encoding_error;
return char_encoding_UTF8;
}
if ((xstrnicmp((char*)b, "shiftjis", 8) == 0) ||
(xstrnicmp((char*)b, "shift-jis", 9) == 0) ||
(xstrnicmp((char*)b, "sjis", 4) == 0)) return char_encoding_ShiftJIS;
if (xstrnicmp((char*)b, "GB2312", 6) == 0) return char_encoding_GB2312;
if (xstrnicmp((char*)b, "Big5", 4) == 0) return char_encoding_Big5;
if (xstrnicmp((char*)b, "GBK", 3) == 0) return char_encoding_GBK;
return char_encoding_legacy;
#endif
}
#undef XML_isSPACECHAR
//////////////////////////////////////////////////////////
// Here starts the base64 conversion functions. //
//////////////////////////////////////////////////////////
static const char base64Fillchar = _CXML('='); // used to mark partial words at the end
// this lookup table defines the base64 encoding
XMLCSTR base64EncodeTable = _CXML("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
// Decode Table gives the index of any valid base64 character in the Base64 table]
// 96: '=' - 97: space char - 98: illegal char - 99: end of string
const unsigned char base64DecodeTable[] = {
99, 98, 98, 98, 98, 98, 98, 98, 98, 97, 97, 98, 98, 97, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, //00 -29
98, 98, 97, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 62, 98, 98, 98, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 98, 98, //30 -59
98, 96, 98, 98, 98, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, //60 -89
25, 98, 98, 98, 98, 98, 98, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, //90 -119
49, 50, 51, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, //120 -149
98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, //150 -179
98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, //180 -209
98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, //210 -239
98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98, 98 //240 -255
};
XMLParserBase64Tool::~XMLParserBase64Tool() { freeBuffer(); }
void XMLParserBase64Tool::freeBuffer() { if (buf) free(buf); buf = NULL; buflen = 0; }
int XMLParserBase64Tool::encodeLength(int inlen, char formatted)
{
unsigned int i = ((inlen-1)/3*4+4+1);
if (formatted) i+=inlen/54;
return i;
}
XMLSTR XMLParserBase64Tool::encode(unsigned char *inbuf, unsigned int inlen, char formatted)
{
int i = encodeLength(inlen, formatted), k = 17, eLen = inlen/3, j;
alloc(i*sizeof(XMLCHAR));
XMLSTR curr = (XMLSTR)buf;
for (i=0;i<eLen;i++)
{
// Copy next three bytes into lower 24 bits of int, paying attention to sign.
j = (inbuf[0]<<16)|(inbuf[1]<<8)|inbuf[2]; inbuf+=3;
// Encode the int into four chars
*(curr++) = base64EncodeTable[ j>>18 ];
*(curr++) = base64EncodeTable[(j>>12)&0x3f];
*(curr++) = base64EncodeTable[(j>> 6)&0x3f];
*(curr++) = base64EncodeTable[(j)&0x3f];
if (formatted) { if (!k) { *(curr++) = _CXML('\n'); k = 18; } k--; }
}
eLen = inlen-eLen*3; // 0 - 2.
if (eLen == 1)
{
*(curr++) = base64EncodeTable[ inbuf[0]>>2 ];
*(curr++) = base64EncodeTable[(inbuf[0]<<4)&0x3F];
*(curr++) = base64Fillchar;
*(curr++) = base64Fillchar;
} else if (eLen == 2)
{
j = (inbuf[0]<<8)|inbuf[1];
*(curr++) = base64EncodeTable[ j>>10 ];
*(curr++) = base64EncodeTable[(j>> 4)&0x3f];
*(curr++) = base64EncodeTable[(j<< 2)&0x3f];
*(curr++) = base64Fillchar;
}
*(curr++) = 0;
return (XMLSTR)buf;
}
unsigned int XMLParserBase64Tool::decodeSize(XMLCSTR data, XMLError *xe)
{
if (!data) return 0;
if (xe) *xe = eXMLErrorNone;
int size = 0;
unsigned char c;
//skip any extra characters (e.g. newlines or spaces)
while (*data)
{
#ifdef _XMLWIDECHAR
if (*data>255) { if (xe) *xe = eXMLErrorBase64DecodeIllegalCharacter; return 0; }
#endif
c = base64DecodeTable[(unsigned char)(*data)];
if (c<97) size++;
else if (c == 98) { if (xe) *xe = eXMLErrorBase64DecodeIllegalCharacter; return 0; }
data++;
}
if (xe&&(size%4 != 0)) *xe = eXMLErrorBase64DataSizeIsNotMultipleOf4;
if (size == 0) return 0;
do { data--; size--; } while (*data == base64Fillchar); size++;
return (unsigned int)((size*3)/4);
}
unsigned char XMLParserBase64Tool::decode(XMLCSTR data, unsigned char *buf, int len, XMLError *xe)
{
if (!data) return 0;
if (xe) *xe = eXMLErrorNone;
int i=0, p = 0;
unsigned char d, c;
for (;;)
{
#ifdef _XMLWIDECHAR
#define BASE64DECODE_READ_NEXT_CHAR(c) \
do { \
if (data[i]>255) { c = 98; break; } \
c = base64DecodeTable[(unsigned char)data[i++]]; \
}while (c == 97); \
if(c == 98) { if(xe)*xe = eXMLErrorBase64DecodeIllegalCharacter; return 0; }
#else
#define BASE64DECODE_READ_NEXT_CHAR(c) \
do { c = base64DecodeTable[(unsigned char)data[i++]]; }while (c == 97); \
if(c == 98) { if(xe)*xe = eXMLErrorBase64DecodeIllegalCharacter; return 0; }
#endif
BASE64DECODE_READ_NEXT_CHAR(c)
if (c == 99) { return 2; }
if (c == 96)
{
if (p == (int)len) return 2;
if (xe) *xe = eXMLErrorBase64DecodeTruncatedData;
return 1;
}
BASE64DECODE_READ_NEXT_CHAR(d)
if ((d == 99) || (d == 96)) { if (xe) *xe = eXMLErrorBase64DecodeTruncatedData; return 1; }
if (p == (int)len) { if (xe) *xe = eXMLErrorBase64DecodeBufferTooSmall; return 0; }
buf[p++] = (unsigned char)((c<<2)|((d>>4)&0x3));
BASE64DECODE_READ_NEXT_CHAR(c)
if (c == 99) { if (xe) *xe = eXMLErrorBase64DecodeTruncatedData; return 1; }
if (p == (int)len)
{
if (c == 96) return 2;
if (xe) *xe = eXMLErrorBase64DecodeBufferTooSmall;
return 0;
}
if (c == 96) { if (xe) *xe = eXMLErrorBase64DecodeTruncatedData; return 1; }
buf[p++] = (unsigned char)(((d<<4)&0xf0)|((c>>2)&0xf));
BASE64DECODE_READ_NEXT_CHAR(d)
if (d == 99) { if (xe) *xe = eXMLErrorBase64DecodeTruncatedData; return 1; }
if (p == (int)len)
{
if (d == 96) return 2;
if (xe) *xe = eXMLErrorBase64DecodeBufferTooSmall;
return 0;
}
if (d == 96) { if (xe) *xe = eXMLErrorBase64DecodeTruncatedData; return 1; }
buf[p++] = (unsigned char)(((c<<6)&0xc0)|d);
}
}
#undef BASE64DECODE_READ_NEXT_CHAR
void XMLParserBase64Tool::alloc(int newsize)
{
if ((!buf)&&(newsize)) { buf = malloc(newsize); buflen = newsize; return; }
if (newsize>buflen) { buf = realloc(buf, newsize); buflen = newsize; }
}
unsigned char *XMLParserBase64Tool::decode(XMLCSTR data, int *outlen, XMLError *xe)
{
if (xe)
*xe = eXMLErrorNone;
if (!data) {
if (outlen)
*outlen = 0;
return (unsigned char*)""; // XXX: check me!
}
unsigned int len = decodeSize(data, xe);
if (outlen)
*outlen = len;
if (!len)
return NULL;
alloc(len+1);
if(!decode(data, (unsigned char*)buf, len, xe))
return NULL;
return (unsigned char*)buf;
}
//////////////////////////////////////////////////////////
// Helpers for external C APIs. //
//////////////////////////////////////////////////////////
XMLNode::XMLNode(HXML h) :
d((XMLNodeDataTag*)h)
{
if (d)
d->ref_count++;
}
void XMLNode::attach(HXML h)
{
d = (XMLNodeDataTag*)h;
}
HXML XMLNode::detach()
{
HXML res = (HXML)d;
d = NULL;
return res;
}
|