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 |
|
| 26 | #include <string.h>
|
| 27 | #include <stdlib.h>
|
| 28 | #include <stdio.h>
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 29 | #include <ctype.h>
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 30 | #include <new>
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 31 | #include <stdarg.h>
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 32 |
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 33 |
|
| 34 | using namespace tinyxml2;
|
| 35 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 36 | 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] | 37 | static const char LF = LINE_FEED;
|
| 38 | static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
|
| 39 | static const char CR = CARRIAGE_RETURN;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 40 | static const char SINGLE_QUOTE = '\'';
|
| 41 | static const char DOUBLE_QUOTE = '\"';
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 42 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 43 | // Bunch of unicode info at:
|
| 44 | // http://www.unicode.org/faq/utf_bom.html
|
| 45 | // ef bb bf (Microsoft "lead bytes") - designates UTF-8
|
| 46 |
|
| 47 | static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
|
| 48 | static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
|
| 49 | static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 50 |
|
| 51 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 52 | #define DELETE_NODE( node ) { \
|
| 53 | if ( node ) { \
|
| 54 | MemPool* pool = node->memPool; \
|
| 55 | node->~XMLNode(); \
|
| 56 | pool->Free( node ); \
|
| 57 | } \
|
| 58 | }
|
| 59 | #define DELETE_ATTRIBUTE( attrib ) { \
|
| 60 | if ( attrib ) { \
|
| 61 | MemPool* pool = attrib->memPool; \
|
| 62 | attrib->~XMLAttribute(); \
|
| 63 | pool->Free( attrib ); \
|
| 64 | } \
|
| 65 | }
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 66 |
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 67 | struct Entity {
|
| 68 | const char* pattern;
|
| 69 | int length;
|
| 70 | char value;
|
| 71 | };
|
| 72 |
|
| 73 | static const int NUM_ENTITIES = 5;
|
| 74 | static const Entity entities[NUM_ENTITIES] =
|
| 75 | {
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 76 | { "quot", 4, DOUBLE_QUOTE },
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 77 | { "amp", 3, '&' },
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 78 | { "apos", 4, SINGLE_QUOTE },
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 79 | { "lt", 2, '<' },
|
| 80 | { "gt", 2, '>' }
|
| 81 | };
|
| 82 |
|
Lee Thomason | fde6a75 | 2012-01-14 18:08:12 -0800 | [diff] [blame] | 83 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 84 | StrPair::~StrPair()
|
| 85 | {
|
| 86 | Reset();
|
| 87 | }
|
| 88 |
|
| 89 |
|
| 90 | void StrPair::Reset()
|
| 91 | {
|
| 92 | if ( flags & NEEDS_DELETE ) {
|
| 93 | delete [] start;
|
| 94 | }
|
| 95 | flags = 0;
|
| 96 | start = 0;
|
| 97 | end = 0;
|
| 98 | }
|
| 99 |
|
| 100 |
|
| 101 | void StrPair::SetStr( const char* str, int flags )
|
| 102 | {
|
| 103 | Reset();
|
| 104 | size_t len = strlen( str );
|
| 105 | start = new char[ len+1 ];
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 106 | memcpy( start, str, len+1 );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 107 | end = start + len;
|
| 108 | this->flags = flags | NEEDS_DELETE;
|
| 109 | }
|
| 110 |
|
| 111 |
|
| 112 | char* StrPair::ParseText( char* p, const char* endTag, int strFlags )
|
| 113 | {
|
| 114 | TIXMLASSERT( endTag && *endTag );
|
| 115 |
|
| 116 | char* start = p; // fixme: hides a member
|
| 117 | char endChar = *endTag;
|
| 118 | int length = strlen( endTag );
|
| 119 |
|
| 120 | // Inner loop of text parsing.
|
| 121 | while ( *p ) {
|
| 122 | if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
|
| 123 | Set( start, p, strFlags );
|
| 124 | return p + length;
|
| 125 | }
|
| 126 | ++p;
|
| 127 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 128 | return 0;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 129 | }
|
| 130 |
|
| 131 |
|
| 132 | char* StrPair::ParseName( char* p )
|
| 133 | {
|
| 134 | char* start = p;
|
| 135 |
|
| 136 | start = p;
|
| 137 | if ( !start || !(*start) ) {
|
| 138 | return 0;
|
| 139 | }
|
| 140 |
|
| 141 | if ( !XMLUtil::IsAlpha( *p ) ) {
|
| 142 | return 0;
|
| 143 | }
|
| 144 |
|
| 145 | while( *p && (
|
| 146 | XMLUtil::IsAlphaNum( (unsigned char) *p )
|
| 147 | || *p == '_'
|
| 148 | || *p == '-'
|
| 149 | || *p == '.'
|
| 150 | || *p == ':' ))
|
| 151 | {
|
| 152 | ++p;
|
| 153 | }
|
| 154 |
|
| 155 | if ( p > start ) {
|
| 156 | Set( start, p, 0 );
|
| 157 | return p;
|
| 158 | }
|
| 159 | return 0;
|
| 160 | }
|
| 161 |
|
| 162 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 163 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 164 | const char* StrPair::GetStr()
|
| 165 | {
|
| 166 | if ( flags & NEEDS_FLUSH ) {
|
| 167 | *end = 0;
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 168 | flags ^= NEEDS_FLUSH;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 169 |
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 170 | if ( flags ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 171 | char* p = start; // the read pointer
|
| 172 | char* q = start; // the write pointer
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 173 |
|
| 174 | while( p < end ) {
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 175 | if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 176 | // CR-LF pair becomes LF
|
| 177 | // CR alone becomes LF
|
| 178 | // LF-CR becomes LF
|
| 179 | if ( *(p+1) == LF ) {
|
| 180 | p += 2;
|
| 181 | }
|
| 182 | else {
|
| 183 | ++p;
|
| 184 | }
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 185 | *q++ = LF;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 186 | }
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 187 | else if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 188 | if ( *(p+1) == CR ) {
|
| 189 | p += 2;
|
| 190 | }
|
| 191 | else {
|
| 192 | ++p;
|
| 193 | }
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 194 | *q++ = LF;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 195 | }
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 196 | else if ( (flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
|
| 197 | int i=0;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 198 |
|
| 199 | // Entities handled by tinyXML2:
|
| 200 | // - special entities in the entity table [in/out]
|
| 201 | // - numeric character reference [in]
|
| 202 | // 中 or 中
|
| 203 |
|
| 204 | if ( *(p+1) == '#' ) {
|
| 205 | char buf[10] = { 0 };
|
| 206 | int len;
|
| 207 | p = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
|
| 208 | for( int i=0; i<len; ++i ) {
|
| 209 | *q++ = buf[i];
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 210 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 211 | TIXMLASSERT( q <= p );
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 212 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 213 | else {
|
| 214 | for( i=0; i<NUM_ENTITIES; ++i ) {
|
| 215 | if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
|
| 216 | && *(p+entities[i].length+1) == ';' )
|
| 217 | {
|
| 218 | // Found an entity convert;
|
| 219 | *q = entities[i].value;
|
| 220 | ++q;
|
| 221 | p += entities[i].length + 2;
|
| 222 | break;
|
| 223 | }
|
| 224 | }
|
| 225 | if ( i == NUM_ENTITIES ) {
|
| 226 | // fixme: treat as error?
|
| 227 | ++p;
|
| 228 | ++q;
|
| 229 | }
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 230 | }
|
| 231 | }
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 232 | else {
|
| 233 | *q = *p;
|
| 234 | ++p;
|
Lee Thomason | ec975ce | 2012-01-23 11:42:06 -0800 | [diff] [blame] | 235 | ++q;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 236 | }
|
| 237 | }
|
Lee Thomason | 8ee7989 | 2012-01-25 17:44:30 -0800 | [diff] [blame] | 238 | *q = 0;
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 239 | }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 240 | flags = (flags & NEEDS_DELETE);
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 241 | }
|
| 242 | return start;
|
| 243 | }
|
| 244 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 245 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 246 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 247 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 248 | // --------- XMLUtil ----------- //
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 249 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 250 | const char* XMLUtil::ReadBOM( const char* p, bool* bom )
|
| 251 | {
|
| 252 | *bom = false;
|
| 253 | const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);
|
| 254 | // Check for BOM:
|
| 255 | if ( *(pu+0) == TIXML_UTF_LEAD_0
|
| 256 | && *(pu+1) == TIXML_UTF_LEAD_1
|
| 257 | && *(pu+2) == TIXML_UTF_LEAD_2 )
|
| 258 | {
|
| 259 | *bom = true;
|
| 260 | p += 3;
|
| 261 | }
|
| 262 | return p;
|
| 263 | }
|
| 264 |
|
| 265 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 266 | void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
|
| 267 | {
|
| 268 | const unsigned long BYTE_MASK = 0xBF;
|
| 269 | const unsigned long BYTE_MARK = 0x80;
|
| 270 | const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
|
| 271 |
|
| 272 | if (input < 0x80)
|
| 273 | *length = 1;
|
| 274 | else if ( input < 0x800 )
|
| 275 | *length = 2;
|
| 276 | else if ( input < 0x10000 )
|
| 277 | *length = 3;
|
| 278 | else if ( input < 0x200000 )
|
| 279 | *length = 4;
|
| 280 | else
|
| 281 | { *length = 0; return; } // This code won't covert this correctly anyway.
|
| 282 |
|
| 283 | output += *length;
|
| 284 |
|
| 285 | // Scary scary fall throughs.
|
| 286 | switch (*length)
|
| 287 | {
|
| 288 | case 4:
|
| 289 | --output;
|
| 290 | *output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
| 291 | input >>= 6;
|
| 292 | case 3:
|
| 293 | --output;
|
| 294 | *output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
| 295 | input >>= 6;
|
| 296 | case 2:
|
| 297 | --output;
|
| 298 | *output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
| 299 | input >>= 6;
|
| 300 | case 1:
|
| 301 | --output;
|
| 302 | *output = (char)(input | FIRST_BYTE_MARK[*length]);
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 |
|
| 307 | const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
|
| 308 | {
|
| 309 | // Presume an entity, and pull it out.
|
| 310 | *length = 0;
|
| 311 |
|
| 312 | if ( *(p+1) == '#' && *(p+2) )
|
| 313 | {
|
| 314 | unsigned long ucs = 0;
|
Lee Thomason (grinliz) | 7ca5558 | 2012-03-07 21:54:57 -0800 | [diff] [blame^] | 315 | int delta = 0;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 316 | unsigned mult = 1;
|
| 317 |
|
| 318 | if ( *(p+2) == 'x' )
|
| 319 | {
|
| 320 | // Hexadecimal.
|
| 321 | if ( !*(p+3) ) return 0;
|
| 322 |
|
| 323 | const char* q = p+3;
|
| 324 | q = strchr( q, ';' );
|
| 325 |
|
| 326 | if ( !q || !*q ) return 0;
|
| 327 |
|
Lee Thomason (grinliz) | 7ca5558 | 2012-03-07 21:54:57 -0800 | [diff] [blame^] | 328 | delta = (q-p);
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 329 | --q;
|
| 330 |
|
| 331 | while ( *q != 'x' )
|
| 332 | {
|
| 333 | if ( *q >= '0' && *q <= '9' )
|
| 334 | ucs += mult * (*q - '0');
|
| 335 | else if ( *q >= 'a' && *q <= 'f' )
|
| 336 | ucs += mult * (*q - 'a' + 10);
|
| 337 | else if ( *q >= 'A' && *q <= 'F' )
|
| 338 | ucs += mult * (*q - 'A' + 10 );
|
| 339 | else
|
| 340 | return 0;
|
| 341 | mult *= 16;
|
| 342 | --q;
|
| 343 | }
|
| 344 | }
|
| 345 | else
|
| 346 | {
|
| 347 | // Decimal.
|
| 348 | if ( !*(p+2) ) return 0;
|
| 349 |
|
| 350 | const char* q = p+2;
|
| 351 | q = strchr( q, ';' );
|
| 352 |
|
| 353 | if ( !q || !*q ) return 0;
|
| 354 |
|
| 355 | delta = q-p;
|
| 356 | --q;
|
| 357 |
|
| 358 | while ( *q != '#' )
|
| 359 | {
|
| 360 | if ( *q >= '0' && *q <= '9' )
|
| 361 | ucs += mult * (*q - '0');
|
| 362 | else
|
| 363 | return 0;
|
| 364 | mult *= 10;
|
| 365 | --q;
|
| 366 | }
|
| 367 | }
|
| 368 | // convert the UCS to UTF-8
|
| 369 | ConvertUTF32ToUTF8( ucs, value, length );
|
| 370 | return p + delta + 1;
|
| 371 | }
|
| 372 | return p+1;
|
| 373 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 374 |
|
| 375 |
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 376 | char* XMLDocument::Identify( char* p, XMLNode** node )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 377 | {
|
| 378 | XMLNode* returnNode = 0;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 379 | char* start = p;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 380 | p = XMLUtil::SkipWhiteSpace( p );
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 381 | if( !p || !*p )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 382 | {
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 383 | return p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 384 | }
|
| 385 |
|
| 386 | // What is this thing?
|
| 387 | // - Elements start with a letter or underscore, but xml is reserved.
|
| 388 | // - Comments: <!--
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 389 | // - Decleration: <?
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 390 | // - Everthing else is unknown to tinyxml.
|
| 391 | //
|
| 392 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 393 | static const char* xmlHeader = { "<?" };
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 394 | static const char* commentHeader = { "<!--" };
|
| 395 | static const char* dtdHeader = { "<!" };
|
| 396 | static const char* cdataHeader = { "<![CDATA[" };
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 397 | static const char* elementHeader = { "<" }; // and a header for everything else; check last.
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 398 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 399 | static const int xmlHeaderLen = 2;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 400 | static const int commentHeaderLen = 4;
|
| 401 | static const int dtdHeaderLen = 2;
|
| 402 | static const int cdataHeaderLen = 9;
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 403 | static const int elementHeaderLen = 1;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 404 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 405 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 406 | #pragma warning ( push )
|
| 407 | #pragma warning ( disable : 4127 )
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 408 | #endif
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 409 | TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
|
| 410 | TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 411 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 412 | #pragma warning (pop)
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 413 | #endif
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 414 | if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
|
| 415 | returnNode = new (commentPool.Alloc()) XMLDeclaration( this );
|
| 416 | returnNode->memPool = &commentPool;
|
| 417 | p += xmlHeaderLen;
|
| 418 | }
|
| 419 | else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 420 | returnNode = new (commentPool.Alloc()) XMLComment( this );
|
| 421 | returnNode->memPool = &commentPool;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 422 | p += commentHeaderLen;
|
| 423 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 424 | else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
|
| 425 | XMLText* text = new (textPool.Alloc()) XMLText( this );
|
| 426 | returnNode = text;
|
| 427 | returnNode->memPool = &textPool;
|
| 428 | p += cdataHeaderLen;
|
| 429 | text->SetCData( true );
|
| 430 | }
|
| 431 | else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
|
| 432 | returnNode = new (commentPool.Alloc()) XMLUnknown( this );
|
| 433 | returnNode->memPool = &commentPool;
|
| 434 | p += dtdHeaderLen;
|
| 435 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 436 | else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 437 | returnNode = new (elementPool.Alloc()) XMLElement( this );
|
| 438 | returnNode->memPool = &elementPool;
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 439 | p += elementHeaderLen;
|
| 440 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 441 | else {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 442 | returnNode = new (textPool.Alloc()) XMLText( this );
|
| 443 | returnNode->memPool = &textPool;
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 444 | p = start; // Back it up, all the text counts.
|
| 445 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 446 |
|
| 447 | *node = returnNode;
|
| 448 | return p;
|
| 449 | }
|
| 450 |
|
| 451 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 452 | bool XMLDocument::Accept( XMLVisitor* visitor ) const
|
| 453 | {
|
| 454 | if ( visitor->VisitEnter( *this ) )
|
| 455 | {
|
| 456 | for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
|
| 457 | {
|
| 458 | if ( !node->Accept( visitor ) )
|
| 459 | break;
|
| 460 | }
|
| 461 | }
|
| 462 | return visitor->VisitExit( *this );
|
| 463 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 464 |
|
| 465 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 466 | // --------- XMLNode ----------- //
|
| 467 |
|
| 468 | XMLNode::XMLNode( XMLDocument* doc ) :
|
| 469 | document( doc ),
|
| 470 | parent( 0 ),
|
| 471 | firstChild( 0 ), lastChild( 0 ),
|
| 472 | prev( 0 ), next( 0 )
|
| 473 | {
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 474 | }
|
| 475 |
|
| 476 |
|
| 477 | XMLNode::~XMLNode()
|
| 478 | {
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 479 | DeleteChildren();
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 480 | if ( parent ) {
|
| 481 | parent->Unlink( this );
|
| 482 | }
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 483 | }
|
| 484 |
|
| 485 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 486 | void XMLNode::SetValue( const char* str, bool staticMem )
|
| 487 | {
|
| 488 | if ( staticMem )
|
| 489 | value.SetInternedStr( str );
|
| 490 | else
|
| 491 | value.SetStr( str );
|
| 492 | }
|
| 493 |
|
| 494 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 495 | void XMLNode::DeleteChildren()
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 496 | {
|
Lee Thomason | d923c67 | 2012-01-23 08:44:25 -0800 | [diff] [blame] | 497 | while( firstChild ) {
|
| 498 | XMLNode* node = firstChild;
|
| 499 | Unlink( node );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 500 |
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 501 | DELETE_NODE( node );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 502 | }
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 503 | firstChild = lastChild = 0;
|
Lee Thomason | d923c67 | 2012-01-23 08:44:25 -0800 | [diff] [blame] | 504 | }
|
| 505 |
|
| 506 |
|
| 507 | void XMLNode::Unlink( XMLNode* child )
|
| 508 | {
|
| 509 | TIXMLASSERT( child->parent == this );
|
| 510 | if ( child == firstChild )
|
| 511 | firstChild = firstChild->next;
|
| 512 | if ( child == lastChild )
|
| 513 | lastChild = lastChild->prev;
|
| 514 |
|
| 515 | if ( child->prev ) {
|
| 516 | child->prev->next = child->next;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 517 | }
|
Lee Thomason | d923c67 | 2012-01-23 08:44:25 -0800 | [diff] [blame] | 518 | if ( child->next ) {
|
| 519 | child->next->prev = child->prev;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 520 | }
|
Lee Thomason | d923c67 | 2012-01-23 08:44:25 -0800 | [diff] [blame] | 521 | child->parent = 0;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 522 | }
|
| 523 |
|
| 524 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 525 | void XMLNode::DeleteChild( XMLNode* node )
|
| 526 | {
|
| 527 | TIXMLASSERT( node->parent == this );
|
| 528 | DELETE_NODE( node );
|
| 529 | }
|
| 530 |
|
| 531 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 532 | XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
|
| 533 | {
|
| 534 | if ( lastChild ) {
|
| 535 | TIXMLASSERT( firstChild );
|
| 536 | TIXMLASSERT( lastChild->next == 0 );
|
| 537 | lastChild->next = addThis;
|
| 538 | addThis->prev = lastChild;
|
| 539 | lastChild = addThis;
|
| 540 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 541 | addThis->next = 0;
|
| 542 | }
|
| 543 | else {
|
| 544 | TIXMLASSERT( firstChild == 0 );
|
| 545 | firstChild = lastChild = addThis;
|
| 546 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 547 | addThis->prev = 0;
|
| 548 | addThis->next = 0;
|
| 549 | }
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 550 | addThis->parent = this;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 551 | return addThis;
|
| 552 | }
|
| 553 |
|
| 554 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 555 | XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
|
| 556 | {
|
| 557 | if ( firstChild ) {
|
| 558 | TIXMLASSERT( lastChild );
|
| 559 | TIXMLASSERT( firstChild->prev == 0 );
|
| 560 |
|
| 561 | firstChild->prev = addThis;
|
| 562 | addThis->next = firstChild;
|
| 563 | firstChild = addThis;
|
| 564 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 565 | addThis->prev = 0;
|
| 566 | }
|
| 567 | else {
|
| 568 | TIXMLASSERT( lastChild == 0 );
|
| 569 | firstChild = lastChild = addThis;
|
| 570 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 571 | addThis->prev = 0;
|
| 572 | addThis->next = 0;
|
| 573 | }
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 574 | addThis->parent = this;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 575 | return addThis;
|
| 576 | }
|
| 577 |
|
| 578 |
|
| 579 | XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
|
| 580 | {
|
| 581 | TIXMLASSERT( afterThis->parent == this );
|
| 582 | if ( afterThis->parent != this )
|
| 583 | return 0;
|
| 584 |
|
| 585 | if ( afterThis->next == 0 ) {
|
| 586 | // The last node or the only node.
|
| 587 | return InsertEndChild( addThis );
|
| 588 | }
|
| 589 | addThis->prev = afterThis;
|
| 590 | addThis->next = afterThis->next;
|
| 591 | afterThis->next->prev = addThis;
|
| 592 | afterThis->next = addThis;
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 593 | addThis->parent = this;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 594 | return addThis;
|
| 595 | }
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 600 | const XMLElement* XMLNode::FirstChildElement( const char* value ) const
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 601 | {
|
| 602 | for( XMLNode* node=firstChild; node; node=node->next ) {
|
| 603 | XMLElement* element = node->ToElement();
|
| 604 | if ( element ) {
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 605 | if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 606 | return element;
|
| 607 | }
|
| 608 | }
|
| 609 | }
|
| 610 | return 0;
|
| 611 | }
|
| 612 |
|
| 613 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 614 | const XMLElement* XMLNode::LastChildElement( const char* value ) const
|
| 615 | {
|
| 616 | for( XMLNode* node=lastChild; node; node=node->prev ) {
|
| 617 | XMLElement* element = node->ToElement();
|
| 618 | if ( element ) {
|
| 619 | if ( !value || XMLUtil::StringEqual( element->Name(), value ) ) {
|
| 620 | return element;
|
| 621 | }
|
| 622 | }
|
| 623 | }
|
| 624 | return 0;
|
| 625 | }
|
| 626 |
|
| 627 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 628 | const XMLElement* XMLNode::NextSiblingElement( const char* value ) const
|
| 629 | {
|
| 630 | for( XMLNode* element=this->next; element; element = element->next ) {
|
| 631 | if ( element->ToElement()
|
| 632 | && (!value || XMLUtil::StringEqual( value, element->Value() )))
|
| 633 | {
|
| 634 | return element->ToElement();
|
| 635 | }
|
| 636 | }
|
| 637 | return 0;
|
| 638 | }
|
| 639 |
|
| 640 |
|
| 641 | const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const
|
| 642 | {
|
| 643 | for( XMLNode* element=this->prev; element; element = element->prev ) {
|
| 644 | if ( element->ToElement()
|
| 645 | && (!value || XMLUtil::StringEqual( value, element->Value() )))
|
| 646 | {
|
| 647 | return element->ToElement();
|
| 648 | }
|
| 649 | }
|
| 650 | return 0;
|
| 651 | }
|
| 652 |
|
| 653 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 654 | char* XMLNode::ParseDeep( char* p, StrPair* parentEnd )
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 655 | {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 656 | // This is a recursive method, but thinking about it "at the current level"
|
| 657 | // it is a pretty simple flat list:
|
| 658 | // <foo/>
|
| 659 | // <!-- comment -->
|
| 660 | //
|
| 661 | // With a special case:
|
| 662 | // <foo>
|
| 663 | // </foo>
|
| 664 | // <!-- comment -->
|
| 665 | //
|
| 666 | // Where the closing element (/foo) *must* be the next thing after the opening
|
| 667 | // element, and the names must match. BUT the tricky bit is that the closing
|
| 668 | // element will be read by the child.
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 669 | //
|
| 670 | // 'endTag' is the end tag for this node, it is returned by a call to a child.
|
| 671 | // '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] | 672 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 673 | while( p && *p ) {
|
| 674 | XMLNode* node = 0;
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 675 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 676 | p = document->Identify( p, &node );
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 677 | if ( p == 0 || node == 0 ) {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 678 | break;
|
| 679 | }
|
| 680 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 681 | StrPair endTag;
|
| 682 | p = node->ParseDeep( p, &endTag );
|
| 683 | if ( !p ) {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 684 | DELETE_NODE( node );
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 685 | node = 0;
|
Lee Thomason (grinliz) | 784607f | 2012-02-24 16:23:40 -0800 | [diff] [blame] | 686 | if ( !document->Error() ) {
|
| 687 | document->SetError( ERROR_PARSING, 0, 0 );
|
| 688 | }
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 689 | break;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 690 | }
|
| 691 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 692 | // We read the end tag. Return it to the parent.
|
| 693 | if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
|
| 694 | if ( parentEnd ) {
|
| 695 | *parentEnd = ((XMLElement*)node)->value;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 696 | }
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 697 | DELETE_NODE( node );
|
| 698 | return p;
|
| 699 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 700 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 701 | // Handle an end tag returned to this level.
|
| 702 | // And handle a bunch of annoying errors.
|
| 703 | XMLElement* ele = node->ToElement();
|
| 704 | if ( ele ) {
|
| 705 | if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
|
| 706 | document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
|
| 707 | p = 0;
|
| 708 | }
|
| 709 | else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) {
|
| 710 | document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
|
| 711 | p = 0;
|
| 712 | }
|
| 713 | else if ( !endTag.Empty() ) {
|
| 714 | if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 715 | document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
|
| 716 | p = 0;
|
| 717 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 718 | }
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 719 | }
|
| 720 | if ( p == 0 ) {
|
| 721 | DELETE_NODE( node );
|
| 722 | node = 0;
|
| 723 | }
|
| 724 | if ( node ) {
|
| 725 | this->InsertEndChild( node );
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 726 | }
|
| 727 | }
|
| 728 | return 0;
|
| 729 | }
|
| 730 |
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 731 | // --------- XMLText ---------- //
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 732 | char* XMLText::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 733 | {
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 734 | const char* start = p;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 735 | if ( this->CData() ) {
|
| 736 | p = value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 737 | if ( !p ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 738 | document->SetError( ERROR_PARSING_CDATA, start, 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 739 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 740 | return p;
|
| 741 | }
|
| 742 | else {
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 743 | p = value.ParseText( p, "<", document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 744 | if ( !p ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 745 | document->SetError( ERROR_PARSING_TEXT, start, 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 746 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 747 | if ( p && *p ) {
|
| 748 | return p-1;
|
| 749 | }
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 750 | }
|
| 751 | return 0;
|
| 752 | }
|
| 753 |
|
| 754 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 755 | XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
|
| 756 | {
|
| 757 | if ( !doc ) {
|
| 758 | doc = document;
|
| 759 | }
|
| 760 | XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
|
| 761 | text->SetCData( this->CData() );
|
| 762 | return text;
|
| 763 | }
|
| 764 |
|
| 765 |
|
| 766 | bool XMLText::ShallowEqual( const XMLNode* compare ) const
|
| 767 | {
|
| 768 | return ( compare->ToText() && XMLUtil::StringEqual( compare->ToText()->Value(), Value() ));
|
| 769 | }
|
| 770 |
|
| 771 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 772 | bool XMLText::Accept( XMLVisitor* visitor ) const
|
| 773 | {
|
| 774 | return visitor->Visit( *this );
|
| 775 | }
|
| 776 |
|
| 777 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 778 | // --------- XMLComment ---------- //
|
| 779 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 780 | XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 781 | {
|
| 782 | }
|
| 783 |
|
| 784 |
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 785 | XMLComment::~XMLComment()
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 786 | {
|
Lee Thomason | d923c67 | 2012-01-23 08:44:25 -0800 | [diff] [blame] | 787 | //printf( "~XMLComment\n" );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 788 | }
|
| 789 |
|
| 790 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 791 | char* XMLComment::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 792 | {
|
| 793 | // Comment parses as text.
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 794 | const char* start = p;
|
| 795 | p = value.ParseText( p, "-->", StrPair::COMMENT );
|
| 796 | if ( p == 0 ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 797 | document->SetError( ERROR_PARSING_COMMENT, start, 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 798 | }
|
| 799 | return p;
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 800 | }
|
| 801 |
|
| 802 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 803 | XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
|
| 804 | {
|
| 805 | if ( !doc ) {
|
| 806 | doc = document;
|
| 807 | }
|
| 808 | XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
|
| 809 | return comment;
|
| 810 | }
|
| 811 |
|
| 812 |
|
| 813 | bool XMLComment::ShallowEqual( const XMLNode* compare ) const
|
| 814 | {
|
| 815 | return ( compare->ToComment() && XMLUtil::StringEqual( compare->ToComment()->Value(), Value() ));
|
| 816 | }
|
| 817 |
|
| 818 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 819 | bool XMLComment::Accept( XMLVisitor* visitor ) const
|
| 820 | {
|
| 821 | return visitor->Visit( *this );
|
| 822 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 823 |
|
| 824 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 825 | // --------- XMLDeclaration ---------- //
|
| 826 |
|
| 827 | XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
|
| 828 | {
|
| 829 | }
|
| 830 |
|
| 831 |
|
| 832 | XMLDeclaration::~XMLDeclaration()
|
| 833 | {
|
| 834 | //printf( "~XMLDeclaration\n" );
|
| 835 | }
|
| 836 |
|
| 837 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 838 | char* XMLDeclaration::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 839 | {
|
| 840 | // Declaration parses as text.
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 841 | const char* start = p;
|
| 842 | p = value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
| 843 | if ( p == 0 ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 844 | document->SetError( ERROR_PARSING_DECLARATION, start, 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 845 | }
|
| 846 | return p;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 847 | }
|
| 848 |
|
| 849 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 850 | XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
|
| 851 | {
|
| 852 | if ( !doc ) {
|
| 853 | doc = document;
|
| 854 | }
|
| 855 | XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
|
| 856 | return dec;
|
| 857 | }
|
| 858 |
|
| 859 |
|
| 860 | bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
|
| 861 | {
|
| 862 | return ( compare->ToDeclaration() && XMLUtil::StringEqual( compare->ToDeclaration()->Value(), Value() ));
|
| 863 | }
|
| 864 |
|
| 865 |
|
| 866 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 867 | bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
|
| 868 | {
|
| 869 | return visitor->Visit( *this );
|
| 870 | }
|
| 871 |
|
| 872 | // --------- XMLUnknown ---------- //
|
| 873 |
|
| 874 | XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
|
| 875 | {
|
| 876 | }
|
| 877 |
|
| 878 |
|
| 879 | XMLUnknown::~XMLUnknown()
|
| 880 | {
|
| 881 | }
|
| 882 |
|
| 883 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 884 | char* XMLUnknown::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 885 | {
|
| 886 | // Unknown parses as text.
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 887 | const char* start = p;
|
| 888 |
|
| 889 | p = value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
| 890 | if ( !p ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 891 | document->SetError( ERROR_PARSING_UNKNOWN, start, 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 892 | }
|
| 893 | return p;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 894 | }
|
| 895 |
|
| 896 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 897 | XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
|
| 898 | {
|
| 899 | if ( !doc ) {
|
| 900 | doc = document;
|
| 901 | }
|
| 902 | XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
|
| 903 | return text;
|
| 904 | }
|
| 905 |
|
| 906 |
|
| 907 | bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
|
| 908 | {
|
| 909 | return ( compare->ToUnknown() && XMLUtil::StringEqual( compare->ToUnknown()->Value(), Value() ));
|
| 910 | }
|
| 911 |
|
| 912 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 913 | bool XMLUnknown::Accept( XMLVisitor* visitor ) const
|
| 914 | {
|
| 915 | return visitor->Visit( *this );
|
| 916 | }
|
| 917 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 918 | // --------- XMLAttribute ---------- //
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 919 | char* XMLAttribute::ParseDeep( char* p, bool processEntities )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 920 | {
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 921 | p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
|
Lee Thomason | 22aead1 | 2012-01-23 13:29:35 -0800 | [diff] [blame] | 922 | if ( !p || !*p ) return 0;
|
| 923 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 924 | char endTag[2] = { *p, 0 };
|
| 925 | ++p;
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 926 | p = value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 927 | //if ( value.Empty() ) return 0;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 928 | return p;
|
| 929 | }
|
| 930 |
|
| 931 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 932 | void XMLAttribute::SetName( const char* n )
|
| 933 | {
|
| 934 | name.SetStr( n );
|
| 935 | }
|
| 936 |
|
| 937 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 938 | int XMLAttribute::QueryIntValue( int* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 939 | {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 940 | if ( TIXML_SSCANF( Value(), "%d", value ) == 1 )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 941 | return XML_NO_ERROR;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 942 | return WRONG_ATTRIBUTE_TYPE;
|
| 943 | }
|
| 944 |
|
| 945 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 946 | int XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 947 | {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 948 | if ( TIXML_SSCANF( Value(), "%u", value ) == 1 )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 949 | return XML_NO_ERROR;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 950 | return WRONG_ATTRIBUTE_TYPE;
|
| 951 | }
|
| 952 |
|
| 953 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 954 | int XMLAttribute::QueryBoolValue( bool* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 955 | {
|
| 956 | int ival = -1;
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 957 | QueryIntValue( &ival );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 958 |
|
| 959 | if ( ival > 0 || XMLUtil::StringEqual( Value(), "true" ) ) {
|
| 960 | *value = true;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 961 | return XML_NO_ERROR;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 962 | }
|
| 963 | else if ( ival == 0 || XMLUtil::StringEqual( Value(), "false" ) ) {
|
| 964 | *value = false;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 965 | return XML_NO_ERROR;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 966 | }
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 967 | return WRONG_ATTRIBUTE_TYPE;
|
| 968 | }
|
| 969 |
|
| 970 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 971 | int XMLAttribute::QueryDoubleValue( double* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 972 | {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 973 | if ( TIXML_SSCANF( Value(), "%lf", value ) == 1 )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 974 | return XML_NO_ERROR;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 975 | return WRONG_ATTRIBUTE_TYPE;
|
| 976 | }
|
| 977 |
|
| 978 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 979 | int XMLAttribute::QueryFloatValue( float* value ) const
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 980 | {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 981 | if ( TIXML_SSCANF( Value(), "%f", value ) == 1 )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 982 | return XML_NO_ERROR;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 983 | return WRONG_ATTRIBUTE_TYPE;
|
| 984 | }
|
| 985 |
|
| 986 |
|
| 987 | void XMLAttribute::SetAttribute( const char* v )
|
| 988 | {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 989 | value.SetStr( v );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 990 | }
|
| 991 |
|
| 992 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 993 | void XMLAttribute::SetAttribute( int v )
|
| 994 | {
|
| 995 | char buf[BUF_SIZE];
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 996 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v );
|
| 997 | value.SetStr( buf );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 998 | }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 999 |
|
| 1000 |
|
| 1001 | void XMLAttribute::SetAttribute( unsigned v )
|
| 1002 | {
|
| 1003 | char buf[BUF_SIZE];
|
| 1004 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%u", v );
|
| 1005 | value.SetStr( buf );
|
| 1006 | }
|
| 1007 |
|
| 1008 |
|
| 1009 | void XMLAttribute::SetAttribute( bool v )
|
| 1010 | {
|
| 1011 | char buf[BUF_SIZE];
|
| 1012 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v ? 1 : 0 );
|
| 1013 | value.SetStr( buf );
|
| 1014 | }
|
| 1015 |
|
| 1016 | void XMLAttribute::SetAttribute( double v )
|
| 1017 | {
|
| 1018 | char buf[BUF_SIZE];
|
| 1019 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
|
| 1020 | value.SetStr( buf );
|
| 1021 | }
|
| 1022 |
|
| 1023 | void XMLAttribute::SetAttribute( float v )
|
| 1024 | {
|
| 1025 | char buf[BUF_SIZE];
|
| 1026 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
|
| 1027 | value.SetStr( buf );
|
| 1028 | }
|
| 1029 |
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 1030 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1031 | // --------- XMLElement ---------- //
|
| 1032 | XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1033 | closingType( 0 ),
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1034 | rootAttribute( 0 )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1035 | {
|
| 1036 | }
|
| 1037 |
|
| 1038 |
|
| 1039 | XMLElement::~XMLElement()
|
| 1040 | {
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1041 | while( rootAttribute ) {
|
| 1042 | XMLAttribute* next = rootAttribute->next;
|
| 1043 | DELETE_ATTRIBUTE( rootAttribute );
|
| 1044 | rootAttribute = next;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1045 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1046 | }
|
| 1047 |
|
| 1048 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1049 | XMLAttribute* XMLElement::FindAttribute( const char* name )
|
| 1050 | {
|
| 1051 | XMLAttribute* a = 0;
|
| 1052 | for( a=rootAttribute; a; a = a->next ) {
|
| 1053 | if ( XMLUtil::StringEqual( a->Name(), name ) )
|
| 1054 | return a;
|
| 1055 | }
|
| 1056 | return 0;
|
| 1057 | }
|
| 1058 |
|
| 1059 |
|
| 1060 | const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
|
| 1061 | {
|
| 1062 | XMLAttribute* a = 0;
|
| 1063 | for( a=rootAttribute; a; a = a->next ) {
|
| 1064 | if ( XMLUtil::StringEqual( a->Name(), name ) )
|
| 1065 | return a;
|
| 1066 | }
|
| 1067 | return 0;
|
| 1068 | }
|
| 1069 |
|
| 1070 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1071 | const char* XMLElement::GetText() const
|
| 1072 | {
|
| 1073 | if ( FirstChild() && FirstChild()->ToText() ) {
|
| 1074 | return FirstChild()->ToText()->Value();
|
| 1075 | }
|
| 1076 | return 0;
|
| 1077 | }
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1081 | XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
|
| 1082 | {
|
| 1083 | XMLAttribute* attrib = FindAttribute( name );
|
| 1084 | if ( !attrib ) {
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 1085 | attrib = new (document->attributePool.Alloc() ) XMLAttribute();
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1086 | attrib->memPool = &document->attributePool;
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1087 | LinkAttribute( attrib );
|
| 1088 | attrib->SetName( name );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 1089 | }
|
| 1090 | return attrib;
|
| 1091 | }
|
| 1092 |
|
| 1093 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1094 | void XMLElement::LinkAttribute( XMLAttribute* attrib )
|
| 1095 | {
|
| 1096 | if ( rootAttribute ) {
|
| 1097 | XMLAttribute* end = rootAttribute;
|
| 1098 | while ( end->next )
|
| 1099 | end = end->next;
|
| 1100 | end->next = attrib;
|
| 1101 | }
|
| 1102 | else {
|
| 1103 | rootAttribute = attrib;
|
| 1104 | }
|
| 1105 | }
|
| 1106 |
|
| 1107 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1108 | void XMLElement::DeleteAttribute( const char* name )
|
| 1109 | {
|
| 1110 | XMLAttribute* prev = 0;
|
| 1111 | for( XMLAttribute* a=rootAttribute; a; a=a->next ) {
|
| 1112 | if ( XMLUtil::StringEqual( name, a->Name() ) ) {
|
| 1113 | if ( prev ) {
|
| 1114 | prev->next = a->next;
|
| 1115 | }
|
| 1116 | else {
|
| 1117 | rootAttribute = a->next;
|
| 1118 | }
|
| 1119 | DELETE_ATTRIBUTE( a );
|
| 1120 | break;
|
| 1121 | }
|
| 1122 | prev = a;
|
| 1123 | }
|
| 1124 | }
|
| 1125 |
|
| 1126 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1127 | char* XMLElement::ParseAttributes( char* p )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1128 | {
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1129 | const char* start = p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1130 |
|
| 1131 | // Read the attributes.
|
| 1132 | while( p ) {
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1133 | p = XMLUtil::SkipWhiteSpace( p );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1134 | if ( !p || !(*p) ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1135 | document->SetError( ERROR_PARSING_ELEMENT, start, Name() );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1136 | return 0;
|
| 1137 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1138 |
|
| 1139 | // attribute.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1140 | if ( XMLUtil::IsAlpha( *p ) ) {
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 1141 | XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute();
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 1142 | attrib->memPool = &document->attributePool;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1143 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1144 | p = attrib->ParseDeep( p, document->ProcessEntities() );
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 1145 | if ( !p || Attribute( attrib->Name() ) ) {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 1146 | DELETE_ATTRIBUTE( attrib );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1147 | document->SetError( ERROR_PARSING_ATTRIBUTE, start, p );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1148 | return 0;
|
| 1149 | }
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1150 | LinkAttribute( attrib );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1151 | }
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 1152 | // end of the tag
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1153 | else if ( *p == '/' && *(p+1) == '>' ) {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1154 | closingType = CLOSED;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1155 | return p+2; // done; sealed element.
|
| 1156 | }
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 1157 | // end of the tag
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1158 | else if ( *p == '>' ) {
|
| 1159 | ++p;
|
| 1160 | break;
|
| 1161 | }
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 1162 | else {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1163 | document->SetError( ERROR_PARSING_ELEMENT, start, p );
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 1164 | return 0;
|
| 1165 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1166 | }
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1167 | return p;
|
| 1168 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1169 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1170 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1171 | //
|
| 1172 | // <ele></ele>
|
| 1173 | // <ele>foo<b>bar</b></ele>
|
| 1174 | //
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 1175 | char* XMLElement::ParseDeep( char* p, StrPair* strPair )
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1176 | {
|
| 1177 | // Read the element name.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1178 | p = XMLUtil::SkipWhiteSpace( p );
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1179 | if ( !p ) return 0;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1180 |
|
| 1181 | // The closing element is the </element> form. It is
|
| 1182 | // parsed just like a regular element then deleted from
|
| 1183 | // the DOM.
|
| 1184 | if ( *p == '/' ) {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1185 | closingType = CLOSING;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1186 | ++p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1187 | }
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1188 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1189 | p = value.ParseName( p );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1190 | if ( value.Empty() ) return 0;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1191 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1192 | p = ParseAttributes( p );
|
| 1193 | if ( !p || !*p || closingType )
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1194 | return p;
|
| 1195 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 1196 | p = XMLNode::ParseDeep( p, strPair );
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1197 | return p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1198 | }
|
| 1199 |
|
| 1200 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1201 |
|
| 1202 | XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const
|
| 1203 | {
|
| 1204 | if ( !doc ) {
|
| 1205 | doc = document;
|
| 1206 | }
|
| 1207 | XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?
|
| 1208 | for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {
|
| 1209 | element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?
|
| 1210 | }
|
| 1211 | return element;
|
| 1212 | }
|
| 1213 |
|
| 1214 |
|
| 1215 | bool XMLElement::ShallowEqual( const XMLNode* compare ) const
|
| 1216 | {
|
| 1217 | const XMLElement* other = compare->ToElement();
|
| 1218 | if ( other && XMLUtil::StringEqual( other->Value(), Value() )) {
|
| 1219 |
|
| 1220 | const XMLAttribute* a=FirstAttribute();
|
| 1221 | const XMLAttribute* b=other->FirstAttribute();
|
| 1222 |
|
| 1223 | while ( a && b ) {
|
| 1224 | if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {
|
| 1225 | return false;
|
| 1226 | }
|
| 1227 | }
|
| 1228 | if ( a || b ) {
|
| 1229 | // different count
|
| 1230 | return false;
|
| 1231 | }
|
| 1232 | return true;
|
| 1233 | }
|
| 1234 | return false;
|
| 1235 | }
|
| 1236 |
|
| 1237 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1238 | bool XMLElement::Accept( XMLVisitor* visitor ) const
|
| 1239 | {
|
| 1240 | if ( visitor->VisitEnter( *this, rootAttribute ) )
|
| 1241 | {
|
| 1242 | for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
|
| 1243 | {
|
| 1244 | if ( !node->Accept( visitor ) )
|
| 1245 | break;
|
| 1246 | }
|
| 1247 | }
|
| 1248 | return visitor->VisitExit( *this );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1249 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1250 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1251 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1252 | // --------- XMLDocument ----------- //
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1253 | XMLDocument::XMLDocument( bool _processEntities ) :
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1254 | XMLNode( 0 ),
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1255 | writeBOM( false ),
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1256 | processEntities( _processEntities ),
|
| 1257 | errorID( 0 ),
|
| 1258 | errorStr1( 0 ),
|
| 1259 | errorStr2( 0 ),
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1260 | charBuffer( 0 )
|
| 1261 | {
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1262 | document = this; // avoid warning about 'this' in initializer list
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1263 | }
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1264 |
|
| 1265 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1266 | XMLDocument::~XMLDocument()
|
| 1267 | {
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1268 | DeleteChildren();
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1269 | delete [] charBuffer;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1270 |
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 1271 | #if 0
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 1272 | textPool.Trace( "text" );
|
| 1273 | elementPool.Trace( "element" );
|
| 1274 | commentPool.Trace( "comment" );
|
| 1275 | attributePool.Trace( "attribute" );
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 1276 | #endif
|
| 1277 |
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 1278 | TIXMLASSERT( textPool.CurrentAllocs() == 0 );
|
| 1279 | TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
|
| 1280 | TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
|
| 1281 | TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1282 | }
|
| 1283 |
|
| 1284 |
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1285 | void XMLDocument::InitDocument()
|
| 1286 | {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1287 | errorID = XML_NO_ERROR;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1288 | errorStr1 = 0;
|
| 1289 | errorStr2 = 0;
|
| 1290 |
|
| 1291 | delete [] charBuffer;
|
| 1292 | charBuffer = 0;
|
| 1293 |
|
| 1294 | }
|
| 1295 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1296 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1297 | XMLElement* XMLDocument::NewElement( const char* name )
|
| 1298 | {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1299 | XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
|
| 1300 | ele->memPool = &elementPool;
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1301 | ele->SetName( name );
|
| 1302 | return ele;
|
| 1303 | }
|
| 1304 |
|
| 1305 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1306 | XMLComment* XMLDocument::NewComment( const char* str )
|
| 1307 | {
|
| 1308 | XMLComment* comment = new (commentPool.Alloc()) XMLComment( this );
|
| 1309 | comment->memPool = &commentPool;
|
| 1310 | comment->SetValue( str );
|
| 1311 | return comment;
|
| 1312 | }
|
| 1313 |
|
| 1314 |
|
| 1315 | XMLText* XMLDocument::NewText( const char* str )
|
| 1316 | {
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1317 | XMLText* text = new (textPool.Alloc()) XMLText( this );
|
| 1318 | text->memPool = &textPool;
|
| 1319 | text->SetValue( str );
|
| 1320 | return text;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1321 | }
|
| 1322 |
|
| 1323 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1324 | XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
|
| 1325 | {
|
| 1326 | XMLDeclaration* dec = new (commentPool.Alloc()) XMLDeclaration( this );
|
| 1327 | dec->memPool = &commentPool;
|
| 1328 | dec->SetValue( str );
|
| 1329 | return dec;
|
| 1330 | }
|
| 1331 |
|
| 1332 |
|
| 1333 | XMLUnknown* XMLDocument::NewUnknown( const char* str )
|
| 1334 | {
|
| 1335 | XMLUnknown* unk = new (commentPool.Alloc()) XMLUnknown( this );
|
| 1336 | unk->memPool = &commentPool;
|
| 1337 | unk->SetValue( str );
|
| 1338 | return unk;
|
| 1339 | }
|
| 1340 |
|
| 1341 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1342 | int XMLDocument::LoadFile( const char* filename )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1343 | {
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1344 | DeleteChildren();
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1345 | InitDocument();
|
| 1346 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1347 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 1348 | #pragma warning ( push )
|
| 1349 | #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1350 | #endif
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1351 | FILE* fp = fopen( filename, "rb" );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1352 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 1353 | #pragma warning ( pop )
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1354 | #endif
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1355 | if ( !fp ) {
|
| 1356 | SetError( ERROR_FILE_NOT_FOUND, filename, 0 );
|
| 1357 | return errorID;
|
| 1358 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1359 | LoadFile( fp );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1360 | fclose( fp );
|
| 1361 | return errorID;
|
| 1362 | }
|
| 1363 |
|
| 1364 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1365 | int XMLDocument::LoadFile( FILE* fp )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1366 | {
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1367 | DeleteChildren();
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1368 | InitDocument();
|
| 1369 |
|
| 1370 | fseek( fp, 0, SEEK_END );
|
| 1371 | unsigned size = ftell( fp );
|
| 1372 | fseek( fp, 0, SEEK_SET );
|
| 1373 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1374 | if ( size == 0 ) {
|
| 1375 | return errorID;
|
| 1376 | }
|
| 1377 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1378 | charBuffer = new char[size+1];
|
| 1379 | fread( charBuffer, size, 1, fp );
|
| 1380 | charBuffer[size] = 0;
|
| 1381 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1382 | const char* p = charBuffer;
|
| 1383 | p = XMLUtil::SkipWhiteSpace( p );
|
| 1384 | p = XMLUtil::ReadBOM( p, &writeBOM );
|
| 1385 | if ( !p || !*p ) {
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 1386 | SetError( ERROR_EMPTY_DOCUMENT, 0, 0 );
|
| 1387 | return errorID;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1388 | }
|
| 1389 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 1390 | ParseDeep( charBuffer + (p-charBuffer), 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1391 | return errorID;
|
| 1392 | }
|
| 1393 |
|
| 1394 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1395 | void XMLDocument::SaveFile( const char* filename )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1396 | {
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1397 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 1398 | #pragma warning ( push )
|
| 1399 | #pragma warning ( disable : 4996 ) // Fail to see a compelling reason why this should be deprecated.
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1400 | #endif
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1401 | FILE* fp = fopen( filename, "w" );
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1402 | #if defined(_MSC_VER)
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 1403 | #pragma warning ( pop )
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1404 | #endif
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1405 | XMLPrinter stream( fp );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1406 | Print( &stream );
|
| 1407 | fclose( fp );
|
| 1408 | }
|
| 1409 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1410 |
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 1411 | int XMLDocument::Parse( const char* p )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1412 | {
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1413 | DeleteChildren();
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1414 | InitDocument();
|
| 1415 |
|
| 1416 | if ( !p || !*p ) {
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 1417 | SetError( ERROR_EMPTY_DOCUMENT, 0, 0 );
|
| 1418 | return errorID;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1419 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1420 | p = XMLUtil::SkipWhiteSpace( p );
|
| 1421 | p = XMLUtil::ReadBOM( p, &writeBOM );
|
| 1422 | if ( !p || !*p ) {
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 1423 | SetError( ERROR_EMPTY_DOCUMENT, 0, 0 );
|
| 1424 | return errorID;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1425 | }
|
| 1426 |
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1427 | size_t len = strlen( p );
|
| 1428 | charBuffer = new char[ len+1 ];
|
| 1429 | memcpy( charBuffer, p, len+1 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1430 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1431 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame] | 1432 | ParseDeep( charBuffer, 0 );
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 1433 | return errorID;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1434 | }
|
| 1435 |
|
| 1436 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1437 | void XMLDocument::Print( XMLPrinter* streamer )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1438 | {
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1439 | XMLPrinter stdStreamer( stdout );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1440 | if ( !streamer )
|
| 1441 | streamer = &stdStreamer;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1442 | Accept( streamer );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1443 | }
|
| 1444 |
|
| 1445 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1446 | void XMLDocument::SetError( int error, const char* str1, const char* str2 )
|
| 1447 | {
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1448 | errorID = error;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1449 | errorStr1 = str1;
|
| 1450 | errorStr2 = str2;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1451 | }
|
| 1452 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1453 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1454 | void XMLDocument::PrintError() const
|
| 1455 | {
|
| 1456 | if ( errorID ) {
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 1457 | static const int LEN = 20;
|
| 1458 | char buf1[LEN] = { 0 };
|
| 1459 | char buf2[LEN] = { 0 };
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1460 |
|
| 1461 | if ( errorStr1 ) {
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 1462 | TIXML_SNPRINTF( buf1, LEN, "%s", errorStr1 );
|
| 1463 | buf1[LEN-1] = 0;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1464 | }
|
| 1465 | if ( errorStr2 ) {
|
Lee Thomason (grinliz) | 5ce4d97 | 2012-02-26 21:14:23 -0800 | [diff] [blame] | 1466 | TIXML_SNPRINTF( buf2, LEN, "%s", errorStr2 );
|
| 1467 | buf2[LEN-1] = 0;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1468 | }
|
| 1469 |
|
| 1470 | printf( "XMLDocument error id=%d str1=%s str2=%s\n",
|
| 1471 | errorID, buf1, buf2 );
|
| 1472 | }
|
| 1473 | }
|
| 1474 |
|
| 1475 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1476 | XMLPrinter::XMLPrinter( FILE* file ) :
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1477 | elementJustOpened( false ),
|
| 1478 | firstElement( true ),
|
| 1479 | fp( file ),
|
| 1480 | depth( 0 ),
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1481 | textDepth( -1 ),
|
| 1482 | processEntities( true )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1483 | {
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1484 | for( int i=0; i<ENTITY_RANGE; ++i ) {
|
| 1485 | entityFlag[i] = false;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1486 | restrictedEntityFlag[i] = false;
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1487 | }
|
| 1488 | for( int i=0; i<NUM_ENTITIES; ++i ) {
|
| 1489 | TIXMLASSERT( entities[i].value < ENTITY_RANGE );
|
| 1490 | if ( entities[i].value < ENTITY_RANGE ) {
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 1491 | entityFlag[ (int)entities[i].value ] = true;
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1492 | }
|
| 1493 | }
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 1494 | restrictedEntityFlag[(int)'&'] = true;
|
| 1495 | restrictedEntityFlag[(int)'<'] = true;
|
| 1496 | restrictedEntityFlag[(int)'>'] = true; // not required, but consistency is nice
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1497 | buffer.Push( 0 );
|
| 1498 | }
|
| 1499 |
|
| 1500 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1501 | void XMLPrinter::Print( const char* format, ... )
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1502 | {
|
| 1503 | va_list va;
|
| 1504 | va_start( va, format );
|
| 1505 |
|
| 1506 | if ( fp ) {
|
| 1507 | vfprintf( fp, format, va );
|
| 1508 | }
|
| 1509 | else {
|
| 1510 | // This seems brutally complex. Haven't figured out a better
|
| 1511 | // way on windows.
|
| 1512 | #ifdef _MSC_VER
|
| 1513 | int len = -1;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1514 | int expand = 1000;
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1515 | while ( len < 0 ) {
|
| 1516 | len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), accumulator.Capacity()-1, format, va );
|
| 1517 | if ( len < 0 ) {
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1518 | accumulator.PushArr( expand );
|
| 1519 | expand *= 3/2;
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1520 | }
|
| 1521 | }
|
| 1522 | char* p = buffer.PushArr( len ) - 1;
|
| 1523 | memcpy( p, accumulator.Mem(), len+1 );
|
| 1524 | #else
|
| 1525 | int len = vsnprintf( 0, 0, format, va );
|
| 1526 | char* p = buffer.PushArr( len ) - 1;
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 1527 | vsnprintf( p, len+1, format, va );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1528 | #endif
|
| 1529 | }
|
| 1530 | va_end( va );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1531 | }
|
| 1532 |
|
| 1533 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1534 | void XMLPrinter::PrintSpace( int depth )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1535 | {
|
| 1536 | for( int i=0; i<depth; ++i ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1537 | Print( " " );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1538 | }
|
| 1539 | }
|
| 1540 |
|
| 1541 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1542 | void XMLPrinter::PrintString( const char* p, bool restricted )
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1543 | {
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1544 | // Look for runs of bytes between entities to print.
|
| 1545 | const char* q = p;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1546 | const bool* flag = restricted ? restrictedEntityFlag : entityFlag;
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1547 |
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1548 | if ( processEntities ) {
|
| 1549 | while ( *q ) {
|
| 1550 | // Remember, char is sometimes signed. (How many times has that bitten me?)
|
| 1551 | if ( *q > 0 && *q < ENTITY_RANGE ) {
|
| 1552 | // Check for entities. If one is found, flush
|
| 1553 | // the stream up until the entity, write the
|
| 1554 | // entity, and keep looking.
|
| 1555 | if ( flag[*q] ) {
|
| 1556 | while ( p < q ) {
|
| 1557 | Print( "%c", *p );
|
| 1558 | ++p;
|
| 1559 | }
|
| 1560 | for( int i=0; i<NUM_ENTITIES; ++i ) {
|
| 1561 | if ( entities[i].value == *q ) {
|
| 1562 | Print( "&%s;", entities[i].pattern );
|
| 1563 | break;
|
| 1564 | }
|
| 1565 | }
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1566 | ++p;
|
| 1567 | }
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1568 | }
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1569 | ++q;
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1570 | }
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1571 | }
|
| 1572 | // Flush the remaining string. This will be the entire
|
| 1573 | // string if an entity wasn't found.
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1574 | if ( !processEntities || (q-p > 0) ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1575 | Print( "%s", p );
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1576 | }
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1577 | }
|
| 1578 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1579 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1580 | void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1581 | {
|
| 1582 | static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
|
| 1583 | if ( writeBOM ) {
|
| 1584 | Print( "%s", bom );
|
| 1585 | }
|
| 1586 | if ( writeDec ) {
|
| 1587 | PushDeclaration( "xml version=\"1.0\"" );
|
| 1588 | }
|
| 1589 | }
|
| 1590 |
|
| 1591 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1592 | void XMLPrinter::OpenElement( const char* name )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1593 | {
|
| 1594 | if ( elementJustOpened ) {
|
| 1595 | SealElement();
|
| 1596 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1597 | stack.Push( name );
|
| 1598 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1599 | if ( textDepth < 0 && !firstElement ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1600 | Print( "\n" );
|
Lee Thomason | 24767b0 | 2012-01-25 17:16:23 -0800 | [diff] [blame] | 1601 | PrintSpace( depth );
|
| 1602 | }
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1603 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1604 | Print( "<%s", name );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1605 | elementJustOpened = true;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1606 | firstElement = false;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1607 | ++depth;
|
| 1608 | }
|
| 1609 |
|
| 1610 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1611 | void XMLPrinter::PushAttribute( const char* name, const char* value )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1612 | {
|
| 1613 | TIXMLASSERT( elementJustOpened );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1614 | Print( " %s=\"", name );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1615 | PrintString( value, false );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1616 | Print( "\"" );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1617 | }
|
| 1618 |
|
| 1619 |
|
Lee Thomason | 7d00b9a | 2012-02-27 17:54:22 -0800 | [diff] [blame] | 1620 | void XMLPrinter::PushAttribute( const char* name, int v )
|
| 1621 | {
|
| 1622 | char buf[BUF_SIZE];
|
| 1623 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v );
|
| 1624 | PushAttribute( name, buf );
|
| 1625 | }
|
| 1626 |
|
| 1627 |
|
| 1628 | void XMLPrinter::PushAttribute( const char* name, unsigned v )
|
| 1629 | {
|
| 1630 | char buf[BUF_SIZE];
|
| 1631 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%u", v );
|
| 1632 | PushAttribute( name, buf );
|
| 1633 | }
|
| 1634 |
|
| 1635 |
|
| 1636 | void XMLPrinter::PushAttribute( const char* name, bool v )
|
| 1637 | {
|
| 1638 | char buf[BUF_SIZE];
|
| 1639 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v ? 1 : 0 );
|
| 1640 | PushAttribute( name, buf );
|
| 1641 | }
|
| 1642 |
|
| 1643 |
|
| 1644 | void XMLPrinter::PushAttribute( const char* name, double v )
|
| 1645 | {
|
| 1646 | char buf[BUF_SIZE];
|
| 1647 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
|
| 1648 | PushAttribute( name, buf );
|
| 1649 | }
|
| 1650 |
|
| 1651 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1652 | void XMLPrinter::CloseElement()
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1653 | {
|
| 1654 | --depth;
|
| 1655 | const char* name = stack.Pop();
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1656 |
|
| 1657 | if ( elementJustOpened ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1658 | Print( "/>" );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1659 | }
|
| 1660 | else {
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1661 | if ( textDepth < 0 ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1662 | Print( "\n" );
|
Lee Thomason | 24767b0 | 2012-01-25 17:16:23 -0800 | [diff] [blame] | 1663 | PrintSpace( depth );
|
| 1664 | }
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1665 | Print( "</%s>", name );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1666 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1667 |
|
| 1668 | if ( textDepth == depth )
|
| 1669 | textDepth = -1;
|
| 1670 | if ( depth == 0 )
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1671 | Print( "\n" );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1672 | elementJustOpened = false;
|
| 1673 | }
|
| 1674 |
|
| 1675 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1676 | void XMLPrinter::SealElement()
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1677 | {
|
| 1678 | elementJustOpened = false;
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1679 | Print( ">" );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1680 | }
|
| 1681 |
|
| 1682 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1683 | void XMLPrinter::PushText( const char* text, bool cdata )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1684 | {
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1685 | textDepth = depth-1;
|
| 1686 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1687 | if ( elementJustOpened ) {
|
| 1688 | SealElement();
|
| 1689 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1690 | if ( cdata ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1691 | Print( "<![CDATA[" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1692 | Print( "%s", text );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1693 | Print( "]]>" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1694 | }
|
| 1695 | else {
|
| 1696 | PrintString( text, true );
|
| 1697 | }
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1698 | }
|
| 1699 |
|
| 1700 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1701 | void XMLPrinter::PushComment( const char* comment )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1702 | {
|
| 1703 | if ( elementJustOpened ) {
|
| 1704 | SealElement();
|
| 1705 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1706 | if ( textDepth < 0 && !firstElement ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1707 | Print( "\n" );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1708 | PrintSpace( depth );
|
| 1709 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1710 | firstElement = false;
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1711 | Print( "<!--%s-->", comment );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1712 | }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1713 |
|
| 1714 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1715 | void XMLPrinter::PushDeclaration( const char* value )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1716 | {
|
| 1717 | if ( elementJustOpened ) {
|
| 1718 | SealElement();
|
| 1719 | }
|
| 1720 | if ( textDepth < 0 && !firstElement) {
|
| 1721 | Print( "\n" );
|
| 1722 | PrintSpace( depth );
|
| 1723 | }
|
| 1724 | firstElement = false;
|
| 1725 | Print( "<?%s?>", value );
|
| 1726 | }
|
| 1727 |
|
| 1728 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1729 | void XMLPrinter::PushUnknown( const char* value )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1730 | {
|
| 1731 | if ( elementJustOpened ) {
|
| 1732 | SealElement();
|
| 1733 | }
|
| 1734 | if ( textDepth < 0 && !firstElement ) {
|
| 1735 | Print( "\n" );
|
| 1736 | PrintSpace( depth );
|
| 1737 | }
|
| 1738 | firstElement = false;
|
| 1739 | Print( "<!%s>", value );
|
| 1740 | }
|
| 1741 |
|
| 1742 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1743 | bool XMLPrinter::VisitEnter( const XMLDocument& doc )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1744 | {
|
Lee Thomason | 6f381b7 | 2012-03-02 12:59:39 -0800 | [diff] [blame] | 1745 | processEntities = doc.ProcessEntities();
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1746 | if ( doc.HasBOM() ) {
|
| 1747 | PushHeader( true, false );
|
| 1748 | }
|
| 1749 | return true;
|
| 1750 | }
|
| 1751 |
|
| 1752 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1753 | bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1754 | {
|
| 1755 | OpenElement( element.Name() );
|
| 1756 | while ( attribute ) {
|
| 1757 | PushAttribute( attribute->Name(), attribute->Value() );
|
| 1758 | attribute = attribute->Next();
|
| 1759 | }
|
| 1760 | return true;
|
| 1761 | }
|
| 1762 |
|
| 1763 |
|
Lee Thomason (grinliz) | 9b093cc | 2012-02-25 21:30:18 -0800 | [diff] [blame] | 1764 | bool XMLPrinter::VisitExit( const XMLElement& )
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1765 | {
|
| 1766 | CloseElement();
|
| 1767 | return true;
|
| 1768 | }
|
| 1769 |
|
| 1770 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1771 | bool XMLPrinter::Visit( const XMLText& text )
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1772 | {
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 1773 | PushText( text.Value(), text.CData() );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1774 | return true;
|
| 1775 | }
|
| 1776 |
|
| 1777 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1778 | bool XMLPrinter::Visit( const XMLComment& comment )
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1779 | {
|
| 1780 | PushComment( comment.Value() );
|
| 1781 | return true;
|
| 1782 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1783 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1784 | bool XMLPrinter::Visit( const XMLDeclaration& declaration )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1785 | {
|
| 1786 | PushDeclaration( declaration.Value() );
|
| 1787 | return true;
|
| 1788 | }
|
| 1789 |
|
| 1790 |
|
Lee Thomason (grinliz) | 2a1cd27 | 2012-02-24 17:37:53 -0800 | [diff] [blame] | 1791 | bool XMLPrinter::Visit( const XMLUnknown& unknown )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1792 | {
|
| 1793 | PushUnknown( unknown.Value() );
|
| 1794 | return true;
|
| 1795 | }
|