diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 367f05c..0a02c4b 100644 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -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, "\n", Name() ); + // fixme: see note above + //if ( useNewline ) fprintf( cfile, "\n" ); } else { fprintf( cfile, "/>\n" ); diff --git a/tinyxml2.h b/tinyxml2.h index ba5d031..227c7c7 100644 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -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(); } diff --git a/tinyxml2.suo b/tinyxml2.suo index bcaeb41..e3bbf90 100644 Binary files a/tinyxml2.suo and b/tinyxml2.suo differ diff --git a/xmltest.cpp b/xmltest.cpp index 4972275..0301c41 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -37,6 +37,8 @@ int main( int argc, const char* argv ) "", " \n \n ", "", + "", + "Text inside element.", 0 }; for( int i=0; test[i]; ++i ) {