tinyxml2.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. ++q;
  60. }
  61. }
  62. }
  63. flags = 0;
  64. }
  65. return start;
  66. }
  67. // --------- XMLBase ----------- //
  68. char* XMLBase::ParseText( char* p, StrPair* pair, const char* endTag )
  69. {
  70. TIXMLASSERT( endTag && *endTag );
  71. char* start = p;
  72. char endChar = *endTag;
  73. int length = strlen( endTag );
  74. // Inner loop of text parsing.
  75. while ( *p ) {
  76. if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
  77. pair->Set( start, p, StrPair::NEEDS_ENTITY_PROCESSING | StrPair::NEEDS_NEWLINE_NORMALIZATION );
  78. return p + length;
  79. }
  80. ++p;
  81. }
  82. return p;
  83. }
  84. char* XMLBase::ParseName( char* p, StrPair* pair )
  85. {
  86. char* start = p;
  87. start = p;
  88. if ( !start || !(*start) ) {
  89. return 0;
  90. }
  91. if ( !IsAlpha( *p ) ) {
  92. return 0;
  93. }
  94. while( *p && (
  95. IsAlphaNum( (unsigned char) *p )
  96. || *p == '_'
  97. || *p == '-'
  98. || *p == '.'
  99. || *p == ':' ))
  100. {
  101. ++p;
  102. }
  103. if ( p > start ) {
  104. pair->Set( start, p, 0 );
  105. return p;
  106. }
  107. return 0;
  108. }
  109. char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
  110. {
  111. XMLNode* returnNode = 0;
  112. char* start = p;
  113. p = XMLNode::SkipWhiteSpace( p );
  114. if( !p || !*p )
  115. {
  116. return 0;
  117. }
  118. // What is this thing?
  119. // - Elements start with a letter or underscore, but xml is reserved.
  120. // - Comments: <!--
  121. // - Decleration: <?xml
  122. // - Everthing else is unknown to tinyxml.
  123. //
  124. static const char* xmlHeader = { "<?xml" };
  125. static const char* commentHeader = { "<!--" };
  126. static const char* dtdHeader = { "<!" };
  127. static const char* cdataHeader = { "<![CDATA[" };
  128. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  129. static const int xmlHeaderLen = 5;
  130. static const int commentHeaderLen = 4;
  131. static const int dtdHeaderLen = 2;
  132. static const int cdataHeaderLen = 9;
  133. static const int elementHeaderLen = 1;
  134. if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
  135. returnNode = new XMLComment( document );
  136. p += commentHeaderLen;
  137. }
  138. else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
  139. returnNode = new XMLElement( document );
  140. p += elementHeaderLen;
  141. }
  142. // fixme: better text detection
  143. else if ( (*p != '<') && IsAlphaNum( *p ) ) {
  144. // fixme: this is filtering out empty text...should it?
  145. returnNode = new XMLText( document );
  146. p = start; // Back it up, all the text counts.
  147. }
  148. else {
  149. TIXMLASSERT( 0 );
  150. }
  151. *node = returnNode;
  152. return p;
  153. }
  154. // --------- XMLNode ----------- //
  155. XMLNode::XMLNode( XMLDocument* doc ) :
  156. document( doc ),
  157. parent( 0 ),
  158. isTextParent( false ),
  159. firstChild( 0 ), lastChild( 0 ),
  160. prev( 0 ), next( 0 )
  161. {
  162. }
  163. XMLNode::~XMLNode()
  164. {
  165. //printf( "~XMLNode %x\n", this );
  166. while( firstChild ) {
  167. XMLNode* node = firstChild;
  168. Unlink( node );
  169. delete node;
  170. }
  171. }
  172. void XMLNode::Unlink( XMLNode* child )
  173. {
  174. TIXMLASSERT( child->parent == this );
  175. if ( child == firstChild )
  176. firstChild = firstChild->next;
  177. if ( child == lastChild )
  178. lastChild = lastChild->prev;
  179. if ( child->prev ) {
  180. child->prev->next = child->next;
  181. }
  182. if ( child->next ) {
  183. child->next->prev = child->prev;
  184. }
  185. child->parent = 0;
  186. }
  187. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  188. {
  189. if ( lastChild ) {
  190. TIXMLASSERT( firstChild );
  191. TIXMLASSERT( lastChild->next == 0 );
  192. lastChild->next = addThis;
  193. addThis->prev = lastChild;
  194. lastChild = addThis;
  195. addThis->parent = this;
  196. addThis->next = 0;
  197. }
  198. else {
  199. TIXMLASSERT( firstChild == 0 );
  200. firstChild = lastChild = addThis;
  201. addThis->parent = this;
  202. addThis->prev = 0;
  203. addThis->next = 0;
  204. }
  205. if ( addThis->ToText() ) {
  206. SetTextParent();
  207. }
  208. return addThis;
  209. }
  210. void XMLNode::Print( FILE* fp, int depth )
  211. {
  212. for( XMLNode* node = firstChild; node; node=node->next ) {
  213. node->Print( fp, depth );
  214. }
  215. }
  216. char* XMLNode::ParseDeep( char* p )
  217. {
  218. while( p && *p ) {
  219. XMLNode* node = 0;
  220. p = Identify( document, p, &node );
  221. if ( p && node ) {
  222. p = node->ParseDeep( p );
  223. // FIXME: is it the correct closing element?
  224. if ( node->IsClosingElement() ) {
  225. delete node;
  226. return p;
  227. }
  228. this->InsertEndChild( node );
  229. }
  230. }
  231. return 0;
  232. }
  233. void XMLNode::PrintSpace( FILE* fp, int depth )
  234. {
  235. for( int i=0; i<depth; ++i ) {
  236. fprintf( fp, " " );
  237. }
  238. }
  239. // --------- XMLText ---------- //
  240. char* XMLText::ParseDeep( char* p )
  241. {
  242. p = ParseText( p, &value, "<" );
  243. // consumes the end tag.
  244. if ( p && *p ) {
  245. return p-1;
  246. }
  247. return 0;
  248. }
  249. void XMLText::Print( FILE* cfile, int depth )
  250. {
  251. const char* v = value.GetStr();
  252. fprintf( cfile, v );
  253. }
  254. // --------- XMLComment ---------- //
  255. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  256. {
  257. }
  258. XMLComment::~XMLComment()
  259. {
  260. //printf( "~XMLComment\n" );
  261. }
  262. void XMLComment::Print( FILE* fp, int depth )
  263. {
  264. XMLNode::Print( fp, depth );
  265. fprintf( fp, "<!--%s-->\n", value.GetStr() );
  266. }
  267. char* XMLComment::ParseDeep( char* p )
  268. {
  269. // Comment parses as text.
  270. return ParseText( p, &value, "-->" );
  271. }
  272. // --------- XMLAttribute ---------- //
  273. char* XMLAttribute::ParseDeep( char* p )
  274. {
  275. p = ParseText( p, &name, "=" );
  276. if ( !p || !*p ) return 0;
  277. char endTag[2] = { *p, 0 };
  278. ++p;
  279. p = ParseText( p, &value, endTag );
  280. if ( value.Empty() ) return 0;
  281. return p;
  282. }
  283. void XMLAttribute::Print( FILE* cfile )
  284. {
  285. // fixme: sort out single vs. double quote
  286. fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
  287. }
  288. // --------- XMLElement ---------- //
  289. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  290. closing( false ),
  291. rootAttribute( 0 ),
  292. lastAttribute( 0 )
  293. {
  294. }
  295. XMLElement::~XMLElement()
  296. {
  297. //printf( "~XMLElemen %x\n",this );
  298. XMLAttribute* attribute = rootAttribute;
  299. while( attribute ) {
  300. XMLAttribute* next = attribute->next;
  301. delete attribute;
  302. attribute = next;
  303. }
  304. }
  305. char* XMLElement::ParseAttributes( char* p, bool* closedElement )
  306. {
  307. const char* start = p;
  308. *closedElement = false;
  309. // Read the attributes.
  310. while( p ) {
  311. p = SkipWhiteSpace( p );
  312. if ( !p || !(*p) ) {
  313. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
  314. return 0;
  315. }
  316. // attribute.
  317. if ( IsAlpha( *p ) ) {
  318. XMLAttribute* attrib = new XMLAttribute( this );
  319. p = attrib->ParseDeep( p );
  320. if ( !p ) {
  321. delete attrib;
  322. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
  323. return 0;
  324. }
  325. if ( rootAttribute ) {
  326. TIXMLASSERT( lastAttribute );
  327. lastAttribute->next = attrib;
  328. lastAttribute = attrib;
  329. }
  330. else {
  331. rootAttribute = lastAttribute = attrib;
  332. }
  333. }
  334. // end of the tag
  335. else if ( *p == '/' && *(p+1) == '>' ) {
  336. if ( closing ) {
  337. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  338. return 0;
  339. }
  340. *closedElement = true;
  341. return p+2; // done; sealed element.
  342. }
  343. // end of the tag
  344. else if ( *p == '>' ) {
  345. ++p;
  346. break;
  347. }
  348. else {
  349. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  350. return 0;
  351. }
  352. }
  353. return p;
  354. }
  355. //
  356. // <ele></ele>
  357. // <ele>foo<b>bar</b></ele>
  358. //
  359. char* XMLElement::ParseDeep( char* p )
  360. {
  361. // Read the element name.
  362. p = SkipWhiteSpace( p );
  363. if ( !p ) return 0;
  364. const char* start = p;
  365. // The closing element is the </element> form. It is
  366. // parsed just like a regular element then deleted from
  367. // the DOM.
  368. if ( *p == '/' ) {
  369. closing = true;
  370. ++p;
  371. }
  372. p = ParseName( p, &name );
  373. if ( name.Empty() ) return 0;
  374. bool elementClosed=false;
  375. p = ParseAttributes( p, &elementClosed );
  376. if ( !p || !*p || elementClosed || closing )
  377. return p;
  378. p = XMLNode::ParseDeep( p );
  379. return p;
  380. }
  381. void XMLElement::Print( FILE* cfile, int depth )
  382. {
  383. if ( !parent || !parent->IsTextParent() ) {
  384. PrintSpace( cfile, depth );
  385. }
  386. fprintf( cfile, "<%s", Name() );
  387. for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
  388. fprintf( cfile, " " );
  389. attrib->Print( cfile );
  390. }
  391. if ( firstChild ) {
  392. fprintf( cfile, ">", Name() );
  393. if ( !IsTextParent() ) {
  394. fprintf( cfile, "\n" );
  395. }
  396. for( XMLNode* node=firstChild; node; node=node->next ) {
  397. node->Print( cfile, depth+1 );
  398. }
  399. fprintf( cfile, "</%s>", Name() );
  400. if ( !IsTextParent() ) {
  401. fprintf( cfile, "\n" );
  402. }
  403. }
  404. else {
  405. fprintf( cfile, "/>" );
  406. if ( !IsTextParent() ) {
  407. fprintf( cfile, "\n" );
  408. }
  409. }
  410. }
  411. // --------- XMLDocument ----------- //
  412. XMLDocument::XMLDocument() :
  413. XMLNode( this ),
  414. charBuffer( 0 )
  415. {
  416. }
  417. XMLDocument::~XMLDocument()
  418. {
  419. }
  420. bool XMLDocument::Parse( const char* p )
  421. {
  422. charBuffer = CharBuffer::Construct( p );
  423. XMLNode* node = 0;
  424. char* q = ParseDeep( charBuffer->mem );
  425. return true;
  426. }
  427. void XMLDocument::Print( FILE* fp, int depth )
  428. {
  429. for( XMLNode* node = firstChild; node; node=node->next ) {
  430. node->Print( fp, depth );
  431. }
  432. }
  433. void XMLDocument::SetError( int error, const char* str1, const char* str2 )
  434. {
  435. printf( "ERROR: id=%d '%s' '%s'\n", error, str1, str2 );
  436. }