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