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