added entity input support.

This commit is contained in:
Lee Thomason 2012-01-25 17:44:30 -08:00
parent 24767b05ab
commit 8ee79897c0
2 changed files with 47 additions and 40 deletions

View File

@ -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; i<NUM_ENTITIES; ++i ) {
if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
&& *(p+entities[i].length+1) == ';' )
{
// Found an entity convert;
*q = entities[i].value;
++q;
p += entities[i].length + 2;
break;
}
}
if ( i == NUM_ENTITIES ) {
// fixme: treat as error?
++p;
++q;
}
}
else {
*q = *p;
++p;
++q;
}
}
*q = 0;
}
flags = 0;
}
@ -276,15 +313,6 @@ char* XMLNode::ParseDeep( char* p )
return 0;
}
/*
void XMLNode::PrintSpace( FILE* fp, int depth )
{
for( int i=0; i<depth; ++i ) {
fprintf( fp, " " );
}
}
*/
// --------- XMLText ---------- //
char* XMLText::ParseDeep( char* p )
{
@ -480,28 +508,6 @@ void XMLElement::Print( XMLStreamer* streamer )
node->Print( 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, "</%s>", Name() );
if ( !IsTextParent() ) {
fprintf( cfile, "\n" );
}
}
else {
fprintf( cfile, "/>" );
if ( !IsTextParent() ) {
fprintf( cfile, "\n" );
}
}*/
}

View File

@ -32,15 +32,16 @@ int main( int argc, const char* argv )
//"<element></element>",
//"<element><subelement/></element>",
//"<element><subelement></subelement></element>",
"<element><subelement><subsub/></subelement></element>",
"<!--comment beside elements--><element><subelement></subelement></element>",
// "<element><subelement><subsub/></subelement></element>",
//"<!--comment beside elements--><element><subelement></subelement></element>",
//"<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
"<element attrib1='foo' attrib2=\"bar\" ></element>",
"<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
//"<element attrib1='foo' attrib2=\"bar\" ></element>",
//"<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
//"<element>Text inside element.</element>",
//"<element><b></b></element>",
"<element>Text inside and <b>bolded</b> in the element.</element>",
"<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
//"<element>Text inside and <b>bolded</b> in the element.</element>",
//"<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
"<element>This &amp; That.</element>",
0
};
for( int i=0; test[i]; ++i ) {