tinyxml2.h 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  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__) || defined(__QNXNTO__)
  21. # include <ctype.h>
  22. # include <limits.h>
  23. # include <stdio.h>
  24. # include <stdlib.h>
  25. # include <string.h>
  26. #else
  27. # include <cctype>
  28. # include <climits>
  29. # include <cstdio>
  30. # include <cstdlib>
  31. # include <cstring>
  32. #endif
  33. /*
  34. TODO: intern strings instead of allocation.
  35. */
  36. /*
  37. gcc:
  38. g++ -Wall -DDEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
  39. Formatting, Artistic Style:
  40. AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
  41. */
  42. #if defined( _DEBUG ) || defined( DEBUG ) || defined (__DEBUG__)
  43. # ifndef DEBUG
  44. # define DEBUG
  45. # endif
  46. #endif
  47. #ifdef _MSC_VER
  48. # pragma warning(push)
  49. # pragma warning(disable: 4251)
  50. #endif
  51. #ifdef _WIN32
  52. # ifdef TINYXML2_EXPORT
  53. # define TINYXML2_LIB __declspec(dllexport)
  54. # elif defined(TINYXML2_IMPORT)
  55. # define TINYXML2_LIB __declspec(dllimport)
  56. # else
  57. # define TINYXML2_LIB
  58. # endif
  59. #else
  60. # define TINYXML2_LIB
  61. #endif
  62. #if defined(DEBUG)
  63. # if defined(_MSC_VER)
  64. # // "(void)0," is for suppressing C4127 warning in "assert(false)", "assert(true)" and the like
  65. # define TIXMLASSERT( x ) if ( !((void)0,(x))) { __debugbreak(); }
  66. # elif defined (ANDROID_NDK)
  67. # include <android/log.h>
  68. # define TIXMLASSERT( x ) if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); }
  69. # else
  70. # include <assert.h>
  71. # define TIXMLASSERT assert
  72. # endif
  73. #else
  74. # define TIXMLASSERT( x ) {}
  75. #endif
  76. /* Versioning, past 1.0.14:
  77. http://semver.org/
  78. */
  79. static const int TIXML2_MAJOR_VERSION = 3;
  80. static const int TIXML2_MINOR_VERSION = 0;
  81. static const int TIXML2_PATCH_VERSION = 0;
  82. namespace tinyxml2
  83. {
  84. class XMLDocument;
  85. class XMLElement;
  86. class XMLAttribute;
  87. class XMLComment;
  88. class XMLText;
  89. class XMLDeclaration;
  90. class XMLUnknown;
  91. class XMLPrinter;
  92. /*
  93. A class that wraps strings. Normally stores the start and end
  94. pointers into the XML file itself, and will apply normalization
  95. and entity translation if actually read. Can also store (and memory
  96. manage) a traditional char[]
  97. */
  98. class StrPair
  99. {
  100. public:
  101. enum {
  102. NEEDS_ENTITY_PROCESSING = 0x01,
  103. NEEDS_NEWLINE_NORMALIZATION = 0x02,
  104. NEEDS_WHITESPACE_COLLAPSING = 0x04,
  105. TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
  106. TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
  107. ATTRIBUTE_NAME = 0,
  108. ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
  109. ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
  110. COMMENT = NEEDS_NEWLINE_NORMALIZATION
  111. };
  112. StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
  113. ~StrPair();
  114. void Set( char* start, char* end, int flags ) {
  115. Reset();
  116. _start = start;
  117. _end = end;
  118. _flags = flags | NEEDS_FLUSH;
  119. }
  120. const char* GetStr();
  121. bool Empty() const {
  122. return _start == _end;
  123. }
  124. void SetInternedStr( const char* str ) {
  125. Reset();
  126. _start = const_cast<char*>(str);
  127. }
  128. void SetStr( const char* str, int flags=0 );
  129. char* ParseText( char* in, const char* endTag, int strFlags );
  130. char* ParseName( char* in );
  131. void TransferTo( StrPair* other );
  132. private:
  133. void Reset();
  134. void CollapseWhitespace();
  135. enum {
  136. NEEDS_FLUSH = 0x100,
  137. NEEDS_DELETE = 0x200
  138. };
  139. // After parsing, if *_end != 0, it can be set to zero.
  140. int _flags;
  141. char* _start;
  142. char* _end;
  143. StrPair( const StrPair& other ); // not supported
  144. void operator=( StrPair& other ); // not supported, use TransferTo()
  145. };
  146. /*
  147. A dynamic array of Plain Old Data. Doesn't support constructors, etc.
  148. Has a small initial memory pool, so that low or no usage will not
  149. cause a call to new/delete
  150. */
  151. template <class T, int INITIAL_SIZE>
  152. class DynArray
  153. {
  154. public:
  155. DynArray() {
  156. _mem = _pool;
  157. _allocated = INITIAL_SIZE;
  158. _size = 0;
  159. }
  160. ~DynArray() {
  161. if ( _mem != _pool ) {
  162. delete [] _mem;
  163. }
  164. }
  165. void Clear() {
  166. _size = 0;
  167. }
  168. void Push( T t ) {
  169. TIXMLASSERT( _size < INT_MAX );
  170. EnsureCapacity( _size+1 );
  171. _mem[_size++] = t;
  172. }
  173. T* PushArr( int count ) {
  174. TIXMLASSERT( count >= 0 );
  175. TIXMLASSERT( _size <= INT_MAX - count );
  176. EnsureCapacity( _size+count );
  177. T* ret = &_mem[_size];
  178. _size += count;
  179. return ret;
  180. }
  181. T Pop() {
  182. TIXMLASSERT( _size > 0 );
  183. return _mem[--_size];
  184. }
  185. void PopArr( int count ) {
  186. TIXMLASSERT( _size >= count );
  187. _size -= count;
  188. }
  189. bool Empty() const {
  190. return _size == 0;
  191. }
  192. T& operator[](int i) {
  193. TIXMLASSERT( i>= 0 && i < _size );
  194. return _mem[i];
  195. }
  196. const T& operator[](int i) const {
  197. TIXMLASSERT( i>= 0 && i < _size );
  198. return _mem[i];
  199. }
  200. const T& PeekTop() const {
  201. TIXMLASSERT( _size > 0 );
  202. return _mem[ _size - 1];
  203. }
  204. int Size() const {
  205. TIXMLASSERT( _size >= 0 );
  206. return _size;
  207. }
  208. int Capacity() const {
  209. TIXMLASSERT( _allocated >= INITIAL_SIZE );
  210. return _allocated;
  211. }
  212. const T* Mem() const {
  213. TIXMLASSERT( _mem );
  214. return _mem;
  215. }
  216. T* Mem() {
  217. TIXMLASSERT( _mem );
  218. return _mem;
  219. }
  220. private:
  221. DynArray( const DynArray& ); // not supported
  222. void operator=( const DynArray& ); // not supported
  223. void EnsureCapacity( int cap ) {
  224. TIXMLASSERT( cap > 0 );
  225. if ( cap > _allocated ) {
  226. TIXMLASSERT( cap <= INT_MAX / 2 );
  227. int newAllocated = cap * 2;
  228. T* newMem = new T[newAllocated];
  229. memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs
  230. if ( _mem != _pool ) {
  231. delete [] _mem;
  232. }
  233. _mem = newMem;
  234. _allocated = newAllocated;
  235. }
  236. }
  237. T* _mem;
  238. T _pool[INITIAL_SIZE];
  239. int _allocated; // objects allocated
  240. int _size; // number objects in use
  241. };
  242. /*
  243. Parent virtual class of a pool for fast allocation
  244. and deallocation of objects.
  245. */
  246. class MemPool
  247. {
  248. public:
  249. MemPool() {}
  250. virtual ~MemPool() {}
  251. virtual int ItemSize() const = 0;
  252. virtual void* Alloc() = 0;
  253. virtual void Free( void* ) = 0;
  254. virtual void SetTracked() = 0;
  255. virtual void Clear() = 0;
  256. };
  257. /*
  258. Template child class to create pools of the correct type.
  259. */
  260. template< int SIZE >
  261. class MemPoolT : public MemPool
  262. {
  263. public:
  264. MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
  265. ~MemPoolT() {
  266. Clear();
  267. }
  268. void Clear() {
  269. // Delete the blocks.
  270. while( !_blockPtrs.Empty()) {
  271. Block* b = _blockPtrs.Pop();
  272. delete b;
  273. }
  274. _root = 0;
  275. _currentAllocs = 0;
  276. _nAllocs = 0;
  277. _maxAllocs = 0;
  278. _nUntracked = 0;
  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 = static_cast<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. MemPoolT( const MemPoolT& ); // not supported
  341. void operator=( const MemPoolT& ); // not supported
  342. union Chunk {
  343. Chunk* next;
  344. char mem[SIZE];
  345. };
  346. struct Block {
  347. Chunk chunk[COUNT];
  348. };
  349. DynArray< Block*, 10 > _blockPtrs;
  350. Chunk* _root;
  351. int _currentAllocs;
  352. int _nAllocs;
  353. int _maxAllocs;
  354. int _nUntracked;
  355. };
  356. /**
  357. Implements the interface to the "Visitor pattern" (see the Accept() method.)
  358. If you call the Accept() method, it requires being passed a XMLVisitor
  359. class to handle callbacks. For nodes that contain other nodes (Document, Element)
  360. you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
  361. are simply called with Visit().
  362. If you return 'true' from a Visit method, recursive parsing will continue. If you return
  363. false, <b>no children of this node or its siblings</b> will be visited.
  364. All flavors of Visit methods have a default implementation that returns 'true' (continue
  365. visiting). You need to only override methods that are interesting to you.
  366. Generally Accept() is called on the XMLDocument, although all nodes support visiting.
  367. You should never change the document from a callback.
  368. @sa XMLNode::Accept()
  369. */
  370. class TINYXML2_LIB XMLVisitor
  371. {
  372. public:
  373. virtual ~XMLVisitor() {}
  374. /// Visit a document.
  375. virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
  376. return true;
  377. }
  378. /// Visit a document.
  379. virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
  380. return true;
  381. }
  382. /// Visit an element.
  383. virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
  384. return true;
  385. }
  386. /// Visit an element.
  387. virtual bool VisitExit( const XMLElement& /*element*/ ) {
  388. return true;
  389. }
  390. /// Visit a declaration.
  391. virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
  392. return true;
  393. }
  394. /// Visit a text node.
  395. virtual bool Visit( const XMLText& /*text*/ ) {
  396. return true;
  397. }
  398. /// Visit a comment node.
  399. virtual bool Visit( const XMLComment& /*comment*/ ) {
  400. return true;
  401. }
  402. /// Visit an unknown node.
  403. virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
  404. return true;
  405. }
  406. };
  407. // WARNING: must match XMLDocument::_errorNames[]
  408. enum XMLError {
  409. XML_SUCCESS = 0,
  410. XML_NO_ERROR = 0,
  411. XML_NO_ATTRIBUTE,
  412. XML_WRONG_ATTRIBUTE_TYPE,
  413. XML_ERROR_FILE_NOT_FOUND,
  414. XML_ERROR_FILE_COULD_NOT_BE_OPENED,
  415. XML_ERROR_FILE_READ_ERROR,
  416. XML_ERROR_ELEMENT_MISMATCH,
  417. XML_ERROR_PARSING_ELEMENT,
  418. XML_ERROR_PARSING_ATTRIBUTE,
  419. XML_ERROR_IDENTIFYING_TAG,
  420. XML_ERROR_PARSING_TEXT,
  421. XML_ERROR_PARSING_CDATA,
  422. XML_ERROR_PARSING_COMMENT,
  423. XML_ERROR_PARSING_DECLARATION,
  424. XML_ERROR_PARSING_UNKNOWN,
  425. XML_ERROR_EMPTY_DOCUMENT,
  426. XML_ERROR_MISMATCHED_ELEMENT,
  427. XML_ERROR_PARSING,
  428. XML_CAN_NOT_CONVERT_TEXT,
  429. XML_NO_TEXT_NODE,
  430. XML_ERROR_COUNT
  431. };
  432. /*
  433. Utility functionality.
  434. */
  435. class XMLUtil
  436. {
  437. public:
  438. static const char* SkipWhiteSpace( const char* p ) {
  439. TIXMLASSERT( p );
  440. while( IsWhiteSpace(*p) ) {
  441. ++p;
  442. }
  443. TIXMLASSERT( p );
  444. return p;
  445. }
  446. static char* SkipWhiteSpace( char* p ) {
  447. return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p) ) );
  448. }
  449. // Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
  450. // correct, but simple, and usually works.
  451. static bool IsWhiteSpace( char p ) {
  452. return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
  453. }
  454. inline static bool IsNameStartChar( unsigned char ch ) {
  455. if ( ch >= 128 ) {
  456. // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
  457. return true;
  458. }
  459. if ( isalpha( ch ) ) {
  460. return true;
  461. }
  462. return ch == ':' || ch == '_';
  463. }
  464. inline static bool IsNameChar( unsigned char ch ) {
  465. return IsNameStartChar( ch )
  466. || isdigit( ch )
  467. || ch == '.'
  468. || ch == '-';
  469. }
  470. inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
  471. if ( p == q ) {
  472. return true;
  473. }
  474. return strncmp( p, q, nChar ) == 0;
  475. }
  476. inline static bool IsUTF8Continuation( char p ) {
  477. return ( p & 0x80 ) != 0;
  478. }
  479. static const char* ReadBOM( const char* p, bool* hasBOM );
  480. // p is the starting location,
  481. // the UTF-8 value of the entity will be placed in value, and length filled in.
  482. static const char* GetCharacterRef( const char* p, char* value, int* length );
  483. static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
  484. // converts primitive types to strings
  485. static void ToStr( int v, char* buffer, int bufferSize );
  486. static void ToStr( unsigned v, char* buffer, int bufferSize );
  487. static void ToStr( bool v, char* buffer, int bufferSize );
  488. static void ToStr( float v, char* buffer, int bufferSize );
  489. static void ToStr( double v, char* buffer, int bufferSize );
  490. // converts strings to primitive types
  491. static bool ToInt( const char* str, int* value );
  492. static bool ToUnsigned( const char* str, unsigned* value );
  493. static bool ToBool( const char* str, bool* value );
  494. static bool ToFloat( const char* str, float* value );
  495. static bool ToDouble( const char* str, double* value );
  496. };
  497. /** XMLNode is a base class for every object that is in the
  498. XML Document Object Model (DOM), except XMLAttributes.
  499. Nodes have siblings, a parent, and children which can
  500. be navigated. A node is always in a XMLDocument.
  501. The type of a XMLNode can be queried, and it can
  502. be cast to its more defined type.
  503. A XMLDocument allocates memory for all its Nodes.
  504. When the XMLDocument gets deleted, all its Nodes
  505. will also be deleted.
  506. @verbatim
  507. A Document can contain: Element (container or leaf)
  508. Comment (leaf)
  509. Unknown (leaf)
  510. Declaration( leaf )
  511. An Element can contain: Element (container or leaf)
  512. Text (leaf)
  513. Attributes (not on tree)
  514. Comment (leaf)
  515. Unknown (leaf)
  516. @endverbatim
  517. */
  518. class TINYXML2_LIB XMLNode
  519. {
  520. friend class XMLDocument;
  521. friend class XMLElement;
  522. public:
  523. /// Get the XMLDocument that owns this XMLNode.
  524. const XMLDocument* GetDocument() const {
  525. TIXMLASSERT( _document );
  526. return _document;
  527. }
  528. /// Get the XMLDocument that owns this XMLNode.
  529. XMLDocument* GetDocument() {
  530. TIXMLASSERT( _document );
  531. return _document;
  532. }
  533. /// Safely cast to an Element, or null.
  534. virtual XMLElement* ToElement() {
  535. return 0;
  536. }
  537. /// Safely cast to Text, or null.
  538. virtual XMLText* ToText() {
  539. return 0;
  540. }
  541. /// Safely cast to a Comment, or null.
  542. virtual XMLComment* ToComment() {
  543. return 0;
  544. }
  545. /// Safely cast to a Document, or null.
  546. virtual XMLDocument* ToDocument() {
  547. return 0;
  548. }
  549. /// Safely cast to a Declaration, or null.
  550. virtual XMLDeclaration* ToDeclaration() {
  551. return 0;
  552. }
  553. /// Safely cast to an Unknown, or null.
  554. virtual XMLUnknown* ToUnknown() {
  555. return 0;
  556. }
  557. virtual const XMLElement* ToElement() const {
  558. return 0;
  559. }
  560. virtual const XMLText* ToText() const {
  561. return 0;
  562. }
  563. virtual const XMLComment* ToComment() const {
  564. return 0;
  565. }
  566. virtual const XMLDocument* ToDocument() const {
  567. return 0;
  568. }
  569. virtual const XMLDeclaration* ToDeclaration() const {
  570. return 0;
  571. }
  572. virtual const XMLUnknown* ToUnknown() const {
  573. return 0;
  574. }
  575. /** The meaning of 'value' changes for the specific type.
  576. @verbatim
  577. Document: empty (NULL is returned, not an empty string)
  578. Element: name of the element
  579. Comment: the comment text
  580. Unknown: the tag contents
  581. Text: the text string
  582. @endverbatim
  583. */
  584. const char* Value() const;
  585. /** Set the Value of an XML node.
  586. @sa Value()
  587. */
  588. void SetValue( const char* val, bool staticMem=false );
  589. /// Get the parent of this node on the DOM.
  590. const XMLNode* Parent() const {
  591. return _parent;
  592. }
  593. XMLNode* Parent() {
  594. return _parent;
  595. }
  596. /// Returns true if this node has no children.
  597. bool NoChildren() const {
  598. return !_firstChild;
  599. }
  600. /// Get the first child node, or null if none exists.
  601. const XMLNode* FirstChild() const {
  602. return _firstChild;
  603. }
  604. XMLNode* FirstChild() {
  605. return _firstChild;
  606. }
  607. /** Get the first child element, or optionally the first child
  608. element with the specified name.
  609. */
  610. const XMLElement* FirstChildElement( const char* name = 0 ) const;
  611. XMLElement* FirstChildElement( const char* name = 0 ) {
  612. return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
  613. }
  614. /// Get the last child node, or null if none exists.
  615. const XMLNode* LastChild() const {
  616. return _lastChild;
  617. }
  618. XMLNode* LastChild() {
  619. return _lastChild;
  620. }
  621. /** Get the last child element or optionally the last child
  622. element with the specified name.
  623. */
  624. const XMLElement* LastChildElement( const char* name = 0 ) const;
  625. XMLElement* LastChildElement( const char* name = 0 ) {
  626. return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
  627. }
  628. /// Get the previous (left) sibling node of this node.
  629. const XMLNode* PreviousSibling() const {
  630. return _prev;
  631. }
  632. XMLNode* PreviousSibling() {
  633. return _prev;
  634. }
  635. /// Get the previous (left) sibling element of this node, with an optionally supplied name.
  636. const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
  637. XMLElement* PreviousSiblingElement( const char* name = 0 ) {
  638. return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
  639. }
  640. /// Get the next (right) sibling node of this node.
  641. const XMLNode* NextSibling() const {
  642. return _next;
  643. }
  644. XMLNode* NextSibling() {
  645. return _next;
  646. }
  647. /// Get the next (right) sibling element of this node, with an optionally supplied name.
  648. const XMLElement* NextSiblingElement( const char* name = 0 ) const;
  649. XMLElement* NextSiblingElement( const char* name = 0 ) {
  650. return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
  651. }
  652. /**
  653. Add a child node as the last (right) child.
  654. If the child node is already part of the document,
  655. it is moved from its old location to the new location.
  656. Returns the addThis argument or 0 if the node does not
  657. belong to the same document.
  658. */
  659. XMLNode* InsertEndChild( XMLNode* addThis );
  660. XMLNode* LinkEndChild( XMLNode* addThis ) {
  661. return InsertEndChild( addThis );
  662. }
  663. /**
  664. Add a child node as the first (left) child.
  665. If the child node is already part of the document,
  666. it is moved from its old location to the new location.
  667. Returns the addThis argument or 0 if the node does not
  668. belong to the same document.
  669. */
  670. XMLNode* InsertFirstChild( XMLNode* addThis );
  671. /**
  672. Add a node after the specified child node.
  673. If the child node is already part of the document,
  674. it is moved from its old location to the new location.
  675. Returns the addThis argument or 0 if the afterThis node
  676. is not a child of this node, or if the node does not
  677. belong to the same document.
  678. */
  679. XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
  680. /**
  681. Delete all the children of this node.
  682. */
  683. void DeleteChildren();
  684. /**
  685. Delete a child of this node.
  686. */
  687. void DeleteChild( XMLNode* node );
  688. /**
  689. Make a copy of this node, but not its children.
  690. You may pass in a Document pointer that will be
  691. the owner of the new Node. If the 'document' is
  692. null, then the node returned will be allocated
  693. from the current Document. (this->GetDocument())
  694. Note: if called on a XMLDocument, this will return null.
  695. */
  696. virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
  697. /**
  698. Test if 2 nodes are the same, but don't test children.
  699. The 2 nodes do not need to be in the same Document.
  700. Note: if called on a XMLDocument, this will return false.
  701. */
  702. virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
  703. /** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
  704. XML tree will be conditionally visited and the host will be called back
  705. via the XMLVisitor interface.
  706. This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
  707. the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
  708. interface versus any other.)
  709. The interface has been based on ideas from:
  710. - http://www.saxproject.org/
  711. - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
  712. Which are both good references for "visiting".
  713. An example of using Accept():
  714. @verbatim
  715. XMLPrinter printer;
  716. tinyxmlDoc.Accept( &printer );
  717. const char* xmlcstr = printer.CStr();
  718. @endverbatim
  719. */
  720. virtual bool Accept( XMLVisitor* visitor ) const = 0;
  721. protected:
  722. XMLNode( XMLDocument* );
  723. virtual ~XMLNode();
  724. virtual char* ParseDeep( char*, StrPair* );
  725. XMLDocument* _document;
  726. XMLNode* _parent;
  727. mutable StrPair _value;
  728. XMLNode* _firstChild;
  729. XMLNode* _lastChild;
  730. XMLNode* _prev;
  731. XMLNode* _next;
  732. private:
  733. MemPool* _memPool;
  734. void Unlink( XMLNode* child );
  735. static void DeleteNode( XMLNode* node );
  736. void InsertChildPreamble( XMLNode* insertThis ) const;
  737. XMLNode( const XMLNode& ); // not supported
  738. XMLNode& operator=( const XMLNode& ); // not supported
  739. };
  740. /** XML text.
  741. Note that a text node can have child element nodes, for example:
  742. @verbatim
  743. <root>This is <b>bold</b></root>
  744. @endverbatim
  745. A text node can have 2 ways to output the next. "normal" output
  746. and CDATA. It will default to the mode it was parsed from the XML file and
  747. you generally want to leave it alone, but you can change the output mode with
  748. SetCData() and query it with CData().
  749. */
  750. class TINYXML2_LIB XMLText : public XMLNode
  751. {
  752. friend class XMLBase;
  753. friend class XMLDocument;
  754. public:
  755. virtual bool Accept( XMLVisitor* visitor ) const;
  756. virtual XMLText* ToText() {
  757. return this;
  758. }
  759. virtual const XMLText* ToText() const {
  760. return this;
  761. }
  762. /// Declare whether this should be CDATA or standard text.
  763. void SetCData( bool isCData ) {
  764. _isCData = isCData;
  765. }
  766. /// Returns true if this is a CDATA text element.
  767. bool CData() const {
  768. return _isCData;
  769. }
  770. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  771. virtual bool ShallowEqual( const XMLNode* compare ) const;
  772. protected:
  773. XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
  774. virtual ~XMLText() {}
  775. char* ParseDeep( char*, StrPair* endTag );
  776. private:
  777. bool _isCData;
  778. XMLText( const XMLText& ); // not supported
  779. XMLText& operator=( const XMLText& ); // not supported
  780. };
  781. /** An XML Comment. */
  782. class TINYXML2_LIB XMLComment : public XMLNode
  783. {
  784. friend class XMLDocument;
  785. public:
  786. virtual XMLComment* ToComment() {
  787. return this;
  788. }
  789. virtual const XMLComment* ToComment() const {
  790. return this;
  791. }
  792. virtual bool Accept( XMLVisitor* visitor ) const;
  793. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  794. virtual bool ShallowEqual( const XMLNode* compare ) const;
  795. protected:
  796. XMLComment( XMLDocument* doc );
  797. virtual ~XMLComment();
  798. char* ParseDeep( char*, StrPair* endTag );
  799. private:
  800. XMLComment( const XMLComment& ); // not supported
  801. XMLComment& operator=( const XMLComment& ); // not supported
  802. };
  803. /** In correct XML the declaration is the first entry in the file.
  804. @verbatim
  805. <?xml version="1.0" standalone="yes"?>
  806. @endverbatim
  807. TinyXML-2 will happily read or write files without a declaration,
  808. however.
  809. The text of the declaration isn't interpreted. It is parsed
  810. and written as a string.
  811. */
  812. class TINYXML2_LIB XMLDeclaration : public XMLNode
  813. {
  814. friend class XMLDocument;
  815. public:
  816. virtual XMLDeclaration* ToDeclaration() {
  817. return this;
  818. }
  819. virtual const XMLDeclaration* ToDeclaration() const {
  820. return this;
  821. }
  822. virtual bool Accept( XMLVisitor* visitor ) const;
  823. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  824. virtual bool ShallowEqual( const XMLNode* compare ) const;
  825. protected:
  826. XMLDeclaration( XMLDocument* doc );
  827. virtual ~XMLDeclaration();
  828. char* ParseDeep( char*, StrPair* endTag );
  829. private:
  830. XMLDeclaration( const XMLDeclaration& ); // not supported
  831. XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
  832. };
  833. /** Any tag that TinyXML-2 doesn't recognize is saved as an
  834. unknown. It is a tag of text, but should not be modified.
  835. It will be written back to the XML, unchanged, when the file
  836. is saved.
  837. DTD tags get thrown into XMLUnknowns.
  838. */
  839. class TINYXML2_LIB XMLUnknown : public XMLNode
  840. {
  841. friend class XMLDocument;
  842. public:
  843. virtual XMLUnknown* ToUnknown() {
  844. return this;
  845. }
  846. virtual const XMLUnknown* ToUnknown() const {
  847. return this;
  848. }
  849. virtual bool Accept( XMLVisitor* visitor ) const;
  850. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  851. virtual bool ShallowEqual( const XMLNode* compare ) const;
  852. protected:
  853. XMLUnknown( XMLDocument* doc );
  854. virtual ~XMLUnknown();
  855. char* ParseDeep( char*, StrPair* endTag );
  856. private:
  857. XMLUnknown( const XMLUnknown& ); // not supported
  858. XMLUnknown& operator=( const XMLUnknown& ); // not supported
  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. virtual XMLNode* ShallowClone( XMLDocument* document ) const;
  1252. virtual bool ShallowEqual( const XMLNode* compare ) const;
  1253. protected:
  1254. char* ParseDeep( char* p, StrPair* endTag );
  1255. private:
  1256. XMLElement( XMLDocument* doc );
  1257. virtual ~XMLElement();
  1258. XMLElement( const XMLElement& ); // not supported
  1259. void operator=( const XMLElement& ); // not supported
  1260. XMLAttribute* FindAttribute( const char* name ) {
  1261. return const_cast<XMLAttribute*>(const_cast<const XMLElement*>(this)->FindAttribute( name ));
  1262. }
  1263. XMLAttribute* FindOrCreateAttribute( const char* name );
  1264. //void LinkAttribute( XMLAttribute* attrib );
  1265. char* ParseAttributes( char* p );
  1266. static void DeleteAttribute( XMLAttribute* attribute );
  1267. enum { BUF_SIZE = 200 };
  1268. int _closingType;
  1269. // The attribute list is ordered; there is no 'lastAttribute'
  1270. // because the list needs to be scanned for dupes before adding
  1271. // a new attribute.
  1272. XMLAttribute* _rootAttribute;
  1273. };
  1274. enum Whitespace {
  1275. PRESERVE_WHITESPACE,
  1276. COLLAPSE_WHITESPACE
  1277. };
  1278. /** A Document binds together all the functionality.
  1279. It can be saved, loaded, and printed to the screen.
  1280. All Nodes are connected and allocated to a Document.
  1281. If the Document is deleted, all its Nodes are also deleted.
  1282. */
  1283. class TINYXML2_LIB XMLDocument : public XMLNode
  1284. {
  1285. friend class XMLElement;
  1286. public:
  1287. /// constructor
  1288. XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE );
  1289. ~XMLDocument();
  1290. virtual XMLDocument* ToDocument() {
  1291. TIXMLASSERT( this == _document );
  1292. return this;
  1293. }
  1294. virtual const XMLDocument* ToDocument() const {
  1295. TIXMLASSERT( this == _document );
  1296. return this;
  1297. }
  1298. /**
  1299. Parse an XML file from a character string.
  1300. Returns XML_NO_ERROR (0) on success, or
  1301. an errorID.
  1302. You may optionally pass in the 'nBytes', which is
  1303. the number of bytes which will be parsed. If not
  1304. specified, TinyXML-2 will assume 'xml' points to a
  1305. null terminated string.
  1306. */
  1307. XMLError Parse( const char* xml, size_t nBytes=(size_t)(-1) );
  1308. /**
  1309. Load an XML file from disk.
  1310. Returns XML_NO_ERROR (0) on success, or
  1311. an errorID.
  1312. */
  1313. XMLError LoadFile( const char* filename );
  1314. /**
  1315. Load an XML file from disk. You are responsible
  1316. for providing and closing the FILE*.
  1317. NOTE: The file should be opened as binary ("rb")
  1318. not text in order for TinyXML-2 to correctly
  1319. do newline normalization.
  1320. Returns XML_NO_ERROR (0) on success, or
  1321. an errorID.
  1322. */
  1323. XMLError LoadFile( FILE* );
  1324. /**
  1325. Save the XML file to disk.
  1326. Returns XML_NO_ERROR (0) on success, or
  1327. an errorID.
  1328. */
  1329. XMLError SaveFile( const char* filename, bool compact = false );
  1330. /**
  1331. Save the XML file to disk. You are responsible
  1332. for providing and closing the FILE*.
  1333. Returns XML_NO_ERROR (0) on success, or
  1334. an errorID.
  1335. */
  1336. XMLError SaveFile( FILE* fp, bool compact = false );
  1337. bool ProcessEntities() const {
  1338. return _processEntities;
  1339. }
  1340. Whitespace WhitespaceMode() const {
  1341. return _whitespace;
  1342. }
  1343. /**
  1344. Returns true if this document has a leading Byte Order Mark of UTF8.
  1345. */
  1346. bool HasBOM() const {
  1347. return _writeBOM;
  1348. }
  1349. /** Sets whether to write the BOM when writing the file.
  1350. */
  1351. void SetBOM( bool useBOM ) {
  1352. _writeBOM = useBOM;
  1353. }
  1354. /** Return the root element of DOM. Equivalent to FirstChildElement().
  1355. To get the first node, use FirstChild().
  1356. */
  1357. XMLElement* RootElement() {
  1358. return FirstChildElement();
  1359. }
  1360. const XMLElement* RootElement() const {
  1361. return FirstChildElement();
  1362. }
  1363. /** Print the Document. If the Printer is not provided, it will
  1364. print to stdout. If you provide Printer, this can print to a file:
  1365. @verbatim
  1366. XMLPrinter printer( fp );
  1367. doc.Print( &printer );
  1368. @endverbatim
  1369. Or you can use a printer to print to memory:
  1370. @verbatim
  1371. XMLPrinter printer;
  1372. doc.Print( &printer );
  1373. // printer.CStr() has a const char* to the XML
  1374. @endverbatim
  1375. */
  1376. void Print( XMLPrinter* streamer=0 ) const;
  1377. virtual bool Accept( XMLVisitor* visitor ) const;
  1378. /**
  1379. Create a new Element associated with
  1380. this Document. The memory for the Element
  1381. is managed by the Document.
  1382. */
  1383. XMLElement* NewElement( const char* name );
  1384. /**
  1385. Create a new Comment associated with
  1386. this Document. The memory for the Comment
  1387. is managed by the Document.
  1388. */
  1389. XMLComment* NewComment( const char* comment );
  1390. /**
  1391. Create a new Text associated with
  1392. this Document. The memory for the Text
  1393. is managed by the Document.
  1394. */
  1395. XMLText* NewText( const char* text );
  1396. /**
  1397. Create a new Declaration associated with
  1398. this Document. The memory for the object
  1399. is managed by the Document.
  1400. If the 'text' param is null, the standard
  1401. declaration is used.:
  1402. @verbatim
  1403. <?xml version="1.0" encoding="UTF-8"?>
  1404. @endverbatim
  1405. */
  1406. XMLDeclaration* NewDeclaration( const char* text=0 );
  1407. /**
  1408. Create a new Unknown associated with
  1409. this Document. The memory for the object
  1410. is managed by the Document.
  1411. */
  1412. XMLUnknown* NewUnknown( const char* text );
  1413. /**
  1414. Delete a node associated with this document.
  1415. It will be unlinked from the DOM.
  1416. */
  1417. void DeleteNode( XMLNode* node );
  1418. void SetError( XMLError error, const char* str1, const char* str2 );
  1419. /// Return true if there was an error parsing the document.
  1420. bool Error() const {
  1421. return _errorID != XML_NO_ERROR;
  1422. }
  1423. /// Return the errorID.
  1424. XMLError ErrorID() const {
  1425. return _errorID;
  1426. }
  1427. const char* ErrorName() const;
  1428. /// Return a possibly helpful diagnostic location or string.
  1429. const char* GetErrorStr1() const {
  1430. return _errorStr1;
  1431. }
  1432. /// Return a possibly helpful secondary diagnostic location or string.
  1433. const char* GetErrorStr2() const {
  1434. return _errorStr2;
  1435. }
  1436. /// If there is an error, print it to stdout.
  1437. void PrintError() const;
  1438. /// Clear the document, resetting it to the initial state.
  1439. void Clear();
  1440. // internal
  1441. char* Identify( char* p, XMLNode** node );
  1442. virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const {
  1443. return 0;
  1444. }
  1445. virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const {
  1446. return false;
  1447. }
  1448. private:
  1449. XMLDocument( const XMLDocument& ); // not supported
  1450. void operator=( const XMLDocument& ); // not supported
  1451. bool _writeBOM;
  1452. bool _processEntities;
  1453. XMLError _errorID;
  1454. Whitespace _whitespace;
  1455. const char* _errorStr1;
  1456. const char* _errorStr2;
  1457. char* _charBuffer;
  1458. MemPoolT< sizeof(XMLElement) > _elementPool;
  1459. MemPoolT< sizeof(XMLAttribute) > _attributePool;
  1460. MemPoolT< sizeof(XMLText) > _textPool;
  1461. MemPoolT< sizeof(XMLComment) > _commentPool;
  1462. static const char* _errorNames[XML_ERROR_COUNT];
  1463. void Parse();
  1464. };
  1465. /**
  1466. A XMLHandle is a class that wraps a node pointer with null checks; this is
  1467. an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
  1468. DOM structure. It is a separate utility class.
  1469. Take an example:
  1470. @verbatim
  1471. <Document>
  1472. <Element attributeA = "valueA">
  1473. <Child attributeB = "value1" />
  1474. <Child attributeB = "value2" />
  1475. </Element>
  1476. </Document>
  1477. @endverbatim
  1478. Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
  1479. easy to write a *lot* of code that looks like:
  1480. @verbatim
  1481. XMLElement* root = document.FirstChildElement( "Document" );
  1482. if ( root )
  1483. {
  1484. XMLElement* element = root->FirstChildElement( "Element" );
  1485. if ( element )
  1486. {
  1487. XMLElement* child = element->FirstChildElement( "Child" );
  1488. if ( child )
  1489. {
  1490. XMLElement* child2 = child->NextSiblingElement( "Child" );
  1491. if ( child2 )
  1492. {
  1493. // Finally do something useful.
  1494. @endverbatim
  1495. And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
  1496. of such code. A XMLHandle checks for null pointers so it is perfectly safe
  1497. and correct to use:
  1498. @verbatim
  1499. XMLHandle docHandle( &document );
  1500. XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();
  1501. if ( child2 )
  1502. {
  1503. // do something useful
  1504. @endverbatim
  1505. Which is MUCH more concise and useful.
  1506. It is also safe to copy handles - internally they are nothing more than node pointers.
  1507. @verbatim
  1508. XMLHandle handleCopy = handle;
  1509. @endverbatim
  1510. See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
  1511. */
  1512. class TINYXML2_LIB XMLHandle
  1513. {
  1514. public:
  1515. /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
  1516. XMLHandle( XMLNode* node ) {
  1517. _node = node;
  1518. }
  1519. /// Create a handle from a node.
  1520. XMLHandle( XMLNode& node ) {
  1521. _node = &node;
  1522. }
  1523. /// Copy constructor
  1524. XMLHandle( const XMLHandle& ref ) {
  1525. _node = ref._node;
  1526. }
  1527. /// Assignment
  1528. XMLHandle& operator=( const XMLHandle& ref ) {
  1529. _node = ref._node;
  1530. return *this;
  1531. }
  1532. /// Get the first child of this handle.
  1533. XMLHandle FirstChild() {
  1534. return XMLHandle( _node ? _node->FirstChild() : 0 );
  1535. }
  1536. /// Get the first child element of this handle.
  1537. XMLHandle FirstChildElement( const char* name = 0 ) {
  1538. return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
  1539. }
  1540. /// Get the last child of this handle.
  1541. XMLHandle LastChild() {
  1542. return XMLHandle( _node ? _node->LastChild() : 0 );
  1543. }
  1544. /// Get the last child element of this handle.
  1545. XMLHandle LastChildElement( const char* name = 0 ) {
  1546. return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
  1547. }
  1548. /// Get the previous sibling of this handle.
  1549. XMLHandle PreviousSibling() {
  1550. return XMLHandle( _node ? _node->PreviousSibling() : 0 );
  1551. }
  1552. /// Get the previous sibling element of this handle.
  1553. XMLHandle PreviousSiblingElement( const char* name = 0 ) {
  1554. return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
  1555. }
  1556. /// Get the next sibling of this handle.
  1557. XMLHandle NextSibling() {
  1558. return XMLHandle( _node ? _node->NextSibling() : 0 );
  1559. }
  1560. /// Get the next sibling element of this handle.
  1561. XMLHandle NextSiblingElement( const char* name = 0 ) {
  1562. return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
  1563. }
  1564. /// Safe cast to XMLNode. This can return null.
  1565. XMLNode* ToNode() {
  1566. return _node;
  1567. }
  1568. /// Safe cast to XMLElement. This can return null.
  1569. XMLElement* ToElement() {
  1570. return ( ( _node == 0 ) ? 0 : _node->ToElement() );
  1571. }
  1572. /// Safe cast to XMLText. This can return null.
  1573. XMLText* ToText() {
  1574. return ( ( _node == 0 ) ? 0 : _node->ToText() );
  1575. }
  1576. /// Safe cast to XMLUnknown. This can return null.
  1577. XMLUnknown* ToUnknown() {
  1578. return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
  1579. }
  1580. /// Safe cast to XMLDeclaration. This can return null.
  1581. XMLDeclaration* ToDeclaration() {
  1582. return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
  1583. }
  1584. private:
  1585. XMLNode* _node;
  1586. };
  1587. /**
  1588. A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
  1589. same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
  1590. */
  1591. class TINYXML2_LIB XMLConstHandle
  1592. {
  1593. public:
  1594. XMLConstHandle( const XMLNode* node ) {
  1595. _node = node;
  1596. }
  1597. XMLConstHandle( const XMLNode& node ) {
  1598. _node = &node;
  1599. }
  1600. XMLConstHandle( const XMLConstHandle& ref ) {
  1601. _node = ref._node;
  1602. }
  1603. XMLConstHandle& operator=( const XMLConstHandle& ref ) {
  1604. _node = ref._node;
  1605. return *this;
  1606. }
  1607. const XMLConstHandle FirstChild() const {
  1608. return XMLConstHandle( _node ? _node->FirstChild() : 0 );
  1609. }
  1610. const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
  1611. return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
  1612. }
  1613. const XMLConstHandle LastChild() const {
  1614. return XMLConstHandle( _node ? _node->LastChild() : 0 );
  1615. }
  1616. const XMLConstHandle LastChildElement( const char* name = 0 ) const {
  1617. return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
  1618. }
  1619. const XMLConstHandle PreviousSibling() const {
  1620. return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
  1621. }
  1622. const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
  1623. return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
  1624. }
  1625. const XMLConstHandle NextSibling() const {
  1626. return XMLConstHandle( _node ? _node->NextSibling() : 0 );
  1627. }
  1628. const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
  1629. return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
  1630. }
  1631. const XMLNode* ToNode() const {
  1632. return _node;
  1633. }
  1634. const XMLElement* ToElement() const {
  1635. return ( ( _node == 0 ) ? 0 : _node->ToElement() );
  1636. }
  1637. const XMLText* ToText() const {
  1638. return ( ( _node == 0 ) ? 0 : _node->ToText() );
  1639. }
  1640. const XMLUnknown* ToUnknown() const {
  1641. return ( ( _node == 0 ) ? 0 : _node->ToUnknown() );
  1642. }
  1643. const XMLDeclaration* ToDeclaration() const {
  1644. return ( ( _node == 0 ) ? 0 : _node->ToDeclaration() );
  1645. }
  1646. private:
  1647. const XMLNode* _node;
  1648. };
  1649. /**
  1650. Printing functionality. The XMLPrinter gives you more
  1651. options than the XMLDocument::Print() method.
  1652. It can:
  1653. -# Print to memory.
  1654. -# Print to a file you provide.
  1655. -# Print XML without a XMLDocument.
  1656. Print to Memory
  1657. @verbatim
  1658. XMLPrinter printer;
  1659. doc.Print( &printer );
  1660. SomeFunction( printer.CStr() );
  1661. @endverbatim
  1662. Print to a File
  1663. You provide the file pointer.
  1664. @verbatim
  1665. XMLPrinter printer( fp );
  1666. doc.Print( &printer );
  1667. @endverbatim
  1668. Print without a XMLDocument
  1669. When loading, an XML parser is very useful. However, sometimes
  1670. when saving, it just gets in the way. The code is often set up
  1671. for streaming, and constructing the DOM is just overhead.
  1672. The Printer supports the streaming case. The following code
  1673. prints out a trivially simple XML file without ever creating
  1674. an XML document.
  1675. @verbatim
  1676. XMLPrinter printer( fp );
  1677. printer.OpenElement( "foo" );
  1678. printer.PushAttribute( "foo", "bar" );
  1679. printer.CloseElement();
  1680. @endverbatim
  1681. */
  1682. class TINYXML2_LIB XMLPrinter : public XMLVisitor
  1683. {
  1684. public:
  1685. /** Construct the printer. If the FILE* is specified,
  1686. this will print to the FILE. Else it will print
  1687. to memory, and the result is available in CStr().
  1688. If 'compact' is set to true, then output is created
  1689. with only required whitespace and newlines.
  1690. */
  1691. XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
  1692. virtual ~XMLPrinter() {}
  1693. /** If streaming, write the BOM and declaration. */
  1694. void PushHeader( bool writeBOM, bool writeDeclaration );
  1695. /** If streaming, start writing an element.
  1696. The element must be closed with CloseElement()
  1697. */
  1698. void OpenElement( const char* name, bool compactMode=false );
  1699. /// If streaming, add an attribute to an open element.
  1700. void PushAttribute( const char* name, const char* value );
  1701. void PushAttribute( const char* name, int value );
  1702. void PushAttribute( const char* name, unsigned value );
  1703. void PushAttribute( const char* name, bool value );
  1704. void PushAttribute( const char* name, double value );
  1705. /// If streaming, close the Element.
  1706. virtual void CloseElement( bool compactMode=false );
  1707. /// Add a text node.
  1708. void PushText( const char* text, bool cdata=false );
  1709. /// Add a text node from an integer.
  1710. void PushText( int value );
  1711. /// Add a text node from an unsigned.
  1712. void PushText( unsigned value );
  1713. /// Add a text node from a bool.
  1714. void PushText( bool value );
  1715. /// Add a text node from a float.
  1716. void PushText( float value );
  1717. /// Add a text node from a double.
  1718. void PushText( double value );
  1719. /// Add a comment
  1720. void PushComment( const char* comment );
  1721. void PushDeclaration( const char* value );
  1722. void PushUnknown( const char* value );
  1723. virtual bool VisitEnter( const XMLDocument& /*doc*/ );
  1724. virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
  1725. return true;
  1726. }
  1727. virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute );
  1728. virtual bool VisitExit( const XMLElement& element );
  1729. virtual bool Visit( const XMLText& text );
  1730. virtual bool Visit( const XMLComment& comment );
  1731. virtual bool Visit( const XMLDeclaration& declaration );
  1732. virtual bool Visit( const XMLUnknown& unknown );
  1733. /**
  1734. If in print to memory mode, return a pointer to
  1735. the XML file in memory.
  1736. */
  1737. const char* CStr() const {
  1738. return _buffer.Mem();
  1739. }
  1740. /**
  1741. If in print to memory mode, return the size
  1742. of the XML file in memory. (Note the size returned
  1743. includes the terminating null.)
  1744. */
  1745. int CStrSize() const {
  1746. return _buffer.Size();
  1747. }
  1748. /**
  1749. If in print to memory mode, reset the buffer to the
  1750. beginning.
  1751. */
  1752. void ClearBuffer() {
  1753. _buffer.Clear();
  1754. _buffer.Push(0);
  1755. }
  1756. protected:
  1757. virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
  1758. /** Prints out the space before an element. You may override to change
  1759. the space and tabs used. A PrintSpace() override should call Print().
  1760. */
  1761. virtual void PrintSpace( int depth );
  1762. void Print( const char* format, ... );
  1763. void SealElementIfJustOpened();
  1764. bool _elementJustOpened;
  1765. DynArray< const char*, 10 > _stack;
  1766. private:
  1767. void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
  1768. bool _firstElement;
  1769. FILE* _fp;
  1770. int _depth;
  1771. int _textDepth;
  1772. bool _processEntities;
  1773. bool _compactMode;
  1774. enum {
  1775. ENTITY_RANGE = 64,
  1776. BUF_SIZE = 200
  1777. };
  1778. bool _entityFlag[ENTITY_RANGE];
  1779. bool _restrictedEntityFlag[ENTITY_RANGE];
  1780. DynArray< char, 20 > _buffer;
  1781. };
  1782. } // tinyxml2
  1783. #if defined(_MSC_VER)
  1784. # pragma warning(pop)
  1785. #endif
  1786. #endif // TINYXML2_INCLUDED