mirror of https://github.com/AxioDL/tinyxml2.git
test cases in progress
This commit is contained in:
parent
8a5dfee8ce
commit
dadcdfad4a
42
tinyxml2.cpp
42
tinyxml2.cpp
|
@ -143,16 +143,22 @@ char* XMLBase::Identify( XMLDocument* document, char* p, XMLNode** node )
|
||||||
static const char* commentHeader = { "<!--" };
|
static const char* commentHeader = { "<!--" };
|
||||||
static const char* dtdHeader = { "<!" };
|
static const char* dtdHeader = { "<!" };
|
||||||
static const char* cdataHeader = { "<![CDATA[" };
|
static const char* cdataHeader = { "<![CDATA[" };
|
||||||
|
static const char* elementHeader = { "<" }; // and a header for everything else; check last.
|
||||||
|
|
||||||
static const int xmlHeaderLen = 5;
|
static const int xmlHeaderLen = 5;
|
||||||
static const int commentHeaderLen = 4;
|
static const int commentHeaderLen = 4;
|
||||||
static const int dtdHeaderLen = 2;
|
static const int dtdHeaderLen = 2;
|
||||||
static const int cdataHeaderLen = 9;
|
static const int cdataHeaderLen = 9;
|
||||||
|
static const int elementHeaderLen = 1;
|
||||||
|
|
||||||
if ( XMLNode::StringEqual( p, commentHeader, commentHeaderLen ) ) {
|
if ( StringEqual( p, commentHeader, commentHeaderLen ) ) {
|
||||||
returnNode = new XMLComment( document );
|
returnNode = new XMLComment( document );
|
||||||
p += commentHeaderLen;
|
p += commentHeaderLen;
|
||||||
}
|
}
|
||||||
|
else if ( StringEqual( p, elementHeader, elementHeaderLen ) ) {
|
||||||
|
returnNode = new XMLElement( document );
|
||||||
|
p += elementHeaderLen;
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
TIXMLASSERT( 0 );
|
TIXMLASSERT( 0 );
|
||||||
}
|
}
|
||||||
|
@ -222,6 +228,7 @@ void XMLNode::Print( FILE* fp, int depth )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void XMLNode::PrintSpace( FILE* fp, int depth )
|
void XMLNode::PrintSpace( FILE* fp, int depth )
|
||||||
{
|
{
|
||||||
for( int i=0; i<depth; ++i ) {
|
for( int i=0; i<depth; ++i ) {
|
||||||
|
@ -270,6 +277,12 @@ char* XMLAttribute::ParseDeep( char* p )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void XMLAttribute::Print( FILE* cfile )
|
||||||
|
{
|
||||||
|
fprintf( cfile, "\"%s\"", value );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// --------- XMLElement ---------- //
|
// --------- XMLElement ---------- //
|
||||||
XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
|
XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
|
||||||
name( 0 ),
|
name( 0 ),
|
||||||
|
@ -385,6 +398,29 @@ char* XMLElement::ParseDeep( char* p )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void XMLElement::Print( FILE* cfile, int depth )
|
||||||
|
{
|
||||||
|
PrintSpace( cfile, depth );
|
||||||
|
fprintf( cfile, "<%s", Name() );
|
||||||
|
|
||||||
|
for( XMLAttribute* attrib=rootAttribute; attrib; attrib=attrib->next ) {
|
||||||
|
fprintf( cfile, " " );
|
||||||
|
attrib->Print( cfile );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( firstChild ) {
|
||||||
|
fprintf( cfile, ">/n" );
|
||||||
|
for( XMLNode* node=firstChild; node; node=node->next ) {
|
||||||
|
node->Print( cfile, depth+1 );
|
||||||
|
}
|
||||||
|
fprintf( cfile, "</%s>", Name() );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fprintf( cfile, "/>\n" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// --------- XMLDocument ----------- //
|
// --------- XMLDocument ----------- //
|
||||||
XMLDocument::XMLDocument() :
|
XMLDocument::XMLDocument() :
|
||||||
charBuffer( 0 )
|
charBuffer( 0 )
|
||||||
|
@ -407,11 +443,13 @@ bool XMLDocument::Parse( const char* p )
|
||||||
XMLNode* node = 0;
|
XMLNode* node = 0;
|
||||||
|
|
||||||
char* q = Identify( this, charBuffer->mem, &node );
|
char* q = Identify( this, charBuffer->mem, &node );
|
||||||
|
if ( node ) {
|
||||||
root->InsertEndChild( node );
|
root->InsertEndChild( node );
|
||||||
node->ParseDeep( q );
|
node->ParseDeep( q );
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void XMLDocument::Print( FILE* fp, int depth )
|
void XMLDocument::Print( FILE* fp, int depth )
|
||||||
|
|
|
@ -133,6 +133,7 @@ class XMLAttribute : public XMLBase
|
||||||
public:
|
public:
|
||||||
XMLAttribute( XMLElement* element ) : value( 0 ), next( 0 ) {}
|
XMLAttribute( XMLElement* element ) : value( 0 ), next( 0 ) {}
|
||||||
virtual ~XMLAttribute() {}
|
virtual ~XMLAttribute() {}
|
||||||
|
virtual void Print( FILE* cfile );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
char* ParseDeep( char* p );
|
char* ParseDeep( char* p );
|
||||||
|
@ -149,6 +150,7 @@ public:
|
||||||
virtual ~XMLElement();
|
virtual ~XMLElement();
|
||||||
|
|
||||||
const char* Name() const { return name; }
|
const char* Name() const { return name; }
|
||||||
|
virtual void Print( FILE* cfile, int depth );
|
||||||
|
|
||||||
virtual XMLElement* ToElement() { return this; }
|
virtual XMLElement* ToElement() { return this; }
|
||||||
bool Closing() const { return closing; }
|
bool Closing() const { return closing; }
|
||||||
|
|
BIN
tinyxml2.suo
BIN
tinyxml2.suo
Binary file not shown.
10
xmltest.cpp
10
xmltest.cpp
|
@ -28,11 +28,15 @@ int main( int argc, const char* argv )
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
static const char* test = "<element />";
|
static const char* test[] = { "<element />",
|
||||||
|
"<element></element>",
|
||||||
|
0
|
||||||
|
};
|
||||||
|
for( const char* t=test[0]; *t; ++t ) {
|
||||||
XMLDocument doc;
|
XMLDocument doc;
|
||||||
doc.Parse( test );
|
doc.Parse( t );
|
||||||
doc.Print( stdout );
|
doc.Print( stdout );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue