tinyxml2.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. #ifndef TINYXML_INCLUDED
  2. #define TINYXML2_INCLUDED
  3. /*
  4. TODO
  5. X const and non-const versions of API
  6. X memory pool the class construction
  7. X attribute accessors
  8. X node navigation
  9. - handles
  10. X visit pattern - change streamer?
  11. X make constructors protected
  12. X hide copy constructor
  13. X hide = operator
  14. X UTF8 support: isAlpha, etc.
  15. X string buffer for sets. (Grr.)
  16. - MS BOM
  17. X print to memory buffer
  18. - tests from xml1
  19. - xml1 tests especially UTF-8
  20. - perf test: xml1
  21. - perf test: xenowar
  22. - test: load(char*)
  23. - test: load(FILE*)
  24. - rename declaration
  25. - rename streamer
  26. */
  27. #include <limits.h>
  28. #include <ctype.h>
  29. #include <stdio.h>
  30. #include <memory.h>
  31. #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
  32. #ifndef DEBUG
  33. #define DEBUG
  34. #endif
  35. #endif
  36. #if defined(DEBUG)
  37. #if defined(_MSC_VER)
  38. #define TIXMLASSERT( x ) if ( !(x)) { _asm { int 3 } } //if ( !(x)) WinDebugBreak()
  39. #elif defined (ANDROID_NDK)
  40. #include <android/log.h>
  41. #define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
  42. #else
  43. #include <assert.h>
  44. #define TIXMLASSERT assert
  45. #endif
  46. #else
  47. #define TIXMLASSERT( x ) {}
  48. #endif
  49. // Deprecated library function hell. Compilers want to use the
  50. // new safe versions. This probably doesn't fully address the problem,
  51. // but it gets closer. There are too many compilers for me to fully
  52. // test. If you get compilation troubles, undefine TIXML_SAFE
  53. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  54. // Microsoft visual studio, version 2005 and higher.
  55. #define TIXML_SNPRINTF _snprintf_s
  56. #define TIXML_SSCANF sscanf_s
  57. #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
  58. // Microsoft visual studio, version 6 and higher.
  59. //#pragma message( "Using _sn* functions." )
  60. #define TIXML_SNPRINTF _snprintf
  61. #define TIXML_SSCANF sscanf
  62. #elif defined(__GNUC__) && (__GNUC__ >= 3 )
  63. // GCC version 3 and higher
  64. //#warning( "Using sn* functions." )
  65. #define TIXML_SNPRINTF snprintf
  66. #define TIXML_SSCANF sscanf
  67. #else
  68. #define TIXML_SNPRINTF snprintf
  69. #define TIXML_SSCANF sscanf
  70. #endif
  71. namespace tinyxml2
  72. {
  73. class XMLDocument;
  74. class XMLElement;
  75. class XMLAttribute;
  76. class XMLComment;
  77. class XMLNode;
  78. class XMLText;
  79. class XMLDeclaration;
  80. class XMLUnknown;
  81. class XMLStreamer;
  82. /*
  83. A class that wraps strings. Normally stores the start and end
  84. pointers into the XML file itself, and will apply normalization
  85. and entity transalion if actually read. Can also store (and memory
  86. manage) a traditional char[]
  87. */
  88. class StrPair
  89. {
  90. public:
  91. enum {
  92. NEEDS_ENTITY_PROCESSING = 0x01,
  93. NEEDS_NEWLINE_NORMALIZATION = 0x02,
  94. TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
  95. ATTRIBUTE_NAME = 0,
  96. ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
  97. COMMENT = NEEDS_NEWLINE_NORMALIZATION,
  98. };
  99. StrPair() : flags( 0 ), start( 0 ), end( 0 ) {}
  100. ~StrPair();
  101. void Set( char* start, char* end, int flags ) {
  102. Reset();
  103. this->start = start; this->end = end; this->flags = flags | NEEDS_FLUSH;
  104. }
  105. const char* GetStr();
  106. bool Empty() const { return start == end; }
  107. void SetInternedStr( const char* str ) { Reset(); this->start = (char*) str; }
  108. void SetStr( const char* str, int flags=0 );
  109. char* ParseText( char* in, const char* endTag, int strFlags );
  110. char* ParseName( char* in );
  111. private:
  112. void Reset();
  113. enum {
  114. NEEDS_FLUSH = 0x100,
  115. NEEDS_DELETE = 0x200
  116. };
  117. // After parsing, if *end != 0, it can be set to zero.
  118. int flags;
  119. char* start;
  120. char* end;
  121. };
  122. /*
  123. A dynamic array of Plain Old Data. Doesn't support constructors, etc.
  124. Has a small initial memory pool, so that low or no usage will not
  125. cause a call to new/delete
  126. */
  127. template <class T, int INIT>
  128. class DynArray
  129. {
  130. public:
  131. DynArray< T, INIT >()
  132. {
  133. mem = pool;
  134. allocated = INIT;
  135. size = 0;
  136. }
  137. ~DynArray()
  138. {
  139. if ( mem != pool ) {
  140. delete mem;
  141. }
  142. }
  143. void Push( T t )
  144. {
  145. EnsureCapacity( size+1 );
  146. mem[size++] = t;
  147. }
  148. T* PushArr( int count )
  149. {
  150. EnsureCapacity( size+count );
  151. T* ret = &mem[size];
  152. size += count;
  153. return ret;
  154. }
  155. T Pop() {
  156. return mem[--size];
  157. }
  158. void PopArr( int count )
  159. {
  160. TIXMLASSERT( size >= count );
  161. size -= count;
  162. }
  163. bool Empty() const { return size == 0; }
  164. T& operator[](int i) { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; }
  165. const T& operator[](int i) const { TIXMLASSERT( i>= 0 && i < size ); return mem[i]; }
  166. int Size() const { return size; }
  167. int Capacity() const { return allocated; }
  168. const T* Mem() const { return mem; }
  169. T* Mem() { return mem; }
  170. private:
  171. void EnsureCapacity( int cap ) {
  172. if ( cap > allocated ) {
  173. int newAllocated = cap * 2;
  174. T* newMem = new T[newAllocated];
  175. memcpy( newMem, mem, sizeof(T)*size ); // warning: not using constructors, only works for PODs
  176. if ( mem != pool ) delete [] mem;
  177. mem = newMem;
  178. allocated = newAllocated;
  179. }
  180. }
  181. T* mem;
  182. T pool[INIT];
  183. int allocated; // objects allocated
  184. int size; // number objects in use
  185. };
  186. /*
  187. Parent virtual class a a pool for fast allocation
  188. and deallocation of objects.
  189. */
  190. class MemPool
  191. {
  192. public:
  193. MemPool() {}
  194. virtual ~MemPool() {}
  195. virtual int ItemSize() const = 0;
  196. virtual void* Alloc() = 0;
  197. virtual void Free( void* ) = 0;
  198. };
  199. /*
  200. Template child class to create pools of the correct type.
  201. */
  202. template< int SIZE >
  203. class MemPoolT : public MemPool
  204. {
  205. public:
  206. MemPoolT() : root(0), currentAllocs(0), nAllocs(0), maxAllocs(0) {}
  207. ~MemPoolT() {
  208. // Delete the blocks.
  209. for( int i=0; i<blockPtrs.Size(); ++i ) {
  210. delete blockPtrs[i];
  211. }
  212. }
  213. virtual int ItemSize() const { return SIZE; }
  214. int CurrentAllocs() const { return currentAllocs; }
  215. virtual void* Alloc() {
  216. if ( !root ) {
  217. // Need a new block.
  218. Block* block = new Block();
  219. blockPtrs.Push( block );
  220. for( int i=0; i<COUNT-1; ++i ) {
  221. block->chunk[i].next = &block->chunk[i+1];
  222. }
  223. block->chunk[COUNT-1].next = 0;
  224. root = block->chunk;
  225. }
  226. void* result = root;
  227. root = root->next;
  228. ++currentAllocs;
  229. if ( currentAllocs > maxAllocs ) maxAllocs = currentAllocs;
  230. nAllocs++;
  231. return result;
  232. }
  233. virtual void Free( void* mem ) {
  234. if ( !mem ) return;
  235. --currentAllocs;
  236. Chunk* chunk = (Chunk*)mem;
  237. memset( chunk, 0xfe, sizeof(Chunk) );
  238. chunk->next = root;
  239. root = chunk;
  240. }
  241. void Trace( const char* name ) {
  242. printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
  243. name, maxAllocs, maxAllocs*SIZE/1024, currentAllocs, SIZE, nAllocs, blockPtrs.Size() );
  244. }
  245. private:
  246. enum { COUNT = 1024/SIZE };
  247. union Chunk {
  248. Chunk* next;
  249. char mem[SIZE];
  250. };
  251. struct Block {
  252. Chunk chunk[COUNT];
  253. };
  254. DynArray< Block*, 10 > blockPtrs;
  255. Chunk* root;
  256. int currentAllocs;
  257. int nAllocs;
  258. int maxAllocs;
  259. };
  260. /**
  261. Implements the interface to the "Visitor pattern" (see the Accept() method.)
  262. If you call the Accept() method, it requires being passed a XMLVisitor
  263. class to handle callbacks. For nodes that contain other nodes (Document, Element)
  264. you will get called with a VisitEnter/VisitExit pair. Nodes that are always leaves
  265. are simply called with Visit().
  266. If you return 'true' from a Visit method, recursive parsing will continue. If you return
  267. false, <b>no children of this node or its sibilings</b> will be Visited.
  268. All flavors of Visit methods have a default implementation that returns 'true' (continue
  269. visiting). You need to only override methods that are interesting to you.
  270. Generally Accept() is called on the TiXmlDocument, although all nodes suppert Visiting.
  271. You should never change the document from a callback.
  272. @sa XMLNode::Accept()
  273. */
  274. class XMLVisitor
  275. {
  276. public:
  277. virtual ~XMLVisitor() {}
  278. /// Visit a document.
  279. virtual bool VisitEnter( const XMLDocument& /*doc*/ ) { return true; }
  280. /// Visit a document.
  281. virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; }
  282. /// Visit an element.
  283. virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) { return true; }
  284. /// Visit an element.
  285. virtual bool VisitExit( const XMLElement& /*element*/ ) { return true; }
  286. /// Visit a declaration
  287. virtual bool Visit( const XMLDeclaration& /*declaration*/ ) { return true; }
  288. /// Visit a text node
  289. virtual bool Visit( const XMLText& /*text*/ ) { return true; }
  290. /// Visit a comment node
  291. virtual bool Visit( const XMLComment& /*comment*/ ) { return true; }
  292. /// Visit an unknown node
  293. virtual bool Visit( const XMLUnknown& /*unknown*/ ) { return true; }
  294. };
  295. /*
  296. Utility functionality.
  297. */
  298. class XMLUtil
  299. {
  300. public:
  301. // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
  302. // correct, but simple, and usually works.
  303. static const char* SkipWhiteSpace( const char* p ) { while( !IsUTF8Continuation(*p) && isspace( *p ) ) { ++p; } return p; }
  304. static char* SkipWhiteSpace( char* p ) { while( !IsUTF8Continuation(*p) && isspace( *p ) ) { ++p; } return p; }
  305. inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
  306. int n = 0;
  307. if ( p == q ) {
  308. return true;
  309. }
  310. while( *p && *q && *p == *q && n<nChar ) {
  311. ++p; ++q; ++n;
  312. }
  313. if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
  314. return true;
  315. }
  316. return false;
  317. }
  318. inline static int IsUTF8Continuation( unsigned char p ) { return p & 0x80; }
  319. inline static int IsAlphaNum( unsigned char anyByte ) { return ( anyByte < 128 ) ? isalnum( anyByte ) : 1; }
  320. inline static int IsAlpha( unsigned char anyByte ) { return ( anyByte < 128 ) ? isalpha( anyByte ) : 1; }
  321. static const char* ReadBOM( const char* p, bool* hasBOM );
  322. // p is the starting location,
  323. // the UTF-8 value of the entity will be placed in value, and length filled in.
  324. static const char* GetCharacterRef( const char* p, char* value, int* length );
  325. static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
  326. };
  327. class XMLNode
  328. {
  329. friend class XMLDocument;
  330. friend class XMLElement;
  331. public:
  332. const XMLDocument* GetDocument() const { return document; }
  333. XMLDocument* GetDocument() { return document; }
  334. virtual XMLElement* ToElement() { return 0; }
  335. virtual XMLText* ToText() { return 0; }
  336. virtual XMLComment* ToComment() { return 0; }
  337. virtual XMLDocument* ToDocument() { return 0; }
  338. virtual XMLDeclaration* ToDeclaration() { return 0; }
  339. virtual XMLUnknown* ToUnknown() { return 0; }
  340. virtual const XMLElement* ToElement() const { return 0; }
  341. virtual const XMLText* ToText() const { return 0; }
  342. virtual const XMLComment* ToComment() const { return 0; }
  343. virtual const XMLDocument* ToDocument() const { return 0; }
  344. virtual const XMLDeclaration* ToDeclaration() const { return 0; }
  345. virtual const XMLUnknown* ToUnknown() const { return 0; }
  346. const char* Value() const { return value.GetStr(); }
  347. void SetValue( const char* val, bool staticMem=false );
  348. const XMLNode* Parent() const { return parent; }
  349. XMLNode* Parent() { return parent; }
  350. /// Returns true if this node has no children.
  351. bool NoChildren() const { return !firstChild; }
  352. const XMLNode* FirstChild() const { return firstChild; }
  353. XMLNode* FirstChild() { return firstChild; }
  354. const XMLElement* FirstChildElement( const char* value=0 ) const;
  355. XMLElement* FirstChildElement( const char* value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( value )); }
  356. const XMLNode* LastChild() const { return lastChild; }
  357. XMLNode* LastChild() { return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->LastChild() ); }
  358. const XMLElement* LastChildElement( const char* value=0 ) const;
  359. XMLElement* LastChildElement( const char* value=0 ) { return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(value) ); }
  360. const XMLNode* PreviousSibling() const { return prev; }
  361. XMLNode* PreviousSibling() { return prev; }
  362. const XMLNode* PreviousSiblingElement( const char* value=0 ) const ;
  363. XMLNode* PreviousSiblingElement( const char* value=0 ) { return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( value ) ); }
  364. const XMLNode* NextSibling() const { return next; }
  365. XMLNode* NextSibling() { return next; }
  366. const XMLNode* NextSiblingElement( const char* value=0 ) const;
  367. XMLNode* NextSiblingElement( const char* value=0 ) { return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->NextSiblingElement( value ) ); }
  368. /**
  369. Tests: Programmatic DOM
  370. */
  371. XMLNode* InsertEndChild( XMLNode* addThis );
  372. /**
  373. Tests: Programmatic DOM
  374. */
  375. XMLNode* InsertFirstChild( XMLNode* addThis );
  376. /**
  377. Tests: Programmatic DOM
  378. */
  379. XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
  380. /**
  381. Tests: All (used by destructor)
  382. */
  383. void ClearChildren();
  384. /**
  385. Tests: Progammatic DOM
  386. */
  387. void DeleteChild( XMLNode* node );
  388. virtual bool Accept( XMLVisitor* visitor ) const = 0;
  389. virtual char* ParseDeep( char*, StrPair* );
  390. protected:
  391. XMLNode( XMLDocument* );
  392. virtual ~XMLNode();
  393. XMLNode( const XMLNode& ); // not supported
  394. void operator=( const XMLNode& ); // not supported
  395. XMLDocument* document;
  396. XMLNode* parent;
  397. mutable StrPair value;
  398. XMLNode* firstChild;
  399. XMLNode* lastChild;
  400. XMLNode* prev;
  401. XMLNode* next;
  402. private:
  403. MemPool* memPool;
  404. void Unlink( XMLNode* child );
  405. };
  406. class XMLText : public XMLNode
  407. {
  408. friend class XMLBase;
  409. friend class XMLDocument;
  410. public:
  411. virtual bool Accept( XMLVisitor* visitor ) const;
  412. virtual XMLText* ToText() { return this; }
  413. virtual const XMLText* ToText() const { return this; }
  414. void SetCData( bool value ) { isCData = true; }
  415. bool CData() const { return isCData; }
  416. char* ParseDeep( char*, StrPair* endTag );
  417. protected:
  418. XMLText( XMLDocument* doc ) : XMLNode( doc ), isCData( false ) {}
  419. virtual ~XMLText() {}
  420. XMLText( const XMLText& ); // not supported
  421. void operator=( const XMLText& ); // not supported
  422. private:
  423. bool isCData;
  424. };
  425. class XMLComment : public XMLNode
  426. {
  427. friend class XMLDocument;
  428. public:
  429. virtual XMLComment* ToComment() { return this; }
  430. virtual const XMLComment* ToComment() const { return this; }
  431. virtual bool Accept( XMLVisitor* visitor ) const;
  432. char* ParseDeep( char*, StrPair* endTag );
  433. protected:
  434. XMLComment( XMLDocument* doc );
  435. virtual ~XMLComment();
  436. XMLComment( const XMLComment& ); // not supported
  437. void operator=( const XMLComment& ); // not supported
  438. private:
  439. };
  440. class XMLDeclaration : public XMLNode
  441. {
  442. friend class XMLDocument;
  443. public:
  444. virtual XMLDeclaration* ToDeclaration() { return this; }
  445. virtual const XMLDeclaration* ToDeclaration() const { return this; }
  446. virtual bool Accept( XMLVisitor* visitor ) const;
  447. char* ParseDeep( char*, StrPair* endTag );
  448. protected:
  449. XMLDeclaration( XMLDocument* doc );
  450. virtual ~XMLDeclaration();
  451. XMLDeclaration( const XMLDeclaration& ); // not supported
  452. void operator=( const XMLDeclaration& ); // not supported
  453. };
  454. class XMLUnknown : public XMLNode
  455. {
  456. friend class XMLDocument;
  457. public:
  458. virtual XMLUnknown* ToUnknown() { return this; }
  459. virtual const XMLUnknown* ToUnknown() const { return this; }
  460. virtual bool Accept( XMLVisitor* visitor ) const;
  461. char* ParseDeep( char*, StrPair* endTag );
  462. protected:
  463. XMLUnknown( XMLDocument* doc );
  464. virtual ~XMLUnknown();
  465. XMLUnknown( const XMLUnknown& ); // not supported
  466. void operator=( const XMLUnknown& ); // not supported
  467. };
  468. enum {
  469. XML_NO_ERROR = 0,
  470. NO_ATTRIBUTE,
  471. WRONG_ATTRIBUTE_TYPE,
  472. ERROR_FILE_NOT_FOUND,
  473. ERROR_ELEMENT_MISMATCH,
  474. ERROR_PARSING_ELEMENT,
  475. ERROR_PARSING_ATTRIBUTE,
  476. ERROR_IDENTIFYING_TAG,
  477. ERROR_PARSING_TEXT,
  478. ERROR_PARSING_CDATA,
  479. ERROR_PARSING_COMMENT,
  480. ERROR_PARSING_DECLARATION,
  481. ERROR_PARSING_UNKNOWN,
  482. ERROR_EMPTY_DOCUMENT,
  483. ERROR_MISMATCHED_ELEMENT,
  484. ERROR_PARSING
  485. };
  486. class XMLAttribute
  487. {
  488. friend class XMLElement;
  489. public:
  490. const char* Name() const { return name.GetStr(); }
  491. const char* Value() const { return value.GetStr(); }
  492. const XMLAttribute* Next() const { return next; }
  493. int IntAttribute( const char* name ) const { int i=0; QueryIntAttribute( &i ); return i; }
  494. unsigned UnsignedAttribute( const char* name ) const{ unsigned i=0; QueryUnsignedAttribute( &i ); return i; }
  495. bool BoolAttribute( const char* name ) const { bool b=false; QueryBoolAttribute( &b ); return b; }
  496. double DoubleAttribute( const char* name ) const { double d=0; QueryDoubleAttribute( &d ); return d; }
  497. float FloatAttribute( const char* name ) const { float f=0; QueryFloatAttribute( &f ); return f; }
  498. int QueryIntAttribute( int* value ) const;
  499. int QueryUnsignedAttribute( unsigned int* value ) const;
  500. int QueryBoolAttribute( bool* value ) const;
  501. int QueryDoubleAttribute( double* value ) const;
  502. int QueryFloatAttribute( float* value ) const;
  503. void SetAttribute( const char* value );
  504. void SetAttribute( int value );
  505. void SetAttribute( unsigned value );
  506. void SetAttribute( bool value );
  507. void SetAttribute( double value );
  508. void SetAttribute( float value );
  509. private:
  510. enum { BUF_SIZE = 200 };
  511. XMLAttribute( XMLElement* element ) : next( 0 ) {}
  512. virtual ~XMLAttribute() {}
  513. XMLAttribute( const XMLAttribute& ); // not supported
  514. void operator=( const XMLAttribute& ); // not supported
  515. void SetName( const char* name );
  516. char* ParseDeep( char* p );
  517. mutable StrPair name;
  518. mutable StrPair value;
  519. XMLAttribute* next;
  520. MemPool* memPool;
  521. };
  522. class XMLElement : public XMLNode
  523. {
  524. friend class XMLBase;
  525. friend class XMLDocument;
  526. public:
  527. const char* Name() const { return Value(); }
  528. void SetName( const char* str, bool staticMem=false ) { SetValue( str, staticMem ); }
  529. virtual XMLElement* ToElement() { return this; }
  530. virtual const XMLElement* ToElement() const { return this; }
  531. virtual bool Accept( XMLVisitor* visitor ) const;
  532. const char* Attribute( const char* name ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return 0; return a->Value(); }
  533. int IntAttribute( const char* name ) const { int i=0; QueryIntAttribute( name, &i ); return i; }
  534. unsigned UnsignedAttribute( const char* name ) const{ unsigned i=0; QueryUnsignedAttribute( name, &i ); return i; }
  535. bool BoolAttribute( const char* name ) const { bool b=false; QueryBoolAttribute( name, &b ); return b; }
  536. double DoubleAttribute( const char* name ) const { double d=0; QueryDoubleAttribute( name, &d ); return d; }
  537. float FloatAttribute( const char* name ) const { float f=0; QueryFloatAttribute( name, &f ); return f; }
  538. int QueryIntAttribute( const char* name, int* value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return NO_ATTRIBUTE; return a->QueryIntAttribute( value ); }
  539. int QueryUnsignedAttribute( const char* name, unsigned int* value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return NO_ATTRIBUTE; return a->QueryUnsignedAttribute( value ); }
  540. int QueryBoolAttribute( const char* name, bool* value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return NO_ATTRIBUTE; return a->QueryBoolAttribute( value ); }
  541. int QueryDoubleAttribute( const char* name, double* value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return NO_ATTRIBUTE; return a->QueryDoubleAttribute( value ); }
  542. int QueryFloatAttribute( const char* name, float* value ) const { const XMLAttribute* a = FindAttribute( name ); if ( !a ) return NO_ATTRIBUTE; return a->QueryFloatAttribute( value ); }
  543. void SetAttribute( const char* name, const char* value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( value ); }
  544. void SetAttribute( const char* name, int value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( value ); }
  545. void SetAttribute( const char* name, unsigned value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( value ); }
  546. void SetAttribute( const char* name, bool value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( value ); }
  547. void SetAttribute( const char* name, double value ) { XMLAttribute* a = FindOrCreateAttribute( name ); a->SetAttribute( value ); }
  548. /**
  549. Tests: Programmatic DOM
  550. */
  551. void DeleteAttribute( const char* name );
  552. const XMLAttribute* FirstAttribute() const { return rootAttribute; }
  553. const XMLAttribute* FindAttribute( const char* name ) const;
  554. const char* GetText() const;
  555. // internal:
  556. enum {
  557. OPEN, // <foo>
  558. CLOSED, // <foo/>
  559. CLOSING // </foo>
  560. };
  561. int ClosingType() const { return closingType; }
  562. char* ParseDeep( char* p, StrPair* endTag );
  563. private:
  564. XMLElement( XMLDocument* doc );
  565. virtual ~XMLElement();
  566. XMLElement( const XMLElement& ); // not supported
  567. void operator=( const XMLElement& ); // not supported
  568. XMLAttribute* FindAttribute( const char* name );
  569. XMLAttribute* FindOrCreateAttribute( const char* name );
  570. void LinkAttribute( XMLAttribute* attrib );
  571. char* ParseAttributes( char* p );
  572. int closingType;
  573. XMLAttribute* rootAttribute;
  574. };
  575. class XMLDocument : public XMLNode
  576. {
  577. friend class XMLElement;
  578. public:
  579. XMLDocument();
  580. ~XMLDocument();
  581. virtual XMLDocument* ToDocument() { return this; }
  582. virtual const XMLDocument* ToDocument() const { return this; }
  583. int Parse( const char* xml );
  584. int LoadFile( const char* filename );
  585. int LoadFile( FILE* );
  586. void SaveFile( const char* filename );
  587. bool HasBOM() const { return writeBOM; }
  588. XMLElement* RootElement() { return FirstChildElement(); }
  589. const XMLElement* RootElement() const { return FirstChildElement(); }
  590. void Print( XMLStreamer* streamer=0 );
  591. virtual bool Accept( XMLVisitor* visitor ) const;
  592. /**
  593. Tests: Programmatic DOM
  594. */
  595. XMLElement* NewElement( const char* name );
  596. /**
  597. Tests: Programmatic DOM
  598. */
  599. XMLComment* NewComment( const char* comment );
  600. /**
  601. Tests: Programmatic DOM
  602. */
  603. XMLText* NewText( const char* text );
  604. /**
  605. Tests: Programmatic DOM
  606. */
  607. void DeleteNode( XMLNode* node ) { node->parent->DeleteChild( node ); }
  608. void SetError( int error, const char* str1, const char* str2 );
  609. bool Error() const { return errorID != XML_NO_ERROR; }
  610. int ErrorID() const { return errorID; }
  611. const char* GetErrorStr1() const { return errorStr1; }
  612. const char* GetErrorStr2() const { return errorStr2; }
  613. void PrintError() const;
  614. char* Identify( char* p, XMLNode** node );
  615. private:
  616. XMLDocument( const XMLDocument& ); // not supported
  617. void operator=( const XMLDocument& ); // not supported
  618. void InitDocument();
  619. bool writeBOM;
  620. int errorID;
  621. const char* errorStr1;
  622. const char* errorStr2;
  623. char* charBuffer;
  624. MemPoolT< sizeof(XMLElement) > elementPool;
  625. MemPoolT< sizeof(XMLAttribute) > attributePool;
  626. MemPoolT< sizeof(XMLText) > textPool;
  627. MemPoolT< sizeof(XMLComment) > commentPool;
  628. };
  629. class XMLStreamer : public XMLVisitor
  630. {
  631. public:
  632. XMLStreamer( FILE* file=0 );
  633. ~XMLStreamer() {}
  634. void PushHeader( bool writeBOM, bool writeDeclaration );
  635. void OpenElement( const char* name );
  636. void PushAttribute( const char* name, const char* value );
  637. void CloseElement();
  638. void PushText( const char* text, bool cdata=false );
  639. void PushComment( const char* comment );
  640. void PushDeclaration( const char* value );
  641. void PushUnknown( const char* value );
  642. virtual bool VisitEnter( const XMLDocument& /*doc*/ );
  643. virtual bool VisitExit( const XMLDocument& /*doc*/ ) { return true; }
  644. virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
  645. virtual bool VisitExit( const XMLElement& element );
  646. virtual bool Visit( const XMLText& text );
  647. virtual bool Visit( const XMLComment& comment );
  648. virtual bool Visit( const XMLDeclaration& declaration );
  649. virtual bool Visit( const XMLUnknown& unknown );
  650. const char* CStr() const { return buffer.Mem(); }
  651. private:
  652. void SealElement();
  653. void PrintSpace( int depth );
  654. void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
  655. void Print( const char* format, ... );
  656. bool elementJustOpened;
  657. bool firstElement;
  658. FILE* fp;
  659. int depth;
  660. int textDepth;
  661. enum {
  662. ENTITY_RANGE = 64
  663. };
  664. bool entityFlag[ENTITY_RANGE];
  665. bool restrictedEntityFlag[ENTITY_RANGE];
  666. DynArray< const char*, 10 > stack;
  667. DynArray< char, 20 > buffer, accumulator;
  668. };
  669. }; // tinyxml2
  670. #endif // TINYXML2_INCLUDED