tinyxml.h 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  1. /*
  2. www.sourceforge.net/projects/tinyxml
  3. Original code by Lee Thomason (www.grinninglizard.com)
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must
  11. not claim that you wrote the original software. If you use this
  12. software in a product, an acknowledgment in the product documentation
  13. would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. */
  19. #ifndef TINYXML_INCLUDED
  20. #define TINYXML_INCLUDED
  21. #ifdef _MSC_VER
  22. #pragma warning( push )
  23. #pragma warning( disable : 4530 )
  24. #pragma warning( disable : 4786 )
  25. #endif
  26. #include <ctype.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <assert.h>
  31. // Help out windows:
  32. #if defined( _DEBUG ) && !defined( DEBUG )
  33. #define DEBUG
  34. #endif
  35. #ifdef TIXML_USE_STL
  36. #include <string>
  37. #include <iostream>
  38. #include <sstream>
  39. #define TIXML_STRING std::string
  40. #else
  41. #include "tinystr.h"
  42. #define TIXML_STRING TiXmlString
  43. #endif
  44. // Deprecated library function hell. Compilers want to use the
  45. // new safe versions. This probably doesn't fully address the problem,
  46. // but it gets closer. There are too many compilers for me to fully
  47. // test. If you get compilation troubles, undefine TIXML_SAFE
  48. #define TIXML_SAFE
  49. #ifdef TIXML_SAFE
  50. #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
  51. // Microsoft visual studio, version 2005 and higher.
  52. #define TIXML_SNPRINTF _snprintf_s
  53. #define TIXML_SSCANF sscanf_s
  54. #elif defined(_MSC_VER) && (_MSC_VER >= 1200 )
  55. // Microsoft visual studio, version 6 and higher.
  56. //#pragma message( "Using _sn* functions." )
  57. #define TIXML_SNPRINTF _snprintf
  58. #define TIXML_SSCANF sscanf
  59. #elif defined(__GNUC__) && (__GNUC__ >= 3 )
  60. // GCC version 3 and higher.s
  61. //#warning( "Using sn* functions." )
  62. #define TIXML_SNPRINTF snprintf
  63. #define TIXML_SSCANF sscanf
  64. #else
  65. #define TIXML_SNPRINTF snprintf
  66. #define TIXML_SSCANF sscanf
  67. #endif
  68. #endif
  69. class TiXmlDocument;
  70. class TiXmlElement;
  71. class TiXmlComment;
  72. class TiXmlUnknown;
  73. class TiXmlAttribute;
  74. class TiXmlText;
  75. class TiXmlDeclaration;
  76. class TiXmlParsingData;
  77. const int TIXML_MAJOR_VERSION = 2;
  78. const int TIXML_MINOR_VERSION = 6;
  79. const int TIXML_PATCH_VERSION = 2;
  80. /* Internal structure for tracking location of items
  81. in the XML file.
  82. */
  83. struct TiXmlCursor
  84. {
  85. TiXmlCursor() { Clear(); }
  86. void Clear() { row = col = -1; }
  87. int row; // 0 based.
  88. int col; // 0 based.
  89. };
  90. /**
  91. Implements the interface to the "Visitor pattern" (see the Accept() method.)
  92. If you call the Accept() method, it requires being passed a TiXmlVisitor
  93. class to handle callbacks. For nodes that contain other nodes (Document, Element)
  94. you will get called with a VisitEnter/VisitExit pair. Nodes that are always leaves
  95. are simply called with Visit().
  96. If you return 'true' from a Visit method, recursive parsing will continue. If you return
  97. false, <b>no children of this node or its sibilings</b> will be Visited.
  98. All flavors of Visit methods have a default implementation that returns 'true' (continue
  99. visiting). You need to only override methods that are interesting to you.
  100. Generally Accept() is called on the TiXmlDocument, although all nodes suppert Visiting.
  101. You should never change the document from a callback.
  102. @sa TiXmlNode::Accept()
  103. */
  104. class TiXmlVisitor
  105. {
  106. public:
  107. virtual ~TiXmlVisitor() {}
  108. /// Visit a document.
  109. virtual bool VisitEnter( const TiXmlDocument& /*doc*/ ) { return true; }
  110. /// Visit a document.
  111. virtual bool VisitExit( const TiXmlDocument& /*doc*/ ) { return true; }
  112. /// Visit an element.
  113. virtual bool VisitEnter( const TiXmlElement& /*element*/, const TiXmlAttribute* /*firstAttribute*/ ) { return true; }
  114. /// Visit an element.
  115. virtual bool VisitExit( const TiXmlElement& /*element*/ ) { return true; }
  116. /// Visit a declaration
  117. virtual bool Visit( const TiXmlDeclaration& /*declaration*/ ) { return true; }
  118. /// Visit a text node
  119. virtual bool Visit( const TiXmlText& /*text*/ ) { return true; }
  120. /// Visit a comment node
  121. virtual bool Visit( const TiXmlComment& /*comment*/ ) { return true; }
  122. /// Visit an unknown node
  123. virtual bool Visit( const TiXmlUnknown& /*unknown*/ ) { return true; }
  124. };
  125. // Only used by Attribute::Query functions
  126. enum
  127. {
  128. TIXML_SUCCESS,
  129. TIXML_NO_ATTRIBUTE,
  130. TIXML_WRONG_TYPE
  131. };
  132. // Used by the parsing routines.
  133. enum TiXmlEncoding
  134. {
  135. TIXML_ENCODING_UNKNOWN,
  136. TIXML_ENCODING_UTF8,
  137. TIXML_ENCODING_LEGACY
  138. };
  139. const TiXmlEncoding TIXML_DEFAULT_ENCODING = TIXML_ENCODING_UNKNOWN;
  140. /** TiXmlBase is a base class for every class in TinyXml.
  141. It does little except to establish that TinyXml classes
  142. can be printed and provide some utility functions.
  143. In XML, the document and elements can contain
  144. other elements and other types of nodes.
  145. @verbatim
  146. A Document can contain: Element (container or leaf)
  147. Comment (leaf)
  148. Unknown (leaf)
  149. Declaration( leaf )
  150. An Element can contain: Element (container or leaf)
  151. Text (leaf)
  152. Attributes (not on tree)
  153. Comment (leaf)
  154. Unknown (leaf)
  155. A Decleration contains: Attributes (not on tree)
  156. @endverbatim
  157. */
  158. class TiXmlBase
  159. {
  160. friend class TiXmlNode;
  161. friend class TiXmlElement;
  162. friend class TiXmlDocument;
  163. public:
  164. TiXmlBase() : userData(0) {}
  165. virtual ~TiXmlBase() {}
  166. /** All TinyXml classes can print themselves to a filestream
  167. or the string class (TiXmlString in non-STL mode, std::string
  168. in STL mode.) Either or both cfile and str can be null.
  169. This is a formatted print, and will insert
  170. tabs and newlines.
  171. (For an unformatted stream, use the << operator.)
  172. */
  173. virtual void Print( FILE* cfile, int depth ) const = 0;
  174. /** The world does not agree on whether white space should be kept or
  175. not. In order to make everyone happy, these global, static functions
  176. are provided to set whether or not TinyXml will condense all white space
  177. into a single space or not. The default is to condense. Note changing this
  178. value is not thread safe.
  179. */
  180. static void SetCondenseWhiteSpace( bool condense ) { condenseWhiteSpace = condense; }
  181. /// Return the current white space setting.
  182. static bool IsWhiteSpaceCondensed() { return condenseWhiteSpace; }
  183. /** Return the position, in the original source file, of this node or attribute.
  184. The row and column are 1-based. (That is the first row and first column is
  185. 1,1). If the returns values are 0 or less, then the parser does not have
  186. a row and column value.
  187. Generally, the row and column value will be set when the TiXmlDocument::Load(),
  188. TiXmlDocument::LoadFile(), or any TiXmlNode::Parse() is called. It will NOT be set
  189. when the DOM was created from operator>>.
  190. The values reflect the initial load. Once the DOM is modified programmatically
  191. (by adding or changing nodes and attributes) the new values will NOT update to
  192. reflect changes in the document.
  193. There is a minor performance cost to computing the row and column. Computation
  194. can be disabled if TiXmlDocument::SetTabSize() is called with 0 as the value.
  195. @sa TiXmlDocument::SetTabSize()
  196. */
  197. int Row() const { return location.row + 1; }
  198. int Column() const { return location.col + 1; } ///< See Row()
  199. void SetUserData( void* user ) { userData = user; } ///< Set a pointer to arbitrary user data.
  200. void* GetUserData() { return userData; } ///< Get a pointer to arbitrary user data.
  201. const void* GetUserData() const { return userData; } ///< Get a pointer to arbitrary user data.
  202. // Table that returs, for a given lead byte, the total number of bytes
  203. // in the UTF-8 sequence.
  204. static const int utf8ByteTable[256];
  205. virtual const char* Parse( const char* p,
  206. TiXmlParsingData* data,
  207. TiXmlEncoding encoding /*= TIXML_ENCODING_UNKNOWN */ ) = 0;
  208. /** Expands entities in a string. Note this should not contian the tag's '<', '>', etc,
  209. or they will be transformed into entities!
  210. */
  211. static void EncodeString( const TIXML_STRING& str, TIXML_STRING* out );
  212. enum
  213. {
  214. TIXML_NO_ERROR = 0,
  215. TIXML_ERROR,
  216. TIXML_ERROR_OPENING_FILE,
  217. TIXML_ERROR_PARSING_ELEMENT,
  218. TIXML_ERROR_FAILED_TO_READ_ELEMENT_NAME,
  219. TIXML_ERROR_READING_ELEMENT_VALUE,
  220. TIXML_ERROR_READING_ATTRIBUTES,
  221. TIXML_ERROR_PARSING_EMPTY,
  222. TIXML_ERROR_READING_END_TAG,
  223. TIXML_ERROR_PARSING_UNKNOWN,
  224. TIXML_ERROR_PARSING_COMMENT,
  225. TIXML_ERROR_PARSING_DECLARATION,
  226. TIXML_ERROR_DOCUMENT_EMPTY,
  227. TIXML_ERROR_EMBEDDED_NULL,
  228. TIXML_ERROR_PARSING_CDATA,
  229. TIXML_ERROR_DOCUMENT_TOP_ONLY,
  230. TIXML_ERROR_STRING_COUNT
  231. };
  232. protected:
  233. static const char* SkipWhiteSpace( const char*, TiXmlEncoding encoding );
  234. inline static bool IsWhiteSpace( char c )
  235. {
  236. return ( isspace( (unsigned char) c ) || c == '\n' || c == '\r' );
  237. }
  238. inline static bool IsWhiteSpace( int c )
  239. {
  240. if ( c < 256 )
  241. return IsWhiteSpace( (char) c );
  242. return false; // Again, only truly correct for English/Latin...but usually works.
  243. }
  244. #ifdef TIXML_USE_STL
  245. static bool StreamWhiteSpace( std::istream * in, TIXML_STRING * tag );
  246. static bool StreamTo( std::istream * in, int character, TIXML_STRING * tag );
  247. #endif
  248. /* Reads an XML name into the string provided. Returns
  249. a pointer just past the last character of the name,
  250. or 0 if the function has an error.
  251. */
  252. static const char* ReadName( const char* p, TIXML_STRING* name, TiXmlEncoding encoding );
  253. /* Reads text. Returns a pointer past the given end tag.
  254. Wickedly complex options, but it keeps the (sensitive) code in one place.
  255. */
  256. static const char* ReadText( const char* in, // where to start
  257. TIXML_STRING* text, // the string read
  258. bool ignoreWhiteSpace, // whether to keep the white space
  259. const char* endTag, // what ends this text
  260. bool ignoreCase, // whether to ignore case in the end tag
  261. TiXmlEncoding encoding ); // the current encoding
  262. // If an entity has been found, transform it into a character.
  263. static const char* GetEntity( const char* in, char* value, int* length, TiXmlEncoding encoding );
  264. // Get a character, while interpreting entities.
  265. // The length can be from 0 to 4 bytes.
  266. inline static const char* GetChar( const char* p, char* _value, int* length, TiXmlEncoding encoding )
  267. {
  268. assert( p );
  269. if ( encoding == TIXML_ENCODING_UTF8 )
  270. {
  271. *length = utf8ByteTable[ *((const unsigned char*)p) ];
  272. assert( *length >= 0 && *length < 5 );
  273. }
  274. else
  275. {
  276. *length = 1;
  277. }
  278. if ( *length == 1 )
  279. {
  280. if ( *p == '&' )
  281. return GetEntity( p, _value, length, encoding );
  282. *_value = *p;
  283. return p+1;
  284. }
  285. else if ( *length )
  286. {
  287. //strncpy( _value, p, *length ); // lots of compilers don't like this function (unsafe),
  288. // and the null terminator isn't needed
  289. for( int i=0; p[i] && i<*length; ++i ) {
  290. _value[i] = p[i];
  291. }
  292. return p + (*length);
  293. }
  294. else
  295. {
  296. // Not valid text.
  297. return 0;
  298. }
  299. }
  300. // Return true if the next characters in the stream are any of the endTag sequences.
  301. // Ignore case only works for english, and should only be relied on when comparing
  302. // to English words: StringEqual( p, "version", true ) is fine.
  303. static bool StringEqual( const char* p,
  304. const char* endTag,
  305. bool ignoreCase,
  306. TiXmlEncoding encoding );
  307. static const char* errorString[ TIXML_ERROR_STRING_COUNT ];
  308. TiXmlCursor location;
  309. /// Field containing a generic user pointer
  310. void* userData;
  311. // None of these methods are reliable for any language except English.
  312. // Good for approximation, not great for accuracy.
  313. static int IsAlpha( unsigned char anyByte, TiXmlEncoding encoding );
  314. static int IsAlphaNum( unsigned char anyByte, TiXmlEncoding encoding );
  315. inline static int ToLower( int v, TiXmlEncoding encoding )
  316. {
  317. if ( encoding == TIXML_ENCODING_UTF8 )
  318. {
  319. if ( v < 128 ) return tolower( v );
  320. return v;
  321. }
  322. else
  323. {
  324. return tolower( v );
  325. }
  326. }
  327. static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
  328. private:
  329. TiXmlBase( const TiXmlBase& ); // not implemented.
  330. void operator=( const TiXmlBase& base ); // not allowed.
  331. struct Entity
  332. {
  333. const char* str;
  334. unsigned int strLength;
  335. char chr;
  336. };
  337. enum
  338. {
  339. NUM_ENTITY = 5,
  340. MAX_ENTITY_LENGTH = 6
  341. };
  342. static Entity entity[ NUM_ENTITY ];
  343. static bool condenseWhiteSpace;
  344. };
  345. /** The parent class for everything in the Document Object Model.
  346. (Except for attributes).
  347. Nodes have siblings, a parent, and children. A node can be
  348. in a document, or stand on its own. The type of a TiXmlNode
  349. can be queried, and it can be cast to its more defined type.
  350. */
  351. class TiXmlNode : public TiXmlBase
  352. {
  353. friend class TiXmlDocument;
  354. friend class TiXmlElement;
  355. public:
  356. #ifdef TIXML_USE_STL
  357. /** An input stream operator, for every class. Tolerant of newlines and
  358. formatting, but doesn't expect them.
  359. */
  360. friend std::istream& operator >> (std::istream& in, TiXmlNode& base);
  361. /** An output stream operator, for every class. Note that this outputs
  362. without any newlines or formatting, as opposed to Print(), which
  363. includes tabs and new lines.
  364. The operator<< and operator>> are not completely symmetric. Writing
  365. a node to a stream is very well defined. You'll get a nice stream
  366. of output, without any extra whitespace or newlines.
  367. But reading is not as well defined. (As it always is.) If you create
  368. a TiXmlElement (for example) and read that from an input stream,
  369. the text needs to define an element or junk will result. This is
  370. true of all input streams, but it's worth keeping in mind.
  371. A TiXmlDocument will read nodes until it reads a root element, and
  372. all the children of that root element.
  373. */
  374. friend std::ostream& operator<< (std::ostream& out, const TiXmlNode& base);
  375. /// Appends the XML node or attribute to a std::string.
  376. friend std::string& operator<< (std::string& out, const TiXmlNode& base );
  377. #endif
  378. /** The types of XML nodes supported by TinyXml. (All the
  379. unsupported types are picked up by UNKNOWN.)
  380. */
  381. enum NodeType
  382. {
  383. TINYXML_DOCUMENT,
  384. TINYXML_ELEMENT,
  385. TINYXML_COMMENT,
  386. TINYXML_UNKNOWN,
  387. TINYXML_TEXT,
  388. TINYXML_DECLARATION,
  389. TINYXML_TYPECOUNT
  390. };
  391. virtual ~TiXmlNode();
  392. /** The meaning of 'value' changes for the specific type of
  393. TiXmlNode.
  394. @verbatim
  395. Document: filename of the xml file
  396. Element: name of the element
  397. Comment: the comment text
  398. Unknown: the tag contents
  399. Text: the text string
  400. @endverbatim
  401. The subclasses will wrap this function.
  402. */
  403. const char *Value() const { return value.c_str (); }
  404. #ifdef TIXML_USE_STL
  405. /** Return Value() as a std::string. If you only use STL,
  406. this is more efficient than calling Value().
  407. Only available in STL mode.
  408. */
  409. const std::string& ValueStr() const { return value; }
  410. #endif
  411. const TIXML_STRING& ValueTStr() const { return value; }
  412. /** Changes the value of the node. Defined as:
  413. @verbatim
  414. Document: filename of the xml file
  415. Element: name of the element
  416. Comment: the comment text
  417. Unknown: the tag contents
  418. Text: the text string
  419. @endverbatim
  420. */
  421. void SetValue(const char * _value) { value = _value;}
  422. #ifdef TIXML_USE_STL
  423. /// STL std::string form.
  424. void SetValue( const std::string& _value ) { value = _value; }
  425. #endif
  426. /// Delete all the children of this node. Does not affect 'this'.
  427. void Clear();
  428. /// One step up the DOM.
  429. TiXmlNode* Parent() { return parent; }
  430. const TiXmlNode* Parent() const { return parent; }
  431. const TiXmlNode* FirstChild() const { return firstChild; } ///< The first child of this node. Will be null if there are no children.
  432. TiXmlNode* FirstChild() { return firstChild; }
  433. const TiXmlNode* FirstChild( const char * value ) const; ///< The first child of this node with the matching 'value'. Will be null if none found.
  434. /// The first child of this node with the matching 'value'. Will be null if none found.
  435. TiXmlNode* FirstChild( const char * _value ) {
  436. // Call through to the const version - safe since nothing is changed. Exiting syntax: cast this to a const (always safe)
  437. // call the method, cast the return back to non-const.
  438. return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->FirstChild( _value ));
  439. }
  440. const TiXmlNode* LastChild() const { return lastChild; } /// The last child of this node. Will be null if there are no children.
  441. TiXmlNode* LastChild() { return lastChild; }
  442. const TiXmlNode* LastChild( const char * value ) const; /// The last child of this node matching 'value'. Will be null if there are no children.
  443. TiXmlNode* LastChild( const char * _value ) {
  444. return const_cast< TiXmlNode* > ((const_cast< const TiXmlNode* >(this))->LastChild( _value ));
  445. }
  446. #ifdef TIXML_USE_STL
  447. const TiXmlNode* FirstChild( const std::string& _value ) const { return FirstChild (_value.c_str ()); } ///< STL std::string form.
  448. TiXmlNode* FirstChild( const std::string& _value ) { return FirstChild (_value.c_str ()); } ///< STL std::string form.
  449. const TiXmlNode* LastChild( const std::string& _value ) const { return LastChild (_value.c_str ()); } ///< STL std::string form.
  450. TiXmlNode* LastChild( const std::string& _value ) { return LastChild (_value.c_str ()); } ///< STL std::string form.
  451. #endif
  452. /** An alternate way to walk the children of a node.
  453. One way to iterate over nodes is:
  454. @verbatim
  455. for( child = parent->FirstChild(); child; child = child->NextSibling() )
  456. @endverbatim
  457. IterateChildren does the same thing with the syntax:
  458. @verbatim
  459. child = 0;
  460. while( child = parent->IterateChildren( child ) )
  461. @endverbatim
  462. IterateChildren takes the previous child as input and finds
  463. the next one. If the previous child is null, it returns the
  464. first. IterateChildren will return null when done.
  465. */
  466. const TiXmlNode* IterateChildren( const TiXmlNode* previous ) const;
  467. TiXmlNode* IterateChildren( const TiXmlNode* previous ) {
  468. return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( previous ) );
  469. }
  470. /// This flavor of IterateChildren searches for children with a particular 'value'
  471. const TiXmlNode* IterateChildren( const char * value, const TiXmlNode* previous ) const;
  472. TiXmlNode* IterateChildren( const char * _value, const TiXmlNode* previous ) {
  473. return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->IterateChildren( _value, previous ) );
  474. }
  475. #ifdef TIXML_USE_STL
  476. const TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) const { return IterateChildren (_value.c_str (), previous); } ///< STL std::string form.
  477. TiXmlNode* IterateChildren( const std::string& _value, const TiXmlNode* previous ) { return IterateChildren (_value.c_str (), previous); } ///< STL std::string form.
  478. #endif
  479. /** Add a new node related to this. Adds a child past the LastChild.
  480. Returns a pointer to the new object or NULL if an error occured.
  481. */
  482. TiXmlNode* InsertEndChild( const TiXmlNode& addThis );
  483. /** Add a new node related to this. Adds a child past the LastChild.
  484. NOTE: the node to be added is passed by pointer, and will be
  485. henceforth owned (and deleted) by tinyXml. This method is efficient
  486. and avoids an extra copy, but should be used with care as it
  487. uses a different memory model than the other insert functions.
  488. @sa InsertEndChild
  489. */
  490. TiXmlNode* LinkEndChild( TiXmlNode* addThis );
  491. /** Add a new node related to this. Adds a child before the specified child.
  492. Returns a pointer to the new object or NULL if an error occured.
  493. */
  494. TiXmlNode* InsertBeforeChild( TiXmlNode* beforeThis, const TiXmlNode& addThis );
  495. /** Add a new node related to this. Adds a child after the specified child.
  496. Returns a pointer to the new object or NULL if an error occured.
  497. */
  498. TiXmlNode* InsertAfterChild( TiXmlNode* afterThis, const TiXmlNode& addThis );
  499. /** Replace a child of this node.
  500. Returns a pointer to the new object or NULL if an error occured.
  501. */
  502. TiXmlNode* ReplaceChild( TiXmlNode* replaceThis, const TiXmlNode& withThis );
  503. /// Delete a child of this node.
  504. bool RemoveChild( TiXmlNode* removeThis );
  505. /// Navigate to a sibling node.
  506. const TiXmlNode* PreviousSibling() const { return prev; }
  507. TiXmlNode* PreviousSibling() { return prev; }
  508. /// Navigate to a sibling node.
  509. const TiXmlNode* PreviousSibling( const char * ) const;
  510. TiXmlNode* PreviousSibling( const char *_prev ) {
  511. return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->PreviousSibling( _prev ) );
  512. }
  513. #ifdef TIXML_USE_STL
  514. const TiXmlNode* PreviousSibling( const std::string& _value ) const { return PreviousSibling (_value.c_str ()); } ///< STL std::string form.
  515. TiXmlNode* PreviousSibling( const std::string& _value ) { return PreviousSibling (_value.c_str ()); } ///< STL std::string form.
  516. const TiXmlNode* NextSibling( const std::string& _value) const { return NextSibling (_value.c_str ()); } ///< STL std::string form.
  517. TiXmlNode* NextSibling( const std::string& _value) { return NextSibling (_value.c_str ()); } ///< STL std::string form.
  518. #endif
  519. /// Navigate to a sibling node.
  520. const TiXmlNode* NextSibling() const { return next; }
  521. TiXmlNode* NextSibling() { return next; }
  522. /// Navigate to a sibling node with the given 'value'.
  523. const TiXmlNode* NextSibling( const char * ) const;
  524. TiXmlNode* NextSibling( const char* _next ) {
  525. return const_cast< TiXmlNode* >( (const_cast< const TiXmlNode* >(this))->NextSibling( _next ) );
  526. }
  527. /** Convenience function to get through elements.
  528. Calls NextSibling and ToElement. Will skip all non-Element
  529. nodes. Returns 0 if there is not another element.
  530. */
  531. const TiXmlElement* NextSiblingElement() const;
  532. TiXmlElement* NextSiblingElement() {
  533. return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement() );
  534. }
  535. /** Convenience function to get through elements.
  536. Calls NextSibling and ToElement. Will skip all non-Element
  537. nodes. Returns 0 if there is not another element.
  538. */
  539. const TiXmlElement* NextSiblingElement( const char * ) const;
  540. TiXmlElement* NextSiblingElement( const char *_next ) {
  541. return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->NextSiblingElement( _next ) );
  542. }
  543. #ifdef TIXML_USE_STL
  544. const TiXmlElement* NextSiblingElement( const std::string& _value) const { return NextSiblingElement (_value.c_str ()); } ///< STL std::string form.
  545. TiXmlElement* NextSiblingElement( const std::string& _value) { return NextSiblingElement (_value.c_str ()); } ///< STL std::string form.
  546. #endif
  547. /// Convenience function to get through elements.
  548. const TiXmlElement* FirstChildElement() const;
  549. TiXmlElement* FirstChildElement() {
  550. return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement() );
  551. }
  552. /// Convenience function to get through elements.
  553. const TiXmlElement* FirstChildElement( const char * _value ) const;
  554. TiXmlElement* FirstChildElement( const char * _value ) {
  555. return const_cast< TiXmlElement* >( (const_cast< const TiXmlNode* >(this))->FirstChildElement( _value ) );
  556. }
  557. #ifdef TIXML_USE_STL
  558. const TiXmlElement* FirstChildElement( const std::string& _value ) const { return FirstChildElement (_value.c_str ()); } ///< STL std::string form.
  559. TiXmlElement* FirstChildElement( const std::string& _value ) { return FirstChildElement (_value.c_str ()); } ///< STL std::string form.
  560. #endif
  561. /** Query the type (as an enumerated value, above) of this node.
  562. The possible types are: TINYXML_DOCUMENT, TINYXML_ELEMENT, TINYXML_COMMENT,
  563. TINYXML_UNKNOWN, TINYXML_TEXT, and TINYXML_DECLARATION.
  564. */
  565. int Type() const { return type; }
  566. /** Return a pointer to the Document this node lives in.
  567. Returns null if not in a document.
  568. */
  569. const TiXmlDocument* GetDocument() const;
  570. TiXmlDocument* GetDocument() {
  571. return const_cast< TiXmlDocument* >( (const_cast< const TiXmlNode* >(this))->GetDocument() );
  572. }
  573. /// Returns true if this node has no children.
  574. bool NoChildren() const { return !firstChild; }
  575. virtual const TiXmlDocument* ToDocument() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  576. virtual const TiXmlElement* ToElement() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  577. virtual const TiXmlComment* ToComment() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  578. virtual const TiXmlUnknown* ToUnknown() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  579. virtual const TiXmlText* ToText() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  580. virtual const TiXmlDeclaration* ToDeclaration() const { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  581. virtual TiXmlDocument* ToDocument() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  582. virtual TiXmlElement* ToElement() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  583. virtual TiXmlComment* ToComment() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  584. virtual TiXmlUnknown* ToUnknown() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  585. virtual TiXmlText* ToText() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  586. virtual TiXmlDeclaration* ToDeclaration() { return 0; } ///< Cast to a more defined type. Will return null if not of the requested type.
  587. /** Create an exact duplicate of this node and return it. The memory must be deleted
  588. by the caller.
  589. */
  590. virtual TiXmlNode* Clone() const = 0;
  591. /** Accept a hierchical visit the nodes in the TinyXML DOM. Every node in the
  592. XML tree will be conditionally visited and the host will be called back
  593. via the TiXmlVisitor interface.
  594. This is essentially a SAX interface for TinyXML. (Note however it doesn't re-parse
  595. the XML for the callbacks, so the performance of TinyXML is unchanged by using this
  596. interface versus any other.)
  597. The interface has been based on ideas from:
  598. - http://www.saxproject.org/
  599. - http://c2.com/cgi/wiki?HierarchicalVisitorPattern
  600. Which are both good references for "visiting".
  601. An example of using Accept():
  602. @verbatim
  603. TiXmlPrinter printer;
  604. tinyxmlDoc.Accept( &printer );
  605. const char* xmlcstr = printer.CStr();
  606. @endverbatim
  607. */
  608. virtual bool Accept( TiXmlVisitor* visitor ) const = 0;
  609. protected:
  610. TiXmlNode( NodeType _type );
  611. // Copy to the allocated object. Shared functionality between Clone, Copy constructor,
  612. // and the assignment operator.
  613. void CopyTo( TiXmlNode* target ) const;
  614. #ifdef TIXML_USE_STL
  615. // The real work of the input operator.
  616. virtual void StreamIn( std::istream* in, TIXML_STRING* tag ) = 0;
  617. #endif
  618. // Figure out what is at *p, and parse it. Returns null if it is not an xml node.
  619. TiXmlNode* Identify( const char* start, TiXmlEncoding encoding );
  620. TiXmlNode* parent;
  621. NodeType type;
  622. TiXmlNode* firstChild;
  623. TiXmlNode* lastChild;
  624. TIXML_STRING value;
  625. TiXmlNode* prev;
  626. TiXmlNode* next;
  627. private:
  628. TiXmlNode( const TiXmlNode& ); // not implemented.
  629. void operator=( const TiXmlNode& base ); // not allowed.
  630. };
  631. /** An attribute is a name-value pair. Elements have an arbitrary
  632. number of attributes, each with a unique name.
  633. @note The attributes are not TiXmlNodes, since they are not
  634. part of the tinyXML document object model. There are other
  635. suggested ways to look at this problem.
  636. */
  637. class TiXmlAttribute : public TiXmlBase
  638. {
  639. friend class TiXmlAttributeSet;
  640. public:
  641. /// Construct an empty attribute.
  642. TiXmlAttribute() : TiXmlBase()
  643. {
  644. document = 0;
  645. prev = next = 0;
  646. }
  647. #ifdef TIXML_USE_STL
  648. /// std::string constructor.
  649. TiXmlAttribute( const std::string& _name, const std::string& _value )
  650. {
  651. name = _name;
  652. value = _value;
  653. document = 0;
  654. prev = next = 0;
  655. }
  656. #endif
  657. /// Construct an attribute with a name and value.
  658. TiXmlAttribute( const char * _name, const char * _value )
  659. {
  660. name = _name;
  661. value = _value;
  662. document = 0;
  663. prev = next = 0;
  664. }
  665. const char* Name() const { return name.c_str(); } ///< Return the name of this attribute.
  666. const char* Value() const { return value.c_str(); } ///< Return the value of this attribute.
  667. #ifdef TIXML_USE_STL
  668. const std::string& ValueStr() const { return value; } ///< Return the value of this attribute.
  669. #endif
  670. int IntValue() const; ///< Return the value of this attribute, converted to an integer.
  671. double DoubleValue() const; ///< Return the value of this attribute, converted to a double.
  672. // Get the tinyxml string representation
  673. const TIXML_STRING& NameTStr() const { return name; }
  674. /** QueryIntValue examines the value string. It is an alternative to the
  675. IntValue() method with richer error checking.
  676. If the value is an integer, it is stored in 'value' and
  677. the call returns TIXML_SUCCESS. If it is not
  678. an integer, it returns TIXML_WRONG_TYPE.
  679. A specialized but useful call. Note that for success it returns 0,
  680. which is the opposite of almost all other TinyXml calls.
  681. */
  682. int QueryIntValue( int* _value ) const;
  683. /// QueryDoubleValue examines the value string. See QueryIntValue().
  684. int QueryDoubleValue( double* _value ) const;
  685. void SetName( const char* _name ) { name = _name; } ///< Set the name of this attribute.
  686. void SetValue( const char* _value ) { value = _value; } ///< Set the value.
  687. void SetIntValue( int _value ); ///< Set the value from an integer.
  688. void SetDoubleValue( double _value ); ///< Set the value from a double.
  689. #ifdef TIXML_USE_STL
  690. /// STL std::string form.
  691. void SetName( const std::string& _name ) { name = _name; }
  692. /// STL std::string form.
  693. void SetValue( const std::string& _value ) { value = _value; }
  694. #endif
  695. /// Get the next sibling attribute in the DOM. Returns null at end.
  696. const TiXmlAttribute* Next() const;
  697. TiXmlAttribute* Next() {
  698. return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Next() );
  699. }
  700. /// Get the previous sibling attribute in the DOM. Returns null at beginning.
  701. const TiXmlAttribute* Previous() const;
  702. TiXmlAttribute* Previous() {
  703. return const_cast< TiXmlAttribute* >( (const_cast< const TiXmlAttribute* >(this))->Previous() );
  704. }
  705. bool operator==( const TiXmlAttribute& rhs ) const { return rhs.name == name; }
  706. bool operator<( const TiXmlAttribute& rhs ) const { return name < rhs.name; }
  707. bool operator>( const TiXmlAttribute& rhs ) const { return name > rhs.name; }
  708. /* Attribute parsing starts: first letter of the name
  709. returns: the next char after the value end quote
  710. */
  711. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  712. // Prints this Attribute to a FILE stream.
  713. virtual void Print( FILE* cfile, int depth ) const {
  714. Print( cfile, depth, 0 );
  715. }
  716. void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
  717. // [internal use]
  718. // Set the document pointer so the attribute can report errors.
  719. void SetDocument( TiXmlDocument* doc ) { document = doc; }
  720. private:
  721. TiXmlAttribute( const TiXmlAttribute& ); // not implemented.
  722. void operator=( const TiXmlAttribute& base ); // not allowed.
  723. TiXmlDocument* document; // A pointer back to a document, for error reporting.
  724. TIXML_STRING name;
  725. TIXML_STRING value;
  726. TiXmlAttribute* prev;
  727. TiXmlAttribute* next;
  728. };
  729. /* A class used to manage a group of attributes.
  730. It is only used internally, both by the ELEMENT and the DECLARATION.
  731. The set can be changed transparent to the Element and Declaration
  732. classes that use it, but NOT transparent to the Attribute
  733. which has to implement a next() and previous() method. Which makes
  734. it a bit problematic and prevents the use of STL.
  735. This version is implemented with circular lists because:
  736. - I like circular lists
  737. - it demonstrates some independence from the (typical) doubly linked list.
  738. */
  739. class TiXmlAttributeSet
  740. {
  741. public:
  742. TiXmlAttributeSet();
  743. ~TiXmlAttributeSet();
  744. void Add( TiXmlAttribute* attribute );
  745. void Remove( TiXmlAttribute* attribute );
  746. const TiXmlAttribute* First() const { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
  747. TiXmlAttribute* First() { return ( sentinel.next == &sentinel ) ? 0 : sentinel.next; }
  748. const TiXmlAttribute* Last() const { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
  749. TiXmlAttribute* Last() { return ( sentinel.prev == &sentinel ) ? 0 : sentinel.prev; }
  750. TiXmlAttribute* Find( const char* _name ) const;
  751. TiXmlAttribute* FindOrCreate( const char* _name );
  752. # ifdef TIXML_USE_STL
  753. TiXmlAttribute* Find( const std::string& _name ) const;
  754. TiXmlAttribute* FindOrCreate( const std::string& _name );
  755. # endif
  756. private:
  757. //*ME: Because of hidden/disabled copy-construktor in TiXmlAttribute (sentinel-element),
  758. //*ME: this class must be also use a hidden/disabled copy-constructor !!!
  759. TiXmlAttributeSet( const TiXmlAttributeSet& ); // not allowed
  760. void operator=( const TiXmlAttributeSet& ); // not allowed (as TiXmlAttribute)
  761. TiXmlAttribute sentinel;
  762. };
  763. /** The element is a container class. It has a value, the element name,
  764. and can contain other elements, text, comments, and unknowns.
  765. Elements also contain an arbitrary number of attributes.
  766. */
  767. class TiXmlElement : public TiXmlNode
  768. {
  769. public:
  770. /// Construct an element.
  771. TiXmlElement (const char * in_value);
  772. #ifdef TIXML_USE_STL
  773. /// std::string constructor.
  774. TiXmlElement( const std::string& _value );
  775. #endif
  776. TiXmlElement( const TiXmlElement& );
  777. TiXmlElement& operator=( const TiXmlElement& base );
  778. virtual ~TiXmlElement();
  779. /** Given an attribute name, Attribute() returns the value
  780. for the attribute of that name, or null if none exists.
  781. */
  782. const char* Attribute( const char* name ) const;
  783. /** Given an attribute name, Attribute() returns the value
  784. for the attribute of that name, or null if none exists.
  785. If the attribute exists and can be converted to an integer,
  786. the integer value will be put in the return 'i', if 'i'
  787. is non-null.
  788. */
  789. const char* Attribute( const char* name, int* i ) const;
  790. /** Given an attribute name, Attribute() returns the value
  791. for the attribute of that name, or null if none exists.
  792. If the attribute exists and can be converted to an double,
  793. the double value will be put in the return 'd', if 'd'
  794. is non-null.
  795. */
  796. const char* Attribute( const char* name, double* d ) const;
  797. /** QueryIntAttribute examines the attribute - it is an alternative to the
  798. Attribute() method with richer error checking.
  799. If the attribute is an integer, it is stored in 'value' and
  800. the call returns TIXML_SUCCESS. If it is not
  801. an integer, it returns TIXML_WRONG_TYPE. If the attribute
  802. does not exist, then TIXML_NO_ATTRIBUTE is returned.
  803. */
  804. int QueryIntAttribute( const char* name, int* _value ) const;
  805. /// QueryUnsignedAttribute examines the attribute - see QueryIntAttribute().
  806. int QueryUnsignedAttribute( const char* name, unsigned* _value ) const;
  807. /** QueryBoolAttribute examines the attribute - see QueryIntAttribute().
  808. Note that '1', 'true', or 'yes' are considered true, while '0', 'false'
  809. and 'no' are considered false.
  810. */
  811. int QueryBoolAttribute( const char* name, bool* _value ) const;
  812. /// QueryDoubleAttribute examines the attribute - see QueryIntAttribute().
  813. int QueryDoubleAttribute( const char* name, double* _value ) const;
  814. /// QueryFloatAttribute examines the attribute - see QueryIntAttribute().
  815. int QueryFloatAttribute( const char* name, float* _value ) const {
  816. double d;
  817. int result = QueryDoubleAttribute( name, &d );
  818. if ( result == TIXML_SUCCESS ) {
  819. *_value = (float)d;
  820. }
  821. return result;
  822. }
  823. #ifdef TIXML_USE_STL
  824. /// QueryStringAttribute examines the attribute - see QueryIntAttribute().
  825. int QueryStringAttribute( const char* name, std::string* _value ) const {
  826. const char* cstr = Attribute( name );
  827. if ( cstr ) {
  828. *_value = std::string( cstr );
  829. return TIXML_SUCCESS;
  830. }
  831. return TIXML_NO_ATTRIBUTE;
  832. }
  833. /** Template form of the attribute query which will try to read the
  834. attribute into the specified type. Very easy, very powerful, but
  835. be careful to make sure to call this with the correct type.
  836. NOTE: This method doesn't work correctly for 'string' types that contain spaces.
  837. @return TIXML_SUCCESS, TIXML_WRONG_TYPE, or TIXML_NO_ATTRIBUTE
  838. */
  839. template< typename T > int QueryValueAttribute( const std::string& name, T* outValue ) const
  840. {
  841. const TiXmlAttribute* node = attributeSet.Find( name );
  842. if ( !node )
  843. return TIXML_NO_ATTRIBUTE;
  844. std::stringstream sstream( node->ValueStr() );
  845. sstream >> *outValue;
  846. if ( !sstream.fail() )
  847. return TIXML_SUCCESS;
  848. return TIXML_WRONG_TYPE;
  849. }
  850. int QueryValueAttribute( const std::string& name, std::string* outValue ) const
  851. {
  852. const TiXmlAttribute* node = attributeSet.Find( name );
  853. if ( !node )
  854. return TIXML_NO_ATTRIBUTE;
  855. *outValue = node->ValueStr();
  856. return TIXML_SUCCESS;
  857. }
  858. #endif
  859. /** Sets an attribute of name to a given value. The attribute
  860. will be created if it does not exist, or changed if it does.
  861. */
  862. void SetAttribute( const char* name, const char * _value );
  863. #ifdef TIXML_USE_STL
  864. const std::string* Attribute( const std::string& name ) const;
  865. const std::string* Attribute( const std::string& name, int* i ) const;
  866. const std::string* Attribute( const std::string& name, double* d ) const;
  867. int QueryIntAttribute( const std::string& name, int* _value ) const;
  868. int QueryDoubleAttribute( const std::string& name, double* _value ) const;
  869. /// STL std::string form.
  870. void SetAttribute( const std::string& name, const std::string& _value );
  871. ///< STL std::string form.
  872. void SetAttribute( const std::string& name, int _value );
  873. ///< STL std::string form.
  874. void SetDoubleAttribute( const std::string& name, double value );
  875. #endif
  876. /** Sets an attribute of name to a given value. The attribute
  877. will be created if it does not exist, or changed if it does.
  878. */
  879. void SetAttribute( const char * name, int value );
  880. /** Sets an attribute of name to a given value. The attribute
  881. will be created if it does not exist, or changed if it does.
  882. */
  883. void SetDoubleAttribute( const char * name, double value );
  884. /** Deletes an attribute with the given name.
  885. */
  886. void RemoveAttribute( const char * name );
  887. #ifdef TIXML_USE_STL
  888. void RemoveAttribute( const std::string& name ) { RemoveAttribute (name.c_str ()); } ///< STL std::string form.
  889. #endif
  890. const TiXmlAttribute* FirstAttribute() const { return attributeSet.First(); } ///< Access the first attribute in this element.
  891. TiXmlAttribute* FirstAttribute() { return attributeSet.First(); }
  892. const TiXmlAttribute* LastAttribute() const { return attributeSet.Last(); } ///< Access the last attribute in this element.
  893. TiXmlAttribute* LastAttribute() { return attributeSet.Last(); }
  894. /** Convenience function for easy access to the text inside an element. Although easy
  895. and concise, GetText() is limited compared to getting the TiXmlText child
  896. and accessing it directly.
  897. If the first child of 'this' is a TiXmlText, the GetText()
  898. returns the character string of the Text node, else null is returned.
  899. This is a convenient method for getting the text of simple contained text:
  900. @verbatim
  901. <foo>This is text</foo>
  902. const char* str = fooElement->GetText();
  903. @endverbatim
  904. 'str' will be a pointer to "This is text".
  905. Note that this function can be misleading. If the element foo was created from
  906. this XML:
  907. @verbatim
  908. <foo><b>This is text</b></foo>
  909. @endverbatim
  910. then the value of str would be null. The first child node isn't a text node, it is
  911. another element. From this XML:
  912. @verbatim
  913. <foo>This is <b>text</b></foo>
  914. @endverbatim
  915. GetText() will return "This is ".
  916. WARNING: GetText() accesses a child node - don't become confused with the
  917. similarly named TiXmlHandle::Text() and TiXmlNode::ToText() which are
  918. safe type casts on the referenced node.
  919. */
  920. const char* GetText() const;
  921. /// Creates a new Element and returns it - the returned element is a copy.
  922. virtual TiXmlNode* Clone() const;
  923. // Print the Element to a FILE stream.
  924. virtual void Print( FILE* cfile, int depth ) const;
  925. /* Attribtue parsing starts: next char past '<'
  926. returns: next char past '>'
  927. */
  928. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  929. virtual const TiXmlElement* ToElement() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  930. virtual TiXmlElement* ToElement() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  931. /** Walk the XML tree visiting this node and all of its children.
  932. */
  933. virtual bool Accept( TiXmlVisitor* visitor ) const;
  934. protected:
  935. void CopyTo( TiXmlElement* target ) const;
  936. void ClearThis(); // like clear, but initializes 'this' object as well
  937. // Used to be public [internal use]
  938. #ifdef TIXML_USE_STL
  939. virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
  940. #endif
  941. /* [internal use]
  942. Reads the "value" of the element -- another element, or text.
  943. This should terminate with the current end tag.
  944. */
  945. const char* ReadValue( const char* in, TiXmlParsingData* prevData, TiXmlEncoding encoding );
  946. private:
  947. TiXmlAttributeSet attributeSet;
  948. };
  949. /** An XML comment.
  950. */
  951. class TiXmlComment : public TiXmlNode
  952. {
  953. public:
  954. /// Constructs an empty comment.
  955. TiXmlComment() : TiXmlNode( TiXmlNode::TINYXML_COMMENT ) {}
  956. /// Construct a comment from text.
  957. TiXmlComment( const char* _value ) : TiXmlNode( TiXmlNode::TINYXML_COMMENT ) {
  958. SetValue( _value );
  959. }
  960. TiXmlComment( const TiXmlComment& );
  961. TiXmlComment& operator=( const TiXmlComment& base );
  962. virtual ~TiXmlComment() {}
  963. /// Returns a copy of this Comment.
  964. virtual TiXmlNode* Clone() const;
  965. // Write this Comment to a FILE stream.
  966. virtual void Print( FILE* cfile, int depth ) const;
  967. /* Attribtue parsing starts: at the ! of the !--
  968. returns: next char past '>'
  969. */
  970. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  971. virtual const TiXmlComment* ToComment() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  972. virtual TiXmlComment* ToComment() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  973. /** Walk the XML tree visiting this node and all of its children.
  974. */
  975. virtual bool Accept( TiXmlVisitor* visitor ) const;
  976. protected:
  977. void CopyTo( TiXmlComment* target ) const;
  978. // used to be public
  979. #ifdef TIXML_USE_STL
  980. virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
  981. #endif
  982. // virtual void StreamOut( TIXML_OSTREAM * out ) const;
  983. private:
  984. };
  985. /** XML text. A text node can have 2 ways to output the next. "normal" output
  986. and CDATA. It will default to the mode it was parsed from the XML file and
  987. you generally want to leave it alone, but you can change the output mode with
  988. SetCDATA() and query it with CDATA().
  989. */
  990. class TiXmlText : public TiXmlNode
  991. {
  992. friend class TiXmlElement;
  993. public:
  994. /** Constructor for text element. By default, it is treated as
  995. normal, encoded text. If you want it be output as a CDATA text
  996. element, set the parameter _cdata to 'true'
  997. */
  998. TiXmlText (const char * initValue ) : TiXmlNode (TiXmlNode::TINYXML_TEXT)
  999. {
  1000. SetValue( initValue );
  1001. cdata = false;
  1002. }
  1003. virtual ~TiXmlText() {}
  1004. #ifdef TIXML_USE_STL
  1005. /// Constructor.
  1006. TiXmlText( const std::string& initValue ) : TiXmlNode (TiXmlNode::TINYXML_TEXT)
  1007. {
  1008. SetValue( initValue );
  1009. cdata = false;
  1010. }
  1011. #endif
  1012. TiXmlText( const TiXmlText& copy ) : TiXmlNode( TiXmlNode::TINYXML_TEXT ) { copy.CopyTo( this ); }
  1013. TiXmlText& operator=( const TiXmlText& base ) { base.CopyTo( this ); return *this; }
  1014. // Write this text object to a FILE stream.
  1015. virtual void Print( FILE* cfile, int depth ) const;
  1016. /// Queries whether this represents text using a CDATA section.
  1017. bool CDATA() const { return cdata; }
  1018. /// Turns on or off a CDATA representation of text.
  1019. void SetCDATA( bool _cdata ) { cdata = _cdata; }
  1020. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  1021. virtual const TiXmlText* ToText() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1022. virtual TiXmlText* ToText() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1023. /** Walk the XML tree visiting this node and all of its children.
  1024. */
  1025. virtual bool Accept( TiXmlVisitor* content ) const;
  1026. protected :
  1027. /// [internal use] Creates a new Element and returns it.
  1028. virtual TiXmlNode* Clone() const;
  1029. void CopyTo( TiXmlText* target ) const;
  1030. bool Blank() const; // returns true if all white space and new lines
  1031. // [internal use]
  1032. #ifdef TIXML_USE_STL
  1033. virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
  1034. #endif
  1035. private:
  1036. bool cdata; // true if this should be input and output as a CDATA style text element
  1037. };
  1038. /** In correct XML the declaration is the first entry in the file.
  1039. @verbatim
  1040. <?xml version="1.0" standalone="yes"?>
  1041. @endverbatim
  1042. TinyXml will happily read or write files without a declaration,
  1043. however. There are 3 possible attributes to the declaration:
  1044. version, encoding, and standalone.
  1045. Note: In this version of the code, the attributes are
  1046. handled as special cases, not generic attributes, simply
  1047. because there can only be at most 3 and they are always the same.
  1048. */
  1049. class TiXmlDeclaration : public TiXmlNode
  1050. {
  1051. public:
  1052. /// Construct an empty declaration.
  1053. TiXmlDeclaration() : TiXmlNode( TiXmlNode::TINYXML_DECLARATION ) {}
  1054. #ifdef TIXML_USE_STL
  1055. /// Constructor.
  1056. TiXmlDeclaration( const std::string& _version,
  1057. const std::string& _encoding,
  1058. const std::string& _standalone );
  1059. #endif
  1060. /// Construct.
  1061. TiXmlDeclaration( const char* _version,
  1062. const char* _encoding,
  1063. const char* _standalone );
  1064. TiXmlDeclaration( const TiXmlDeclaration& copy );
  1065. TiXmlDeclaration& operator=( const TiXmlDeclaration& copy );
  1066. virtual ~TiXmlDeclaration() {}
  1067. /// Version. Will return an empty string if none was found.
  1068. const char *Version() const { return version.c_str (); }
  1069. /// Encoding. Will return an empty string if none was found.
  1070. const char *Encoding() const { return encoding.c_str (); }
  1071. /// Is this a standalone document?
  1072. const char *Standalone() const { return standalone.c_str (); }
  1073. /// Creates a copy of this Declaration and returns it.
  1074. virtual TiXmlNode* Clone() const;
  1075. // Print this declaration to a FILE stream.
  1076. virtual void Print( FILE* cfile, int depth, TIXML_STRING* str ) const;
  1077. virtual void Print( FILE* cfile, int depth ) const {
  1078. Print( cfile, depth, 0 );
  1079. }
  1080. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  1081. virtual const TiXmlDeclaration* ToDeclaration() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1082. virtual TiXmlDeclaration* ToDeclaration() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1083. /** Walk the XML tree visiting this node and all of its children.
  1084. */
  1085. virtual bool Accept( TiXmlVisitor* visitor ) const;
  1086. protected:
  1087. void CopyTo( TiXmlDeclaration* target ) const;
  1088. // used to be public
  1089. #ifdef TIXML_USE_STL
  1090. virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
  1091. #endif
  1092. private:
  1093. TIXML_STRING version;
  1094. TIXML_STRING encoding;
  1095. TIXML_STRING standalone;
  1096. };
  1097. /** Any tag that tinyXml doesn't recognize is saved as an
  1098. unknown. It is a tag of text, but should not be modified.
  1099. It will be written back to the XML, unchanged, when the file
  1100. is saved.
  1101. DTD tags get thrown into TiXmlUnknowns.
  1102. */
  1103. class TiXmlUnknown : public TiXmlNode
  1104. {
  1105. public:
  1106. TiXmlUnknown() : TiXmlNode( TiXmlNode::TINYXML_UNKNOWN ) {}
  1107. virtual ~TiXmlUnknown() {}
  1108. TiXmlUnknown( const TiXmlUnknown& copy ) : TiXmlNode( TiXmlNode::TINYXML_UNKNOWN ) { copy.CopyTo( this ); }
  1109. TiXmlUnknown& operator=( const TiXmlUnknown& copy ) { copy.CopyTo( this ); return *this; }
  1110. /// Creates a copy of this Unknown and returns it.
  1111. virtual TiXmlNode* Clone() const;
  1112. // Print this Unknown to a FILE stream.
  1113. virtual void Print( FILE* cfile, int depth ) const;
  1114. virtual const char* Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding );
  1115. virtual const TiXmlUnknown* ToUnknown() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1116. virtual TiXmlUnknown* ToUnknown() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1117. /** Walk the XML tree visiting this node and all of its children.
  1118. */
  1119. virtual bool Accept( TiXmlVisitor* content ) const;
  1120. protected:
  1121. void CopyTo( TiXmlUnknown* target ) const;
  1122. #ifdef TIXML_USE_STL
  1123. virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
  1124. #endif
  1125. private:
  1126. };
  1127. /** Always the top level node. A document binds together all the
  1128. XML pieces. It can be saved, loaded, and printed to the screen.
  1129. The 'value' of a document node is the xml file name.
  1130. */
  1131. class TiXmlDocument : public TiXmlNode
  1132. {
  1133. public:
  1134. /// Create an empty document, that has no name.
  1135. TiXmlDocument();
  1136. /// Create a document with a name. The name of the document is also the filename of the xml.
  1137. TiXmlDocument( const char * documentName );
  1138. #ifdef TIXML_USE_STL
  1139. /// Constructor.
  1140. TiXmlDocument( const std::string& documentName );
  1141. #endif
  1142. TiXmlDocument( const TiXmlDocument& copy );
  1143. TiXmlDocument& operator=( const TiXmlDocument& copy );
  1144. virtual ~TiXmlDocument() {}
  1145. /** Load a file using the current document value.
  1146. Returns true if successful. Will delete any existing
  1147. document data before loading.
  1148. */
  1149. bool LoadFile( TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
  1150. /// Save a file using the current document value. Returns true if successful.
  1151. bool SaveFile() const;
  1152. /// Load a file using the given filename. Returns true if successful.
  1153. bool LoadFile( const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
  1154. /// Save a file using the given filename. Returns true if successful.
  1155. bool SaveFile( const char * filename ) const;
  1156. /** Load a file using the given FILE*. Returns true if successful. Note that this method
  1157. doesn't stream - the entire object pointed at by the FILE*
  1158. will be interpreted as an XML file. TinyXML doesn't stream in XML from the current
  1159. file location. Streaming may be added in the future.
  1160. */
  1161. bool LoadFile( FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
  1162. /// Save a file using the given FILE*. Returns true if successful.
  1163. bool SaveFile( FILE* ) const;
  1164. #ifdef TIXML_USE_STL
  1165. bool LoadFile( const std::string& filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING ) ///< STL std::string version.
  1166. {
  1167. return LoadFile( filename.c_str(), encoding );
  1168. }
  1169. bool SaveFile( const std::string& filename ) const ///< STL std::string version.
  1170. {
  1171. return SaveFile( filename.c_str() );
  1172. }
  1173. #endif
  1174. /** Parse the given null terminated block of xml data. Passing in an encoding to this
  1175. method (either TIXML_ENCODING_LEGACY or TIXML_ENCODING_UTF8 will force TinyXml
  1176. to use that encoding, regardless of what TinyXml might otherwise try to detect.
  1177. */
  1178. virtual const char* Parse( const char* p, TiXmlParsingData* data = 0, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING );
  1179. /** Get the root element -- the only top level element -- of the document.
  1180. In well formed XML, there should only be one. TinyXml is tolerant of
  1181. multiple elements at the document level.
  1182. */
  1183. const TiXmlElement* RootElement() const { return FirstChildElement(); }
  1184. TiXmlElement* RootElement() { return FirstChildElement(); }
  1185. /** If an error occurs, Error will be set to true. Also,
  1186. - The ErrorId() will contain the integer identifier of the error (not generally useful)
  1187. - The ErrorDesc() method will return the name of the error. (very useful)
  1188. - The ErrorRow() and ErrorCol() will return the location of the error (if known)
  1189. */
  1190. bool Error() const { return error; }
  1191. /// Contains a textual (english) description of the error if one occurs.
  1192. const char * ErrorDesc() const { return errorDesc.c_str (); }
  1193. /** Generally, you probably want the error string ( ErrorDesc() ). But if you
  1194. prefer the ErrorId, this function will fetch it.
  1195. */
  1196. int ErrorId() const { return errorId; }
  1197. /** Returns the location (if known) of the error. The first column is column 1,
  1198. and the first row is row 1. A value of 0 means the row and column wasn't applicable
  1199. (memory errors, for example, have no row/column) or the parser lost the error. (An
  1200. error in the error reporting, in that case.)
  1201. @sa SetTabSize, Row, Column
  1202. */
  1203. int ErrorRow() const { return errorLocation.row+1; }
  1204. int ErrorCol() const { return errorLocation.col+1; } ///< The column where the error occured. See ErrorRow()
  1205. /** SetTabSize() allows the error reporting functions (ErrorRow() and ErrorCol())
  1206. to report the correct values for row and column. It does not change the output
  1207. or input in any way.
  1208. By calling this method, with a tab size
  1209. greater than 0, the row and column of each node and attribute is stored
  1210. when the file is loaded. Very useful for tracking the DOM back in to
  1211. the source file.
  1212. The tab size is required for calculating the location of nodes. If not
  1213. set, the default of 4 is used. The tabsize is set per document. Setting
  1214. the tabsize to 0 disables row/column tracking.
  1215. Note that row and column tracking is not supported when using operator>>.
  1216. The tab size needs to be enabled before the parse or load. Correct usage:
  1217. @verbatim
  1218. TiXmlDocument doc;
  1219. doc.SetTabSize( 8 );
  1220. doc.Load( "myfile.xml" );
  1221. @endverbatim
  1222. @sa Row, Column
  1223. */
  1224. void SetTabSize( int _tabsize ) { tabsize = _tabsize; }
  1225. int TabSize() const { return tabsize; }
  1226. /** If you have handled the error, it can be reset with this call. The error
  1227. state is automatically cleared if you Parse a new XML block.
  1228. */
  1229. void ClearError() { error = false;
  1230. errorId = 0;
  1231. errorDesc = "";
  1232. errorLocation.row = errorLocation.col = 0;
  1233. //errorLocation.last = 0;
  1234. }
  1235. /** Write the document to standard out using formatted printing ("pretty print"). */
  1236. void Print() const { Print( stdout, 0 ); }
  1237. /* Write the document to a string using formatted printing ("pretty print"). This
  1238. will allocate a character array (new char[]) and return it as a pointer. The
  1239. calling code pust call delete[] on the return char* to avoid a memory leak.
  1240. */
  1241. //char* PrintToMemory() const;
  1242. /// Print this Document to a FILE stream.
  1243. virtual void Print( FILE* cfile, int depth = 0 ) const;
  1244. // [internal use]
  1245. void SetError( int err, const char* errorLocation, TiXmlParsingData* prevData, TiXmlEncoding encoding );
  1246. virtual const TiXmlDocument* ToDocument() const { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1247. virtual TiXmlDocument* ToDocument() { return this; } ///< Cast to a more defined type. Will return null not of the requested type.
  1248. /** Walk the XML tree visiting this node and all of its children.
  1249. */
  1250. virtual bool Accept( TiXmlVisitor* content ) const;
  1251. protected :
  1252. // [internal use]
  1253. virtual TiXmlNode* Clone() const;
  1254. #ifdef TIXML_USE_STL
  1255. virtual void StreamIn( std::istream * in, TIXML_STRING * tag );
  1256. #endif
  1257. private:
  1258. void CopyTo( TiXmlDocument* target ) const;
  1259. bool error;
  1260. int errorId;
  1261. TIXML_STRING errorDesc;
  1262. int tabsize;
  1263. TiXmlCursor errorLocation;
  1264. bool useMicrosoftBOM; // the UTF-8 BOM were found when read. Note this, and try to write.
  1265. };
  1266. /**
  1267. A TiXmlHandle is a class that wraps a node pointer with null checks; this is
  1268. an incredibly useful thing. Note that TiXmlHandle is not part of the TinyXml
  1269. DOM structure. It is a separate utility class.
  1270. Take an example:
  1271. @verbatim
  1272. <Document>
  1273. <Element attributeA = "valueA">
  1274. <Child attributeB = "value1" />
  1275. <Child attributeB = "value2" />
  1276. </Element>
  1277. <Document>
  1278. @endverbatim
  1279. Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
  1280. easy to write a *lot* of code that looks like:
  1281. @verbatim
  1282. TiXmlElement* root = document.FirstChildElement( "Document" );
  1283. if ( root )
  1284. {
  1285. TiXmlElement* element = root->FirstChildElement( "Element" );
  1286. if ( element )
  1287. {
  1288. TiXmlElement* child = element->FirstChildElement( "Child" );
  1289. if ( child )
  1290. {
  1291. TiXmlElement* child2 = child->NextSiblingElement( "Child" );
  1292. if ( child2 )
  1293. {
  1294. // Finally do something useful.
  1295. @endverbatim
  1296. And that doesn't even cover "else" cases. TiXmlHandle addresses the verbosity
  1297. of such code. A TiXmlHandle checks for null pointers so it is perfectly safe
  1298. and correct to use:
  1299. @verbatim
  1300. TiXmlHandle docHandle( &document );
  1301. TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).ToElement();
  1302. if ( child2 )
  1303. {
  1304. // do something useful
  1305. @endverbatim
  1306. Which is MUCH more concise and useful.
  1307. It is also safe to copy handles - internally they are nothing more than node pointers.
  1308. @verbatim
  1309. TiXmlHandle handleCopy = handle;
  1310. @endverbatim
  1311. What they should not be used for is iteration:
  1312. @verbatim
  1313. int i=0;
  1314. while ( true )
  1315. {
  1316. TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", i ).ToElement();
  1317. if ( !child )
  1318. break;
  1319. // do something
  1320. ++i;
  1321. }
  1322. @endverbatim
  1323. It seems reasonable, but it is in fact two embedded while loops. The Child method is
  1324. a linear walk to find the element, so this code would iterate much more than it needs
  1325. to. Instead, prefer:
  1326. @verbatim
  1327. TiXmlElement* child = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).FirstChild( "Child" ).ToElement();
  1328. for( child; child; child=child->NextSiblingElement() )
  1329. {
  1330. // do something
  1331. }
  1332. @endverbatim
  1333. */
  1334. class TiXmlHandle
  1335. {
  1336. public:
  1337. /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
  1338. TiXmlHandle( TiXmlNode* _node ) { this->node = _node; }
  1339. /// Copy constructor
  1340. TiXmlHandle( const TiXmlHandle& ref ) { this->node = ref.node; }
  1341. TiXmlHandle operator=( const TiXmlHandle& ref ) { if ( &ref != this ) this->node = ref.node; return *this; }
  1342. /// Return a handle to the first child node.
  1343. TiXmlHandle FirstChild() const;
  1344. /// Return a handle to the first child node with the given name.
  1345. TiXmlHandle FirstChild( const char * value ) const;
  1346. /// Return a handle to the first child element.
  1347. TiXmlHandle FirstChildElement() const;
  1348. /// Return a handle to the first child element with the given name.
  1349. TiXmlHandle FirstChildElement( const char * value ) const;
  1350. /** Return a handle to the "index" child with the given name.
  1351. The first child is 0, the second 1, etc.
  1352. */
  1353. TiXmlHandle Child( const char* value, int index ) const;
  1354. /** Return a handle to the "index" child.
  1355. The first child is 0, the second 1, etc.
  1356. */
  1357. TiXmlHandle Child( int index ) const;
  1358. /** Return a handle to the "index" child element with the given name.
  1359. The first child element is 0, the second 1, etc. Note that only TiXmlElements
  1360. are indexed: other types are not counted.
  1361. */
  1362. TiXmlHandle ChildElement( const char* value, int index ) const;
  1363. /** Return a handle to the "index" child element.
  1364. The first child element is 0, the second 1, etc. Note that only TiXmlElements
  1365. are indexed: other types are not counted.
  1366. */
  1367. TiXmlHandle ChildElement( int index ) const;
  1368. #ifdef TIXML_USE_STL
  1369. TiXmlHandle FirstChild( const std::string& _value ) const { return FirstChild( _value.c_str() ); }
  1370. TiXmlHandle FirstChildElement( const std::string& _value ) const { return FirstChildElement( _value.c_str() ); }
  1371. TiXmlHandle Child( const std::string& _value, int index ) const { return Child( _value.c_str(), index ); }
  1372. TiXmlHandle ChildElement( const std::string& _value, int index ) const { return ChildElement( _value.c_str(), index ); }
  1373. #endif
  1374. /** Return the handle as a TiXmlNode. This may return null.
  1375. */
  1376. TiXmlNode* ToNode() const { return node; }
  1377. /** Return the handle as a TiXmlElement. This may return null.
  1378. */
  1379. TiXmlElement* ToElement() const { return ( ( node && node->ToElement() ) ? node->ToElement() : 0 ); }
  1380. /** Return the handle as a TiXmlText. This may return null.
  1381. */
  1382. TiXmlText* ToText() const { return ( ( node && node->ToText() ) ? node->ToText() : 0 ); }
  1383. /** Return the handle as a TiXmlUnknown. This may return null.
  1384. */
  1385. TiXmlUnknown* ToUnknown() const { return ( ( node && node->ToUnknown() ) ? node->ToUnknown() : 0 ); }
  1386. /** @deprecated use ToNode.
  1387. Return the handle as a TiXmlNode. This may return null.
  1388. */
  1389. TiXmlNode* Node() const { return ToNode(); }
  1390. /** @deprecated use ToElement.
  1391. Return the handle as a TiXmlElement. This may return null.
  1392. */
  1393. TiXmlElement* Element() const { return ToElement(); }
  1394. /** @deprecated use ToText()
  1395. Return the handle as a TiXmlText. This may return null.
  1396. */
  1397. TiXmlText* Text() const { return ToText(); }
  1398. /** @deprecated use ToUnknown()
  1399. Return the handle as a TiXmlUnknown. This may return null.
  1400. */
  1401. TiXmlUnknown* Unknown() const { return ToUnknown(); }
  1402. private:
  1403. TiXmlNode* node;
  1404. };
  1405. /** Print to memory functionality. The TiXmlPrinter is useful when you need to:
  1406. -# Print to memory (especially in non-STL mode)
  1407. -# Control formatting (line endings, etc.)
  1408. When constructed, the TiXmlPrinter is in its default "pretty printing" mode.
  1409. Before calling Accept() you can call methods to control the printing
  1410. of the XML document. After TiXmlNode::Accept() is called, the printed document can
  1411. be accessed via the CStr(), Str(), and Size() methods.
  1412. TiXmlPrinter uses the Visitor API.
  1413. @verbatim
  1414. TiXmlPrinter printer;
  1415. printer.SetIndent( "\t" );
  1416. doc.Accept( &printer );
  1417. fprintf( stdout, "%s", printer.CStr() );
  1418. @endverbatim
  1419. */
  1420. class TiXmlPrinter : public TiXmlVisitor
  1421. {
  1422. public:
  1423. TiXmlPrinter() : depth( 0 ), simpleTextPrint( false ),
  1424. buffer(), indent( " " ), lineBreak( "\n" ) {}
  1425. virtual bool VisitEnter( const TiXmlDocument& doc );
  1426. virtual bool VisitExit( const TiXmlDocument& doc );
  1427. virtual bool VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute );
  1428. virtual bool VisitExit( const TiXmlElement& element );
  1429. virtual bool Visit( const TiXmlDeclaration& declaration );
  1430. virtual bool Visit( const TiXmlText& text );
  1431. virtual bool Visit( const TiXmlComment& comment );
  1432. virtual bool Visit( const TiXmlUnknown& unknown );
  1433. /** Set the indent characters for printing. By default 4 spaces
  1434. but tab (\t) is also useful, or null/empty string for no indentation.
  1435. */
  1436. void SetIndent( const char* _indent ) { indent = _indent ? _indent : "" ; }
  1437. /// Query the indention string.
  1438. const char* Indent() { return indent.c_str(); }
  1439. /** Set the line breaking string. By default set to newline (\n).
  1440. Some operating systems prefer other characters, or can be
  1441. set to the null/empty string for no indenation.
  1442. */
  1443. void SetLineBreak( const char* _lineBreak ) { lineBreak = _lineBreak ? _lineBreak : ""; }
  1444. /// Query the current line breaking string.
  1445. const char* LineBreak() { return lineBreak.c_str(); }
  1446. /** Switch over to "stream printing" which is the most dense formatting without
  1447. linebreaks. Common when the XML is needed for network transmission.
  1448. */
  1449. void SetStreamPrinting() { indent = "";
  1450. lineBreak = "";
  1451. }
  1452. /// Return the result.
  1453. const char* CStr() { return buffer.c_str(); }
  1454. /// Return the length of the result string.
  1455. size_t Size() { return buffer.size(); }
  1456. #ifdef TIXML_USE_STL
  1457. /// Return the result.
  1458. const std::string& Str() { return buffer; }
  1459. #endif
  1460. private:
  1461. void DoIndent() {
  1462. for( int i=0; i<depth; ++i )
  1463. buffer += indent;
  1464. }
  1465. void DoLineBreak() {
  1466. buffer += lineBreak;
  1467. }
  1468. int depth;
  1469. bool simpleTextPrint;
  1470. TIXML_STRING buffer;
  1471. TIXML_STRING indent;
  1472. TIXML_STRING lineBreak;
  1473. };
  1474. #ifdef _MSC_VER
  1475. #pragma warning( pop )
  1476. #endif
  1477. #endif