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