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