From 5492a1c705f57d65a323b9b65ea8c0cd52e21329 Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Mon, 23 Jan 2012 15:32:10 -0800 Subject: [PATCH] basic text support --- tinyxml2.cpp | 36 +++++++++++++++++++++++++++++++++--- tinyxml2.h | 25 ++++++++++++++++++++++++- tinyxml2.suo | Bin 32256 -> 32256 bytes xmltest.cpp | 2 ++ 4 files changed, 59 insertions(+), 4 deletions(-) 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 bcaeb41d4b6e9ff08220c3f499efae611566cbb5..e3bbf90cd91fc6c593cad22ee0ff262db5e586ea 100644 GIT binary patch delta 437 zcmYk1-z!657{|}^?i@RXHJa7JHf>6rw8Ji3NGC~=Xv+L7lEP`oZ+7FoawWAr+J!6a z`~%u^;YK-f=W0sHKY`+g){b|gJoWAQ^nJcPPY*8vFTv#qQ-YEtHDH286tAEV9kO>& zic^E-R@#(bK`uLvLrYaLs1xv0D@>p@&l zJJImTbmF~IYbBQm281fB@Ij8^PGGDS>V?zz&$T`RZp9~zbNme7C7#cFmk($| zB7_7W!@_1iMlL?fzEN}heetD-PfK5^lt-42TjF%6fYBu4F5(_yui0$gN3S9tAc}}7 zLseR}#7%QsH4f ztXM2CeOwuISlc@AM$}RWNu8kEB&ZdF@ant+ zN<(tT7|u&fEWh8LuS~2mpEx9u9Bh@M`_-cRb_7pX$sO_#$D1c^o8ksukv&k=kC z;}W7DGsQa%!?xsY!+lzP*EsYhMQZY;qaVec;0==?V;cai%h4lPX3#(VU!|Ngq-0y| zgm+@y0TqAr6bmB;V29`ZN#0Q76mqf*YSIDmN@2pt{XfxSbqo`)`M2nh 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 ) {