basic text support

This commit is contained in:
Lee Thomason 2012-01-23 15:32:10 -08:00
parent 22aead1c73
commit 5492a1c705
4 changed files with 59 additions and 4 deletions

View File

@ -132,9 +132,9 @@ char* XMLBase::ParseName( char* p, StrPair* pair )
char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
{
XMLNode* returnNode = 0;
char* start = p;
p = XMLNode::SkipWhiteSpace( p );
if( !p || !*p || *p != '<' )
if( !p || !*p )
{
return 0;
}
@ -166,6 +166,12 @@ char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
returnNode = new XMLElement( document );
p += elementHeaderLen;
}
// fixme: better text detection
else if ( (*p != '<') && IsAlphaNum( *p ) ) {
// fixme: this is filtering out empty text...should it?
returnNode = new XMLText( document );
p = start; // Back it up, all the text counts.
}
else {
TIXMLASSERT( 0 );
}
@ -256,6 +262,22 @@ void XMLNode::PrintSpace( FILE* fp, int depth )
}
// --------- XMLText ---------- //
char* XMLText::ParseDeep( char* p )
{
p = ParseText( p, &value, "<" );
// consumes the end tag.
if ( p && *p ) {
return p-1;
}
return 0;
}
void XMLText::Print( FILE* cfile, int depth )
{
fprintf( cfile, value.GetStr() );
}
// --------- XMLComment ---------- //
@ -430,11 +452,19 @@ void XMLElement::Print( FILE* cfile, int depth )
}
if ( firstChild ) {
fprintf( cfile, ">\n" );
// fixme: once text is on, it should stay on, and not use newlines.
bool useNewline = firstChild->ToText() == 0;
fprintf( cfile, ">", Name() );
if ( useNewline ) fprintf( cfile, "\n" );
for( XMLNode* node=firstChild; node; node=node->next ) {
node->Print( cfile, depth+1 );
}
fprintf( cfile, "</%s>\n", Name() );
// fixme: see note above
//if ( useNewline ) fprintf( cfile, "\n" );
}
else {
fprintf( cfile, "/>\n" );

View File

@ -34,6 +34,7 @@ class XMLElement;
class XMLAttribute;
class XMLComment;
class XMLNode;
class XMLText;
// internal - move to separate namespace
struct CharBuffer
@ -116,7 +117,9 @@ public:
XMLNode* InsertEndChild( XMLNode* addThis );
virtual void Print( FILE* cfile, int depth );
virtual XMLElement* ToElement() { return 0; }
virtual XMLElement* ToElement() { return 0; }
virtual XMLText* ToText() { return 0; }
virtual XMLComment* ToComment() { return 0; }
virtual char* ParseDeep( char* ) { TIXMLASSERT( 0 ); }
@ -139,6 +142,25 @@ private:
};
class XMLText : public XMLNode
{
public:
XMLText( XMLDocument* doc ) : XMLNode( doc ) {}
virtual ~XMLText() {}
virtual void Print( FILE* cfile, int depth );
const char* Value() { return value.GetStr(); }
virtual XMLText* ToText() { return this; }
char* ParseDeep( char* );
protected:
private:
StrPair value;
};
class XMLComment : public XMLNode
{
public:
@ -146,6 +168,7 @@ public:
virtual ~XMLComment();
virtual void Print( FILE* cfile, int depth );
virtual XMLComment* ToComment() { return this; }
const char* Value() { return value.GetStr(); }

Binary file not shown.

View File

@ -37,6 +37,8 @@ int main( int argc, const char* argv )
"<!--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>Text inside element.</element>",
0
};
for( int i=0; test[i]; ++i ) {