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