/src/tinyxml2/tinyxml2.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Original code by Lee Thomason (www.grinninglizard.com) |
3 | | |
4 | | This software is provided 'as-is', without any express or implied |
5 | | warranty. In no event will the authors be held liable for any |
6 | | damages arising from the use of this software. |
7 | | |
8 | | Permission is granted to anyone to use this software for any |
9 | | purpose, including commercial applications, and to alter it and |
10 | | redistribute it freely, subject to the following restrictions: |
11 | | |
12 | | 1. The origin of this software must not be misrepresented; you must |
13 | | not claim that you wrote the original software. If you use this |
14 | | software in a product, an acknowledgment in the product documentation |
15 | | would be appreciated but is not required. |
16 | | |
17 | | 2. Altered source versions must be plainly marked as such, and |
18 | | must not be misrepresented as being the original software. |
19 | | |
20 | | 3. This notice may not be removed or altered from any source |
21 | | distribution. |
22 | | */ |
23 | | |
24 | | #include "tinyxml2.h" |
25 | | |
26 | | #include <new> // yes, this one new style header, is in the Android SDK. |
27 | | #if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__) || defined(__CC_ARM) |
28 | | # include <stddef.h> |
29 | | # include <stdarg.h> |
30 | | #else |
31 | | # include <cstddef> |
32 | | # include <cstdarg> |
33 | | #endif |
34 | | |
35 | | #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE) |
36 | | // Microsoft Visual Studio, version 2005 and higher. Not WinCE. |
37 | | /*int _snprintf_s( |
38 | | char *buffer, |
39 | | size_t sizeOfBuffer, |
40 | | size_t count, |
41 | | const char *format [, |
42 | | argument] ... |
43 | | );*/ |
44 | | static inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... ) |
45 | | { |
46 | | va_list va; |
47 | | va_start( va, format ); |
48 | | const int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va ); |
49 | | va_end( va ); |
50 | | return result; |
51 | | } |
52 | | |
53 | | static inline int TIXML_VSNPRINTF( char* buffer, size_t size, const char* format, va_list va ) |
54 | | { |
55 | | const int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va ); |
56 | | return result; |
57 | | } |
58 | | |
59 | | #define TIXML_VSCPRINTF _vscprintf |
60 | | #define TIXML_SSCANF sscanf_s |
61 | | #elif defined _MSC_VER |
62 | | // Microsoft Visual Studio 2003 and earlier or WinCE |
63 | | #define TIXML_SNPRINTF _snprintf |
64 | | #define TIXML_VSNPRINTF _vsnprintf |
65 | | #define TIXML_SSCANF sscanf |
66 | | #if (_MSC_VER < 1400 ) && (!defined WINCE) |
67 | | // Microsoft Visual Studio 2003 and not WinCE. |
68 | | #define TIXML_VSCPRINTF _vscprintf // VS2003's C runtime has this, but VC6 C runtime or WinCE SDK doesn't have. |
69 | | #else |
70 | | // Microsoft Visual Studio 2003 and earlier or WinCE. |
71 | | static inline int TIXML_VSCPRINTF( const char* format, va_list va ) |
72 | | { |
73 | | int len = 512; |
74 | | for (;;) { |
75 | | len = len*2; |
76 | | char* str = new char[len](); |
77 | | const int required = _vsnprintf(str, len, format, va); |
78 | | delete[] str; |
79 | | if ( required != -1 ) { |
80 | | TIXMLASSERT( required >= 0 ); |
81 | | len = required; |
82 | | break; |
83 | | } |
84 | | } |
85 | | TIXMLASSERT( len >= 0 ); |
86 | | return len; |
87 | | } |
88 | | #endif |
89 | | #else |
90 | | // GCC version 3 and higher |
91 | | //#warning( "Using sn* functions." ) |
92 | 3.34k | #define TIXML_SNPRINTF snprintf |
93 | 1.17k | #define TIXML_VSNPRINTF vsnprintf |
94 | | static inline int TIXML_VSCPRINTF( const char* format, va_list va ) |
95 | 0 | { |
96 | 0 | int len = vsnprintf( 0, 0, format, va ); |
97 | 0 | TIXMLASSERT( len >= 0 ); |
98 | 0 | return len; |
99 | 0 | } |
100 | 0 | #define TIXML_SSCANF sscanf |
101 | | #endif |
102 | | |
103 | | #if defined(_WIN64) |
104 | | #define TIXML_FSEEK _fseeki64 |
105 | | #define TIXML_FTELL _ftelli64 |
106 | | #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__CYGWIN__) |
107 | | #define TIXML_FSEEK fseeko |
108 | | #define TIXML_FTELL ftello |
109 | | #elif defined(__ANDROID__) && __ANDROID_API__ > 24 |
110 | | #define TIXML_FSEEK fseeko64 |
111 | | #define TIXML_FTELL ftello64 |
112 | | #else |
113 | 0 | #define TIXML_FSEEK fseek |
114 | 0 | #define TIXML_FTELL ftell |
115 | | #endif |
116 | | |
117 | | |
118 | | static const char LINE_FEED = static_cast<char>(0x0a); // all line endings are normalized to LF |
119 | | static const char LF = LINE_FEED; |
120 | | static const char CARRIAGE_RETURN = static_cast<char>(0x0d); // CR gets filtered out |
121 | | static const char CR = CARRIAGE_RETURN; |
122 | | static const char SINGLE_QUOTE = '\''; |
123 | | static const char DOUBLE_QUOTE = '\"'; |
124 | | |
125 | | // Bunch of unicode info at: |
126 | | // http://www.unicode.org/faq/utf_bom.html |
127 | | // ef bb bf (Microsoft "lead bytes") - designates UTF-8 |
128 | | |
129 | | static const unsigned char TIXML_UTF_LEAD_0 = 0xefU; |
130 | | static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU; |
131 | | static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU; |
132 | | |
133 | | namespace tinyxml2 |
134 | | { |
135 | | |
136 | | struct Entity { |
137 | | const char* pattern; |
138 | | int length; |
139 | | char value; |
140 | | }; |
141 | | |
142 | | static const int NUM_ENTITIES = 5; |
143 | | static const Entity entities[NUM_ENTITIES] = { |
144 | | { "quot", 4, DOUBLE_QUOTE }, |
145 | | { "amp", 3, '&' }, |
146 | | { "apos", 4, SINGLE_QUOTE }, |
147 | | { "lt", 2, '<' }, |
148 | | { "gt", 2, '>' } |
149 | | }; |
150 | | |
151 | | |
152 | | StrPair::~StrPair() |
153 | 23.8M | { |
154 | 23.8M | Reset(); |
155 | 23.8M | } |
156 | | |
157 | | |
158 | | void StrPair::TransferTo( StrPair* other ) |
159 | 3.78k | { |
160 | 3.78k | if ( this == other ) { |
161 | 0 | return; |
162 | 0 | } |
163 | | // This in effect implements the assignment operator by "moving" |
164 | | // ownership (as in auto_ptr). |
165 | | |
166 | 3.78k | TIXMLASSERT( other != 0 ); |
167 | 3.78k | TIXMLASSERT( other->_flags == 0 ); |
168 | 3.78k | TIXMLASSERT( other->_start == 0 ); |
169 | 3.78k | TIXMLASSERT( other->_end == 0 ); |
170 | | |
171 | 3.78k | other->Reset(); |
172 | | |
173 | 3.78k | other->_flags = _flags; |
174 | 3.78k | other->_start = _start; |
175 | 3.78k | other->_end = _end; |
176 | | |
177 | 3.78k | _flags = 0; |
178 | 3.78k | _start = 0; |
179 | 3.78k | _end = 0; |
180 | 3.78k | } |
181 | | |
182 | | |
183 | | void StrPair::Reset() |
184 | 37.6M | { |
185 | 37.6M | if ( _flags & NEEDS_DELETE ) { |
186 | 2.17k | delete [] _start; |
187 | 2.17k | } |
188 | 37.6M | _flags = 0; |
189 | 37.6M | _start = 0; |
190 | 37.6M | _end = 0; |
191 | 37.6M | } |
192 | | |
193 | | |
194 | | void StrPair::SetStr( const char* str, int flags ) |
195 | 2.17k | { |
196 | 2.17k | TIXMLASSERT( str ); |
197 | 2.17k | Reset(); |
198 | 2.17k | size_t len = strlen( str ); |
199 | 2.17k | TIXMLASSERT( _start == 0 ); |
200 | 2.17k | _start = new char[ len+1 ]; |
201 | 2.17k | memcpy( _start, str, len+1 ); |
202 | 2.17k | _end = _start + len; |
203 | 2.17k | _flags = flags | NEEDS_DELETE; |
204 | 2.17k | } |
205 | | |
206 | | |
207 | | char* StrPair::ParseText( char* p, const char* endTag, int strFlags, int* curLineNumPtr ) |
208 | 10.1M | { |
209 | 10.1M | TIXMLASSERT( p ); |
210 | 10.1M | TIXMLASSERT( endTag && *endTag ); |
211 | 10.1M | TIXMLASSERT(curLineNumPtr); |
212 | | |
213 | 10.1M | char* start = p; |
214 | 10.1M | const char endChar = *endTag; |
215 | 10.1M | size_t length = strlen( endTag ); |
216 | | |
217 | | // Inner loop of text parsing. |
218 | 13.1M | while ( *p ) { |
219 | 13.1M | if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) { |
220 | 10.1M | Set( start, p, strFlags ); |
221 | 10.1M | return p + length; |
222 | 10.1M | } else if (*p == '\n') { |
223 | 3.17k | ++(*curLineNumPtr); |
224 | 3.17k | } |
225 | 2.90M | ++p; |
226 | 2.90M | TIXMLASSERT( p ); |
227 | 2.90M | } |
228 | 697 | return 0; |
229 | 10.1M | } |
230 | | |
231 | | |
232 | | char* StrPair::ParseName( char* p ) |
233 | 3.57M | { |
234 | 3.57M | if ( !p || !(*p) ) { |
235 | 139 | return 0; |
236 | 139 | } |
237 | 3.57M | if ( !XMLUtil::IsNameStartChar( static_cast<unsigned char>(*p) ) ) { |
238 | 49 | return 0; |
239 | 49 | } |
240 | | |
241 | 3.57M | char* const start = p; |
242 | 3.57M | ++p; |
243 | 6.04M | while ( *p && XMLUtil::IsNameChar( static_cast<unsigned char>(*p) ) ) { |
244 | 2.47M | ++p; |
245 | 2.47M | } |
246 | | |
247 | 3.57M | Set( start, p, 0 ); |
248 | 3.57M | return p; |
249 | 3.57M | } |
250 | | |
251 | | |
252 | | void StrPair::CollapseWhitespace() |
253 | 0 | { |
254 | | // Adjusting _start would cause undefined behavior on delete[] |
255 | 0 | TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 ); |
256 | | // Trim leading space. |
257 | 0 | _start = XMLUtil::SkipWhiteSpace( _start, 0 ); |
258 | |
|
259 | 0 | if ( *_start ) { |
260 | 0 | const char* p = _start; // the read pointer |
261 | 0 | char* q = _start; // the write pointer |
262 | |
|
263 | 0 | while( *p ) { |
264 | 0 | if ( XMLUtil::IsWhiteSpace( *p )) { |
265 | 0 | p = XMLUtil::SkipWhiteSpace( p, 0 ); |
266 | 0 | if ( *p == 0 ) { |
267 | 0 | break; // don't write to q; this trims the trailing space. |
268 | 0 | } |
269 | 0 | *q = ' '; |
270 | 0 | ++q; |
271 | 0 | } |
272 | 0 | *q = *p; |
273 | 0 | ++q; |
274 | 0 | ++p; |
275 | 0 | } |
276 | 0 | *q = 0; |
277 | 0 | } |
278 | 0 | } |
279 | | |
280 | | |
281 | | const char* StrPair::GetStr() |
282 | 15.6M | { |
283 | 15.6M | TIXMLASSERT( _start ); |
284 | 15.6M | TIXMLASSERT( _end ); |
285 | 15.6M | if ( _flags & NEEDS_FLUSH ) { |
286 | 1.87M | *_end = 0; |
287 | 1.87M | _flags ^= NEEDS_FLUSH; |
288 | | |
289 | 1.87M | if ( _flags ) { |
290 | 461 | const char* p = _start; // the read pointer |
291 | 461 | char* q = _start; // the write pointer |
292 | | |
293 | 264k | while( p < _end ) { |
294 | 263k | if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) { |
295 | | // CR-LF pair becomes LF |
296 | | // CR alone becomes LF |
297 | | // LF-CR becomes LF |
298 | 489 | if ( *(p+1) == LF ) { |
299 | 226 | p += 2; |
300 | 226 | } |
301 | 263 | else { |
302 | 263 | ++p; |
303 | 263 | } |
304 | 489 | *q = LF; |
305 | 489 | ++q; |
306 | 489 | } |
307 | 263k | else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) { |
308 | 638 | if ( *(p+1) == CR ) { |
309 | 231 | p += 2; |
310 | 231 | } |
311 | 407 | else { |
312 | 407 | ++p; |
313 | 407 | } |
314 | 638 | *q = LF; |
315 | 638 | ++q; |
316 | 638 | } |
317 | 262k | else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) { |
318 | | // Entities handled by tinyXML2: |
319 | | // - special entities in the entity table [in/out] |
320 | | // - numeric character reference [in] |
321 | | // 中 or 中 |
322 | | |
323 | 89.6k | if ( *(p+1) == '#' ) { |
324 | 67.7k | const int buflen = 10; |
325 | 67.7k | char buf[buflen] = { 0 }; |
326 | 67.7k | int len = 0; |
327 | 67.7k | const char* adjusted = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) ); |
328 | 67.7k | if ( adjusted == 0 ) { |
329 | 66.4k | *q = *p; |
330 | 66.4k | ++p; |
331 | 66.4k | ++q; |
332 | 66.4k | } |
333 | 1.33k | else { |
334 | 1.33k | TIXMLASSERT( 0 <= len && len <= buflen ); |
335 | 1.33k | TIXMLASSERT( q + len <= adjusted ); |
336 | 1.33k | p = adjusted; |
337 | 1.33k | memcpy( q, buf, len ); |
338 | 1.33k | q += len; |
339 | 1.33k | } |
340 | 67.7k | } |
341 | 21.8k | else { |
342 | 21.8k | bool entityFound = false; |
343 | 126k | for( int i = 0; i < NUM_ENTITIES; ++i ) { |
344 | 106k | const Entity& entity = entities[i]; |
345 | 106k | if ( strncmp( p + 1, entity.pattern, entity.length ) == 0 |
346 | 106k | && *( p + entity.length + 1 ) == ';' ) { |
347 | | // Found an entity - convert. |
348 | 2.60k | *q = entity.value; |
349 | 2.60k | ++q; |
350 | 2.60k | p += entity.length + 2; |
351 | 2.60k | entityFound = true; |
352 | 2.60k | break; |
353 | 2.60k | } |
354 | 106k | } |
355 | 21.8k | if ( !entityFound ) { |
356 | | // fixme: treat as error? |
357 | 19.2k | ++p; |
358 | 19.2k | ++q; |
359 | 19.2k | } |
360 | 21.8k | } |
361 | 89.6k | } |
362 | 173k | else { |
363 | 173k | *q = *p; |
364 | 173k | ++p; |
365 | 173k | ++q; |
366 | 173k | } |
367 | 263k | } |
368 | 461 | *q = 0; |
369 | 461 | } |
370 | | // The loop below has plenty going on, and this |
371 | | // is a less useful mode. Break it out. |
372 | 1.87M | if ( _flags & NEEDS_WHITESPACE_COLLAPSING ) { |
373 | 0 | CollapseWhitespace(); |
374 | 0 | } |
375 | 1.87M | _flags = (_flags & NEEDS_DELETE); |
376 | 1.87M | } |
377 | 15.6M | TIXMLASSERT( _start ); |
378 | 15.6M | return _start; |
379 | 15.6M | } |
380 | | |
381 | | |
382 | | |
383 | | |
384 | | // --------- XMLUtil ----------- // |
385 | | |
386 | | const char* XMLUtil::writeBoolTrue = "true"; |
387 | | const char* XMLUtil::writeBoolFalse = "false"; |
388 | | |
389 | | void XMLUtil::SetBoolSerialization(const char* writeTrue, const char* writeFalse) |
390 | 0 | { |
391 | 0 | static const char* defTrue = "true"; |
392 | 0 | static const char* defFalse = "false"; |
393 | |
|
394 | 0 | writeBoolTrue = (writeTrue) ? writeTrue : defTrue; |
395 | 0 | writeBoolFalse = (writeFalse) ? writeFalse : defFalse; |
396 | 0 | } |
397 | | |
398 | | |
399 | | const char* XMLUtil::ReadBOM( const char* p, bool* bom ) |
400 | 2.36k | { |
401 | 2.36k | TIXMLASSERT( p ); |
402 | 2.36k | TIXMLASSERT( bom ); |
403 | 2.36k | *bom = false; |
404 | 2.36k | const unsigned char* pu = reinterpret_cast<const unsigned char*>(p); |
405 | | // Check for BOM: |
406 | 2.36k | if ( *(pu+0) == TIXML_UTF_LEAD_0 |
407 | 2.36k | && *(pu+1) == TIXML_UTF_LEAD_1 |
408 | 2.36k | && *(pu+2) == TIXML_UTF_LEAD_2 ) { |
409 | 4 | *bom = true; |
410 | 4 | p += 3; |
411 | 4 | } |
412 | 2.36k | TIXMLASSERT( p ); |
413 | 2.36k | return p; |
414 | 2.36k | } |
415 | | |
416 | | |
417 | | void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length ) |
418 | 1.31k | { |
419 | 1.31k | const unsigned long BYTE_MASK = 0xBF; |
420 | 1.31k | const unsigned long BYTE_MARK = 0x80; |
421 | 1.31k | const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; |
422 | | |
423 | 1.31k | if (input < 0x80) { |
424 | 527 | *length = 1; |
425 | 527 | } |
426 | 791 | else if ( input < 0x800 ) { |
427 | 215 | *length = 2; |
428 | 215 | } |
429 | 576 | else if ( input < 0x10000 ) { |
430 | 220 | *length = 3; |
431 | 220 | } |
432 | 356 | else if ( input < 0x200000 ) { |
433 | 356 | *length = 4; |
434 | 356 | } |
435 | 0 | else { |
436 | 0 | *length = 0; // This code won't convert this correctly anyway. |
437 | 0 | return; |
438 | 0 | } |
439 | | |
440 | 1.31k | output += *length; |
441 | | |
442 | | // Scary scary fall throughs are annotated with carefully designed comments |
443 | | // to suppress compiler warnings such as -Wimplicit-fallthrough in gcc |
444 | 1.31k | switch (*length) { |
445 | 356 | case 4: |
446 | 356 | --output; |
447 | 356 | *output = static_cast<char>((input | BYTE_MARK) & BYTE_MASK); |
448 | 356 | input >>= 6; |
449 | | //fall through |
450 | 576 | case 3: |
451 | 576 | --output; |
452 | 576 | *output = static_cast<char>((input | BYTE_MARK) & BYTE_MASK); |
453 | 576 | input >>= 6; |
454 | | //fall through |
455 | 791 | case 2: |
456 | 791 | --output; |
457 | 791 | *output = static_cast<char>((input | BYTE_MARK) & BYTE_MASK); |
458 | 791 | input >>= 6; |
459 | | //fall through |
460 | 1.31k | case 1: |
461 | 1.31k | --output; |
462 | 1.31k | *output = static_cast<char>(input | FIRST_BYTE_MARK[*length]); |
463 | 1.31k | break; |
464 | 0 | default: |
465 | 0 | TIXMLASSERT( false ); |
466 | 1.31k | } |
467 | 1.31k | } |
468 | | |
469 | | |
470 | | const char* XMLUtil::GetCharacterRef(const char* p, char* value, int* length) |
471 | 67.7k | { |
472 | | // Assume an entity, and pull it out. |
473 | 67.7k | *length = 0; |
474 | | |
475 | 67.7k | static const uint32_t MAX_CODE_POINT = 0x10FFFF; |
476 | | |
477 | 67.7k | if (*(p + 1) == '#' && *(p + 2)) { |
478 | 67.7k | uint32_t ucs = 0; |
479 | 67.7k | ptrdiff_t delta = 0; |
480 | 67.7k | uint32_t mult = 1; |
481 | 67.7k | static const char SEMICOLON = ';'; |
482 | | |
483 | 67.7k | bool hex = false; |
484 | 67.7k | uint32_t radix = 10; |
485 | 67.7k | const char* q = 0; |
486 | 67.7k | char terminator = '#'; |
487 | | |
488 | 67.7k | if (*(p + 2) == 'x') { |
489 | | // Hexadecimal. |
490 | 4.50k | hex = true; |
491 | 4.50k | radix = 16; |
492 | 4.50k | terminator = 'x'; |
493 | | |
494 | 4.50k | q = p + 3; |
495 | 4.50k | } |
496 | 63.2k | else { |
497 | | // Decimal. |
498 | 63.2k | q = p + 2; |
499 | 63.2k | } |
500 | 67.7k | if (!(*q)) { |
501 | 8 | return 0; |
502 | 8 | } |
503 | | |
504 | 67.7k | q = strchr(q, SEMICOLON); |
505 | 67.7k | if (!q) { |
506 | 610 | return 0; |
507 | 610 | } |
508 | 67.1k | TIXMLASSERT(*q == SEMICOLON); |
509 | | |
510 | 67.1k | delta = q - p; |
511 | 67.1k | --q; |
512 | | |
513 | 122k | while (*q != terminator) { |
514 | 119k | uint32_t digit = 0; |
515 | | |
516 | 119k | if (*q >= '0' && *q <= '9') { |
517 | 50.9k | digit = *q - '0'; |
518 | 50.9k | } |
519 | 68.7k | else if (hex && (*q >= 'a' && *q <= 'f')) { |
520 | 988 | digit = *q - 'a' + 10; |
521 | 988 | } |
522 | 67.7k | else if (hex && (*q >= 'A' && *q <= 'F')) { |
523 | 3.24k | digit = *q - 'A' + 10; |
524 | 3.24k | } |
525 | 64.5k | else { |
526 | 64.5k | return 0; |
527 | 64.5k | } |
528 | 55.1k | TIXMLASSERT(digit < radix); |
529 | | |
530 | 55.1k | const unsigned int digitScaled = mult * digit; |
531 | 55.1k | ucs += digitScaled; |
532 | 55.1k | mult *= radix; |
533 | | |
534 | | // Security check: could a value exist that is out of range? |
535 | | // Easily; limit to the MAX_CODE_POINT, which also allows for a |
536 | | // bunch of leading zeroes. |
537 | 55.1k | if (mult > MAX_CODE_POINT) { |
538 | 4.69k | mult = MAX_CODE_POINT; |
539 | 4.69k | } |
540 | 55.1k | --q; |
541 | 55.1k | } |
542 | | // Out of range: |
543 | 2.59k | if (ucs > MAX_CODE_POINT) { |
544 | 1.27k | return 0; |
545 | 1.27k | } |
546 | | // convert the UCS to UTF-8 |
547 | 1.31k | ConvertUTF32ToUTF8(ucs, value, length); |
548 | 1.31k | if (length == 0) { |
549 | | // If length is 0, there was an error. (Security? Bad input?) |
550 | | // Fail safely. |
551 | 0 | return 0; |
552 | 0 | } |
553 | 1.31k | return p + delta + 1; |
554 | 1.31k | } |
555 | 13 | return p + 1; |
556 | 67.7k | } |
557 | | |
558 | | void XMLUtil::ToStr( int v, char* buffer, int bufferSize ) |
559 | 0 | { |
560 | 0 | TIXML_SNPRINTF( buffer, bufferSize, "%d", v ); |
561 | 0 | } |
562 | | |
563 | | |
564 | | void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize ) |
565 | 0 | { |
566 | 0 | TIXML_SNPRINTF( buffer, bufferSize, "%u", v ); |
567 | 0 | } |
568 | | |
569 | | |
570 | | void XMLUtil::ToStr( bool v, char* buffer, int bufferSize ) |
571 | 0 | { |
572 | 0 | TIXML_SNPRINTF( buffer, bufferSize, "%s", v ? writeBoolTrue : writeBoolFalse); |
573 | 0 | } |
574 | | |
575 | | /* |
576 | | ToStr() of a number is a very tricky topic. |
577 | | https://github.com/leethomason/tinyxml2/issues/106 |
578 | | */ |
579 | | void XMLUtil::ToStr( float v, char* buffer, int bufferSize ) |
580 | 0 | { |
581 | 0 | TIXML_SNPRINTF( buffer, bufferSize, "%.8g", v ); |
582 | 0 | } |
583 | | |
584 | | |
585 | | void XMLUtil::ToStr( double v, char* buffer, int bufferSize ) |
586 | 0 | { |
587 | 0 | TIXML_SNPRINTF( buffer, bufferSize, "%.17g", v ); |
588 | 0 | } |
589 | | |
590 | | |
591 | | void XMLUtil::ToStr( int64_t v, char* buffer, int bufferSize ) |
592 | 0 | { |
593 | | // horrible syntax trick to make the compiler happy about %lld |
594 | 0 | TIXML_SNPRINTF(buffer, bufferSize, "%lld", static_cast<long long>(v)); |
595 | 0 | } |
596 | | |
597 | | void XMLUtil::ToStr( uint64_t v, char* buffer, int bufferSize ) |
598 | 0 | { |
599 | | // horrible syntax trick to make the compiler happy about %llu |
600 | 0 | TIXML_SNPRINTF(buffer, bufferSize, "%llu", static_cast<unsigned long long>(v)); |
601 | 0 | } |
602 | | |
603 | | bool XMLUtil::ToInt(const char* str, int* value) |
604 | 0 | { |
605 | 0 | if (IsPrefixHex(str)) { |
606 | 0 | unsigned v; |
607 | 0 | if (TIXML_SSCANF(str, "%x", &v) == 1) { |
608 | 0 | *value = static_cast<int>(v); |
609 | 0 | return true; |
610 | 0 | } |
611 | 0 | } |
612 | 0 | else { |
613 | 0 | if (TIXML_SSCANF(str, "%d", value) == 1) { |
614 | 0 | return true; |
615 | 0 | } |
616 | 0 | } |
617 | 0 | return false; |
618 | 0 | } |
619 | | |
620 | | bool XMLUtil::ToUnsigned(const char* str, unsigned* value) |
621 | 0 | { |
622 | 0 | if (TIXML_SSCANF(str, IsPrefixHex(str) ? "%x" : "%u", value) == 1) { |
623 | 0 | return true; |
624 | 0 | } |
625 | 0 | return false; |
626 | 0 | } |
627 | | |
628 | | bool XMLUtil::ToBool( const char* str, bool* value ) |
629 | 0 | { |
630 | 0 | int ival = 0; |
631 | 0 | if ( ToInt( str, &ival )) { |
632 | 0 | *value = (ival==0) ? false : true; |
633 | 0 | return true; |
634 | 0 | } |
635 | 0 | static const char* TRUE_VALS[] = { "true", "True", "TRUE", 0 }; |
636 | 0 | static const char* FALSE_VALS[] = { "false", "False", "FALSE", 0 }; |
637 | |
|
638 | 0 | for (int i = 0; TRUE_VALS[i]; ++i) { |
639 | 0 | if (StringEqual(str, TRUE_VALS[i])) { |
640 | 0 | *value = true; |
641 | 0 | return true; |
642 | 0 | } |
643 | 0 | } |
644 | 0 | for (int i = 0; FALSE_VALS[i]; ++i) { |
645 | 0 | if (StringEqual(str, FALSE_VALS[i])) { |
646 | 0 | *value = false; |
647 | 0 | return true; |
648 | 0 | } |
649 | 0 | } |
650 | 0 | return false; |
651 | 0 | } |
652 | | |
653 | | |
654 | | bool XMLUtil::ToFloat( const char* str, float* value ) |
655 | 0 | { |
656 | 0 | if ( TIXML_SSCANF( str, "%f", value ) == 1 ) { |
657 | 0 | return true; |
658 | 0 | } |
659 | 0 | return false; |
660 | 0 | } |
661 | | |
662 | | |
663 | | bool XMLUtil::ToDouble( const char* str, double* value ) |
664 | 0 | { |
665 | 0 | if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) { |
666 | 0 | return true; |
667 | 0 | } |
668 | 0 | return false; |
669 | 0 | } |
670 | | |
671 | | |
672 | | bool XMLUtil::ToInt64(const char* str, int64_t* value) |
673 | 0 | { |
674 | 0 | if (IsPrefixHex(str)) { |
675 | 0 | unsigned long long v = 0; // horrible syntax trick to make the compiler happy about %llx |
676 | 0 | if (TIXML_SSCANF(str, "%llx", &v) == 1) { |
677 | 0 | *value = static_cast<int64_t>(v); |
678 | 0 | return true; |
679 | 0 | } |
680 | 0 | } |
681 | 0 | else { |
682 | 0 | long long v = 0; // horrible syntax trick to make the compiler happy about %lld |
683 | 0 | if (TIXML_SSCANF(str, "%lld", &v) == 1) { |
684 | 0 | *value = static_cast<int64_t>(v); |
685 | 0 | return true; |
686 | 0 | } |
687 | 0 | } |
688 | 0 | return false; |
689 | 0 | } |
690 | | |
691 | | |
692 | 0 | bool XMLUtil::ToUnsigned64(const char* str, uint64_t* value) { |
693 | 0 | unsigned long long v = 0; // horrible syntax trick to make the compiler happy about %llu |
694 | 0 | if(TIXML_SSCANF(str, IsPrefixHex(str) ? "%llx" : "%llu", &v) == 1) { |
695 | 0 | *value = static_cast<uint64_t>(v); |
696 | 0 | return true; |
697 | 0 | } |
698 | 0 | return false; |
699 | 0 | } |
700 | | |
701 | | |
702 | | char* XMLDocument::Identify( char* p, XMLNode** node, bool first ) |
703 | 10.0M | { |
704 | 10.0M | TIXMLASSERT( node ); |
705 | 10.0M | TIXMLASSERT( p ); |
706 | 10.0M | char* const start = p; |
707 | 10.0M | int const startLine = _parseCurLineNum; |
708 | 10.0M | p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum ); |
709 | 10.0M | if( !*p ) { |
710 | 26 | *node = 0; |
711 | 26 | TIXMLASSERT( p ); |
712 | 26 | return p; |
713 | 26 | } |
714 | | |
715 | | // These strings define the matching patterns: |
716 | 10.0M | static const char* xmlHeader = { "<?" }; |
717 | 10.0M | static const char* commentHeader = { "<!--" }; |
718 | 10.0M | static const char* cdataHeader = { "<![CDATA[" }; |
719 | 10.0M | static const char* dtdHeader = { "<!" }; |
720 | 10.0M | static const char* elementHeader = { "<" }; // and a header for everything else; check last. |
721 | | |
722 | 10.0M | static const int xmlHeaderLen = 2; |
723 | 10.0M | static const int commentHeaderLen = 4; |
724 | 10.0M | static const int cdataHeaderLen = 9; |
725 | 10.0M | static const int dtdHeaderLen = 2; |
726 | 10.0M | static const int elementHeaderLen = 1; |
727 | | |
728 | 10.0M | TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool |
729 | 10.0M | TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool |
730 | 10.0M | XMLNode* returnNode = 0; |
731 | 10.0M | if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) { |
732 | 806 | returnNode = CreateUnlinkedNode<XMLDeclaration>( _commentPool ); |
733 | 806 | returnNode->_parseLineNum = _parseCurLineNum; |
734 | 806 | p += xmlHeaderLen; |
735 | 806 | } |
736 | 10.0M | else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) { |
737 | 402 | returnNode = CreateUnlinkedNode<XMLComment>( _commentPool ); |
738 | 402 | returnNode->_parseLineNum = _parseCurLineNum; |
739 | 402 | p += commentHeaderLen; |
740 | 402 | } |
741 | 10.0M | else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) { |
742 | 678 | XMLText* text = CreateUnlinkedNode<XMLText>( _textPool ); |
743 | 678 | returnNode = text; |
744 | 678 | returnNode->_parseLineNum = _parseCurLineNum; |
745 | 678 | p += cdataHeaderLen; |
746 | 678 | text->SetCData( true ); |
747 | 678 | } |
748 | 10.0M | else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) { |
749 | 5.81M | returnNode = CreateUnlinkedNode<XMLUnknown>( _commentPool ); |
750 | 5.81M | returnNode->_parseLineNum = _parseCurLineNum; |
751 | 5.81M | p += dtdHeaderLen; |
752 | 5.81M | } |
753 | 4.21M | else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) { |
754 | | |
755 | | // Preserve whitespace pedantically before closing tag, when it's immediately after opening tag |
756 | 1.70M | if (WhitespaceMode() == PEDANTIC_WHITESPACE && first && p != start && *(p + elementHeaderLen) == '/') { |
757 | 0 | returnNode = CreateUnlinkedNode<XMLText>(_textPool); |
758 | 0 | returnNode->_parseLineNum = startLine; |
759 | 0 | p = start; // Back it up, all the text counts. |
760 | 0 | _parseCurLineNum = startLine; |
761 | 0 | } |
762 | 1.70M | else { |
763 | 1.70M | returnNode = CreateUnlinkedNode<XMLElement>(_elementPool); |
764 | 1.70M | returnNode->_parseLineNum = _parseCurLineNum; |
765 | 1.70M | p += elementHeaderLen; |
766 | 1.70M | } |
767 | 1.70M | } |
768 | 2.51M | else { |
769 | 2.51M | returnNode = CreateUnlinkedNode<XMLText>( _textPool ); |
770 | 2.51M | returnNode->_parseLineNum = _parseCurLineNum; // Report line of first non-whitespace character |
771 | 2.51M | p = start; // Back it up, all the text counts. |
772 | 2.51M | _parseCurLineNum = startLine; |
773 | 2.51M | } |
774 | | |
775 | 10.0M | TIXMLASSERT( returnNode ); |
776 | 10.0M | TIXMLASSERT( p ); |
777 | 10.0M | *node = returnNode; |
778 | 10.0M | return p; |
779 | 10.0M | } |
780 | | |
781 | | |
782 | | bool XMLDocument::Accept( XMLVisitor* visitor ) const |
783 | 0 | { |
784 | 0 | TIXMLASSERT( visitor ); |
785 | 0 | if ( visitor->VisitEnter( *this ) ) { |
786 | 0 | for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) { |
787 | 0 | if ( !node->Accept( visitor ) ) { |
788 | 0 | break; |
789 | 0 | } |
790 | 0 | } |
791 | 0 | } |
792 | 0 | return visitor->VisitExit( *this ); |
793 | 0 | } |
794 | | |
795 | | |
796 | | // --------- XMLNode ----------- // |
797 | | |
798 | | XMLNode::XMLNode( XMLDocument* doc ) : |
799 | 10.0M | _document( doc ), |
800 | 10.0M | _parent( 0 ), |
801 | 10.0M | _value(), |
802 | 10.0M | _parseLineNum( 0 ), |
803 | 10.0M | _firstChild( 0 ), _lastChild( 0 ), |
804 | 10.0M | _prev( 0 ), _next( 0 ), |
805 | 10.0M | _userData( 0 ), |
806 | 10.0M | _memPool( 0 ) |
807 | 10.0M | { |
808 | 10.0M | } |
809 | | |
810 | | |
811 | | XMLNode::~XMLNode() |
812 | 10.0M | { |
813 | 10.0M | DeleteChildren(); |
814 | 10.0M | if ( _parent ) { |
815 | 0 | _parent->Unlink( this ); |
816 | 0 | } |
817 | 10.0M | } |
818 | | |
819 | | // ChildElementCount was originally suggested by msteiger on the sourceforge page for TinyXML and modified by KB1SPH for TinyXML-2. |
820 | | |
821 | 0 | int XMLNode::ChildElementCount(const char *value) const { |
822 | 0 | int count = 0; |
823 | |
|
824 | 0 | const XMLElement *e = FirstChildElement(value); |
825 | |
|
826 | 0 | while (e) { |
827 | 0 | e = e->NextSiblingElement(value); |
828 | 0 | count++; |
829 | 0 | } |
830 | |
|
831 | 0 | return count; |
832 | 0 | } |
833 | | |
834 | 0 | int XMLNode::ChildElementCount() const { |
835 | 0 | int count = 0; |
836 | |
|
837 | 0 | const XMLElement *e = FirstChildElement(); |
838 | |
|
839 | 0 | while (e) { |
840 | 0 | e = e->NextSiblingElement(); |
841 | 0 | count++; |
842 | 0 | } |
843 | |
|
844 | 0 | return count; |
845 | 0 | } |
846 | | |
847 | | const char* XMLNode::Value() const |
848 | 4.95k | { |
849 | | // Edge case: XMLDocuments don't have a Value. Return null. |
850 | 4.95k | if ( this->ToDocument() ) |
851 | 0 | return 0; |
852 | 4.95k | return _value.GetStr(); |
853 | 4.95k | } |
854 | | |
855 | | void XMLNode::SetValue( const char* str, bool staticMem ) |
856 | 0 | { |
857 | 0 | if ( staticMem ) { |
858 | 0 | _value.SetInternedStr( str ); |
859 | 0 | } |
860 | 0 | else { |
861 | 0 | _value.SetStr( str ); |
862 | 0 | } |
863 | 0 | } |
864 | | |
865 | | XMLNode* XMLNode::DeepClone(XMLDocument* target) const |
866 | 0 | { |
867 | 0 | XMLNode* clone = this->ShallowClone(target); |
868 | 0 | if (!clone) return 0; |
869 | | |
870 | 0 | for (const XMLNode* child = this->FirstChild(); child; child = child->NextSibling()) { |
871 | 0 | XMLNode* childClone = child->DeepClone(target); |
872 | 0 | TIXMLASSERT(childClone); |
873 | 0 | clone->InsertEndChild(childClone); |
874 | 0 | } |
875 | 0 | return clone; |
876 | 0 | } |
877 | | |
878 | | void XMLNode::DeleteChildren() |
879 | 10.0M | { |
880 | 20.0M | while( _firstChild ) { |
881 | 9.97M | TIXMLASSERT( _lastChild ); |
882 | 9.97M | DeleteChild( _firstChild ); |
883 | 9.97M | } |
884 | 10.0M | _firstChild = _lastChild = 0; |
885 | 10.0M | } |
886 | | |
887 | | |
888 | | void XMLNode::Unlink( XMLNode* child ) |
889 | 9.97M | { |
890 | 9.97M | TIXMLASSERT( child ); |
891 | 9.97M | TIXMLASSERT( child->_document == _document ); |
892 | 9.97M | TIXMLASSERT( child->_parent == this ); |
893 | 9.97M | if ( child == _firstChild ) { |
894 | 9.97M | _firstChild = _firstChild->_next; |
895 | 9.97M | } |
896 | 9.97M | if ( child == _lastChild ) { |
897 | 8.27k | _lastChild = _lastChild->_prev; |
898 | 8.27k | } |
899 | | |
900 | 9.97M | if ( child->_prev ) { |
901 | 0 | child->_prev->_next = child->_next; |
902 | 0 | } |
903 | 9.97M | if ( child->_next ) { |
904 | 9.96M | child->_next->_prev = child->_prev; |
905 | 9.96M | } |
906 | 9.97M | child->_next = 0; |
907 | 9.97M | child->_prev = 0; |
908 | 9.97M | child->_parent = 0; |
909 | 9.97M | } |
910 | | |
911 | | |
912 | | void XMLNode::DeleteChild( XMLNode* node ) |
913 | 9.97M | { |
914 | 9.97M | TIXMLASSERT( node ); |
915 | 9.97M | TIXMLASSERT( node->_document == _document ); |
916 | 9.97M | TIXMLASSERT( node->_parent == this ); |
917 | 9.97M | Unlink( node ); |
918 | 9.97M | TIXMLASSERT(node->_prev == 0); |
919 | 9.97M | TIXMLASSERT(node->_next == 0); |
920 | 9.97M | TIXMLASSERT(node->_parent == 0); |
921 | 9.97M | DeleteNode( node ); |
922 | 9.97M | } |
923 | | |
924 | | |
925 | | XMLNode* XMLNode::InsertEndChild( XMLNode* addThis ) |
926 | 9.97M | { |
927 | 9.97M | TIXMLASSERT( addThis ); |
928 | 9.97M | if ( addThis->_document != _document ) { |
929 | 0 | TIXMLASSERT( false ); |
930 | 0 | return 0; |
931 | 0 | } |
932 | 9.97M | InsertChildPreamble( addThis ); |
933 | | |
934 | 9.97M | if ( _lastChild ) { |
935 | 9.96M | TIXMLASSERT( _firstChild ); |
936 | 9.96M | TIXMLASSERT( _lastChild->_next == 0 ); |
937 | 9.96M | _lastChild->_next = addThis; |
938 | 9.96M | addThis->_prev = _lastChild; |
939 | 9.96M | _lastChild = addThis; |
940 | | |
941 | 9.96M | addThis->_next = 0; |
942 | 9.96M | } |
943 | 8.27k | else { |
944 | 8.27k | TIXMLASSERT( _firstChild == 0 ); |
945 | 8.27k | _firstChild = _lastChild = addThis; |
946 | | |
947 | 8.27k | addThis->_prev = 0; |
948 | 8.27k | addThis->_next = 0; |
949 | 8.27k | } |
950 | 9.97M | addThis->_parent = this; |
951 | 9.97M | return addThis; |
952 | 9.97M | } |
953 | | |
954 | | |
955 | | XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis ) |
956 | 0 | { |
957 | 0 | TIXMLASSERT( addThis ); |
958 | 0 | if ( addThis->_document != _document ) { |
959 | 0 | TIXMLASSERT( false ); |
960 | 0 | return 0; |
961 | 0 | } |
962 | 0 | InsertChildPreamble( addThis ); |
963 | |
|
964 | 0 | if ( _firstChild ) { |
965 | 0 | TIXMLASSERT( _lastChild ); |
966 | 0 | TIXMLASSERT( _firstChild->_prev == 0 ); |
967 | |
|
968 | 0 | _firstChild->_prev = addThis; |
969 | 0 | addThis->_next = _firstChild; |
970 | 0 | _firstChild = addThis; |
971 | |
|
972 | 0 | addThis->_prev = 0; |
973 | 0 | } |
974 | 0 | else { |
975 | 0 | TIXMLASSERT( _lastChild == 0 ); |
976 | 0 | _firstChild = _lastChild = addThis; |
977 | |
|
978 | 0 | addThis->_prev = 0; |
979 | 0 | addThis->_next = 0; |
980 | 0 | } |
981 | 0 | addThis->_parent = this; |
982 | 0 | return addThis; |
983 | 0 | } |
984 | | |
985 | | |
986 | | XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis ) |
987 | 0 | { |
988 | 0 | TIXMLASSERT( addThis ); |
989 | 0 | if ( addThis->_document != _document ) { |
990 | 0 | TIXMLASSERT( false ); |
991 | 0 | return 0; |
992 | 0 | } |
993 | | |
994 | 0 | TIXMLASSERT( afterThis ); |
995 | |
|
996 | 0 | if ( afterThis->_parent != this ) { |
997 | 0 | TIXMLASSERT( false ); |
998 | 0 | return 0; |
999 | 0 | } |
1000 | 0 | if ( afterThis == addThis ) { |
1001 | | // Current state: BeforeThis -> AddThis -> OneAfterAddThis |
1002 | | // Now AddThis must disappear from it's location and then |
1003 | | // reappear between BeforeThis and OneAfterAddThis. |
1004 | | // So just leave it where it is. |
1005 | 0 | return addThis; |
1006 | 0 | } |
1007 | | |
1008 | 0 | if ( afterThis->_next == 0 ) { |
1009 | | // The last node or the only node. |
1010 | 0 | return InsertEndChild( addThis ); |
1011 | 0 | } |
1012 | 0 | InsertChildPreamble( addThis ); |
1013 | 0 | addThis->_prev = afterThis; |
1014 | 0 | addThis->_next = afterThis->_next; |
1015 | 0 | afterThis->_next->_prev = addThis; |
1016 | 0 | afterThis->_next = addThis; |
1017 | 0 | addThis->_parent = this; |
1018 | 0 | return addThis; |
1019 | 0 | } |
1020 | | |
1021 | | |
1022 | | |
1023 | | |
1024 | | const XMLElement* XMLNode::FirstChildElement( const char* name ) const |
1025 | 0 | { |
1026 | 0 | for( const XMLNode* node = _firstChild; node; node = node->_next ) { |
1027 | 0 | const XMLElement* element = node->ToElementWithName( name ); |
1028 | 0 | if ( element ) { |
1029 | 0 | return element; |
1030 | 0 | } |
1031 | 0 | } |
1032 | 0 | return 0; |
1033 | 0 | } |
1034 | | |
1035 | | |
1036 | | const XMLElement* XMLNode::LastChildElement( const char* name ) const |
1037 | 0 | { |
1038 | 0 | for( const XMLNode* node = _lastChild; node; node = node->_prev ) { |
1039 | 0 | const XMLElement* element = node->ToElementWithName( name ); |
1040 | 0 | if ( element ) { |
1041 | 0 | return element; |
1042 | 0 | } |
1043 | 0 | } |
1044 | 0 | return 0; |
1045 | 0 | } |
1046 | | |
1047 | | |
1048 | | const XMLElement* XMLNode::NextSiblingElement( const char* name ) const |
1049 | 0 | { |
1050 | 0 | for( const XMLNode* node = _next; node; node = node->_next ) { |
1051 | 0 | const XMLElement* element = node->ToElementWithName( name ); |
1052 | 0 | if ( element ) { |
1053 | 0 | return element; |
1054 | 0 | } |
1055 | 0 | } |
1056 | 0 | return 0; |
1057 | 0 | } |
1058 | | |
1059 | | |
1060 | | const XMLElement* XMLNode::PreviousSiblingElement( const char* name ) const |
1061 | 0 | { |
1062 | 0 | for( const XMLNode* node = _prev; node; node = node->_prev ) { |
1063 | 0 | const XMLElement* element = node->ToElementWithName( name ); |
1064 | 0 | if ( element ) { |
1065 | 0 | return element; |
1066 | 0 | } |
1067 | 0 | } |
1068 | 0 | return 0; |
1069 | 0 | } |
1070 | | |
1071 | | |
1072 | | char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) |
1073 | 58.3k | { |
1074 | | // This is a recursive method, but thinking about it "at the current level" |
1075 | | // it is a pretty simple flat list: |
1076 | | // <foo/> |
1077 | | // <!-- comment --> |
1078 | | // |
1079 | | // With a special case: |
1080 | | // <foo> |
1081 | | // </foo> |
1082 | | // <!-- comment --> |
1083 | | // |
1084 | | // Where the closing element (/foo) *must* be the next thing after the opening |
1085 | | // element, and the names must match. BUT the tricky bit is that the closing |
1086 | | // element will be read by the child. |
1087 | | // |
1088 | | // 'endTag' is the end tag for this node, it is returned by a call to a child. |
1089 | | // 'parentEnd' is the end tag for the parent, which is filled in and returned. |
1090 | | |
1091 | 58.3k | XMLDocument::DepthTracker tracker(_document); |
1092 | 58.3k | if (_document->Error()) |
1093 | 2 | return 0; |
1094 | | |
1095 | 58.3k | bool first = true; |
1096 | 10.0M | while( p && *p ) { |
1097 | 10.0M | XMLNode* node = 0; |
1098 | | |
1099 | 10.0M | p = _document->Identify( p, &node, first ); |
1100 | 10.0M | TIXMLASSERT( p ); |
1101 | 10.0M | if ( node == 0 ) { |
1102 | 26 | break; |
1103 | 26 | } |
1104 | 10.0M | first = false; |
1105 | | |
1106 | 10.0M | const int initialLineNum = node->_parseLineNum; |
1107 | | |
1108 | 10.0M | StrPair endTag; |
1109 | 10.0M | p = node->ParseDeep( p, &endTag, curLineNumPtr ); |
1110 | 10.0M | if ( !p ) { |
1111 | 54.0k | _document->DeleteNode( node ); |
1112 | 54.0k | if ( !_document->Error() ) { |
1113 | 280 | _document->SetError( XML_ERROR_PARSING, initialLineNum, 0); |
1114 | 280 | } |
1115 | 54.0k | break; |
1116 | 54.0k | } |
1117 | | |
1118 | 9.98M | const XMLDeclaration* const decl = node->ToDeclaration(); |
1119 | 9.98M | if ( decl ) { |
1120 | | // Declarations are only allowed at document level |
1121 | | // |
1122 | | // Multiple declarations are allowed but all declarations |
1123 | | // must occur before anything else. |
1124 | | // |
1125 | | // Optimized due to a security test case. If the first node is |
1126 | | // a declaration, and the last node is a declaration, then only |
1127 | | // declarations have so far been added. |
1128 | 718 | bool wellLocated = false; |
1129 | | |
1130 | 718 | if (ToDocument()) { |
1131 | 708 | if (FirstChild()) { |
1132 | 683 | wellLocated = |
1133 | 683 | FirstChild() && |
1134 | 683 | FirstChild()->ToDeclaration() && |
1135 | 683 | LastChild() && |
1136 | 683 | LastChild()->ToDeclaration(); |
1137 | 683 | } |
1138 | 25 | else { |
1139 | 25 | wellLocated = true; |
1140 | 25 | } |
1141 | 708 | } |
1142 | 718 | if ( !wellLocated ) { |
1143 | 81 | _document->SetError( XML_ERROR_PARSING_DECLARATION, initialLineNum, "XMLDeclaration value=%s", decl->Value()); |
1144 | 81 | _document->DeleteNode( node ); |
1145 | 81 | break; |
1146 | 81 | } |
1147 | 718 | } |
1148 | | |
1149 | 9.98M | XMLElement* ele = node->ToElement(); |
1150 | 9.98M | if ( ele ) { |
1151 | | // We read the end tag. Return it to the parent. |
1152 | 1.65M | if ( ele->ClosingType() == XMLElement::CLOSING ) { |
1153 | 3.79k | if ( parentEndTag ) { |
1154 | 3.78k | ele->_value.TransferTo( parentEndTag ); |
1155 | 3.78k | } |
1156 | 3.79k | node->_memPool->SetTracked(); // created and then immediately deleted. |
1157 | 3.79k | DeleteNode( node ); |
1158 | 3.79k | return p; |
1159 | 3.79k | } |
1160 | | |
1161 | | // Handle an end tag returned to this level. |
1162 | | // And handle a bunch of annoying errors. |
1163 | 1.64M | bool mismatch = false; |
1164 | 1.64M | if ( endTag.Empty() ) { |
1165 | 1.64M | if ( ele->ClosingType() == XMLElement::OPEN ) { |
1166 | 18 | mismatch = true; |
1167 | 18 | } |
1168 | 1.64M | } |
1169 | 3.78k | else { |
1170 | 3.78k | if ( ele->ClosingType() != XMLElement::OPEN ) { |
1171 | 0 | mismatch = true; |
1172 | 0 | } |
1173 | 3.78k | else if ( !XMLUtil::StringEqual( endTag.GetStr(), ele->Name() ) ) { |
1174 | 154 | mismatch = true; |
1175 | 154 | } |
1176 | 3.78k | } |
1177 | 1.64M | if ( mismatch ) { |
1178 | 172 | _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, initialLineNum, "XMLElement name=%s", ele->Name()); |
1179 | 172 | _document->DeleteNode( node ); |
1180 | 172 | break; |
1181 | 172 | } |
1182 | 1.64M | } |
1183 | 9.97M | InsertEndChild( node ); |
1184 | 9.97M | } |
1185 | 54.5k | return 0; |
1186 | 58.3k | } |
1187 | | |
1188 | | /*static*/ void XMLNode::DeleteNode( XMLNode* node ) |
1189 | 10.0M | { |
1190 | 10.0M | if ( node == 0 ) { |
1191 | 0 | return; |
1192 | 0 | } |
1193 | 10.0M | TIXMLASSERT(node->_document); |
1194 | 10.0M | if (!node->ToDocument()) { |
1195 | 10.0M | node->_document->MarkInUse(node); |
1196 | 10.0M | } |
1197 | | |
1198 | 10.0M | MemPool* pool = node->_memPool; |
1199 | 10.0M | node->~XMLNode(); |
1200 | 10.0M | pool->Free( node ); |
1201 | 10.0M | } |
1202 | | |
1203 | | void XMLNode::InsertChildPreamble( XMLNode* insertThis ) const |
1204 | 9.97M | { |
1205 | 9.97M | TIXMLASSERT( insertThis ); |
1206 | 9.97M | TIXMLASSERT( insertThis->_document == _document ); |
1207 | | |
1208 | 9.97M | if (insertThis->_parent) { |
1209 | 0 | insertThis->_parent->Unlink( insertThis ); |
1210 | 0 | } |
1211 | 9.97M | else { |
1212 | 9.97M | insertThis->_document->MarkInUse(insertThis); |
1213 | 9.97M | insertThis->_memPool->SetTracked(); |
1214 | 9.97M | } |
1215 | 9.97M | } |
1216 | | |
1217 | | const XMLElement* XMLNode::ToElementWithName( const char* name ) const |
1218 | 0 | { |
1219 | 0 | const XMLElement* element = this->ToElement(); |
1220 | 0 | if ( element == 0 ) { |
1221 | 0 | return 0; |
1222 | 0 | } |
1223 | 0 | if ( name == 0 ) { |
1224 | 0 | return element; |
1225 | 0 | } |
1226 | 0 | if ( XMLUtil::StringEqual( element->Name(), name ) ) { |
1227 | 0 | return element; |
1228 | 0 | } |
1229 | 0 | return 0; |
1230 | 0 | } |
1231 | | |
1232 | | // --------- XMLText ---------- // |
1233 | | char* XMLText::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) |
1234 | 2.51M | { |
1235 | 2.51M | if ( this->CData() ) { |
1236 | 678 | p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr ); |
1237 | 678 | if ( !p ) { |
1238 | 96 | _document->SetError( XML_ERROR_PARSING_CDATA, _parseLineNum, 0 ); |
1239 | 96 | } |
1240 | 678 | return p; |
1241 | 678 | } |
1242 | 2.51M | else { |
1243 | 2.51M | int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES; |
1244 | 2.51M | if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) { |
1245 | 0 | flags |= StrPair::NEEDS_WHITESPACE_COLLAPSING; |
1246 | 0 | } |
1247 | | |
1248 | 2.51M | p = _value.ParseText( p, "<", flags, curLineNumPtr ); |
1249 | 2.51M | if ( p && *p ) { |
1250 | 2.51M | return p-1; |
1251 | 2.51M | } |
1252 | 189 | if ( !p ) { |
1253 | 174 | _document->SetError( XML_ERROR_PARSING_TEXT, _parseLineNum, 0 ); |
1254 | 174 | } |
1255 | 189 | } |
1256 | 189 | return 0; |
1257 | 2.51M | } |
1258 | | |
1259 | | |
1260 | | XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const |
1261 | 0 | { |
1262 | 0 | if ( !doc ) { |
1263 | 0 | doc = _document; |
1264 | 0 | } |
1265 | 0 | XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern? |
1266 | 0 | text->SetCData( this->CData() ); |
1267 | 0 | return text; |
1268 | 0 | } |
1269 | | |
1270 | | |
1271 | | bool XMLText::ShallowEqual( const XMLNode* compare ) const |
1272 | 0 | { |
1273 | 0 | TIXMLASSERT( compare ); |
1274 | 0 | const XMLText* text = compare->ToText(); |
1275 | 0 | return ( text && XMLUtil::StringEqual( text->Value(), Value() ) ); |
1276 | 0 | } |
1277 | | |
1278 | | |
1279 | | bool XMLText::Accept( XMLVisitor* visitor ) const |
1280 | 0 | { |
1281 | 0 | TIXMLASSERT( visitor ); |
1282 | 0 | return visitor->Visit( *this ); |
1283 | 0 | } |
1284 | | |
1285 | | |
1286 | | // --------- XMLComment ---------- // |
1287 | | |
1288 | 402 | XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc ) |
1289 | 402 | { |
1290 | 402 | } |
1291 | | |
1292 | | |
1293 | | XMLComment::~XMLComment() |
1294 | | { |
1295 | | } |
1296 | | |
1297 | | |
1298 | | char* XMLComment::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) |
1299 | 402 | { |
1300 | | // Comment parses as text. |
1301 | 402 | p = _value.ParseText( p, "-->", StrPair::COMMENT, curLineNumPtr ); |
1302 | 402 | if ( p == 0 ) { |
1303 | 94 | _document->SetError( XML_ERROR_PARSING_COMMENT, _parseLineNum, 0 ); |
1304 | 94 | } |
1305 | 402 | return p; |
1306 | 402 | } |
1307 | | |
1308 | | |
1309 | | XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const |
1310 | 0 | { |
1311 | 0 | if ( !doc ) { |
1312 | 0 | doc = _document; |
1313 | 0 | } |
1314 | 0 | XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern? |
1315 | 0 | return comment; |
1316 | 0 | } |
1317 | | |
1318 | | |
1319 | | bool XMLComment::ShallowEqual( const XMLNode* compare ) const |
1320 | 0 | { |
1321 | 0 | TIXMLASSERT( compare ); |
1322 | 0 | const XMLComment* comment = compare->ToComment(); |
1323 | 0 | return ( comment && XMLUtil::StringEqual( comment->Value(), Value() )); |
1324 | 0 | } |
1325 | | |
1326 | | |
1327 | | bool XMLComment::Accept( XMLVisitor* visitor ) const |
1328 | 0 | { |
1329 | 0 | TIXMLASSERT( visitor ); |
1330 | 0 | return visitor->Visit( *this ); |
1331 | 0 | } |
1332 | | |
1333 | | |
1334 | | // --------- XMLDeclaration ---------- // |
1335 | | |
1336 | 806 | XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc ) |
1337 | 806 | { |
1338 | 806 | } |
1339 | | |
1340 | | |
1341 | | XMLDeclaration::~XMLDeclaration() |
1342 | | { |
1343 | | //printf( "~XMLDeclaration\n" ); |
1344 | | } |
1345 | | |
1346 | | |
1347 | | char* XMLDeclaration::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) |
1348 | 806 | { |
1349 | | // Declaration parses as text. |
1350 | 806 | p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr ); |
1351 | 806 | if ( p == 0 ) { |
1352 | 88 | _document->SetError( XML_ERROR_PARSING_DECLARATION, _parseLineNum, 0 ); |
1353 | 88 | } |
1354 | 806 | return p; |
1355 | 806 | } |
1356 | | |
1357 | | |
1358 | | XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const |
1359 | 0 | { |
1360 | 0 | if ( !doc ) { |
1361 | 0 | doc = _document; |
1362 | 0 | } |
1363 | 0 | XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern? |
1364 | 0 | return dec; |
1365 | 0 | } |
1366 | | |
1367 | | |
1368 | | bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const |
1369 | 0 | { |
1370 | 0 | TIXMLASSERT( compare ); |
1371 | 0 | const XMLDeclaration* declaration = compare->ToDeclaration(); |
1372 | 0 | return ( declaration && XMLUtil::StringEqual( declaration->Value(), Value() )); |
1373 | 0 | } |
1374 | | |
1375 | | |
1376 | | |
1377 | | bool XMLDeclaration::Accept( XMLVisitor* visitor ) const |
1378 | 0 | { |
1379 | 0 | TIXMLASSERT( visitor ); |
1380 | 0 | return visitor->Visit( *this ); |
1381 | 0 | } |
1382 | | |
1383 | | // --------- XMLUnknown ---------- // |
1384 | | |
1385 | 5.81M | XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc ) |
1386 | 5.81M | { |
1387 | 5.81M | } |
1388 | | |
1389 | | |
1390 | | XMLUnknown::~XMLUnknown() |
1391 | | { |
1392 | | } |
1393 | | |
1394 | | |
1395 | | char* XMLUnknown::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) |
1396 | 5.81M | { |
1397 | | // Unknown parses as text. |
1398 | 5.81M | p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr ); |
1399 | 5.81M | if ( !p ) { |
1400 | 190 | _document->SetError( XML_ERROR_PARSING_UNKNOWN, _parseLineNum, 0 ); |
1401 | 190 | } |
1402 | 5.81M | return p; |
1403 | 5.81M | } |
1404 | | |
1405 | | |
1406 | | XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const |
1407 | 0 | { |
1408 | 0 | if ( !doc ) { |
1409 | 0 | doc = _document; |
1410 | 0 | } |
1411 | 0 | XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern? |
1412 | 0 | return text; |
1413 | 0 | } |
1414 | | |
1415 | | |
1416 | | bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const |
1417 | 0 | { |
1418 | 0 | TIXMLASSERT( compare ); |
1419 | 0 | const XMLUnknown* unknown = compare->ToUnknown(); |
1420 | 0 | return ( unknown && XMLUtil::StringEqual( unknown->Value(), Value() )); |
1421 | 0 | } |
1422 | | |
1423 | | |
1424 | | bool XMLUnknown::Accept( XMLVisitor* visitor ) const |
1425 | 0 | { |
1426 | 0 | TIXMLASSERT( visitor ); |
1427 | 0 | return visitor->Visit( *this ); |
1428 | 0 | } |
1429 | | |
1430 | | // --------- XMLAttribute ---------- // |
1431 | | |
1432 | | const char* XMLAttribute::Name() const |
1433 | 15.6M | { |
1434 | 15.6M | return _name.GetStr(); |
1435 | 15.6M | } |
1436 | | |
1437 | | const char* XMLAttribute::Value() const |
1438 | 380 | { |
1439 | 380 | return _value.GetStr(); |
1440 | 380 | } |
1441 | | |
1442 | | char* XMLAttribute::ParseDeep( char* p, bool processEntities, int* curLineNumPtr ) |
1443 | 1.87M | { |
1444 | | // Parse using the name rules: bug fix, was using ParseText before |
1445 | 1.87M | p = _name.ParseName( p ); |
1446 | 1.87M | if ( !p || !*p ) { |
1447 | 100 | return 0; |
1448 | 100 | } |
1449 | | |
1450 | | // Skip white space before = |
1451 | 1.87M | p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); |
1452 | 1.87M | if ( *p != '=' ) { |
1453 | 39 | return 0; |
1454 | 39 | } |
1455 | | |
1456 | 1.87M | ++p; // move up to opening quote |
1457 | 1.87M | p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); |
1458 | 1.87M | if ( *p != '\"' && *p != '\'' ) { |
1459 | 49 | return 0; |
1460 | 49 | } |
1461 | | |
1462 | 1.86M | const char endTag[2] = { *p, 0 }; |
1463 | 1.86M | ++p; // move past opening quote |
1464 | | |
1465 | 1.86M | p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES, curLineNumPtr ); |
1466 | 1.86M | return p; |
1467 | 1.87M | } |
1468 | | |
1469 | | |
1470 | | void XMLAttribute::SetName( const char* n ) |
1471 | 0 | { |
1472 | 0 | _name.SetStr( n ); |
1473 | 0 | } |
1474 | | |
1475 | | |
1476 | | XMLError XMLAttribute::QueryIntValue( int* value ) const |
1477 | 0 | { |
1478 | 0 | if ( XMLUtil::ToInt( Value(), value )) { |
1479 | 0 | return XML_SUCCESS; |
1480 | 0 | } |
1481 | 0 | return XML_WRONG_ATTRIBUTE_TYPE; |
1482 | 0 | } |
1483 | | |
1484 | | |
1485 | | XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const |
1486 | 0 | { |
1487 | 0 | if ( XMLUtil::ToUnsigned( Value(), value )) { |
1488 | 0 | return XML_SUCCESS; |
1489 | 0 | } |
1490 | 0 | return XML_WRONG_ATTRIBUTE_TYPE; |
1491 | 0 | } |
1492 | | |
1493 | | |
1494 | | XMLError XMLAttribute::QueryInt64Value(int64_t* value) const |
1495 | 0 | { |
1496 | 0 | if (XMLUtil::ToInt64(Value(), value)) { |
1497 | 0 | return XML_SUCCESS; |
1498 | 0 | } |
1499 | 0 | return XML_WRONG_ATTRIBUTE_TYPE; |
1500 | 0 | } |
1501 | | |
1502 | | |
1503 | | XMLError XMLAttribute::QueryUnsigned64Value(uint64_t* value) const |
1504 | 0 | { |
1505 | 0 | if(XMLUtil::ToUnsigned64(Value(), value)) { |
1506 | 0 | return XML_SUCCESS; |
1507 | 0 | } |
1508 | 0 | return XML_WRONG_ATTRIBUTE_TYPE; |
1509 | 0 | } |
1510 | | |
1511 | | |
1512 | | XMLError XMLAttribute::QueryBoolValue( bool* value ) const |
1513 | 0 | { |
1514 | 0 | if ( XMLUtil::ToBool( Value(), value )) { |
1515 | 0 | return XML_SUCCESS; |
1516 | 0 | } |
1517 | 0 | return XML_WRONG_ATTRIBUTE_TYPE; |
1518 | 0 | } |
1519 | | |
1520 | | |
1521 | | XMLError XMLAttribute::QueryFloatValue( float* value ) const |
1522 | 0 | { |
1523 | 0 | if ( XMLUtil::ToFloat( Value(), value )) { |
1524 | 0 | return XML_SUCCESS; |
1525 | 0 | } |
1526 | 0 | return XML_WRONG_ATTRIBUTE_TYPE; |
1527 | 0 | } |
1528 | | |
1529 | | |
1530 | | XMLError XMLAttribute::QueryDoubleValue( double* value ) const |
1531 | 0 | { |
1532 | 0 | if ( XMLUtil::ToDouble( Value(), value )) { |
1533 | 0 | return XML_SUCCESS; |
1534 | 0 | } |
1535 | 0 | return XML_WRONG_ATTRIBUTE_TYPE; |
1536 | 0 | } |
1537 | | |
1538 | | |
1539 | | void XMLAttribute::SetAttribute( const char* v ) |
1540 | 0 | { |
1541 | 0 | _value.SetStr( v ); |
1542 | 0 | } |
1543 | | |
1544 | | |
1545 | | void XMLAttribute::SetAttribute( int v ) |
1546 | 0 | { |
1547 | 0 | char buf[BUF_SIZE]; |
1548 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
1549 | 0 | _value.SetStr( buf ); |
1550 | 0 | } |
1551 | | |
1552 | | |
1553 | | void XMLAttribute::SetAttribute( unsigned v ) |
1554 | 0 | { |
1555 | 0 | char buf[BUF_SIZE]; |
1556 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
1557 | 0 | _value.SetStr( buf ); |
1558 | 0 | } |
1559 | | |
1560 | | |
1561 | | void XMLAttribute::SetAttribute(int64_t v) |
1562 | 0 | { |
1563 | 0 | char buf[BUF_SIZE]; |
1564 | 0 | XMLUtil::ToStr(v, buf, BUF_SIZE); |
1565 | 0 | _value.SetStr(buf); |
1566 | 0 | } |
1567 | | |
1568 | | void XMLAttribute::SetAttribute(uint64_t v) |
1569 | 0 | { |
1570 | 0 | char buf[BUF_SIZE]; |
1571 | 0 | XMLUtil::ToStr(v, buf, BUF_SIZE); |
1572 | 0 | _value.SetStr(buf); |
1573 | 0 | } |
1574 | | |
1575 | | |
1576 | | void XMLAttribute::SetAttribute( bool v ) |
1577 | 0 | { |
1578 | 0 | char buf[BUF_SIZE]; |
1579 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
1580 | 0 | _value.SetStr( buf ); |
1581 | 0 | } |
1582 | | |
1583 | | void XMLAttribute::SetAttribute( double v ) |
1584 | 0 | { |
1585 | 0 | char buf[BUF_SIZE]; |
1586 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
1587 | 0 | _value.SetStr( buf ); |
1588 | 0 | } |
1589 | | |
1590 | | void XMLAttribute::SetAttribute( float v ) |
1591 | 0 | { |
1592 | 0 | char buf[BUF_SIZE]; |
1593 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
1594 | 0 | _value.SetStr( buf ); |
1595 | 0 | } |
1596 | | |
1597 | | |
1598 | | // --------- XMLElement ---------- // |
1599 | 1.70M | XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ), |
1600 | 1.70M | _closingType( OPEN ), |
1601 | 1.70M | _rootAttribute( 0 ) |
1602 | 1.70M | { |
1603 | 1.70M | } |
1604 | | |
1605 | | |
1606 | | XMLElement::~XMLElement() |
1607 | 1.70M | { |
1608 | 3.57M | while( _rootAttribute ) { |
1609 | 1.86M | XMLAttribute* next = _rootAttribute->_next; |
1610 | 1.86M | DeleteAttribute( _rootAttribute ); |
1611 | 1.86M | _rootAttribute = next; |
1612 | 1.86M | } |
1613 | 1.70M | } |
1614 | | |
1615 | | |
1616 | | const XMLAttribute* XMLElement::FindAttribute( const char* name ) const |
1617 | 1.86M | { |
1618 | 15.6M | for( XMLAttribute* a = _rootAttribute; a; a = a->_next ) { |
1619 | 13.7M | if ( XMLUtil::StringEqual( a->Name(), name ) ) { |
1620 | 380 | return a; |
1621 | 380 | } |
1622 | 13.7M | } |
1623 | 1.86M | return 0; |
1624 | 1.86M | } |
1625 | | |
1626 | | |
1627 | | const char* XMLElement::Attribute( const char* name, const char* value ) const |
1628 | 1.86M | { |
1629 | 1.86M | const XMLAttribute* a = FindAttribute( name ); |
1630 | 1.86M | if ( !a ) { |
1631 | 1.86M | return 0; |
1632 | 1.86M | } |
1633 | 380 | if ( !value || XMLUtil::StringEqual( a->Value(), value )) { |
1634 | 380 | return a->Value(); |
1635 | 380 | } |
1636 | 0 | return 0; |
1637 | 380 | } |
1638 | | |
1639 | | int XMLElement::IntAttribute(const char* name, int defaultValue) const |
1640 | 0 | { |
1641 | 0 | int i = defaultValue; |
1642 | 0 | QueryIntAttribute(name, &i); |
1643 | 0 | return i; |
1644 | 0 | } |
1645 | | |
1646 | | unsigned XMLElement::UnsignedAttribute(const char* name, unsigned defaultValue) const |
1647 | 0 | { |
1648 | 0 | unsigned i = defaultValue; |
1649 | 0 | QueryUnsignedAttribute(name, &i); |
1650 | 0 | return i; |
1651 | 0 | } |
1652 | | |
1653 | | int64_t XMLElement::Int64Attribute(const char* name, int64_t defaultValue) const |
1654 | 0 | { |
1655 | 0 | int64_t i = defaultValue; |
1656 | 0 | QueryInt64Attribute(name, &i); |
1657 | 0 | return i; |
1658 | 0 | } |
1659 | | |
1660 | | uint64_t XMLElement::Unsigned64Attribute(const char* name, uint64_t defaultValue) const |
1661 | 0 | { |
1662 | 0 | uint64_t i = defaultValue; |
1663 | 0 | QueryUnsigned64Attribute(name, &i); |
1664 | 0 | return i; |
1665 | 0 | } |
1666 | | |
1667 | | bool XMLElement::BoolAttribute(const char* name, bool defaultValue) const |
1668 | 0 | { |
1669 | 0 | bool b = defaultValue; |
1670 | 0 | QueryBoolAttribute(name, &b); |
1671 | 0 | return b; |
1672 | 0 | } |
1673 | | |
1674 | | double XMLElement::DoubleAttribute(const char* name, double defaultValue) const |
1675 | 0 | { |
1676 | 0 | double d = defaultValue; |
1677 | 0 | QueryDoubleAttribute(name, &d); |
1678 | 0 | return d; |
1679 | 0 | } |
1680 | | |
1681 | | float XMLElement::FloatAttribute(const char* name, float defaultValue) const |
1682 | 0 | { |
1683 | 0 | float f = defaultValue; |
1684 | 0 | QueryFloatAttribute(name, &f); |
1685 | 0 | return f; |
1686 | 0 | } |
1687 | | |
1688 | | const char* XMLElement::GetText() const |
1689 | 0 | { |
1690 | | /* skip comment node */ |
1691 | 0 | const XMLNode* node = FirstChild(); |
1692 | 0 | while (node) { |
1693 | 0 | if (node->ToComment()) { |
1694 | 0 | node = node->NextSibling(); |
1695 | 0 | continue; |
1696 | 0 | } |
1697 | 0 | break; |
1698 | 0 | } |
1699 | |
|
1700 | 0 | if ( node && node->ToText() ) { |
1701 | 0 | return node->Value(); |
1702 | 0 | } |
1703 | 0 | return 0; |
1704 | 0 | } |
1705 | | |
1706 | | |
1707 | | void XMLElement::SetText( const char* inText ) |
1708 | 0 | { |
1709 | 0 | if ( FirstChild() && FirstChild()->ToText() ) |
1710 | 0 | FirstChild()->SetValue( inText ); |
1711 | 0 | else { |
1712 | 0 | XMLText* theText = GetDocument()->NewText( inText ); |
1713 | 0 | InsertFirstChild( theText ); |
1714 | 0 | } |
1715 | 0 | } |
1716 | | |
1717 | | |
1718 | | void XMLElement::SetText( int v ) |
1719 | 0 | { |
1720 | 0 | char buf[BUF_SIZE]; |
1721 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
1722 | 0 | SetText( buf ); |
1723 | 0 | } |
1724 | | |
1725 | | |
1726 | | void XMLElement::SetText( unsigned v ) |
1727 | 0 | { |
1728 | 0 | char buf[BUF_SIZE]; |
1729 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
1730 | 0 | SetText( buf ); |
1731 | 0 | } |
1732 | | |
1733 | | |
1734 | | void XMLElement::SetText(int64_t v) |
1735 | 0 | { |
1736 | 0 | char buf[BUF_SIZE]; |
1737 | 0 | XMLUtil::ToStr(v, buf, BUF_SIZE); |
1738 | 0 | SetText(buf); |
1739 | 0 | } |
1740 | | |
1741 | 0 | void XMLElement::SetText(uint64_t v) { |
1742 | 0 | char buf[BUF_SIZE]; |
1743 | 0 | XMLUtil::ToStr(v, buf, BUF_SIZE); |
1744 | 0 | SetText(buf); |
1745 | 0 | } |
1746 | | |
1747 | | |
1748 | | void XMLElement::SetText( bool v ) |
1749 | 0 | { |
1750 | 0 | char buf[BUF_SIZE]; |
1751 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
1752 | 0 | SetText( buf ); |
1753 | 0 | } |
1754 | | |
1755 | | |
1756 | | void XMLElement::SetText( float v ) |
1757 | 0 | { |
1758 | 0 | char buf[BUF_SIZE]; |
1759 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
1760 | 0 | SetText( buf ); |
1761 | 0 | } |
1762 | | |
1763 | | |
1764 | | void XMLElement::SetText( double v ) |
1765 | 0 | { |
1766 | 0 | char buf[BUF_SIZE]; |
1767 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
1768 | 0 | SetText( buf ); |
1769 | 0 | } |
1770 | | |
1771 | | |
1772 | | XMLError XMLElement::QueryIntText( int* ival ) const |
1773 | 0 | { |
1774 | 0 | if ( FirstChild() && FirstChild()->ToText() ) { |
1775 | 0 | const char* t = FirstChild()->Value(); |
1776 | 0 | if ( XMLUtil::ToInt( t, ival ) ) { |
1777 | 0 | return XML_SUCCESS; |
1778 | 0 | } |
1779 | 0 | return XML_CAN_NOT_CONVERT_TEXT; |
1780 | 0 | } |
1781 | 0 | return XML_NO_TEXT_NODE; |
1782 | 0 | } |
1783 | | |
1784 | | |
1785 | | XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const |
1786 | 0 | { |
1787 | 0 | if ( FirstChild() && FirstChild()->ToText() ) { |
1788 | 0 | const char* t = FirstChild()->Value(); |
1789 | 0 | if ( XMLUtil::ToUnsigned( t, uval ) ) { |
1790 | 0 | return XML_SUCCESS; |
1791 | 0 | } |
1792 | 0 | return XML_CAN_NOT_CONVERT_TEXT; |
1793 | 0 | } |
1794 | 0 | return XML_NO_TEXT_NODE; |
1795 | 0 | } |
1796 | | |
1797 | | |
1798 | | XMLError XMLElement::QueryInt64Text(int64_t* ival) const |
1799 | 0 | { |
1800 | 0 | if (FirstChild() && FirstChild()->ToText()) { |
1801 | 0 | const char* t = FirstChild()->Value(); |
1802 | 0 | if (XMLUtil::ToInt64(t, ival)) { |
1803 | 0 | return XML_SUCCESS; |
1804 | 0 | } |
1805 | 0 | return XML_CAN_NOT_CONVERT_TEXT; |
1806 | 0 | } |
1807 | 0 | return XML_NO_TEXT_NODE; |
1808 | 0 | } |
1809 | | |
1810 | | |
1811 | | XMLError XMLElement::QueryUnsigned64Text(uint64_t* uval) const |
1812 | 0 | { |
1813 | 0 | if(FirstChild() && FirstChild()->ToText()) { |
1814 | 0 | const char* t = FirstChild()->Value(); |
1815 | 0 | if(XMLUtil::ToUnsigned64(t, uval)) { |
1816 | 0 | return XML_SUCCESS; |
1817 | 0 | } |
1818 | 0 | return XML_CAN_NOT_CONVERT_TEXT; |
1819 | 0 | } |
1820 | 0 | return XML_NO_TEXT_NODE; |
1821 | 0 | } |
1822 | | |
1823 | | |
1824 | | XMLError XMLElement::QueryBoolText( bool* bval ) const |
1825 | 0 | { |
1826 | 0 | if ( FirstChild() && FirstChild()->ToText() ) { |
1827 | 0 | const char* t = FirstChild()->Value(); |
1828 | 0 | if ( XMLUtil::ToBool( t, bval ) ) { |
1829 | 0 | return XML_SUCCESS; |
1830 | 0 | } |
1831 | 0 | return XML_CAN_NOT_CONVERT_TEXT; |
1832 | 0 | } |
1833 | 0 | return XML_NO_TEXT_NODE; |
1834 | 0 | } |
1835 | | |
1836 | | |
1837 | | XMLError XMLElement::QueryDoubleText( double* dval ) const |
1838 | 0 | { |
1839 | 0 | if ( FirstChild() && FirstChild()->ToText() ) { |
1840 | 0 | const char* t = FirstChild()->Value(); |
1841 | 0 | if ( XMLUtil::ToDouble( t, dval ) ) { |
1842 | 0 | return XML_SUCCESS; |
1843 | 0 | } |
1844 | 0 | return XML_CAN_NOT_CONVERT_TEXT; |
1845 | 0 | } |
1846 | 0 | return XML_NO_TEXT_NODE; |
1847 | 0 | } |
1848 | | |
1849 | | |
1850 | | XMLError XMLElement::QueryFloatText( float* fval ) const |
1851 | 0 | { |
1852 | 0 | if ( FirstChild() && FirstChild()->ToText() ) { |
1853 | 0 | const char* t = FirstChild()->Value(); |
1854 | 0 | if ( XMLUtil::ToFloat( t, fval ) ) { |
1855 | 0 | return XML_SUCCESS; |
1856 | 0 | } |
1857 | 0 | return XML_CAN_NOT_CONVERT_TEXT; |
1858 | 0 | } |
1859 | 0 | return XML_NO_TEXT_NODE; |
1860 | 0 | } |
1861 | | |
1862 | | int XMLElement::IntText(int defaultValue) const |
1863 | 0 | { |
1864 | 0 | int i = defaultValue; |
1865 | 0 | QueryIntText(&i); |
1866 | 0 | return i; |
1867 | 0 | } |
1868 | | |
1869 | | unsigned XMLElement::UnsignedText(unsigned defaultValue) const |
1870 | 0 | { |
1871 | 0 | unsigned i = defaultValue; |
1872 | 0 | QueryUnsignedText(&i); |
1873 | 0 | return i; |
1874 | 0 | } |
1875 | | |
1876 | | int64_t XMLElement::Int64Text(int64_t defaultValue) const |
1877 | 0 | { |
1878 | 0 | int64_t i = defaultValue; |
1879 | 0 | QueryInt64Text(&i); |
1880 | 0 | return i; |
1881 | 0 | } |
1882 | | |
1883 | | uint64_t XMLElement::Unsigned64Text(uint64_t defaultValue) const |
1884 | 0 | { |
1885 | 0 | uint64_t i = defaultValue; |
1886 | 0 | QueryUnsigned64Text(&i); |
1887 | 0 | return i; |
1888 | 0 | } |
1889 | | |
1890 | | bool XMLElement::BoolText(bool defaultValue) const |
1891 | 0 | { |
1892 | 0 | bool b = defaultValue; |
1893 | 0 | QueryBoolText(&b); |
1894 | 0 | return b; |
1895 | 0 | } |
1896 | | |
1897 | | double XMLElement::DoubleText(double defaultValue) const |
1898 | 0 | { |
1899 | 0 | double d = defaultValue; |
1900 | 0 | QueryDoubleText(&d); |
1901 | 0 | return d; |
1902 | 0 | } |
1903 | | |
1904 | | float XMLElement::FloatText(float defaultValue) const |
1905 | 0 | { |
1906 | 0 | float f = defaultValue; |
1907 | 0 | QueryFloatText(&f); |
1908 | 0 | return f; |
1909 | 0 | } |
1910 | | |
1911 | | |
1912 | | XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name ) |
1913 | 0 | { |
1914 | 0 | XMLAttribute* last = 0; |
1915 | 0 | XMLAttribute* attrib = 0; |
1916 | 0 | for( attrib = _rootAttribute; |
1917 | 0 | attrib; |
1918 | 0 | last = attrib, attrib = attrib->_next ) { |
1919 | 0 | if ( XMLUtil::StringEqual( attrib->Name(), name ) ) { |
1920 | 0 | break; |
1921 | 0 | } |
1922 | 0 | } |
1923 | 0 | if ( !attrib ) { |
1924 | 0 | attrib = CreateAttribute(); |
1925 | 0 | TIXMLASSERT( attrib ); |
1926 | 0 | if ( last ) { |
1927 | 0 | TIXMLASSERT( last->_next == 0 ); |
1928 | 0 | last->_next = attrib; |
1929 | 0 | } |
1930 | 0 | else { |
1931 | 0 | TIXMLASSERT( _rootAttribute == 0 ); |
1932 | 0 | _rootAttribute = attrib; |
1933 | 0 | } |
1934 | 0 | attrib->SetName( name ); |
1935 | 0 | } |
1936 | 0 | return attrib; |
1937 | 0 | } |
1938 | | |
1939 | | |
1940 | | void XMLElement::DeleteAttribute( const char* name ) |
1941 | 0 | { |
1942 | 0 | XMLAttribute* prev = 0; |
1943 | 0 | for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) { |
1944 | 0 | if ( XMLUtil::StringEqual( name, a->Name() ) ) { |
1945 | 0 | if ( prev ) { |
1946 | 0 | prev->_next = a->_next; |
1947 | 0 | } |
1948 | 0 | else { |
1949 | 0 | _rootAttribute = a->_next; |
1950 | 0 | } |
1951 | 0 | DeleteAttribute( a ); |
1952 | 0 | break; |
1953 | 0 | } |
1954 | 0 | prev = a; |
1955 | 0 | } |
1956 | 0 | } |
1957 | | |
1958 | | |
1959 | | char* XMLElement::ParseAttributes( char* p, int* curLineNumPtr ) |
1960 | 1.70M | { |
1961 | 1.70M | XMLAttribute* prevAttribute = 0; |
1962 | | |
1963 | | // Read the attributes. |
1964 | 3.57M | while( p ) { |
1965 | 3.57M | p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); |
1966 | 3.57M | if ( !(*p) ) { |
1967 | 293 | _document->SetError( XML_ERROR_PARSING_ELEMENT, _parseLineNum, "XMLElement name=%s", Name() ); |
1968 | 293 | return 0; |
1969 | 293 | } |
1970 | | |
1971 | | // attribute. |
1972 | 3.57M | if (XMLUtil::IsNameStartChar( static_cast<unsigned char>(*p) ) ) { |
1973 | 1.87M | XMLAttribute* attrib = CreateAttribute(); |
1974 | 1.87M | TIXMLASSERT( attrib ); |
1975 | 1.87M | attrib->_parseLineNum = _document->_parseCurLineNum; |
1976 | | |
1977 | 1.87M | const int attrLineNum = attrib->_parseLineNum; |
1978 | | |
1979 | 1.87M | p = attrib->ParseDeep( p, _document->ProcessEntities(), curLineNumPtr ); |
1980 | 1.87M | if ( !p || Attribute( attrib->Name() ) ) { |
1981 | 623 | DeleteAttribute( attrib ); |
1982 | 623 | _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, attrLineNum, "XMLElement name=%s", Name() ); |
1983 | 623 | return 0; |
1984 | 623 | } |
1985 | | // There is a minor bug here: if the attribute in the source xml |
1986 | | // document is duplicated, it will not be detected and the |
1987 | | // attribute will be doubly added. However, tracking the 'prevAttribute' |
1988 | | // avoids re-scanning the attribute list. Preferring performance for |
1989 | | // now, may reconsider in the future. |
1990 | 1.86M | if ( prevAttribute ) { |
1991 | 1.74M | TIXMLASSERT( prevAttribute->_next == 0 ); |
1992 | 1.74M | prevAttribute->_next = attrib; |
1993 | 1.74M | } |
1994 | 123k | else { |
1995 | 123k | TIXMLASSERT( _rootAttribute == 0 ); |
1996 | 123k | _rootAttribute = attrib; |
1997 | 123k | } |
1998 | 1.86M | prevAttribute = attrib; |
1999 | 1.86M | } |
2000 | | // end of the tag |
2001 | 1.70M | else if ( *p == '>' ) { |
2002 | 59.8k | ++p; |
2003 | 59.8k | break; |
2004 | 59.8k | } |
2005 | | // end of the tag |
2006 | 1.64M | else if ( *p == '/' && *(p+1) == '>' ) { |
2007 | 1.64M | _closingType = CLOSED; |
2008 | 1.64M | return p+2; // done; sealed element. |
2009 | 1.64M | } |
2010 | 52 | else { |
2011 | 52 | _document->SetError( XML_ERROR_PARSING_ELEMENT, _parseLineNum, 0 ); |
2012 | 52 | return 0; |
2013 | 52 | } |
2014 | 3.57M | } |
2015 | 59.8k | return p; |
2016 | 1.70M | } |
2017 | | |
2018 | | void XMLElement::DeleteAttribute( XMLAttribute* attribute ) |
2019 | 1.87M | { |
2020 | 1.87M | if ( attribute == 0 ) { |
2021 | 0 | return; |
2022 | 0 | } |
2023 | 1.87M | MemPool* pool = attribute->_memPool; |
2024 | 1.87M | attribute->~XMLAttribute(); |
2025 | 1.87M | pool->Free( attribute ); |
2026 | 1.87M | } |
2027 | | |
2028 | | XMLAttribute* XMLElement::CreateAttribute() |
2029 | 1.87M | { |
2030 | 1.87M | TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() ); |
2031 | 1.87M | XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); |
2032 | 1.87M | TIXMLASSERT( attrib ); |
2033 | 1.87M | attrib->_memPool = &_document->_attributePool; |
2034 | 1.87M | attrib->_memPool->SetTracked(); |
2035 | 1.87M | return attrib; |
2036 | 1.87M | } |
2037 | | |
2038 | | |
2039 | | XMLElement* XMLElement::InsertNewChildElement(const char* name) |
2040 | 0 | { |
2041 | 0 | XMLElement* node = _document->NewElement(name); |
2042 | 0 | return InsertEndChild(node) ? node : 0; |
2043 | 0 | } |
2044 | | |
2045 | | XMLComment* XMLElement::InsertNewComment(const char* comment) |
2046 | 0 | { |
2047 | 0 | XMLComment* node = _document->NewComment(comment); |
2048 | 0 | return InsertEndChild(node) ? node : 0; |
2049 | 0 | } |
2050 | | |
2051 | | XMLText* XMLElement::InsertNewText(const char* text) |
2052 | 0 | { |
2053 | 0 | XMLText* node = _document->NewText(text); |
2054 | 0 | return InsertEndChild(node) ? node : 0; |
2055 | 0 | } |
2056 | | |
2057 | | XMLDeclaration* XMLElement::InsertNewDeclaration(const char* text) |
2058 | 0 | { |
2059 | 0 | XMLDeclaration* node = _document->NewDeclaration(text); |
2060 | 0 | return InsertEndChild(node) ? node : 0; |
2061 | 0 | } |
2062 | | |
2063 | | XMLUnknown* XMLElement::InsertNewUnknown(const char* text) |
2064 | 0 | { |
2065 | 0 | XMLUnknown* node = _document->NewUnknown(text); |
2066 | 0 | return InsertEndChild(node) ? node : 0; |
2067 | 0 | } |
2068 | | |
2069 | | |
2070 | | |
2071 | | // |
2072 | | // <ele></ele> |
2073 | | // <ele>foo<b>bar</b></ele> |
2074 | | // |
2075 | | char* XMLElement::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) |
2076 | 1.70M | { |
2077 | | // Read the element name. |
2078 | 1.70M | p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); |
2079 | | |
2080 | | // The closing element is the </element> form. It is |
2081 | | // parsed just like a regular element then deleted from |
2082 | | // the DOM. |
2083 | 1.70M | if ( *p == '/' ) { |
2084 | 4.02k | _closingType = CLOSING; |
2085 | 4.02k | ++p; |
2086 | 4.02k | } |
2087 | | |
2088 | 1.70M | p = _value.ParseName( p ); |
2089 | 1.70M | if ( _value.Empty() ) { |
2090 | 188 | return 0; |
2091 | 188 | } |
2092 | | |
2093 | 1.70M | p = ParseAttributes( p, curLineNumPtr ); |
2094 | 1.70M | if ( !p || !*p || _closingType != OPEN ) { |
2095 | 1.65M | return p; |
2096 | 1.65M | } |
2097 | | |
2098 | 56.0k | p = XMLNode::ParseDeep( p, parentEndTag, curLineNumPtr ); |
2099 | 56.0k | return p; |
2100 | 1.70M | } |
2101 | | |
2102 | | |
2103 | | |
2104 | | XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const |
2105 | 0 | { |
2106 | 0 | if ( !doc ) { |
2107 | 0 | doc = _document; |
2108 | 0 | } |
2109 | 0 | XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern? |
2110 | 0 | for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) { |
2111 | 0 | element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern? |
2112 | 0 | } |
2113 | 0 | return element; |
2114 | 0 | } |
2115 | | |
2116 | | |
2117 | | bool XMLElement::ShallowEqual( const XMLNode* compare ) const |
2118 | 0 | { |
2119 | 0 | TIXMLASSERT( compare ); |
2120 | 0 | const XMLElement* other = compare->ToElement(); |
2121 | 0 | if ( other && XMLUtil::StringEqual( other->Name(), Name() )) { |
2122 | |
|
2123 | 0 | const XMLAttribute* a=FirstAttribute(); |
2124 | 0 | const XMLAttribute* b=other->FirstAttribute(); |
2125 | |
|
2126 | 0 | while ( a && b ) { |
2127 | 0 | if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) { |
2128 | 0 | return false; |
2129 | 0 | } |
2130 | 0 | a = a->Next(); |
2131 | 0 | b = b->Next(); |
2132 | 0 | } |
2133 | 0 | if ( a || b ) { |
2134 | | // different count |
2135 | 0 | return false; |
2136 | 0 | } |
2137 | 0 | return true; |
2138 | 0 | } |
2139 | 0 | return false; |
2140 | 0 | } |
2141 | | |
2142 | | |
2143 | | bool XMLElement::Accept( XMLVisitor* visitor ) const |
2144 | 0 | { |
2145 | 0 | TIXMLASSERT( visitor ); |
2146 | 0 | if ( visitor->VisitEnter( *this, _rootAttribute ) ) { |
2147 | 0 | for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) { |
2148 | 0 | if ( !node->Accept( visitor ) ) { |
2149 | 0 | break; |
2150 | 0 | } |
2151 | 0 | } |
2152 | 0 | } |
2153 | 0 | return visitor->VisitExit( *this ); |
2154 | 0 | } |
2155 | | |
2156 | | |
2157 | | // --------- XMLDocument ----------- // |
2158 | | |
2159 | | // Warning: List must match 'enum XMLError' |
2160 | | const char* XMLDocument::_errorNames[XML_ERROR_COUNT] = { |
2161 | | "XML_SUCCESS", |
2162 | | "XML_NO_ATTRIBUTE", |
2163 | | "XML_WRONG_ATTRIBUTE_TYPE", |
2164 | | "XML_ERROR_FILE_NOT_FOUND", |
2165 | | "XML_ERROR_FILE_COULD_NOT_BE_OPENED", |
2166 | | "XML_ERROR_FILE_READ_ERROR", |
2167 | | "XML_ERROR_PARSING_ELEMENT", |
2168 | | "XML_ERROR_PARSING_ATTRIBUTE", |
2169 | | "XML_ERROR_PARSING_TEXT", |
2170 | | "XML_ERROR_PARSING_CDATA", |
2171 | | "XML_ERROR_PARSING_COMMENT", |
2172 | | "XML_ERROR_PARSING_DECLARATION", |
2173 | | "XML_ERROR_PARSING_UNKNOWN", |
2174 | | "XML_ERROR_EMPTY_DOCUMENT", |
2175 | | "XML_ERROR_MISMATCHED_ELEMENT", |
2176 | | "XML_ERROR_PARSING", |
2177 | | "XML_CAN_NOT_CONVERT_TEXT", |
2178 | | "XML_NO_TEXT_NODE", |
2179 | | "XML_ELEMENT_DEPTH_EXCEEDED" |
2180 | | }; |
2181 | | |
2182 | | |
2183 | | XMLDocument::XMLDocument( bool processEntities, Whitespace whitespaceMode ) : |
2184 | 2.37k | XMLNode( 0 ), |
2185 | 2.37k | _writeBOM( false ), |
2186 | 2.37k | _processEntities( processEntities ), |
2187 | 2.37k | _errorID(XML_SUCCESS), |
2188 | 2.37k | _whitespaceMode( whitespaceMode ), |
2189 | 2.37k | _errorStr(), |
2190 | 2.37k | _errorLineNum( 0 ), |
2191 | 2.37k | _charBuffer( 0 ), |
2192 | 2.37k | _parseCurLineNum( 0 ), |
2193 | 2.37k | _parsingDepth(0), |
2194 | 2.37k | _unlinked(), |
2195 | 2.37k | _elementPool(), |
2196 | 2.37k | _attributePool(), |
2197 | 2.37k | _textPool(), |
2198 | 2.37k | _commentPool() |
2199 | 2.37k | { |
2200 | | // avoid VC++ C4355 warning about 'this' in initializer list (C4355 is off by default in VS2012+) |
2201 | 2.37k | _document = this; |
2202 | 2.37k | } |
2203 | | |
2204 | | |
2205 | | XMLDocument::~XMLDocument() |
2206 | 2.37k | { |
2207 | 2.37k | Clear(); |
2208 | 2.37k | } |
2209 | | |
2210 | | |
2211 | | void XMLDocument::MarkInUse(const XMLNode* const node) |
2212 | 20.0M | { |
2213 | 20.0M | TIXMLASSERT(node); |
2214 | 20.0M | TIXMLASSERT(node->_parent == 0); |
2215 | | |
2216 | 334M | for (size_t i = 0; i < _unlinked.Size(); ++i) { |
2217 | 324M | if (node == _unlinked[i]) { |
2218 | 10.0M | _unlinked.SwapRemove(i); |
2219 | 10.0M | break; |
2220 | 10.0M | } |
2221 | 324M | } |
2222 | 20.0M | } |
2223 | | |
2224 | | void XMLDocument::Clear() |
2225 | 4.74k | { |
2226 | 4.74k | DeleteChildren(); |
2227 | 4.74k | while( _unlinked.Size()) { |
2228 | 0 | DeleteNode(_unlinked[0]); // Will remove from _unlinked as part of delete. |
2229 | 0 | } |
2230 | | |
2231 | | #ifdef TINYXML2_DEBUG |
2232 | | const bool hadError = Error(); |
2233 | | #endif |
2234 | 4.74k | ClearError(); |
2235 | | |
2236 | 4.74k | delete [] _charBuffer; |
2237 | 4.74k | _charBuffer = 0; |
2238 | 4.74k | _parsingDepth = 0; |
2239 | | |
2240 | | #if 0 |
2241 | | _textPool.Trace( "text" ); |
2242 | | _elementPool.Trace( "element" ); |
2243 | | _commentPool.Trace( "comment" ); |
2244 | | _attributePool.Trace( "attribute" ); |
2245 | | #endif |
2246 | | |
2247 | | #ifdef TINYXML2_DEBUG |
2248 | | if ( !hadError ) { |
2249 | | TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() ); |
2250 | | TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() ); |
2251 | | TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() ); |
2252 | | TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() ); |
2253 | | } |
2254 | | #endif |
2255 | 4.74k | } |
2256 | | |
2257 | | |
2258 | | void XMLDocument::DeepCopy(XMLDocument* target) const |
2259 | 0 | { |
2260 | 0 | TIXMLASSERT(target); |
2261 | 0 | if (target == this) { |
2262 | 0 | return; // technically success - a no-op. |
2263 | 0 | } |
2264 | | |
2265 | 0 | target->Clear(); |
2266 | 0 | for (const XMLNode* node = this->FirstChild(); node; node = node->NextSibling()) { |
2267 | 0 | target->InsertEndChild(node->DeepClone(target)); |
2268 | 0 | } |
2269 | 0 | } |
2270 | | |
2271 | | XMLElement* XMLDocument::NewElement( const char* name ) |
2272 | 0 | { |
2273 | 0 | XMLElement* ele = CreateUnlinkedNode<XMLElement>( _elementPool ); |
2274 | 0 | ele->SetName( name ); |
2275 | 0 | return ele; |
2276 | 0 | } |
2277 | | |
2278 | | |
2279 | | XMLComment* XMLDocument::NewComment( const char* str ) |
2280 | 0 | { |
2281 | 0 | XMLComment* comment = CreateUnlinkedNode<XMLComment>( _commentPool ); |
2282 | 0 | comment->SetValue( str ); |
2283 | 0 | return comment; |
2284 | 0 | } |
2285 | | |
2286 | | |
2287 | | XMLText* XMLDocument::NewText( const char* str ) |
2288 | 0 | { |
2289 | 0 | XMLText* text = CreateUnlinkedNode<XMLText>( _textPool ); |
2290 | 0 | text->SetValue( str ); |
2291 | 0 | return text; |
2292 | 0 | } |
2293 | | |
2294 | | |
2295 | | XMLDeclaration* XMLDocument::NewDeclaration( const char* str ) |
2296 | 0 | { |
2297 | 0 | XMLDeclaration* dec = CreateUnlinkedNode<XMLDeclaration>( _commentPool ); |
2298 | 0 | dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" ); |
2299 | 0 | return dec; |
2300 | 0 | } |
2301 | | |
2302 | | |
2303 | | XMLUnknown* XMLDocument::NewUnknown( const char* str ) |
2304 | 0 | { |
2305 | 0 | XMLUnknown* unk = CreateUnlinkedNode<XMLUnknown>( _commentPool ); |
2306 | 0 | unk->SetValue( str ); |
2307 | 0 | return unk; |
2308 | 0 | } |
2309 | | |
2310 | | static FILE* callfopen( const char* filepath, const char* mode ) |
2311 | 0 | { |
2312 | 0 | TIXMLASSERT( filepath ); |
2313 | 0 | TIXMLASSERT( mode ); |
2314 | | #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE) |
2315 | | FILE* fp = 0; |
2316 | | const errno_t err = fopen_s( &fp, filepath, mode ); |
2317 | | if ( err ) { |
2318 | | return 0; |
2319 | | } |
2320 | | #else |
2321 | 0 | FILE* fp = fopen( filepath, mode ); |
2322 | 0 | #endif |
2323 | 0 | return fp; |
2324 | 0 | } |
2325 | | |
2326 | 54.3k | void XMLDocument::DeleteNode( XMLNode* node ) { |
2327 | 54.3k | TIXMLASSERT( node ); |
2328 | 54.3k | TIXMLASSERT(node->_document == this ); |
2329 | 54.3k | if (node->_parent) { |
2330 | 0 | node->_parent->DeleteChild( node ); |
2331 | 0 | } |
2332 | 54.3k | else { |
2333 | | // Isn't in the tree. |
2334 | | // Use the parent delete. |
2335 | | // Also, we need to mark it tracked: we 'know' |
2336 | | // it was never used. |
2337 | 54.3k | node->_memPool->SetTracked(); |
2338 | | // Call the static XMLNode version: |
2339 | 54.3k | XMLNode::DeleteNode(node); |
2340 | 54.3k | } |
2341 | 54.3k | } |
2342 | | |
2343 | | |
2344 | | XMLError XMLDocument::LoadFile( const char* filename ) |
2345 | 0 | { |
2346 | 0 | if ( !filename ) { |
2347 | 0 | TIXMLASSERT( false ); |
2348 | 0 | SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=<null>" ); |
2349 | 0 | return _errorID; |
2350 | 0 | } |
2351 | | |
2352 | 0 | Clear(); |
2353 | 0 | FILE* fp = callfopen( filename, "rb" ); |
2354 | 0 | if ( !fp ) { |
2355 | 0 | SetError( XML_ERROR_FILE_NOT_FOUND, 0, "filename=%s", filename ); |
2356 | 0 | return _errorID; |
2357 | 0 | } |
2358 | 0 | LoadFile( fp ); |
2359 | 0 | fclose( fp ); |
2360 | 0 | return _errorID; |
2361 | 0 | } |
2362 | | |
2363 | | XMLError XMLDocument::LoadFile( FILE* fp ) |
2364 | 0 | { |
2365 | 0 | Clear(); |
2366 | |
|
2367 | 0 | TIXML_FSEEK( fp, 0, SEEK_SET ); |
2368 | 0 | if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) { |
2369 | 0 | SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); |
2370 | 0 | return _errorID; |
2371 | 0 | } |
2372 | | |
2373 | 0 | TIXML_FSEEK( fp, 0, SEEK_END ); |
2374 | |
|
2375 | 0 | unsigned long long filelength; |
2376 | 0 | { |
2377 | 0 | const long long fileLengthSigned = TIXML_FTELL( fp ); |
2378 | 0 | TIXML_FSEEK( fp, 0, SEEK_SET ); |
2379 | 0 | if ( fileLengthSigned == -1L ) { |
2380 | 0 | SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); |
2381 | 0 | return _errorID; |
2382 | 0 | } |
2383 | 0 | TIXMLASSERT( fileLengthSigned >= 0 ); |
2384 | 0 | filelength = static_cast<unsigned long long>(fileLengthSigned); |
2385 | 0 | } |
2386 | | |
2387 | 0 | const size_t maxSizeT = static_cast<size_t>(-1); |
2388 | | // We'll do the comparison as an unsigned long long, because that's guaranteed to be at |
2389 | | // least 8 bytes, even on a 32-bit platform. |
2390 | 0 | if ( filelength >= static_cast<unsigned long long>(maxSizeT) ) { |
2391 | | // Cannot handle files which won't fit in buffer together with null terminator |
2392 | 0 | SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); |
2393 | 0 | return _errorID; |
2394 | 0 | } |
2395 | | |
2396 | 0 | if ( filelength == 0 ) { |
2397 | 0 | SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); |
2398 | 0 | return _errorID; |
2399 | 0 | } |
2400 | | |
2401 | 0 | const size_t size = static_cast<size_t>(filelength); |
2402 | 0 | TIXMLASSERT( _charBuffer == 0 ); |
2403 | 0 | _charBuffer = new char[size+1]; |
2404 | 0 | const size_t read = fread( _charBuffer, 1, size, fp ); |
2405 | 0 | if ( read != size ) { |
2406 | 0 | SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); |
2407 | 0 | return _errorID; |
2408 | 0 | } |
2409 | | |
2410 | 0 | _charBuffer[size] = 0; |
2411 | |
|
2412 | 0 | Parse(); |
2413 | 0 | return _errorID; |
2414 | 0 | } |
2415 | | |
2416 | | |
2417 | | XMLError XMLDocument::SaveFile( const char* filename, bool compact ) |
2418 | 0 | { |
2419 | 0 | if ( !filename ) { |
2420 | 0 | TIXMLASSERT( false ); |
2421 | 0 | SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=<null>" ); |
2422 | 0 | return _errorID; |
2423 | 0 | } |
2424 | | |
2425 | 0 | FILE* fp = callfopen( filename, "w" ); |
2426 | 0 | if ( !fp ) { |
2427 | 0 | SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=%s", filename ); |
2428 | 0 | return _errorID; |
2429 | 0 | } |
2430 | 0 | SaveFile(fp, compact); |
2431 | 0 | fclose( fp ); |
2432 | 0 | return _errorID; |
2433 | 0 | } |
2434 | | |
2435 | | |
2436 | | XMLError XMLDocument::SaveFile( FILE* fp, bool compact ) |
2437 | 0 | { |
2438 | | // Clear any error from the last save, otherwise it will get reported |
2439 | | // for *this* call. |
2440 | 0 | ClearError(); |
2441 | 0 | XMLPrinter stream( fp, compact ); |
2442 | 0 | Print( &stream ); |
2443 | 0 | return _errorID; |
2444 | 0 | } |
2445 | | |
2446 | | |
2447 | | XMLError XMLDocument::Parse( const char* xml, size_t nBytes ) |
2448 | 2.37k | { |
2449 | 2.37k | Clear(); |
2450 | | |
2451 | 2.37k | if ( nBytes == 0 || !xml || !*xml ) { |
2452 | 3 | SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); |
2453 | 3 | return _errorID; |
2454 | 3 | } |
2455 | 2.36k | if ( nBytes == static_cast<size_t>(-1) ) { |
2456 | 2.36k | nBytes = strlen( xml ); |
2457 | 2.36k | } |
2458 | 2.36k | TIXMLASSERT( _charBuffer == 0 ); |
2459 | 2.36k | _charBuffer = new char[ nBytes+1 ]; |
2460 | 2.36k | memcpy( _charBuffer, xml, nBytes ); |
2461 | 2.36k | _charBuffer[nBytes] = 0; |
2462 | | |
2463 | 2.36k | Parse(); |
2464 | 2.36k | if ( Error() ) { |
2465 | | // clean up now essentially dangling memory. |
2466 | | // and the parse fail can put objects in the |
2467 | | // pools that are dead and inaccessible. |
2468 | 2.16k | DeleteChildren(); |
2469 | 2.16k | _elementPool.Clear(); |
2470 | 2.16k | _attributePool.Clear(); |
2471 | 2.16k | _textPool.Clear(); |
2472 | 2.16k | _commentPool.Clear(); |
2473 | 2.16k | } |
2474 | 2.36k | return _errorID; |
2475 | 2.37k | } |
2476 | | |
2477 | | |
2478 | | void XMLDocument::Print( XMLPrinter* streamer ) const |
2479 | 0 | { |
2480 | 0 | if ( streamer ) { |
2481 | 0 | Accept( streamer ); |
2482 | 0 | } |
2483 | 0 | else { |
2484 | 0 | XMLPrinter stdoutStreamer( stdout ); |
2485 | 0 | Accept( &stdoutStreamer ); |
2486 | 0 | } |
2487 | 0 | } |
2488 | | |
2489 | | |
2490 | 4.74k | void XMLDocument::ClearError() { |
2491 | 4.74k | _errorID = XML_SUCCESS; |
2492 | 4.74k | _errorLineNum = 0; |
2493 | 4.74k | _errorStr.Reset(); |
2494 | 4.74k | } |
2495 | | |
2496 | | |
2497 | | void XMLDocument::SetError( XMLError error, int lineNum, const char* format, ... ) |
2498 | 2.17k | { |
2499 | 2.17k | TIXMLASSERT(error >= 0 && error < XML_ERROR_COUNT); |
2500 | 2.17k | _errorID = error; |
2501 | 2.17k | _errorLineNum = lineNum; |
2502 | 2.17k | _errorStr.Reset(); |
2503 | | |
2504 | 2.17k | const size_t BUFFER_SIZE = 1000; |
2505 | 2.17k | char* buffer = new char[BUFFER_SIZE]; |
2506 | | |
2507 | 2.17k | TIXMLASSERT(sizeof(error) <= sizeof(int)); |
2508 | 2.17k | TIXML_SNPRINTF(buffer, BUFFER_SIZE, "Error=%s ErrorID=%d (0x%x) Line number=%d", |
2509 | 2.17k | ErrorIDToName(error), static_cast<int>(error), static_cast<unsigned int>(error), lineNum); |
2510 | | |
2511 | 2.17k | if (format) { |
2512 | 1.17k | size_t len = strlen(buffer); |
2513 | 1.17k | TIXML_SNPRINTF(buffer + len, BUFFER_SIZE - len, ": "); |
2514 | 1.17k | len = strlen(buffer); |
2515 | | |
2516 | 1.17k | va_list va; |
2517 | 1.17k | va_start(va, format); |
2518 | 1.17k | TIXML_VSNPRINTF(buffer + len, BUFFER_SIZE - len, format, va); |
2519 | 1.17k | va_end(va); |
2520 | 1.17k | } |
2521 | 2.17k | _errorStr.SetStr(buffer); |
2522 | 2.17k | delete[] buffer; |
2523 | 2.17k | } |
2524 | | |
2525 | | |
2526 | | /*static*/ const char* XMLDocument::ErrorIDToName(XMLError errorID) |
2527 | 2.17k | { |
2528 | 2.17k | TIXMLASSERT( errorID >= 0 && errorID < XML_ERROR_COUNT ); |
2529 | 2.17k | const char* errorName = _errorNames[errorID]; |
2530 | 2.17k | TIXMLASSERT( errorName && errorName[0] ); |
2531 | 2.17k | return errorName; |
2532 | 2.17k | } |
2533 | | |
2534 | | const char* XMLDocument::ErrorStr() const |
2535 | 0 | { |
2536 | 0 | return _errorStr.Empty() ? "" : _errorStr.GetStr(); |
2537 | 0 | } |
2538 | | |
2539 | | |
2540 | | void XMLDocument::PrintError() const |
2541 | 0 | { |
2542 | 0 | printf("%s\n", ErrorStr()); |
2543 | 0 | } |
2544 | | |
2545 | | const char* XMLDocument::ErrorName() const |
2546 | 0 | { |
2547 | 0 | return ErrorIDToName(_errorID); |
2548 | 0 | } |
2549 | | |
2550 | | void XMLDocument::Parse() |
2551 | 2.36k | { |
2552 | 2.36k | TIXMLASSERT( NoChildren() ); // Clear() must have been called previously |
2553 | 2.36k | TIXMLASSERT( _charBuffer ); |
2554 | 2.36k | _parseCurLineNum = 1; |
2555 | 2.36k | _parseLineNum = 1; |
2556 | 2.36k | char* p = _charBuffer; |
2557 | 2.36k | p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum ); |
2558 | 2.36k | p = const_cast<char*>( XMLUtil::ReadBOM( p, &_writeBOM ) ); |
2559 | 2.36k | if ( !*p ) { |
2560 | 23 | SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); |
2561 | 23 | return; |
2562 | 23 | } |
2563 | 2.34k | ParseDeep(p, 0, &_parseCurLineNum ); |
2564 | 2.34k | } |
2565 | | |
2566 | | void XMLDocument::PushDepth() |
2567 | 58.3k | { |
2568 | 58.3k | _parsingDepth++; |
2569 | 58.3k | if (_parsingDepth == TINYXML2_MAX_ELEMENT_DEPTH) { |
2570 | 2 | SetError(XML_ELEMENT_DEPTH_EXCEEDED, _parseCurLineNum, "Element nesting is too deep." ); |
2571 | 2 | } |
2572 | 58.3k | } |
2573 | | |
2574 | | void XMLDocument::PopDepth() |
2575 | 58.3k | { |
2576 | 58.3k | TIXMLASSERT(_parsingDepth > 0); |
2577 | 58.3k | --_parsingDepth; |
2578 | 58.3k | } |
2579 | | |
2580 | | XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) : |
2581 | 0 | _elementJustOpened( false ), |
2582 | 0 | _stack(), |
2583 | 0 | _firstElement( true ), |
2584 | 0 | _fp( file ), |
2585 | 0 | _depth( depth ), |
2586 | 0 | _textDepth( -1 ), |
2587 | 0 | _processEntities( true ), |
2588 | 0 | _compactMode( compact ), |
2589 | 0 | _buffer() |
2590 | 0 | { |
2591 | 0 | for( int i=0; i<ENTITY_RANGE; ++i ) { |
2592 | 0 | _entityFlag[i] = false; |
2593 | 0 | _restrictedEntityFlag[i] = false; |
2594 | 0 | } |
2595 | 0 | for( int i=0; i<NUM_ENTITIES; ++i ) { |
2596 | 0 | const char entityValue = entities[i].value; |
2597 | 0 | const unsigned char flagIndex = static_cast<unsigned char>(entityValue); |
2598 | 0 | TIXMLASSERT( flagIndex < ENTITY_RANGE ); |
2599 | 0 | _entityFlag[flagIndex] = true; |
2600 | 0 | } |
2601 | 0 | _restrictedEntityFlag[static_cast<unsigned char>('&')] = true; |
2602 | 0 | _restrictedEntityFlag[static_cast<unsigned char>('<')] = true; |
2603 | 0 | _restrictedEntityFlag[static_cast<unsigned char>('>')] = true; // not required, but consistency is nice |
2604 | 0 | _buffer.Push( 0 ); |
2605 | 0 | } |
2606 | | |
2607 | | |
2608 | | void XMLPrinter::Print( const char* format, ... ) |
2609 | 0 | { |
2610 | 0 | va_list va; |
2611 | 0 | va_start( va, format ); |
2612 | |
|
2613 | 0 | if ( _fp ) { |
2614 | 0 | vfprintf( _fp, format, va ); |
2615 | 0 | } |
2616 | 0 | else { |
2617 | 0 | const int len = TIXML_VSCPRINTF( format, va ); |
2618 | | // Close out and re-start the va-args |
2619 | 0 | va_end( va ); |
2620 | 0 | TIXMLASSERT( len >= 0 ); |
2621 | 0 | va_start( va, format ); |
2622 | 0 | TIXMLASSERT( _buffer.Size() > 0 && _buffer[_buffer.Size() - 1] == 0 ); |
2623 | 0 | char* p = _buffer.PushArr( len ) - 1; // back up over the null terminator. |
2624 | 0 | TIXML_VSNPRINTF( p, len+1, format, va ); |
2625 | 0 | } |
2626 | 0 | va_end( va ); |
2627 | 0 | } |
2628 | | |
2629 | | |
2630 | | void XMLPrinter::Write( const char* data, size_t size ) |
2631 | 0 | { |
2632 | 0 | if ( _fp ) { |
2633 | 0 | fwrite ( data , sizeof(char), size, _fp); |
2634 | 0 | } |
2635 | 0 | else { |
2636 | 0 | char* p = _buffer.PushArr( static_cast<int>(size) ) - 1; // back up over the null terminator. |
2637 | 0 | memcpy( p, data, size ); |
2638 | 0 | p[size] = 0; |
2639 | 0 | } |
2640 | 0 | } |
2641 | | |
2642 | | |
2643 | | void XMLPrinter::Putc( char ch ) |
2644 | 0 | { |
2645 | 0 | if ( _fp ) { |
2646 | 0 | fputc ( ch, _fp); |
2647 | 0 | } |
2648 | 0 | else { |
2649 | 0 | char* p = _buffer.PushArr( sizeof(char) ) - 1; // back up over the null terminator. |
2650 | 0 | p[0] = ch; |
2651 | 0 | p[1] = 0; |
2652 | 0 | } |
2653 | 0 | } |
2654 | | |
2655 | | |
2656 | | void XMLPrinter::PrintSpace( int depth ) |
2657 | 0 | { |
2658 | 0 | for( int i=0; i<depth; ++i ) { |
2659 | 0 | Write( " " ); |
2660 | 0 | } |
2661 | 0 | } |
2662 | | |
2663 | | |
2664 | | void XMLPrinter::PrintString( const char* p, bool restricted ) |
2665 | 0 | { |
2666 | | // Look for runs of bytes between entities to print. |
2667 | 0 | const char* q = p; |
2668 | |
|
2669 | 0 | if ( _processEntities ) { |
2670 | 0 | const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag; |
2671 | 0 | while ( *q ) { |
2672 | 0 | TIXMLASSERT( p <= q ); |
2673 | | // Remember, char is sometimes signed. (How many times has that bitten me?) |
2674 | 0 | if ( *q > 0 && *q < ENTITY_RANGE ) { |
2675 | | // Check for entities. If one is found, flush |
2676 | | // the stream up until the entity, write the |
2677 | | // entity, and keep looking. |
2678 | 0 | if ( flag[static_cast<unsigned char>(*q)] ) { |
2679 | 0 | while ( p < q ) { |
2680 | 0 | const size_t delta = q - p; |
2681 | 0 | const int toPrint = ( INT_MAX < delta ) ? INT_MAX : static_cast<int>(delta); |
2682 | 0 | Write( p, toPrint ); |
2683 | 0 | p += toPrint; |
2684 | 0 | } |
2685 | 0 | bool entityPatternPrinted = false; |
2686 | 0 | for( int i=0; i<NUM_ENTITIES; ++i ) { |
2687 | 0 | if ( entities[i].value == *q ) { |
2688 | 0 | Putc( '&' ); |
2689 | 0 | Write( entities[i].pattern, entities[i].length ); |
2690 | 0 | Putc( ';' ); |
2691 | 0 | entityPatternPrinted = true; |
2692 | 0 | break; |
2693 | 0 | } |
2694 | 0 | } |
2695 | 0 | if ( !entityPatternPrinted ) { |
2696 | | // TIXMLASSERT( entityPatternPrinted ) causes gcc -Wunused-but-set-variable in release |
2697 | 0 | TIXMLASSERT( false ); |
2698 | 0 | } |
2699 | 0 | ++p; |
2700 | 0 | } |
2701 | 0 | } |
2702 | 0 | ++q; |
2703 | 0 | TIXMLASSERT( p <= q ); |
2704 | 0 | } |
2705 | | // Flush the remaining string. This will be the entire |
2706 | | // string if an entity wasn't found. |
2707 | 0 | if ( p < q ) { |
2708 | 0 | const size_t delta = q - p; |
2709 | 0 | const int toPrint = ( INT_MAX < delta ) ? INT_MAX : static_cast<int>(delta); |
2710 | 0 | Write( p, toPrint ); |
2711 | 0 | } |
2712 | 0 | } |
2713 | 0 | else { |
2714 | 0 | Write( p ); |
2715 | 0 | } |
2716 | 0 | } |
2717 | | |
2718 | | |
2719 | | void XMLPrinter::PushHeader( bool writeBOM, bool writeDec ) |
2720 | 0 | { |
2721 | 0 | if ( writeBOM ) { |
2722 | 0 | static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 }; |
2723 | 0 | Write( reinterpret_cast< const char* >( bom ) ); |
2724 | 0 | } |
2725 | 0 | if ( writeDec ) { |
2726 | 0 | PushDeclaration( "xml version=\"1.0\"" ); |
2727 | 0 | } |
2728 | 0 | } |
2729 | | |
2730 | | void XMLPrinter::PrepareForNewNode( bool compactMode ) |
2731 | 0 | { |
2732 | 0 | SealElementIfJustOpened(); |
2733 | |
|
2734 | 0 | if ( compactMode ) { |
2735 | 0 | return; |
2736 | 0 | } |
2737 | | |
2738 | 0 | if ( _firstElement ) { |
2739 | 0 | PrintSpace (_depth); |
2740 | 0 | } else if ( _textDepth < 0) { |
2741 | 0 | Putc( '\n' ); |
2742 | 0 | PrintSpace( _depth ); |
2743 | 0 | } |
2744 | |
|
2745 | 0 | _firstElement = false; |
2746 | 0 | } |
2747 | | |
2748 | | void XMLPrinter::OpenElement( const char* name, bool compactMode ) |
2749 | 0 | { |
2750 | 0 | PrepareForNewNode( compactMode ); |
2751 | 0 | _stack.Push( name ); |
2752 | |
|
2753 | 0 | Write ( "<" ); |
2754 | 0 | Write ( name ); |
2755 | |
|
2756 | 0 | _elementJustOpened = true; |
2757 | 0 | ++_depth; |
2758 | 0 | } |
2759 | | |
2760 | | |
2761 | | void XMLPrinter::PushAttribute( const char* name, const char* value ) |
2762 | 0 | { |
2763 | 0 | TIXMLASSERT( _elementJustOpened ); |
2764 | 0 | Putc ( ' ' ); |
2765 | 0 | Write( name ); |
2766 | 0 | Write( "=\"" ); |
2767 | 0 | PrintString( value, false ); |
2768 | 0 | Putc ( '\"' ); |
2769 | 0 | } |
2770 | | |
2771 | | |
2772 | | void XMLPrinter::PushAttribute( const char* name, int v ) |
2773 | 0 | { |
2774 | 0 | char buf[BUF_SIZE]; |
2775 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
2776 | 0 | PushAttribute( name, buf ); |
2777 | 0 | } |
2778 | | |
2779 | | |
2780 | | void XMLPrinter::PushAttribute( const char* name, unsigned v ) |
2781 | 0 | { |
2782 | 0 | char buf[BUF_SIZE]; |
2783 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
2784 | 0 | PushAttribute( name, buf ); |
2785 | 0 | } |
2786 | | |
2787 | | |
2788 | | void XMLPrinter::PushAttribute(const char* name, int64_t v) |
2789 | 0 | { |
2790 | 0 | char buf[BUF_SIZE]; |
2791 | 0 | XMLUtil::ToStr(v, buf, BUF_SIZE); |
2792 | 0 | PushAttribute(name, buf); |
2793 | 0 | } |
2794 | | |
2795 | | |
2796 | | void XMLPrinter::PushAttribute(const char* name, uint64_t v) |
2797 | 0 | { |
2798 | 0 | char buf[BUF_SIZE]; |
2799 | 0 | XMLUtil::ToStr(v, buf, BUF_SIZE); |
2800 | 0 | PushAttribute(name, buf); |
2801 | 0 | } |
2802 | | |
2803 | | |
2804 | | void XMLPrinter::PushAttribute( const char* name, bool v ) |
2805 | 0 | { |
2806 | 0 | char buf[BUF_SIZE]; |
2807 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
2808 | 0 | PushAttribute( name, buf ); |
2809 | 0 | } |
2810 | | |
2811 | | |
2812 | | void XMLPrinter::PushAttribute( const char* name, double v ) |
2813 | 0 | { |
2814 | 0 | char buf[BUF_SIZE]; |
2815 | 0 | XMLUtil::ToStr( v, buf, BUF_SIZE ); |
2816 | 0 | PushAttribute( name, buf ); |
2817 | 0 | } |
2818 | | |
2819 | | |
2820 | | void XMLPrinter::CloseElement( bool compactMode ) |
2821 | 0 | { |
2822 | 0 | --_depth; |
2823 | 0 | const char* name = _stack.Pop(); |
2824 | |
|
2825 | 0 | if ( _elementJustOpened ) { |
2826 | 0 | Write( "/>" ); |
2827 | 0 | } |
2828 | 0 | else { |
2829 | 0 | if ( _textDepth < 0 && !compactMode) { |
2830 | 0 | Putc( '\n' ); |
2831 | 0 | PrintSpace( _depth ); |
2832 | 0 | } |
2833 | 0 | Write ( "</" ); |
2834 | 0 | Write ( name ); |
2835 | 0 | Write ( ">" ); |
2836 | 0 | } |
2837 | |
|
2838 | 0 | if ( _textDepth == _depth ) { |
2839 | 0 | _textDepth = -1; |
2840 | 0 | } |
2841 | 0 | if ( _depth == 0 && !compactMode) { |
2842 | 0 | Putc( '\n' ); |
2843 | 0 | } |
2844 | 0 | _elementJustOpened = false; |
2845 | 0 | } |
2846 | | |
2847 | | |
2848 | | void XMLPrinter::SealElementIfJustOpened() |
2849 | 0 | { |
2850 | 0 | if ( !_elementJustOpened ) { |
2851 | 0 | return; |
2852 | 0 | } |
2853 | 0 | _elementJustOpened = false; |
2854 | 0 | Putc( '>' ); |
2855 | 0 | } |
2856 | | |
2857 | | |
2858 | | void XMLPrinter::PushText( const char* text, bool cdata ) |
2859 | 0 | { |
2860 | 0 | _textDepth = _depth-1; |
2861 | |
|
2862 | 0 | SealElementIfJustOpened(); |
2863 | 0 | if ( cdata ) { |
2864 | 0 | Write( "<![CDATA[" ); |
2865 | 0 | Write( text ); |
2866 | 0 | Write( "]]>" ); |
2867 | 0 | } |
2868 | 0 | else { |
2869 | 0 | PrintString( text, true ); |
2870 | 0 | } |
2871 | 0 | } |
2872 | | |
2873 | | |
2874 | | void XMLPrinter::PushText( int64_t value ) |
2875 | 0 | { |
2876 | 0 | char buf[BUF_SIZE]; |
2877 | 0 | XMLUtil::ToStr( value, buf, BUF_SIZE ); |
2878 | 0 | PushText( buf, false ); |
2879 | 0 | } |
2880 | | |
2881 | | |
2882 | | void XMLPrinter::PushText( uint64_t value ) |
2883 | 0 | { |
2884 | 0 | char buf[BUF_SIZE]; |
2885 | 0 | XMLUtil::ToStr(value, buf, BUF_SIZE); |
2886 | 0 | PushText(buf, false); |
2887 | 0 | } |
2888 | | |
2889 | | |
2890 | | void XMLPrinter::PushText( int value ) |
2891 | 0 | { |
2892 | 0 | char buf[BUF_SIZE]; |
2893 | 0 | XMLUtil::ToStr( value, buf, BUF_SIZE ); |
2894 | 0 | PushText( buf, false ); |
2895 | 0 | } |
2896 | | |
2897 | | |
2898 | | void XMLPrinter::PushText( unsigned value ) |
2899 | 0 | { |
2900 | 0 | char buf[BUF_SIZE]; |
2901 | 0 | XMLUtil::ToStr( value, buf, BUF_SIZE ); |
2902 | 0 | PushText( buf, false ); |
2903 | 0 | } |
2904 | | |
2905 | | |
2906 | | void XMLPrinter::PushText( bool value ) |
2907 | 0 | { |
2908 | 0 | char buf[BUF_SIZE]; |
2909 | 0 | XMLUtil::ToStr( value, buf, BUF_SIZE ); |
2910 | 0 | PushText( buf, false ); |
2911 | 0 | } |
2912 | | |
2913 | | |
2914 | | void XMLPrinter::PushText( float value ) |
2915 | 0 | { |
2916 | 0 | char buf[BUF_SIZE]; |
2917 | 0 | XMLUtil::ToStr( value, buf, BUF_SIZE ); |
2918 | 0 | PushText( buf, false ); |
2919 | 0 | } |
2920 | | |
2921 | | |
2922 | | void XMLPrinter::PushText( double value ) |
2923 | 0 | { |
2924 | 0 | char buf[BUF_SIZE]; |
2925 | 0 | XMLUtil::ToStr( value, buf, BUF_SIZE ); |
2926 | 0 | PushText( buf, false ); |
2927 | 0 | } |
2928 | | |
2929 | | |
2930 | | void XMLPrinter::PushComment( const char* comment ) |
2931 | 0 | { |
2932 | 0 | PrepareForNewNode( _compactMode ); |
2933 | |
|
2934 | 0 | Write( "<!--" ); |
2935 | 0 | Write( comment ); |
2936 | 0 | Write( "-->" ); |
2937 | 0 | } |
2938 | | |
2939 | | |
2940 | | void XMLPrinter::PushDeclaration( const char* value ) |
2941 | 0 | { |
2942 | 0 | PrepareForNewNode( _compactMode ); |
2943 | |
|
2944 | 0 | Write( "<?" ); |
2945 | 0 | Write( value ); |
2946 | 0 | Write( "?>" ); |
2947 | 0 | } |
2948 | | |
2949 | | |
2950 | | void XMLPrinter::PushUnknown( const char* value ) |
2951 | 0 | { |
2952 | 0 | PrepareForNewNode( _compactMode ); |
2953 | |
|
2954 | 0 | Write( "<!" ); |
2955 | 0 | Write( value ); |
2956 | 0 | Putc( '>' ); |
2957 | 0 | } |
2958 | | |
2959 | | |
2960 | | bool XMLPrinter::VisitEnter( const XMLDocument& doc ) |
2961 | 0 | { |
2962 | 0 | _processEntities = doc.ProcessEntities(); |
2963 | 0 | if ( doc.HasBOM() ) { |
2964 | 0 | PushHeader( true, false ); |
2965 | 0 | } |
2966 | 0 | return true; |
2967 | 0 | } |
2968 | | |
2969 | | |
2970 | | bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) |
2971 | 0 | { |
2972 | 0 | const XMLElement* parentElem = 0; |
2973 | 0 | if ( element.Parent() ) { |
2974 | 0 | parentElem = element.Parent()->ToElement(); |
2975 | 0 | } |
2976 | 0 | const bool compactMode = parentElem ? CompactMode( *parentElem ) : _compactMode; |
2977 | 0 | OpenElement( element.Name(), compactMode ); |
2978 | 0 | while ( attribute ) { |
2979 | 0 | PushAttribute( attribute->Name(), attribute->Value() ); |
2980 | 0 | attribute = attribute->Next(); |
2981 | 0 | } |
2982 | 0 | return true; |
2983 | 0 | } |
2984 | | |
2985 | | |
2986 | | bool XMLPrinter::VisitExit( const XMLElement& element ) |
2987 | 0 | { |
2988 | 0 | CloseElement( CompactMode(element) ); |
2989 | 0 | return true; |
2990 | 0 | } |
2991 | | |
2992 | | |
2993 | | bool XMLPrinter::Visit( const XMLText& text ) |
2994 | 0 | { |
2995 | 0 | PushText( text.Value(), text.CData() ); |
2996 | 0 | return true; |
2997 | 0 | } |
2998 | | |
2999 | | |
3000 | | bool XMLPrinter::Visit( const XMLComment& comment ) |
3001 | 0 | { |
3002 | 0 | PushComment( comment.Value() ); |
3003 | 0 | return true; |
3004 | 0 | } |
3005 | | |
3006 | | bool XMLPrinter::Visit( const XMLDeclaration& declaration ) |
3007 | 0 | { |
3008 | 0 | PushDeclaration( declaration.Value() ); |
3009 | 0 | return true; |
3010 | 0 | } |
3011 | | |
3012 | | |
3013 | | bool XMLPrinter::Visit( const XMLUnknown& unknown ) |
3014 | 0 | { |
3015 | 0 | PushUnknown( unknown.Value() ); |
3016 | 0 | return true; |
3017 | 0 | } |
3018 | | |
3019 | | } // namespace tinyxml2 |