entity output

This commit is contained in:
Lee Thomason 2012-01-26 08:47:06 -08:00
parent 857b868ca6
commit 951d88394c
2 changed files with 34 additions and 4 deletions

View File

@ -628,12 +628,39 @@ void XMLStreamer::PrintSpace( int depth )
} }
void XMLStreamer::PrintString( const char* ) void XMLStreamer::PrintString( const char* p )
{ {
// Look for runs of bytes between entities to print.
const char* q = p;
while ( *q ) {
if ( *q < ENTITY_RANGE ) {
// Check for entities. If one is found, flush
// the stream up until the entity, write the
// entity, and keep looking.
if ( entityFlag[*q] ) {
while ( p < q ) {
fputc( *p, fp );
++p;
}
for( int i=0; i<NUM_ENTITIES; ++i ) {
if ( entities[i].value == *q ) {
fprintf( fp, "&%s;", entities[i].pattern );
break;
}
}
++p;
}
}
++q;
}
// Flush the remaining string. This will be the entire
// string if an entity wasn't found.
if ( q-p > 0 ) {
fprintf( fp, "%s", p );
}
} }
void XMLStreamer::OpenElement( const char* name, bool textParent ) void XMLStreamer::OpenElement( const char* name, bool textParent )
{ {
if ( elementJustOpened ) { if ( elementJustOpened ) {
@ -645,6 +672,7 @@ void XMLStreamer::OpenElement( const char* name, bool textParent )
stack.Push( name ); stack.Push( name );
text.Push( textParent ? "T" : "" ); text.Push( textParent ? "T" : "" );
// fixme: can names have entities?
fprintf( fp, "<%s", name ); fprintf( fp, "<%s", name );
elementJustOpened = true; elementJustOpened = true;
++depth; ++depth;
@ -654,6 +682,7 @@ void XMLStreamer::OpenElement( const char* name, bool textParent )
void XMLStreamer::PushAttribute( const char* name, const char* value ) void XMLStreamer::PushAttribute( const char* name, const char* value )
{ {
TIXMLASSERT( elementJustOpened ); TIXMLASSERT( elementJustOpened );
// fixme: supports entities?
fprintf( fp, " %s=\"%s\"", name, value ); fprintf( fp, " %s=\"%s\"", name, value );
} }
@ -675,6 +704,7 @@ void XMLStreamer::CloseElement()
if ( wasPositive == 0 ) { if ( wasPositive == 0 ) {
PrintSpace( depth ); PrintSpace( depth );
} }
// fixme can names have entities?
fprintf( fp, "</%s>", name ); fprintf( fp, "</%s>", name );
if ( text.NumPositive() == 0 ) { if ( text.NumPositive() == 0 ) {
fprintf( fp, "\n" ); fprintf( fp, "\n" );
@ -699,7 +729,7 @@ void XMLStreamer::PushText( const char* text )
if ( elementJustOpened ) { if ( elementJustOpened ) {
SealElement(); SealElement();
} }
fprintf( fp, "%s", text ); PrintString( text );
} }

View File

@ -301,7 +301,7 @@ private:
int depth; int depth;
bool elementJustOpened; bool elementJustOpened;
enum { enum {
ENTITY_RANGE = 64, ENTITY_RANGE = 64
}; };
bool entityFlag[ENTITY_RANGE]; bool entityFlag[ENTITY_RANGE];