mirror of https://github.com/AxioDL/tinyxml2.git
basic text support
This commit is contained in:
parent
22aead1c73
commit
5492a1c705
36
tinyxml2.cpp
36
tinyxml2.cpp
|
@ -132,9 +132,9 @@ char* XMLBase::ParseName( char* p, StrPair* pair )
|
||||||
char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
|
char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
|
||||||
{
|
{
|
||||||
XMLNode* returnNode = 0;
|
XMLNode* returnNode = 0;
|
||||||
|
char* start = p;
|
||||||
p = XMLNode::SkipWhiteSpace( p );
|
p = XMLNode::SkipWhiteSpace( p );
|
||||||
if( !p || !*p || *p != '<' )
|
if( !p || !*p )
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -166,6 +166,12 @@ char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
|
||||||
returnNode = new XMLElement( document );
|
returnNode = new XMLElement( document );
|
||||||
p += elementHeaderLen;
|
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 {
|
else {
|
||||||
TIXMLASSERT( 0 );
|
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 ---------- //
|
// --------- XMLComment ---------- //
|
||||||
|
@ -430,11 +452,19 @@ void XMLElement::Print( FILE* cfile, int depth )
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( firstChild ) {
|
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 ) {
|
for( XMLNode* node=firstChild; node; node=node->next ) {
|
||||||
node->Print( cfile, depth+1 );
|
node->Print( cfile, depth+1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf( cfile, "</%s>\n", Name() );
|
fprintf( cfile, "</%s>\n", Name() );
|
||||||
|
// fixme: see note above
|
||||||
|
//if ( useNewline ) fprintf( cfile, "\n" );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
fprintf( cfile, "/>\n" );
|
fprintf( cfile, "/>\n" );
|
||||||
|
|
25
tinyxml2.h
25
tinyxml2.h
|
@ -34,6 +34,7 @@ class XMLElement;
|
||||||
class XMLAttribute;
|
class XMLAttribute;
|
||||||
class XMLComment;
|
class XMLComment;
|
||||||
class XMLNode;
|
class XMLNode;
|
||||||
|
class XMLText;
|
||||||
|
|
||||||
// internal - move to separate namespace
|
// internal - move to separate namespace
|
||||||
struct CharBuffer
|
struct CharBuffer
|
||||||
|
@ -116,7 +117,9 @@ public:
|
||||||
XMLNode* InsertEndChild( XMLNode* addThis );
|
XMLNode* InsertEndChild( XMLNode* addThis );
|
||||||
virtual void Print( FILE* cfile, int depth );
|
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 ); }
|
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
|
class XMLComment : public XMLNode
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -146,6 +168,7 @@ public:
|
||||||
virtual ~XMLComment();
|
virtual ~XMLComment();
|
||||||
|
|
||||||
virtual void Print( FILE* cfile, int depth );
|
virtual void Print( FILE* cfile, int depth );
|
||||||
|
virtual XMLComment* ToComment() { return this; }
|
||||||
|
|
||||||
const char* Value() { return value.GetStr(); }
|
const char* Value() { return value.GetStr(); }
|
||||||
|
|
||||||
|
|
BIN
tinyxml2.suo
BIN
tinyxml2.suo
Binary file not shown.
|
@ -37,6 +37,8 @@ int main( int argc, const char* argv )
|
||||||
"<!--comment beside elements--><element><subelement></subelement></element>",
|
"<!--comment beside elements--><element><subelement></subelement></element>",
|
||||||
"<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </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\" ></element>",
|
||||||
|
"<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
|
||||||
|
"<element>Text inside element.</element>",
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
for( int i=0; test[i]; ++i ) {
|
for( int i=0; test[i]; ++i ) {
|
||||||
|
|
Loading…
Reference in New Issue