tinyxml2.h 62 KB

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