tinyxml2.h 61 KB

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