U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 1 | #include "tinyxml2.h"
|
| 2 |
|
| 3 | #include <string.h>
|
| 4 | #include <stdlib.h>
|
| 5 | #include <stdio.h>
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 6 | #include <ctype.h>
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 7 |
|
| 8 | using namespace tinyxml2;
|
| 9 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 10 | // --------- CharBuffer ----------- //
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 11 | /*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 Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 28 | // --------- XMLNode ----------- //
|
| 29 |
|
| 30 | XMLNode::XMLNode( XMLDocument* doc ) :
|
| 31 | document( doc ),
|
| 32 | parent( 0 ),
|
| 33 | firstChild( 0 ), lastChild( 0 ),
|
| 34 | prev( 0 ), next( 0 )
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 35 | {
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 36 |
|
| 37 | }
|
| 38 |
|
| 39 |
|
| 40 | XMLNode::~XMLNode()
|
| 41 | {
|
| 42 | XMLNode* node=firstChild;
|
| 43 | while( node ) {
|
| 44 | XMLNode* temp = node->next;
|
| 45 | delete node;
|
| 46 | node = temp;
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 47 | }
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 48 | }
|
| 49 |
|
| 50 |
|
| 51 | XMLNode* 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;
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame^] | 61 | addThis->next = 0;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 62 | }
|
| 63 | else {
|
| 64 | TIXMLASSERT( firstChild == 0 );
|
| 65 | firstChild = lastChild = addThis;
|
| 66 |
|
| 67 | addThis->parent = this;
|
| 68 | addThis->prev = 0;
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame^] | 69 | addThis->next = 0;
|
| 70 | }
|
| 71 | return addThis;
|
| 72 | }
|
| 73 |
|
| 74 |
|
| 75 | void XMLNode::Print( FILE* fp, int depth )
|
| 76 | {
|
| 77 | for( int i=0; i<depth; ++i ) {
|
| 78 | fprintf( fp, " " );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 79 | }
|
| 80 | }
|
| 81 |
|
| 82 |
|
| 83 | const char* XMLNode::ParseText( char* p, const char* endTag, char** next )
|
| 84 | {
|
| 85 | TIXMLASSERT( endTag && *endTag );
|
| 86 |
|
| 87 | char* start = SkipWhiteSpace( p );
|
| 88 | if ( !start )
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame^] | 89 | return 0;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 90 |
|
| 91 | char endChar = *endTag;
|
| 92 | p = start;
|
| 93 | int length = strlen( endTag );
|
| 94 |
|
| 95 | while ( *p ) {
|
| 96 | if ( *p == endChar ) {
|
| 97 | if ( strncmp( p, endTag, length ) == 0 ) {
|
| 98 | *p = 0;
|
| 99 | *next = p + length;
|
| 100 | return start;
|
| 101 | }
|
| 102 | }
|
| 103 | ++p;
|
| 104 | }
|
| 105 | return 0;
|
| 106 | }
|
| 107 |
|
| 108 |
|
| 109 | // --------- XMLComment ---------- //
|
| 110 |
|
| 111 | XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
|
| 112 | {
|
| 113 | }
|
| 114 |
|
| 115 |
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame^] | 116 | XMLComment::~XMLComment()
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 117 | {
|
| 118 |
|
| 119 | }
|
| 120 |
|
| 121 |
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame^] | 122 | void XMLComment::Print( FILE* fp, int depth )
|
| 123 | {
|
| 124 | XMLNode::Print( fp, depth );
|
| 125 | fprintf( fp, "<!-- %s -->\n", value );
|
| 126 | }
|
| 127 |
|
| 128 |
|
| 129 | char* XMLComment::ParseDeep( char* p )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 130 | {
|
| 131 | // Comment parses as text.
|
| 132 | value = ParseText( p, "-->", &p );
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 133 | return p;
|
| 134 | }
|
| 135 |
|
| 136 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 137 | // --------- XMLDocument ----------- //
|
U-Lama\Lee | 560bd47 | 2011-12-28 19:42:49 -0800 | [diff] [blame] | 138 | XMLDocument::XMLDocument() :
|
| 139 | charBuffer( 0 )
|
| 140 | {
|
| 141 | }
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 142 |
|
| 143 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 144 | XMLDocument::~XMLDocument()
|
| 145 | {
|
| 146 | delete root;
|
| 147 | delete charBuffer;
|
| 148 | }
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 | bool XMLDocument::Parse( const char* p )
|
| 153 | {
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame^] | 154 | charBuffer = CharBuffer::Construct( p );
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 155 | XMLNode* node = 0;
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame^] | 156 | char* q = Identify( charBuffer->mem, &node );
|
| 157 | node->ParseDeep( q );
|
| 158 | return true;
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 159 | }
|
| 160 |
|
| 161 |
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame^] | 162 | void XMLDocument::Print( FILE* fp, int depth )
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 163 | {
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame^] | 164 | for( XMLNode* node = root->firstChild; node; node=node->next ) {
|
| 165 | node->Print( fp, depth );
|
| 166 | }
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 167 | }
|
| 168 |
|
| 169 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 170 | char* XMLDocument::Identify( char* p, XMLNode** node )
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 171 | {
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 172 | XMLNode* returnNode = 0;
|
| 173 |
|
| 174 | p = XMLNode::SkipWhiteSpace( p );
|
| 175 | if( !p || !*p || *p != '<' )
|
| 176 | {
|
| 177 | return 0;
|
| 178 | }
|
| 179 |
|
| 180 | // What is this thing?
|
| 181 | // - Elements start with a letter or underscore, but xml is reserved.
|
| 182 | // - Comments: <!--
|
| 183 | // - Decleration: <?xml
|
| 184 | // - Everthing else is unknown to tinyxml.
|
| 185 | //
|
| 186 |
|
| 187 | const char* xmlHeader = { "<?xml" };
|
| 188 | const char* commentHeader = { "<!--" };
|
| 189 | const char* dtdHeader = { "<!" };
|
| 190 | const char* cdataHeader = { "<![CDATA[" };
|
| 191 |
|
| 192 | if ( XMLNode::StringEqual( p, xmlHeader, 5 ) ) {
|
Lee Thomason | ce0763e | 2012-01-11 15:43:54 -0800 | [diff] [blame^] | 193 | returnNode = new XMLComment( this );
|
U-Lama\Lee | 4cee611 | 2011-12-31 14:58:18 -0800 | [diff] [blame] | 194 | }
|
| 195 | else {
|
| 196 | TIXMLASSERT( 0 );
|
| 197 | }
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 198 |
|
Lee Thomason | 3f57d27 | 2012-01-11 15:30:03 -0800 | [diff] [blame] | 199 | *node = returnNode;
|
| 200 | return p;
|
U-Lama\Lee | e13c3e6 | 2011-12-28 14:36:55 -0800 | [diff] [blame] | 201 | }
|