Lee Thomason (grinliz) | 2812986 | 2012-02-25 21:11:20 -0800 | [diff] [blame] | 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 | */
|
Lee Thomason (grinliz) | 9c38d13 | 2012-02-24 21:50:50 -0800 | [diff] [blame] | 23 |
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 24 | #include "tinyxml2.h"
|
| 25 |
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 26 | #include <new> // yes, this one new style header, is in the Android SDK.
|
Wilfred van Velzen | 67abee5 | 2016-03-25 14:01:15 +0100 | [diff] [blame] | 27 | #if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 28 | # include <stddef.h>
|
Philipp Kloke | 358202c | 2015-07-30 16:02:26 +0200 | [diff] [blame] | 29 | # include <stdarg.h>
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 30 | #else
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 31 | # include <cstddef>
|
Philipp Kloke | 358202c | 2015-07-30 16:02:26 +0200 | [diff] [blame] | 32 | # include <cstdarg>
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 33 | #endif
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 34 |
|
Lee Thomason | 53db4a6 | 2015-06-11 22:52:08 -0700 | [diff] [blame] | 35 | #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
|
Dmitry-Me | 1ca593c | 2015-06-22 12:49:32 +0300 | [diff] [blame] | 36 | // Microsoft Visual Studio, version 2005 and higher. Not WinCE.
|
Lee Thomason | 53db4a6 | 2015-06-11 22:52:08 -0700 | [diff] [blame] | 37 | /*int _snprintf_s(
|
| 38 | char *buffer,
|
| 39 | size_t sizeOfBuffer,
|
| 40 | size_t count,
|
| 41 | const char *format [,
|
| 42 | argument] ...
|
| 43 | );*/
|
PKEuS | cac7578 | 2015-08-15 18:17:27 +0200 | [diff] [blame] | 44 | static inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... )
|
Lee Thomason | 53db4a6 | 2015-06-11 22:52:08 -0700 | [diff] [blame] | 45 | {
|
| 46 | va_list va;
|
| 47 | va_start( va, format );
|
| 48 | int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
|
| 49 | va_end( va );
|
| 50 | return result;
|
| 51 | }
|
| 52 |
|
PKEuS | cac7578 | 2015-08-15 18:17:27 +0200 | [diff] [blame] | 53 | static inline int TIXML_VSNPRINTF( char* buffer, size_t size, const char* format, va_list va )
|
Lee Thomason | 53db4a6 | 2015-06-11 22:52:08 -0700 | [diff] [blame] | 54 | {
|
| 55 | 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
|
Lee Thomason | aa8566b | 2015-06-19 16:52:40 -0700 | [diff] [blame] | 66 | #if (_MSC_VER < 1400 ) && (!defined WINCE)
|
Lee Thomason | 53db4a6 | 2015-06-11 22:52:08 -0700 | [diff] [blame] | 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.
|
PKEuS | cac7578 | 2015-08-15 18:17:27 +0200 | [diff] [blame] | 71 | static inline int TIXML_VSCPRINTF( const char* format, va_list va )
|
Lee Thomason | 53db4a6 | 2015-06-11 22:52:08 -0700 | [diff] [blame] | 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 ) {
|
Dmitry-Me | 1d32e58 | 2015-07-27 17:11:51 +0300 | [diff] [blame] | 80 | TIXMLASSERT( required >= 0 );
|
Lee Thomason | 53db4a6 | 2015-06-11 22:52:08 -0700 | [diff] [blame] | 81 | len = required;
|
| 82 | break;
|
| 83 | }
|
| 84 | }
|
Dmitry-Me | 1d32e58 | 2015-07-27 17:11:51 +0300 | [diff] [blame] | 85 | TIXMLASSERT( len >= 0 );
|
Lee Thomason | 53db4a6 | 2015-06-11 22:52:08 -0700 | [diff] [blame] | 86 | return len;
|
| 87 | }
|
| 88 | #endif
|
| 89 | #else
|
| 90 | // GCC version 3 and higher
|
| 91 | //#warning( "Using sn* functions." )
|
| 92 | #define TIXML_SNPRINTF snprintf
|
| 93 | #define TIXML_VSNPRINTF vsnprintf
|
PKEuS | cac7578 | 2015-08-15 18:17:27 +0200 | [diff] [blame] | 94 | static inline int TIXML_VSCPRINTF( const char* format, va_list va )
|
Lee Thomason | 53db4a6 | 2015-06-11 22:52:08 -0700 | [diff] [blame] | 95 | {
|
| 96 | int len = vsnprintf( 0, 0, format, va );
|
Dmitry-Me | 1d32e58 | 2015-07-27 17:11:51 +0300 | [diff] [blame] | 97 | TIXMLASSERT( len >= 0 );
|
Lee Thomason | 53db4a6 | 2015-06-11 22:52:08 -0700 | [diff] [blame] | 98 | return len;
|
| 99 | }
|
| 100 | #define TIXML_SSCANF sscanf
|
| 101 | #endif
|
| 102 |
|
| 103 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 104 | static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 105 | static const char LF = LINE_FEED;
|
| 106 | static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
|
| 107 | static const char CR = CARRIAGE_RETURN;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 108 | static const char SINGLE_QUOTE = '\'';
|
| 109 | static const char DOUBLE_QUOTE = '\"';
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 110 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 111 | // Bunch of unicode info at:
|
| 112 | // http://www.unicode.org/faq/utf_bom.html
|
| 113 | // ef bb bf (Microsoft "lead bytes") - designates UTF-8
|
| 114 |
|
| 115 | static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
|
| 116 | static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
|
| 117 | static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 118 |
|
Kevin Wojniak | 04c22d2 | 2012-11-08 11:02:22 -0800 | [diff] [blame] | 119 | namespace tinyxml2
|
| 120 | {
|
| 121 |
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 122 | struct Entity {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 123 | const char* pattern;
|
| 124 | int length;
|
| 125 | char value;
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 126 | };
|
| 127 |
|
| 128 | static const int NUM_ENTITIES = 5;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 129 | static const Entity entities[NUM_ENTITIES] = {
|
| 130 | { "quot", 4, DOUBLE_QUOTE },
|
| 131 | { "amp", 3, '&' },
|
| 132 | { "apos", 4, SINGLE_QUOTE },
|
| 133 | { "lt", 2, '<' },
|
| 134 | { "gt", 2, '>' }
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 135 | };
|
| 136 |
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 137 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 138 | StrPair::~StrPair()
|
| 139 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 140 | Reset();
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 141 | }
|
| 142 |
|
| 143 |
|
Lee Thomason | 2965880 | 2014-11-27 22:31:11 -0800 | [diff] [blame] | 144 | void StrPair::TransferTo( StrPair* other )
|
Dmitry-Me | 08b40dd | 2014-11-10 11:17:21 +0300 | [diff] [blame] | 145 | {
|
Lee Thomason | 2965880 | 2014-11-27 22:31:11 -0800 | [diff] [blame] | 146 | if ( this == other ) {
|
Dmitry-Me | 08b40dd | 2014-11-10 11:17:21 +0300 | [diff] [blame] | 147 | return;
|
| 148 | }
|
| 149 | // This in effect implements the assignment operator by "moving"
|
| 150 | // ownership (as in auto_ptr).
|
| 151 |
|
Dmitry-Me | db02b21 | 2016-08-04 17:16:05 +0300 | [diff] [blame] | 152 | TIXMLASSERT( other != 0 );
|
Lee Thomason | 2965880 | 2014-11-27 22:31:11 -0800 | [diff] [blame] | 153 | TIXMLASSERT( other->_flags == 0 );
|
| 154 | TIXMLASSERT( other->_start == 0 );
|
| 155 | TIXMLASSERT( other->_end == 0 );
|
Dmitry-Me | 08b40dd | 2014-11-10 11:17:21 +0300 | [diff] [blame] | 156 |
|
Lee Thomason | 2965880 | 2014-11-27 22:31:11 -0800 | [diff] [blame] | 157 | other->Reset();
|
Dmitry-Me | 08b40dd | 2014-11-10 11:17:21 +0300 | [diff] [blame] | 158 |
|
Lee Thomason | 2965880 | 2014-11-27 22:31:11 -0800 | [diff] [blame] | 159 | other->_flags = _flags;
|
| 160 | other->_start = _start;
|
| 161 | other->_end = _end;
|
Dmitry-Me | 08b40dd | 2014-11-10 11:17:21 +0300 | [diff] [blame] | 162 |
|
| 163 | _flags = 0;
|
| 164 | _start = 0;
|
| 165 | _end = 0;
|
| 166 | }
|
| 167 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 168 | void StrPair::Reset()
|
| 169 | {
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 170 | if ( _flags & NEEDS_DELETE ) {
|
| 171 | delete [] _start;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 172 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 173 | _flags = 0;
|
| 174 | _start = 0;
|
| 175 | _end = 0;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 176 | }
|
| 177 |
|
| 178 |
|
| 179 | void StrPair::SetStr( const char* str, int flags )
|
| 180 | {
|
Dmitry-Me | 0515fa9 | 2015-12-09 11:54:06 +0300 | [diff] [blame] | 181 | TIXMLASSERT( str );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 182 | Reset();
|
| 183 | size_t len = strlen( str );
|
Dmitry-Me | 96f38cc | 2015-08-10 16:45:12 +0300 | [diff] [blame] | 184 | TIXMLASSERT( _start == 0 );
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 185 | _start = new char[ len+1 ];
|
| 186 | memcpy( _start, str, len+1 );
|
| 187 | _end = _start + len;
|
| 188 | _flags = flags | NEEDS_DELETE;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 189 | }
|
| 190 |
|
| 191 |
|
| 192 | char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
|
| 193 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 194 | TIXMLASSERT( endTag && *endTag );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 195 |
|
Dmitry-Me | ec19a0e | 2014-08-25 11:05:55 +0400 | [diff] [blame] | 196 | char* start = p;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 197 | char endChar = *endTag;
|
| 198 | size_t length = strlen( endTag );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 199 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 200 | // Inner loop of text parsing.
|
| 201 | while ( *p ) {
|
| 202 | if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
|
| 203 | Set( start, p, strFlags );
|
| 204 | return p + length;
|
| 205 | }
|
| 206 | ++p;
|
| 207 | }
|
| 208 | return 0;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 209 | }
|
| 210 |
|
| 211 |
|
| 212 | char* StrPair::ParseName( char* p )
|
| 213 | {
|
Dmitry-Me | 9fb2b0f | 2014-09-23 17:27:39 +0400 | [diff] [blame] | 214 | if ( !p || !(*p) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 215 | return 0;
|
| 216 | }
|
JayXon | ee525db | 2014-12-24 04:01:42 -0500 | [diff] [blame] | 217 | if ( !XMLUtil::IsNameStartChar( *p ) ) {
|
| 218 | return 0;
|
| 219 | }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 220 |
|
Dmitry-Me | 9fb2b0f | 2014-09-23 17:27:39 +0400 | [diff] [blame] | 221 | char* const start = p;
|
JayXon | ee525db | 2014-12-24 04:01:42 -0500 | [diff] [blame] | 222 | ++p;
|
| 223 | while ( *p && XMLUtil::IsNameChar( *p ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 224 | ++p;
|
| 225 | }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 226 |
|
JayXon | ee525db | 2014-12-24 04:01:42 -0500 | [diff] [blame] | 227 | Set( start, p, 0 );
|
| 228 | return p;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 229 | }
|
| 230 |
|
| 231 |
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 232 | void StrPair::CollapseWhitespace()
|
| 233 | {
|
Dmitry-Me | 67a5bb0 | 2014-08-20 10:01:53 +0400 | [diff] [blame] | 234 | // Adjusting _start would cause undefined behavior on delete[]
|
| 235 | TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 236 | // Trim leading space.
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 237 | _start = XMLUtil::SkipWhiteSpace( _start );
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 238 |
|
Dmitry-Me | bb836dc | 2014-12-24 11:54:05 +0300 | [diff] [blame] | 239 | if ( *_start ) {
|
Dmitry-Me | 2449582 | 2016-09-02 16:53:32 +0300 | [diff] [blame] | 240 | const char* p = _start; // the read pointer
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 241 | char* q = _start; // the write pointer
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 242 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 243 | while( *p ) {
|
| 244 | if ( XMLUtil::IsWhiteSpace( *p )) {
|
| 245 | p = XMLUtil::SkipWhiteSpace( p );
|
| 246 | if ( *p == 0 ) {
|
| 247 | break; // don't write to q; this trims the trailing space.
|
| 248 | }
|
| 249 | *q = ' ';
|
| 250 | ++q;
|
| 251 | }
|
| 252 | *q = *p;
|
| 253 | ++q;
|
| 254 | ++p;
|
| 255 | }
|
| 256 | *q = 0;
|
| 257 | }
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 258 | }
|
| 259 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 260 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 261 | const char* StrPair::GetStr()
|
| 262 | {
|
Dmitry-Me | 5ffa73e | 2015-01-01 17:47:40 +0300 | [diff] [blame] | 263 | TIXMLASSERT( _start );
|
| 264 | TIXMLASSERT( _end );
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 265 | if ( _flags & NEEDS_FLUSH ) {
|
| 266 | *_end = 0;
|
| 267 | _flags ^= NEEDS_FLUSH;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 268 |
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 269 | if ( _flags ) {
|
Dmitry-Me | 2449582 | 2016-09-02 16:53:32 +0300 | [diff] [blame] | 270 | const char* p = _start; // the read pointer
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 271 | char* q = _start; // the write pointer
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 272 |
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 273 | while( p < _end ) {
|
| 274 | if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 275 | // CR-LF pair becomes LF
|
| 276 | // CR alone becomes LF
|
| 277 | // LF-CR becomes LF
|
| 278 | if ( *(p+1) == LF ) {
|
| 279 | p += 2;
|
| 280 | }
|
| 281 | else {
|
| 282 | ++p;
|
| 283 | }
|
| 284 | *q++ = LF;
|
| 285 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 286 | else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 287 | if ( *(p+1) == CR ) {
|
| 288 | p += 2;
|
| 289 | }
|
| 290 | else {
|
| 291 | ++p;
|
| 292 | }
|
| 293 | *q++ = LF;
|
| 294 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 295 | else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 296 | // Entities handled by tinyXML2:
|
| 297 | // - special entities in the entity table [in/out]
|
| 298 | // - numeric character reference [in]
|
| 299 | // 中 or 中
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 300 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 301 | if ( *(p+1) == '#' ) {
|
Dmitry-Me | 63f3de1 | 2014-08-21 12:33:19 +0400 | [diff] [blame] | 302 | const int buflen = 10;
|
| 303 | char buf[buflen] = { 0 };
|
| 304 | int len = 0;
|
Dmitry-Me | 6f51c80 | 2015-03-14 13:25:03 +0300 | [diff] [blame] | 305 | char* adjusted = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
|
| 306 | if ( adjusted == 0 ) {
|
| 307 | *q = *p;
|
| 308 | ++p;
|
| 309 | ++q;
|
| 310 | }
|
| 311 | else {
|
| 312 | TIXMLASSERT( 0 <= len && len <= buflen );
|
| 313 | TIXMLASSERT( q + len <= adjusted );
|
| 314 | p = adjusted;
|
| 315 | memcpy( q, buf, len );
|
| 316 | q += len;
|
| 317 | }
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 318 | }
|
| 319 | else {
|
Dmitry-Me | 764545e | 2015-05-20 10:29:24 +0300 | [diff] [blame] | 320 | bool entityFound = false;
|
| 321 | for( int i = 0; i < NUM_ENTITIES; ++i ) {
|
Dmitry-Me | d048f1e | 2014-10-01 10:30:16 +0400 | [diff] [blame] | 322 | const Entity& entity = entities[i];
|
| 323 | if ( strncmp( p + 1, entity.pattern, entity.length ) == 0
|
| 324 | && *( p + entity.length + 1 ) == ';' ) {
|
| 325 | // Found an entity - convert.
|
| 326 | *q = entity.value;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 327 | ++q;
|
Dmitry-Me | d048f1e | 2014-10-01 10:30:16 +0400 | [diff] [blame] | 328 | p += entity.length + 2;
|
Dmitry-Me | 764545e | 2015-05-20 10:29:24 +0300 | [diff] [blame] | 329 | entityFound = true;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 330 | break;
|
| 331 | }
|
| 332 | }
|
Dmitry-Me | 764545e | 2015-05-20 10:29:24 +0300 | [diff] [blame] | 333 | if ( !entityFound ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 334 | // fixme: treat as error?
|
| 335 | ++p;
|
| 336 | ++q;
|
| 337 | }
|
| 338 | }
|
| 339 | }
|
| 340 | else {
|
| 341 | *q = *p;
|
| 342 | ++p;
|
| 343 | ++q;
|
| 344 | }
|
| 345 | }
|
| 346 | *q = 0;
|
| 347 | }
|
| 348 | // The loop below has plenty going on, and this
|
| 349 | // is a less useful mode. Break it out.
|
Dmitry-Me | 5420e54 | 2015-05-20 10:51:26 +0300 | [diff] [blame] | 350 | if ( _flags & NEEDS_WHITESPACE_COLLAPSING ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 351 | CollapseWhitespace();
|
| 352 | }
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 353 | _flags = (_flags & NEEDS_DELETE);
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 354 | }
|
Dmitry-Me | 5ffa73e | 2015-01-01 17:47:40 +0300 | [diff] [blame] | 355 | TIXMLASSERT( _start );
|
Lee Thomason | 120b3a6 | 2012-10-12 10:06:59 -0700 | [diff] [blame] | 356 | return _start;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 357 | }
|
| 358 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 359 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 360 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 361 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 362 | // --------- XMLUtil ----------- //
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 363 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 364 | const char* XMLUtil::ReadBOM( const char* p, bool* bom )
|
| 365 | {
|
Dmitry-Me | bb836dc | 2014-12-24 11:54:05 +0300 | [diff] [blame] | 366 | TIXMLASSERT( p );
|
| 367 | TIXMLASSERT( bom );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 368 | *bom = false;
|
| 369 | const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);
|
| 370 | // Check for BOM:
|
| 371 | if ( *(pu+0) == TIXML_UTF_LEAD_0
|
| 372 | && *(pu+1) == TIXML_UTF_LEAD_1
|
| 373 | && *(pu+2) == TIXML_UTF_LEAD_2 ) {
|
| 374 | *bom = true;
|
| 375 | p += 3;
|
| 376 | }
|
Dmitry-Me | bb836dc | 2014-12-24 11:54:05 +0300 | [diff] [blame] | 377 | TIXMLASSERT( p );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 378 | return p;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 379 | }
|
| 380 |
|
| 381 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 382 | void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
|
| 383 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 384 | const unsigned long BYTE_MASK = 0xBF;
|
| 385 | const unsigned long BYTE_MARK = 0x80;
|
| 386 | const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 387 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 388 | if (input < 0x80) {
|
| 389 | *length = 1;
|
| 390 | }
|
| 391 | else if ( input < 0x800 ) {
|
| 392 | *length = 2;
|
| 393 | }
|
| 394 | else if ( input < 0x10000 ) {
|
| 395 | *length = 3;
|
| 396 | }
|
| 397 | else if ( input < 0x200000 ) {
|
| 398 | *length = 4;
|
| 399 | }
|
| 400 | else {
|
Dmitry-Me | 2f465c4 | 2015-03-16 11:08:23 +0300 | [diff] [blame] | 401 | *length = 0; // This code won't convert this correctly anyway.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 402 | return;
|
| 403 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 404 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 405 | output += *length;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 406 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 407 | // Scary scary fall throughs.
|
| 408 | switch (*length) {
|
| 409 | case 4:
|
| 410 | --output;
|
| 411 | *output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
| 412 | input >>= 6;
|
| 413 | case 3:
|
| 414 | --output;
|
| 415 | *output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
| 416 | input >>= 6;
|
| 417 | case 2:
|
| 418 | --output;
|
| 419 | *output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
| 420 | input >>= 6;
|
| 421 | case 1:
|
| 422 | --output;
|
| 423 | *output = (char)(input | FIRST_BYTE_MARK[*length]);
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 424 | break;
|
Dmitry-Me | 33bb764 | 2015-03-14 17:14:00 +0300 | [diff] [blame] | 425 | default:
|
| 426 | TIXMLASSERT( false );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 427 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 428 | }
|
| 429 |
|
| 430 |
|
| 431 | const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
|
| 432 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 433 | // Presume an entity, and pull it out.
|
| 434 | *length = 0;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 435 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 436 | if ( *(p+1) == '#' && *(p+2) ) {
|
| 437 | unsigned long ucs = 0;
|
Dmitry-Me | bbaf1e1 | 2015-01-12 14:07:10 +0300 | [diff] [blame] | 438 | TIXMLASSERT( sizeof( ucs ) >= 4 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 439 | ptrdiff_t delta = 0;
|
| 440 | unsigned mult = 1;
|
Lee Thomason | 7e67bc8 | 2015-01-12 14:05:12 -0800 | [diff] [blame] | 441 | static const char SEMICOLON = ';';
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 442 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 443 | if ( *(p+2) == 'x' ) {
|
| 444 | // Hexadecimal.
|
Dmitry-Me | 6acc9a5 | 2015-01-15 13:27:47 +0300 | [diff] [blame] | 445 | const char* q = p+3;
|
| 446 | if ( !(*q) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 447 | return 0;
|
| 448 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 449 |
|
Lee Thomason | 7e67bc8 | 2015-01-12 14:05:12 -0800 | [diff] [blame] | 450 | q = strchr( q, SEMICOLON );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 451 |
|
Dmitry-Me | 9f56e12 | 2015-01-12 10:07:54 +0300 | [diff] [blame] | 452 | if ( !q ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 453 | return 0;
|
| 454 | }
|
Lee Thomason | 7e67bc8 | 2015-01-12 14:05:12 -0800 | [diff] [blame] | 455 | TIXMLASSERT( *q == SEMICOLON );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 456 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 457 | delta = q-p;
|
| 458 | --q;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 459 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 460 | while ( *q != 'x' ) {
|
Lee Thomason | 7265b76 | 2015-03-15 16:11:47 -0700 | [diff] [blame] | 461 | unsigned int digit = 0;
|
| 462 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 463 | if ( *q >= '0' && *q <= '9' ) {
|
Dmitry-Me | bab9b6d | 2015-03-14 16:41:46 +0300 | [diff] [blame] | 464 | digit = *q - '0';
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 465 | }
|
| 466 | else if ( *q >= 'a' && *q <= 'f' ) {
|
Dmitry-Me | bab9b6d | 2015-03-14 16:41:46 +0300 | [diff] [blame] | 467 | digit = *q - 'a' + 10;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 468 | }
|
| 469 | else if ( *q >= 'A' && *q <= 'F' ) {
|
Dmitry-Me | bab9b6d | 2015-03-14 16:41:46 +0300 | [diff] [blame] | 470 | digit = *q - 'A' + 10;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 471 | }
|
| 472 | else {
|
| 473 | return 0;
|
| 474 | }
|
Wilfred van Velzen | 0aeac18 | 2016-03-25 14:14:03 +0100 | [diff] [blame] | 475 | TIXMLASSERT( digit < 16 );
|
Dmitry-Me | bab9b6d | 2015-03-14 16:41:46 +0300 | [diff] [blame] | 476 | TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
|
| 477 | const unsigned int digitScaled = mult * digit;
|
| 478 | TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );
|
| 479 | ucs += digitScaled;
|
Dmitry-Me | bbaf1e1 | 2015-01-12 14:07:10 +0300 | [diff] [blame] | 480 | TIXMLASSERT( mult <= UINT_MAX / 16 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 481 | mult *= 16;
|
| 482 | --q;
|
| 483 | }
|
| 484 | }
|
| 485 | else {
|
| 486 | // Decimal.
|
Dmitry-Me | 6acc9a5 | 2015-01-15 13:27:47 +0300 | [diff] [blame] | 487 | const char* q = p+2;
|
| 488 | if ( !(*q) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 489 | return 0;
|
| 490 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 491 |
|
Lee Thomason | 7e67bc8 | 2015-01-12 14:05:12 -0800 | [diff] [blame] | 492 | q = strchr( q, SEMICOLON );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 493 |
|
Dmitry-Me | 9f56e12 | 2015-01-12 10:07:54 +0300 | [diff] [blame] | 494 | if ( !q ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 495 | return 0;
|
| 496 | }
|
Lee Thomason | 7e67bc8 | 2015-01-12 14:05:12 -0800 | [diff] [blame] | 497 | TIXMLASSERT( *q == SEMICOLON );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 498 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 499 | delta = q-p;
|
| 500 | --q;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 501 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 502 | while ( *q != '#' ) {
|
| 503 | if ( *q >= '0' && *q <= '9' ) {
|
Dmitry-Me | bab9b6d | 2015-03-14 16:41:46 +0300 | [diff] [blame] | 504 | const unsigned int digit = *q - '0';
|
Wilfred van Velzen | 0aeac18 | 2016-03-25 14:14:03 +0100 | [diff] [blame] | 505 | TIXMLASSERT( digit < 10 );
|
Dmitry-Me | bab9b6d | 2015-03-14 16:41:46 +0300 | [diff] [blame] | 506 | TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
|
| 507 | const unsigned int digitScaled = mult * digit;
|
| 508 | TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );
|
| 509 | ucs += digitScaled;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 510 | }
|
| 511 | else {
|
| 512 | return 0;
|
| 513 | }
|
Dmitry-Me | bbaf1e1 | 2015-01-12 14:07:10 +0300 | [diff] [blame] | 514 | TIXMLASSERT( mult <= UINT_MAX / 10 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 515 | mult *= 10;
|
| 516 | --q;
|
| 517 | }
|
| 518 | }
|
| 519 | // convert the UCS to UTF-8
|
| 520 | ConvertUTF32ToUTF8( ucs, value, length );
|
| 521 | return p + delta + 1;
|
| 522 | }
|
| 523 | return p+1;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 524 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 525 |
|
| 526 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 527 | void XMLUtil::ToStr( int v, char* buffer, int bufferSize )
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 528 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 529 | TIXML_SNPRINTF( buffer, bufferSize, "%d", v );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 530 | }
|
| 531 |
|
| 532 |
|
| 533 | void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize )
|
| 534 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 535 | TIXML_SNPRINTF( buffer, bufferSize, "%u", v );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 536 | }
|
| 537 |
|
| 538 |
|
| 539 | void XMLUtil::ToStr( bool v, char* buffer, int bufferSize )
|
| 540 | {
|
Doruk Turak | de45d04 | 2016-08-28 20:47:08 +0200 | [diff] [blame] | 541 | TIXML_SNPRINTF( buffer, bufferSize, "%s", v ? "true" : "false" );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 542 | }
|
| 543 |
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 544 | /*
|
| 545 | ToStr() of a number is a very tricky topic.
|
| 546 | https://github.com/leethomason/tinyxml2/issues/106
|
| 547 | */
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 548 | void XMLUtil::ToStr( float v, char* buffer, int bufferSize )
|
| 549 | {
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 550 | TIXML_SNPRINTF( buffer, bufferSize, "%.8g", v );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 551 | }
|
| 552 |
|
| 553 |
|
| 554 | void XMLUtil::ToStr( double v, char* buffer, int bufferSize )
|
| 555 | {
|
Lee Thomason | c3708cc | 2014-01-14 12:30:03 -0800 | [diff] [blame] | 556 | TIXML_SNPRINTF( buffer, bufferSize, "%.17g", v );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 557 | }
|
| 558 |
|
| 559 |
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 560 | void XMLUtil::ToStr(int64_t v, char* buffer, int bufferSize)
|
| 561 | {
|
Lee Thomason | 5bf60e9 | 2016-07-17 22:49:40 -0700 | [diff] [blame] | 562 | // horrible syntax trick to make the compiler happy about %lld
|
| 563 | TIXML_SNPRINTF(buffer, bufferSize, "%lld", (long long)v);
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 564 | }
|
| 565 |
|
| 566 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 567 | bool XMLUtil::ToInt( const char* str, int* value )
|
| 568 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 569 | if ( TIXML_SSCANF( str, "%d", value ) == 1 ) {
|
| 570 | return true;
|
| 571 | }
|
| 572 | return false;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 573 | }
|
| 574 |
|
| 575 | bool XMLUtil::ToUnsigned( const char* str, unsigned *value )
|
| 576 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 577 | if ( TIXML_SSCANF( str, "%u", value ) == 1 ) {
|
| 578 | return true;
|
| 579 | }
|
| 580 | return false;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 581 | }
|
| 582 |
|
| 583 | bool XMLUtil::ToBool( const char* str, bool* value )
|
| 584 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 585 | int ival = 0;
|
| 586 | if ( ToInt( str, &ival )) {
|
| 587 | *value = (ival==0) ? false : true;
|
| 588 | return true;
|
| 589 | }
|
| 590 | if ( StringEqual( str, "true" ) ) {
|
| 591 | *value = true;
|
| 592 | return true;
|
| 593 | }
|
| 594 | else if ( StringEqual( str, "false" ) ) {
|
| 595 | *value = false;
|
| 596 | return true;
|
| 597 | }
|
| 598 | return false;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 599 | }
|
| 600 |
|
| 601 |
|
| 602 | bool XMLUtil::ToFloat( const char* str, float* value )
|
| 603 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 604 | if ( TIXML_SSCANF( str, "%f", value ) == 1 ) {
|
| 605 | return true;
|
| 606 | }
|
| 607 | return false;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 608 | }
|
| 609 |
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 610 |
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 611 | bool XMLUtil::ToDouble( const char* str, double* value )
|
| 612 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 613 | if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) {
|
| 614 | return true;
|
| 615 | }
|
| 616 | return false;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 617 | }
|
| 618 |
|
| 619 |
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 620 | bool XMLUtil::ToInt64(const char* str, int64_t* value)
|
| 621 | {
|
Lee Thomason | 5bf60e9 | 2016-07-17 22:49:40 -0700 | [diff] [blame] | 622 | long long v = 0; // horrible syntax trick to make the compiler happy about %lld
|
| 623 | if (TIXML_SSCANF(str, "%lld", &v) == 1) {
|
| 624 | *value = (int64_t)v;
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 625 | return true;
|
| 626 | }
|
| 627 | return false;
|
| 628 | }
|
| 629 |
|
| 630 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 631 | char* XMLDocument::Identify( char* p, XMLNode** node )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 632 | {
|
Dmitry-Me | 0238466 | 2015-03-03 16:02:13 +0300 | [diff] [blame] | 633 | TIXMLASSERT( node );
|
| 634 | TIXMLASSERT( p );
|
Dmitry-Me | 9fb2b0f | 2014-09-23 17:27:39 +0400 | [diff] [blame] | 635 | char* const start = p;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 636 | p = XMLUtil::SkipWhiteSpace( p );
|
Dmitry-Me | bb836dc | 2014-12-24 11:54:05 +0300 | [diff] [blame] | 637 | if( !*p ) {
|
Dmitry-Me | 9fcb876 | 2015-03-05 17:53:34 +0300 | [diff] [blame] | 638 | *node = 0;
|
| 639 | TIXMLASSERT( p );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 640 | return p;
|
| 641 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 642 |
|
Dmitry-Me | 962083b | 2015-05-26 11:38:30 +0300 | [diff] [blame] | 643 | // These strings define the matching patterns:
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 644 | static const char* xmlHeader = { "<?" };
|
| 645 | static const char* commentHeader = { "<!--" };
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 646 | static const char* cdataHeader = { "<![CDATA[" };
|
Dmitry-Me | c505e13 | 2015-03-30 09:54:36 +0300 | [diff] [blame] | 647 | static const char* dtdHeader = { "<!" };
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 648 | static const char* elementHeader = { "<" }; // and a header for everything else; check last.
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 649 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 650 | static const int xmlHeaderLen = 2;
|
| 651 | static const int commentHeaderLen = 4;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 652 | static const int cdataHeaderLen = 9;
|
Dmitry-Me | c505e13 | 2015-03-30 09:54:36 +0300 | [diff] [blame] | 653 | static const int dtdHeaderLen = 2;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 654 | static const int elementHeaderLen = 1;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 655 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 656 | TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
|
| 657 | TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
|
Dmitry-Me | 9fb2b0f | 2014-09-23 17:27:39 +0400 | [diff] [blame] | 658 | XMLNode* returnNode = 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 659 | if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 660 | TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 661 | returnNode = new (_commentPool.Alloc()) XMLDeclaration( this );
|
| 662 | returnNode->_memPool = &_commentPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 663 | p += xmlHeaderLen;
|
| 664 | }
|
| 665 | else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 666 | TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 667 | returnNode = new (_commentPool.Alloc()) XMLComment( this );
|
| 668 | returnNode->_memPool = &_commentPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 669 | p += commentHeaderLen;
|
| 670 | }
|
| 671 | else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 672 | TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 673 | XMLText* text = new (_textPool.Alloc()) XMLText( this );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 674 | returnNode = text;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 675 | returnNode->_memPool = &_textPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 676 | p += cdataHeaderLen;
|
| 677 | text->SetCData( true );
|
| 678 | }
|
| 679 | else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 680 | TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 681 | returnNode = new (_commentPool.Alloc()) XMLUnknown( this );
|
| 682 | returnNode->_memPool = &_commentPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 683 | p += dtdHeaderLen;
|
| 684 | }
|
| 685 | else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 686 | TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 687 | returnNode = new (_elementPool.Alloc()) XMLElement( this );
|
| 688 | returnNode->_memPool = &_elementPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 689 | p += elementHeaderLen;
|
| 690 | }
|
| 691 | else {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 692 | TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 693 | returnNode = new (_textPool.Alloc()) XMLText( this );
|
| 694 | returnNode->_memPool = &_textPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 695 | p = start; // Back it up, all the text counts.
|
| 696 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 697 |
|
Dmitry-Me | 0238466 | 2015-03-03 16:02:13 +0300 | [diff] [blame] | 698 | TIXMLASSERT( returnNode );
|
| 699 | TIXMLASSERT( p );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 700 | *node = returnNode;
|
| 701 | return p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 702 | }
|
| 703 |
|
| 704 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 705 | bool XMLDocument::Accept( XMLVisitor* visitor ) const
|
| 706 | {
|
Dmitry-Me | bbaf1e1 | 2015-01-12 14:07:10 +0300 | [diff] [blame] | 707 | TIXMLASSERT( visitor );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 708 | if ( visitor->VisitEnter( *this ) ) {
|
| 709 | for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
|
| 710 | if ( !node->Accept( visitor ) ) {
|
| 711 | break;
|
| 712 | }
|
| 713 | }
|
| 714 | }
|
| 715 | return visitor->VisitExit( *this );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 716 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 717 |
|
| 718 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 719 | // --------- XMLNode ----------- //
|
| 720 |
|
| 721 | XMLNode::XMLNode( XMLDocument* doc ) :
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 722 | _document( doc ),
|
| 723 | _parent( 0 ),
|
| 724 | _firstChild( 0 ), _lastChild( 0 ),
|
Thomas Roß | 6189231 | 2013-05-12 14:07:38 +0200 | [diff] [blame] | 725 | _prev( 0 ), _next( 0 ),
|
Lee Thomason | af9bce1 | 2016-07-17 22:35:52 -0700 | [diff] [blame] | 726 | _userData( 0 ),
|
Thomas Roß | 6189231 | 2013-05-12 14:07:38 +0200 | [diff] [blame] | 727 | _memPool( 0 )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 728 | {
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 729 | }
|
| 730 |
|
| 731 |
|
| 732 | XMLNode::~XMLNode()
|
| 733 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 734 | DeleteChildren();
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 735 | if ( _parent ) {
|
| 736 | _parent->Unlink( this );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 737 | }
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 738 | }
|
| 739 |
|
Michael Daumling | 2162688 | 2013-10-22 17:03:37 +0200 | [diff] [blame] | 740 | const char* XMLNode::Value() const
|
| 741 | {
|
Dmitry-Me | caa72a6 | 2016-08-10 17:34:34 +0300 | [diff] [blame] | 742 | // Edge case: XMLDocuments don't have a Value. Return null.
|
Sarat Addepalli | 9c3122b | 2015-05-19 12:49:32 +0530 | [diff] [blame] | 743 | if ( this->ToDocument() )
|
Sarat Addepalli | 96b4346 | 2015-05-20 10:36:06 +0530 | [diff] [blame] | 744 | return 0;
|
Michael Daumling | 2162688 | 2013-10-22 17:03:37 +0200 | [diff] [blame] | 745 | return _value.GetStr();
|
| 746 | }
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 747 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 748 | void XMLNode::SetValue( const char* str, bool staticMem )
|
| 749 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 750 | if ( staticMem ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 751 | _value.SetInternedStr( str );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 752 | }
|
| 753 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 754 | _value.SetStr( str );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 755 | }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 756 | }
|
| 757 |
|
| 758 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 759 | void XMLNode::DeleteChildren()
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 760 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 761 | while( _firstChild ) {
|
Dmitry-Me | 9614f8f | 2015-04-08 10:06:06 +0300 | [diff] [blame] | 762 | TIXMLASSERT( _lastChild );
|
Dmitry-Me | 9cb4eca | 2016-08-18 18:10:59 +0300 | [diff] [blame] | 763 | DeleteChild( _firstChild );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 764 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 765 | _firstChild = _lastChild = 0;
|
Lee Thomason | d923c67 | 2012-01-23 08:44:25 -0800 | [diff] [blame] | 766 | }
|
| 767 |
|
| 768 |
|
| 769 | void XMLNode::Unlink( XMLNode* child )
|
| 770 | {
|
Dmitry-Me | bb836dc | 2014-12-24 11:54:05 +0300 | [diff] [blame] | 771 | TIXMLASSERT( child );
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 772 | TIXMLASSERT( child->_document == _document );
|
Dmitry-Me | 9614f8f | 2015-04-08 10:06:06 +0300 | [diff] [blame] | 773 | TIXMLASSERT( child->_parent == this );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 774 | if ( child == _firstChild ) {
|
| 775 | _firstChild = _firstChild->_next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 776 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 777 | if ( child == _lastChild ) {
|
| 778 | _lastChild = _lastChild->_prev;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 779 | }
|
Lee Thomason | d923c67 | 2012-01-23 08:44:25 -0800 | [diff] [blame] | 780 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 781 | if ( child->_prev ) {
|
| 782 | child->_prev->_next = child->_next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 783 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 784 | if ( child->_next ) {
|
| 785 | child->_next->_prev = child->_prev;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 786 | }
|
Lee Thomason | 3b7927e | 2013-10-26 21:50:46 -0700 | [diff] [blame] | 787 | child->_parent = 0;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 788 | }
|
| 789 |
|
| 790 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 791 | void XMLNode::DeleteChild( XMLNode* node )
|
| 792 | {
|
Dmitry-Me | bb836dc | 2014-12-24 11:54:05 +0300 | [diff] [blame] | 793 | TIXMLASSERT( node );
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 794 | TIXMLASSERT( node->_document == _document );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 795 | TIXMLASSERT( node->_parent == this );
|
Jarle Strand | 81abfd6 | 2015-12-27 17:30:04 +0100 | [diff] [blame] | 796 | Unlink( node );
|
Dmitry-Me | e3225b1 | 2014-09-03 11:03:11 +0400 | [diff] [blame] | 797 | DeleteNode( node );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 798 | }
|
| 799 |
|
| 800 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 801 | XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
|
| 802 | {
|
Dmitry-Me | ed2a407 | 2014-12-12 10:38:48 +0300 | [diff] [blame] | 803 | TIXMLASSERT( addThis );
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 804 | if ( addThis->_document != _document ) {
|
| 805 | TIXMLASSERT( false );
|
| 806 | return 0;
|
| 807 | }
|
Lee Thomason | 3cebdc4 | 2015-01-05 17:16:28 -0800 | [diff] [blame] | 808 | InsertChildPreamble( addThis );
|
Lee Thomason | 3b7927e | 2013-10-26 21:50:46 -0700 | [diff] [blame] | 809 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 810 | if ( _lastChild ) {
|
| 811 | TIXMLASSERT( _firstChild );
|
| 812 | TIXMLASSERT( _lastChild->_next == 0 );
|
| 813 | _lastChild->_next = addThis;
|
| 814 | addThis->_prev = _lastChild;
|
| 815 | _lastChild = addThis;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 816 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 817 | addThis->_next = 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 818 | }
|
| 819 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 820 | TIXMLASSERT( _firstChild == 0 );
|
| 821 | _firstChild = _lastChild = addThis;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 822 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 823 | addThis->_prev = 0;
|
| 824 | addThis->_next = 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 825 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 826 | addThis->_parent = this;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 827 | return addThis;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 828 | }
|
| 829 |
|
| 830 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 831 | XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
|
| 832 | {
|
Dmitry-Me | ed2a407 | 2014-12-12 10:38:48 +0300 | [diff] [blame] | 833 | TIXMLASSERT( addThis );
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 834 | if ( addThis->_document != _document ) {
|
| 835 | TIXMLASSERT( false );
|
| 836 | return 0;
|
| 837 | }
|
Lee Thomason | 3cebdc4 | 2015-01-05 17:16:28 -0800 | [diff] [blame] | 838 | InsertChildPreamble( addThis );
|
Lee Thomason | 3b7927e | 2013-10-26 21:50:46 -0700 | [diff] [blame] | 839 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 840 | if ( _firstChild ) {
|
| 841 | TIXMLASSERT( _lastChild );
|
| 842 | TIXMLASSERT( _firstChild->_prev == 0 );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 843 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 844 | _firstChild->_prev = addThis;
|
| 845 | addThis->_next = _firstChild;
|
| 846 | _firstChild = addThis;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 847 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 848 | addThis->_prev = 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 849 | }
|
| 850 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 851 | TIXMLASSERT( _lastChild == 0 );
|
| 852 | _firstChild = _lastChild = addThis;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 853 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 854 | addThis->_prev = 0;
|
| 855 | addThis->_next = 0;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 856 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 857 | addThis->_parent = this;
|
Dmitry-Me | 9fb2b0f | 2014-09-23 17:27:39 +0400 | [diff] [blame] | 858 | return addThis;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 859 | }
|
| 860 |
|
| 861 |
|
| 862 | XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
|
| 863 | {
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 864 | TIXMLASSERT( addThis );
|
| 865 | if ( addThis->_document != _document ) {
|
| 866 | TIXMLASSERT( false );
|
| 867 | return 0;
|
| 868 | }
|
Lee Thomason | 3b7927e | 2013-10-26 21:50:46 -0700 | [diff] [blame] | 869 |
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 870 | TIXMLASSERT( afterThis );
|
Lee Thomason | 3b7927e | 2013-10-26 21:50:46 -0700 | [diff] [blame] | 871 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 872 | if ( afterThis->_parent != this ) {
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 873 | TIXMLASSERT( false );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 874 | return 0;
|
| 875 | }
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 876 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 877 | if ( afterThis->_next == 0 ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 878 | // The last node or the only node.
|
| 879 | return InsertEndChild( addThis );
|
| 880 | }
|
Lee Thomason | 3cebdc4 | 2015-01-05 17:16:28 -0800 | [diff] [blame] | 881 | InsertChildPreamble( addThis );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 882 | addThis->_prev = afterThis;
|
| 883 | addThis->_next = afterThis->_next;
|
| 884 | afterThis->_next->_prev = addThis;
|
| 885 | afterThis->_next = addThis;
|
| 886 | addThis->_parent = this;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 887 | return addThis;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 888 | }
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 893 | const XMLElement* XMLNode::FirstChildElement( const char* name ) const
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 894 | {
|
Dmitry-Me | 2667aab | 2015-04-03 10:56:59 +0300 | [diff] [blame] | 895 | for( const XMLNode* node = _firstChild; node; node = node->_next ) {
|
| 896 | const XMLElement* element = node->ToElement();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 897 | if ( element ) {
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 898 | if ( !name || XMLUtil::StringEqual( element->Name(), name ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 899 | return element;
|
| 900 | }
|
| 901 | }
|
| 902 | }
|
| 903 | return 0;
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 904 | }
|
| 905 |
|
| 906 |
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 907 | const XMLElement* XMLNode::LastChildElement( const char* name ) const
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 908 | {
|
Dmitry-Me | 2667aab | 2015-04-03 10:56:59 +0300 | [diff] [blame] | 909 | for( const XMLNode* node = _lastChild; node; node = node->_prev ) {
|
| 910 | const XMLElement* element = node->ToElement();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 911 | if ( element ) {
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 912 | if ( !name || XMLUtil::StringEqual( element->Name(), name ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 913 | return element;
|
| 914 | }
|
| 915 | }
|
| 916 | }
|
| 917 | return 0;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 918 | }
|
| 919 |
|
| 920 |
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 921 | const XMLElement* XMLNode::NextSiblingElement( const char* name ) const
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 922 | {
|
Dmitry-Me | 2667aab | 2015-04-03 10:56:59 +0300 | [diff] [blame] | 923 | for( const XMLNode* node = _next; node; node = node->_next ) {
|
Dmitry-Me | b6b4e82 | 2014-08-27 17:17:47 +0400 | [diff] [blame] | 924 | const XMLElement* element = node->ToElement();
|
| 925 | if ( element
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 926 | && (!name || XMLUtil::StringEqual( name, element->Name() ))) {
|
Dmitry-Me | b6b4e82 | 2014-08-27 17:17:47 +0400 | [diff] [blame] | 927 | return element;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 928 | }
|
| 929 | }
|
| 930 | return 0;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 931 | }
|
| 932 |
|
| 933 |
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 934 | const XMLElement* XMLNode::PreviousSiblingElement( const char* name ) const
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 935 | {
|
Dmitry-Me | 2667aab | 2015-04-03 10:56:59 +0300 | [diff] [blame] | 936 | for( const XMLNode* node = _prev; node; node = node->_prev ) {
|
Dmitry-Me | b6b4e82 | 2014-08-27 17:17:47 +0400 | [diff] [blame] | 937 | const XMLElement* element = node->ToElement();
|
| 938 | if ( element
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 939 | && (!name || XMLUtil::StringEqual( name, element->Name() ))) {
|
Dmitry-Me | b6b4e82 | 2014-08-27 17:17:47 +0400 | [diff] [blame] | 940 | return element;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 941 | }
|
| 942 | }
|
| 943 | return 0;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 944 | }
|
| 945 |
|
| 946 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 947 | char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 948 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 949 | // This is a recursive method, but thinking about it "at the current level"
|
| 950 | // it is a pretty simple flat list:
|
| 951 | // <foo/>
|
| 952 | // <!-- comment -->
|
| 953 | //
|
| 954 | // With a special case:
|
| 955 | // <foo>
|
| 956 | // </foo>
|
| 957 | // <!-- comment -->
|
| 958 | //
|
| 959 | // Where the closing element (/foo) *must* be the next thing after the opening
|
| 960 | // element, and the names must match. BUT the tricky bit is that the closing
|
| 961 | // element will be read by the child.
|
| 962 | //
|
| 963 | // 'endTag' is the end tag for this node, it is returned by a call to a child.
|
| 964 | // 'parentEnd' is the end tag for the parent, which is filled in and returned.
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 965 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 966 | while( p && *p ) {
|
| 967 | XMLNode* node = 0;
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 968 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 969 | p = _document->Identify( p, &node );
|
Dmitry-Me | 9fcb876 | 2015-03-05 17:53:34 +0300 | [diff] [blame] | 970 | if ( node == 0 ) {
|
| 971 | break;
|
| 972 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 973 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 974 | StrPair endTag;
|
| 975 | p = node->ParseDeep( p, &endTag );
|
| 976 | if ( !p ) {
|
Dmitry-Me | e3225b1 | 2014-09-03 11:03:11 +0400 | [diff] [blame] | 977 | DeleteNode( node );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 978 | if ( !_document->Error() ) {
|
| 979 | _document->SetError( XML_ERROR_PARSING, 0, 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 980 | }
|
| 981 | break;
|
| 982 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 983 |
|
Sarat Addepalli | 3df007e | 2015-05-20 10:43:51 +0530 | [diff] [blame] | 984 | XMLDeclaration* decl = node->ToDeclaration();
|
Sarat Addepalli | a0f499d | 2015-05-18 09:25:17 +0530 | [diff] [blame] | 985 | if ( decl ) {
|
Sarat Addepalli | 2f0d173 | 2015-05-19 09:02:16 +0530 | [diff] [blame] | 986 | // A declaration can only be the first child of a document.
|
| 987 | // Set error, if document already has children.
|
| 988 | if ( !_document->NoChildren() ) {
|
| 989 | _document->SetError( XML_ERROR_PARSING_DECLARATION, decl->Value(), 0);
|
Dmitry-Me | 4de7abb | 2016-08-10 17:30:02 +0300 | [diff] [blame] | 990 | DeleteNode( node );
|
Sarat Addepalli | 2f0d173 | 2015-05-19 09:02:16 +0530 | [diff] [blame] | 991 | break;
|
| 992 | }
|
Sarat Addepalli | a0f499d | 2015-05-18 09:25:17 +0530 | [diff] [blame] | 993 | }
|
Sarat Addepalli | 2f0d173 | 2015-05-19 09:02:16 +0530 | [diff] [blame] | 994 |
|
Dmitry-Me | b6b4e82 | 2014-08-27 17:17:47 +0400 | [diff] [blame] | 995 | XMLElement* ele = node->ToElement();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 996 | if ( ele ) {
|
JayXon | e4bf6e3 | 2014-12-26 01:00:24 -0500 | [diff] [blame] | 997 | // We read the end tag. Return it to the parent.
|
| 998 | if ( ele->ClosingType() == XMLElement::CLOSING ) {
|
| 999 | if ( parentEnd ) {
|
| 1000 | ele->_value.TransferTo( parentEnd );
|
| 1001 | }
|
| 1002 | node->_memPool->SetTracked(); // created and then immediately deleted.
|
| 1003 | DeleteNode( node );
|
| 1004 | return p;
|
| 1005 | }
|
| 1006 |
|
| 1007 | // Handle an end tag returned to this level.
|
| 1008 | // And handle a bunch of annoying errors.
|
Dmitry-Me | 9fb2b0f | 2014-09-23 17:27:39 +0400 | [diff] [blame] | 1009 | bool mismatch = false;
|
Dmitry-Me | 3ae4f3e | 2015-01-09 15:44:16 +0300 | [diff] [blame] | 1010 | if ( endTag.Empty() ) {
|
| 1011 | if ( ele->ClosingType() == XMLElement::OPEN ) {
|
| 1012 | mismatch = true;
|
| 1013 | }
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1014 | }
|
Dmitry-Me | 3ae4f3e | 2015-01-09 15:44:16 +0300 | [diff] [blame] | 1015 | else {
|
| 1016 | if ( ele->ClosingType() != XMLElement::OPEN ) {
|
| 1017 | mismatch = true;
|
| 1018 | }
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 1019 | else if ( !XMLUtil::StringEqual( endTag.GetStr(), ele->Name() ) ) {
|
Dmitry-Me | 9fb2b0f | 2014-09-23 17:27:39 +0400 | [diff] [blame] | 1020 | mismatch = true;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1021 | }
|
| 1022 | }
|
Dmitry-Me | 9fb2b0f | 2014-09-23 17:27:39 +0400 | [diff] [blame] | 1023 | if ( mismatch ) {
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 1024 | _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, ele->Name(), 0 );
|
JayXon | dbfdd8f | 2014-12-12 20:07:14 -0500 | [diff] [blame] | 1025 | DeleteNode( node );
|
| 1026 | break;
|
Dmitry-Me | 9fb2b0f | 2014-09-23 17:27:39 +0400 | [diff] [blame] | 1027 | }
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1028 | }
|
JayXon | dbfdd8f | 2014-12-12 20:07:14 -0500 | [diff] [blame] | 1029 | InsertEndChild( node );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1030 | }
|
| 1031 | return 0;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1032 | }
|
| 1033 |
|
Dmitry-Me | e3225b1 | 2014-09-03 11:03:11 +0400 | [diff] [blame] | 1034 | void XMLNode::DeleteNode( XMLNode* node )
|
| 1035 | {
|
| 1036 | if ( node == 0 ) {
|
| 1037 | return;
|
| 1038 | }
|
| 1039 | MemPool* pool = node->_memPool;
|
| 1040 | node->~XMLNode();
|
| 1041 | pool->Free( node );
|
| 1042 | }
|
| 1043 |
|
Lee Thomason | 3cebdc4 | 2015-01-05 17:16:28 -0800 | [diff] [blame] | 1044 | void XMLNode::InsertChildPreamble( XMLNode* insertThis ) const
|
Dmitry-Me | 74e3940 | 2015-01-01 16:26:17 +0300 | [diff] [blame] | 1045 | {
|
| 1046 | TIXMLASSERT( insertThis );
|
| 1047 | TIXMLASSERT( insertThis->_document == _document );
|
| 1048 |
|
| 1049 | if ( insertThis->_parent )
|
| 1050 | insertThis->_parent->Unlink( insertThis );
|
| 1051 | else
|
| 1052 | insertThis->_memPool->SetTracked();
|
| 1053 | }
|
| 1054 |
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 1055 | // --------- XMLText ---------- //
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 1056 | char* XMLText::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 1057 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1058 | const char* start = p;
|
| 1059 | if ( this->CData() ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1060 | p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1061 | if ( !p ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1062 | _document->SetError( XML_ERROR_PARSING_CDATA, start, 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1063 | }
|
| 1064 | return p;
|
| 1065 | }
|
| 1066 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1067 | int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES;
|
| 1068 | if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) {
|
Dmitry-Me | 5420e54 | 2015-05-20 10:51:26 +0300 | [diff] [blame] | 1069 | flags |= StrPair::NEEDS_WHITESPACE_COLLAPSING;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1070 | }
|
Lee Thomason (grinliz) | bc1bfb7 | 2012-08-20 22:00:38 -0700 | [diff] [blame] | 1071 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1072 | p = _value.ParseText( p, "<", flags );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1073 | if ( p && *p ) {
|
| 1074 | return p-1;
|
Dmitry-Me | 257e11b | 2015-01-09 15:50:47 +0300 | [diff] [blame] | 1075 | }
|
| 1076 | if ( !p ) {
|
Dmitry-Me | 7a7e5dc | 2015-01-01 17:58:35 +0300 | [diff] [blame] | 1077 | _document->SetError( XML_ERROR_PARSING_TEXT, start, 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1078 | }
|
| 1079 | }
|
| 1080 | return 0;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 1081 | }
|
| 1082 |
|
| 1083 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1084 | XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
|
| 1085 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1086 | if ( !doc ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1087 | doc = _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1088 | }
|
| 1089 | XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
|
| 1090 | text->SetCData( this->CData() );
|
| 1091 | return text;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1092 | }
|
| 1093 |
|
| 1094 |
|
| 1095 | bool XMLText::ShallowEqual( const XMLNode* compare ) const
|
| 1096 | {
|
Dmitry-Me | 6d202ff | 2014-09-26 14:21:00 +0400 | [diff] [blame] | 1097 | const XMLText* text = compare->ToText();
|
| 1098 | return ( text && XMLUtil::StringEqual( text->Value(), Value() ) );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1099 | }
|
| 1100 |
|
| 1101 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1102 | bool XMLText::Accept( XMLVisitor* visitor ) const
|
| 1103 | {
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 1104 | TIXMLASSERT( visitor );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1105 | return visitor->Visit( *this );
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1106 | }
|
| 1107 |
|
| 1108 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1109 | // --------- XMLComment ---------- //
|
| 1110 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 1111 | XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1112 | {
|
| 1113 | }
|
| 1114 |
|
| 1115 |
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 1116 | XMLComment::~XMLComment()
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1117 | {
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1118 | }
|
| 1119 |
|
| 1120 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 1121 | char* XMLComment::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1122 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1123 | // Comment parses as text.
|
| 1124 | const char* start = p;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1125 | p = _value.ParseText( p, "-->", StrPair::COMMENT );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1126 | if ( p == 0 ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1127 | _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1128 | }
|
| 1129 | return p;
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 1130 | }
|
| 1131 |
|
| 1132 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1133 | XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
|
| 1134 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1135 | if ( !doc ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1136 | doc = _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1137 | }
|
| 1138 | XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
|
| 1139 | return comment;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1140 | }
|
| 1141 |
|
| 1142 |
|
| 1143 | bool XMLComment::ShallowEqual( const XMLNode* compare ) const
|
| 1144 | {
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 1145 | TIXMLASSERT( compare );
|
Dmitry-Me | b6b4e82 | 2014-08-27 17:17:47 +0400 | [diff] [blame] | 1146 | const XMLComment* comment = compare->ToComment();
|
| 1147 | return ( comment && XMLUtil::StringEqual( comment->Value(), Value() ));
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1148 | }
|
| 1149 |
|
| 1150 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1151 | bool XMLComment::Accept( XMLVisitor* visitor ) const
|
| 1152 | {
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 1153 | TIXMLASSERT( visitor );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1154 | return visitor->Visit( *this );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1155 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1156 |
|
| 1157 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1158 | // --------- XMLDeclaration ---------- //
|
| 1159 |
|
| 1160 | XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
|
| 1161 | {
|
| 1162 | }
|
| 1163 |
|
| 1164 |
|
| 1165 | XMLDeclaration::~XMLDeclaration()
|
| 1166 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1167 | //printf( "~XMLDeclaration\n" );
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1168 | }
|
| 1169 |
|
| 1170 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 1171 | char* XMLDeclaration::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1172 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1173 | // Declaration parses as text.
|
| 1174 | const char* start = p;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1175 | p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1176 | if ( p == 0 ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1177 | _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1178 | }
|
| 1179 | return p;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1180 | }
|
| 1181 |
|
| 1182 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1183 | XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
|
| 1184 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1185 | if ( !doc ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1186 | doc = _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1187 | }
|
| 1188 | XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
|
| 1189 | return dec;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1190 | }
|
| 1191 |
|
| 1192 |
|
| 1193 | bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
|
| 1194 | {
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 1195 | TIXMLASSERT( compare );
|
Dmitry-Me | b6b4e82 | 2014-08-27 17:17:47 +0400 | [diff] [blame] | 1196 | const XMLDeclaration* declaration = compare->ToDeclaration();
|
| 1197 | return ( declaration && XMLUtil::StringEqual( declaration->Value(), Value() ));
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1198 | }
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1202 | bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
|
| 1203 | {
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 1204 | TIXMLASSERT( visitor );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1205 | return visitor->Visit( *this );
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1206 | }
|
| 1207 |
|
| 1208 | // --------- XMLUnknown ---------- //
|
| 1209 |
|
| 1210 | XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
|
| 1211 | {
|
| 1212 | }
|
| 1213 |
|
| 1214 |
|
| 1215 | XMLUnknown::~XMLUnknown()
|
| 1216 | {
|
| 1217 | }
|
| 1218 |
|
| 1219 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 1220 | char* XMLUnknown::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1221 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1222 | // Unknown parses as text.
|
| 1223 | const char* start = p;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1224 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1225 | p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1226 | if ( !p ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1227 | _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1228 | }
|
| 1229 | return p;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1230 | }
|
| 1231 |
|
| 1232 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1233 | XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
|
| 1234 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1235 | if ( !doc ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1236 | doc = _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1237 | }
|
| 1238 | XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
|
| 1239 | return text;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1240 | }
|
| 1241 |
|
| 1242 |
|
| 1243 | bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
|
| 1244 | {
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 1245 | TIXMLASSERT( compare );
|
Dmitry-Me | b6b4e82 | 2014-08-27 17:17:47 +0400 | [diff] [blame] | 1246 | const XMLUnknown* unknown = compare->ToUnknown();
|
| 1247 | return ( unknown && XMLUtil::StringEqual( unknown->Value(), Value() ));
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1248 | }
|
| 1249 |
|
| 1250 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1251 | bool XMLUnknown::Accept( XMLVisitor* visitor ) const
|
| 1252 | {
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 1253 | TIXMLASSERT( visitor );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1254 | return visitor->Visit( *this );
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1255 | }
|
| 1256 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1257 | // --------- XMLAttribute ---------- //
|
Michael Daumling | 2162688 | 2013-10-22 17:03:37 +0200 | [diff] [blame] | 1258 |
|
| 1259 | const char* XMLAttribute::Name() const
|
| 1260 | {
|
| 1261 | return _name.GetStr();
|
| 1262 | }
|
| 1263 |
|
| 1264 | const char* XMLAttribute::Value() const
|
| 1265 | {
|
| 1266 | return _value.GetStr();
|
| 1267 | }
|
| 1268 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1269 | char* XMLAttribute::ParseDeep( char* p, bool processEntities )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1270 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1271 | // Parse using the name rules: bug fix, was using ParseText before
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1272 | p = _name.ParseName( p );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1273 | if ( !p || !*p ) {
|
| 1274 | return 0;
|
| 1275 | }
|
Lee Thomason | 22aead1 | 2012-01-23 13:29:35 -0800 | [diff] [blame] | 1276 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1277 | // Skip white space before =
|
| 1278 | p = XMLUtil::SkipWhiteSpace( p );
|
Dmitry-Me | bb836dc | 2014-12-24 11:54:05 +0300 | [diff] [blame] | 1279 | if ( *p != '=' ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1280 | return 0;
|
| 1281 | }
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame] | 1282 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1283 | ++p; // move up to opening quote
|
| 1284 | p = XMLUtil::SkipWhiteSpace( p );
|
| 1285 | if ( *p != '\"' && *p != '\'' ) {
|
| 1286 | return 0;
|
| 1287 | }
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame] | 1288 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1289 | char endTag[2] = { *p, 0 };
|
| 1290 | ++p; // move past opening quote
|
Lee Thomason | 78a773d | 2012-07-02 10:10:19 -0700 | [diff] [blame] | 1291 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1292 | p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1293 | return p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1294 | }
|
| 1295 |
|
| 1296 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1297 | void XMLAttribute::SetName( const char* n )
|
| 1298 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1299 | _name.SetStr( n );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1300 | }
|
| 1301 |
|
| 1302 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1303 | XMLError XMLAttribute::QueryIntValue( int* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1304 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1305 | if ( XMLUtil::ToInt( Value(), value )) {
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 1306 | return XML_SUCCESS;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1307 | }
|
| 1308 | return XML_WRONG_ATTRIBUTE_TYPE;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1309 | }
|
| 1310 |
|
| 1311 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1312 | XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1313 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1314 | if ( XMLUtil::ToUnsigned( Value(), value )) {
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 1315 | return XML_SUCCESS;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1316 | }
|
| 1317 | return XML_WRONG_ATTRIBUTE_TYPE;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1318 | }
|
| 1319 |
|
| 1320 |
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1321 | XMLError XMLAttribute::QueryInt64Value(int64_t* value) const
|
| 1322 | {
|
| 1323 | if (XMLUtil::ToInt64(Value(), value)) {
|
| 1324 | return XML_SUCCESS;
|
| 1325 | }
|
| 1326 | return XML_WRONG_ATTRIBUTE_TYPE;
|
| 1327 | }
|
| 1328 |
|
| 1329 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1330 | XMLError XMLAttribute::QueryBoolValue( bool* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1331 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1332 | if ( XMLUtil::ToBool( Value(), value )) {
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 1333 | return XML_SUCCESS;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1334 | }
|
| 1335 | return XML_WRONG_ATTRIBUTE_TYPE;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1336 | }
|
| 1337 |
|
| 1338 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1339 | XMLError XMLAttribute::QueryFloatValue( float* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1340 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1341 | if ( XMLUtil::ToFloat( Value(), value )) {
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 1342 | return XML_SUCCESS;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1343 | }
|
| 1344 | return XML_WRONG_ATTRIBUTE_TYPE;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1345 | }
|
| 1346 |
|
| 1347 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1348 | XMLError XMLAttribute::QueryDoubleValue( double* value ) const
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1349 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1350 | if ( XMLUtil::ToDouble( Value(), value )) {
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 1351 | return XML_SUCCESS;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1352 | }
|
| 1353 | return XML_WRONG_ATTRIBUTE_TYPE;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1354 | }
|
| 1355 |
|
| 1356 |
|
| 1357 | void XMLAttribute::SetAttribute( const char* v )
|
| 1358 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1359 | _value.SetStr( v );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1360 | }
|
| 1361 |
|
| 1362 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1363 | void XMLAttribute::SetAttribute( int v )
|
| 1364 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1365 | char buf[BUF_SIZE];
|
| 1366 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1367 | _value.SetStr( buf );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1368 | }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1369 |
|
| 1370 |
|
| 1371 | void XMLAttribute::SetAttribute( unsigned v )
|
| 1372 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1373 | char buf[BUF_SIZE];
|
| 1374 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1375 | _value.SetStr( buf );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1376 | }
|
| 1377 |
|
| 1378 |
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1379 | void XMLAttribute::SetAttribute(int64_t v)
|
| 1380 | {
|
| 1381 | char buf[BUF_SIZE];
|
| 1382 | XMLUtil::ToStr(v, buf, BUF_SIZE);
|
| 1383 | _value.SetStr(buf);
|
| 1384 | }
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1388 | void XMLAttribute::SetAttribute( bool v )
|
| 1389 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1390 | char buf[BUF_SIZE];
|
| 1391 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1392 | _value.SetStr( buf );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1393 | }
|
| 1394 |
|
| 1395 | void XMLAttribute::SetAttribute( double v )
|
| 1396 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1397 | char buf[BUF_SIZE];
|
| 1398 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1399 | _value.SetStr( buf );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1400 | }
|
| 1401 |
|
| 1402 | void XMLAttribute::SetAttribute( float v )
|
| 1403 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1404 | char buf[BUF_SIZE];
|
| 1405 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1406 | _value.SetStr( buf );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1407 | }
|
| 1408 |
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 1409 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1410 | // --------- XMLElement ---------- //
|
| 1411 | XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1412 | _closingType( 0 ),
|
| 1413 | _rootAttribute( 0 )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1414 | {
|
| 1415 | }
|
| 1416 |
|
| 1417 |
|
| 1418 | XMLElement::~XMLElement()
|
| 1419 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1420 | while( _rootAttribute ) {
|
| 1421 | XMLAttribute* next = _rootAttribute->_next;
|
Dmitry-Me | e3225b1 | 2014-09-03 11:03:11 +0400 | [diff] [blame] | 1422 | DeleteAttribute( _rootAttribute );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1423 | _rootAttribute = next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1424 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1425 | }
|
| 1426 |
|
| 1427 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1428 | const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
|
| 1429 | {
|
Dmitry-Me | 3659fe1 | 2014-09-04 11:33:49 +0400 | [diff] [blame] | 1430 | for( XMLAttribute* a = _rootAttribute; a; a = a->_next ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1431 | if ( XMLUtil::StringEqual( a->Name(), name ) ) {
|
| 1432 | return a;
|
| 1433 | }
|
| 1434 | }
|
| 1435 | return 0;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1436 | }
|
| 1437 |
|
| 1438 |
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 1439 | const char* XMLElement::Attribute( const char* name, const char* value ) const
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 1440 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1441 | const XMLAttribute* a = FindAttribute( name );
|
| 1442 | if ( !a ) {
|
| 1443 | return 0;
|
| 1444 | }
|
| 1445 | if ( !value || XMLUtil::StringEqual( a->Value(), value )) {
|
| 1446 | return a->Value();
|
| 1447 | }
|
| 1448 | return 0;
|
Lee Thomason | 8ba7f7d | 2012-03-24 13:04:04 -0700 | [diff] [blame] | 1449 | }
|
| 1450 |
|
| 1451 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1452 | const char* XMLElement::GetText() const
|
| 1453 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1454 | if ( FirstChild() && FirstChild()->ToText() ) {
|
Dmitry-Me | 097339a | 2014-09-29 13:20:29 +0400 | [diff] [blame] | 1455 | return FirstChild()->Value();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1456 | }
|
| 1457 | return 0;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1458 | }
|
| 1459 |
|
| 1460 |
|
Uli Kusterer | 8fe342a | 2014-01-21 01:12:47 +0100 | [diff] [blame] | 1461 | void XMLElement::SetText( const char* inText )
|
| 1462 | {
|
Uli Kusterer | 869bb59 | 2014-01-21 01:36:16 +0100 | [diff] [blame] | 1463 | if ( FirstChild() && FirstChild()->ToText() )
|
Uli Kusterer | 8fe342a | 2014-01-21 01:12:47 +0100 | [diff] [blame] | 1464 | FirstChild()->SetValue( inText );
|
| 1465 | else {
|
| 1466 | XMLText* theText = GetDocument()->NewText( inText );
|
| 1467 | InsertFirstChild( theText );
|
| 1468 | }
|
| 1469 | }
|
| 1470 |
|
Lee Thomason | 5bb2d80 | 2014-01-24 10:42:57 -0800 | [diff] [blame] | 1471 |
|
| 1472 | void XMLElement::SetText( int v )
|
| 1473 | {
|
| 1474 | char buf[BUF_SIZE];
|
| 1475 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 1476 | SetText( buf );
|
| 1477 | }
|
| 1478 |
|
| 1479 |
|
| 1480 | void XMLElement::SetText( unsigned v )
|
| 1481 | {
|
| 1482 | char buf[BUF_SIZE];
|
| 1483 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 1484 | SetText( buf );
|
| 1485 | }
|
| 1486 |
|
| 1487 |
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1488 | void XMLElement::SetText(int64_t v)
|
| 1489 | {
|
| 1490 | char buf[BUF_SIZE];
|
| 1491 | XMLUtil::ToStr(v, buf, BUF_SIZE);
|
| 1492 | SetText(buf);
|
| 1493 | }
|
| 1494 |
|
| 1495 |
|
| 1496 | void XMLElement::SetText( bool v )
|
Lee Thomason | 5bb2d80 | 2014-01-24 10:42:57 -0800 | [diff] [blame] | 1497 | {
|
| 1498 | char buf[BUF_SIZE];
|
| 1499 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 1500 | SetText( buf );
|
| 1501 | }
|
| 1502 |
|
| 1503 |
|
| 1504 | void XMLElement::SetText( float v )
|
| 1505 | {
|
| 1506 | char buf[BUF_SIZE];
|
| 1507 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 1508 | SetText( buf );
|
| 1509 | }
|
| 1510 |
|
| 1511 |
|
| 1512 | void XMLElement::SetText( double v )
|
| 1513 | {
|
| 1514 | char buf[BUF_SIZE];
|
| 1515 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 1516 | SetText( buf );
|
| 1517 | }
|
| 1518 |
|
| 1519 |
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1520 | XMLError XMLElement::QueryIntText( int* ival ) const
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1521 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1522 | if ( FirstChild() && FirstChild()->ToText() ) {
|
Dmitry-Me | 097339a | 2014-09-29 13:20:29 +0400 | [diff] [blame] | 1523 | const char* t = FirstChild()->Value();
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1524 | if ( XMLUtil::ToInt( t, ival ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1525 | return XML_SUCCESS;
|
| 1526 | }
|
| 1527 | return XML_CAN_NOT_CONVERT_TEXT;
|
| 1528 | }
|
| 1529 | return XML_NO_TEXT_NODE;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1530 | }
|
| 1531 |
|
| 1532 |
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1533 | XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1534 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1535 | if ( FirstChild() && FirstChild()->ToText() ) {
|
Dmitry-Me | 097339a | 2014-09-29 13:20:29 +0400 | [diff] [blame] | 1536 | const char* t = FirstChild()->Value();
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1537 | if ( XMLUtil::ToUnsigned( t, uval ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1538 | return XML_SUCCESS;
|
| 1539 | }
|
| 1540 | return XML_CAN_NOT_CONVERT_TEXT;
|
| 1541 | }
|
| 1542 | return XML_NO_TEXT_NODE;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1543 | }
|
| 1544 |
|
| 1545 |
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 1546 | XMLError XMLElement::QueryInt64Text(int64_t* ival) const
|
| 1547 | {
|
| 1548 | if (FirstChild() && FirstChild()->ToText()) {
|
| 1549 | const char* t = FirstChild()->Value();
|
| 1550 | if (XMLUtil::ToInt64(t, ival)) {
|
| 1551 | return XML_SUCCESS;
|
| 1552 | }
|
| 1553 | return XML_CAN_NOT_CONVERT_TEXT;
|
| 1554 | }
|
| 1555 | return XML_NO_TEXT_NODE;
|
| 1556 | }
|
| 1557 |
|
| 1558 |
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1559 | XMLError XMLElement::QueryBoolText( bool* bval ) const
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1560 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1561 | if ( FirstChild() && FirstChild()->ToText() ) {
|
Dmitry-Me | 097339a | 2014-09-29 13:20:29 +0400 | [diff] [blame] | 1562 | const char* t = FirstChild()->Value();
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1563 | if ( XMLUtil::ToBool( t, bval ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1564 | return XML_SUCCESS;
|
| 1565 | }
|
| 1566 | return XML_CAN_NOT_CONVERT_TEXT;
|
| 1567 | }
|
| 1568 | return XML_NO_TEXT_NODE;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1569 | }
|
| 1570 |
|
| 1571 |
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1572 | XMLError XMLElement::QueryDoubleText( double* dval ) const
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1573 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1574 | if ( FirstChild() && FirstChild()->ToText() ) {
|
Dmitry-Me | 097339a | 2014-09-29 13:20:29 +0400 | [diff] [blame] | 1575 | const char* t = FirstChild()->Value();
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1576 | if ( XMLUtil::ToDouble( t, dval ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1577 | return XML_SUCCESS;
|
| 1578 | }
|
| 1579 | return XML_CAN_NOT_CONVERT_TEXT;
|
| 1580 | }
|
| 1581 | return XML_NO_TEXT_NODE;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1582 | }
|
| 1583 |
|
| 1584 |
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1585 | XMLError XMLElement::QueryFloatText( float* fval ) const
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1586 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1587 | if ( FirstChild() && FirstChild()->ToText() ) {
|
Dmitry-Me | 097339a | 2014-09-29 13:20:29 +0400 | [diff] [blame] | 1588 | const char* t = FirstChild()->Value();
|
MortenMacFly | 4ee49f1 | 2013-01-14 20:03:14 +0100 | [diff] [blame] | 1589 | if ( XMLUtil::ToFloat( t, fval ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1590 | return XML_SUCCESS;
|
| 1591 | }
|
| 1592 | return XML_CAN_NOT_CONVERT_TEXT;
|
| 1593 | }
|
| 1594 | return XML_NO_TEXT_NODE;
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 1595 | }
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1599 | XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
|
| 1600 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1601 | XMLAttribute* last = 0;
|
| 1602 | XMLAttribute* attrib = 0;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1603 | for( attrib = _rootAttribute;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1604 | attrib;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1605 | last = attrib, attrib = attrib->_next ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1606 | if ( XMLUtil::StringEqual( attrib->Name(), name ) ) {
|
| 1607 | break;
|
| 1608 | }
|
| 1609 | }
|
| 1610 | if ( !attrib ) {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 1611 | TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1612 | attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
|
| 1613 | attrib->_memPool = &_document->_attributePool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1614 | if ( last ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1615 | last->_next = attrib;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1616 | }
|
| 1617 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1618 | _rootAttribute = attrib;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1619 | }
|
| 1620 | attrib->SetName( name );
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 1621 | attrib->_memPool->SetTracked(); // always created and linked.
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1622 | }
|
| 1623 | return attrib;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1624 | }
|
| 1625 |
|
| 1626 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1627 | void XMLElement::DeleteAttribute( const char* name )
|
| 1628 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1629 | XMLAttribute* prev = 0;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1630 | for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1631 | if ( XMLUtil::StringEqual( name, a->Name() ) ) {
|
| 1632 | if ( prev ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1633 | prev->_next = a->_next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1634 | }
|
| 1635 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1636 | _rootAttribute = a->_next;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1637 | }
|
Dmitry-Me | e3225b1 | 2014-09-03 11:03:11 +0400 | [diff] [blame] | 1638 | DeleteAttribute( a );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1639 | break;
|
| 1640 | }
|
| 1641 | prev = a;
|
| 1642 | }
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1643 | }
|
| 1644 |
|
| 1645 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1646 | char* XMLElement::ParseAttributes( char* p )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1647 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1648 | const char* start = p;
|
| 1649 | XMLAttribute* prevAttribute = 0;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1650 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1651 | // Read the attributes.
|
| 1652 | while( p ) {
|
| 1653 | p = XMLUtil::SkipWhiteSpace( p );
|
Dmitry-Me | bb836dc | 2014-12-24 11:54:05 +0300 | [diff] [blame] | 1654 | if ( !(*p) ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1655 | _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1656 | return 0;
|
| 1657 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1658 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1659 | // attribute.
|
Martinsh Shaiters | c6d02f4 | 2013-01-26 21:22:57 +0200 | [diff] [blame] | 1660 | if (XMLUtil::IsNameStartChar( *p ) ) {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 1661 | TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1662 | XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
|
| 1663 | attrib->_memPool = &_document->_attributePool;
|
Lee Thomason | 5b0a677 | 2012-11-19 13:54:42 -0800 | [diff] [blame] | 1664 | attrib->_memPool->SetTracked();
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1665 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1666 | p = attrib->ParseDeep( p, _document->ProcessEntities() );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1667 | if ( !p || Attribute( attrib->Name() ) ) {
|
Dmitry-Me | e3225b1 | 2014-09-03 11:03:11 +0400 | [diff] [blame] | 1668 | DeleteAttribute( attrib );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1669 | _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1670 | return 0;
|
| 1671 | }
|
| 1672 | // There is a minor bug here: if the attribute in the source xml
|
| 1673 | // document is duplicated, it will not be detected and the
|
| 1674 | // attribute will be doubly added. However, tracking the 'prevAttribute'
|
| 1675 | // avoids re-scanning the attribute list. Preferring performance for
|
| 1676 | // now, may reconsider in the future.
|
| 1677 | if ( prevAttribute ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1678 | prevAttribute->_next = attrib;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1679 | }
|
| 1680 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1681 | _rootAttribute = attrib;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1682 | }
|
| 1683 | prevAttribute = attrib;
|
| 1684 | }
|
| 1685 | // end of the tag
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1686 | else if ( *p == '>' ) {
|
| 1687 | ++p;
|
| 1688 | break;
|
| 1689 | }
|
Dmitry-Me | ccd267a | 2015-04-10 15:42:54 +0300 | [diff] [blame] | 1690 | // end of the tag
|
| 1691 | else if ( *p == '/' && *(p+1) == '>' ) {
|
| 1692 | _closingType = CLOSED;
|
| 1693 | return p+2; // done; sealed element.
|
| 1694 | }
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1695 | else {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1696 | _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1697 | return 0;
|
| 1698 | }
|
| 1699 | }
|
| 1700 | return p;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1701 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1702 |
|
Dmitry-Me | e3225b1 | 2014-09-03 11:03:11 +0400 | [diff] [blame] | 1703 | void XMLElement::DeleteAttribute( XMLAttribute* attribute )
|
| 1704 | {
|
| 1705 | if ( attribute == 0 ) {
|
| 1706 | return;
|
| 1707 | }
|
| 1708 | MemPool* pool = attribute->_memPool;
|
| 1709 | attribute->~XMLAttribute();
|
| 1710 | pool->Free( attribute );
|
| 1711 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1712 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1713 | //
|
| 1714 | // <ele></ele>
|
| 1715 | // <ele>foo<b>bar</b></ele>
|
| 1716 | //
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 1717 | char* XMLElement::ParseDeep( char* p, StrPair* strPair )
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1718 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1719 | // Read the element name.
|
| 1720 | p = XMLUtil::SkipWhiteSpace( p );
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1721 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1722 | // The closing element is the </element> form. It is
|
| 1723 | // parsed just like a regular element then deleted from
|
| 1724 | // the DOM.
|
| 1725 | if ( *p == '/' ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1726 | _closingType = CLOSING;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1727 | ++p;
|
| 1728 | }
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1729 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1730 | p = _value.ParseName( p );
|
| 1731 | if ( _value.Empty() ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1732 | return 0;
|
| 1733 | }
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1734 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1735 | p = ParseAttributes( p );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1736 | if ( !p || !*p || _closingType ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1737 | return p;
|
| 1738 | }
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1739 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1740 | p = XMLNode::ParseDeep( p, strPair );
|
| 1741 | return p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1742 | }
|
| 1743 |
|
| 1744 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1745 |
|
| 1746 | XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const
|
| 1747 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1748 | if ( !doc ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1749 | doc = _document;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1750 | }
|
| 1751 | XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?
|
| 1752 | for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {
|
| 1753 | element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?
|
| 1754 | }
|
| 1755 | return element;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1756 | }
|
| 1757 |
|
| 1758 |
|
| 1759 | bool XMLElement::ShallowEqual( const XMLNode* compare ) const
|
| 1760 | {
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 1761 | TIXMLASSERT( compare );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1762 | const XMLElement* other = compare->ToElement();
|
Dmitry-Me | 886ad97 | 2015-07-22 11:00:51 +0300 | [diff] [blame] | 1763 | if ( other && XMLUtil::StringEqual( other->Name(), Name() )) {
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1764 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1765 | const XMLAttribute* a=FirstAttribute();
|
| 1766 | const XMLAttribute* b=other->FirstAttribute();
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1767 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1768 | while ( a && b ) {
|
| 1769 | if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {
|
| 1770 | return false;
|
| 1771 | }
|
| 1772 | a = a->Next();
|
| 1773 | b = b->Next();
|
| 1774 | }
|
| 1775 | if ( a || b ) {
|
| 1776 | // different count
|
| 1777 | return false;
|
| 1778 | }
|
| 1779 | return true;
|
| 1780 | }
|
| 1781 | return false;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1782 | }
|
| 1783 |
|
| 1784 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1785 | bool XMLElement::Accept( XMLVisitor* visitor ) const
|
| 1786 | {
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 1787 | TIXMLASSERT( visitor );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1788 | if ( visitor->VisitEnter( *this, _rootAttribute ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1789 | for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
|
| 1790 | if ( !node->Accept( visitor ) ) {
|
| 1791 | break;
|
| 1792 | }
|
| 1793 | }
|
| 1794 | }
|
| 1795 | return visitor->VisitExit( *this );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1796 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1797 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1798 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1799 | // --------- XMLDocument ----------- //
|
Lee Thomason | 331596e | 2014-09-11 14:56:43 -0700 | [diff] [blame] | 1800 |
|
| 1801 | // Warning: List must match 'enum XMLError'
|
| 1802 | const char* XMLDocument::_errorNames[XML_ERROR_COUNT] = {
|
| 1803 | "XML_SUCCESS",
|
| 1804 | "XML_NO_ATTRIBUTE",
|
| 1805 | "XML_WRONG_ATTRIBUTE_TYPE",
|
| 1806 | "XML_ERROR_FILE_NOT_FOUND",
|
| 1807 | "XML_ERROR_FILE_COULD_NOT_BE_OPENED",
|
| 1808 | "XML_ERROR_FILE_READ_ERROR",
|
| 1809 | "XML_ERROR_ELEMENT_MISMATCH",
|
| 1810 | "XML_ERROR_PARSING_ELEMENT",
|
| 1811 | "XML_ERROR_PARSING_ATTRIBUTE",
|
| 1812 | "XML_ERROR_IDENTIFYING_TAG",
|
| 1813 | "XML_ERROR_PARSING_TEXT",
|
| 1814 | "XML_ERROR_PARSING_CDATA",
|
| 1815 | "XML_ERROR_PARSING_COMMENT",
|
| 1816 | "XML_ERROR_PARSING_DECLARATION",
|
| 1817 | "XML_ERROR_PARSING_UNKNOWN",
|
| 1818 | "XML_ERROR_EMPTY_DOCUMENT",
|
| 1819 | "XML_ERROR_MISMATCHED_ELEMENT",
|
| 1820 | "XML_ERROR_PARSING",
|
| 1821 | "XML_CAN_NOT_CONVERT_TEXT",
|
| 1822 | "XML_NO_TEXT_NODE"
|
| 1823 | };
|
| 1824 |
|
| 1825 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1826 | XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) :
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1827 | XMLNode( 0 ),
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1828 | _writeBOM( false ),
|
| 1829 | _processEntities( processEntities ),
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 1830 | _errorID(XML_SUCCESS),
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1831 | _whitespace( whitespace ),
|
| 1832 | _errorStr1( 0 ),
|
| 1833 | _errorStr2( 0 ),
|
| 1834 | _charBuffer( 0 )
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1835 | {
|
Dmitry-Me | 8dd493b | 2015-07-02 13:59:30 +0300 | [diff] [blame] | 1836 | // avoid VC++ C4355 warning about 'this' in initializer list (C4355 is off by default in VS2012+)
|
| 1837 | _document = this;
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1838 | }
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1839 |
|
| 1840 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1841 | XMLDocument::~XMLDocument()
|
| 1842 | {
|
Lee Thomason | f07b952 | 2014-10-30 13:25:12 -0700 | [diff] [blame] | 1843 | Clear();
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1844 | }
|
| 1845 |
|
| 1846 |
|
Martinsh Shaiters | a9d42b0 | 2013-01-30 11:14:27 +0200 | [diff] [blame] | 1847 | void XMLDocument::Clear()
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1848 | {
|
Martinsh Shaiters | a9d42b0 | 2013-01-30 11:14:27 +0200 | [diff] [blame] | 1849 | DeleteChildren();
|
| 1850 |
|
Dmitry-Me | ab37df8 | 2014-11-28 12:08:36 +0300 | [diff] [blame] | 1851 | #ifdef DEBUG
|
| 1852 | const bool hadError = Error();
|
| 1853 | #endif
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 1854 | _errorID = XML_SUCCESS;
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1855 | _errorStr1 = 0;
|
| 1856 | _errorStr2 = 0;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1857 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1858 | delete [] _charBuffer;
|
| 1859 | _charBuffer = 0;
|
Lee Thomason | f07b952 | 2014-10-30 13:25:12 -0700 | [diff] [blame] | 1860 |
|
| 1861 | #if 0
|
| 1862 | _textPool.Trace( "text" );
|
| 1863 | _elementPool.Trace( "element" );
|
| 1864 | _commentPool.Trace( "comment" );
|
| 1865 | _attributePool.Trace( "attribute" );
|
| 1866 | #endif
|
| 1867 |
|
| 1868 | #ifdef DEBUG
|
Dmitry-Me | ab37df8 | 2014-11-28 12:08:36 +0300 | [diff] [blame] | 1869 | if ( !hadError ) {
|
Lee Thomason | f07b952 | 2014-10-30 13:25:12 -0700 | [diff] [blame] | 1870 | TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() );
|
| 1871 | TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() );
|
| 1872 | TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() );
|
| 1873 | TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() );
|
| 1874 | }
|
| 1875 | #endif
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1876 | }
|
| 1877 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1878 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1879 | XMLElement* XMLDocument::NewElement( const char* name )
|
| 1880 | {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 1881 | TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1882 | XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this );
|
| 1883 | ele->_memPool = &_elementPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1884 | ele->SetName( name );
|
| 1885 | return ele;
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1886 | }
|
| 1887 |
|
| 1888 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1889 | XMLComment* XMLDocument::NewComment( const char* str )
|
| 1890 | {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 1891 | TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1892 | XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this );
|
| 1893 | comment->_memPool = &_commentPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1894 | comment->SetValue( str );
|
| 1895 | return comment;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1896 | }
|
| 1897 |
|
| 1898 |
|
| 1899 | XMLText* XMLDocument::NewText( const char* str )
|
| 1900 | {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 1901 | TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1902 | XMLText* text = new (_textPool.Alloc()) XMLText( this );
|
| 1903 | text->_memPool = &_textPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1904 | text->SetValue( str );
|
| 1905 | return text;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1906 | }
|
| 1907 |
|
| 1908 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1909 | XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
|
| 1910 | {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 1911 | TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1912 | XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this );
|
| 1913 | dec->_memPool = &_commentPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1914 | dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );
|
| 1915 | return dec;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1916 | }
|
| 1917 |
|
| 1918 |
|
| 1919 | XMLUnknown* XMLDocument::NewUnknown( const char* str )
|
| 1920 | {
|
Dmitry-Me | e6a95ce | 2014-12-10 09:10:27 +0300 | [diff] [blame] | 1921 | TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1922 | XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this );
|
| 1923 | unk->_memPool = &_commentPool;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1924 | unk->SetValue( str );
|
| 1925 | return unk;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1926 | }
|
| 1927 |
|
Dmitry-Me | 01578db | 2014-08-19 10:18:48 +0400 | [diff] [blame] | 1928 | static FILE* callfopen( const char* filepath, const char* mode )
|
| 1929 | {
|
Dmitry-Me | abb2d04 | 2014-12-09 12:59:31 +0300 | [diff] [blame] | 1930 | TIXMLASSERT( filepath );
|
| 1931 | TIXMLASSERT( mode );
|
Dmitry-Me | 01578db | 2014-08-19 10:18:48 +0400 | [diff] [blame] | 1932 | #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
|
| 1933 | FILE* fp = 0;
|
| 1934 | errno_t err = fopen_s( &fp, filepath, mode );
|
| 1935 | if ( err ) {
|
| 1936 | return 0;
|
| 1937 | }
|
| 1938 | #else
|
| 1939 | FILE* fp = fopen( filepath, mode );
|
| 1940 | #endif
|
| 1941 | return fp;
|
| 1942 | }
|
Lee Thomason | cd011bc | 2014-12-17 10:41:34 -0800 | [diff] [blame] | 1943 |
|
| 1944 | void XMLDocument::DeleteNode( XMLNode* node ) {
|
| 1945 | TIXMLASSERT( node );
|
| 1946 | TIXMLASSERT(node->_document == this );
|
| 1947 | if (node->_parent) {
|
| 1948 | node->_parent->DeleteChild( node );
|
| 1949 | }
|
| 1950 | else {
|
| 1951 | // Isn't in the tree.
|
| 1952 | // Use the parent delete.
|
| 1953 | // Also, we need to mark it tracked: we 'know'
|
| 1954 | // it was never used.
|
| 1955 | node->_memPool->SetTracked();
|
| 1956 | // Call the static XMLNode version:
|
| 1957 | XMLNode::DeleteNode(node);
|
| 1958 | }
|
| 1959 | }
|
| 1960 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1961 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1962 | XMLError XMLDocument::LoadFile( const char* filename )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1963 | {
|
Martinsh Shaiters | a9d42b0 | 2013-01-30 11:14:27 +0200 | [diff] [blame] | 1964 | Clear();
|
Dmitry-Me | 01578db | 2014-08-19 10:18:48 +0400 | [diff] [blame] | 1965 | FILE* fp = callfopen( filename, "rb" );
|
| 1966 | if ( !fp ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1967 | SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1968 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 1969 | }
|
| 1970 | LoadFile( fp );
|
| 1971 | fclose( fp );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 1972 | return _errorID;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1973 | }
|
| 1974 |
|
Dmitry-Me | 901fed5 | 2015-09-25 10:29:51 +0300 | [diff] [blame] | 1975 | // This is likely overengineered template art to have a check that unsigned long value incremented
|
| 1976 | // by one still fits into size_t. If size_t type is larger than unsigned long type
|
| 1977 | // (x86_64-w64-mingw32 target) then the check is redundant and gcc and clang emit
|
| 1978 | // -Wtype-limits warning. This piece makes the compiler select code with a check when a check
|
| 1979 | // is useful and code with no check when a check is redundant depending on how size_t and unsigned long
|
| 1980 | // types sizes relate to each other.
|
| 1981 | template
|
| 1982 | <bool = (sizeof(unsigned long) >= sizeof(size_t))>
|
| 1983 | struct LongFitsIntoSizeTMinusOne {
|
| 1984 | static bool Fits( unsigned long value )
|
| 1985 | {
|
| 1986 | return value < (size_t)-1;
|
| 1987 | }
|
| 1988 | };
|
| 1989 |
|
| 1990 | template <>
|
Manlio Morini | 0f45b24 | 2016-07-11 12:14:59 +0200 | [diff] [blame] | 1991 | struct LongFitsIntoSizeTMinusOne<false> {
|
| 1992 | static bool Fits( unsigned long )
|
| 1993 | {
|
| 1994 | return true;
|
| 1995 | }
|
| 1996 | };
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1997 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 1998 | XMLError XMLDocument::LoadFile( FILE* fp )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1999 | {
|
Martinsh Shaiters | a9d42b0 | 2013-01-30 11:14:27 +0200 | [diff] [blame] | 2000 | Clear();
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2001 |
|
Daniel Marjamäki | ba4b328 | 2014-01-10 21:37:27 +0100 | [diff] [blame] | 2002 | fseek( fp, 0, SEEK_SET );
|
Dmitry-Me | 08e7f7b | 2014-07-31 15:19:14 +0400 | [diff] [blame] | 2003 | if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {
|
Daniel Marjamäki | ba4b328 | 2014-01-10 21:37:27 +0100 | [diff] [blame] | 2004 | SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
|
| 2005 | return _errorID;
|
| 2006 | }
|
| 2007 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2008 | fseek( fp, 0, SEEK_END );
|
Dmitry-Me | acb9c9c | 2014-08-04 09:49:25 +0400 | [diff] [blame] | 2009 | const long filelength = ftell( fp );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2010 | fseek( fp, 0, SEEK_SET );
|
Dmitry-Me | acb9c9c | 2014-08-04 09:49:25 +0400 | [diff] [blame] | 2011 | if ( filelength == -1L ) {
|
| 2012 | SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
|
| 2013 | return _errorID;
|
| 2014 | }
|
Dmitry-Me | 96b110d | 2016-02-09 15:12:40 +0300 | [diff] [blame] | 2015 | TIXMLASSERT( filelength >= 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2016 |
|
Dmitry-Me | 901fed5 | 2015-09-25 10:29:51 +0300 | [diff] [blame] | 2017 | if ( !LongFitsIntoSizeTMinusOne<>::Fits( filelength ) ) {
|
Dmitry-Me | 2a8b1f5 | 2015-04-30 14:58:57 +0300 | [diff] [blame] | 2018 | // Cannot handle files which won't fit in buffer together with null terminator
|
| 2019 | SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
|
| 2020 | return _errorID;
|
| 2021 | }
|
| 2022 |
|
Dmitry-Me | 72801b8 | 2015-05-07 09:41:39 +0300 | [diff] [blame] | 2023 | if ( filelength == 0 ) {
|
Vasily Biryukov | 1cfafd0 | 2013-04-20 14:12:33 +0600 | [diff] [blame] | 2024 | SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2025 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2026 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 2027 |
|
Dmitry-Me | 72801b8 | 2015-05-07 09:41:39 +0300 | [diff] [blame] | 2028 | const size_t size = filelength;
|
Dmitry-Me | 96f38cc | 2015-08-10 16:45:12 +0300 | [diff] [blame] | 2029 | TIXMLASSERT( _charBuffer == 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2030 | _charBuffer = new char[size+1];
|
| 2031 | size_t read = fread( _charBuffer, 1, size, fp );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2032 | if ( read != size ) {
|
| 2033 | SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2034 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2035 | }
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 2036 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2037 | _charBuffer[size] = 0;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2038 |
|
Dmitry-Me | 97476b7 | 2015-01-01 16:15:57 +0300 | [diff] [blame] | 2039 | Parse();
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2040 | return _errorID;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2041 | }
|
| 2042 |
|
| 2043 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 2044 | XMLError XMLDocument::SaveFile( const char* filename, bool compact )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2045 | {
|
Dmitry-Me | 01578db | 2014-08-19 10:18:48 +0400 | [diff] [blame] | 2046 | FILE* fp = callfopen( filename, "w" );
|
| 2047 | if ( !fp ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2048 | SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2049 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2050 | }
|
| 2051 | SaveFile(fp, compact);
|
| 2052 | fclose( fp );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2053 | return _errorID;
|
Ken Miller | 81da1fb | 2012-04-09 23:32:26 -0500 | [diff] [blame] | 2054 | }
|
| 2055 |
|
| 2056 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 2057 | XMLError XMLDocument::SaveFile( FILE* fp, bool compact )
|
Ken Miller | 81da1fb | 2012-04-09 23:32:26 -0500 | [diff] [blame] | 2058 | {
|
Ant Mitchell | 189198f | 2015-03-24 16:20:36 +0000 | [diff] [blame] | 2059 | // Clear any error from the last save, otherwise it will get reported
|
| 2060 | // for *this* call.
|
Lee Thomason | 8553625 | 2016-06-04 19:10:53 -0700 | [diff] [blame] | 2061 | SetError(XML_SUCCESS, 0, 0);
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2062 | XMLPrinter stream( fp, compact );
|
| 2063 | Print( &stream );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2064 | return _errorID;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2065 | }
|
| 2066 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 2067 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 2068 | XMLError XMLDocument::Parse( const char* p, size_t len )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 2069 | {
|
Martinsh Shaiters | a9d42b0 | 2013-01-30 11:14:27 +0200 | [diff] [blame] | 2070 | Clear();
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 2071 |
|
Lee Thomason | 82d3200 | 2014-02-21 22:47:18 -0800 | [diff] [blame] | 2072 | if ( len == 0 || !p || !*p ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2073 | SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2074 | return _errorID;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2075 | }
|
| 2076 | if ( len == (size_t)(-1) ) {
|
| 2077 | len = strlen( p );
|
| 2078 | }
|
Dmitry-Me | 96f38cc | 2015-08-10 16:45:12 +0300 | [diff] [blame] | 2079 | TIXMLASSERT( _charBuffer == 0 );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2080 | _charBuffer = new char[ len+1 ];
|
| 2081 | memcpy( _charBuffer, p, len );
|
| 2082 | _charBuffer[len] = 0;
|
Lee Thomason (grinliz) | e2bcb32 | 2012-09-17 17:58:25 -0700 | [diff] [blame] | 2083 |
|
Dmitry-Me | 97476b7 | 2015-01-01 16:15:57 +0300 | [diff] [blame] | 2084 | Parse();
|
Dmitry-Me | 9bcd9c7 | 2014-12-09 10:58:14 +0300 | [diff] [blame] | 2085 | if ( Error() ) {
|
Lee Thomason | f07b952 | 2014-10-30 13:25:12 -0700 | [diff] [blame] | 2086 | // clean up now essentially dangling memory.
|
| 2087 | // and the parse fail can put objects in the
|
| 2088 | // pools that are dead and inaccessible.
|
| 2089 | DeleteChildren();
|
| 2090 | _elementPool.Clear();
|
| 2091 | _attributePool.Clear();
|
| 2092 | _textPool.Clear();
|
| 2093 | _commentPool.Clear();
|
| 2094 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2095 | return _errorID;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 2096 | }
|
| 2097 |
|
| 2098 |
|
PKEuS | 1c5f99e | 2013-07-06 11:28:39 +0200 | [diff] [blame] | 2099 | void XMLDocument::Print( XMLPrinter* streamer ) const
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 2100 | {
|
Dmitry-Me | 67c429e | 2015-05-08 18:08:18 +0300 | [diff] [blame] | 2101 | if ( streamer ) {
|
| 2102 | Accept( streamer );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2103 | }
|
Dmitry-Me | 67c429e | 2015-05-08 18:08:18 +0300 | [diff] [blame] | 2104 | else {
|
| 2105 | XMLPrinter stdoutStreamer( stdout );
|
| 2106 | Accept( &stdoutStreamer );
|
| 2107 | }
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 2108 | }
|
| 2109 |
|
| 2110 |
|
Lee Thomason | 2fa8172 | 2012-11-09 12:37:46 -0800 | [diff] [blame] | 2111 | void XMLDocument::SetError( XMLError error, const char* str1, const char* str2 )
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 2112 | {
|
Dmitry-Me | 66d2a84 | 2014-11-08 15:24:52 +0300 | [diff] [blame] | 2113 | TIXMLASSERT( error >= 0 && error < XML_ERROR_COUNT );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2114 | _errorID = error;
|
| 2115 | _errorStr1 = str1;
|
| 2116 | _errorStr2 = str2;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 2117 | }
|
| 2118 |
|
Lee Thomason | 331596e | 2014-09-11 14:56:43 -0700 | [diff] [blame] | 2119 | const char* XMLDocument::ErrorName() const
|
| 2120 | {
|
Dmitry-Me | 66d2a84 | 2014-11-08 15:24:52 +0300 | [diff] [blame] | 2121 | TIXMLASSERT( _errorID >= 0 && _errorID < XML_ERROR_COUNT );
|
Dmitry-Me | a1beddf | 2015-05-26 16:19:21 +0300 | [diff] [blame] | 2122 | const char* errorName = _errorNames[_errorID];
|
| 2123 | TIXMLASSERT( errorName && errorName[0] );
|
| 2124 | return errorName;
|
Lee Thomason | 331596e | 2014-09-11 14:56:43 -0700 | [diff] [blame] | 2125 | }
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2126 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 2127 | void XMLDocument::PrintError() const
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2128 | {
|
Dmitry-Me | 9bcd9c7 | 2014-12-09 10:58:14 +0300 | [diff] [blame] | 2129 | if ( Error() ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2130 | static const int LEN = 20;
|
| 2131 | char buf1[LEN] = { 0 };
|
| 2132 | char buf2[LEN] = { 0 };
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 2133 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2134 | if ( _errorStr1 ) {
|
| 2135 | TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2136 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2137 | if ( _errorStr2 ) {
|
| 2138 | TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2139 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2140 |
|
Dmitry-Me | 2ad4320 | 2015-04-16 12:18:58 +0300 | [diff] [blame] | 2141 | // Should check INT_MIN <= _errorID && _errorId <= INT_MAX, but that
|
| 2142 | // causes a clang "always true" -Wtautological-constant-out-of-range-compare warning
|
| 2143 | TIXMLASSERT( 0 <= _errorID && XML_ERROR_COUNT - 1 <= INT_MAX );
|
Lee Thomason | 331596e | 2014-09-11 14:56:43 -0700 | [diff] [blame] | 2144 | printf( "XMLDocument error id=%d '%s' str1=%s str2=%s\n",
|
Dmitry-Me | 400f119 | 2015-04-07 11:51:21 +0300 | [diff] [blame] | 2145 | static_cast<int>( _errorID ), ErrorName(), buf1, buf2 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2146 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2147 | }
|
| 2148 |
|
Dmitry-Me | 97476b7 | 2015-01-01 16:15:57 +0300 | [diff] [blame] | 2149 | void XMLDocument::Parse()
|
| 2150 | {
|
| 2151 | TIXMLASSERT( NoChildren() ); // Clear() must have been called previously
|
| 2152 | TIXMLASSERT( _charBuffer );
|
Lee Thomason | 3cebdc4 | 2015-01-05 17:16:28 -0800 | [diff] [blame] | 2153 | char* p = _charBuffer;
|
Dmitry-Me | 97476b7 | 2015-01-01 16:15:57 +0300 | [diff] [blame] | 2154 | p = XMLUtil::SkipWhiteSpace( p );
|
Dmitry-Me | e28be75 | 2015-01-09 14:59:30 +0300 | [diff] [blame] | 2155 | p = const_cast<char*>( XMLUtil::ReadBOM( p, &_writeBOM ) );
|
Dmitry-Me | 97476b7 | 2015-01-01 16:15:57 +0300 | [diff] [blame] | 2156 | if ( !*p ) {
|
| 2157 | SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
|
| 2158 | return;
|
| 2159 | }
|
Lee Thomason | 3cebdc4 | 2015-01-05 17:16:28 -0800 | [diff] [blame] | 2160 | ParseDeep(p, 0 );
|
Dmitry-Me | 97476b7 | 2015-01-01 16:15:57 +0300 | [diff] [blame] | 2161 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2162 |
|
PKEuS | 1bfb954 | 2013-08-04 13:51:17 +0200 | [diff] [blame] | 2163 | XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2164 | _elementJustOpened( false ),
|
| 2165 | _firstElement( true ),
|
| 2166 | _fp( file ),
|
PKEuS | 1bfb954 | 2013-08-04 13:51:17 +0200 | [diff] [blame] | 2167 | _depth( depth ),
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2168 | _textDepth( -1 ),
|
| 2169 | _processEntities( true ),
|
| 2170 | _compactMode( compact )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2171 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2172 | for( int i=0; i<ENTITY_RANGE; ++i ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2173 | _entityFlag[i] = false;
|
| 2174 | _restrictedEntityFlag[i] = false;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2175 | }
|
| 2176 | for( int i=0; i<NUM_ENTITIES; ++i ) {
|
Dmitry-Me | 8b67d74 | 2014-12-22 11:35:12 +0300 | [diff] [blame] | 2177 | const char entityValue = entities[i].value;
|
| 2178 | TIXMLASSERT( 0 <= entityValue && entityValue < ENTITY_RANGE );
|
| 2179 | _entityFlag[ (unsigned char)entityValue ] = true;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2180 | }
|
Dmitry-Me | 8b67d74 | 2014-12-22 11:35:12 +0300 | [diff] [blame] | 2181 | _restrictedEntityFlag[(unsigned char)'&'] = true;
|
| 2182 | _restrictedEntityFlag[(unsigned char)'<'] = true;
|
| 2183 | _restrictedEntityFlag[(unsigned char)'>'] = true; // not required, but consistency is nice
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2184 | _buffer.Push( 0 );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 2185 | }
|
| 2186 |
|
| 2187 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2188 | void XMLPrinter::Print( const char* format, ... )
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 2189 | {
|
| 2190 | va_list va;
|
| 2191 | va_start( va, format );
|
| 2192 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2193 | if ( _fp ) {
|
| 2194 | vfprintf( _fp, format, va );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2195 | }
|
| 2196 | else {
|
Dmitry-Me | 1d32e58 | 2015-07-27 17:11:51 +0300 | [diff] [blame] | 2197 | const int len = TIXML_VSCPRINTF( format, va );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2198 | // Close out and re-start the va-args
|
| 2199 | va_end( va );
|
Dmitry-Me | 1d32e58 | 2015-07-27 17:11:51 +0300 | [diff] [blame] | 2200 | TIXMLASSERT( len >= 0 );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2201 | va_start( va, format );
|
Dmitry-Me | 30bdc97 | 2015-01-14 08:32:23 +0300 | [diff] [blame] | 2202 | TIXMLASSERT( _buffer.Size() > 0 && _buffer[_buffer.Size() - 1] == 0 );
|
Lee Thomason | a0744c8 | 2014-03-16 10:32:27 -0700 | [diff] [blame] | 2203 | char* p = _buffer.PushArr( len ) - 1; // back up over the null terminator.
|
pffang | 1527cf4 | 2015-06-09 13:57:11 +0800 | [diff] [blame] | 2204 | TIXML_VSNPRINTF( p, len+1, format, va );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2205 | }
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 2206 | va_end( va );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2207 | }
|
| 2208 |
|
| 2209 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2210 | void XMLPrinter::PrintSpace( int depth )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2211 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2212 | for( int i=0; i<depth; ++i ) {
|
| 2213 | Print( " " );
|
| 2214 | }
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2215 | }
|
| 2216 |
|
| 2217 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2218 | void XMLPrinter::PrintString( const char* p, bool restricted )
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 2219 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2220 | // Look for runs of bytes between entities to print.
|
| 2221 | const char* q = p;
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 2222 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2223 | if ( _processEntities ) {
|
Dmitry-Me | 6acc9a5 | 2015-01-15 13:27:47 +0300 | [diff] [blame] | 2224 | const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2225 | while ( *q ) {
|
Dmitry-Me | 69d521d | 2015-04-20 18:05:53 +0300 | [diff] [blame] | 2226 | TIXMLASSERT( p <= q );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2227 | // Remember, char is sometimes signed. (How many times has that bitten me?)
|
| 2228 | if ( *q > 0 && *q < ENTITY_RANGE ) {
|
| 2229 | // Check for entities. If one is found, flush
|
| 2230 | // the stream up until the entity, write the
|
| 2231 | // entity, and keep looking.
|
Dmitry-Me | 8b67d74 | 2014-12-22 11:35:12 +0300 | [diff] [blame] | 2232 | if ( flag[(unsigned char)(*q)] ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2233 | while ( p < q ) {
|
Dmitry-Me | d95172b | 2015-03-30 08:11:18 +0300 | [diff] [blame] | 2234 | const size_t delta = q - p;
|
| 2235 | // %.*s accepts type int as "precision"
|
Ross Bencina | e7fa0e1 | 2015-07-22 16:58:05 +1000 | [diff] [blame] | 2236 | const int toPrint = ( INT_MAX < delta ) ? INT_MAX : (int)delta;
|
Dmitry-Me | d95172b | 2015-03-30 08:11:18 +0300 | [diff] [blame] | 2237 | Print( "%.*s", toPrint, p );
|
| 2238 | p += toPrint;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2239 | }
|
Dmitry-Me | 39c399a | 2015-05-28 15:32:27 +0300 | [diff] [blame] | 2240 | bool entityPatternPrinted = false;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2241 | for( int i=0; i<NUM_ENTITIES; ++i ) {
|
| 2242 | if ( entities[i].value == *q ) {
|
| 2243 | Print( "&%s;", entities[i].pattern );
|
Dmitry-Me | 39c399a | 2015-05-28 15:32:27 +0300 | [diff] [blame] | 2244 | entityPatternPrinted = true;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2245 | break;
|
| 2246 | }
|
| 2247 | }
|
Dmitry-Me | 39c399a | 2015-05-28 15:32:27 +0300 | [diff] [blame] | 2248 | if ( !entityPatternPrinted ) {
|
| 2249 | // TIXMLASSERT( entityPatternPrinted ) causes gcc -Wunused-but-set-variable in release
|
| 2250 | TIXMLASSERT( false );
|
| 2251 | }
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2252 | ++p;
|
| 2253 | }
|
| 2254 | }
|
| 2255 | ++q;
|
Dmitry-Me | 69d521d | 2015-04-20 18:05:53 +0300 | [diff] [blame] | 2256 | TIXMLASSERT( p <= q );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2257 | }
|
| 2258 | }
|
| 2259 | // Flush the remaining string. This will be the entire
|
| 2260 | // string if an entity wasn't found.
|
Dmitry-Me | 69d521d | 2015-04-20 18:05:53 +0300 | [diff] [blame] | 2261 | TIXMLASSERT( p <= q );
|
| 2262 | if ( !_processEntities || ( p < q ) ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2263 | Print( "%s", p );
|
| 2264 | }
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 2265 | }
|
| 2266 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 2267 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2268 | void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 2269 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2270 | if ( writeBOM ) {
|
PKEuS | 1bfb954 | 2013-08-04 13:51:17 +0200 | [diff] [blame] | 2271 | static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2272 | Print( "%s", bom );
|
| 2273 | }
|
| 2274 | if ( writeDec ) {
|
| 2275 | PushDeclaration( "xml version=\"1.0\"" );
|
| 2276 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 2277 | }
|
| 2278 |
|
| 2279 |
|
Uli Kusterer | 593a33d | 2014-02-01 12:48:51 +0100 | [diff] [blame] | 2280 | void XMLPrinter::OpenElement( const char* name, bool compactMode )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2281 | {
|
Dmitry-Me | a092bc1 | 2014-12-23 17:57:05 +0300 | [diff] [blame] | 2282 | SealElementIfJustOpened();
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2283 | _stack.Push( name );
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 2284 |
|
Uli Kusterer | 593a33d | 2014-02-01 12:48:51 +0100 | [diff] [blame] | 2285 | if ( _textDepth < 0 && !_firstElement && !compactMode ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2286 | Print( "\n" );
|
PKEuS | 1bfb954 | 2013-08-04 13:51:17 +0200 | [diff] [blame] | 2287 | }
|
Uli Kusterer | 593a33d | 2014-02-01 12:48:51 +0100 | [diff] [blame] | 2288 | if ( !compactMode ) {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2289 | PrintSpace( _depth );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2290 | }
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2291 |
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2292 | Print( "<%s", name );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2293 | _elementJustOpened = true;
|
| 2294 | _firstElement = false;
|
| 2295 | ++_depth;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2296 | }
|
| 2297 |
|
| 2298 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2299 | void XMLPrinter::PushAttribute( const char* name, const char* value )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2300 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2301 | TIXMLASSERT( _elementJustOpened );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2302 | Print( " %s=\"", name );
|
| 2303 | PrintString( value, false );
|
| 2304 | Print( "\"" );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2305 | }
|
| 2306 |
|
| 2307 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 2308 | void XMLPrinter::PushAttribute( const char* name, int v )
|
| 2309 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2310 | char buf[BUF_SIZE];
|
| 2311 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 2312 | PushAttribute( name, buf );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 2313 | }
|
| 2314 |
|
| 2315 |
|
| 2316 | void XMLPrinter::PushAttribute( const char* name, unsigned v )
|
| 2317 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2318 | char buf[BUF_SIZE];
|
| 2319 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 2320 | PushAttribute( name, buf );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 2321 | }
|
| 2322 |
|
| 2323 |
|
Lee Thomason | 51c1271 | 2016-06-04 20:18:49 -0700 | [diff] [blame] | 2324 | void XMLPrinter::PushAttribute(const char* name, int64_t v)
|
| 2325 | {
|
| 2326 | char buf[BUF_SIZE];
|
| 2327 | XMLUtil::ToStr(v, buf, BUF_SIZE);
|
| 2328 | PushAttribute(name, buf);
|
| 2329 | }
|
| 2330 |
|
| 2331 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 2332 | void XMLPrinter::PushAttribute( const char* name, bool v )
|
| 2333 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2334 | char buf[BUF_SIZE];
|
| 2335 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 2336 | PushAttribute( name, buf );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 2337 | }
|
| 2338 |
|
| 2339 |
|
| 2340 | void XMLPrinter::PushAttribute( const char* name, double v )
|
| 2341 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2342 | char buf[BUF_SIZE];
|
| 2343 | XMLUtil::ToStr( v, buf, BUF_SIZE );
|
| 2344 | PushAttribute( name, buf );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 2345 | }
|
| 2346 |
|
| 2347 |
|
Uli Kusterer | ca412e8 | 2014-02-01 13:35:05 +0100 | [diff] [blame] | 2348 | void XMLPrinter::CloseElement( bool compactMode )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2349 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2350 | --_depth;
|
| 2351 | const char* name = _stack.Pop();
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2352 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2353 | if ( _elementJustOpened ) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2354 | Print( "/>" );
|
| 2355 | }
|
| 2356 | else {
|
Uli Kusterer | ca412e8 | 2014-02-01 13:35:05 +0100 | [diff] [blame] | 2357 | if ( _textDepth < 0 && !compactMode) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2358 | Print( "\n" );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2359 | PrintSpace( _depth );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2360 | }
|
| 2361 | Print( "</%s>", name );
|
| 2362 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 2363 |
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2364 | if ( _textDepth == _depth ) {
|
| 2365 | _textDepth = -1;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2366 | }
|
Uli Kusterer | ca412e8 | 2014-02-01 13:35:05 +0100 | [diff] [blame] | 2367 | if ( _depth == 0 && !compactMode) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2368 | Print( "\n" );
|
| 2369 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2370 | _elementJustOpened = false;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2371 | }
|
| 2372 |
|
| 2373 |
|
Dmitry-Me | a092bc1 | 2014-12-23 17:57:05 +0300 | [diff] [blame] | 2374 | void XMLPrinter::SealElementIfJustOpened()
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2375 | {
|
Dmitry-Me | a092bc1 | 2014-12-23 17:57:05 +0300 | [diff] [blame] | 2376 | if ( !_elementJustOpened ) {
|
| 2377 | return;
|
| 2378 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2379 | _elementJustOpened = false;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2380 | Print( ">" );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2381 | }
|
| 2382 |
|
| 2383 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2384 | void XMLPrinter::PushText( const char* text, bool cdata )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2385 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2386 | _textDepth = _depth-1;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 2387 |
|
Dmitry-Me | a092bc1 | 2014-12-23 17:57:05 +0300 | [diff] [blame] | 2388 | SealElementIfJustOpened();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2389 | if ( cdata ) {
|
Dmitry-Me | 6a79c17 | 2015-03-31 12:18:17 +0300 | [diff] [blame] | 2390 | Print( "<![CDATA[%s]]>", text );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2391 | }
|
| 2392 | else {
|
| 2393 | PrintString( text, true );
|
| 2394 | }
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2395 | }
|
| 2396 |
|
Stefan Asbeck | e1a82c1 | 2016-08-04 09:12:45 +0200 | [diff] [blame] | 2397 | void XMLPrinter::PushText( int64_t value )
|
| 2398 | {
|
| 2399 | char buf[BUF_SIZE];
|
| 2400 | XMLUtil::ToStr( value, buf, BUF_SIZE );
|
| 2401 | PushText( buf, false );
|
| 2402 | }
|
| 2403 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 2404 | void XMLPrinter::PushText( int value )
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 2405 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2406 | char buf[BUF_SIZE];
|
| 2407 | XMLUtil::ToStr( value, buf, BUF_SIZE );
|
| 2408 | PushText( buf, false );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 2409 | }
|
| 2410 |
|
| 2411 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 2412 | void XMLPrinter::PushText( unsigned value )
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 2413 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2414 | char buf[BUF_SIZE];
|
| 2415 | XMLUtil::ToStr( value, buf, BUF_SIZE );
|
| 2416 | PushText( buf, false );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 2417 | }
|
| 2418 |
|
| 2419 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 2420 | void XMLPrinter::PushText( bool value )
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 2421 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2422 | char buf[BUF_SIZE];
|
| 2423 | XMLUtil::ToStr( value, buf, BUF_SIZE );
|
| 2424 | PushText( buf, false );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 2425 | }
|
| 2426 |
|
| 2427 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 2428 | void XMLPrinter::PushText( float value )
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 2429 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2430 | char buf[BUF_SIZE];
|
| 2431 | XMLUtil::ToStr( value, buf, BUF_SIZE );
|
| 2432 | PushText( buf, false );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 2433 | }
|
| 2434 |
|
| 2435 |
|
Lee Thomason (grinliz) | 2f1f624 | 2012-09-16 11:32:34 -0700 | [diff] [blame] | 2436 | void XMLPrinter::PushText( double value )
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 2437 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2438 | char buf[BUF_SIZE];
|
| 2439 | XMLUtil::ToStr( value, buf, BUF_SIZE );
|
| 2440 | PushText( buf, false );
|
Lee Thomason | 21be882 | 2012-07-15 17:27:22 -0700 | [diff] [blame] | 2441 | }
|
| 2442 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2443 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2444 | void XMLPrinter::PushComment( const char* comment )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2445 | {
|
Dmitry-Me | a092bc1 | 2014-12-23 17:57:05 +0300 | [diff] [blame] | 2446 | SealElementIfJustOpened();
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2447 | if ( _textDepth < 0 && !_firstElement && !_compactMode) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2448 | Print( "\n" );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2449 | PrintSpace( _depth );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2450 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2451 | _firstElement = false;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2452 | Print( "<!--%s-->", comment );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 2453 | }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2454 |
|
| 2455 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2456 | void XMLPrinter::PushDeclaration( const char* value )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2457 | {
|
Dmitry-Me | a092bc1 | 2014-12-23 17:57:05 +0300 | [diff] [blame] | 2458 | SealElementIfJustOpened();
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2459 | if ( _textDepth < 0 && !_firstElement && !_compactMode) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2460 | Print( "\n" );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2461 | PrintSpace( _depth );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2462 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2463 | _firstElement = false;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2464 | Print( "<?%s?>", value );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2465 | }
|
| 2466 |
|
| 2467 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2468 | void XMLPrinter::PushUnknown( const char* value )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2469 | {
|
Dmitry-Me | a092bc1 | 2014-12-23 17:57:05 +0300 | [diff] [blame] | 2470 | SealElementIfJustOpened();
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2471 | if ( _textDepth < 0 && !_firstElement && !_compactMode) {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2472 | Print( "\n" );
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2473 | PrintSpace( _depth );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2474 | }
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2475 | _firstElement = false;
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2476 | Print( "<!%s>", value );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2477 | }
|
| 2478 |
|
| 2479 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2480 | bool XMLPrinter::VisitEnter( const XMLDocument& doc )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 2481 | {
|
Lee Thomason | 624d43f | 2012-10-12 10:58:48 -0700 | [diff] [blame] | 2482 | _processEntities = doc.ProcessEntities();
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2483 | if ( doc.HasBOM() ) {
|
| 2484 | PushHeader( true, false );
|
| 2485 | }
|
| 2486 | return true;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 2487 | }
|
| 2488 |
|
| 2489 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2490 | bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2491 | {
|
Dmitry-Me | e76b851 | 2015-04-08 13:41:40 +0300 | [diff] [blame] | 2492 | const XMLElement* parentElem = 0;
|
| 2493 | if ( element.Parent() ) {
|
| 2494 | parentElem = element.Parent()->ToElement();
|
Ant Mitchell | 7e74477 | 2015-03-24 14:33:28 +0000 | [diff] [blame] | 2495 | }
|
Dmitry-Me | e76b851 | 2015-04-08 13:41:40 +0300 | [diff] [blame] | 2496 | const bool compactMode = parentElem ? CompactMode( *parentElem ) : _compactMode;
|
Uli Kusterer | 5d1d27e | 2014-02-20 11:50:22 +0100 | [diff] [blame] | 2497 | OpenElement( element.Name(), compactMode );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2498 | while ( attribute ) {
|
| 2499 | PushAttribute( attribute->Name(), attribute->Value() );
|
| 2500 | attribute = attribute->Next();
|
| 2501 | }
|
| 2502 | return true;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2503 | }
|
| 2504 |
|
| 2505 |
|
Uli Kusterer | ca412e8 | 2014-02-01 13:35:05 +0100 | [diff] [blame] | 2506 | bool XMLPrinter::VisitExit( const XMLElement& element )
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2507 | {
|
Uli Kusterer | 5d1d27e | 2014-02-20 11:50:22 +0100 | [diff] [blame] | 2508 | CloseElement( CompactMode(element) );
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2509 | return true;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2510 | }
|
| 2511 |
|
| 2512 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2513 | bool XMLPrinter::Visit( const XMLText& text )
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2514 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2515 | PushText( text.Value(), text.CData() );
|
| 2516 | return true;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2517 | }
|
| 2518 |
|
| 2519 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2520 | bool XMLPrinter::Visit( const XMLComment& comment )
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2521 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2522 | PushComment( comment.Value() );
|
| 2523 | return true;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 2524 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2525 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2526 | bool XMLPrinter::Visit( const XMLDeclaration& declaration )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2527 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2528 | PushDeclaration( declaration.Value() );
|
| 2529 | return true;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2530 | }
|
| 2531 |
|
| 2532 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 2533 | bool XMLPrinter::Visit( const XMLUnknown& unknown )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2534 | {
|
Lee Thomason | a9cf3f9 | 2012-10-11 16:56:51 -0700 | [diff] [blame] | 2535 | PushUnknown( unknown.Value() );
|
| 2536 | return true;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 2537 | }
|
Kevin Wojniak | 04c22d2 | 2012-11-08 11:02:22 -0800 | [diff] [blame] | 2538 |
|
Lee Thomason | 685b895 | 2012-11-12 13:00:06 -0800 | [diff] [blame] | 2539 | } // namespace tinyxml2
|
| 2540 |
|