tinyxml2.h 59 KB

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