tinyxml2.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. XMLNode* node=firstChild;
  157. while( node ) {
  158. XMLNode* temp = node->next;
  159. delete node;
  160. node = temp;
  161. }
  162. if ( prev ) {
  163. prev->next = next;
  164. }
  165. if ( next ) {
  166. next->prev = prev;
  167. }
  168. }
  169. XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
  170. {
  171. if ( lastChild ) {
  172. TIXMLASSERT( firstChild );
  173. TIXMLASSERT( lastChild->next == 0 );
  174. lastChild->next = addThis;
  175. addThis->prev = lastChild;
  176. lastChild = addThis;
  177. addThis->parent = this;
  178. addThis->next = 0;
  179. }
  180. else {
  181. TIXMLASSERT( firstChild == 0 );
  182. firstChild = lastChild = addThis;
  183. addThis->parent = this;
  184. addThis->prev = 0;
  185. addThis->next = 0;
  186. }
  187. return addThis;
  188. }
  189. void XMLNode::Print( FILE* fp, int depth )
  190. {
  191. for( XMLNode* node = firstChild; node; node=node->next ) {
  192. node->Print( fp, depth );
  193. }
  194. }
  195. void XMLNode::PrintSpace( FILE* fp, int depth )
  196. {
  197. for( int i=0; i<depth; ++i ) {
  198. fprintf( fp, " " );
  199. }
  200. }
  201. // --------- XMLComment ---------- //
  202. XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
  203. {
  204. }
  205. XMLComment::~XMLComment()
  206. {
  207. }
  208. void XMLComment::Print( FILE* fp, int depth )
  209. {
  210. XMLNode::Print( fp, depth );
  211. fprintf( fp, "<!--%s-->\n", value );
  212. }
  213. char* XMLComment::ParseDeep( char* p )
  214. {
  215. // Comment parses as text.
  216. return ParseText( p, &value, "-->" );
  217. }
  218. // --------- XMLAttribute ---------- //
  219. char* XMLAttribute::ParseDeep( char* p )
  220. {
  221. char endTag[2] = { *p, 0 };
  222. ++p;
  223. p = ParseText( p, &value, endTag );
  224. if ( value.Empty() ) return 0;
  225. return p;
  226. }
  227. void XMLAttribute::Print( FILE* cfile )
  228. {
  229. fprintf( cfile, "\"%s\"", value );
  230. }
  231. // --------- XMLElement ---------- //
  232. XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
  233. closing( false ),
  234. rootAttribute( 0 ),
  235. lastAttribute( 0 )
  236. {
  237. }
  238. XMLElement::~XMLElement()
  239. {
  240. XMLAttribute* attribute = rootAttribute;
  241. while( attribute ) {
  242. XMLAttribute* next = attribute->next;
  243. delete attribute;
  244. attribute = next;
  245. }
  246. XMLNode* child = firstChild;
  247. while( child ) {
  248. XMLNode* next = child->next;
  249. delete child;
  250. child = next;
  251. }
  252. }
  253. char* XMLElement::ParseDeep( char* p )
  254. {
  255. // Read the element name.
  256. p = SkipWhiteSpace( p );
  257. if ( !p ) return 0;
  258. const char* start = p;
  259. // The closing element is the </element> form. It is
  260. // parsed just like a regular element then deleted from
  261. // the DOM.
  262. if ( *p == '/' ) {
  263. closing = true;
  264. ++p;
  265. }
  266. p = ParseName( p, &name );
  267. if ( name.Empty() ) return 0;
  268. // Read the attributes.
  269. while( p ) {
  270. p = SkipWhiteSpace( p );
  271. if ( !p || !(*p) ) {
  272. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, name.GetStr() );
  273. return 0;
  274. }
  275. // attribute.
  276. if ( *p == SINGLE_QUOTE || *p == DOUBLE_QUOTE ) {
  277. XMLAttribute* attrib = new XMLAttribute( this );
  278. p = attrib->ParseDeep( p );
  279. if ( !p ) {
  280. delete attrib;
  281. document->SetError( XMLDocument::ERROR_PARSING_ATTRIBUTE, start, p );
  282. return 0;
  283. }
  284. if ( rootAttribute ) {
  285. TIXMLASSERT( lastAttribute );
  286. lastAttribute->next = attrib;
  287. lastAttribute = attrib;
  288. }
  289. else {
  290. rootAttribute = lastAttribute = attrib;
  291. }
  292. }
  293. // end of the tag
  294. else if ( *p == '/' && *(p+1) == '>' ) {
  295. if ( closing ) {
  296. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  297. return 0;
  298. }
  299. return p+2; // done; sealed element.
  300. }
  301. // end of the tag
  302. else if ( *p == '>' ) {
  303. ++p;
  304. break;
  305. }
  306. else {
  307. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  308. return 0;
  309. }
  310. }
  311. while( p && *p ) {
  312. XMLNode* node = 0;
  313. p = Identify( document, p, &node );
  314. if ( p && node ) {
  315. p = node->ParseDeep( p );
  316. XMLElement* element = node->ToElement();
  317. if ( element && element->Closing() ) {
  318. if ( StringEqual( element->Name(), this->Name() ) ) {
  319. // All good, this is closing tag.
  320. delete node;
  321. }
  322. else {
  323. document->SetError( XMLDocument::ERROR_PARSING_ELEMENT, start, p );
  324. delete node;
  325. p = 0;
  326. }
  327. return p;
  328. }
  329. else {
  330. this->InsertEndChild( node );
  331. }
  332. }
  333. }
  334. return 0;
  335. }
  336. void XMLElement::Print( FILE* cfile, int depth )
  337. {
  338. PrintSpace( cfile, depth );
  339. fprintf( cfile, "<%s", Name() );
  340. for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
  341. fprintf( cfile, " " );
  342. attrib->Print( cfile );
  343. }
  344. if ( firstChild ) {
  345. fprintf( cfile, ">/n" );
  346. for( XMLNode* node=firstChild; node; node=node->next ) {
  347. node->Print( cfile, depth+1 );
  348. }
  349. fprintf( cfile, "</%s>", Name() );
  350. }
  351. else {
  352. fprintf( cfile, "/>\n" );
  353. }
  354. }
  355. // --------- XMLDocument ----------- //
  356. XMLDocument::XMLDocument() :
  357. charBuffer( 0 )
  358. {
  359. root = new XMLNode( this );
  360. }
  361. XMLDocument::~XMLDocument()
  362. {
  363. delete root;
  364. delete charBuffer;
  365. }
  366. bool XMLDocument::Parse( const char* p )
  367. {
  368. charBuffer = CharBuffer::Construct( p );
  369. XMLNode* node = 0;
  370. char* q = Identify( this, charBuffer->mem, &node );
  371. if ( node ) {
  372. root->InsertEndChild( node );
  373. node->ParseDeep( q );
  374. return true;
  375. }
  376. return false;
  377. }
  378. void XMLDocument::Print( FILE* fp, int depth )
  379. {
  380. for( XMLNode* node = root->firstChild; node; node=node->next ) {
  381. node->Print( fp, depth );
  382. }
  383. }