1
0

tinyxml2.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #include "tinyxml2.h"
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. using namespace tinyxml2;
  7. static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
  8. static const char LF = LINE_FEED;
  9. static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
  10. static const char CR = CARRIAGE_RETURN;
  11. static const char SINGLE_QUOTE = '\'';
  12. static const char DOUBLE_QUOTE = '\"';
  13. // --------- CharBuffer ----------- //
  14. /*static*/ CharBuffer* CharBuffer::Construct( const char* in )
  15. {
  16. size_t len = strlen( in );
  17. size_t size = len + sizeof( CharBuffer );
  18. CharBuffer* cb = (CharBuffer*) malloc( size );
  19. cb->length = len;
  20. strcpy( cb->mem, in );
  21. return cb;
  22. }
  23. /*static*/ void CharBuffer::Free( CharBuffer* cb )
  24. {
  25. free( cb );
  26. }
  27. const char* StrPair::GetStr()
  28. {
  29. if ( flags & NEEDS_FLUSH ) {
  30. *end = 0;
  31. if ( flags & ( NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION ) ) {
  32. char* p = start;
  33. char* q = start;
  34. while( p < end ) {
  35. if ( *p == CR ) {
  36. // CR-LF pair becomes LF
  37. // CR alone becomes LF
  38. // LF-CR becomes LF
  39. if ( *(p+1) == LF ) {
  40. p += 2;
  41. }
  42. else {
  43. ++p;
  44. }
  45. *q = LF;
  46. }
  47. else if ( *p == LF ) {
  48. if ( *(p+1) == CR ) {
  49. p += 2;
  50. }
  51. else {
  52. ++p;
  53. }
  54. *q = LF;
  55. }
  56. else {
  57. *q = *p;
  58. ++p;
  59. }
  60. }
  61. }
  62. flags = 0;
  63. }
  64. return start;
  65. }
  66. // --------- XMLBase ----------- //
  67. char* XMLBase::ParseText( char* p, StrPair* pair, const char* endTag )
  68. {
  69. TIXMLASSERT( endTag && *endTag );
  70. char* start = p;
  71. char endChar = *endTag;
  72. int length = strlen( endTag );
  73. // Inner loop of text parsing.
  74. while ( *p ) {
  75. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  76. pair->Set( start, p, StrPair::NEEDS_ENTITY_PROCESSING | StrPair::NEEDS_NEWLINE_NORMALIZATION );
  77. break;
  78. }
  79. }
  80. return p;
  81. }
  82. char* XMLBase::ParseName( char* p, StrPair* pair )
  83. {
  84. char* start = p;
  85. char* nextTag = 0;
  86. start = p;
  87. if ( !start || !(*start) ) {
  88. return 0;
  89. }
  90. if ( !IsAlpha( *p ) ) {
  91. return 0;
  92. }
  93. while( *p && (
  94. IsAlphaNum( (unsigned char) *p )
  95. || *p == '_'
  96. || *p == '-'
  97. || *p == '.'
  98. || *p == ':' ))
  99. {
  100. ++p;
  101. }
  102. if ( p > start ) {
  103. pair->Set( start, p, 0 );
  104. return p;
  105. }
  106. return 0;
  107. }
  108. char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
  109. {
  110. XMLNode* returnNode = 0;
  111. p = XMLNode::SkipWhiteSpace( p );
  112. if( !p || !*p || *p != '<' )
  113. {
  114. return 0;
  115. }
  116. // What is this thing?
  117. // - Elements start with a letter or underscore, but xml is reserved.
  118. // - Comments: <!--
  119. // - Decleration: <?xml
  120. // - Everthing else is unknown to tinyxml.
  121. //
  122. static const char* xmlHeader = { "<?xml" };
  123. static const char* commentHeader = { "<!--" };
  124. static const char* dtdHeader = { "<!" };
  125. static const char* cdataHeader = { "<![CDATA[" };
  126. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  127. static const int xmlHeaderLen = 5;
  128. static const int commentHeaderLen = 4;
  129. static const int dtdHeaderLen = 2;
  130. static const int cdataHeaderLen = 9;
  131. static const int elementHeaderLen = 1;
  132. if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
  133. returnNode = new XMLComment( document );
  134. p += commentHeaderLen;
  135. }
  136. else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
  137. returnNode = new XMLElement( document );
  138. p += elementHeaderLen;
  139. }
  140. else {
  141. TIXMLASSERT( 0 );
  142. }
  143. *node = returnNode;
  144. return p;
  145. }
  146. // --------- XMLNode ----------- //
  147. XMLNode::XMLNode( XMLDocument* doc ) :
  148. document( doc ),
  149. parent( 0 ),
  150. firstChild( 0 ), lastChild( 0 ),
  151. prev( 0 ), next( 0 )
  152. {
  153. }
  154. XMLNode::~XMLNode()
  155. {
  156. //printf( "~XMLNode %x\n", this );
  157. while( firstChild ) {
  158. XMLNode* node = firstChild;
  159. Unlink( node );
  160. delete node;
  161. }
  162. }
  163. void XMLNode::Unlink( XMLNode* child )
  164. {
  165. TIXMLASSERT( child->parent == this );
  166. if ( child == firstChild )
  167. firstChild = firstChild->next;
  168. if ( child == lastChild )
  169. lastChild = lastChild->prev;
  170. if ( child->prev ) {
  171. child->prev->next = child->next;
  172. }
  173. if ( child->next ) {
  174. child->next->prev = child->prev;
  175. }
  176. child->parent = 0;
  177. }
  178. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  179. {
  180. if ( lastChild ) {
  181. TIXMLASSERT( firstChild );
  182. TIXMLASSERT( lastChild->next == 0 );
  183. lastChild->next = addThis;
  184. addThis->prev = lastChild;
  185. lastChild = addThis;
  186. addThis->parent = this;
  187. addThis->next = 0;
  188. }
  189. else {
  190. TIXMLASSERT( firstChild == 0 );
  191. firstChild = lastChild = addThis;
  192. addThis->parent = this;
  193. addThis->prev = 0;
  194. addThis->next = 0;
  195. }
  196. return addThis;
  197. }
  198. void XMLNode::Print( FILE* fp, int depth )
  199. {
  200. for( XMLNode* node = firstChild; node; node=node->next ) {
  201. node->Print( fp, depth );
  202. }
  203. }
  204. void XMLNode::PrintSpace( FILE* fp, int depth )
  205. {
  206. for( int i=0; i<depth; ++i ) {
  207. fprintf( fp, " " );
  208. }
  209. }
  210. // --------- XMLComment ---------- //
  211. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  212. {
  213. }
  214. XMLComment::~XMLComment()
  215. {
  216. //printf( "~XMLComment\n" );
  217. }
  218. void XMLComment::Print( FILE* fp, int depth )
  219. {
  220. XMLNode::Print( fp, depth );
  221. fprintf( fp, "<!--%s-->\n", value );
  222. }
  223. char* XMLComment::ParseDeep( char* p )
  224. {
  225. // Comment parses as text.
  226. return ParseText( p, &value, "-->" );
  227. }
  228. // --------- XMLAttribute ---------- //
  229. char* XMLAttribute::ParseDeep( char* p )
  230. {
  231. char endTag[2] = { *p, 0 };
  232. ++p;
  233. p = ParseText( p, &value, endTag );
  234. if ( value.Empty() ) return 0;
  235. return p;
  236. }
  237. void XMLAttribute::Print( FILE* cfile )
  238. {
  239. fprintf( cfile, "\"%s\"", value );
  240. }
  241. // --------- XMLElement ---------- //
  242. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  243. closing( false ),
  244. rootAttribute( 0 ),
  245. lastAttribute( 0 )
  246. {
  247. }
  248. XMLElement::~XMLElement()
  249. {
  250. //printf( "~XMLElemen %x\n",this );
  251. XMLAttribute* attribute = rootAttribute;
  252. while( attribute ) {
  253. XMLAttribute* next = attribute->next;
  254. delete attribute;
  255. attribute = next;
  256. }
  257. }
  258. char* XMLElement::ParseDeep( char* p )
  259. {
  260. // Read the element name.
  261. p = SkipWhiteSpace( p );
  262. if ( !p ) return 0;
  263. const char* start = p;
  264. // The closing element is the </element> form. It is
  265. // parsed just like a regular element then deleted from
  266. // the DOM.
  267. if ( *p == '/' ) {
  268. closing = true;
  269. ++p;
  270. }
  271. p = ParseName( p, &name );
  272. if ( name.Empty() ) return 0;
  273. // Read the attributes.
  274. while( p ) {
  275. p = SkipWhiteSpace( p );
  276. if ( !p || !(*p) ) {
  277. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
  278. return 0;
  279. }
  280. // attribute.
  281. if ( *p == SINGLE_QUOTE || *p == DOUBLE_QUOTE ) {
  282. XMLAttribute* attrib = new XMLAttribute( this );
  283. p = attrib->ParseDeep( p );
  284. if ( !p ) {
  285. delete attrib;
  286. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
  287. return 0;
  288. }
  289. if ( rootAttribute ) {
  290. TIXMLASSERT( lastAttribute );
  291. lastAttribute->next = attrib;
  292. lastAttribute = attrib;
  293. }
  294. else {
  295. rootAttribute = lastAttribute = attrib;
  296. }
  297. }
  298. // end of the tag
  299. else if ( *p == '/' && *(p+1) == '>' ) {
  300. if ( closing ) {
  301. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  302. return 0;
  303. }
  304. return p+2; // done; sealed element.
  305. }
  306. // end of the tag
  307. else if ( *p == '>' ) {
  308. ++p;
  309. break;
  310. }
  311. else {
  312. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  313. return 0;
  314. }
  315. }
  316. while( p && *p ) {
  317. XMLNode* node = 0;
  318. p = Identify( document, p, &node );
  319. if ( p && node ) {
  320. p = node->ParseDeep( p );
  321. XMLElement* element = node->ToElement();
  322. if ( element && element->Closing() ) {
  323. if ( StringEqual( element->Name(), this->Name() ) ) {
  324. // All good, this is closing tag.
  325. delete node;
  326. }
  327. else {
  328. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  329. delete node;
  330. p = 0;
  331. }
  332. return p;
  333. }
  334. else {
  335. this->InsertEndChild( node );
  336. }
  337. }
  338. }
  339. return 0;
  340. }
  341. void XMLElement::Print( FILE* cfile, int depth )
  342. {
  343. PrintSpace( cfile, depth );
  344. fprintf( cfile, "<%s", Name() );
  345. for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
  346. fprintf( cfile, " " );
  347. attrib->Print( cfile );
  348. }
  349. if ( firstChild ) {
  350. fprintf( cfile, ">/n" );
  351. for( XMLNode* node=firstChild; node; node=node->next ) {
  352. node->Print( cfile, depth+1 );
  353. }
  354. fprintf( cfile, "</%s>", Name() );
  355. }
  356. else {
  357. fprintf( cfile, "/>\n" );
  358. }
  359. }
  360. // --------- XMLDocument ----------- //
  361. XMLDocument::XMLDocument() :
  362. charBuffer( 0 )
  363. {
  364. root = new XMLNode( this );
  365. }
  366. XMLDocument::~XMLDocument()
  367. {
  368. delete root;
  369. delete charBuffer;
  370. }
  371. bool XMLDocument::Parse( const char* p )
  372. {
  373. charBuffer = CharBuffer::Construct( p );
  374. XMLNode* node = 0;
  375. char* q = Identify( this, charBuffer->mem, &node );
  376. if ( node ) {
  377. root->InsertEndChild( node );
  378. node->ParseDeep( q );
  379. return true;
  380. }
  381. return false;
  382. }
  383. void XMLDocument::Print( FILE* fp, int depth )
  384. {
  385. for( XMLNode* node = root->firstChild; node; node=node->next ) {
  386. node->Print( fp, depth );
  387. }
  388. }