tinyxml2.h 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  1. /*
  2. Original code by Lee Thomason (www.grinninglizard.com)
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any
  5. damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any
  7. purpose, including commercial applications, and to alter it and
  8. redistribute it freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must
  10. not claim that you wrote the original software. If you use this
  11. software in a product, an acknowledgment in the product documentation
  12. would be appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and
  14. must not be misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. */
  18. #ifndef TINYXML2_INCLUDED
  19. #define TINYXML2_INCLUDED
  20. #include "platform/CCPlatformConfig.h"
  21. #include "platform/CCPlatformMacros.h"
  22. #if defined(ANDROID_NDK) || defined(__BORLANDC__) || (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)
  23. # include <ctype.h>
  24. # include <limits.h>
  25. # include <stdio.h>
  26. # include <stdlib.h>
  27. # include <string.h>
  28. # include <stdarg.h>
  29. #else
  30. # include <cctype>
  31. # include <climits>
  32. # include <cstdio>
  33. # include <cstdlib>
  34. # include <cstring>
  35. # include <cstdarg>
  36. #endif
  37. /*
  38. TODO: intern strings instead of allocation.
  39. */
  40. /*
  41. gcc:
  42. g++ -Wall -DDEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
  43. Formatting, Artistic Style:
  44. AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
  45. */
  46. #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
  47. # ifndef DEBUG
  48. # define DEBUG
  49. # endif
  50. #endif
  51. #if defined(DEBUG)
  52. # if defined(_MSC_VER)
  53. # define TIXMLASSERT( x ) if ( !(x)) { __debugbreak(); } //if ( !(x)) WinDebugBreak()
  54. # elif defined (ANDROID_NDK)
  55. # include <android/log.h>
  56. # define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
  57. # else
  58. # include <assert.h>
  59. # define TIXMLASSERT assert
  60. # endif
  61. # else
  62. # define TIXMLASSERT( x ) {}
  63. #endif
  64. #if (defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE))
  65. // Microsoft visual studio, version 2005 and higher.
  66. /*int _snprintf_s(
  67. char *buffer,
  68. size_t sizeOfBuffer,
  69. size_t count,
  70. const char *format [,
  71. argument] ...
  72. );*/
  73. inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... )
  74. {
  75. va_list va;
  76. va_start( va, format );
  77. int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
  78. va_end( va );
  79. return result;
  80. }
  81. #define TIXML_SSCANF sscanf_s
  82. #else
  83. // GCC version 3 and higher
  84. //#warning( "Using sn* functions." )
  85. #define TIXML_SNPRINTF snprintf
  86. #define TIXML_SSCANF sscanf
  87. #endif
  88. static const int TIXML2_MAJOR_VERSION = 1;
  89. static const int TIXML2_MINOR_VERSION = 0;
  90. static const int TIXML2_PATCH_VERSION = 9;
  91. namespace tinyxml2
  92. {
  93. class XMLDocument;
  94. class XMLElement;
  95. class XMLAttribute;
  96. class XMLComment;
  97. class XMLNode;
  98. class XMLText;
  99. class XMLDeclaration;
  100. class XMLUnknown;
  101. class XMLPrinter;
  102. /*
  103. A class that wraps strings. Normally stores the start and end
  104. pointers into the XML file itself, and will apply normalization
  105. and entity translation if actually read. Can also store (and memory
  106. manage) a traditional char[]
  107. */
  108. class CC_DLL StrPair
  109. {
  110. public:
  111. enum {
  112. NEEDS_ENTITY_PROCESSING = 0x01,
  113. NEEDS_NEWLINE_NORMALIZATION = 0x02,
  114. COLLAPSE_WHITESPACE = 0x04,
  115. TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
  116. TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
  117. ATTRIBUTE_NAME = 0,
  118. ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
  119. ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
  120. COMMENT = NEEDS_NEWLINE_NORMALIZATION
  121. };
  122. StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
  123. ~StrPair();
  124. void Set( char* start, char* end, int flags ) {
  125. Reset();
  126. _start = start;
  127. _end = end;
  128. _flags = flags | NEEDS_FLUSH;
  129. }
  130. const char* GetStr();
  131. bool Empty() const {
  132. return _start == _end;
  133. }
  134. void SetInternedStr( const char* str ) {
  135. Reset();
  136. _start = const_cast<char*>(str);
  137. }
  138. void SetStr( const char* str, int flags=0 );
  139. char* ParseText( char* in, const char* endTag, int strFlags );
  140. char* ParseName( char* in );
  141. private:
  142. void Reset();
  143. void CollapseWhitespace();
  144. enum {
  145. NEEDS_FLUSH = 0x100,
  146. NEEDS_DELETE = 0x200
  147. };
  148. // After parsing, if *end != 0, it can be set to zero.
  149. int _flags;
  150. char* _start;
  151. char* _end;
  152. };
  153. /*
  154. A dynamic array of Plain Old Data. Doesn't support constructors, etc.
  155. Has a small initial memory pool, so that low or no usage will not
  156. cause a call to new/delete
  157. */
  158. template <class T, int INIT>
  159. class CC_DLL DynArray
  160. {
  161. public:
  162. DynArray< T, INIT >() {
  163. _mem = _pool;
  164. _allocated = INIT;
  165. _size = 0;
  166. }
  167. ~DynArray() {
  168. if ( _mem != _pool ) {
  169. delete [] _mem;
  170. }
  171. }
  172. void Push( T t ) {
  173. EnsureCapacity( _size+1 );
  174. _mem[_size++] = t;
  175. }
  176. T* PushArr( int count ) {
  177. EnsureCapacity( _size+count );
  178. T* ret = &_mem[_size];
  179. _size += count;
  180. return ret;
  181. }
  182. T Pop() {
  183. return _mem[--_size];
  184. }
  185. void PopArr( int count ) {
  186. TIXMLASSERT( _size >= count );
  187. _size -= count;
  188. }
  189. bool Empty() const {
  190. return _size == 0;
  191. }
  192. T& operator[](int i) {
  193. TIXMLASSERT( i>= 0 && i < _size );
  194. return _mem[i];
  195. }
  196. const T& operator[](int i) const {
  197. TIXMLASSERT( i>= 0 && i < _size );
  198. return _mem[i];
  199. }
  200. int Size() const {
  201. return _size;
  202. }
  203. int Capacity() const {
  204. return _allocated;
  205. }
  206. const T* Mem() const {
  207. return _mem;
  208. }
  209. T* Mem() {
  210. return _mem;
  211. }
  212. private:
  213. void EnsureCapacity( int cap ) {
  214. if ( cap > _allocated ) {
  215. int newAllocated = cap * 2;
  216. T* newMem = new T[newAllocated];
  217. memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
  218. if ( _mem != _pool ) {
  219. delete [] _mem;
  220. }
  221. _mem = newMem;
  222. _allocated = newAllocated;
  223. }
  224. }
  225. T* _mem;
  226. T _pool[INIT];
  227. int _allocated; // objects allocated
  228. int _size; // number objects in use
  229. };
  230. /*
  231. Parent virtual class of a pool for fast allocation
  232. and deallocation of objects.
  233. */
  234. class CC_DLL MemPool
  235. {
  236. public:
  237. MemPool() {}
  238. virtual ~MemPool() {}
  239. virtual int ItemSize() const = 0;
  240. virtual void* Alloc() = 0;
  241. virtual void Free( void* ) = 0;
  242. virtual void SetTracked() = 0;
  243. };
  244. /*
  245. Template child class to create pools of the correct type.
  246. */
  247. template< int SIZE >
  248. class CC_DLL MemPoolT : public MemPool
  249. {
  250. public:
  251. MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
  252. ~MemPoolT() {
  253. // Delete the blocks.
  254. for( int i=0; i<_blockPtrs.Size(); ++i ) {
  255. delete _blockPtrs[i];
  256. }
  257. }
  258. virtual int ItemSize() const {
  259. return SIZE;
  260. }
  261. int CurrentAllocs() const {
  262. return _currentAllocs;
  263. }
  264. virtual void* Alloc() {
  265. if ( !_root ) {
  266. // Need a new block.
  267. Block* block = new Block();
  268. _blockPtrs.Push( block );
  269. for( int i=0; i<COUNT-1; ++i ) {
  270. block->chunk[i].next = &block->chunk[i+1];
  271. }
  272. block->chunk[COUNT-1].next = 0;
  273. _root = block->chunk;
  274. }
  275. void* result = _root;
  276. _root = _root->next;
  277. ++_currentAllocs;
  278. if ( _currentAllocs > _maxAllocs ) {
  279. _maxAllocs = _currentAllocs;
  280. }
  281. _nAllocs++;
  282. _nUntracked++;
  283. return result;
  284. }
  285. virtual void Free( void* mem ) {
  286. if ( !mem ) {
  287. return;
  288. }
  289. --_currentAllocs;
  290. Chunk* chunk = (Chunk*)mem;
  291. #ifdef DEBUG
  292. memset( chunk, 0xfe, sizeof(Chunk) );
  293. #endif
  294. chunk->next = _root;
  295. _root = chunk;
  296. }
  297. void Trace( const char* name ) {
  298. printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
  299. name, _maxAllocs, _maxAllocs*SIZE/1024, _currentAllocs, SIZE, _nAllocs, _blockPtrs.Size() );
  300. }
  301. void SetTracked() {
  302. _nUntracked--;
  303. }
  304. int Untracked() const {
  305. return _nUntracked;
  306. }
  307. enum { COUNT = 1024/SIZE }; // Some compilers do not accept to use COUNT in private part if COUNT is private
  308. private:
  309. union Chunk {
  310. Chunk* next;
  311. char mem[SIZE];
  312. };
  313. struct Block {
  314. Chunk chunk[COUNT];
  315. };
  316. DynArray< Block*, 10 > _blockPtrs;
  317. Chunk* _root;
  318. int _currentAllocs;
  319. int _nAllocs;
  320. int _maxAllocs;
  321. int _nUntracked;
  322. };
  323. /**
  324. Implements the interface to the "Visitor pattern" (see the Accept() method.)
  325. If you call the Accept() method, it requires being passed a XMLVisitor
  326. class to handle callbacks. For nodes that contain other nodes (Document, Element)
  327. you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
  328. are simply called with Visit().
  329. If you return 'true' from a Visit method, recursive parsing will continue. If you return
  330. false, <b>no children of this node or its sibilings</b> will be visited.
  331. All flavors of Visit methods have a default implementation that returns 'true' (continue
  332. visiting). You need to only override methods that are interesting to you.
  333. Generally Accept() is called on the TiXmlDocument, although all nodes support visiting.
  334. You should never change the document from a callback.
  335. @sa XMLNode::Accept()
  336. */
  337. class CC_DLL XMLVisitor
  338. {
  339. public:
  340. virtual ~XMLVisitor() {}
  341. /// Visit a document.
  342. virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
  343. return true;
  344. }
  345. /// Visit a document.
  346. virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
  347. return true;
  348. }
  349. /// Visit an element.
  350. virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
  351. return true;
  352. }
  353. /// Visit an element.
  354. virtual bool VisitExit( const XMLElement& /*element*/ ) {
  355. return true;
  356. }
  357. /// Visit a declaration.
  358. virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
  359. return true;
  360. }
  361. /// Visit a text node.
  362. virtual bool Visit( const XMLText& /*text*/ ) {
  363. return true;
  364. }
  365. /// Visit a comment node.
  366. virtual bool Visit( const XMLComment& /*comment*/ ) {
  367. return true;
  368. }
  369. /// Visit an unknown node.
  370. virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
  371. return true;
  372. }
  373. };
  374. /*
  375. Utility functionality.
  376. */
  377. class CC_DLL XMLUtil
  378. {
  379. public:
  380. // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
  381. // correct, but simple, and usually works.
  382. static const char* SkipWhiteSpace( const char* p ) {
  383. while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<const unsigned char*>(p) ) ) {
  384. ++p;
  385. }
  386. return p;
  387. }
  388. static char* SkipWhiteSpace( char* p ) {
  389. while( !IsUTF8Continuation(*p) && isspace( *reinterpret_cast<unsigned char*>(p) ) ) {
  390. ++p;
  391. }
  392. return p;
  393. }
  394. static bool IsWhiteSpace( char p ) {
  395. return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
  396. }
  397. inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
  398. int n = 0;
  399. if ( p == q ) {
  400. return true;
  401. }
  402. while( *p && *q && *p == *q && n<nChar ) {
  403. ++p;
  404. ++q;
  405. ++n;
  406. }
  407. if ( (n == nChar) || ( *p == 0 && *q == 0 ) ) {
  408. return true;
  409. }
  410. return false;
  411. }
  412. inline static int IsUTF8Continuation( const char p ) {
  413. return p & 0x80;
  414. }
  415. inline static int IsAlphaNum( unsigned char anyByte ) {
  416. return ( anyByte < 128 ) ? isalnum( anyByte ) : 1;
  417. }
  418. inline static int IsAlpha( unsigned char anyByte ) {
  419. return ( anyByte < 128 ) ? isalpha( anyByte ) : 1;
  420. }
  421. static const char* ReadBOM( const char* p, bool* hasBOM );
  422. // p is the starting location,
  423. // the UTF-8 value of the entity will be placed in value, and length filled in.
  424. static const char* GetCharacterRef( const char* p, char* value, int* length );
  425. static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
  426. // converts primitive types to strings
  427. static void ToStr( int v, char* buffer, int bufferSize );
  428. static void ToStr( unsigned v, char* buffer, int bufferSize );
  429. static void ToStr( bool v, char* buffer, int bufferSize );
  430. static void ToStr( float v, char* buffer, int bufferSize );
  431. static void ToStr( double v, char* buffer, int bufferSize );
  432. // converts strings to primitive types
  433. static bool ToInt( const char* str, int* value );
  434. static bool ToUnsigned( const char* str, unsigned* value );
  435. static bool ToBool( const char* str, bool* value );
  436. static bool ToFloat( const char* str, float* value );
  437. static bool ToDouble( const char* str, double* value );
  438. };
  439. /** XMLNode is a base class for every object that is in the
  440. XML Document Object Model (DOM), except XMLAttributes.
  441. Nodes have siblings, a parent, and children which can
  442. be navigated. A node is always in a XMLDocument.
  443. The type of a XMLNode can be queried, and it can
  444. be cast to its more defined type.
  445. A XMLDocument allocates memory for all its Nodes.
  446. When the XMLDocument gets deleted, all its Nodes
  447. will also be deleted.
  448. @verbatim
  449. A Document can contain: Element (container or leaf)
  450. Comment (leaf)
  451. Unknown (leaf)
  452. Declaration( leaf )
  453. An Element can contain: Element (container or leaf)
  454. Text (leaf)
  455. Attributes (not on tree)
  456. Comment (leaf)
  457. Unknown (leaf)
  458. @endverbatim
  459. */
  460. class CC_DLL XMLNode
  461. {
  462. friend class XMLDocument;
  463. friend class XMLElement;
  464. public:
  465. /// Get the XMLDocument that owns this XMLNode.
  466. const XMLDocument* GetDocument() const {
  467. return _document;
  468. }
  469. /// Get the XMLDocument that owns this XMLNode.
  470. XMLDocument* GetDocument() {
  471. return _document;
  472. }
  473. /// Safely cast to an Element, or null.
  474. virtual XMLElement* ToElement() {
  475. return 0;
  476. }
  477. /// Safely cast to Text, or null.
  478. virtual XMLText* ToText() {
  479. return 0;
  480. }
  481. /// Safely cast to a Comment, or null.
  482. virtual XMLComment* ToComment() {
  483. return 0;
  484. }
  485. /// Safely cast to a Document, or null.
  486. virtual XMLDocument* ToDocument() {
  487. return 0;
  488. }
  489. /// Safely cast to a Declaration, or null.
  490. virtual XMLDeclaration* ToDeclaration() {
  491. return 0;
  492. }
  493. /// Safely cast to an Unknown, or null.
  494. virtual XMLUnknown* ToUnknown() {
  495. return 0;
  496. }
  497. virtual const XMLElement* ToElement() const {
  498. return 0;
  499. }
  500. virtual const XMLText* ToText() const {
  501. return 0;
  502. }
  503. virtual const XMLComment* ToComment() const {
  504. return 0;
  505. }
  506. virtual const XMLDocument* ToDocument() const {
  507. return 0;
  508. }
  509. virtual const XMLDeclaration* ToDeclaration() const {
  510. return 0;
  511. }
  512. virtual const XMLUnknown* ToUnknown() const {
  513. return 0;
  514. }
  515. /** The meaning of 'value' changes for the specific type.
  516. @verbatim
  517. Document: empty
  518. Element: name of the element
  519. Comment: the comment text
  520. Unknown: the tag contents
  521. Text: the text string
  522. @endverbatim
  523. */
  524. const char* Value() const {
  525. return _value.GetStr();
  526. }
  527. /** Set the Value of an XML node.
  528. @sa Value()
  529. */
  530. void SetValue( const char* val, bool staticMem=false );
  531. /// Get the parent of this node on the DOM.
  532. const XMLNode* Parent() const {
  533. return _parent;
  534. }
  535. XMLNode* Parent() {
  536. return _parent;
  537. }
  538. /// Returns true if this node has no children.
  539. bool NoChildren() const {
  540. return !_firstChild;
  541. }
  542. /// Get the first child node, or null if none exists.
  543. const XMLNode* FirstChild() const {
  544. return _firstChild;
  545. }
  546. XMLNode* FirstChild() {
  547. return _firstChild;
  548. }
  549. /** Get the first child element, or optionally the first child
  550. element with the specified name.
  551. */
  552. const XMLElement* FirstChildElement( const char* value=0 ) const;
  553. XMLElement* FirstChildElement( const char* value=0 ) {
  554. return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( value ));
  555. }
  556. /// Get the last child node, or null if none exists.
  557. const XMLNode* LastChild() const {
  558. return _lastChild;
  559. }
  560. XMLNode* LastChild() {
  561. return const_cast<XMLNode*>(const_cast<const XMLNode*>(this)->LastChild() );
  562. }
  563. /** Get the last child element or optionally the last child
  564. element with the specified name.
  565. */
  566. const XMLElement* LastChildElement( const char* value=0 ) const;
  567. XMLElement* LastChildElement( const char* value=0 ) {
  568. return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(value) );
  569. }
  570. /// Get the previous (left) sibling node of this node.
  571. const XMLNode* PreviousSibling() const {
  572. return _prev;
  573. }
  574. XMLNode* PreviousSibling() {
  575. return _prev;
  576. }
  577. /// Get the previous (left) sibling element of this node, with an opitionally supplied name.
  578. const XMLElement* PreviousSiblingElement( const char* value=0 ) const ;
  579. XMLElement* PreviousSiblingElement( const char* value=0 ) {
  580. return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( value ) );
  581. }
  582. /// Get the next (right) sibling node of this node.
  583. const XMLNode* NextSibling() const {
  584. return _next;
  585. }
  586. XMLNode* NextSibling() {
  587. return _next;
  588. }
  589. /// Get the next (right) sibling element of this node, with an opitionally supplied name.
  590. const XMLElement* NextSiblingElement( const char* value=0 ) const;
  591. XMLElement* NextSiblingElement( const char* value=0 ) {
  592. return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( value ) );
  593. }
  594. /**
  595. Add a child node as the last (right) child.
  596. */
  597. XMLNode* InsertEndChild( XMLNode* addThis );
  598. XMLNode* LinkEndChild( XMLNode* addThis ) {
  599. return InsertEndChild( addThis );
  600. }
  601. /**
  602. Add a child node as the first (left) child.
  603. */
  604. XMLNode* InsertFirstChild( XMLNode* addThis );
  605. /**
  606. Add a node after the specified child node.
  607. */
  608. XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
  609. /**
  610. Delete all the children of this node.
  611. */
  612. void DeleteChildren();
  613. /**
  614. Delete a child of this node.
  615. */
  616. void DeleteChild( XMLNode* node );
  617. /**
  618. Make a copy of this node, but not its children.
  619. You may pass in a Document pointer that will be
  620. the owner of the new Node. If the 'document' is
  621. null, then the node returned will be allocated
  622. from the current Document. (this->GetDocument())
  623. Note: if called on a XMLDocument, this will return null.
  624. */
  625. virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
  626. /**
  627. Test if 2 nodes are the same, but don't test children.
  628. The 2 nodes do not need to be in the same Document.
  629. Note: if called on a XMLDocument, this will return false.
  630. */
  631. virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
  632. /** Accept a hierarchical visit of the nodes in the TinyXML DOM. Every node in the
  633. XML tree will be conditionally visited and the host will be called back
  634. via the TiXmlVisitor interface.
  635. This is essentially a SAX interface for TinyXML. (Note however it doesn't re-parse
  636. the XML for the callbacks, so the performance of TinyXML is unchanged by using this
  637. interface versus any other.)
  638. The interface has been based on ideas from:
  639. - http://www.saxproject.org/
  640. - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
  641. Which are both good references for "visiting".
  642. An example of using Accept():
  643. @verbatim
  644. TiXmlPrinter printer;
  645. tinyxmlDoc.Accept( &printer );
  646. const char* xmlcstr = printer.CStr();
  647. @endverbatim
  648. */
  649. virtual bool Accept( XMLVisitor* visitor ) const = 0;
  650. // internal
  651. virtual char* ParseDeep( char*, StrPair* );
  652. protected:
  653. XMLNode( XMLDocument* );
  654. virtual ~XMLNode();
  655. XMLNode( const XMLNode& ); // not supported
  656. XMLNode& operator=( const XMLNode& ); // not supported
  657. XMLDocument* _document;
  658. XMLNode* _parent;
  659. mutable StrPair _value;
  660. XMLNode* _firstChild;
  661. XMLNode* _lastChild;
  662. XMLNode* _prev;
  663. XMLNode* _next;
  664. private:
  665. MemPool* _memPool;
  666. void Unlink( XMLNode* child );
  667. };
  668. /** XML text.
  669. Note that a text node can have child element nodes, for example:
  670. @verbatim
  671. <root>This is <b>bold</b></root>
  672. @endverbatim
  673. A text node can have 2 ways to output the next. "normal" output
  674. and CDATA. It will default to the mode it was parsed from the XML file and
  675. you generally want to leave it alone, but you can change the output mode with
  676. SetCDATA() and query it with CDATA().
  677. */
  678. class CC_DLL XMLText : public XMLNode
  679. {
  680. friend class XMLBase;
  681. friend class XMLDocument;
  682. public:
  683. virtual bool Accept( XMLVisitor* visitor ) const;
  684. virtual XMLText* ToText() {
  685. return this;
  686. }
  687. virtual const XMLText* ToText() const {
  688. return this;
  689. }
  690. /// Declare whether this should be CDATA or standard text.
  691. void SetCData( bool isCData ) {
  692. _isCData = isCData;
  693. }
  694. /// Returns true if this is a CDATA text element.
  695. bool CData() const {
  696. return _isCData;
  697. }
  698. char* ParseDeep( char*, StrPair* endTag );
  699. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  700. virtual bool ShallowEqual( const XMLNode* compare ) const;
  701. protected:
  702. XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
  703. virtual ~XMLText() {}
  704. XMLText( const XMLText& ); // not supported
  705. XMLText& operator=( const XMLText& ); // not supported
  706. private:
  707. bool _isCData;
  708. };
  709. /** An XML Comment. */
  710. class CC_DLL XMLComment : public XMLNode
  711. {
  712. friend class XMLDocument;
  713. public:
  714. virtual XMLComment* ToComment() {
  715. return this;
  716. }
  717. virtual const XMLComment* ToComment() const {
  718. return this;
  719. }
  720. virtual bool Accept( XMLVisitor* visitor ) const;
  721. char* ParseDeep( char*, StrPair* endTag );
  722. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  723. virtual bool ShallowEqual( const XMLNode* compare ) const;
  724. protected:
  725. XMLComment( XMLDocument* doc );
  726. virtual ~XMLComment();
  727. XMLComment( const XMLComment& ); // not supported
  728. XMLComment& operator=( const XMLComment& ); // not supported
  729. private:
  730. };
  731. /** In correct XML the declaration is the first entry in the file.
  732. @verbatim
  733. <?xml version="1.0" standalone="yes"?>
  734. @endverbatim
  735. TinyXML2 will happily read or write files without a declaration,
  736. however.
  737. The text of the declaration isn't interpreted. It is parsed
  738. and written as a string.
  739. */
  740. class CC_DLL XMLDeclaration : public XMLNode
  741. {
  742. friend class XMLDocument;
  743. public:
  744. virtual XMLDeclaration* ToDeclaration() {
  745. return this;
  746. }
  747. virtual const XMLDeclaration* ToDeclaration() const {
  748. return this;
  749. }
  750. virtual bool Accept( XMLVisitor* visitor ) const;
  751. char* ParseDeep( char*, StrPair* endTag );
  752. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  753. virtual bool ShallowEqual( const XMLNode* compare ) const;
  754. protected:
  755. XMLDeclaration( XMLDocument* doc );
  756. virtual ~XMLDeclaration();
  757. XMLDeclaration( const XMLDeclaration& ); // not supported
  758. XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
  759. };
  760. /** Any tag that tinyXml doesn't recognize is saved as an
  761. unknown. It is a tag of text, but should not be modified.
  762. It will be written back to the XML, unchanged, when the file
  763. is saved.
  764. DTD tags get thrown into TiXmlUnknowns.
  765. */
  766. class CC_DLL XMLUnknown : public XMLNode
  767. {
  768. friend class XMLDocument;
  769. public:
  770. virtual XMLUnknown* ToUnknown() {
  771. return this;
  772. }
  773. virtual const XMLUnknown* ToUnknown() const {
  774. return this;
  775. }
  776. virtual bool Accept( XMLVisitor* visitor ) const;
  777. char* ParseDeep( char*, StrPair* endTag );
  778. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  779. virtual bool ShallowEqual( const XMLNode* compare ) const;
  780. protected:
  781. XMLUnknown( XMLDocument* doc );
  782. virtual ~XMLUnknown();
  783. XMLUnknown( const XMLUnknown& ); // not supported
  784. XMLUnknown& operator=( const XMLUnknown& ); // not supported
  785. };
  786. enum XMLError {
  787. XML_NO_ERROR = 0,
  788. XML_SUCCESS = 0,
  789. XML_NO_ATTRIBUTE,
  790. XML_WRONG_ATTRIBUTE_TYPE,
  791. XML_ERROR_FILE_NOT_FOUND,
  792. XML_ERROR_FILE_COULD_NOT_BE_OPENED,
  793. XML_ERROR_FILE_READ_ERROR,
  794. XML_ERROR_ELEMENT_MISMATCH,
  795. XML_ERROR_PARSING_ELEMENT,
  796. XML_ERROR_PARSING_ATTRIBUTE,
  797. XML_ERROR_IDENTIFYING_TAG,
  798. XML_ERROR_PARSING_TEXT,
  799. XML_ERROR_PARSING_CDATA,
  800. XML_ERROR_PARSING_COMMENT,
  801. XML_ERROR_PARSING_DECLARATION,
  802. XML_ERROR_PARSING_UNKNOWN,
  803. XML_ERROR_EMPTY_DOCUMENT,
  804. XML_ERROR_MISMATCHED_ELEMENT,
  805. XML_ERROR_PARSING,
  806. XML_CAN_NOT_CONVERT_TEXT,
  807. XML_NO_TEXT_NODE
  808. };
  809. /** An attribute is a name-value pair. Elements have an arbitrary
  810. number of attributes, each with a unique name.
  811. @note The attributes are not XMLNodes. You may only query the
  812. Next() attribute in a list.
  813. */
  814. class CC_DLL XMLAttribute
  815. {
  816. friend class XMLElement;
  817. public:
  818. /// The name of the attribute.
  819. const char* Name() const {
  820. return _name.GetStr();
  821. }
  822. /// The value of the attribute.
  823. const char* Value() const {
  824. return _value.GetStr();
  825. }
  826. /// The next attribute in the list.
  827. const XMLAttribute* Next() const {
  828. return _next;
  829. }
  830. /** IntAttribute interprets the attribute as an integer, and returns the value.
  831. If the value isn't an integer, 0 will be returned. There is no error checking;
  832. use QueryIntAttribute() if you need error checking.
  833. */
  834. int IntValue() const {
  835. int i=0;
  836. QueryIntValue( &i );
  837. return i;
  838. }
  839. /// Query as an unsigned integer. See IntAttribute()
  840. unsigned UnsignedValue() const {
  841. unsigned i=0;
  842. QueryUnsignedValue( &i );
  843. return i;
  844. }
  845. /// Query as a boolean. See IntAttribute()
  846. bool BoolValue() const {
  847. bool b=false;
  848. QueryBoolValue( &b );
  849. return b;
  850. }
  851. /// Query as a double. See IntAttribute()
  852. double DoubleValue() const {
  853. double d=0;
  854. QueryDoubleValue( &d );
  855. return d;
  856. }
  857. /// Query as a float. See IntAttribute()
  858. float FloatValue() const {
  859. float f=0;
  860. QueryFloatValue( &f );
  861. return f;
  862. }
  863. /** QueryIntAttribute interprets the attribute as an integer, and returns the value
  864. in the provided paremeter. The function will return XML_NO_ERROR on success,
  865. and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
  866. */
  867. XMLError QueryIntValue( int* value ) const;
  868. /// See QueryIntAttribute
  869. XMLError QueryUnsignedValue( unsigned int* value ) const;
  870. /// See QueryIntAttribute
  871. XMLError QueryBoolValue( bool* value ) const;
  872. /// See QueryIntAttribute
  873. XMLError QueryDoubleValue( double* value ) const;
  874. /// See QueryIntAttribute
  875. XMLError QueryFloatValue( float* value ) const;
  876. /// Set the attribute to a string value.
  877. void SetAttribute( const char* value );
  878. /// Set the attribute to value.
  879. void SetAttribute( int value );
  880. /// Set the attribute to value.
  881. void SetAttribute( unsigned value );
  882. /// Set the attribute to value.
  883. void SetAttribute( bool value );
  884. /// Set the attribute to value.
  885. void SetAttribute( double value );
  886. /// Set the attribute to value.
  887. void SetAttribute( float value );
  888. private:
  889. enum { BUF_SIZE = 200 };
  890. XMLAttribute() : _next( 0 ) {}
  891. virtual ~XMLAttribute() {}
  892. XMLAttribute( const XMLAttribute& ); // not supported
  893. void operator=( const XMLAttribute& ); // not supported
  894. void SetName( const char* name );
  895. char* ParseDeep( char* p, bool processEntities );
  896. mutable StrPair _name;
  897. mutable StrPair _value;
  898. XMLAttribute* _next;
  899. MemPool* _memPool;
  900. };
  901. /** The element is a container class. It has a value, the element name,
  902. and can contain other elements, text, comments, and unknowns.
  903. Elements also contain an arbitrary number of attributes.
  904. */
  905. class CC_DLL XMLElement : public XMLNode
  906. {
  907. friend class XMLBase;
  908. friend class XMLDocument;
  909. public:
  910. /// Get the name of an element (which is the Value() of the node.)
  911. const char* Name() const {
  912. return Value();
  913. }
  914. /// Set the name of the element.
  915. void SetName( const char* str, bool staticMem=false ) {
  916. SetValue( str, staticMem );
  917. }
  918. virtual XMLElement* ToElement() {
  919. return this;
  920. }
  921. virtual const XMLElement* ToElement() const {
  922. return this;
  923. }
  924. virtual bool Accept( XMLVisitor* visitor ) const;
  925. /** Given an attribute name, Attribute() returns the value
  926. for the attribute of that name, or null if none
  927. exists. For example:
  928. @verbatim
  929. const char* value = ele->Attribute( "foo" );
  930. @endverbatim
  931. The 'value' parameter is normally null. However, if specified,
  932. the attribute will only be returned if the 'name' and 'value'
  933. match. This allow you to write code:
  934. @verbatim
  935. if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
  936. @endverbatim
  937. rather than:
  938. @verbatim
  939. if ( ele->Attribute( "foo" ) ) {
  940. if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
  941. }
  942. @endverbatim
  943. */
  944. const char* Attribute( const char* name, const char* value=0 ) const;
  945. /** Given an attribute name, IntAttribute() returns the value
  946. of the attribute interpreted as an integer. 0 will be
  947. returned if there is an error. For a method with error
  948. checking, see QueryIntAttribute()
  949. */
  950. int IntAttribute( const char* name ) const {
  951. int i=0;
  952. QueryIntAttribute( name, &i );
  953. return i;
  954. }
  955. /// See IntAttribute()
  956. unsigned UnsignedAttribute( const char* name ) const {
  957. unsigned i=0;
  958. QueryUnsignedAttribute( name, &i );
  959. return i;
  960. }
  961. /// See IntAttribute()
  962. bool BoolAttribute( const char* name ) const {
  963. bool b=false;
  964. QueryBoolAttribute( name, &b );
  965. return b;
  966. }
  967. /// See IntAttribute()
  968. double DoubleAttribute( const char* name ) const {
  969. double d=0;
  970. QueryDoubleAttribute( name, &d );
  971. return d;
  972. }
  973. /// See IntAttribute()
  974. float FloatAttribute( const char* name ) const {
  975. float f=0;
  976. QueryFloatAttribute( name, &f );
  977. return f;
  978. }
  979. /** Given an attribute name, QueryIntAttribute() returns
  980. XML_NO_ERROR, XML_WRONG_ATTRIBUTE_TYPE if the conversion
  981. can't be performed, or XML_NO_ATTRIBUTE if the attribute
  982. doesn't exist. If successful, the result of the conversion
  983. will be written to 'value'. If not successful, nothing will
  984. be written to 'value'. This allows you to provide default
  985. value:
  986. @verbatim
  987. int value = 10;
  988. QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
  989. @endverbatim
  990. */
  991. XMLError QueryIntAttribute( const char* name, int* value ) const {
  992. const XMLAttribute* a = FindAttribute( name );
  993. if ( !a ) {
  994. return XML_NO_ATTRIBUTE;
  995. }
  996. return a->QueryIntValue( value );
  997. }
  998. /// See QueryIntAttribute()
  999. XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
  1000. const XMLAttribute* a = FindAttribute( name );
  1001. if ( !a ) {
  1002. return XML_NO_ATTRIBUTE;
  1003. }
  1004. return a->QueryUnsignedValue( value );
  1005. }
  1006. /// See QueryIntAttribute()
  1007. XMLError QueryBoolAttribute( const char* name, bool* value ) const {
  1008. const XMLAttribute* a = FindAttribute( name );
  1009. if ( !a ) {
  1010. return XML_NO_ATTRIBUTE;
  1011. }
  1012. return a->QueryBoolValue( value );
  1013. }
  1014. /// See QueryIntAttribute()
  1015. XMLError QueryDoubleAttribute( const char* name, double* value ) const {
  1016. const XMLAttribute* a = FindAttribute( name );
  1017. if ( !a ) {
  1018. return XML_NO_ATTRIBUTE;
  1019. }
  1020. return a->QueryDoubleValue( value );
  1021. }
  1022. /// See QueryIntAttribute()
  1023. XMLError QueryFloatAttribute( const char* name, float* value ) const {
  1024. const XMLAttribute* a = FindAttribute( name );
  1025. if ( !a ) {
  1026. return XML_NO_ATTRIBUTE;
  1027. }
  1028. return a->QueryFloatValue( value );
  1029. }
  1030. /// Sets the named attribute to value.
  1031. void SetAttribute( const char* name, const char* value ) {
  1032. XMLAttribute* a = FindOrCreateAttribute( name );
  1033. a->SetAttribute( value );
  1034. }
  1035. /// Sets the named attribute to value.
  1036. void SetAttribute( const char* name, int value ) {
  1037. XMLAttribute* a = FindOrCreateAttribute( name );
  1038. a->SetAttribute( value );
  1039. }
  1040. /// Sets the named attribute to value.
  1041. void SetAttribute( const char* name, unsigned value ) {
  1042. XMLAttribute* a = FindOrCreateAttribute( name );
  1043. a->SetAttribute( value );
  1044. }
  1045. /// Sets the named attribute to value.
  1046. void SetAttribute( const char* name, bool value ) {
  1047. XMLAttribute* a = FindOrCreateAttribute( name );
  1048. a->SetAttribute( value );
  1049. }
  1050. /// Sets the named attribute to value.
  1051. void SetAttribute( const char* name, double value ) {
  1052. XMLAttribute* a = FindOrCreateAttribute( name );
  1053. a->SetAttribute( value );
  1054. }
  1055. /**
  1056. Delete an attribute.
  1057. */
  1058. void DeleteAttribute( const char* name );
  1059. /// Return the first attribute in the list.
  1060. const XMLAttribute* FirstAttribute() const {
  1061. return _rootAttribute;
  1062. }
  1063. /// Query a specific attribute in the list.
  1064. const XMLAttribute* FindAttribute( const char* name ) const;
  1065. /** Convenience function for easy access to the text inside an element. Although easy
  1066. and concise, GetText() is limited compared to getting the TiXmlText child
  1067. and accessing it directly.
  1068. If the first child of 'this' is a TiXmlText, the GetText()
  1069. returns the character string of the Text node, else null is returned.
  1070. This is a convenient method for getting the text of simple contained text:
  1071. @verbatim
  1072. <foo>This is text</foo>
  1073. const char* str = fooElement->GetText();
  1074. @endverbatim
  1075. 'str' will be a pointer to "This is text".
  1076. Note that this function can be misleading. If the element foo was created from
  1077. this XML:
  1078. @verbatim
  1079. <foo><b>This is text</b></foo>
  1080. @endverbatim
  1081. then the value of str would be null. The first child node isn't a text node, it is
  1082. another element. From this XML:
  1083. @verbatim
  1084. <foo>This is <b>text</b></foo>
  1085. @endverbatim
  1086. GetText() will return "This is ".
  1087. */
  1088. const char* GetText() const;
  1089. /**
  1090. Convenience method to query the value of a child text node. This is probably best
  1091. shown by example. Given you have a document is this form:
  1092. @verbatim
  1093. <point>
  1094. <x>1</x>
  1095. <y>1.4</y>
  1096. </point>
  1097. @endverbatim
  1098. The QueryIntText() and similar functions provide a safe and easier way to get to the
  1099. "value" of x and y.
  1100. @verbatim
  1101. int x = 0;
  1102. float y = 0; // types of x and y are contrived for example
  1103. const XMLElement* xElement = pointElement->FirstChildElement( "x" );
  1104. const XMLElement* yElement = pointElement->FirstChildElement( "y" );
  1105. xElement->QueryIntText( &x );
  1106. yElement->QueryFloatText( &y );
  1107. @endverbatim
  1108. @returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
  1109. to the requested type, and XML_NO_TEXT_NODE if there is no child text to query.
  1110. */
  1111. XMLError QueryIntText( int* ival ) const;
  1112. /// See QueryIntText()
  1113. XMLError QueryUnsignedText( unsigned* uval ) const;
  1114. /// See QueryIntText()
  1115. XMLError QueryBoolText( bool* bval ) const;
  1116. /// See QueryIntText()
  1117. XMLError QueryDoubleText( double* dval ) const;
  1118. /// See QueryIntText()
  1119. XMLError QueryFloatText( float* fval ) const;
  1120. // internal:
  1121. enum {
  1122. OPEN, // <foo>
  1123. CLOSED, // <foo/>
  1124. CLOSING // </foo>
  1125. };
  1126. int ClosingType() const {
  1127. return _closingType;
  1128. }
  1129. char* ParseDeep( char* p, StrPair* endTag );
  1130. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  1131. virtual bool ShallowEqual( const XMLNode* compare ) const;
  1132. private:
  1133. XMLElement( XMLDocument* doc );
  1134. virtual ~XMLElement();
  1135. XMLElement( const XMLElement& ); // not supported
  1136. void operator=( const XMLElement& ); // not supported
  1137. XMLAttribute* FindAttribute( const char* name );
  1138. XMLAttribute* FindOrCreateAttribute( const char* name );
  1139. //void LinkAttribute( XMLAttribute* attrib );
  1140. char* ParseAttributes( char* p );
  1141. int _closingType;
  1142. // The attribute list is ordered; there is no 'lastAttribute'
  1143. // because the list needs to be scanned for dupes before adding
  1144. // a new attribute.
  1145. XMLAttribute* _rootAttribute;
  1146. };
  1147. enum Whitespace {
  1148. PRESERVE_WHITESPACE,
  1149. COLLAPSE_WHITESPACE
  1150. };
  1151. /** A Document binds together all the functionality.
  1152. It can be saved, loaded, and printed to the screen.
  1153. All Nodes are connected and allocated to a Document.
  1154. If the Document is deleted, all its Nodes are also deleted.
  1155. */
  1156. class CC_DLL XMLDocument : public XMLNode
  1157. {
  1158. friend class XMLElement;
  1159. public:
  1160. /// constructor
  1161. XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE );
  1162. ~XMLDocument();
  1163. virtual XMLDocument* ToDocument() {
  1164. return this;
  1165. }
  1166. virtual const XMLDocument* ToDocument() const {
  1167. return this;
  1168. }
  1169. /**
  1170. Parse an XML file from a character string.
  1171. Returns XML_NO_ERROR (0) on success, or
  1172. an errorID.
  1173. You may optionally pass in the 'nBytes', which is
  1174. the number of bytes which will be parsed. If not
  1175. specified, TinyXML will assume 'xml' points to a
  1176. null terminated string.
  1177. */
  1178. XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
  1179. /**
  1180. Load an XML file from disk.
  1181. Returns XML_NO_ERROR (0) on success, or
  1182. an errorID.
  1183. */
  1184. XMLError LoadFile( const char* filename );
  1185. /**
  1186. Load an XML file from disk. You are responsible
  1187. for providing and closing the FILE*.
  1188. Returns XML_NO_ERROR (0) on success, or
  1189. an errorID.
  1190. */
  1191. XMLError LoadFile( FILE* );
  1192. /**
  1193. Save the XML file to disk.
  1194. Returns XML_NO_ERROR (0) on success, or
  1195. an errorID.
  1196. */
  1197. XMLError SaveFile( const char* filename, bool compact = false );
  1198. /**
  1199. Save the XML file to disk. You are responsible
  1200. for providing and closing the FILE*.
  1201. Returns XML_NO_ERROR (0) on success, or
  1202. an errorID.
  1203. */
  1204. XMLError SaveFile( FILE* fp, bool compact = false );
  1205. bool ProcessEntities() const {
  1206. return _processEntities;
  1207. }
  1208. Whitespace WhitespaceMode() const {
  1209. return _whitespace;
  1210. }
  1211. /**
  1212. Returns true if this document has a leading Byte Order Mark of UTF8.
  1213. */
  1214. bool HasBOM() const {
  1215. return _writeBOM;
  1216. }
  1217. /** Sets whether to write the BOM when writing the file.
  1218. */
  1219. void SetBOM( bool useBOM ) {
  1220. _writeBOM = useBOM;
  1221. }
  1222. /** Return the root element of DOM. Equivalent to FirstChildElement().
  1223. To get the first node, use FirstChild().
  1224. */
  1225. XMLElement* RootElement() {
  1226. return FirstChildElement();
  1227. }
  1228. const XMLElement* RootElement() const {
  1229. return FirstChildElement();
  1230. }
  1231. /** Print the Document. If the Printer is not provided, it will
  1232. print to stdout. If you provide Printer, this can print to a file:
  1233. @verbatim
  1234. XMLPrinter printer( fp );
  1235. doc.Print( &printer );
  1236. @endverbatim
  1237. Or you can use a printer to print to memory:
  1238. @verbatim
  1239. XMLPrinter printer;
  1240. doc->Print( &printer );
  1241. // printer.CStr() has a const char* to the XML
  1242. @endverbatim
  1243. */
  1244. void Print( XMLPrinter* streamer=0 );
  1245. virtual bool Accept( XMLVisitor* visitor ) const;
  1246. /**
  1247. Create a new Element associated with
  1248. this Document. The memory for the Element
  1249. is managed by the Document.
  1250. */
  1251. XMLElement* NewElement( const char* name );
  1252. /**
  1253. Create a new Comment associated with
  1254. this Document. The memory for the Comment
  1255. is managed by the Document.
  1256. */
  1257. XMLComment* NewComment( const char* comment );
  1258. /**
  1259. Create a new Text associated with
  1260. this Document. The memory for the Text
  1261. is managed by the Document.
  1262. */
  1263. XMLText* NewText( const char* text );
  1264. /**
  1265. Create a new Declaration associated with
  1266. this Document. The memory for the object
  1267. is managed by the Document.
  1268. If the 'text' param is null, the standard
  1269. declaration is used.:
  1270. @verbatim
  1271. <?xml version="1.0" encoding="UTF-8"?>
  1272. @endverbatim
  1273. */
  1274. XMLDeclaration* NewDeclaration( const char* text=0 );
  1275. /**
  1276. Create a new Unknown associated with
  1277. this Document. The memory forthe object
  1278. is managed by the Document.
  1279. */
  1280. XMLUnknown* NewUnknown( const char* text );
  1281. /**
  1282. Delete a node associated with this document.
  1283. It will be unlinked from the DOM.
  1284. */
  1285. void DeleteNode( XMLNode* node ) {
  1286. node->_parent->DeleteChild( node );
  1287. }
  1288. void SetError( XMLError error, const char* str1, const char* str2 );
  1289. /// Return true if there was an error parsing the document.
  1290. bool Error() const {
  1291. return _errorID != XML_NO_ERROR;
  1292. }
  1293. /// Return the errorID.
  1294. XMLError ErrorID() const {
  1295. return _errorID;
  1296. }
  1297. /// Return a possibly helpful diagnostic location or string.
  1298. const char* GetErrorStr1() const {
  1299. return _errorStr1;
  1300. }
  1301. /// Return a possibly helpful secondary diagnostic location or string.
  1302. const char* GetErrorStr2() const {
  1303. return _errorStr2;
  1304. }
  1305. /// If there is an error, print it to stdout.
  1306. void PrintError() const;
  1307. // internal
  1308. char* Identify( char* p, XMLNode** node );
  1309. virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
  1310. return 0;
  1311. }
  1312. virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
  1313. return false;
  1314. }
  1315. private:
  1316. XMLDocument( const XMLDocument& ); // not supported
  1317. void operator=( const XMLDocument& ); // not supported
  1318. void InitDocument();
  1319. bool _writeBOM;
  1320. bool _processEntities;
  1321. XMLError _errorID;
  1322. Whitespace _whitespace;
  1323. const char* _errorStr1;
  1324. const char* _errorStr2;
  1325. char* _charBuffer;
  1326. MemPoolT< sizeof(XMLElement) > _elementPool;
  1327. MemPoolT< sizeof(XMLAttribute) > _attributePool;
  1328. MemPoolT< sizeof(XMLText) > _textPool;
  1329. MemPoolT< sizeof(XMLComment) > _commentPool;
  1330. };
  1331. /**
  1332. A XMLHandle is a class that wraps a node pointer with null checks; this is
  1333. an incredibly useful thing. Note that XMLHandle is not part of the TinyXML
  1334. DOM structure. It is a separate utility class.
  1335. Take an example:
  1336. @verbatim
  1337. <Document>
  1338. <Element attributeA = "valueA">
  1339. <Child attributeB = "value1" />
  1340. <Child attributeB = "value2" />
  1341. </Element>
  1342. </Document>
  1343. @endverbatim
  1344. Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
  1345. easy to write a *lot* of code that looks like:
  1346. @verbatim
  1347. XMLElement* root = document.FirstChildElement( "Document" );
  1348. if ( root )
  1349. {
  1350. XMLElement* element = root->FirstChildElement( "Element" );
  1351. if ( element )
  1352. {
  1353. XMLElement* child = element->FirstChildElement( "Child" );
  1354. if ( child )
  1355. {
  1356. XMLElement* child2 = child->NextSiblingElement( "Child" );
  1357. if ( child2 )
  1358. {
  1359. // Finally do something useful.
  1360. @endverbatim
  1361. And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
  1362. of such code. A XMLHandle checks for null pointers so it is perfectly safe
  1363. and correct to use:
  1364. @verbatim
  1365. XMLHandle docHandle( &document );
  1366. XMLElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild().NextSibling().ToElement();
  1367. if ( child2 )
  1368. {
  1369. // do something useful
  1370. @endverbatim
  1371. Which is MUCH more concise and useful.
  1372. It is also safe to copy handles - internally they are nothing more than node pointers.
  1373. @verbatim
  1374. XMLHandle handleCopy = handle;
  1375. @endverbatim
  1376. See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
  1377. */
  1378. class CC_DLL XMLHandle
  1379. {
  1380. public:
  1381. /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
  1382. XMLHandle( XMLNode* node ) {
  1383. _node = node;
  1384. }
  1385. /// Create a handle from a node.
  1386. XMLHandle( XMLNode& node ) {
  1387. _node = &node;
  1388. }
  1389. /// Copy constructor
  1390. XMLHandle( const XMLHandle& ref ) {
  1391. _node = ref._node;
  1392. }
  1393. /// Assignment
  1394. XMLHandle& operator=( const XMLHandle& ref ) {
  1395. _node = ref._node;
  1396. return *this;
  1397. }
  1398. /// Get the first child of this handle.
  1399. XMLHandle FirstChild() {
  1400. return XMLHandle( _node ? _node->FirstChild() : 0 );
  1401. }
  1402. /// Get the first child element of this handle.
  1403. XMLHandle FirstChildElement( const char* value=0 ) {
  1404. return XMLHandle( _node ? _node->FirstChildElement( value ) : 0 );
  1405. }
  1406. /// Get the last child of this handle.
  1407. XMLHandle LastChild() {
  1408. return XMLHandle( _node ? _node->LastChild() : 0 );
  1409. }
  1410. /// Get the last child element of this handle.
  1411. XMLHandle LastChildElement( const char* _value=0 ) {
  1412. return XMLHandle( _node ? _node->LastChildElement( _value ) : 0 );
  1413. }
  1414. /// Get the previous sibling of this handle.
  1415. XMLHandle PreviousSibling() {
  1416. return XMLHandle( _node ? _node->PreviousSibling() : 0 );
  1417. }
  1418. /// Get the previous sibling element of this handle.
  1419. XMLHandle PreviousSiblingElement( const char* _value=0 ) {
  1420. return XMLHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
  1421. }
  1422. /// Get the next sibling of this handle.
  1423. XMLHandle NextSibling() {
  1424. return XMLHandle( _node ? _node->NextSibling() : 0 );
  1425. }
  1426. /// Get the next sibling element of this handle.
  1427. XMLHandle NextSiblingElement( const char* _value=0 ) {
  1428. return XMLHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
  1429. }
  1430. /// Safe cast to XMLNode. This can return null.
  1431. XMLNode* ToNode() {
  1432. return _node;
  1433. }
  1434. /// Safe cast to XMLElement. This can return null.
  1435. XMLElement* ToElement() {
  1436. return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 );
  1437. }
  1438. /// Safe cast to XMLText. This can return null.
  1439. XMLText* ToText() {
  1440. return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 );
  1441. }
  1442. /// Safe cast to XMLUnknown. This can return null.
  1443. XMLUnknown* ToUnknown() {
  1444. return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 );
  1445. }
  1446. /// Safe cast to XMLDeclaration. This can return null.
  1447. XMLDeclaration* ToDeclaration() {
  1448. return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 );
  1449. }
  1450. private:
  1451. XMLNode* _node;
  1452. };
  1453. /**
  1454. A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
  1455. same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
  1456. */
  1457. class CC_DLL XMLConstHandle
  1458. {
  1459. public:
  1460. XMLConstHandle( const XMLNode* node ) {
  1461. _node = node;
  1462. }
  1463. XMLConstHandle( const XMLNode& node ) {
  1464. _node = &node;
  1465. }
  1466. XMLConstHandle( const XMLConstHandle& ref ) {
  1467. _node = ref._node;
  1468. }
  1469. XMLConstHandle& operator=( const XMLConstHandle& ref ) {
  1470. _node = ref._node;
  1471. return *this;
  1472. }
  1473. const XMLConstHandle FirstChild() const {
  1474. return XMLConstHandle( _node ? _node->FirstChild() : 0 );
  1475. }
  1476. const XMLConstHandle FirstChildElement( const char* value=0 ) const {
  1477. return XMLConstHandle( _node ? _node->FirstChildElement( value ) : 0 );
  1478. }
  1479. const XMLConstHandle LastChild() const {
  1480. return XMLConstHandle( _node ? _node->LastChild() : 0 );
  1481. }
  1482. const XMLConstHandle LastChildElement( const char* _value=0 ) const {
  1483. return XMLConstHandle( _node ? _node->LastChildElement( _value ) : 0 );
  1484. }
  1485. const XMLConstHandle PreviousSibling() const {
  1486. return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
  1487. }
  1488. const XMLConstHandle PreviousSiblingElement( const char* _value=0 ) const {
  1489. return XMLConstHandle( _node ? _node->PreviousSiblingElement( _value ) : 0 );
  1490. }
  1491. const XMLConstHandle NextSibling() const {
  1492. return XMLConstHandle( _node ? _node->NextSibling() : 0 );
  1493. }
  1494. const XMLConstHandle NextSiblingElement( const char* _value=0 ) const {
  1495. return XMLConstHandle( _node ? _node->NextSiblingElement( _value ) : 0 );
  1496. }
  1497. const XMLNode* ToNode() const {
  1498. return _node;
  1499. }
  1500. const XMLElement* ToElement() const {
  1501. return ( ( _node && _node->ToElement() ) ? _node->ToElement() : 0 );
  1502. }
  1503. const XMLText* ToText() const {
  1504. return ( ( _node && _node->ToText() ) ? _node->ToText() : 0 );
  1505. }
  1506. const XMLUnknown* ToUnknown() const {
  1507. return ( ( _node && _node->ToUnknown() ) ? _node->ToUnknown() : 0 );
  1508. }
  1509. const XMLDeclaration* ToDeclaration() const {
  1510. return ( ( _node && _node->ToDeclaration() ) ? _node->ToDeclaration() : 0 );
  1511. }
  1512. private:
  1513. const XMLNode* _node;
  1514. };
  1515. /**
  1516. Printing functionality. The XMLPrinter gives you more
  1517. options than the XMLDocument::Print() method.
  1518. It can:
  1519. -# Print to memory.
  1520. -# Print to a file you provide.
  1521. -# Print XML without a XMLDocument.
  1522. Print to Memory
  1523. @verbatim
  1524. XMLPrinter printer;
  1525. doc->Print( &printer );
  1526. SomeFunction( printer.CStr() );
  1527. @endverbatim
  1528. Print to a File
  1529. You provide the file pointer.
  1530. @verbatim
  1531. XMLPrinter printer( fp );
  1532. doc.Print( &printer );
  1533. @endverbatim
  1534. Print without a XMLDocument
  1535. When loading, an XML parser is very useful. However, sometimes
  1536. when saving, it just gets in the way. The code is often set up
  1537. for streaming, and constructing the DOM is just overhead.
  1538. The Printer supports the streaming case. The following code
  1539. prints out a trivially simple XML file without ever creating
  1540. an XML document.
  1541. @verbatim
  1542. XMLPrinter printer( fp );
  1543. printer.OpenElement( "foo" );
  1544. printer.PushAttribute( "foo", "bar" );
  1545. printer.CloseElement();
  1546. @endverbatim
  1547. */
  1548. class CC_DLL XMLPrinter : public XMLVisitor
  1549. {
  1550. public:
  1551. /** Construct the printer. If the FILE* is specified,
  1552. this will print to the FILE. Else it will print
  1553. to memory, and the result is available in CStr().
  1554. If 'compact' is set to true, then output is created
  1555. with only required whitespace and newlines.
  1556. */
  1557. XMLPrinter( FILE* file=0, bool compact = false );
  1558. ~XMLPrinter() {}
  1559. /** If streaming, write the BOM and declaration. */
  1560. void PushHeader( bool writeBOM, bool writeDeclaration );
  1561. /** If streaming, start writing an element.
  1562. The element must be closed with CloseElement()
  1563. */
  1564. void OpenElement( const char* name );
  1565. /// If streaming, add an attribute to an open element.
  1566. void PushAttribute( const char* name, const char* value );
  1567. void PushAttribute( const char* name, int value );
  1568. void PushAttribute( const char* name, unsigned value );
  1569. void PushAttribute( const char* name, bool value );
  1570. void PushAttribute( const char* name, double value );
  1571. /// If streaming, close the Element.
  1572. void CloseElement();
  1573. /// Add a text node.
  1574. void PushText( const char* text, bool cdata=false );
  1575. /// Add a text node from an integer.
  1576. void PushText( int value );
  1577. /// Add a text node from an unsigned.
  1578. void PushText( unsigned value );
  1579. /// Add a text node from a bool.
  1580. void PushText( bool value );
  1581. /// Add a text node from a float.
  1582. void PushText( float value );
  1583. /// Add a text node from a double.
  1584. void PushText( double value );
  1585. /// Add a comment
  1586. void PushComment( const char* comment );
  1587. void PushDeclaration( const char* value );
  1588. void PushUnknown( const char* value );
  1589. virtual bool VisitEnter( const XMLDocument& /*doc*/ );
  1590. virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
  1591. return true;
  1592. }
  1593. virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
  1594. virtual bool VisitExit( const XMLElement& element );
  1595. virtual bool Visit( const XMLText& text );
  1596. virtual bool Visit( const XMLComment& comment );
  1597. virtual bool Visit( const XMLDeclaration& declaration );
  1598. virtual bool Visit( const XMLUnknown& unknown );
  1599. /**
  1600. If in print to memory mode, return a pointer to
  1601. the XML file in memory.
  1602. */
  1603. const char* CStr() const {
  1604. return _buffer.Mem();
  1605. }
  1606. /**
  1607. If in print to memory mode, return the size
  1608. of the XML file in memory. (Note the size returned
  1609. includes the terminating null.)
  1610. */
  1611. int CStrSize() const {
  1612. return _buffer.Size();
  1613. }
  1614. private:
  1615. void SealElement();
  1616. void PrintSpace( int depth );
  1617. void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
  1618. void Print( const char* format, ... );
  1619. bool _elementJustOpened;
  1620. bool _firstElement;
  1621. FILE* _fp;
  1622. int _depth;
  1623. int _textDepth;
  1624. bool _processEntities;
  1625. bool _compactMode;
  1626. enum {
  1627. ENTITY_RANGE = 64,
  1628. BUF_SIZE = 200
  1629. };
  1630. bool _entityFlag[ENTITY_RANGE];
  1631. bool _restrictedEntityFlag[ENTITY_RANGE];
  1632. DynArray< const char*, 10 > _stack;
  1633. DynArray< char, 20 > _buffer;
  1634. #ifdef _MSC_VER
  1635. DynArray< char, 20 > _accumulator;
  1636. #endif
  1637. };
  1638. } // tinyxml2
  1639. #endif // TINYXML2_INCLUDED