blob: 83bafb39ce3e6f0d271e28945c48d131667515d2 [file] [log] [blame]
U-Lama\Lee560bd472011-12-28 19:42:49 -08001#include "tinyxml2.h"
2
3#include <string.h>
4#include <stdlib.h>
5#include <stdio.h>
U-Lama\Lee4cee6112011-12-31 14:58:18 -08006#include <ctype.h>
U-Lama\Lee560bd472011-12-28 19:42:49 -08007
8using namespace tinyxml2;
9
Lee Thomason3f57d272012-01-11 15:30:03 -080010// --------- CharBuffer ----------- //
U-Lama\Lee560bd472011-12-28 19:42:49 -080011/*static*/ CharBuffer* CharBuffer::Construct( const char* in )
12{
13 size_t len = strlen( in );
14 size_t size = len + sizeof( CharBuffer );
15 CharBuffer* cb = (CharBuffer*) malloc( size );
16 cb->length = len;
17 strcpy( cb->mem, in );
18 return cb;
19}
20
21
22/*static*/ void CharBuffer::Free( CharBuffer* cb )
23{
24 free( cb );
25}
26
27
Lee Thomason3f57d272012-01-11 15:30:03 -080028// --------- XMLNode ----------- //
29
30XMLNode::XMLNode( XMLDocument* doc ) :
31 document( doc ),
32 parent( 0 ),
33 firstChild( 0 ), lastChild( 0 ),
34 prev( 0 ), next( 0 )
U-Lama\Lee4cee6112011-12-31 14:58:18 -080035{
Lee Thomason3f57d272012-01-11 15:30:03 -080036
37}
38
39
40XMLNode::~XMLNode()
41{
42 XMLNode* node=firstChild;
43 while( node ) {
44 XMLNode* temp = node->next;
45 delete node;
46 node = temp;
U-Lama\Lee4cee6112011-12-31 14:58:18 -080047 }
Lee Thomason3f57d272012-01-11 15:30:03 -080048}
49
50
51XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
52{
53 if ( lastChild ) {
54 TIXMLASSERT( firstChild );
55 TIXMLASSERT( lastChild->next == 0 );
56 lastChild->next = addThis;
57 addThis->prev = lastChild;
58 lastChild = addThis;
59
60 addThis->parent = this;
61 addThis->next = null;
62 }
63 else {
64 TIXMLASSERT( firstChild == 0 );
65 firstChild = lastChild = addThis;
66
67 addThis->parent = this;
68 addThis->prev = 0;
69 addThis->next = null;
70 }
71}
72
73
74const char* XMLNode::ParseText( char* p, const char* endTag, char** next )
75{
76 TIXMLASSERT( endTag && *endTag );
77
78 char* start = SkipWhiteSpace( p );
79 if ( !start )
80 return;
81
82 char endChar = *endTag;
83 p = start;
84 int length = strlen( endTag );
85
86 while ( *p ) {
87 if ( *p == endChar ) {
88 if ( strncmp( p, endTag, length ) == 0 ) {
89 *p = 0;
90 *next = p + length;
91 return start;
92 }
93 }
94 ++p;
95 }
96 return 0;
97}
98
99
100// --------- XMLComment ---------- //
101
102XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
103{
104}
105
106
107virtual XMLComment::~XMLComment()
108{
109
110}
111
112
113virtual char* XMLComment::ParseDeep( char* p )
114{
115 // Comment parses as text.
116 value = ParseText( p, "-->", &p );
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800117 return p;
118}
119
120
Lee Thomason3f57d272012-01-11 15:30:03 -0800121// --------- XMLDocument ----------- //
U-Lama\Lee560bd472011-12-28 19:42:49 -0800122XMLDocument::XMLDocument() :
123 charBuffer( 0 )
124{
125}
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800126
127
Lee Thomason3f57d272012-01-11 15:30:03 -0800128XMLDocument::~XMLDocument()
129{
130 delete root;
131 delete charBuffer;
132}
133
134
135
136bool XMLDocument::Parse( const char* p )
137{
138 charBuffer = CharBuffer.Construct( p );
139 XMLNode* node = 0;
140 Identify( charBuffer., node );
141 node->Parse( p );
142}
143
144
145XMLComment* XMLDocument::newComment( XMLNode* parent )
146{
147
148}
149
150
151
152
153char* XMLDocument::Identify( char* p, XMLNode** node )
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800154{
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800155 XMLNode* returnNode = 0;
156
157 p = XMLNode::SkipWhiteSpace( p );
158 if( !p || !*p || *p != '<' )
159 {
160 return 0;
161 }
162
163 // What is this thing?
164 // - Elements start with a letter or underscore, but xml is reserved.
165 // - Comments: <!--
166 // - Decleration: <?xml
167 // - Everthing else is unknown to tinyxml.
168 //
169
170 const char* xmlHeader = { "<?xml" };
171 const char* commentHeader = { "<!--" };
172 const char* dtdHeader = { "<!" };
173 const char* cdataHeader = { "<![CDATA[" };
174
175 if ( XMLNode::StringEqual( p, xmlHeader, 5 ) ) {
Lee Thomason3f57d272012-01-11 15:30:03 -0800176 returnNode = new XMLComment();
U-Lama\Lee4cee6112011-12-31 14:58:18 -0800177 }
178 else {
179 TIXMLASSERT( 0 );
180 }
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800181
Lee Thomason3f57d272012-01-11 15:30:03 -0800182 *node = returnNode;
183 return p;
U-Lama\Leee13c3e62011-12-28 14:36:55 -0800184}