nuclear@19: /* nuclear@19: Original code by Lee Thomason (www.grinninglizard.com) nuclear@19: nuclear@19: This software is provided 'as-is', without any express or implied nuclear@19: warranty. In no event will the authors be held liable for any nuclear@19: damages arising from the use of this software. nuclear@19: nuclear@19: Permission is granted to anyone to use this software for any nuclear@19: purpose, including commercial applications, and to alter it and nuclear@19: redistribute it freely, subject to the following restrictions: nuclear@19: nuclear@19: 1. The origin of this software must not be misrepresented; you must nuclear@19: not claim that you wrote the original software. If you use this nuclear@19: software in a product, an acknowledgment in the product documentation nuclear@19: would be appreciated but is not required. nuclear@19: nuclear@19: 2. Altered source versions must be plainly marked as such, and nuclear@19: must not be misrepresented as being the original software. nuclear@19: nuclear@19: 3. This notice may not be removed or altered from any source nuclear@19: distribution. nuclear@19: */ nuclear@19: nuclear@19: #include "tinyxml2.h" nuclear@19: nuclear@19: #include // yes, this one new style header, is in the Android SDK. nuclear@19: # ifdef ANDROID_NDK nuclear@19: # include nuclear@19: #else nuclear@19: # include nuclear@19: #endif nuclear@19: nuclear@19: static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF nuclear@19: static const char LF = LINE_FEED; nuclear@19: static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out nuclear@19: static const char CR = CARRIAGE_RETURN; nuclear@19: static const char SINGLE_QUOTE = '\''; nuclear@19: static const char DOUBLE_QUOTE = '\"'; nuclear@19: nuclear@19: // Bunch of unicode info at: nuclear@19: // http://www.unicode.org/faq/utf_bom.html nuclear@19: // ef bb bf (Microsoft "lead bytes") - designates UTF-8 nuclear@19: nuclear@19: static const unsigned char TIXML_UTF_LEAD_0 = 0xefU; nuclear@19: static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU; nuclear@19: static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU; nuclear@19: nuclear@19: nuclear@19: #define DELETE_NODE( node ) { \ nuclear@19: if ( node ) { \ nuclear@19: MemPool* pool = node->_memPool; \ nuclear@19: node->~XMLNode(); \ nuclear@19: pool->Free( node ); \ nuclear@19: } \ nuclear@19: } nuclear@19: #define DELETE_ATTRIBUTE( attrib ) { \ nuclear@19: if ( attrib ) { \ nuclear@19: MemPool* pool = attrib->_memPool; \ nuclear@19: attrib->~XMLAttribute(); \ nuclear@19: pool->Free( attrib ); \ nuclear@19: } \ nuclear@19: } nuclear@19: nuclear@19: namespace tinyxml2 nuclear@19: { nuclear@19: nuclear@19: struct Entity { nuclear@19: const char* pattern; nuclear@19: int length; nuclear@19: char value; nuclear@19: }; nuclear@19: nuclear@19: static const int NUM_ENTITIES = 5; nuclear@19: static const Entity entities[NUM_ENTITIES] = { nuclear@19: { "quot", 4, DOUBLE_QUOTE }, nuclear@19: { "amp", 3, '&' }, nuclear@19: { "apos", 4, SINGLE_QUOTE }, nuclear@19: { "lt", 2, '<' }, nuclear@19: { "gt", 2, '>' } nuclear@19: }; nuclear@19: nuclear@19: nuclear@19: StrPair::~StrPair() nuclear@19: { nuclear@19: Reset(); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void StrPair::Reset() nuclear@19: { nuclear@19: if ( _flags & NEEDS_DELETE ) { nuclear@19: delete [] _start; nuclear@19: } nuclear@19: _flags = 0; nuclear@19: _start = 0; nuclear@19: _end = 0; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void StrPair::SetStr( const char* str, int flags ) nuclear@19: { nuclear@19: Reset(); nuclear@19: size_t len = strlen( str ); nuclear@19: _start = new char[ len+1 ]; nuclear@19: memcpy( _start, str, len+1 ); nuclear@19: _end = _start + len; nuclear@19: _flags = flags | NEEDS_DELETE; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: char* StrPair::ParseText( char* p, const char* endTag, int strFlags ) nuclear@19: { nuclear@19: TIXMLASSERT( endTag && *endTag ); nuclear@19: nuclear@19: char* start = p; // fixme: hides a member nuclear@19: char endChar = *endTag; nuclear@19: size_t length = strlen( endTag ); nuclear@19: nuclear@19: // Inner loop of text parsing. nuclear@19: while ( *p ) { nuclear@19: if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) { nuclear@19: Set( start, p, strFlags ); nuclear@19: return p + length; nuclear@19: } nuclear@19: ++p; nuclear@19: } nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: char* StrPair::ParseName( char* p ) nuclear@19: { nuclear@19: char* start = p; nuclear@19: nuclear@19: if ( !start || !(*start) ) { nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: while( *p && ( p == start ? XMLUtil::IsNameStartChar( *p ) : XMLUtil::IsNameChar( *p ) )) { nuclear@19: ++p; nuclear@19: } nuclear@19: nuclear@19: if ( p > start ) { nuclear@19: Set( start, p, 0 ); nuclear@19: return p; nuclear@19: } nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void StrPair::CollapseWhitespace() nuclear@19: { nuclear@19: // Trim leading space. nuclear@19: _start = XMLUtil::SkipWhiteSpace( _start ); nuclear@19: nuclear@19: if ( _start && *_start ) { nuclear@19: char* p = _start; // the read pointer nuclear@19: char* q = _start; // the write pointer nuclear@19: nuclear@19: while( *p ) { nuclear@19: if ( XMLUtil::IsWhiteSpace( *p )) { nuclear@19: p = XMLUtil::SkipWhiteSpace( p ); nuclear@19: if ( *p == 0 ) { nuclear@19: break; // don't write to q; this trims the trailing space. nuclear@19: } nuclear@19: *q = ' '; nuclear@19: ++q; nuclear@19: } nuclear@19: *q = *p; nuclear@19: ++q; nuclear@19: ++p; nuclear@19: } nuclear@19: *q = 0; nuclear@19: } nuclear@19: } nuclear@19: nuclear@19: nuclear@19: const char* StrPair::GetStr() nuclear@19: { nuclear@19: if ( _flags & NEEDS_FLUSH ) { nuclear@19: *_end = 0; nuclear@19: _flags ^= NEEDS_FLUSH; nuclear@19: nuclear@19: if ( _flags ) { nuclear@19: char* p = _start; // the read pointer nuclear@19: char* q = _start; // the write pointer nuclear@19: nuclear@19: while( p < _end ) { nuclear@19: if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) { nuclear@19: // CR-LF pair becomes LF nuclear@19: // CR alone becomes LF nuclear@19: // LF-CR becomes LF nuclear@19: if ( *(p+1) == LF ) { nuclear@19: p += 2; nuclear@19: } nuclear@19: else { nuclear@19: ++p; nuclear@19: } nuclear@19: *q++ = LF; nuclear@19: } nuclear@19: else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) { nuclear@19: if ( *(p+1) == CR ) { nuclear@19: p += 2; nuclear@19: } nuclear@19: else { nuclear@19: ++p; nuclear@19: } nuclear@19: *q++ = LF; nuclear@19: } nuclear@19: else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) { nuclear@19: // Entities handled by tinyXML2: nuclear@19: // - special entities in the entity table [in/out] nuclear@19: // - numeric character reference [in] nuclear@19: // 中 or 中 nuclear@19: nuclear@19: if ( *(p+1) == '#' ) { nuclear@19: char buf[10] = { 0 }; nuclear@19: int len; nuclear@19: p = const_cast( XMLUtil::GetCharacterRef( p, buf, &len ) ); nuclear@19: for( int i=0; i(p); nuclear@19: // Check for BOM: nuclear@19: if ( *(pu+0) == TIXML_UTF_LEAD_0 nuclear@19: && *(pu+1) == TIXML_UTF_LEAD_1 nuclear@19: && *(pu+2) == TIXML_UTF_LEAD_2 ) { nuclear@19: *bom = true; nuclear@19: p += 3; nuclear@19: } nuclear@19: return p; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length ) nuclear@19: { nuclear@19: const unsigned long BYTE_MASK = 0xBF; nuclear@19: const unsigned long BYTE_MARK = 0x80; nuclear@19: const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; nuclear@19: nuclear@19: if (input < 0x80) { nuclear@19: *length = 1; nuclear@19: } nuclear@19: else if ( input < 0x800 ) { nuclear@19: *length = 2; nuclear@19: } nuclear@19: else if ( input < 0x10000 ) { nuclear@19: *length = 3; nuclear@19: } nuclear@19: else if ( input < 0x200000 ) { nuclear@19: *length = 4; nuclear@19: } nuclear@19: else { nuclear@19: *length = 0; // This code won't covert this correctly anyway. nuclear@19: return; nuclear@19: } nuclear@19: nuclear@19: output += *length; nuclear@19: nuclear@19: // Scary scary fall throughs. nuclear@19: switch (*length) { nuclear@19: case 4: nuclear@19: --output; nuclear@19: *output = (char)((input | BYTE_MARK) & BYTE_MASK); nuclear@19: input >>= 6; nuclear@19: case 3: nuclear@19: --output; nuclear@19: *output = (char)((input | BYTE_MARK) & BYTE_MASK); nuclear@19: input >>= 6; nuclear@19: case 2: nuclear@19: --output; nuclear@19: *output = (char)((input | BYTE_MARK) & BYTE_MASK); nuclear@19: input >>= 6; nuclear@19: case 1: nuclear@19: --output; nuclear@19: *output = (char)(input | FIRST_BYTE_MARK[*length]); nuclear@19: default: nuclear@19: break; nuclear@19: } nuclear@19: } nuclear@19: nuclear@19: nuclear@19: const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length ) nuclear@19: { nuclear@19: // Presume an entity, and pull it out. nuclear@19: *length = 0; nuclear@19: nuclear@19: if ( *(p+1) == '#' && *(p+2) ) { nuclear@19: unsigned long ucs = 0; nuclear@19: ptrdiff_t delta = 0; nuclear@19: unsigned mult = 1; nuclear@19: nuclear@19: if ( *(p+2) == 'x' ) { nuclear@19: // Hexadecimal. nuclear@19: if ( !*(p+3) ) { nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: const char* q = p+3; nuclear@19: q = strchr( q, ';' ); nuclear@19: nuclear@19: if ( !q || !*q ) { nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: delta = q-p; nuclear@19: --q; nuclear@19: nuclear@19: while ( *q != 'x' ) { nuclear@19: if ( *q >= '0' && *q <= '9' ) { nuclear@19: ucs += mult * (*q - '0'); nuclear@19: } nuclear@19: else if ( *q >= 'a' && *q <= 'f' ) { nuclear@19: ucs += mult * (*q - 'a' + 10); nuclear@19: } nuclear@19: else if ( *q >= 'A' && *q <= 'F' ) { nuclear@19: ucs += mult * (*q - 'A' + 10 ); nuclear@19: } nuclear@19: else { nuclear@19: return 0; nuclear@19: } nuclear@19: mult *= 16; nuclear@19: --q; nuclear@19: } nuclear@19: } nuclear@19: else { nuclear@19: // Decimal. nuclear@19: if ( !*(p+2) ) { nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: const char* q = p+2; nuclear@19: q = strchr( q, ';' ); nuclear@19: nuclear@19: if ( !q || !*q ) { nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: delta = q-p; nuclear@19: --q; nuclear@19: nuclear@19: while ( *q != '#' ) { nuclear@19: if ( *q >= '0' && *q <= '9' ) { nuclear@19: ucs += mult * (*q - '0'); nuclear@19: } nuclear@19: else { nuclear@19: return 0; nuclear@19: } nuclear@19: mult *= 10; nuclear@19: --q; nuclear@19: } nuclear@19: } nuclear@19: // convert the UCS to UTF-8 nuclear@19: ConvertUTF32ToUTF8( ucs, value, length ); nuclear@19: return p + delta + 1; nuclear@19: } nuclear@19: return p+1; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLUtil::ToStr( int v, char* buffer, int bufferSize ) nuclear@19: { nuclear@19: TIXML_SNPRINTF( buffer, bufferSize, "%d", v ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize ) nuclear@19: { nuclear@19: TIXML_SNPRINTF( buffer, bufferSize, "%u", v ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLUtil::ToStr( bool v, char* buffer, int bufferSize ) nuclear@19: { nuclear@19: TIXML_SNPRINTF( buffer, bufferSize, "%d", v ? 1 : 0 ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLUtil::ToStr( float v, char* buffer, int bufferSize ) nuclear@19: { nuclear@19: TIXML_SNPRINTF( buffer, bufferSize, "%f", v ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLUtil::ToStr( double v, char* buffer, int bufferSize ) nuclear@19: { nuclear@19: TIXML_SNPRINTF( buffer, bufferSize, "%f", v ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLUtil::ToInt( const char* str, int* value ) nuclear@19: { nuclear@19: if ( TIXML_SSCANF( str, "%d", value ) == 1 ) { nuclear@19: return true; nuclear@19: } nuclear@19: return false; nuclear@19: } nuclear@19: nuclear@19: bool XMLUtil::ToUnsigned( const char* str, unsigned *value ) nuclear@19: { nuclear@19: if ( TIXML_SSCANF( str, "%u", value ) == 1 ) { nuclear@19: return true; nuclear@19: } nuclear@19: return false; nuclear@19: } nuclear@19: nuclear@19: bool XMLUtil::ToBool( const char* str, bool* value ) nuclear@19: { nuclear@19: int ival = 0; nuclear@19: if ( ToInt( str, &ival )) { nuclear@19: *value = (ival==0) ? false : true; nuclear@19: return true; nuclear@19: } nuclear@19: if ( StringEqual( str, "true" ) ) { nuclear@19: *value = true; nuclear@19: return true; nuclear@19: } nuclear@19: else if ( StringEqual( str, "false" ) ) { nuclear@19: *value = false; nuclear@19: return true; nuclear@19: } nuclear@19: return false; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLUtil::ToFloat( const char* str, float* value ) nuclear@19: { nuclear@19: if ( TIXML_SSCANF( str, "%f", value ) == 1 ) { nuclear@19: return true; nuclear@19: } nuclear@19: return false; nuclear@19: } nuclear@19: nuclear@19: bool XMLUtil::ToDouble( const char* str, double* value ) nuclear@19: { nuclear@19: if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) { nuclear@19: return true; nuclear@19: } nuclear@19: return false; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: char* XMLDocument::Identify( char* p, XMLNode** node ) nuclear@19: { nuclear@19: XMLNode* returnNode = 0; nuclear@19: char* start = p; nuclear@19: p = XMLUtil::SkipWhiteSpace( p ); nuclear@19: if( !p || !*p ) { nuclear@19: return p; nuclear@19: } nuclear@19: nuclear@19: // What is this thing? nuclear@19: // - Elements start with a letter or underscore, but xml is reserved. nuclear@19: // - Comments: nuclear@19: // nuclear@19: // With a special case: nuclear@19: // nuclear@19: // nuclear@19: // nuclear@19: // nuclear@19: // Where the closing element (/foo) *must* be the next thing after the opening nuclear@19: // element, and the names must match. BUT the tricky bit is that the closing nuclear@19: // element will be read by the child. nuclear@19: // nuclear@19: // 'endTag' is the end tag for this node, it is returned by a call to a child. nuclear@19: // 'parentEnd' is the end tag for the parent, which is filled in and returned. nuclear@19: nuclear@19: while( p && *p ) { nuclear@19: XMLNode* node = 0; nuclear@19: nuclear@19: p = _document->Identify( p, &node ); nuclear@19: if ( p == 0 || node == 0 ) { nuclear@19: break; nuclear@19: } nuclear@19: nuclear@19: StrPair endTag; nuclear@19: p = node->ParseDeep( p, &endTag ); nuclear@19: if ( !p ) { nuclear@19: DELETE_NODE( node ); nuclear@19: node = 0; nuclear@19: if ( !_document->Error() ) { nuclear@19: _document->SetError( XML_ERROR_PARSING, 0, 0 ); nuclear@19: } nuclear@19: break; nuclear@19: } nuclear@19: nuclear@19: // We read the end tag. Return it to the parent. nuclear@19: if ( node->ToElement() && node->ToElement()->ClosingType() == XMLElement::CLOSING ) { nuclear@19: if ( parentEnd ) { nuclear@19: *parentEnd = static_cast(node)->_value; nuclear@19: } nuclear@19: node->_memPool->SetTracked(); // created and then immediately deleted. nuclear@19: DELETE_NODE( node ); nuclear@19: return p; nuclear@19: } nuclear@19: nuclear@19: // Handle an end tag returned to this level. nuclear@19: // And handle a bunch of annoying errors. nuclear@19: XMLElement* ele = node->ToElement(); nuclear@19: if ( ele ) { nuclear@19: if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) { nuclear@19: _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 ); nuclear@19: p = 0; nuclear@19: } nuclear@19: else if ( !endTag.Empty() && ele->ClosingType() != XMLElement::OPEN ) { nuclear@19: _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 ); nuclear@19: p = 0; nuclear@19: } nuclear@19: else if ( !endTag.Empty() ) { nuclear@19: if ( !XMLUtil::StringEqual( endTag.GetStr(), node->Value() )) { nuclear@19: _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, node->Value(), 0 ); nuclear@19: p = 0; nuclear@19: } nuclear@19: } nuclear@19: } nuclear@19: if ( p == 0 ) { nuclear@19: DELETE_NODE( node ); nuclear@19: node = 0; nuclear@19: } nuclear@19: if ( node ) { nuclear@19: this->InsertEndChild( node ); nuclear@19: } nuclear@19: } nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: // --------- XMLText ---------- // nuclear@19: char* XMLText::ParseDeep( char* p, StrPair* ) nuclear@19: { nuclear@19: const char* start = p; nuclear@19: if ( this->CData() ) { nuclear@19: p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION ); nuclear@19: if ( !p ) { nuclear@19: _document->SetError( XML_ERROR_PARSING_CDATA, start, 0 ); nuclear@19: } nuclear@19: return p; nuclear@19: } nuclear@19: else { nuclear@19: int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES; nuclear@19: if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) { nuclear@19: flags |= StrPair::COLLAPSE_WHITESPACE; nuclear@19: } nuclear@19: nuclear@19: p = _value.ParseText( p, "<", flags ); nuclear@19: if ( !p ) { nuclear@19: _document->SetError( XML_ERROR_PARSING_TEXT, start, 0 ); nuclear@19: } nuclear@19: if ( p && *p ) { nuclear@19: return p-1; nuclear@19: } nuclear@19: } nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const nuclear@19: { nuclear@19: if ( !doc ) { nuclear@19: doc = _document; nuclear@19: } nuclear@19: XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern? nuclear@19: text->SetCData( this->CData() ); nuclear@19: return text; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLText::ShallowEqual( const XMLNode* compare ) const nuclear@19: { nuclear@19: return ( compare->ToText() && XMLUtil::StringEqual( compare->ToText()->Value(), Value() )); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLText::Accept( XMLVisitor* visitor ) const nuclear@19: { nuclear@19: return visitor->Visit( *this ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: // --------- XMLComment ---------- // nuclear@19: nuclear@19: XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc ) nuclear@19: { nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLComment::~XMLComment() nuclear@19: { nuclear@19: } nuclear@19: nuclear@19: nuclear@19: char* XMLComment::ParseDeep( char* p, StrPair* ) nuclear@19: { nuclear@19: // Comment parses as text. nuclear@19: const char* start = p; nuclear@19: p = _value.ParseText( p, "-->", StrPair::COMMENT ); nuclear@19: if ( p == 0 ) { nuclear@19: _document->SetError( XML_ERROR_PARSING_COMMENT, start, 0 ); nuclear@19: } nuclear@19: return p; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const nuclear@19: { nuclear@19: if ( !doc ) { nuclear@19: doc = _document; nuclear@19: } nuclear@19: XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern? nuclear@19: return comment; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLComment::ShallowEqual( const XMLNode* compare ) const nuclear@19: { nuclear@19: return ( compare->ToComment() && XMLUtil::StringEqual( compare->ToComment()->Value(), Value() )); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLComment::Accept( XMLVisitor* visitor ) const nuclear@19: { nuclear@19: return visitor->Visit( *this ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: // --------- XMLDeclaration ---------- // nuclear@19: nuclear@19: XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc ) nuclear@19: { nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLDeclaration::~XMLDeclaration() nuclear@19: { nuclear@19: //printf( "~XMLDeclaration\n" ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: char* XMLDeclaration::ParseDeep( char* p, StrPair* ) nuclear@19: { nuclear@19: // Declaration parses as text. nuclear@19: const char* start = p; nuclear@19: p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION ); nuclear@19: if ( p == 0 ) { nuclear@19: _document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0 ); nuclear@19: } nuclear@19: return p; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const nuclear@19: { nuclear@19: if ( !doc ) { nuclear@19: doc = _document; nuclear@19: } nuclear@19: XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern? nuclear@19: return dec; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const nuclear@19: { nuclear@19: return ( compare->ToDeclaration() && XMLUtil::StringEqual( compare->ToDeclaration()->Value(), Value() )); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: nuclear@19: bool XMLDeclaration::Accept( XMLVisitor* visitor ) const nuclear@19: { nuclear@19: return visitor->Visit( *this ); nuclear@19: } nuclear@19: nuclear@19: // --------- XMLUnknown ---------- // nuclear@19: nuclear@19: XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc ) nuclear@19: { nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLUnknown::~XMLUnknown() nuclear@19: { nuclear@19: } nuclear@19: nuclear@19: nuclear@19: char* XMLUnknown::ParseDeep( char* p, StrPair* ) nuclear@19: { nuclear@19: // Unknown parses as text. nuclear@19: const char* start = p; nuclear@19: nuclear@19: p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION ); nuclear@19: if ( !p ) { nuclear@19: _document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0 ); nuclear@19: } nuclear@19: return p; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const nuclear@19: { nuclear@19: if ( !doc ) { nuclear@19: doc = _document; nuclear@19: } nuclear@19: XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern? nuclear@19: return text; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const nuclear@19: { nuclear@19: return ( compare->ToUnknown() && XMLUtil::StringEqual( compare->ToUnknown()->Value(), Value() )); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLUnknown::Accept( XMLVisitor* visitor ) const nuclear@19: { nuclear@19: return visitor->Visit( *this ); nuclear@19: } nuclear@19: nuclear@19: // --------- XMLAttribute ---------- // nuclear@19: char* XMLAttribute::ParseDeep( char* p, bool processEntities ) nuclear@19: { nuclear@19: // Parse using the name rules: bug fix, was using ParseText before nuclear@19: p = _name.ParseName( p ); nuclear@19: if ( !p || !*p ) { nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: // Skip white space before = nuclear@19: p = XMLUtil::SkipWhiteSpace( p ); nuclear@19: if ( !p || *p != '=' ) { nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: ++p; // move up to opening quote nuclear@19: p = XMLUtil::SkipWhiteSpace( p ); nuclear@19: if ( *p != '\"' && *p != '\'' ) { nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: char endTag[2] = { *p, 0 }; nuclear@19: ++p; // move past opening quote nuclear@19: nuclear@19: p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES ); nuclear@19: return p; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLAttribute::SetName( const char* n ) nuclear@19: { nuclear@19: _name.SetStr( n ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLAttribute::QueryIntValue( int* value ) const nuclear@19: { nuclear@19: if ( XMLUtil::ToInt( Value(), value )) { nuclear@19: return XML_NO_ERROR; nuclear@19: } nuclear@19: return XML_WRONG_ATTRIBUTE_TYPE; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const nuclear@19: { nuclear@19: if ( XMLUtil::ToUnsigned( Value(), value )) { nuclear@19: return XML_NO_ERROR; nuclear@19: } nuclear@19: return XML_WRONG_ATTRIBUTE_TYPE; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLAttribute::QueryBoolValue( bool* value ) const nuclear@19: { nuclear@19: if ( XMLUtil::ToBool( Value(), value )) { nuclear@19: return XML_NO_ERROR; nuclear@19: } nuclear@19: return XML_WRONG_ATTRIBUTE_TYPE; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLAttribute::QueryFloatValue( float* value ) const nuclear@19: { nuclear@19: if ( XMLUtil::ToFloat( Value(), value )) { nuclear@19: return XML_NO_ERROR; nuclear@19: } nuclear@19: return XML_WRONG_ATTRIBUTE_TYPE; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLAttribute::QueryDoubleValue( double* value ) const nuclear@19: { nuclear@19: if ( XMLUtil::ToDouble( Value(), value )) { nuclear@19: return XML_NO_ERROR; nuclear@19: } nuclear@19: return XML_WRONG_ATTRIBUTE_TYPE; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLAttribute::SetAttribute( const char* v ) nuclear@19: { nuclear@19: _value.SetStr( v ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLAttribute::SetAttribute( int v ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( v, buf, BUF_SIZE ); nuclear@19: _value.SetStr( buf ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLAttribute::SetAttribute( unsigned v ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( v, buf, BUF_SIZE ); nuclear@19: _value.SetStr( buf ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLAttribute::SetAttribute( bool v ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( v, buf, BUF_SIZE ); nuclear@19: _value.SetStr( buf ); nuclear@19: } nuclear@19: nuclear@19: void XMLAttribute::SetAttribute( double v ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( v, buf, BUF_SIZE ); nuclear@19: _value.SetStr( buf ); nuclear@19: } nuclear@19: nuclear@19: void XMLAttribute::SetAttribute( float v ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( v, buf, BUF_SIZE ); nuclear@19: _value.SetStr( buf ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: // --------- XMLElement ---------- // nuclear@19: XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ), nuclear@19: _closingType( 0 ), nuclear@19: _rootAttribute( 0 ) nuclear@19: { nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLElement::~XMLElement() nuclear@19: { nuclear@19: while( _rootAttribute ) { nuclear@19: XMLAttribute* next = _rootAttribute->_next; nuclear@19: DELETE_ATTRIBUTE( _rootAttribute ); nuclear@19: _rootAttribute = next; nuclear@19: } nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLAttribute* XMLElement::FindAttribute( const char* name ) nuclear@19: { nuclear@19: XMLAttribute* a = 0; nuclear@19: for( a=_rootAttribute; a; a = a->_next ) { nuclear@19: if ( XMLUtil::StringEqual( a->Name(), name ) ) { nuclear@19: return a; nuclear@19: } nuclear@19: } nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: const XMLAttribute* XMLElement::FindAttribute( const char* name ) const nuclear@19: { nuclear@19: XMLAttribute* a = 0; nuclear@19: for( a=_rootAttribute; a; a = a->_next ) { nuclear@19: if ( XMLUtil::StringEqual( a->Name(), name ) ) { nuclear@19: return a; nuclear@19: } nuclear@19: } nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: const char* XMLElement::Attribute( const char* name, const char* value ) const nuclear@19: { nuclear@19: const XMLAttribute* a = FindAttribute( name ); nuclear@19: if ( !a ) { nuclear@19: return 0; nuclear@19: } nuclear@19: if ( !value || XMLUtil::StringEqual( a->Value(), value )) { nuclear@19: return a->Value(); nuclear@19: } nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: const char* XMLElement::GetText() const nuclear@19: { nuclear@19: if ( FirstChild() && FirstChild()->ToText() ) { nuclear@19: return FirstChild()->ToText()->Value(); nuclear@19: } nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLElement::QueryIntText( int* ival ) const nuclear@19: { nuclear@19: if ( FirstChild() && FirstChild()->ToText() ) { nuclear@19: const char* t = FirstChild()->ToText()->Value(); nuclear@19: if ( XMLUtil::ToInt( t, ival ) ) { nuclear@19: return XML_SUCCESS; nuclear@19: } nuclear@19: return XML_CAN_NOT_CONVERT_TEXT; nuclear@19: } nuclear@19: return XML_NO_TEXT_NODE; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const nuclear@19: { nuclear@19: if ( FirstChild() && FirstChild()->ToText() ) { nuclear@19: const char* t = FirstChild()->ToText()->Value(); nuclear@19: if ( XMLUtil::ToUnsigned( t, uval ) ) { nuclear@19: return XML_SUCCESS; nuclear@19: } nuclear@19: return XML_CAN_NOT_CONVERT_TEXT; nuclear@19: } nuclear@19: return XML_NO_TEXT_NODE; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLElement::QueryBoolText( bool* bval ) const nuclear@19: { nuclear@19: if ( FirstChild() && FirstChild()->ToText() ) { nuclear@19: const char* t = FirstChild()->ToText()->Value(); nuclear@19: if ( XMLUtil::ToBool( t, bval ) ) { nuclear@19: return XML_SUCCESS; nuclear@19: } nuclear@19: return XML_CAN_NOT_CONVERT_TEXT; nuclear@19: } nuclear@19: return XML_NO_TEXT_NODE; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLElement::QueryDoubleText( double* dval ) const nuclear@19: { nuclear@19: if ( FirstChild() && FirstChild()->ToText() ) { nuclear@19: const char* t = FirstChild()->ToText()->Value(); nuclear@19: if ( XMLUtil::ToDouble( t, dval ) ) { nuclear@19: return XML_SUCCESS; nuclear@19: } nuclear@19: return XML_CAN_NOT_CONVERT_TEXT; nuclear@19: } nuclear@19: return XML_NO_TEXT_NODE; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLElement::QueryFloatText( float* fval ) const nuclear@19: { nuclear@19: if ( FirstChild() && FirstChild()->ToText() ) { nuclear@19: const char* t = FirstChild()->ToText()->Value(); nuclear@19: if ( XMLUtil::ToFloat( t, fval ) ) { nuclear@19: return XML_SUCCESS; nuclear@19: } nuclear@19: return XML_CAN_NOT_CONVERT_TEXT; nuclear@19: } nuclear@19: return XML_NO_TEXT_NODE; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: nuclear@19: XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name ) nuclear@19: { nuclear@19: XMLAttribute* last = 0; nuclear@19: XMLAttribute* attrib = 0; nuclear@19: for( attrib = _rootAttribute; nuclear@19: attrib; nuclear@19: last = attrib, attrib = attrib->_next ) { nuclear@19: if ( XMLUtil::StringEqual( attrib->Name(), name ) ) { nuclear@19: break; nuclear@19: } nuclear@19: } nuclear@19: if ( !attrib ) { nuclear@19: attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); nuclear@19: attrib->_memPool = &_document->_attributePool; nuclear@19: if ( last ) { nuclear@19: last->_next = attrib; nuclear@19: } nuclear@19: else { nuclear@19: _rootAttribute = attrib; nuclear@19: } nuclear@19: attrib->SetName( name ); nuclear@19: attrib->_memPool->SetTracked(); // always created and linked. nuclear@19: } nuclear@19: return attrib; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLElement::DeleteAttribute( const char* name ) nuclear@19: { nuclear@19: XMLAttribute* prev = 0; nuclear@19: for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) { nuclear@19: if ( XMLUtil::StringEqual( name, a->Name() ) ) { nuclear@19: if ( prev ) { nuclear@19: prev->_next = a->_next; nuclear@19: } nuclear@19: else { nuclear@19: _rootAttribute = a->_next; nuclear@19: } nuclear@19: DELETE_ATTRIBUTE( a ); nuclear@19: break; nuclear@19: } nuclear@19: prev = a; nuclear@19: } nuclear@19: } nuclear@19: nuclear@19: nuclear@19: char* XMLElement::ParseAttributes( char* p ) nuclear@19: { nuclear@19: const char* start = p; nuclear@19: XMLAttribute* prevAttribute = 0; nuclear@19: nuclear@19: // Read the attributes. nuclear@19: while( p ) { nuclear@19: p = XMLUtil::SkipWhiteSpace( p ); nuclear@19: if ( !p || !(*p) ) { nuclear@19: _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() ); nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: // attribute. nuclear@19: if (XMLUtil::IsNameStartChar( *p ) ) { nuclear@19: XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); nuclear@19: attrib->_memPool = &_document->_attributePool; nuclear@19: attrib->_memPool->SetTracked(); nuclear@19: nuclear@19: p = attrib->ParseDeep( p, _document->ProcessEntities() ); nuclear@19: if ( !p || Attribute( attrib->Name() ) ) { nuclear@19: DELETE_ATTRIBUTE( attrib ); nuclear@19: _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p ); nuclear@19: return 0; nuclear@19: } nuclear@19: // There is a minor bug here: if the attribute in the source xml nuclear@19: // document is duplicated, it will not be detected and the nuclear@19: // attribute will be doubly added. However, tracking the 'prevAttribute' nuclear@19: // avoids re-scanning the attribute list. Preferring performance for nuclear@19: // now, may reconsider in the future. nuclear@19: if ( prevAttribute ) { nuclear@19: prevAttribute->_next = attrib; nuclear@19: } nuclear@19: else { nuclear@19: _rootAttribute = attrib; nuclear@19: } nuclear@19: prevAttribute = attrib; nuclear@19: } nuclear@19: // end of the tag nuclear@19: else if ( *p == '/' && *(p+1) == '>' ) { nuclear@19: _closingType = CLOSED; nuclear@19: return p+2; // done; sealed element. nuclear@19: } nuclear@19: // end of the tag nuclear@19: else if ( *p == '>' ) { nuclear@19: ++p; nuclear@19: break; nuclear@19: } nuclear@19: else { nuclear@19: _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p ); nuclear@19: return 0; nuclear@19: } nuclear@19: } nuclear@19: return p; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: // nuclear@19: // nuclear@19: // foobar nuclear@19: // nuclear@19: char* XMLElement::ParseDeep( char* p, StrPair* strPair ) nuclear@19: { nuclear@19: // Read the element name. nuclear@19: p = XMLUtil::SkipWhiteSpace( p ); nuclear@19: if ( !p ) { nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: // The closing element is the form. It is nuclear@19: // parsed just like a regular element then deleted from nuclear@19: // the DOM. nuclear@19: if ( *p == '/' ) { nuclear@19: _closingType = CLOSING; nuclear@19: ++p; nuclear@19: } nuclear@19: nuclear@19: p = _value.ParseName( p ); nuclear@19: if ( _value.Empty() ) { nuclear@19: return 0; nuclear@19: } nuclear@19: nuclear@19: p = ParseAttributes( p ); nuclear@19: if ( !p || !*p || _closingType ) { nuclear@19: return p; nuclear@19: } nuclear@19: nuclear@19: p = XMLNode::ParseDeep( p, strPair ); nuclear@19: return p; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: nuclear@19: XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const nuclear@19: { nuclear@19: if ( !doc ) { nuclear@19: doc = _document; nuclear@19: } nuclear@19: XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern? nuclear@19: for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) { nuclear@19: element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern? nuclear@19: } nuclear@19: return element; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLElement::ShallowEqual( const XMLNode* compare ) const nuclear@19: { nuclear@19: const XMLElement* other = compare->ToElement(); nuclear@19: if ( other && XMLUtil::StringEqual( other->Value(), Value() )) { nuclear@19: nuclear@19: const XMLAttribute* a=FirstAttribute(); nuclear@19: const XMLAttribute* b=other->FirstAttribute(); nuclear@19: nuclear@19: while ( a && b ) { nuclear@19: if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) { nuclear@19: return false; nuclear@19: } nuclear@19: a = a->Next(); nuclear@19: b = b->Next(); nuclear@19: } nuclear@19: if ( a || b ) { nuclear@19: // different count nuclear@19: return false; nuclear@19: } nuclear@19: return true; nuclear@19: } nuclear@19: return false; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLElement::Accept( XMLVisitor* visitor ) const nuclear@19: { nuclear@19: if ( visitor->VisitEnter( *this, _rootAttribute ) ) { nuclear@19: for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) { nuclear@19: if ( !node->Accept( visitor ) ) { nuclear@19: break; nuclear@19: } nuclear@19: } nuclear@19: } nuclear@19: return visitor->VisitExit( *this ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: // --------- XMLDocument ----------- // nuclear@19: XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) : nuclear@19: XMLNode( 0 ), nuclear@19: _writeBOM( false ), nuclear@19: _processEntities( processEntities ), nuclear@19: _errorID( XML_NO_ERROR ), nuclear@19: _whitespace( whitespace ), nuclear@19: _errorStr1( 0 ), nuclear@19: _errorStr2( 0 ), nuclear@19: _charBuffer( 0 ) nuclear@19: { nuclear@19: _document = this; // avoid warning about 'this' in initializer list nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLDocument::~XMLDocument() nuclear@19: { nuclear@19: DeleteChildren(); nuclear@19: delete [] _charBuffer; nuclear@19: nuclear@19: #if 0 nuclear@19: _textPool.Trace( "text" ); nuclear@19: _elementPool.Trace( "element" ); nuclear@19: _commentPool.Trace( "comment" ); nuclear@19: _attributePool.Trace( "attribute" ); nuclear@19: #endif nuclear@19: nuclear@19: #ifdef DEBUG nuclear@19: if ( Error() == false ) { nuclear@19: TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() ); nuclear@19: TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() ); nuclear@19: TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() ); nuclear@19: TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() ); nuclear@19: } nuclear@19: #endif nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLDocument::Clear() nuclear@19: { nuclear@19: DeleteChildren(); nuclear@19: nuclear@19: _errorID = XML_NO_ERROR; nuclear@19: _errorStr1 = 0; nuclear@19: _errorStr2 = 0; nuclear@19: nuclear@19: delete [] _charBuffer; nuclear@19: _charBuffer = 0; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLElement* XMLDocument::NewElement( const char* name ) nuclear@19: { nuclear@19: XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this ); nuclear@19: ele->_memPool = &_elementPool; nuclear@19: ele->SetName( name ); nuclear@19: return ele; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLComment* XMLDocument::NewComment( const char* str ) nuclear@19: { nuclear@19: XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this ); nuclear@19: comment->_memPool = &_commentPool; nuclear@19: comment->SetValue( str ); nuclear@19: return comment; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLText* XMLDocument::NewText( const char* str ) nuclear@19: { nuclear@19: XMLText* text = new (_textPool.Alloc()) XMLText( this ); nuclear@19: text->_memPool = &_textPool; nuclear@19: text->SetValue( str ); nuclear@19: return text; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLDeclaration* XMLDocument::NewDeclaration( const char* str ) nuclear@19: { nuclear@19: XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this ); nuclear@19: dec->_memPool = &_commentPool; nuclear@19: dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" ); nuclear@19: return dec; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLUnknown* XMLDocument::NewUnknown( const char* str ) nuclear@19: { nuclear@19: XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this ); nuclear@19: unk->_memPool = &_commentPool; nuclear@19: unk->SetValue( str ); nuclear@19: return unk; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLDocument::LoadFile( const char* filename ) nuclear@19: { nuclear@19: Clear(); nuclear@19: FILE* fp = 0; nuclear@19: nuclear@19: #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) nuclear@19: errno_t err = fopen_s(&fp, filename, "rb" ); nuclear@19: if ( !fp || err) { nuclear@19: #else nuclear@19: fp = fopen( filename, "rb" ); nuclear@19: if ( !fp) { nuclear@19: #endif nuclear@19: SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 ); nuclear@19: return _errorID; nuclear@19: } nuclear@19: LoadFile( fp ); nuclear@19: fclose( fp ); nuclear@19: return _errorID; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLDocument::LoadFile( FILE* fp ) nuclear@19: { nuclear@19: Clear(); nuclear@19: nuclear@19: fseek( fp, 0, SEEK_END ); nuclear@19: size_t size = ftell( fp ); nuclear@19: fseek( fp, 0, SEEK_SET ); nuclear@19: nuclear@19: if ( size == 0 ) { nuclear@19: SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); nuclear@19: return _errorID; nuclear@19: } nuclear@19: nuclear@19: _charBuffer = new char[size+1]; nuclear@19: size_t read = fread( _charBuffer, 1, size, fp ); nuclear@19: if ( read != size ) { nuclear@19: SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 ); nuclear@19: return _errorID; nuclear@19: } nuclear@19: nuclear@19: _charBuffer[size] = 0; nuclear@19: nuclear@19: const char* p = _charBuffer; nuclear@19: p = XMLUtil::SkipWhiteSpace( p ); nuclear@19: p = XMLUtil::ReadBOM( p, &_writeBOM ); nuclear@19: if ( !p || !*p ) { nuclear@19: SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); nuclear@19: return _errorID; nuclear@19: } nuclear@19: nuclear@19: ParseDeep( _charBuffer + (p-_charBuffer), 0 ); nuclear@19: return _errorID; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLDocument::SaveFile( const char* filename, bool compact ) nuclear@19: { nuclear@19: FILE* fp = 0; nuclear@19: #if defined(_MSC_VER) && (_MSC_VER >= 1400 ) nuclear@19: errno_t err = fopen_s(&fp, filename, "w" ); nuclear@19: if ( !fp || err) { nuclear@19: #else nuclear@19: fp = fopen( filename, "w" ); nuclear@19: if ( !fp) { nuclear@19: #endif nuclear@19: SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 ); nuclear@19: return _errorID; nuclear@19: } nuclear@19: SaveFile(fp, compact); nuclear@19: fclose( fp ); nuclear@19: return _errorID; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLDocument::SaveFile( FILE* fp, bool compact ) nuclear@19: { nuclear@19: XMLPrinter stream( fp, compact ); nuclear@19: Print( &stream ); nuclear@19: return _errorID; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLError XMLDocument::Parse( const char* p, size_t len ) nuclear@19: { nuclear@19: const char* start = p; nuclear@19: Clear(); nuclear@19: nuclear@19: if ( !p || !*p ) { nuclear@19: SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); nuclear@19: return _errorID; nuclear@19: } nuclear@19: if ( len == (size_t)(-1) ) { nuclear@19: len = strlen( p ); nuclear@19: } nuclear@19: _charBuffer = new char[ len+1 ]; nuclear@19: memcpy( _charBuffer, p, len ); nuclear@19: _charBuffer[len] = 0; nuclear@19: nuclear@19: p = XMLUtil::SkipWhiteSpace( p ); nuclear@19: p = XMLUtil::ReadBOM( p, &_writeBOM ); nuclear@19: if ( !p || !*p ) { nuclear@19: SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); nuclear@19: return _errorID; nuclear@19: } nuclear@19: nuclear@19: ptrdiff_t delta = p - start; // skip initial whitespace, BOM, etc. nuclear@19: ParseDeep( _charBuffer+delta, 0 ); nuclear@19: return _errorID; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLDocument::Print( XMLPrinter* streamer ) const nuclear@19: { nuclear@19: XMLPrinter stdStreamer( stdout ); nuclear@19: if ( !streamer ) { nuclear@19: streamer = &stdStreamer; nuclear@19: } nuclear@19: Accept( streamer ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLDocument::SetError( XMLError error, const char* str1, const char* str2 ) nuclear@19: { nuclear@19: _errorID = error; nuclear@19: _errorStr1 = str1; nuclear@19: _errorStr2 = str2; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLDocument::PrintError() const nuclear@19: { nuclear@19: if ( _errorID ) { nuclear@19: static const int LEN = 20; nuclear@19: char buf1[LEN] = { 0 }; nuclear@19: char buf2[LEN] = { 0 }; nuclear@19: nuclear@19: if ( _errorStr1 ) { nuclear@19: TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1 ); nuclear@19: } nuclear@19: if ( _errorStr2 ) { nuclear@19: TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2 ); nuclear@19: } nuclear@19: nuclear@19: printf( "XMLDocument error id=%d str1=%s str2=%s\n", nuclear@19: _errorID, buf1, buf2 ); nuclear@19: } nuclear@19: } nuclear@19: nuclear@19: nuclear@19: XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) : nuclear@19: _elementJustOpened( false ), nuclear@19: _firstElement( true ), nuclear@19: _fp( file ), nuclear@19: _depth( depth ), nuclear@19: _textDepth( -1 ), nuclear@19: _processEntities( true ), nuclear@19: _compactMode( compact ) nuclear@19: { nuclear@19: for( int i=0; i'] = true; // not required, but consistency is nice nuclear@19: _buffer.Push( 0 ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::Print( const char* format, ... ) nuclear@19: { nuclear@19: va_list va; nuclear@19: va_start( va, format ); nuclear@19: nuclear@19: if ( _fp ) { nuclear@19: vfprintf( _fp, format, va ); nuclear@19: } nuclear@19: else { nuclear@19: // This seems brutally complex. Haven't figured out a better nuclear@19: // way on windows. nuclear@19: #ifdef _MSC_VER nuclear@19: int len = -1; nuclear@19: int expand = 1000; nuclear@19: while ( len < 0 ) { nuclear@19: len = vsnprintf_s( _accumulator.Mem(), _accumulator.Capacity(), _TRUNCATE, format, va ); nuclear@19: if ( len < 0 ) { nuclear@19: expand *= 3/2; nuclear@19: _accumulator.PushArr( expand ); nuclear@19: } nuclear@19: } nuclear@19: char* p = _buffer.PushArr( len ) - 1; nuclear@19: memcpy( p, _accumulator.Mem(), len+1 ); nuclear@19: #else nuclear@19: int len = vsnprintf( 0, 0, format, va ); nuclear@19: // Close out and re-start the va-args nuclear@19: va_end( va ); nuclear@19: va_start( va, format ); nuclear@19: char* p = _buffer.PushArr( len ) - 1; nuclear@19: vsnprintf( p, len+1, format, va ); nuclear@19: #endif nuclear@19: } nuclear@19: va_end( va ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PrintSpace( int depth ) nuclear@19: { nuclear@19: for( int i=0; i 0 && *q < ENTITY_RANGE ) { nuclear@19: // Check for entities. If one is found, flush nuclear@19: // the stream up until the entity, write the nuclear@19: // entity, and keep looking. nuclear@19: if ( flag[(unsigned)(*q)] ) { nuclear@19: while ( p < q ) { nuclear@19: Print( "%c", *p ); nuclear@19: ++p; nuclear@19: } nuclear@19: for( int i=0; i 0) ) { nuclear@19: Print( "%s", p ); nuclear@19: } nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushHeader( bool writeBOM, bool writeDec ) nuclear@19: { nuclear@19: if ( writeBOM ) { nuclear@19: static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 }; nuclear@19: Print( "%s", bom ); nuclear@19: } nuclear@19: if ( writeDec ) { nuclear@19: PushDeclaration( "xml version=\"1.0\"" ); nuclear@19: } nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::OpenElement( const char* name ) nuclear@19: { nuclear@19: if ( _elementJustOpened ) { nuclear@19: SealElement(); nuclear@19: } nuclear@19: _stack.Push( name ); nuclear@19: nuclear@19: if ( _textDepth < 0 && !_firstElement && !_compactMode ) { nuclear@19: Print( "\n" ); nuclear@19: } nuclear@19: if ( !_compactMode ) { nuclear@19: PrintSpace( _depth ); nuclear@19: } nuclear@19: nuclear@19: Print( "<%s", name ); nuclear@19: _elementJustOpened = true; nuclear@19: _firstElement = false; nuclear@19: ++_depth; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushAttribute( const char* name, const char* value ) nuclear@19: { nuclear@19: TIXMLASSERT( _elementJustOpened ); nuclear@19: Print( " %s=\"", name ); nuclear@19: PrintString( value, false ); nuclear@19: Print( "\"" ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushAttribute( const char* name, int v ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( v, buf, BUF_SIZE ); nuclear@19: PushAttribute( name, buf ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushAttribute( const char* name, unsigned v ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( v, buf, BUF_SIZE ); nuclear@19: PushAttribute( name, buf ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushAttribute( const char* name, bool v ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( v, buf, BUF_SIZE ); nuclear@19: PushAttribute( name, buf ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushAttribute( const char* name, double v ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( v, buf, BUF_SIZE ); nuclear@19: PushAttribute( name, buf ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::CloseElement() nuclear@19: { nuclear@19: --_depth; nuclear@19: const char* name = _stack.Pop(); nuclear@19: nuclear@19: if ( _elementJustOpened ) { nuclear@19: Print( "/>" ); nuclear@19: } nuclear@19: else { nuclear@19: if ( _textDepth < 0 && !_compactMode) { nuclear@19: Print( "\n" ); nuclear@19: PrintSpace( _depth ); nuclear@19: } nuclear@19: Print( "", name ); nuclear@19: } nuclear@19: nuclear@19: if ( _textDepth == _depth ) { nuclear@19: _textDepth = -1; nuclear@19: } nuclear@19: if ( _depth == 0 && !_compactMode) { nuclear@19: Print( "\n" ); nuclear@19: } nuclear@19: _elementJustOpened = false; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::SealElement() nuclear@19: { nuclear@19: _elementJustOpened = false; nuclear@19: Print( ">" ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushText( const char* text, bool cdata ) nuclear@19: { nuclear@19: _textDepth = _depth-1; nuclear@19: nuclear@19: if ( _elementJustOpened ) { nuclear@19: SealElement(); nuclear@19: } nuclear@19: if ( cdata ) { nuclear@19: Print( "" ); nuclear@19: } nuclear@19: else { nuclear@19: PrintString( text, true ); nuclear@19: } nuclear@19: } nuclear@19: nuclear@19: void XMLPrinter::PushText( int value ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( value, buf, BUF_SIZE ); nuclear@19: PushText( buf, false ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushText( unsigned value ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( value, buf, BUF_SIZE ); nuclear@19: PushText( buf, false ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushText( bool value ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( value, buf, BUF_SIZE ); nuclear@19: PushText( buf, false ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushText( float value ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( value, buf, BUF_SIZE ); nuclear@19: PushText( buf, false ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushText( double value ) nuclear@19: { nuclear@19: char buf[BUF_SIZE]; nuclear@19: XMLUtil::ToStr( value, buf, BUF_SIZE ); nuclear@19: PushText( buf, false ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushComment( const char* comment ) nuclear@19: { nuclear@19: if ( _elementJustOpened ) { nuclear@19: SealElement(); nuclear@19: } nuclear@19: if ( _textDepth < 0 && !_firstElement && !_compactMode) { nuclear@19: Print( "\n" ); nuclear@19: PrintSpace( _depth ); nuclear@19: } nuclear@19: _firstElement = false; nuclear@19: Print( "", comment ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushDeclaration( const char* value ) nuclear@19: { nuclear@19: if ( _elementJustOpened ) { nuclear@19: SealElement(); nuclear@19: } nuclear@19: if ( _textDepth < 0 && !_firstElement && !_compactMode) { nuclear@19: Print( "\n" ); nuclear@19: PrintSpace( _depth ); nuclear@19: } nuclear@19: _firstElement = false; nuclear@19: Print( "", value ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: void XMLPrinter::PushUnknown( const char* value ) nuclear@19: { nuclear@19: if ( _elementJustOpened ) { nuclear@19: SealElement(); nuclear@19: } nuclear@19: if ( _textDepth < 0 && !_firstElement && !_compactMode) { nuclear@19: Print( "\n" ); nuclear@19: PrintSpace( _depth ); nuclear@19: } nuclear@19: _firstElement = false; nuclear@19: Print( "", value ); nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLPrinter::VisitEnter( const XMLDocument& doc ) nuclear@19: { nuclear@19: _processEntities = doc.ProcessEntities(); nuclear@19: if ( doc.HasBOM() ) { nuclear@19: PushHeader( true, false ); nuclear@19: } nuclear@19: return true; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) nuclear@19: { nuclear@19: OpenElement( element.Name() ); nuclear@19: while ( attribute ) { nuclear@19: PushAttribute( attribute->Name(), attribute->Value() ); nuclear@19: attribute = attribute->Next(); nuclear@19: } nuclear@19: return true; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLPrinter::VisitExit( const XMLElement& ) nuclear@19: { nuclear@19: CloseElement(); nuclear@19: return true; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLPrinter::Visit( const XMLText& text ) nuclear@19: { nuclear@19: PushText( text.Value(), text.CData() ); nuclear@19: return true; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLPrinter::Visit( const XMLComment& comment ) nuclear@19: { nuclear@19: PushComment( comment.Value() ); nuclear@19: return true; nuclear@19: } nuclear@19: nuclear@19: bool XMLPrinter::Visit( const XMLDeclaration& declaration ) nuclear@19: { nuclear@19: PushDeclaration( declaration.Value() ); nuclear@19: return true; nuclear@19: } nuclear@19: nuclear@19: nuclear@19: bool XMLPrinter::Visit( const XMLUnknown& unknown ) nuclear@19: { nuclear@19: PushUnknown( unknown.Value() ); nuclear@19: return true; nuclear@19: } nuclear@19: nuclear@19: } // namespace tinyxml2 nuclear@19: