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 | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 451 | ClearChildren();
|
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 | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 467 | void XMLNode::ClearChildren()
|
| 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;
|
| 632 | break;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 633 | }
|
| 634 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame^] | 635 | // We read the end tag. Return it to the parent.
|
| 636 | if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) {
|
| 637 | if ( parentEnd ) {
|
| 638 | *parentEnd = ((XMLElement*)node)->value;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 639 | }
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame^] | 640 | DELETE_NODE( node );
|
| 641 | return p;
|
| 642 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 643 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame^] | 644 | // Handle an end tag returned to this level.
|
| 645 | // And handle a bunch of annoying errors.
|
| 646 | XMLElement* ele = node->ToElement();
|
| 647 | if ( ele ) {
|
| 648 | if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
|
| 649 | document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
|
| 650 | p = 0;
|
| 651 | }
|
| 652 | else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) {
|
| 653 | document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
|
| 654 | p = 0;
|
| 655 | }
|
| 656 | else if ( !endTag.Empty() ) {
|
| 657 | if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 658 | document->SetError( ERROR_MISMATCHED_ELEMENT, node->Value(), 0 );
|
| 659 | p = 0;
|
| 660 | }
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 661 | }
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame^] | 662 | }
|
| 663 | if ( p == 0 ) {
|
| 664 | DELETE_NODE( node );
|
| 665 | node = 0;
|
| 666 | }
|
| 667 | if ( node ) {
|
| 668 | this->InsertEndChild( node );
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 669 | }
|
| 670 | }
|
| 671 | return 0;
|
| 672 | }
|
| 673 |
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 674 | // --------- XMLText ---------- //
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame^] | 675 | char* XMLText::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 676 | {
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 677 | const char* start = p;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 678 | if ( this->CData() ) {
|
| 679 | p = value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 680 | if ( !p ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 681 | document->SetError( ERROR_PARSING_CDATA, start, 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 682 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 683 | return p;
|
| 684 | }
|
| 685 | else {
|
| 686 | p = value.ParseText( p, "<", StrPair::TEXT_ELEMENT );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 687 | if ( !p ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 688 | document->SetError( ERROR_PARSING_TEXT, start, 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 689 | }
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 690 | if ( p && *p ) {
|
| 691 | return p-1;
|
| 692 | }
|
Lee Thomason | 5492a1c | 2012-01-23 15:32:10 -0800 | [diff] [blame] | 693 | }
|
| 694 | return 0;
|
| 695 | }
|
| 696 |
|
| 697 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 698 | bool XMLText::Accept( XMLVisitor* visitor ) const
|
| 699 | {
|
| 700 | return visitor->Visit( *this );
|
| 701 | }
|
| 702 |
|
| 703 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 704 | // --------- XMLComment ---------- //
|
| 705 |
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 706 | XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 707 | {
|
| 708 | }
|
| 709 |
|
| 710 |
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame] | 711 | XMLComment::~XMLComment()
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 712 | {
|
Lee Thomason | d923c67 | 2012-01-23 08:44:25 -0800 | [diff] [blame] | 713 | //printf( "~XMLComment\n" );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 714 | }
|
| 715 |
|
| 716 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame^] | 717 | char* XMLComment::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 718 | {
|
| 719 | // Comment parses as text.
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 720 | const char* start = p;
|
| 721 | p = value.ParseText( p, "-->", StrPair::COMMENT );
|
| 722 | if ( p == 0 ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 723 | document->SetError( ERROR_PARSING_COMMENT, start, 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 724 | }
|
| 725 | return p;
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 726 | }
|
| 727 |
|
| 728 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 729 | bool XMLComment::Accept( XMLVisitor* visitor ) const
|
| 730 | {
|
| 731 | return visitor->Visit( *this );
|
| 732 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 733 |
|
| 734 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 735 | // --------- XMLDeclaration ---------- //
|
| 736 |
|
| 737 | XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
|
| 738 | {
|
| 739 | }
|
| 740 |
|
| 741 |
|
| 742 | XMLDeclaration::~XMLDeclaration()
|
| 743 | {
|
| 744 | //printf( "~XMLDeclaration\n" );
|
| 745 | }
|
| 746 |
|
| 747 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame^] | 748 | char* XMLDeclaration::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 749 | {
|
| 750 | // Declaration parses as text.
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 751 | const char* start = p;
|
| 752 | p = value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
| 753 | if ( p == 0 ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 754 | document->SetError( ERROR_PARSING_DECLARATION, start, 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 755 | }
|
| 756 | return p;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 757 | }
|
| 758 |
|
| 759 |
|
| 760 | bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
|
| 761 | {
|
| 762 | return visitor->Visit( *this );
|
| 763 | }
|
| 764 |
|
| 765 | // --------- XMLUnknown ---------- //
|
| 766 |
|
| 767 | XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
|
| 768 | {
|
| 769 | }
|
| 770 |
|
| 771 |
|
| 772 | XMLUnknown::~XMLUnknown()
|
| 773 | {
|
| 774 | }
|
| 775 |
|
| 776 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame^] | 777 | char* XMLUnknown::ParseDeep( char* p, StrPair* )
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 778 | {
|
| 779 | // Unknown parses as text.
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 780 | const char* start = p;
|
| 781 |
|
| 782 | p = value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION );
|
| 783 | if ( !p ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 784 | document->SetError( ERROR_PARSING_UNKNOWN, start, 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 785 | }
|
| 786 | return p;
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 787 | }
|
| 788 |
|
| 789 |
|
| 790 | bool XMLUnknown::Accept( XMLVisitor* visitor ) const
|
| 791 | {
|
| 792 | return visitor->Visit( *this );
|
| 793 | }
|
| 794 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 795 | // --------- XMLAttribute ---------- //
|
| 796 | char* XMLAttribute::ParseDeep( char* p )
|
| 797 | {
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 798 | p = name.ParseText( p, "=", StrPair::ATTRIBUTE_NAME );
|
Lee Thomason | 22aead1 | 2012-01-23 13:29:35 -0800 | [diff] [blame] | 799 | if ( !p || !*p ) return 0;
|
| 800 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 801 | char endTag[2] = { *p, 0 };
|
| 802 | ++p;
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 803 | p = value.ParseText( p, endTag, StrPair::ATTRIBUTE_VALUE );
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 804 | //if ( value.Empty() ) return 0;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 805 | return p;
|
| 806 | }
|
| 807 |
|
| 808 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 809 | void XMLAttribute::SetName( const char* n )
|
| 810 | {
|
| 811 | name.SetStr( n );
|
| 812 | }
|
| 813 |
|
| 814 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 815 | int XMLAttribute::QueryIntAttribute( int* value ) const
|
| 816 | {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 817 | if ( TIXML_SSCANF( Value(), "%d", value ) == 1 )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 818 | return XML_NO_ERROR;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 819 | return WRONG_ATTRIBUTE_TYPE;
|
| 820 | }
|
| 821 |
|
| 822 |
|
| 823 | int XMLAttribute::QueryUnsignedAttribute( unsigned int* value ) const
|
| 824 | {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 825 | if ( TIXML_SSCANF( Value(), "%u", value ) == 1 )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 826 | return XML_NO_ERROR;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 827 | return WRONG_ATTRIBUTE_TYPE;
|
| 828 | }
|
| 829 |
|
| 830 |
|
| 831 | int XMLAttribute::QueryBoolAttribute( bool* value ) const
|
| 832 | {
|
| 833 | int ival = -1;
|
| 834 | QueryIntAttribute( &ival );
|
| 835 |
|
| 836 | if ( ival > 0 || XMLUtil::StringEqual( Value(), "true" ) ) {
|
| 837 | *value = true;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 838 | return XML_NO_ERROR;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 839 | }
|
| 840 | else if ( ival == 0 || XMLUtil::StringEqual( Value(), "false" ) ) {
|
| 841 | *value = false;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 842 | return XML_NO_ERROR;
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 843 | }
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 844 | return WRONG_ATTRIBUTE_TYPE;
|
| 845 | }
|
| 846 |
|
| 847 |
|
| 848 | int XMLAttribute::QueryDoubleAttribute( double* value ) const
|
| 849 | {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 850 | if ( TIXML_SSCANF( Value(), "%lf", value ) == 1 )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 851 | return XML_NO_ERROR;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 852 | return WRONG_ATTRIBUTE_TYPE;
|
| 853 | }
|
| 854 |
|
| 855 |
|
| 856 | int XMLAttribute::QueryFloatAttribute( float* value ) const
|
| 857 | {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 858 | if ( TIXML_SSCANF( Value(), "%f", value ) == 1 )
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 859 | return XML_NO_ERROR;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 860 | return WRONG_ATTRIBUTE_TYPE;
|
| 861 | }
|
| 862 |
|
| 863 |
|
| 864 | void XMLAttribute::SetAttribute( const char* v )
|
| 865 | {
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 866 | value.SetStr( v );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 867 | }
|
| 868 |
|
| 869 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 870 | void XMLAttribute::SetAttribute( int v )
|
| 871 | {
|
| 872 | char buf[BUF_SIZE];
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 873 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v );
|
| 874 | value.SetStr( buf );
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 875 | }
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 876 |
|
| 877 |
|
| 878 | void XMLAttribute::SetAttribute( unsigned v )
|
| 879 | {
|
| 880 | char buf[BUF_SIZE];
|
| 881 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%u", v );
|
| 882 | value.SetStr( buf );
|
| 883 | }
|
| 884 |
|
| 885 |
|
| 886 | void XMLAttribute::SetAttribute( bool v )
|
| 887 | {
|
| 888 | char buf[BUF_SIZE];
|
| 889 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%d", v ? 1 : 0 );
|
| 890 | value.SetStr( buf );
|
| 891 | }
|
| 892 |
|
| 893 | void XMLAttribute::SetAttribute( double v )
|
| 894 | {
|
| 895 | char buf[BUF_SIZE];
|
| 896 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
|
| 897 | value.SetStr( buf );
|
| 898 | }
|
| 899 |
|
| 900 | void XMLAttribute::SetAttribute( float v )
|
| 901 | {
|
| 902 | char buf[BUF_SIZE];
|
| 903 | TIXML_SNPRINTF( buf, BUF_SIZE-1, "%f", v );
|
| 904 | value.SetStr( buf );
|
| 905 | }
|
| 906 |
|
Lee Thomason | dadcdfa | 2012-01-18 17:55:48 -0800 | [diff] [blame] | 907 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 908 | // --------- XMLElement ---------- //
|
| 909 | XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 910 | closingType( 0 ),
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 911 | rootAttribute( 0 )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 912 | {
|
| 913 | }
|
| 914 |
|
| 915 |
|
| 916 | XMLElement::~XMLElement()
|
| 917 | {
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 918 | while( rootAttribute ) {
|
| 919 | XMLAttribute* next = rootAttribute->next;
|
| 920 | DELETE_ATTRIBUTE( rootAttribute );
|
| 921 | rootAttribute = next;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 922 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 923 | }
|
| 924 |
|
| 925 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 926 | XMLAttribute* XMLElement::FindAttribute( const char* name )
|
| 927 | {
|
| 928 | XMLAttribute* a = 0;
|
| 929 | for( a=rootAttribute; a; a = a->next ) {
|
| 930 | if ( XMLUtil::StringEqual( a->Name(), name ) )
|
| 931 | return a;
|
| 932 | }
|
| 933 | return 0;
|
| 934 | }
|
| 935 |
|
| 936 |
|
| 937 | const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
|
| 938 | {
|
| 939 | XMLAttribute* a = 0;
|
| 940 | for( a=rootAttribute; a; a = a->next ) {
|
| 941 | if ( XMLUtil::StringEqual( a->Name(), name ) )
|
| 942 | return a;
|
| 943 | }
|
| 944 | return 0;
|
| 945 | }
|
| 946 |
|
| 947 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 948 | const char* XMLElement::GetText() const
|
| 949 | {
|
| 950 | if ( FirstChild() && FirstChild()->ToText() ) {
|
| 951 | return FirstChild()->ToText()->Value();
|
| 952 | }
|
| 953 | return 0;
|
| 954 | }
|
| 955 |
|
| 956 |
|
| 957 |
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 958 | XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
|
| 959 | {
|
| 960 | XMLAttribute* attrib = FindAttribute( name );
|
| 961 | if ( !attrib ) {
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 962 | attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 963 | attrib->memPool = &document->attributePool;
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 964 | LinkAttribute( attrib );
|
| 965 | attrib->SetName( name );
|
Lee Thomason | 1a1d4a7 | 2012-02-15 09:09:25 -0800 | [diff] [blame] | 966 | }
|
| 967 | return attrib;
|
| 968 | }
|
| 969 |
|
| 970 |
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 971 | void XMLElement::LinkAttribute( XMLAttribute* attrib )
|
| 972 | {
|
| 973 | if ( rootAttribute ) {
|
| 974 | XMLAttribute* end = rootAttribute;
|
| 975 | while ( end->next )
|
| 976 | end = end->next;
|
| 977 | end->next = attrib;
|
| 978 | }
|
| 979 | else {
|
| 980 | rootAttribute = attrib;
|
| 981 | }
|
| 982 | }
|
| 983 |
|
| 984 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 985 | void XMLElement::DeleteAttribute( const char* name )
|
| 986 | {
|
| 987 | XMLAttribute* prev = 0;
|
| 988 | for( XMLAttribute* a=rootAttribute; a; a=a->next ) {
|
| 989 | if ( XMLUtil::StringEqual( name, a->Name() ) ) {
|
| 990 | if ( prev ) {
|
| 991 | prev->next = a->next;
|
| 992 | }
|
| 993 | else {
|
| 994 | rootAttribute = a->next;
|
| 995 | }
|
| 996 | DELETE_ATTRIBUTE( a );
|
| 997 | break;
|
| 998 | }
|
| 999 | prev = a;
|
| 1000 | }
|
| 1001 | }
|
| 1002 |
|
| 1003 |
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1004 | char* XMLElement::ParseAttributes( char* p )
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1005 | {
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1006 | const char* start = p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1007 |
|
| 1008 | // Read the attributes.
|
| 1009 | while( p ) {
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1010 | p = XMLUtil::SkipWhiteSpace( p );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1011 | if ( !p || !(*p) ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1012 | document->SetError( ERROR_PARSING_ELEMENT, start, Name() );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1013 | return 0;
|
| 1014 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1015 |
|
| 1016 | // attribute.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1017 | if ( XMLUtil::IsAlpha( *p ) ) {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 1018 | XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute( this );
|
| 1019 | attrib->memPool = &document->attributePool;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1020 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1021 | p = attrib->ParseDeep( p );
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 1022 | if ( !p || Attribute( attrib->Name() ) ) {
|
Lee Thomason | 43f5930 | 2012-02-06 18:18:11 -0800 | [diff] [blame] | 1023 | DELETE_ATTRIBUTE( attrib );
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1024 | document->SetError( ERROR_PARSING_ATTRIBUTE, start, p );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1025 | return 0;
|
| 1026 | }
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1027 | LinkAttribute( attrib );
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1028 | }
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 1029 | // end of the tag
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1030 | else if ( *p == '/' && *(p+1) == '>' ) {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1031 | closingType = CLOSED;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1032 | return p+2; // done; sealed element.
|
| 1033 | }
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 1034 | // end of the tag
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1035 | else if ( *p == '>' ) {
|
| 1036 | ++p;
|
| 1037 | break;
|
| 1038 | }
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 1039 | else {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1040 | document->SetError( ERROR_PARSING_ELEMENT, start, p );
|
Lee Thomason | e442230 | 2012-01-20 17:59:50 -0800 | [diff] [blame] | 1041 | return 0;
|
| 1042 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1043 | }
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1044 | return p;
|
| 1045 | }
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1046 |
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1047 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1048 | //
|
| 1049 | // <ele></ele>
|
| 1050 | // <ele>foo<b>bar</b></ele>
|
| 1051 | //
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame^] | 1052 | char* XMLElement::ParseDeep( char* p, StrPair* strPair )
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1053 | {
|
| 1054 | // Read the element name.
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1055 | p = XMLUtil::SkipWhiteSpace( p );
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1056 | if ( !p ) return 0;
|
| 1057 | const char* start = p;
|
| 1058 |
|
| 1059 | // The closing element is the </element> form. It is
|
| 1060 | // parsed just like a regular element then deleted from
|
| 1061 | // the DOM.
|
| 1062 | if ( *p == '/' ) {
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1063 | closingType = CLOSING;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1064 | ++p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1065 | }
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1066 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1067 | p = value.ParseName( p );
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1068 | if ( value.Empty() ) return 0;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1069 |
|
| 1070 | bool elementClosed=false;
|
Lee Thomason (grinliz) | 46a14cf | 2012-02-23 22:27:28 -0800 | [diff] [blame] | 1071 | p = ParseAttributes( p );
|
| 1072 | if ( !p || !*p || closingType )
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1073 | return p;
|
| 1074 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame^] | 1075 | p = XMLNode::ParseDeep( p, strPair );
|
| 1076 | // FIXME: proces end tage here??
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1077 | return p;
|
Lee Thomason | 8a5dfee | 2012-01-18 17:43:40 -0800 | [diff] [blame] | 1078 | }
|
| 1079 |
|
| 1080 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1081 | bool XMLElement::Accept( XMLVisitor* visitor ) const
|
| 1082 | {
|
| 1083 | if ( visitor->VisitEnter( *this, rootAttribute ) )
|
| 1084 | {
|
| 1085 | for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() )
|
| 1086 | {
|
| 1087 | if ( !node->Accept( visitor ) )
|
| 1088 | break;
|
| 1089 | }
|
| 1090 | }
|
| 1091 | return visitor->VisitExit( *this );
|
| 1092 |
|
| 1093 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1094 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1095 | // --------- XMLDocument ----------- //
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1096 | XMLDocument::XMLDocument() :
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1097 | XMLNode( 0 ),
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1098 | writeBOM( false ),
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1099 | charBuffer( 0 )
|
| 1100 | {
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1101 | document = this; // avoid warning about 'this' in initializer list
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1102 | }
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 1103 |
|
| 1104 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1105 | XMLDocument::~XMLDocument()
|
| 1106 | {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1107 | ClearChildren();
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1108 | delete [] charBuffer;
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1109 |
|
Lee Thomason | ec5a7b4 | 2012-02-13 18:16:52 -0800 | [diff] [blame] | 1110 | #if 0
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 1111 | textPool.Trace( "text" );
|
| 1112 | elementPool.Trace( "element" );
|
| 1113 | commentPool.Trace( "comment" );
|
| 1114 | attributePool.Trace( "attribute" );
|
Lee Thomason | e9ecdab | 2012-02-13 18:11:20 -0800 | [diff] [blame] | 1115 | #endif
|
| 1116 |
|
Lee Thomason | 455c9d4 | 2012-02-06 09:14:14 -0800 | [diff] [blame] | 1117 | TIXMLASSERT( textPool.CurrentAllocs() == 0 );
|
| 1118 | TIXMLASSERT( elementPool.CurrentAllocs() == 0 );
|
| 1119 | TIXMLASSERT( commentPool.CurrentAllocs() == 0 );
|
| 1120 | TIXMLASSERT( attributePool.CurrentAllocs() == 0 );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1121 | }
|
| 1122 |
|
| 1123 |
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1124 | void XMLDocument::InitDocument()
|
| 1125 | {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1126 | errorID = XML_NO_ERROR;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1127 | errorStr1 = 0;
|
| 1128 | errorStr2 = 0;
|
| 1129 |
|
| 1130 | delete [] charBuffer;
|
| 1131 | charBuffer = 0;
|
| 1132 |
|
| 1133 | }
|
| 1134 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1135 |
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1136 | XMLElement* XMLDocument::NewElement( const char* name )
|
| 1137 | {
|
Lee Thomason | d198322 | 2012-02-06 08:41:24 -0800 | [diff] [blame] | 1138 | XMLElement* ele = new (elementPool.Alloc()) XMLElement( this );
|
| 1139 | ele->memPool = &elementPool;
|
Lee Thomason | 2c85a71 | 2012-01-31 08:24:24 -0800 | [diff] [blame] | 1140 | ele->SetName( name );
|
| 1141 | return ele;
|
| 1142 | }
|
| 1143 |
|
| 1144 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1145 | XMLComment* XMLDocument::NewComment( const char* str )
|
| 1146 | {
|
| 1147 | XMLComment* comment = new (commentPool.Alloc()) XMLComment( this );
|
| 1148 | comment->memPool = &commentPool;
|
| 1149 | comment->SetValue( str );
|
| 1150 | return comment;
|
| 1151 | }
|
| 1152 |
|
| 1153 |
|
| 1154 | XMLText* XMLDocument::NewText( const char* str )
|
| 1155 | {
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1156 | XMLText* text = new (textPool.Alloc()) XMLText( this );
|
| 1157 | text->memPool = &textPool;
|
| 1158 | text->SetValue( str );
|
| 1159 | return text;
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1160 | }
|
| 1161 |
|
| 1162 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1163 | int XMLDocument::LoadFile( const char* filename )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1164 | {
|
| 1165 | ClearChildren();
|
| 1166 | InitDocument();
|
| 1167 |
|
| 1168 | FILE* fp = fopen( filename, "rb" );
|
| 1169 | if ( !fp ) {
|
| 1170 | SetError( ERROR_FILE_NOT_FOUND, filename, 0 );
|
| 1171 | return errorID;
|
| 1172 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1173 | LoadFile( fp );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1174 | fclose( fp );
|
| 1175 | return errorID;
|
| 1176 | }
|
| 1177 |
|
| 1178 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1179 | int XMLDocument::LoadFile( FILE* fp )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1180 | {
|
| 1181 | ClearChildren();
|
| 1182 | InitDocument();
|
| 1183 |
|
| 1184 | fseek( fp, 0, SEEK_END );
|
| 1185 | unsigned size = ftell( fp );
|
| 1186 | fseek( fp, 0, SEEK_SET );
|
| 1187 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1188 | if ( size == 0 ) {
|
| 1189 | return errorID;
|
| 1190 | }
|
| 1191 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1192 | charBuffer = new char[size+1];
|
| 1193 | fread( charBuffer, size, 1, fp );
|
| 1194 | charBuffer[size] = 0;
|
| 1195 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1196 | const char* p = charBuffer;
|
| 1197 | p = XMLUtil::SkipWhiteSpace( p );
|
| 1198 | p = XMLUtil::ReadBOM( p, &writeBOM );
|
| 1199 | if ( !p || !*p ) {
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 1200 | SetError( ERROR_EMPTY_DOCUMENT, 0, 0 );
|
| 1201 | return errorID;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1202 | }
|
| 1203 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame^] | 1204 | ParseDeep( charBuffer + (p-charBuffer), 0 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1205 | return errorID;
|
| 1206 | }
|
| 1207 |
|
| 1208 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1209 | void XMLDocument::SaveFile( const char* filename )
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1210 | {
|
| 1211 | FILE* fp = fopen( filename, "w" );
|
| 1212 | XMLStreamer stream( fp );
|
| 1213 | Print( &stream );
|
| 1214 | fclose( fp );
|
| 1215 | }
|
| 1216 |
|
Lee Thomason | 1ff38e0 | 2012-02-14 18:18:16 -0800 | [diff] [blame] | 1217 |
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 1218 | int XMLDocument::Parse( const char* p )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1219 | {
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1220 | ClearChildren();
|
| 1221 | InitDocument();
|
| 1222 |
|
| 1223 | if ( !p || !*p ) {
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 1224 | SetError( ERROR_EMPTY_DOCUMENT, 0, 0 );
|
| 1225 | return errorID;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1226 | }
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1227 | p = XMLUtil::SkipWhiteSpace( p );
|
| 1228 | p = XMLUtil::ReadBOM( p, &writeBOM );
|
| 1229 | if ( !p || !*p ) {
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 1230 | SetError( ERROR_EMPTY_DOCUMENT, 0, 0 );
|
| 1231 | return errorID;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1232 | }
|
| 1233 |
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1234 | size_t len = strlen( p );
|
| 1235 | charBuffer = new char[ len+1 ];
|
| 1236 | memcpy( charBuffer, p, len+1 );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1237 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1238 |
|
Lee Thomason (grinliz) | 7468f11 | 2012-02-24 08:56:50 -0800 | [diff] [blame^] | 1239 | ParseDeep( charBuffer, 0 );
|
Lee Thomason | 7c913cd | 2012-01-26 18:32:34 -0800 | [diff] [blame] | 1240 | return errorID;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1241 | }
|
| 1242 |
|
| 1243 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1244 | void XMLDocument::Print( XMLStreamer* streamer )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1245 | {
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1246 | XMLStreamer stdStreamer( stdout );
|
| 1247 | if ( !streamer )
|
| 1248 | streamer = &stdStreamer;
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1249 | Accept( streamer );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 1250 | }
|
| 1251 |
|
| 1252 |
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1253 | void XMLDocument::SetError( int error, const char* str1, const char* str2 )
|
| 1254 | {
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1255 | errorID = error;
|
Lee Thomason | 18d68bd | 2012-01-26 18:17:26 -0800 | [diff] [blame] | 1256 | errorStr1 = str1;
|
| 1257 | errorStr2 = str2;
|
Lee Thomason | 67d6131 | 2012-01-24 16:01:51 -0800 | [diff] [blame] | 1258 | }
|
| 1259 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1260 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1261 | void XMLDocument::PrintError() const
|
| 1262 | {
|
| 1263 | if ( errorID ) {
|
| 1264 | char buf1[20] = { 0 };
|
| 1265 | char buf2[20] = { 0 };
|
| 1266 |
|
| 1267 | if ( errorStr1 ) {
|
| 1268 | strncpy( buf1, errorStr1, 20 );
|
| 1269 | buf1[19] = 0;
|
| 1270 | }
|
| 1271 | if ( errorStr2 ) {
|
| 1272 | strncpy( buf2, errorStr2, 20 );
|
| 1273 | buf2[19] = 0;
|
| 1274 | }
|
| 1275 |
|
| 1276 | printf( "XMLDocument error id=%d str1=%s str2=%s\n",
|
| 1277 | errorID, buf1, buf2 );
|
| 1278 | }
|
| 1279 | }
|
| 1280 |
|
| 1281 |
|
| 1282 | XMLStreamer::XMLStreamer( FILE* file ) :
|
| 1283 | elementJustOpened( false ),
|
| 1284 | firstElement( true ),
|
| 1285 | fp( file ),
|
| 1286 | depth( 0 ),
|
| 1287 | textDepth( -1 )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1288 | {
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1289 | for( int i=0; i<ENTITY_RANGE; ++i ) {
|
| 1290 | entityFlag[i] = false;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1291 | restrictedEntityFlag[i] = false;
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1292 | }
|
| 1293 | for( int i=0; i<NUM_ENTITIES; ++i ) {
|
| 1294 | TIXMLASSERT( entities[i].value < ENTITY_RANGE );
|
| 1295 | if ( entities[i].value < ENTITY_RANGE ) {
|
| 1296 | entityFlag[ entities[i].value ] = true;
|
| 1297 | }
|
| 1298 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1299 | restrictedEntityFlag['&'] = true;
|
| 1300 | restrictedEntityFlag['<'] = true;
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1301 | restrictedEntityFlag['>'] = true; // not required, but consistency is nice
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1302 | buffer.Push( 0 );
|
| 1303 | }
|
| 1304 |
|
| 1305 |
|
| 1306 | void XMLStreamer::Print( const char* format, ... )
|
| 1307 | {
|
| 1308 | va_list va;
|
| 1309 | va_start( va, format );
|
| 1310 |
|
| 1311 | if ( fp ) {
|
| 1312 | vfprintf( fp, format, va );
|
| 1313 | }
|
| 1314 | else {
|
| 1315 | // This seems brutally complex. Haven't figured out a better
|
| 1316 | // way on windows.
|
| 1317 | #ifdef _MSC_VER
|
| 1318 | int len = -1;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1319 | int expand = 1000;
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1320 | while ( len < 0 ) {
|
| 1321 | len = vsnprintf_s( accumulator.Mem(), accumulator.Capacity(), accumulator.Capacity()-1, format, va );
|
| 1322 | if ( len < 0 ) {
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1323 | accumulator.PushArr( expand );
|
| 1324 | expand *= 3/2;
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1325 | }
|
| 1326 | }
|
| 1327 | char* p = buffer.PushArr( len ) - 1;
|
| 1328 | memcpy( p, accumulator.Mem(), len+1 );
|
| 1329 | #else
|
| 1330 | int len = vsnprintf( 0, 0, format, va );
|
| 1331 | char* p = buffer.PushArr( len ) - 1;
|
| 1332 | vsprintf_s( p, len+1, format, va );
|
| 1333 | #endif
|
| 1334 | }
|
| 1335 | va_end( va );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1336 | }
|
| 1337 |
|
| 1338 |
|
| 1339 | void XMLStreamer::PrintSpace( int depth )
|
| 1340 | {
|
| 1341 | for( int i=0; i<depth; ++i ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1342 | Print( " " );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1343 | }
|
| 1344 | }
|
| 1345 |
|
| 1346 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1347 | void XMLStreamer::PrintString( const char* p, bool restricted )
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1348 | {
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1349 | // Look for runs of bytes between entities to print.
|
| 1350 | const char* q = p;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1351 | const bool* flag = restricted ? restrictedEntityFlag : entityFlag;
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1352 |
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1353 | while ( *q ) {
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1354 | // Remember, char is sometimes signed. (How many times has that bitten me?)
|
| 1355 | if ( *q > 0 && *q < ENTITY_RANGE ) {
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1356 | // Check for entities. If one is found, flush
|
| 1357 | // the stream up until the entity, write the
|
| 1358 | // entity, and keep looking.
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1359 | if ( flag[*q] ) {
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1360 | while ( p < q ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1361 | Print( "%c", *p );
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1362 | ++p;
|
| 1363 | }
|
| 1364 | for( int i=0; i<NUM_ENTITIES; ++i ) {
|
| 1365 | if ( entities[i].value == *q ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1366 | Print( "&%s;", entities[i].pattern );
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1367 | break;
|
| 1368 | }
|
| 1369 | }
|
| 1370 | ++p;
|
| 1371 | }
|
| 1372 | }
|
| 1373 | ++q;
|
| 1374 | }
|
| 1375 | // Flush the remaining string. This will be the entire
|
| 1376 | // string if an entity wasn't found.
|
| 1377 | if ( q-p > 0 ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1378 | Print( "%s", p );
|
Lee Thomason | 951d883 | 2012-01-26 08:47:06 -0800 | [diff] [blame] | 1379 | }
|
Lee Thomason | 857b868 | 2012-01-25 17:50:25 -0800 | [diff] [blame] | 1380 | }
|
| 1381 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1382 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1383 | void XMLStreamer::PushHeader( bool writeBOM, bool writeDec )
|
| 1384 | {
|
| 1385 | static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
|
| 1386 | if ( writeBOM ) {
|
| 1387 | Print( "%s", bom );
|
| 1388 | }
|
| 1389 | if ( writeDec ) {
|
| 1390 | PushDeclaration( "xml version=\"1.0\"" );
|
| 1391 | }
|
| 1392 | }
|
| 1393 |
|
| 1394 |
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1395 | void XMLStreamer::OpenElement( const char* name )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1396 | {
|
| 1397 | if ( elementJustOpened ) {
|
| 1398 | SealElement();
|
| 1399 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1400 | stack.Push( name );
|
| 1401 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1402 | if ( textDepth < 0 && !firstElement ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1403 | Print( "\n" );
|
Lee Thomason | 24767b0 | 2012-01-25 17:16:23 -0800 | [diff] [blame] | 1404 | PrintSpace( depth );
|
| 1405 | }
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1406 |
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1407 | Print( "<%s", name );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1408 | elementJustOpened = true;
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1409 | firstElement = false;
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1410 | ++depth;
|
| 1411 | }
|
| 1412 |
|
| 1413 |
|
| 1414 | void XMLStreamer::PushAttribute( const char* name, const char* value )
|
| 1415 | {
|
| 1416 | TIXMLASSERT( elementJustOpened );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1417 | Print( " %s=\"", name );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1418 | PrintString( value, false );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1419 | Print( "\"" );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1420 | }
|
| 1421 |
|
| 1422 |
|
| 1423 | void XMLStreamer::CloseElement()
|
| 1424 | {
|
| 1425 | --depth;
|
| 1426 | const char* name = stack.Pop();
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1427 |
|
| 1428 | if ( elementJustOpened ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1429 | Print( "/>" );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1430 | }
|
| 1431 | else {
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1432 | if ( textDepth < 0 ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1433 | Print( "\n" );
|
Lee Thomason | 24767b0 | 2012-01-25 17:16:23 -0800 | [diff] [blame] | 1434 | PrintSpace( depth );
|
| 1435 | }
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1436 | Print( "</%s>", name );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1437 | }
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1438 |
|
| 1439 | if ( textDepth == depth )
|
| 1440 | textDepth = -1;
|
| 1441 | if ( depth == 0 )
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1442 | Print( "\n" );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1443 | elementJustOpened = false;
|
| 1444 | }
|
| 1445 |
|
| 1446 |
|
| 1447 | void XMLStreamer::SealElement()
|
| 1448 | {
|
| 1449 | elementJustOpened = false;
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1450 | Print( ">" );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1451 | }
|
| 1452 |
|
| 1453 |
|
Lee Thomason | 50f97b2 | 2012-02-11 16:33:40 -0800 | [diff] [blame] | 1454 | void XMLStreamer::PushText( const char* text, bool cdata )
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1455 | {
|
Lee Thomason | 56bdd02 | 2012-02-09 18:16:58 -0800 | [diff] [blame] | 1456 | textDepth = depth-1;
|
| 1457 |
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1458 | if ( elementJustOpened ) {
|
| 1459 | SealElement();
|
| 1460 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1461 | if ( cdata ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1462 | Print( "<![CDATA[" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1463 | Print( "%s", text );
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1464 | Print( "]]>" );
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1465 | }
|
| 1466 | else {
|
| 1467 | PrintString( text, true );
|
| 1468 | }
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1469 | }
|
| 1470 |
|
| 1471 |
|
| 1472 | void XMLStreamer::PushComment( const char* comment )
|
| 1473 | {
|
| 1474 | if ( elementJustOpened ) {
|
| 1475 | SealElement();
|
| 1476 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1477 | if ( textDepth < 0 && !firstElement ) {
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1478 | Print( "\n" );
|
U-Stream\Lee | 09a11c5 | 2012-02-17 08:31:16 -0800 | [diff] [blame] | 1479 | PrintSpace( depth );
|
| 1480 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1481 | firstElement = false;
|
U-Stream\Lee | ae25a44 | 2012-02-17 17:48:16 -0800 | [diff] [blame] | 1482 | Print( "<!--%s-->", comment );
|
Lee Thomason | 5cae897 | 2012-01-24 18:03:07 -0800 | [diff] [blame] | 1483 | }
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1484 |
|
| 1485 |
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1486 | void XMLStreamer::PushDeclaration( const char* value )
|
| 1487 | {
|
| 1488 | if ( elementJustOpened ) {
|
| 1489 | SealElement();
|
| 1490 | }
|
| 1491 | if ( textDepth < 0 && !firstElement) {
|
| 1492 | Print( "\n" );
|
| 1493 | PrintSpace( depth );
|
| 1494 | }
|
| 1495 | firstElement = false;
|
| 1496 | Print( "<?%s?>", value );
|
| 1497 | }
|
| 1498 |
|
| 1499 |
|
| 1500 | void XMLStreamer::PushUnknown( const char* value )
|
| 1501 | {
|
| 1502 | if ( elementJustOpened ) {
|
| 1503 | SealElement();
|
| 1504 | }
|
| 1505 | if ( textDepth < 0 && !firstElement ) {
|
| 1506 | Print( "\n" );
|
| 1507 | PrintSpace( depth );
|
| 1508 | }
|
| 1509 | firstElement = false;
|
| 1510 | Print( "<!%s>", value );
|
| 1511 | }
|
| 1512 |
|
| 1513 |
|
Lee Thomason (grinliz) | 68db57e | 2012-02-21 09:08:12 -0800 | [diff] [blame] | 1514 | bool XMLStreamer::VisitEnter( const XMLDocument& doc )
|
| 1515 | {
|
| 1516 | if ( doc.HasBOM() ) {
|
| 1517 | PushHeader( true, false );
|
| 1518 | }
|
| 1519 | return true;
|
| 1520 | }
|
| 1521 |
|
| 1522 |
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1523 | bool XMLStreamer::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
|
| 1524 | {
|
| 1525 | OpenElement( element.Name() );
|
| 1526 | while ( attribute ) {
|
| 1527 | PushAttribute( attribute->Name(), attribute->Value() );
|
| 1528 | attribute = attribute->Next();
|
| 1529 | }
|
| 1530 | return true;
|
| 1531 | }
|
| 1532 |
|
| 1533 |
|
| 1534 | bool XMLStreamer::VisitExit( const XMLElement& element )
|
| 1535 | {
|
| 1536 | CloseElement();
|
| 1537 | return true;
|
| 1538 | }
|
| 1539 |
|
| 1540 |
|
| 1541 | bool XMLStreamer::Visit( const XMLText& text )
|
| 1542 | {
|
Lee Thomason | d627776 | 2012-02-22 16:00:12 -0800 | [diff] [blame] | 1543 | PushText( text.Value(), text.CData() );
|
Lee Thomason | 751da52 | 2012-02-10 08:50:51 -0800 | [diff] [blame] | 1544 | return true;
|
| 1545 | }
|
| 1546 |
|
| 1547 |
|
| 1548 | bool XMLStreamer::Visit( const XMLComment& comment )
|
| 1549 | {
|
| 1550 | PushComment( comment.Value() );
|
| 1551 | return true;
|
| 1552 | }
|
Lee Thomason (grinliz) | bd0a8ac | 2012-02-20 20:14:33 -0800 | [diff] [blame] | 1553 |
|
| 1554 | bool XMLStreamer::Visit( const XMLDeclaration& declaration )
|
| 1555 | {
|
| 1556 | PushDeclaration( declaration.Value() );
|
| 1557 | return true;
|
| 1558 | }
|
| 1559 |
|
| 1560 |
|
| 1561 | bool XMLStreamer::Visit( const XMLUnknown& unknown )
|
| 1562 | {
|
| 1563 | PushUnknown( unknown.Value() );
|
| 1564 | return true;
|
| 1565 | }
|
| 1566 |
|
| 1567 |
|