tinyxml2.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. p = XMLNode::SkipWhiteSpace( p );
  113. if( !p || !*p || *p != '<' )
  114. {
  115. return 0;
  116. }
  117. // What is this thing?
  118. // - Elements start with a letter or underscore, but xml is reserved.
  119. // - Comments: <!--
  120. // - Decleration: <?xml
  121. // - Everthing else is unknown to tinyxml.
  122. //
  123. static const char* xmlHeader = { "<?xml" };
  124. static const char* commentHeader = { "<!--" };
  125. static const char* dtdHeader = { "<!" };
  126. static const char* cdataHeader = { "<![CDATA[" };
  127. static const char* elementHeader = { "<" }; // and a header for everything else; check last.
  128. static const int xmlHeaderLen = 5;
  129. static const int commentHeaderLen = 4;
  130. static const int dtdHeaderLen = 2;
  131. static const int cdataHeaderLen = 9;
  132. static const int elementHeaderLen = 1;
  133. if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
  134. returnNode = new XMLComment( document );
  135. p += commentHeaderLen;
  136. }
  137. else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
  138. returnNode = new XMLElement( document );
  139. p += elementHeaderLen;
  140. }
  141. else {
  142. TIXMLASSERT( 0 );
  143. }
  144. *node = returnNode;
  145. return p;
  146. }
  147. // --------- XMLNode ----------- //
  148. XMLNode::XMLNode( XMLDocument* doc ) :
  149. document( doc ),
  150. parent( 0 ),
  151. firstChild( 0 ), lastChild( 0 ),
  152. prev( 0 ), next( 0 )
  153. {
  154. }
  155. XMLNode::~XMLNode()
  156. {
  157. //printf( "~XMLNode %x\n", this );
  158. while( firstChild ) {
  159. XMLNode* node = firstChild;
  160. Unlink( node );
  161. delete node;
  162. }
  163. }
  164. void XMLNode::Unlink( XMLNode* child )
  165. {
  166. TIXMLASSERT( child->parent == this );
  167. if ( child == firstChild )
  168. firstChild = firstChild->next;
  169. if ( child == lastChild )
  170. lastChild = lastChild->prev;
  171. if ( child->prev ) {
  172. child->prev->next = child->next;
  173. }
  174. if ( child->next ) {
  175. child->next->prev = child->prev;
  176. }
  177. child->parent = 0;
  178. }
  179. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  180. {
  181. if ( lastChild ) {
  182. TIXMLASSERT( firstChild );
  183. TIXMLASSERT( lastChild->next == 0 );
  184. lastChild->next = addThis;
  185. addThis->prev = lastChild;
  186. lastChild = addThis;
  187. addThis->parent = this;
  188. addThis->next = 0;
  189. }
  190. else {
  191. TIXMLASSERT( firstChild == 0 );
  192. firstChild = lastChild = addThis;
  193. addThis->parent = this;
  194. addThis->prev = 0;
  195. addThis->next = 0;
  196. }
  197. return addThis;
  198. }
  199. void XMLNode::Print( FILE* fp, int depth )
  200. {
  201. for( XMLNode* node = firstChild; node; node=node->next ) {
  202. node->Print( fp, depth );
  203. }
  204. }
  205. void XMLNode::PrintSpace( FILE* fp, int depth )
  206. {
  207. for( int i=0; i<depth; ++i ) {
  208. fprintf( fp, " " );
  209. }
  210. }
  211. // --------- XMLComment ---------- //
  212. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  213. {
  214. }
  215. XMLComment::~XMLComment()
  216. {
  217. //printf( "~XMLComment\n" );
  218. }
  219. void XMLComment::Print( FILE* fp, int depth )
  220. {
  221. XMLNode::Print( fp, depth );
  222. fprintf( fp, "<!--%s-->\n", value.GetStr() );
  223. }
  224. char* XMLComment::ParseDeep( char* p )
  225. {
  226. // Comment parses as text.
  227. return ParseText( p, &value, "-->" );
  228. }
  229. // --------- XMLAttribute ---------- //
  230. char* XMLAttribute::ParseDeep( char* p )
  231. {
  232. p = ParseText( p, &name, "=" );
  233. if ( !p || !*p ) return 0;
  234. char endTag[2] = { *p, 0 };
  235. ++p;
  236. p = ParseText( p, &value, endTag );
  237. if ( value.Empty() ) return 0;
  238. return p;
  239. }
  240. void XMLAttribute::Print( FILE* cfile )
  241. {
  242. // fixme: sort out single vs. double quote
  243. fprintf( cfile, "%s=\"%s\"", name.GetStr(), value.GetStr() );
  244. }
  245. // --------- XMLElement ---------- //
  246. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  247. closing( false ),
  248. rootAttribute( 0 ),
  249. lastAttribute( 0 )
  250. {
  251. }
  252. XMLElement::~XMLElement()
  253. {
  254. //printf( "~XMLElemen %x\n",this );
  255. XMLAttribute* attribute = rootAttribute;
  256. while( attribute ) {
  257. XMLAttribute* next = attribute->next;
  258. delete attribute;
  259. attribute = next;
  260. }
  261. }
  262. char* XMLElement::ParseDeep( char* p )
  263. {
  264. // Read the element name.
  265. p = SkipWhiteSpace( p );
  266. if ( !p ) return 0;
  267. const char* start = p;
  268. // The closing element is the </element> form. It is
  269. // parsed just like a regular element then deleted from
  270. // the DOM.
  271. if ( *p == '/' ) {
  272. closing = true;
  273. ++p;
  274. }
  275. p = ParseName( p, &name );
  276. if ( name.Empty() ) return 0;
  277. // Read the attributes.
  278. while( p ) {
  279. p = SkipWhiteSpace( p );
  280. if ( !p || !(*p) ) {
  281. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
  282. return 0;
  283. }
  284. // attribute.
  285. if ( IsAlpha( *p ) ) {
  286. XMLAttribute* attrib = new XMLAttribute( this );
  287. p = attrib->ParseDeep( p );
  288. if ( !p ) {
  289. delete attrib;
  290. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
  291. return 0;
  292. }
  293. if ( rootAttribute ) {
  294. TIXMLASSERT( lastAttribute );
  295. lastAttribute->next = attrib;
  296. lastAttribute = attrib;
  297. }
  298. else {
  299. rootAttribute = lastAttribute = attrib;
  300. }
  301. }
  302. // end of the tag
  303. else if ( *p == '/' && *(p+1) == '>' ) {
  304. if ( closing ) {
  305. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  306. return 0;
  307. }
  308. return p+2; // done; sealed element.
  309. }
  310. // end of the tag
  311. else if ( *p == '>' ) {
  312. ++p;
  313. break;
  314. }
  315. else {
  316. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  317. return 0;
  318. }
  319. }
  320. while( p && *p ) {
  321. XMLNode* node = 0;
  322. p = Identify( document, p, &node );
  323. if ( p && node ) {
  324. p = node->ParseDeep( p );
  325. XMLElement* element = node->ToElement();
  326. if ( element && element->Closing() ) {
  327. if ( StringEqual( element->Name(), this->Name() ) ) {
  328. // All good, this is closing tag.
  329. delete node;
  330. }
  331. else {
  332. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  333. delete node;
  334. p = 0;
  335. }
  336. return p;
  337. }
  338. else {
  339. this->InsertEndChild( node );
  340. }
  341. }
  342. }
  343. return 0;
  344. }
  345. void XMLElement::Print( FILE* cfile, int depth )
  346. {
  347. PrintSpace( cfile, depth );
  348. fprintf( cfile, "<%s", Name() );
  349. for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
  350. fprintf( cfile, " " );
  351. attrib->Print( cfile );
  352. }
  353. if ( firstChild ) {
  354. fprintf( cfile, ">\n" );
  355. for( XMLNode* node=firstChild; node; node=node->next ) {
  356. node->Print( cfile, depth+1 );
  357. }
  358. fprintf( cfile, "</%s>\n", Name() );
  359. }
  360. else {
  361. fprintf( cfile, "/>\n" );
  362. }
  363. }
  364. // --------- XMLDocument ----------- //
  365. XMLDocument::XMLDocument() :
  366. charBuffer( 0 )
  367. {
  368. root = new XMLNode( this );
  369. }
  370. XMLDocument::~XMLDocument()
  371. {
  372. delete root;
  373. delete charBuffer;
  374. }
  375. bool XMLDocument::Parse( const char* p )
  376. {
  377. charBuffer = CharBuffer::Construct( p );
  378. XMLNode* node = 0;
  379. // fixme: clean up
  380. char* q = Identify( this, charBuffer->mem, &node );
  381. while ( node ) {
  382. root->InsertEndChild( node );
  383. q = node->ParseDeep( q );
  384. node = 0;
  385. if ( q && *q ) {
  386. q = Identify( this, q, &node );
  387. }
  388. }
  389. return false;
  390. }
  391. void XMLDocument::Print( FILE* fp, int depth )
  392. {
  393. for( XMLNode* node = root->firstChild; node; node=node->next ) {
  394. node->Print( fp, depth );
  395. }
  396. }