Parcourir la source

complete, not working, XMLComment parser.

Lee Thomason il y a 14 ans
Parent
commit
3f57d278e7
3 fichiers modifiés avec 160 ajouts et 9 suppressions
  1. 118 6
      tinyxml2.cpp
  2. 42 3
      tinyxml2.h
  3. BIN
      tinyxml2.suo

+ 118 - 6
tinyxml2.cpp

@@ -7,6 +7,7 @@
 
 using namespace tinyxml2;
 
+// --------- CharBuffer ----------- //
 /*static*/ CharBuffer* CharBuffer::Construct( const char* in )
 {
 	size_t len = strlen( in );
@@ -24,22 +25,132 @@ using namespace tinyxml2;
 }
 
 
-const char* XMLNode::SkipWhiteSpace( const char* p )
+// --------- XMLNode ----------- //
+
+XMLNode::XMLNode( XMLDocument* doc ) :
+	document( doc ),
+	parent( 0 ),
+	firstChild( 0 ), lastChild( 0 ),
+	prev( 0 ), next( 0 )
 {
-	while( isspace( *p ) ) {
-		++p;
+
+}
+
+
+XMLNode::~XMLNode()
+{
+	XMLNode* node=firstChild;
+	while( node ) {
+		XMLNode* temp = node->next;
+		delete node;
+		node = temp;
+	}
+}
+
+
+XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
+{
+	if ( lastChild ) {
+		TIXMLASSERT( firstChild );
+		TIXMLASSERT( lastChild->next == 0 );
+		lastChild->next = addThis;
+		addThis->prev = lastChild;
+		lastChild = addThis;
+
+		addThis->parent = this;
+		addThis->next = null;
+	}
+	else {
+		TIXMLASSERT( firstChild == 0 );
+		firstChild = lastChild = addThis;
+
+		addThis->parent = this;
+		addThis->prev = 0;
+		addThis->next = null;
 	}
+}
+
+
+const char* XMLNode::ParseText( char* p, const char* endTag, char** next )
+{
+	TIXMLASSERT( endTag && *endTag );
+
+	char* start = SkipWhiteSpace( p );
+	if ( !start )
+		return;
+
+	char endChar = *endTag;
+	p = start;
+	int length = strlen( endTag );
+
+	while ( *p ) {
+		if ( *p == endChar ) {
+			if ( strncmp( p, endTag, length ) == 0 ) {
+				*p = 0;
+				*next = p + length;
+				return start;
+			}
+		}
+		++p;
+	}	
+	return 0;
+}
+
+
+// --------- XMLComment ---------- //
+
+XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
+{
+}
+
+
+virtual XMLComment::~XMLComment()
+{
+
+}
+
+
+virtual char* XMLComment::ParseDeep( char* p )
+{
+	// Comment parses as text.
+	value = ParseText( p, "-->", &p );
 	return p;
 }
 
 
+// --------- XMLDocument ----------- //
 XMLDocument::XMLDocument() : 
 	charBuffer( 0 )
 {
 }
 
 
-bool XMLDocument::Parse( const char* p ) 
+XMLDocument::~XMLDocument()
+{
+	delete root;
+	delete charBuffer;
+}
+
+
+
+bool XMLDocument::Parse( const char* p )
+{
+	charBuffer = CharBuffer.Construct( p );
+	XMLNode* node = 0;
+	Identify( charBuffer., node );
+	node->Parse( p );
+}
+
+
+XMLComment* XMLDocument::newComment( XMLNode* parent )
+{
+	
+}
+
+
+
+
+char* XMLDocument::Identify( char* p, XMLNode** node ) 
 {
 	XMLNode* returnNode = 0;
 
@@ -62,11 +173,12 @@ bool XMLDocument::Parse( const char* p )
 	const char* cdataHeader = { "<![CDATA[" };
 
 	if ( XMLNode::StringEqual( p, xmlHeader, 5 ) ) {
-
+		returnNode = new XMLComment();
 	}
 	else {
 		TIXMLASSERT( 0 );
 	}
 
-	return true;
+	*node = returnNode;
+	return p;
 }

+ 42 - 3
tinyxml2.h

@@ -27,6 +27,7 @@
 
 namespace tinyxml2
 {
+class XMLDocument*;
 
 // internal - move to separate namespace
 struct CharBuffer
@@ -47,8 +48,12 @@ public:
 	static XMLNode* Identify( const char* p );
 
 protected:
-	static const char* SkipWhiteSpace( const char* p );
-	static char* SkipWhiteSpace( char* p ) { return (char*) SkipWhiteSpace( (const char*)p ); }
+	XMLNode( XMLDocument* );
+	virtual ~XMLNode();
+
+	// Utility
+	static const char* SkipWhiteSpace( const char* p )	{ while( isspace( *p ) ) { ++p; } return p; }
+	static char* SkipWhiteSpace( char* p )				{ while( isspace( *p ) ) { ++p; } return p; }
 
 	inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX )  {
 		int n = 0;
@@ -61,6 +66,26 @@ protected:
 		return false;
 	}
 
+	/* Parses text. (Not a text node.)
+	   - [ ] EOL normalization.
+	   - [x] Trim leading whitespace
+	   - [ ] Trim trailing whitespace.
+	   - [ ] Leaves inner whitespace
+	   - [ ] Inserts one space between lines.
+	*/
+	const char* ParseText( char* in, const char* endTag, char** next );
+
+	virtual char* ParseDeep( char* )	{ TIXMLASSERT( 0 ); }
+
+	XMLDocument*	document;
+	XMLNode*		parent;
+
+	XMLNode*		firstChild;
+	XMLNode*		lastChild;
+
+	XMLNode*		prev;
+	XMLNode*		next;
+
 private:
 
 };
@@ -68,7 +93,12 @@ private:
 
 class XMLComment : public XMLNode
 {
+public:
+	XMLComment( XMLDocument* doc );
+	virtual ~XMLComment();
 
+private:
+	char* value;
 };
 
 
@@ -76,12 +106,21 @@ class XMLDocument
 {
 public:
 	XMLDocument();
+	~XMLDocument();
 
 	bool Parse( const char* );
 
+	XMLNode* Root()				{ return root; }
+	XMLNode* RootElement();
+
+	XMLNode* InsertEndChild( XMLNode* addThis );
+
 private:
-	XMLDocument( const XMLDocument& );	// not implemented
+	XMLDocument( const XMLDocument& );	// intentionally not implemented
+
+	virtual char* ParseDeep( char* );
 
+	XMLNode*	root;
 	CharBuffer* charBuffer;
 };
 

BIN
tinyxml2.suo