diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 4fd9998..d147ffe 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -2412,10 +2412,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( 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 ) { for( int i=0; i( bom ) ); } if ( writeDec ) { PushDeclaration( "xml version=\"1.0\"" ); @@ -2488,13 +2517,15 @@ void XMLPrinter::OpenElement( const char* name, bool compactMode ) _stack.Push( name ); if ( _textDepth < 0 && !_firstElement && !compactMode ) { - Print( "\n" ); + Putc( '\n' ); } if ( !compactMode ) { PrintSpace( _depth ); } - Print( "<%s", name ); + Write ( "<" ); + Write ( name ); + _elementJustOpened = true; _firstElement = false; ++_depth; @@ -2504,9 +2535,11 @@ void XMLPrinter::OpenElement( const char* name, bool compactMode ) void XMLPrinter::PushAttribute( const char* name, const char* value ) { TIXMLASSERT( _elementJustOpened ); - Print( " %s=\"", name ); + Putc ( ' ' ); + Write( name ); + Write( "=\"" ); PrintString( value, false ); - Print( "\"" ); + Putc ( '\"' ); } @@ -2556,21 +2589,23 @@ void XMLPrinter::CloseElement( bool compactMode ) const char* name = _stack.Pop(); if ( _elementJustOpened ) { - Print( "/>" ); + Write( "/>" ); } else { if ( _textDepth < 0 && !compactMode) { - Print( "\n" ); + Putc( '\n' ); PrintSpace( _depth ); } - Print( "", name ); + Write ( "" ); } if ( _textDepth == _depth ) { _textDepth = -1; } if ( _depth == 0 && !compactMode) { - Print( "\n" ); + Putc( '\n' ); } _elementJustOpened = false; } @@ -2582,7 +2617,7 @@ void XMLPrinter::SealElementIfJustOpened() return; } _elementJustOpened = false; - Print( ">" ); + Putc( '>' ); } @@ -2592,7 +2627,9 @@ void XMLPrinter::PushText( const char* text, bool cdata ) SealElementIfJustOpened(); if ( cdata ) { - Print( "", text ); + Write( "" ); } else { PrintString( text, true ); @@ -2650,11 +2687,14 @@ void XMLPrinter::PushComment( const char* comment ) { SealElementIfJustOpened(); if ( _textDepth < 0 && !_firstElement && !_compactMode) { - Print( "\n" ); + Putc( '\n' ); PrintSpace( _depth ); } _firstElement = false; - Print( "", comment ); + + Write( "" ); } @@ -2662,11 +2702,14 @@ void XMLPrinter::PushDeclaration( const char* value ) { SealElementIfJustOpened(); if ( _textDepth < 0 && !_firstElement && !_compactMode) { - Print( "\n" ); + Putc( '\n' ); PrintSpace( _depth ); } _firstElement = false; - Print( "", value ); + + Write( "" ); } @@ -2674,11 +2717,14 @@ void XMLPrinter::PushUnknown( const char* value ) { SealElementIfJustOpened(); if ( _textDepth < 0 && !_firstElement && !_compactMode) { - Print( "\n" ); + Putc( '\n' ); PrintSpace( _depth ); } _firstElement = false; - Print( "", value ); + + Write( "' ); } diff --git a/tinyxml2.h b/tinyxml2.h index 2958b46..1331615 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -2230,6 +2230,9 @@ protected: */ virtual void PrintSpace( int depth ); 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(); bool _elementJustOpened;