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