Merge branch 'master' into errorstr

This commit is contained in:
Lee Thomason 2017-11-01 07:52:46 -07:00
commit d548b7e717
2 changed files with 71 additions and 22 deletions

View File

@ -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 ) void XMLPrinter::PrintSpace( int depth )
{ {
for( int i=0; i<depth; ++i ) { for( int i=0; i<depth; ++i ) {
Print( " " ); Write( " " );
} }
} }
@ -2437,15 +2463,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 +2492,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 +2503,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 +2517,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 +2535,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 +2589,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 +2617,7 @@ void XMLPrinter::SealElementIfJustOpened()
return; return;
} }
_elementJustOpened = false; _elementJustOpened = false;
Print( ">" ); Putc( '>' );
} }
@ -2592,7 +2627,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 +2687,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 +2702,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 +2717,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

@ -2230,6 +2230,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;