Updated TinyXML

This commit is contained in:
PKEuS 2018-01-08 20:23:46 +01:00
parent b684e1f202
commit d76c5c525c
2 changed files with 193 additions and 121 deletions

View File

@ -165,6 +165,7 @@ void StrPair::TransferTo( StrPair* other )
_end = 0; _end = 0;
} }
void StrPair::Reset() void StrPair::Reset()
{ {
if ( _flags & NEEDS_DELETE ) { if ( _flags & NEEDS_DELETE ) {
@ -741,6 +742,7 @@ bool XMLDocument::Accept( XMLVisitor* visitor ) const
XMLNode::XMLNode( XMLDocument* doc ) : XMLNode::XMLNode( XMLDocument* doc ) :
_document( doc ), _document( doc ),
_parent( 0 ), _parent( 0 ),
_value(),
_parseLineNum( 0 ), _parseLineNum( 0 ),
_firstChild( 0 ), _lastChild( 0 ), _firstChild( 0 ), _lastChild( 0 ),
_prev( 0 ), _next( 0 ), _prev( 0 ), _next( 0 ),
@ -911,6 +913,13 @@ XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
TIXMLASSERT( false ); TIXMLASSERT( false );
return 0; return 0;
} }
if ( afterThis == addThis ) {
// Current state: BeforeThis -> AddThis -> OneAfterAddThis
// Now AddThis must disappear from it's location and then
// reappear between BeforeThis and OneAfterAddThis.
// So just leave it where it is.
return addThis;
}
if ( afterThis->_next == 0 ) { if ( afterThis->_next == 0 ) {
// The last node or the only node. // The last node or the only node.
@ -1011,7 +1020,7 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr )
if ( !p ) { if ( !p ) {
DeleteNode( node ); DeleteNode( node );
if ( !_document->Error() ) { if ( !_document->Error() ) {
_document->SetError( XML_ERROR_PARSING, 0, 0, initialLineNum); _document->SetError( XML_ERROR_PARSING, initialLineNum, 0);
} }
break; break;
} }
@ -1031,7 +1040,7 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr )
} }
} }
if ( !wellLocated ) { if ( !wellLocated ) {
_document->SetError( XML_ERROR_PARSING_DECLARATION, decl->Value(), 0, initialLineNum); _document->SetError( XML_ERROR_PARSING_DECLARATION, initialLineNum, "XMLDeclaration value=%s", decl->Value());
DeleteNode( node ); DeleteNode( node );
break; break;
} }
@ -1066,7 +1075,7 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr )
} }
} }
if ( mismatch ) { if ( mismatch ) {
_document->SetError( XML_ERROR_MISMATCHED_ELEMENT, ele->Name(), 0, initialLineNum); _document->SetError( XML_ERROR_MISMATCHED_ELEMENT, initialLineNum, "XMLElement name=%s", ele->Name());
DeleteNode( node ); DeleteNode( node );
break; break;
} }
@ -1123,11 +1132,10 @@ const XMLElement* XMLNode::ToElementWithName( const char* name ) const
// --------- XMLText ---------- // // --------- XMLText ---------- //
char* XMLText::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) char* XMLText::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
{ {
const char* start = p;
if ( this->CData() ) { if ( this->CData() ) {
p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr ); p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr );
if ( !p ) { if ( !p ) {
_document->SetError( XML_ERROR_PARSING_CDATA, start, 0, _parseLineNum ); _document->SetError( XML_ERROR_PARSING_CDATA, _parseLineNum, 0 );
} }
return p; return p;
} }
@ -1142,7 +1150,7 @@ char* XMLText::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
return p-1; return p-1;
} }
if ( !p ) { if ( !p ) {
_document->SetError( XML_ERROR_PARSING_TEXT, start, 0, _parseLineNum ); _document->SetError( XML_ERROR_PARSING_TEXT, _parseLineNum, 0 );
} }
} }
return 0; return 0;
@ -1190,10 +1198,9 @@ XMLComment::~XMLComment()
char* XMLComment::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) char* XMLComment::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
{ {
// Comment parses as text. // Comment parses as text.
const char* start = p;
p = _value.ParseText( p, "-->", StrPair::COMMENT, curLineNumPtr ); p = _value.ParseText( p, "-->", StrPair::COMMENT, curLineNumPtr );
if ( p == 0 ) { if ( p == 0 ) {
_document->SetError( XML_ERROR_PARSING_COMMENT, start, 0, _parseLineNum ); _document->SetError( XML_ERROR_PARSING_COMMENT, _parseLineNum, 0 );
} }
return p; return p;
} }
@ -1240,10 +1247,9 @@ XMLDeclaration::~XMLDeclaration()
char* XMLDeclaration::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) char* XMLDeclaration::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
{ {
// Declaration parses as text. // Declaration parses as text.
const char* start = p;
p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr ); p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr );
if ( p == 0 ) { if ( p == 0 ) {
_document->SetError( XML_ERROR_PARSING_DECLARATION, start, 0, _parseLineNum ); _document->SetError( XML_ERROR_PARSING_DECLARATION, _parseLineNum, 0 );
} }
return p; return p;
} }
@ -1289,11 +1295,9 @@ XMLUnknown::~XMLUnknown()
char* XMLUnknown::ParseDeep( char* p, StrPair*, int* curLineNumPtr ) char* XMLUnknown::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
{ {
// Unknown parses as text. // Unknown parses as text.
const char* start = p;
p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr ); p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr );
if ( !p ) { if ( !p ) {
_document->SetError( XML_ERROR_PARSING_UNKNOWN, start, 0, _parseLineNum ); _document->SetError( XML_ERROR_PARSING_UNKNOWN, _parseLineNum, 0 );
} }
return p; return p;
} }
@ -1796,14 +1800,13 @@ void XMLElement::DeleteAttribute( const char* name )
char* XMLElement::ParseAttributes( char* p, int* curLineNumPtr ) char* XMLElement::ParseAttributes( char* p, int* curLineNumPtr )
{ {
const char* start = p;
XMLAttribute* prevAttribute = 0; XMLAttribute* prevAttribute = 0;
// Read the attributes. // Read the attributes.
while( p ) { while( p ) {
p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr );
if ( !(*p) ) { if ( !(*p) ) {
_document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name(), _parseLineNum ); _document->SetError( XML_ERROR_PARSING_ELEMENT, _parseLineNum, "XMLElement name=%s", Name() );
return 0; return 0;
} }
@ -1818,7 +1821,7 @@ char* XMLElement::ParseAttributes( char* p, int* curLineNumPtr )
p = attrib->ParseDeep( p, _document->ProcessEntities(), curLineNumPtr ); p = attrib->ParseDeep( p, _document->ProcessEntities(), curLineNumPtr );
if ( !p || Attribute( attrib->Name() ) ) { if ( !p || Attribute( attrib->Name() ) ) {
DeleteAttribute( attrib ); DeleteAttribute( attrib );
_document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p, attrLineNum ); _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, attrLineNum, "XMLElement name=%s", Name() );
return 0; return 0;
} }
// There is a minor bug here: if the attribute in the source xml // There is a minor bug here: if the attribute in the source xml
@ -1847,7 +1850,7 @@ char* XMLElement::ParseAttributes( char* p, int* curLineNumPtr )
return p+2; // done; sealed element. return p+2; // done; sealed element.
} }
else { else {
_document->SetError( XML_ERROR_PARSING_ELEMENT, start, p, _parseLineNum ); _document->SetError( XML_ERROR_PARSING_ELEMENT, _parseLineNum, 0 );
return 0; return 0;
} }
} }
@ -1993,9 +1996,15 @@ XMLDocument::XMLDocument( bool processEntities, Whitespace whitespaceMode ) :
_processEntities( processEntities ), _processEntities( processEntities ),
_errorID(XML_SUCCESS), _errorID(XML_SUCCESS),
_whitespaceMode( whitespaceMode ), _whitespaceMode( whitespaceMode ),
_errorStr(),
_errorLineNum( 0 ), _errorLineNum( 0 ),
_charBuffer( 0 ), _charBuffer( 0 ),
_parseCurLineNum( 0 ) _parseCurLineNum( 0 ),
_unlinked(),
_elementPool(),
_attributePool(),
_textPool(),
_commentPool()
{ {
// avoid VC++ C4355 warning about 'this' in initializer list (C4355 is off by default in VS2012+) // avoid VC++ C4355 warning about 'this' in initializer list (C4355 is off by default in VS2012+)
_document = this; _document = this;
@ -2054,7 +2063,7 @@ void XMLDocument::Clear()
} }
void XMLDocument::DeepCopy(XMLDocument* target) void XMLDocument::DeepCopy(XMLDocument* target) const
{ {
TIXMLASSERT(target); TIXMLASSERT(target);
if (target == this) { if (target == this) {
@ -2145,7 +2154,7 @@ XMLError XMLDocument::LoadFile( const char* filename )
Clear(); Clear();
FILE* fp = callfopen( filename, "rb" ); FILE* fp = callfopen( filename, "rb" );
if ( !fp ) { if ( !fp ) {
SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0, 0 ); SetError( XML_ERROR_FILE_NOT_FOUND, 0, "filename=%s", filename ? filename : "<null>");
return _errorID; return _errorID;
} }
LoadFile( fp ); LoadFile( fp );
@ -2182,7 +2191,7 @@ XMLError XMLDocument::LoadFile( FILE* fp )
fseek( fp, 0, SEEK_SET ); fseek( fp, 0, SEEK_SET );
if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) { if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0, 0 ); SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
return _errorID; return _errorID;
} }
@ -2190,19 +2199,19 @@ XMLError XMLDocument::LoadFile( FILE* fp )
const long filelength = ftell( fp ); const long filelength = ftell( fp );
fseek( fp, 0, SEEK_SET ); fseek( fp, 0, SEEK_SET );
if ( filelength == -1L ) { if ( filelength == -1L ) {
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0, 0 ); SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
return _errorID; return _errorID;
} }
TIXMLASSERT( filelength >= 0 ); TIXMLASSERT( filelength >= 0 );
if ( !LongFitsIntoSizeTMinusOne<>::Fits( filelength ) ) { if ( !LongFitsIntoSizeTMinusOne<>::Fits( filelength ) ) {
// Cannot handle files which won't fit in buffer together with null terminator // Cannot handle files which won't fit in buffer together with null terminator
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0, 0 ); SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
return _errorID; return _errorID;
} }
if ( filelength == 0 ) { if ( filelength == 0 ) {
SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0, 0 ); SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
return _errorID; return _errorID;
} }
@ -2211,7 +2220,7 @@ XMLError XMLDocument::LoadFile( FILE* fp )
_charBuffer = new char[size+1]; _charBuffer = new char[size+1];
size_t read = fread( _charBuffer, 1, size, fp ); size_t read = fread( _charBuffer, 1, size, fp );
if ( read != size ) { if ( read != size ) {
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0, 0 ); SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
return _errorID; return _errorID;
} }
@ -2226,7 +2235,7 @@ XMLError XMLDocument::SaveFile( const char* filename, bool compact )
{ {
FILE* fp = callfopen( filename, "w" ); FILE* fp = callfopen( filename, "w" );
if ( !fp ) { if ( !fp ) {
SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0, 0 ); SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=%s", filename ? filename : "<null>");
return _errorID; return _errorID;
} }
SaveFile(fp, compact); SaveFile(fp, compact);
@ -2251,7 +2260,7 @@ XMLError XMLDocument::Parse( const char* p, size_t len )
Clear(); Clear();
if ( len == 0 || !p || !*p ) { if ( len == 0 || !p || !*p ) {
SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0, 0 ); SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
return _errorID; return _errorID;
} }
if ( len == (size_t)(-1) ) { if ( len == (size_t)(-1) ) {
@ -2289,21 +2298,33 @@ void XMLDocument::Print( XMLPrinter* streamer ) const
} }
void XMLDocument::SetError( XMLError error, const char* str1, const char* str2, int lineNum ) void XMLDocument::SetError( XMLError error, int lineNum, const char* format, ... )
{ {
TIXMLASSERT( error >= 0 && error < XML_ERROR_COUNT ); TIXMLASSERT( error >= 0 && error < XML_ERROR_COUNT );
_errorID = error; _errorID = error;
_errorStr1.Reset();
_errorStr2.Reset();
_errorLineNum = lineNum; _errorLineNum = lineNum;
_errorStr.Reset();
if (str1) size_t BUFFER_SIZE = 1000;
_errorStr1.SetStr(str1); char* buffer = new char[BUFFER_SIZE];
if (str2)
_errorStr2.SetStr(str2); TIXML_SNPRINTF(buffer, BUFFER_SIZE, "Error=%s ErrorID=%d (0x%x) Line number=%d", ErrorIDToName(error), int(error), int(error), lineNum);
if (format) {
size_t len = strlen(buffer);
TIXML_SNPRINTF(buffer + len, BUFFER_SIZE - len, ": ");
len = strlen(buffer);
va_list va;
va_start(va, format);
TIXML_VSNPRINTF(buffer + len, BUFFER_SIZE - len, format, va);
va_end(va);
}
_errorStr.SetStr(buffer);
delete[] buffer;
} }
/*static*/ const char* XMLDocument::ErrorIDToName(XMLError errorID) /*static*/ const char* XMLDocument::ErrorIDToName(XMLError errorID)
{ {
TIXMLASSERT( errorID >= 0 && errorID < XML_ERROR_COUNT ); TIXMLASSERT( errorID >= 0 && errorID < XML_ERROR_COUNT );
@ -2312,14 +2333,15 @@ void XMLDocument::SetError( XMLError error, const char* str1, const char* str2,
return errorName; return errorName;
} }
const char* XMLDocument::GetErrorStr1() const const char* XMLDocument::ErrorStr() const
{ {
return _errorStr1.GetStr(); return _errorStr.Empty() ? "" : _errorStr.GetStr();
} }
const char* XMLDocument::GetErrorStr2() const
void XMLDocument::PrintError() const
{ {
return _errorStr2.GetStr(); printf("%s\n", ErrorStr());
} }
const char* XMLDocument::ErrorName() const const char* XMLDocument::ErrorName() const
@ -2327,28 +2349,6 @@ const char* XMLDocument::ErrorName() const
return ErrorIDToName(_errorID); return ErrorIDToName(_errorID);
} }
void XMLDocument::PrintError() const
{
if ( Error() ) {
static const int LEN = 20;
char buf1[LEN] = { 0 };
char buf2[LEN] = { 0 };
if ( !_errorStr1.Empty() ) {
TIXML_SNPRINTF( buf1, LEN, "%s", _errorStr1.GetStr() );
}
if ( !_errorStr2.Empty() ) {
TIXML_SNPRINTF( buf2, LEN, "%s", _errorStr2.GetStr() );
}
// Should check INT_MIN <= _errorID && _errorId <= INT_MAX, but that
// causes a clang "always true" -Wtautological-constant-out-of-range-compare warning
TIXMLASSERT( 0 <= _errorID && XML_ERROR_COUNT - 1 <= INT_MAX );
printf( "XMLDocument error id=%d '%s' str1=%s str2=%s line=%d\n",
static_cast<int>( _errorID ), ErrorName(), buf1, buf2, _errorLineNum );
}
}
void XMLDocument::Parse() void XMLDocument::Parse()
{ {
TIXMLASSERT( NoChildren() ); // Clear() must have been called previously TIXMLASSERT( NoChildren() ); // Clear() must have been called previously
@ -2359,7 +2359,7 @@ void XMLDocument::Parse()
p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum ); p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum );
p = const_cast<char*>( XMLUtil::ReadBOM( p, &_writeBOM ) ); p = const_cast<char*>( XMLUtil::ReadBOM( p, &_writeBOM ) );
if ( !*p ) { if ( !*p ) {
SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0, 0 ); SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
return; return;
} }
ParseDeep(p, 0, &_parseCurLineNum ); ParseDeep(p, 0, &_parseCurLineNum );
@ -2367,12 +2367,14 @@ void XMLDocument::Parse()
XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) : XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
_elementJustOpened( false ), _elementJustOpened( false ),
_stack(),
_firstElement( true ), _firstElement( true ),
_fp( file ), _fp( file ),
_depth( depth ), _depth( depth ),
_textDepth( -1 ), _textDepth( -1 ),
_processEntities( true ), _processEntities( true ),
_compactMode( compact ) _compactMode( compact ),
_buffer()
{ {
for( int i=0; i<ENTITY_RANGE; ++i ) { for( int i=0; i<ENTITY_RANGE; ++i ) {
_entityFlag[i] = false; _entityFlag[i] = false;
@ -2380,8 +2382,9 @@ XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
} }
for( int i=0; i<NUM_ENTITIES; ++i ) { for( int i=0; i<NUM_ENTITIES; ++i ) {
const char entityValue = entities[i].value; const char entityValue = entities[i].value;
TIXMLASSERT( ((unsigned char)entityValue) < ENTITY_RANGE ); const unsigned char flagIndex = (unsigned char)entityValue;
_entityFlag[ (unsigned char)entityValue ] = true; TIXMLASSERT( flagIndex < ENTITY_RANGE );
_entityFlag[flagIndex] = true;
} }
_restrictedEntityFlag[(unsigned char)'&'] = true; _restrictedEntityFlag[(unsigned char)'&'] = true;
_restrictedEntityFlag[(unsigned char)'<'] = true; _restrictedEntityFlag[(unsigned char)'<'] = true;
@ -2412,10 +2415,36 @@ void XMLPrinter::Print( const char* format, ... )
} }
void XMLPrinter::Write( const char* data, size_t size )
{
if ( _fp ) {
fwrite ( data , sizeof(char), size, _fp);
}
else {
char* p = _buffer.PushArr( static_cast<int>(size) ) - 1; // back up over the null terminator.
memcpy( p, data, size );
p[size] = 0;
}
}
void XMLPrinter::Putc( char ch )
{
if ( _fp ) {
fputc ( ch, _fp);
}
else {
char* p = _buffer.PushArr( sizeof(char) ) - 1; // back up over the null terminator.
p[0] = ch;
p[1] = 0;
}
}
void XMLPrinter::PrintSpace( int depth ) void XMLPrinter::PrintSpace( int depth )
{ {
for( int i=0; i<depth; ++i ) { for( int i=0; i<depth; ++i ) {
Print( " " ); Write( " " );
} }
} }
@ -2437,15 +2466,16 @@ void XMLPrinter::PrintString( const char* p, bool restricted )
if ( flag[(unsigned char)(*q)] ) { if ( flag[(unsigned char)(*q)] ) {
while ( p < q ) { while ( p < q ) {
const size_t delta = q - p; const size_t delta = q - p;
// %.*s accepts type int as "precision"
const int toPrint = ( INT_MAX < delta ) ? INT_MAX : (int)delta; const int toPrint = ( INT_MAX < delta ) ? INT_MAX : (int)delta;
Print( "%.*s", toPrint, p ); Write( p, toPrint );
p += toPrint; p += toPrint;
} }
bool entityPatternPrinted = false; bool entityPatternPrinted = false;
for( int i=0; i<NUM_ENTITIES; ++i ) { for( int i=0; i<NUM_ENTITIES; ++i ) {
if ( entities[i].value == *q ) { if ( entities[i].value == *q ) {
Print( "&%s;", entities[i].pattern ); Putc( '&' );
Write( entities[i].pattern, entities[i].length );
Putc( ';' );
entityPatternPrinted = true; entityPatternPrinted = true;
break; break;
} }
@ -2465,7 +2495,9 @@ void XMLPrinter::PrintString( const char* p, bool restricted )
// string if an entity wasn't found. // string if an entity wasn't found.
TIXMLASSERT( p <= q ); TIXMLASSERT( p <= q );
if ( !_processEntities || ( p < q ) ) { if ( !_processEntities || ( p < q ) ) {
Print( "%s", p ); const size_t delta = q - p;
const int toPrint = ( INT_MAX < delta ) ? INT_MAX : (int)delta;
Write( p, toPrint );
} }
} }
@ -2474,7 +2506,7 @@ void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
{ {
if ( writeBOM ) { if ( writeBOM ) {
static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 }; static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
Print( "%s", bom ); Write( reinterpret_cast< const char* >( bom ) );
} }
if ( writeDec ) { if ( writeDec ) {
PushDeclaration( "xml version=\"1.0\"" ); PushDeclaration( "xml version=\"1.0\"" );
@ -2488,13 +2520,15 @@ void XMLPrinter::OpenElement( const char* name, bool compactMode )
_stack.Push( name ); _stack.Push( name );
if ( _textDepth < 0 && !_firstElement && !compactMode ) { if ( _textDepth < 0 && !_firstElement && !compactMode ) {
Print( "\n" ); Putc( '\n' );
} }
if ( !compactMode ) { if ( !compactMode ) {
PrintSpace( _depth ); PrintSpace( _depth );
} }
Print( "<%s", name ); Write ( "<" );
Write ( name );
_elementJustOpened = true; _elementJustOpened = true;
_firstElement = false; _firstElement = false;
++_depth; ++_depth;
@ -2504,9 +2538,11 @@ void XMLPrinter::OpenElement( const char* name, bool compactMode )
void XMLPrinter::PushAttribute( const char* name, const char* value ) void XMLPrinter::PushAttribute( const char* name, const char* value )
{ {
TIXMLASSERT( _elementJustOpened ); TIXMLASSERT( _elementJustOpened );
Print( " %s=\"", name ); Putc ( ' ' );
Write( name );
Write( "=\"" );
PrintString( value, false ); PrintString( value, false );
Print( "\"" ); Putc ( '\"' );
} }
@ -2556,21 +2592,23 @@ void XMLPrinter::CloseElement( bool compactMode )
const char* name = _stack.Pop(); const char* name = _stack.Pop();
if ( _elementJustOpened ) { if ( _elementJustOpened ) {
Print( "/>" ); Write( "/>" );
} }
else { else {
if ( _textDepth < 0 && !compactMode) { if ( _textDepth < 0 && !compactMode) {
Print( "\n" ); Putc( '\n' );
PrintSpace( _depth ); PrintSpace( _depth );
} }
Print( "</%s>", name ); Write ( "</" );
Write ( name );
Write ( ">" );
} }
if ( _textDepth == _depth ) { if ( _textDepth == _depth ) {
_textDepth = -1; _textDepth = -1;
} }
if ( _depth == 0 && !compactMode) { if ( _depth == 0 && !compactMode) {
Print( "\n" ); Putc( '\n' );
} }
_elementJustOpened = false; _elementJustOpened = false;
} }
@ -2582,7 +2620,7 @@ void XMLPrinter::SealElementIfJustOpened()
return; return;
} }
_elementJustOpened = false; _elementJustOpened = false;
Print( ">" ); Putc( '>' );
} }
@ -2592,7 +2630,9 @@ void XMLPrinter::PushText( const char* text, bool cdata )
SealElementIfJustOpened(); SealElementIfJustOpened();
if ( cdata ) { if ( cdata ) {
Print( "<![CDATA[%s]]>", text ); Write( "<![CDATA[" );
Write( text );
Write( "]]>" );
} }
else { else {
PrintString( text, true ); PrintString( text, true );
@ -2650,11 +2690,14 @@ void XMLPrinter::PushComment( const char* comment )
{ {
SealElementIfJustOpened(); SealElementIfJustOpened();
if ( _textDepth < 0 && !_firstElement && !_compactMode) { if ( _textDepth < 0 && !_firstElement && !_compactMode) {
Print( "\n" ); Putc( '\n' );
PrintSpace( _depth ); PrintSpace( _depth );
} }
_firstElement = false; _firstElement = false;
Print( "<!--%s-->", comment );
Write( "<!--" );
Write( comment );
Write( "-->" );
} }
@ -2662,11 +2705,14 @@ void XMLPrinter::PushDeclaration( const char* value )
{ {
SealElementIfJustOpened(); SealElementIfJustOpened();
if ( _textDepth < 0 && !_firstElement && !_compactMode) { if ( _textDepth < 0 && !_firstElement && !_compactMode) {
Print( "\n" ); Putc( '\n' );
PrintSpace( _depth ); PrintSpace( _depth );
} }
_firstElement = false; _firstElement = false;
Print( "<?%s?>", value );
Write( "<?" );
Write( value );
Write( "?>" );
} }
@ -2674,11 +2720,14 @@ void XMLPrinter::PushUnknown( const char* value )
{ {
SealElementIfJustOpened(); SealElementIfJustOpened();
if ( _textDepth < 0 && !_firstElement && !_compactMode) { if ( _textDepth < 0 && !_firstElement && !_compactMode) {
Print( "\n" ); Putc( '\n' );
PrintSpace( _depth ); PrintSpace( _depth );
} }
_firstElement = false; _firstElement = false;
Print( "<!%s>", value );
Write( "<!" );
Write( value );
Putc( '>' );
} }

View File

@ -98,9 +98,13 @@ distribution.
/* Versioning, past 1.0.14: /* Versioning, past 1.0.14:
http://semver.org/ http://semver.org/
*/ */
static const int TIXML2_MAJOR_VERSION = 5; static const int TIXML2_MAJOR_VERSION = 6;
static const int TIXML2_MINOR_VERSION = 0; static const int TIXML2_MINOR_VERSION = 1;
static const int TIXML2_PATCH_VERSION = 1; static const int TIXML2_PATCH_VERSION = 0;
#define TINYXML2_MAJOR_VERSION 6
#define TINYXML2_MINOR_VERSION 1
#define TINYXML2_PATCH_VERSION 0
namespace tinyxml2 namespace tinyxml2
{ {
@ -192,10 +196,11 @@ template <class T, int INITIAL_SIZE>
class DynArray class DynArray
{ {
public: public:
DynArray() { DynArray() :
_mem = _pool; _mem( _pool ),
_allocated = INITIAL_SIZE; _allocated( INITIAL_SIZE ),
_size = 0; _size( 0 )
{
} }
~DynArray() { ~DynArray() {
@ -333,7 +338,7 @@ template< int ITEM_SIZE >
class MemPoolT : public MemPool class MemPoolT : public MemPool
{ {
public: public:
MemPoolT() : _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {} MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
~MemPoolT() { ~MemPoolT() {
Clear(); Clear();
} }
@ -1211,7 +1216,7 @@ public:
private: private:
enum { BUF_SIZE = 200 }; enum { BUF_SIZE = 200 };
XMLAttribute() : _parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {} XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {}
virtual ~XMLAttribute() {} virtual ~XMLAttribute() {}
XMLAttribute( const XMLAttribute& ); // not supported XMLAttribute( const XMLAttribute& ); // not supported
@ -1360,6 +1365,17 @@ public:
return a->QueryFloatValue( value ); return a->QueryFloatValue( value );
} }
/// See QueryIntAttribute()
XMLError QueryStringAttribute(const char* name, const char** value) const {
const XMLAttribute* a = FindAttribute(name);
if (!a) {
return XML_NO_ATTRIBUTE;
}
*value = a->Value();
return XML_SUCCESS;
}
/** Given an attribute name, QueryAttribute() returns /** Given an attribute name, QueryAttribute() returns
XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
@ -1634,6 +1650,13 @@ enum Whitespace {
class TINYXML2_LIB XMLDocument : public XMLNode class TINYXML2_LIB XMLDocument : public XMLNode
{ {
friend class XMLElement; friend class XMLElement;
// Gives access to SetError, but over-access for everything else.
// Wishing C++ had "internal" scope.
friend class XMLNode;
friend class XMLText;
friend class XMLComment;
friend class XMLDeclaration;
friend class XMLUnknown;
public: public:
/// constructor /// constructor
XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE ); XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE );
@ -1785,10 +1808,8 @@ public:
*/ */
void DeleteNode( XMLNode* node ); void DeleteNode( XMLNode* node );
void SetError( XMLError error, const char* str1, const char* str2, int lineNum );
void ClearError() { void ClearError() {
SetError(XML_SUCCESS, 0, 0, 0); SetError(XML_SUCCESS, 0, 0);
} }
/// Return true if there was an error parsing the document. /// Return true if there was an error parsing the document.
@ -1802,19 +1823,19 @@ public:
const char* ErrorName() const; const char* ErrorName() const;
static const char* ErrorIDToName(XMLError errorID); static const char* ErrorIDToName(XMLError errorID);
/// Return a possibly helpful diagnostic location or string. /** Returns a "long form" error description. A hopefully helpful
const char* GetErrorStr1() const; diagnostic with location, line number, and/or additional info.
*/
const char* ErrorStr() const;
/// Return a possibly helpful secondary diagnostic location or string. /// A (trivial) utility function that prints the ErrorStr() to stdout.
const char* GetErrorStr2() const; void PrintError() const;
/// Return the line where the error occured, or zero if unknown. /// Return the line where the error occured, or zero if unknown.
int GetErrorLineNum() const int ErrorLineNum() const
{ {
return _errorLineNum; return _errorLineNum;
} }
/// If there is an error, print it to stdout.
void PrintError() const;
/// Clear the document, resetting it to the initial state. /// Clear the document, resetting it to the initial state.
void Clear(); void Clear();
@ -1826,7 +1847,7 @@ public:
NOTE: that the 'target' must be non-null. NOTE: that the 'target' must be non-null.
*/ */
void DeepCopy(XMLDocument* target); void DeepCopy(XMLDocument* target) const;
// internal // internal
char* Identify( char* p, XMLNode** node ); char* Identify( char* p, XMLNode** node );
@ -1849,8 +1870,7 @@ private:
bool _processEntities; bool _processEntities;
XMLError _errorID; XMLError _errorID;
Whitespace _whitespaceMode; Whitespace _whitespaceMode;
mutable StrPair _errorStr1; mutable StrPair _errorStr;
mutable StrPair _errorStr2;
int _errorLineNum; int _errorLineNum;
char* _charBuffer; char* _charBuffer;
int _parseCurLineNum; int _parseCurLineNum;
@ -1871,6 +1891,8 @@ private:
void Parse(); void Parse();
void SetError( XMLError error, int lineNum, const char* format, ... );
template<class NodeType, int PoolElementSize> template<class NodeType, int PoolElementSize>
NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool ); NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool );
}; };
@ -1947,16 +1969,13 @@ class TINYXML2_LIB XMLHandle
{ {
public: public:
/// Create a handle from any node (at any depth of the tree.) This can be a null pointer. /// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
XMLHandle( XMLNode* node ) { XMLHandle( XMLNode* node ) : _node( node ) {
_node = node;
} }
/// Create a handle from a node. /// Create a handle from a node.
XMLHandle( XMLNode& node ) { XMLHandle( XMLNode& node ) : _node( &node ) {
_node = &node;
} }
/// Copy constructor /// Copy constructor
XMLHandle( const XMLHandle& ref ) { XMLHandle( const XMLHandle& ref ) : _node( ref._node ) {
_node = ref._node;
} }
/// Assignment /// Assignment
XMLHandle& operator=( const XMLHandle& ref ) { XMLHandle& operator=( const XMLHandle& ref ) {
@ -2030,14 +2049,11 @@ private:
class TINYXML2_LIB XMLConstHandle class TINYXML2_LIB XMLConstHandle
{ {
public: public:
XMLConstHandle( const XMLNode* node ) { XMLConstHandle( const XMLNode* node ) : _node( node ) {
_node = node;
} }
XMLConstHandle( const XMLNode& node ) { XMLConstHandle( const XMLNode& node ) : _node( &node ) {
_node = &node;
} }
XMLConstHandle( const XMLConstHandle& ref ) { XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) {
_node = ref._node;
} }
XMLConstHandle& operator=( const XMLConstHandle& ref ) { XMLConstHandle& operator=( const XMLConstHandle& ref ) {
@ -2229,6 +2245,9 @@ protected:
*/ */
virtual void PrintSpace( int depth ); virtual void PrintSpace( int depth );
void Print( const char* format, ... ); void Print( const char* format, ... );
void Write( const char* data, size_t size );
inline void Write( const char* data ) { Write( data, strlen( data ) ); }
void Putc( char ch );
void SealElementIfJustOpened(); void SealElementIfJustOpened();
bool _elementJustOpened; bool _elementJustOpened;
@ -2252,6 +2271,10 @@ private:
bool _restrictedEntityFlag[ENTITY_RANGE]; bool _restrictedEntityFlag[ENTITY_RANGE];
DynArray< char, 20 > _buffer; DynArray< char, 20 > _buffer;
// Prohibit cloning, intentionally not implemented
XMLPrinter( const XMLPrinter& );
XMLPrinter& operator=( const XMLPrinter& );
}; };