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