[go: up one dir, main page]

Menu

[r19]: / lpi_xml.cpp  Maximize  Restore  History

Download this file

1151 lines (967 with data), 33.5 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 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
/*
Copyright (c) 2005-2007 Lode Vandevenne
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Lode Vandevenne nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "lpi_xml.h"
#include <string>
#include <vector>
#include <sstream>
//parse <-> generate
namespace lpi
{
static const std::string xml_indentation_symbol = " ";
static const std::string xml_newline_symbol = "\n";
////////////////////////////////////////////////////////////////////////////////
///XML Tools
////////////////////////////////////////////////////////////////////////////////
void XML::skipWhiteSpace(const std::string& in, size_t& pos, size_t end)
{
while(pos < end && isWhiteSpace(in[pos])) pos++;
}
void XML::skipToChar(const std::string& in, size_t& pos, size_t end, char c)
{
while(pos < end && in[pos] != c) pos++;
}
bool XML::isWhiteSpace(char c)
{
return (c <= 32);
}
bool XML::isCharacter(const std::string& in, size_t pos, size_t end, char c) //made to have the < size and == test in one function
{
return(pos < end && in[pos] == c);
}
bool XML::isNotCharacter(const std::string& in, size_t pos, size_t end, char c) //made to have the < size and != test in one function
{
return(pos < end && in[pos] != c);
}
void XML::entitify(std::string& out, const std::string& in, size_t pos, size_t end)
{
out.reserve(out.size() + end - pos);
for(size_t i = pos; i < end; i++)
{
if(in[i] == '&') out += "&amp;";
else if(in[i] == '<') out += "&lt;";
else if(in[i] == '>') out += "&gt;";
else if(in[i] == '\'') out += "&apos;";
else if(in[i] == '"') out += "&quot;";
else out += in[i];
}
}
void XML::unentitify(std::string& out, const std::string& in, size_t pos, size_t end)
{
out.reserve(out.size() + end - pos);
for(size_t i = pos; i < end; i++)
{
if(in[i] == '&')
{
if(i > end - 3) break;
if(in[i + 1] == 'a' && in[i + 2] == 'm') { out += "&"; i += 4; }
else if(in[i + 1] == 'l') { out += "<"; i += 3; }
else if(in[i + 1] == 'g') { out += ">"; i += 3; }
else if(in[i + 1] == 'a' && in[i + 2] == 'p') { out += "'"; i += 5; }
else if(in[i + 1] == 'l') { out += "\""; i += 5; }
}
else out += in[i];
}
}
//////////////////////////////////////////////////////////////////////////////
void XML::encodeBase64(const std::vector<unsigned char>& in, std::string& out)
{
const std::string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned long bit24 = 0;
unsigned long sextet[4] = {0, 0, 0, 0};
unsigned long octet[3] = {0, 0, 0};
out.clear();
out.reserve((4 * in.size()) / 3);
for(size_t pos = 0; pos < in.size(); pos += 3)
{
octet[0] = in[pos + 0];
octet[1] = (pos + 1 < in.size()) ? in[pos + 1] : 0;
octet[2] = (pos + 2 < in.size()) ? in[pos + 2] : 0;
bit24 = 256 * 256 * octet[0];
bit24 += 256 * octet[1];
bit24 += octet[2];
sextet[0] = (bit24 / (64 * 64 * 64)) % 64;
sextet[1] = (bit24 / ( 64 * 64)) % 64;
sextet[2] = (bit24 / ( 64)) % 64;
sextet[3] = (bit24 / ( 1)) % 64;
for(size_t i = 0; i < 4; i++)
{
if(pos + i - 1 < in.size()) out.push_back(characters[sextet[i]]);
else out.push_back('=');
}
if(pos % 57 == 0 && pos != 0) out.push_back(10); //newline char every 76 chars (57 = 3/4th of 76)
}
}
void XML::decodeBase64(std::vector<unsigned char>& out, const std::string& in, size_t begin, size_t end)
{
if(end == 0) end = in.size();
size_t size = end - begin;
unsigned long bit24 = 0;
unsigned long sextet[4] = {0, 0, 0, 0};
unsigned long octet[3] = {0, 0, 0};
out.clear();
out.reserve((3 * size) / 4);
for(size_t pos = begin; pos < end - 3; pos += 4)
{
for(size_t i = 0; i < 4; i++)
{
unsigned long c = in[pos + i];
if(c >= 65 && c <= 90) sextet[i] = c - 65;
else if(c >= 97 && c <= 122) sextet[i] = c - 71;
else if(c >= 48 && c <= 57) sextet[i] = c + 4;
else if(c == '+') sextet[i] = 62;
else if(c == '/') sextet[i] = 63;
else if(c == '=') sextet[i] = 0; //value doesn't matter
else //unknown char, can be whitespace, newline, ...
{
pos++;
if(pos > end - 3) return;
i--;
}
}
bit24 = 64 * 64 * 64 * sextet[0];
bit24 += 64 * 64 * sextet[1];
bit24 += 64 * sextet[2];
bit24 += sextet[3];
octet[0] = (bit24 / (256 * 256)) % 256;
octet[1] = (bit24 / ( 256)) % 256;
octet[2] = (bit24 / ( 1)) % 256;
for(size_t i = 0; i < 3; i++)
{
if(in[pos + 1 + i] != '=') out.push_back(octet[i]);
}
}
}
//conversions between C++ and strings for xml
//C++ to XML (convert)
void XML::convert(std::string& out, bool in) //bool -> boolean
{
out += (in ? "true" : "false");
}
void XML::convert(std::string& out, char in) //char -> byte
{
std::stringstream ss;
ss << int(in);
out += ss.str();
}
void XML::convert(std::string& out, int in) //int -> int
{
std::stringstream ss;
ss << in;
out += ss.str();
}
void XML::convert(std::string& out, short in) //short -> short
{
std::stringstream ss;
ss << in;
out += ss.str();
}
void XML::convert(std::string& out, long in) //long -> long
{
std::stringstream ss;
ss << in;
out += ss.str();
}
void XML::convert(std::string& out, unsigned char in) //unsigned char -> unsignedByte
{
std::stringstream ss;
ss << int(in);
out += ss.str();
}
void XML::convert(std::string& out, unsigned short in) //unsigned short -> unsignedShort
{
std::stringstream ss;
ss << in;
out += ss.str();
}
void XML::convert(std::string& out, unsigned int in) //unsigned int -> unsignedInt
{
std::stringstream ss;
ss << in;
out += ss.str();
}
void XML::convert(std::string& out, unsigned long in) //unsigned long -> unsignedLong
{
std::stringstream ss;
ss << in;
out += ss.str();
}
void XML::convert(std::string& out, float in) //float -> float
{
std::stringstream ss;
ss << in;
out += ss.str();
}
void XML::convert(std::string& out, double in) //double -> double
{
std::stringstream ss;
ss << in;
out += ss.str();
}
void XML::convert(std::string& out, const void* in) //NOTE: currently not safe if written by 64-bit PC and read by 32-bit PC
{
size_t address = (size_t)in;
std::stringstream ss;
ss << address;
out += ss.str();
}
void XML::convert(std::string& out, void* in) //NOTE: currently not safe if written by 64-bit PC and read by 32-bit PC
{
size_t address = (size_t)in;
std::stringstream ss;
ss << address;
out += ss.str();
}
void XML::convert(std::string& out, const std::string& in) //std::string -> string
{
entitify(out, in, 0, in.size()); //we generate a string in XML, so turn &, <, >, ' and " into entities
}
void XML::convert(std::string& out, const std::vector<unsigned char>& in) //std::vector<unsigned char> -> base64Binary
{
encodeBase64(in, out);
}
//XML to C++ (unconvert)
void XML::unconvert(bool& out, const std::string& in, size_t pos, size_t end)
{
std::string s(in, pos, end - pos);
out = (s == "true");
}
void XML::unconvert(char& out, const std::string& in, size_t pos, size_t end)
{
std::string s(in, pos, end - pos);
std::stringstream ss(s);
int i;
ss >> i;
out = i;
}
void XML::unconvert(short& out, const std::string& in, size_t pos, size_t end)
{
std::string s(in, pos, end - pos);
std::stringstream ss(s);
ss >> out;
}
void XML::unconvert(int& out, const std::string& in, size_t pos, size_t end)
{
std::string s(in, pos, end - pos);
std::stringstream ss(s);
ss >> out;
}
void XML::unconvert(long& out, const std::string& in, size_t pos, size_t end)
{
std::string s(in, pos, end - pos);
std::stringstream ss(s);
ss >> out;
}
void XML::unconvert(unsigned char& out, const std::string& in, size_t pos, size_t end)
{
std::string s(in, pos, end - pos);
std::stringstream ss(s);
unsigned int i;
ss >> i;
out = i;
}
void XML::unconvert(unsigned short& out, const std::string& in, size_t pos, size_t end)
{
std::string s(in, pos, end - pos);
std::stringstream ss(s);
ss >> out;
}
void XML::unconvert(unsigned int& out, const std::string& in, size_t pos, size_t end)
{
std::string s(in, pos, end - pos);
std::stringstream ss(s);
ss >> out;
}
void XML::unconvert(unsigned long& out, const std::string& in, size_t pos, size_t end)
{
std::string s(in, pos, end - pos);
std::stringstream ss(s);
ss >> out;
}
void XML::unconvert(float& out, const std::string& in, size_t pos, size_t end)
{
std::string s(in, pos, end - pos);
std::stringstream ss(s);
ss >> out;
}
void XML::unconvert(double& out, const std::string& in, size_t pos, size_t end)
{
std::string s(in, pos, end - pos);
std::stringstream ss(s);
ss >> out;
}
void XML::unconvert(const void*& out, const std::string& in, size_t pos, size_t end) //NOTE: currently not safe if written by 64-bit PC and read by 32-bit PC
{
std::string s(in, pos, end - pos);
std::stringstream ss(s);
size_t address;
ss >> address;
out = (void*)address;
}
void XML::unconvert(void*& out, const std::string& in, size_t pos, size_t end) //NOTE: currently not safe if written by 64-bit PC and read by 32-bit PC
{
std::string s(in, pos, end - pos);
std::stringstream ss(s);
size_t address;
ss >> address;
out = (void*)address;
}
void XML::unconvert(std::string& out, const std::string& in, size_t pos, size_t end)
{
out.clear();
unentitify(out, in, pos, end); //we parse a string from XML, so turn entities into &, <, >, ' or "
}
void XML::unconvert(std::vector<unsigned char>& out, const std::string& in, size_t pos, size_t end)
{
out.clear();
decodeBase64(out, in, pos, end); //we parse a string from XML, so turn entities into &, <, >, ' or "
}
void XML::skipToNonCommentTag(const std::string& in, size_t& pos, size_t end) //call when you want to go to a '<' that is not of a comment tag. After calling this function, pos is on top of the first non-comment '<'. This function can throw.
{
bool tag_found = false; //comments and <?...?> tags are not counted as tags
size_t ltpos = pos; //pos of the last '<' character
while(!tag_found)
{
skipToChar(in, pos, end, '<');
ltpos = pos;
pos++;
skipWhiteSpace(in, pos, end);
//if there is an exclamation mark (!), it's a comment tag. Also attempt to follow the rules about "--" here.
if(isCharacter(in, pos, end, '!'))
{
int num_minusses = 0; //how many "--" symbols (-- counts as 1, not 2)
for(;;)
{
pos++;
if(pos >= end) throw Error(20, pos); //document ends in the middle of a comment...
if(in[pos] == '>') if(num_minusses % 2 == 0) { pos++; break; } //only if a multiple of 4 minusses occured, a '>' is the end tag symbol
if(in[pos] == '-' && pos < end && in[pos + 1] == '-') num_minusses++;
}
}
//if there is a question mark (?), it's an xml start tag at the top. Ignore it, skip to the matching ?>, and try again.
else if(isCharacter(in, pos, end, '?'))
{
pos++;
skipToChar(in, pos, end, '>');
pos++;
}
else tag_found = true;
}
pos = ltpos; //this is so that whoever calls skipToNonCommentTag has pos on the expected position: on top of the '<'
}
int XML::parseTag(const std::string& in, size_t& pos, size_t end, std::string& name, ParseTagPos& parsepos) //the string is empty if it was a singleton tag or an empty tag (you can't see the difference)
{
skipWhiteSpace(in, pos, end);
skipToNonCommentTag(in, pos, end); //go to on top of next '<' symbol that is not of a comment tag
if(pos >= end) return END;
name.clear();
parsepos.tb = pos;
pos++;
skipWhiteSpace(in, pos, end);
//parse the name of the tag
while(pos < end && !isWhiteSpace(in[pos]) && in[pos] != '>' && in[pos] != '/')
{
name += in[pos];
pos++;
}
skipWhiteSpace(in, pos, end);
parsepos.ab = pos;
while(pos < end && in[pos] != '>' && in[pos] != '/') pos++; //while there are attributes
parsepos.ae = pos;
if(pos < end && in[pos] == '/') //it's a singleton tag
{
pos++;
skipWhiteSpace(in, pos, end);
parsepos.cb = parsepos.ce = pos;
if(isCharacter(in, pos, end, '>'))
{
pos++; //bring pos to after what we just parsed
parsepos.te = pos;
return SUCCESS;
}
else throw Error(11, pos);
}
else if(!(pos < end && in[pos] == '>')) throw Error(12, pos);
//it's not a singleton tag, now parse the content
//there must be kept parseing until tagcount is 1 and you reach an end tag (making tagcount 0). Then check if the end tag has the same text as the start tag, that must be to be so correct XML.
int tagcount = 1; //1 already because our current tag itself is also counted
bool done = false;
pos++;
parsepos.cb = pos; //content begin
while(pos < end && !done)
{
skipToNonCommentTag(in, pos, end); //go to after next '<' symbol that is not of a comment tag
size_t temp_pos = pos;
pos++;
skipWhiteSpace(in, pos, end);
if(isCharacter(in, pos, end, '/')) //it's an end tag
{
pos++;
tagcount--;
if(tagcount < 1) //matching end tag reached
{
std::string end_tag_name;
skipWhiteSpace(in, pos, end);
while(pos < end && !isWhiteSpace(in[pos]) && in[pos] != '>' && in[pos] != '/')
{
end_tag_name += in[pos];
pos++;
}
skipWhiteSpace(in, pos, end);
if(isNotCharacter(in, pos, end, '>')) throw Error(13, pos);
if(end_tag_name != name) throw Error(14, pos);
pos++; //place the position after the end tag
parsepos.te = pos; //tag end
//now we know where the content start and ends, so parse the content
parsepos.ce = temp_pos; //content end
return SUCCESS;
}
}
else //it's a singleton tag or start tag
{
while(pos < end && in[pos] != '>' && in[pos] != '/') pos++;
if(isCharacter(in, pos, end, '/')) //it's a singleton tag
{
pos++;
skipWhiteSpace(in, pos, end);
if(isNotCharacter(in, pos, end, '>')) throw Error(15, pos);
pos++;
}
else if(isCharacter(in, pos, end, '>'))//it's a start tag
{
tagcount++;
pos++;
}
}
}
throw Error(16, pos); //if you got here, something strange must have happened
}
int XML::parseAttribute(const std::string& in, size_t& pos, size_t end, std::string& name, ParseAttributePos& parsepos)
{
skipWhiteSpace(in, pos, end);
if(pos >= end || in[pos] == '>' || in[pos] == '/') return END;
name.clear();
parsepos.cb = parsepos.ce = pos;
//parse name
while(!isWhiteSpace(in[pos]) && isNotCharacter(in, pos, end, '='))
{
name.push_back(in[pos]);
pos++;
}
skipWhiteSpace(in, pos, end);
if(in[pos] != '=') throw Error(17, pos);
pos++;
skipWhiteSpace(in, pos, end);
if(isNotCharacter(in, pos, end, '\'') && isNotCharacter(in, pos, end, '"')) throw Error(18, pos); //the value must be in quotes
pos++;
//parse the value
parsepos.cb = pos; //value begin
while(isNotCharacter(in, pos, end, '\'') && isNotCharacter(in, pos, end, '"')) pos++;
parsepos.ce = pos; //value end
pos++; //go behind the "
return SUCCESS;
}
int XML::getLineNumber(const std::string& fulltext, size_t pos)
{
/*
getLineNumber can be used for showing error messages: if something returns "ERROR", use this with current pos
works with ascii 10 as newline symbol
*/
int line = 1;
for(size_t i = 0; i < pos; i++) if(fulltext[i] == 10) line++;
return line;
}
bool XML::isNested(const std::string& in, size_t pos, size_t end) //checks if the given content is either a value, or nested tag(s) (combinations are not supported!)
{
while(pos < end)
{
skipWhiteSpace(in, pos, end);
if(pos >= end) return false;
if(in[pos] == '<') return true;
else return false; //if it doesn't begin with a <, it's no tag
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
///XMLCompose (without tree)
////////////////////////////////////////////////////////////////////////////////
void XMLCompose::RefRes::addPair(const void* old, void* newa)
{
pointers[old] = newa;
}
void XMLCompose::RefRes::addClient(void** client, void* address)
{
*client = address;
clients.push_back(client);
}
void XMLCompose::RefRes::resolve() //store the new values in all the clients
{
for(size_t i = 0; i < clients.size(); i++)
{
const void* old = *(clients[i]);
*(clients[i]) = pointers[old];
}
}
void* XMLCompose::getAddress()
{
return static_cast<void*>(this);
}
const void* XMLCompose::getAddress() const
{
return dynamic_cast<const void*>(this);
}
int XMLCompose::parse(const std::string& in) //To this function you have to give the complete xml string, including the name of this tag, even though this name will be ignored. It'll parse the attributes and content.
{
size_t pos = 0;
return parse(in, pos, in.size());
}
int XMLCompose::parse(const std::string& in, size_t& pos)
{
return parse(in, pos, in.size());
}
int XMLCompose::parse(const std::string& in, size_t& pos, size_t end)
{
try
{
ParseTagPos parsepos;
std::string name_dummy; //value will be ignored...
parseTag(in, pos, end, name_dummy, parsepos);
RefRes ref;
parse(in, parsepos.ab, parsepos.ae, parsepos.cb, parsepos.ce, ref);
ref.resolve();
}
catch(Error error)
{
pos = error.pos;
return error.error;
}
return SUCCESS;
}
void XMLCompose::parse(const std::string& in, size_t ab, size_t ae, size_t cb, size_t ce, RefRes& ref)
{
doParseAttributes(in, ab, ae, ref);
doParseContent(in, cb, ce, ref);
}
void XMLCompose::generate(std::string& out, const std::string& name) const
{
generate(out, name, 0);
}
void XMLCompose::generate(std::string& out, const std::string& name, size_t indent) const
{
for(size_t i = 0; i < indent; i++) out += xml_indentation_symbol;
indent++;
out += "<";
out += name;
doGenerateAttributes(out);
size_t size_before_adding_tag_close_symbol = out.size(); //you can't know for sure how much characters the newline symbol will be
out += ">";
out += xml_newline_symbol;
size_t size_before_adding_content = out.size();
doGenerateContent(out, indent);
size_t size_after_adding_content = out.size();
indent--;
if(size_before_adding_content == size_after_adding_content) //make this a singleton tag
{
out.resize(size_before_adding_tag_close_symbol); //remove the newline and >, to put a singleton tag end instead
out += "/>";
}
else
{
for(size_t i = 0; i < indent; i++) out += xml_indentation_symbol;
out += "</";
out += name;
out += ">";
}
out += xml_newline_symbol;
}
//if the content string is empty, it'll generate a singleton tag. If the attributes string is not empty it'll generate the attributes in the tag WITHOUT a space between name and the attributes. The attributes themselves must be correct with quotes already (create them with generateAttribute to get them correct) and with a space in front of each attribute name. The generate function will NOT convert anything to entities, you must manually call functions on a string to convert that part to entities. This because the content can contain other tags, which have all these symbols and may not be broken.
void XMLCompose::generateTagString(std::string& out, const std::string& name, const std::string& content, size_t indent)
{
for(size_t i = 0; i < indent; i++) out += xml_indentation_symbol;
out += "<";
out += name;
//out += attributes;
if(content.size() == 0) //empty content, make singleton tag
{
out += "/>";
}
else
{
out += ">";
out += content;
out += "</";
out += name;
out += ">";
}
out += xml_newline_symbol;
}
void XMLCompose::generateAttributeString(std::string& out, const std::string& name, const std::string& value)
{
std::string value_string;
convert(value_string, value);
out += " ";
out += name;
out += "=";
out += '"';
out += value_string;
out += '"';
}
void XMLCompose::generateTag(std::string& out, const std::string& name, const XMLCompose& value, size_t indent)
{
value.generate(out, name, indent);
}
void XMLCompose::generateTag(std::string& out, const std::string& name, const XMLCompose* value, size_t indent)
{
value->generate(out, name, indent);
}
////////////////////////////////////////////////////////////////////////////////
///XMLTree (stores result in tree)
////////////////////////////////////////////////////////////////////////////////
XMLTree::XMLTree()
{
parent = 0;
}
XMLTree::~XMLTree()
{
for(size_t i = 0; i < children.size(); i++)
{
delete children[i];
}
}
size_t XMLTree::getLevel() const //depth in the tree
{
if(parent) return parent->getLevel() + 1;
else return 0;
}
int XMLTree::parse(const std::string& in)
{
size_t pos = 0;
return parse(in, pos, in.size());
}
int XMLTree::parse(const std::string& in, size_t& pos)
{
return parse(in, pos, in.size());
}
int XMLTree::parse(const std::string& in, size_t& pos, size_t end)
{
try
{
ParseTagPos parsepos;
std::string tagname;
parseTag(in, pos, end, tagname, parsepos);
content.name = tagname;
if(parsepos.ae > parsepos.ab) //if there are attributes
{
//parse attributes
ParseAttributePos attrpos;
std::string attrname;
size_t posa = parsepos.ab;
while(parseAttribute(in, posa, parsepos.ae, attrname, attrpos) == SUCCESS)
{
attributes.resize(attributes.size() + 1);
attributes.back().name = attrname;
attributes.back().value = in.substr(attrpos.cb, attrpos.ce - attrpos.cb);
}
}
bool nest = isNested(in, parsepos.cb, parsepos.ce);
if(nest)
{
size_t subpos = parsepos.cb;
ParseTagPos subparsepos;
while(parseTag(in, subpos, parsepos.ce, tagname, subparsepos) == SUCCESS)
{
children.push_back(new XMLTree);
children.back()->parent = this;
children.back()->parse(in, subparsepos.tb, subparsepos.te);
}
}
else //it's a value
{
content.value = in.substr(parsepos.cb, parsepos.ce - parsepos.cb);
}
}
catch(Error error)
{
pos = error.pos;
return error.error;
}
return SUCCESS;
}
bool XMLTree::isEmptyValueTag() const //returns true if it has no children and the value is empty
{
return children.empty() && content.value.empty();
}
bool XMLTree::isValueTag() const //returns true if this has no nested tags in it (no children)
{
return children.empty();
}
void XMLTree::generate(std::string& out) const //appends to the string
{
size_t level = getLevel();
for(size_t i = 0; i < level; i++) out += xml_indentation_symbol;
out += "<" + content.name;
for(size_t i = 0; i < attributes.size(); i++)
{
out += " " + attributes[i].name + "=\"" + attributes[i].value + "\"";
}
if(isEmptyValueTag())
{
out += "/>";
}
else if(isValueTag())
{
out += ">";
out += content.value;
out += "</" + content.name + ">";
}
else //nested tag
{
out += ">";
out += xml_newline_symbol;
for(size_t i = 0; i < children.size(); i++)
{
children[i]->generate(out);
}
for(size_t i = 0; i < level; i++) out += xml_indentation_symbol;
out += "</" + content.name + ">";
}
out += xml_newline_symbol;
}
} //end of namespace lpi
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
///Tests and examples of lpi_xml
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#if 0 //LPI_XML_TEST
int main(){}
#include <iostream>
using namespace lpi;
namespace xmltest
{
////////////////////////////////////////////////////////////////////////////////
///Test and example of XMLCompose
////////////////////////////////////////////////////////////////////////////////
class MonsterType : public XMLCompose
{
public:
double constitution;
double strength;
std::string name; //for the example, name will be saved as attributes instead of value tags
virtual void doParseContent(const std::string& in, size_t& pos, size_t end, RefRes& ref)
{
ParseTagPos parsepos;
std::string tagname;
const void* old_address = 0;
while(parseTag(in, pos, end, tagname, parsepos) == SUCCESS)
{
if(tagname == "constitution") unconvert(constitution, in, parsepos.cb, parsepos.ce);
else if(tagname == "strength") unconvert(strength, in, parsepos.cb, parsepos.ce);
else if(tagname == "id") unconvert(old_address, in, parsepos.cb, parsepos.ce);
}
ref.lastold = old_address;
}
virtual void doParseAttributes(const std::string& in, size_t& pos, size_t end, RefRes& /*ref*/)
{
ParseAttributePos parsepos;
std::string tagname;
while(parseAttribute(in, pos, end, tagname, parsepos) == SUCCESS)
{
if(tagname == "name") unconvert(name, in, parsepos.cb, parsepos.ce);
}
}
virtual void doGenerateAttributes(std::string& out) const
{
generateAttribute(out, "name", name);
}
virtual void doGenerateContent(std::string& out, size_t indent) const
{
generateValueTag(out, "id", getAddress(), indent);
generateValueTag(out, "constitution", constitution, indent);
generateValueTag(out, "strength", strength, indent);
}
};
class Vector2 : public XMLCompose
{
public:
double x;
double y;
virtual void doParseContent(const std::string& in, size_t& pos, size_t end, RefRes& /*ref*/)
{
ParseTagPos parsepos;
std::string tagname;
while(parseTag(in, pos, end, tagname, parsepos) == SUCCESS)
{
if(tagname == "x") unconvert(x, in, parsepos.cb, parsepos.ce);
else if(tagname == "y") unconvert(y, in, parsepos.cb, parsepos.ce);
}
}
virtual void doGenerateContent(std::string& out, size_t indent) const
{
generateValueTag(out, "x", x, indent);
generateValueTag(out, "y", y, indent);
}
};
class Monster : public XMLCompose
{
public:
double health;
MonsterType* type;
Vector2 position;
Vector2 velocity;
virtual void doParseContent(const std::string& in, size_t& pos, size_t end, RefRes& ref)
{
ParseTagPos parsepos;
std::string tagname;
while(parseTag(in, pos, end, tagname, parsepos) == SUCCESS)
{
if(tagname == "health") unconvert(health, in, parsepos.cb, parsepos.ce);
else if(tagname == "position") position.parse(in, parsepos.ab, parsepos.ae, parsepos.cb, parsepos.ce, ref);
else if(tagname == "type")
{
void* address;
unconvert(address, in, parsepos.cb, parsepos.ce);
ref.addClient((void**)(&type), address);
}
}
}
virtual void doGenerateContent(std::string& out, size_t indent) const
{
generateValueTag(out, "type", type->getAddress(), indent);
generateValueTag(out, "health", health, indent);
generateTag(out, "position", position, indent);
}
};
class World : public XMLCompose
{
public:
std::vector<MonsterType*> monstertypes;
std::vector<Monster*> monsters;
int sizex;
int sizey;
virtual void doParseContent(const std::string& in, size_t& pos, size_t end, RefRes& ref)
{
deletify(monstertypes);
deletify(monsters);
ParseTagPos parsepos;
std::string tagname;
while(parseTag(in, pos, end, tagname, parsepos) == SUCCESS)
{
if(tagname == "monster")
{
monsters.push_back(new Monster);
monsters.back()->parse(in, parsepos.ab, parsepos.ae, parsepos.cb, parsepos.ce, ref);
}
else if(tagname == "monstertype")
{
monstertypes.push_back(new MonsterType);
monstertypes.back()->parse(in, parsepos.ab, parsepos.ae, parsepos.cb, parsepos.ce, ref);
ref.addPair(ref.lastold, monstertypes.back()->getAddress());
}
else if(tagname == "sizex") unconvert(sizex, in, parsepos.cb, parsepos.ce);
else if(tagname == "sizey") unconvert(sizey, in, parsepos.cb, parsepos.ce);
}
}
virtual void doGenerateContent(std::string& out, size_t indent) const
{
generateValueTag(out, "sizex", sizex, indent);
generateValueTag(out, "sizey", sizey, indent);
for(size_t i = 0; i < monstertypes.size(); i++)
{
generateTag(out, "monstertype", monstertypes[i], indent); //or alternatively you can use "monstertypes[i]->generate(out, "monstertype", indent);"
}
for(size_t i = 0; i < monsters.size(); i++)
{
generateTag(out, "monster", monsters[i], indent); //or alternatively you can use "monsters[i]->generate(out, "monster", indent);"
}
}
};
void xmltest()
{
std::string xml_in = "<world>\n\
<sizex>16</sizex>\n\
<sizey>16</sizey>\n\
<!--comment-->\n\
<monstertype name='rat'>\n\
<!--comment with extra '>' symbol in it-->\n\
<id>0</id>\n\
<constitution>10</constitution>\n\
<strength>2</strength>\n\
</monstertype>\n\
<monstertype name='dragon'>\n\
<id>1</id>\n\
<constitution>2000</constitution>\n\
<strength>85</strength>\n\
<!>\n\
</monstertype>\n\
<monster>\n\
<type>1</type>\n\
<health>8</health>\n\
<position>\n\
<x>15.1</x>\n\
<y>8.3</y>\n\
</position>\n\
</monster>\n\
<monster>\n\
<type>0</type>\n\
<health>8</health>\n\
<position>\n\
<x>2.0</x>\n\
<y>0.0</y>\n\
</position>\n\
</monster>\n\
<monster>\n\
<type>0</type>\n\
<health>8</health>\n\
<position>\n\
<x>10.1</x>\n\
<y>9.6</y>\n\
</position>\n\
</monster>\n\
</world>";
World world;
size_t pos = 0; //allows displaying line number of error message, if any
int result = world.parse(xml_in, pos, xml_in.size());
if(XML::isError(result))
std::cout << "parsing error " << result << " on line " << XML::getLineNumber(xml_in, pos) << std::endl;
std::string xml_out;
world.generate(xml_out, "world");
std::cout << xml_out; //after generating the xml again, output it again, it should be the same as the input (except the whitespace, which is indented in a fixed way)
std::cout << std::endl;
std::cout << world.monsters[0]->type->name << std::endl; //if this segfaults, the RefRes system doesn't work correctly, or there aren't enough monsters / no correct types&id's in the xml code
}
////////////////////////////////////////////////////////////////////////////////
///Test and example of XMLTree
////////////////////////////////////////////////////////////////////////////////
void xmltreetest()
{
std::string xml = "<a><b x='y'>c</b><d><e/></d><f/><g><h>i</h><j><k>l</k></j></g></a>";
XMLTree tree;
tree.parse(xml);
std::string result;
tree.generate(result);
std::cout << std::endl << result<< std::endl;
}
struct TEST
{
TEST()
{
xmltest();
std::cout << std::endl << std::endl;
xmltreetest();
}
} test;
} //end of namespace xmltest
#endif