From 8ee79897c000c7e070513d074c3e64159ee614c3 Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Wed, 25 Jan 2012 17:44:30 -0800 Subject: [PATCH] added entity input support. --- tinyxml2.cpp | 74 ++++++++++++++++++++++++++++------------------------ xmltest.cpp | 13 ++++----- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 3bf058e..a8a7be3 100644 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -14,6 +14,22 @@ static const char CR = CARRIAGE_RETURN; static const char SINGLE_QUOTE = '\''; static const char DOUBLE_QUOTE = '\"'; +struct Entity { + const char* pattern; + int length; + char value; +}; + +static const int NUM_ENTITIES = 5; +static const Entity entities[NUM_ENTITIES] = +{ + { "quot", 4, '\"' }, + { "amp", 3, '&' }, + { "apos", 4, '\'' }, + { "lt", 2, '<' }, + { "gt", 2, '>' } +}; + // --------- CharBuffer ----------- // /*static*/ CharBuffer* CharBuffer::Construct( const char* in ) @@ -37,13 +53,14 @@ const char* StrPair::GetStr() { if ( flags & NEEDS_FLUSH ) { *end = 0; + flags ^= NEEDS_FLUSH; - if ( flags & ( NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION ) ) { + if ( flags ) { char* p = start; char* q = start; while( p < end ) { - if ( *p == CR ) { + if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) { // CR-LF pair becomes LF // CR alone becomes LF // LF-CR becomes LF @@ -55,7 +72,7 @@ const char* StrPair::GetStr() } *q = LF; } - else if ( *p == LF ) { + else if ( (flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) { if ( *(p+1) == CR ) { p += 2; } @@ -64,12 +81,32 @@ const char* StrPair::GetStr() } *q = LF; } + else if ( (flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) { + int i=0; + for( i=0; iPrint( streamer ); } streamer->CloseElement(); - -/* if ( firstChild ) { - fprintf( cfile, ">", Name() ); - if ( !IsTextParent() ) { - fprintf( cfile, "\n" ); - } - - for( XMLNode* node=firstChild; node; node=node->next ) { - node->Print( cfile, depth+1 ); - } - - fprintf( cfile, "", Name() ); - if ( !IsTextParent() ) { - fprintf( cfile, "\n" ); - } - } - else { - fprintf( cfile, "/>" ); - if ( !IsTextParent() ) { - fprintf( cfile, "\n" ); - } - }*/ } diff --git a/xmltest.cpp b/xmltest.cpp index 7b12475..6ff4667 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -32,15 +32,16 @@ int main( int argc, const char* argv ) //"", //"", //"", - "", - "", + // "", + //"", //" \n \n ", - "", - "", + //"", + //"", //"Text inside element.", //"", - "Text inside and bolded in the element.", - "Text inside and bolded in the element.", + //"Text inside and bolded in the element.", + //"Text inside and bolded in the element.", + "This & That.", 0 }; for( int i=0; test[i]; ++i ) {