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