Switched to Artistic Style auto-formatting to allow integration of patches from other coding styles.

This commit is contained in:
Lee Thomason 2012-10-11 16:56:51 -07:00
parent 3572ae0308
commit a9cf3f9f3f
2 changed files with 2551 additions and 2156 deletions

223
tinyxml2.cpp Normal file → Executable file
View File

@ -24,10 +24,10 @@ distribution.
#include "tinyxml2.h" #include "tinyxml2.h"
#include <new> // yes, this one new style header, is in the Android SDK. #include <new> // yes, this one new style header, is in the Android SDK.
#ifdef ANDROID_NDK # ifdef ANDROID_NDK
#include <stddef.h> # include <stddef.h>
#else #else
#include <cstddef> # include <cstddef>
#endif #endif
using namespace tinyxml2; using namespace tinyxml2;
@ -54,14 +54,14 @@ static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
node->~XMLNode(); \ node->~XMLNode(); \
pool->Free( node ); \ pool->Free( node ); \
} \ } \
} }
#define DELETE_ATTRIBUTE( attrib ) { \ #define DELETE_ATTRIBUTE( attrib ) { \
if ( attrib ) { \ if ( attrib ) { \
MemPool* pool = attrib->memPool; \ MemPool* pool = attrib->memPool; \
attrib->~XMLAttribute(); \ attrib->~XMLAttribute(); \
pool->Free( attrib ); \ pool->Free( attrib ); \
} \ } \
} }
struct Entity { struct Entity {
const char* pattern; const char* pattern;
@ -70,8 +70,7 @@ struct Entity {
}; };
static const int NUM_ENTITIES = 5; static const int NUM_ENTITIES = 5;
static const Entity entities[NUM_ENTITIES] = static const Entity entities[NUM_ENTITIES] = {
{
{ "quot", 4, DOUBLE_QUOTE }, { "quot", 4, DOUBLE_QUOTE },
{ "amp", 3, '&' }, { "amp", 3, '&' },
{ "apos", 4, SINGLE_QUOTE }, { "apos", 4, SINGLE_QUOTE },
@ -141,8 +140,7 @@ char* StrPair::ParseName( char* p )
|| *p == '_' || *p == '_'
|| *p == ':' || *p == ':'
|| (*p == '-' && p>start ) // can be in a name, but not lead it. || (*p == '-' && p>start ) // can be in a name, but not lead it.
|| (*p == '.' && p>start ) )) // can be in a name, but not lead it. || (*p == '.' && p>start ) )) { // can be in a name, but not lead it.
{
++p; ++p;
} }
@ -166,8 +164,9 @@ void StrPair::CollapseWhitespace()
while( *p ) { while( *p ) {
if ( XMLUtil::IsWhiteSpace( *p )) { if ( XMLUtil::IsWhiteSpace( *p )) {
p = XMLUtil::SkipWhiteSpace( p ); p = XMLUtil::SkipWhiteSpace( p );
if ( *p == 0 ) if ( *p == 0 ) {
break; // don't write to q; this trims the trailing space. break; // don't write to q; this trims the trailing space.
}
*q = ' '; *q = ' ';
++q; ++q;
} }
@ -231,8 +230,7 @@ const char* StrPair::GetStr()
int i=0; int i=0;
for(; i<NUM_ENTITIES; ++i ) { for(; i<NUM_ENTITIES; ++i ) {
if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0 if ( strncmp( p+1, entities[i].pattern, entities[i].length ) == 0
&& *(p+entities[i].length+1) == ';' ) && *(p+entities[i].length+1) == ';' ) {
{
// Found an entity convert; // Found an entity convert;
*q = entities[i].value; *q = entities[i].value;
++q; ++q;
@ -277,8 +275,7 @@ const char* XMLUtil::ReadBOM( const char* p, bool* bom )
// Check for BOM: // Check for BOM:
if ( *(pu+0) == TIXML_UTF_LEAD_0 if ( *(pu+0) == TIXML_UTF_LEAD_0
&& *(pu+1) == TIXML_UTF_LEAD_1 && *(pu+1) == TIXML_UTF_LEAD_1
&& *(pu+2) == TIXML_UTF_LEAD_2 ) && *(pu+2) == TIXML_UTF_LEAD_2 ) {
{
*bom = true; *bom = true;
p += 3; p += 3;
} }
@ -292,22 +289,27 @@ void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length
const unsigned long BYTE_MARK = 0x80; const unsigned long BYTE_MARK = 0x80;
const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
if (input < 0x80) if (input < 0x80) {
*length = 1; *length = 1;
else if ( input < 0x800 ) }
else if ( input < 0x800 ) {
*length = 2; *length = 2;
else if ( input < 0x10000 ) }
else if ( input < 0x10000 ) {
*length = 3; *length = 3;
else if ( input < 0x200000 ) }
else if ( input < 0x200000 ) {
*length = 4; *length = 4;
else }
{ *length = 0; return; } // This code won't covert this correctly anyway. else {
*length = 0; // This code won't covert this correctly anyway.
return;
}
output += *length; output += *length;
// Scary scary fall throughs. // Scary scary fall throughs.
switch (*length) switch (*length) {
{
case 4: case 4:
--output; --output;
*output = (char)((input | BYTE_MARK) & BYTE_MASK); *output = (char)((input | BYTE_MARK) & BYTE_MASK);
@ -332,58 +334,67 @@ const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
// Presume an entity, and pull it out. // Presume an entity, and pull it out.
*length = 0; *length = 0;
if ( *(p+1) == '#' && *(p+2) ) if ( *(p+1) == '#' && *(p+2) ) {
{
unsigned long ucs = 0; unsigned long ucs = 0;
ptrdiff_t delta = 0; ptrdiff_t delta = 0;
unsigned mult = 1; unsigned mult = 1;
if ( *(p+2) == 'x' ) if ( *(p+2) == 'x' ) {
{
// Hexadecimal. // Hexadecimal.
if ( !*(p+3) ) return 0; if ( !*(p+3) ) {
return 0;
}
const char* q = p+3; const char* q = p+3;
q = strchr( q, ';' ); q = strchr( q, ';' );
if ( !q || !*q ) return 0; if ( !q || !*q ) {
return 0;
}
delta = q-p; delta = q-p;
--q; --q;
while ( *q != 'x' ) while ( *q != 'x' ) {
{ if ( *q >= '0' && *q <= '9' ) {
if ( *q >= '0' && *q <= '9' )
ucs += mult * (*q - '0'); ucs += mult * (*q - '0');
else if ( *q >= 'a' && *q <= 'f' ) }
else if ( *q >= 'a' && *q <= 'f' ) {
ucs += mult * (*q - 'a' + 10); ucs += mult * (*q - 'a' + 10);
else if ( *q >= 'A' && *q <= 'F' ) }
else if ( *q >= 'A' && *q <= 'F' ) {
ucs += mult * (*q - 'A' + 10 ); ucs += mult * (*q - 'A' + 10 );
else }
else {
return 0; return 0;
}
mult *= 16; mult *= 16;
--q; --q;
} }
} }
else else {
{
// Decimal. // Decimal.
if ( !*(p+2) ) return 0; if ( !*(p+2) ) {
return 0;
}
const char* q = p+2; const char* q = p+2;
q = strchr( q, ';' ); q = strchr( q, ';' );
if ( !q || !*q ) return 0; if ( !q || !*q ) {
return 0;
}
delta = q-p; delta = q-p;
--q; --q;
while ( *q != '#' ) while ( *q != '#' ) {
{ if ( *q >= '0' && *q <= '9' ) {
if ( *q >= '0' && *q <= '9' )
ucs += mult * (*q - '0'); ucs += mult * (*q - '0');
else }
else {
return 0; return 0;
}
mult *= 10; mult *= 10;
--q; --q;
} }
@ -428,15 +439,17 @@ void XMLUtil::ToStr( double v, char* buffer, int bufferSize )
bool XMLUtil::ToInt( const char* str, int* value ) bool XMLUtil::ToInt( const char* str, int* value )
{ {
if ( TIXML_SSCANF( str, "%d", value ) == 1 ) if ( TIXML_SSCANF( str, "%d", value ) == 1 ) {
return true; return true;
}
return false; return false;
} }
bool XMLUtil::ToUnsigned( const char* str, unsigned *value ) bool XMLUtil::ToUnsigned( const char* str, unsigned *value )
{ {
if ( TIXML_SSCANF( str, "%u", value ) == 1 ) if ( TIXML_SSCANF( str, "%u", value ) == 1 ) {
return true; return true;
}
return false; return false;
} }
@ -481,8 +494,7 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
XMLNode* returnNode = 0; XMLNode* returnNode = 0;
char* start = p; char* start = p;
p = XMLUtil::SkipWhiteSpace( p ); p = XMLUtil::SkipWhiteSpace( p );
if( !p || !*p ) if( !p || !*p ) {
{
return p; return p;
} }
@ -554,14 +566,13 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
bool XMLDocument::Accept( XMLVisitor* visitor ) const bool XMLDocument::Accept( XMLVisitor* visitor ) const
{ {
if ( visitor->VisitEnter( *this ) ) if ( visitor->VisitEnter( *this ) ) {
{ for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) if ( !node->Accept( visitor ) ) {
{
if ( !node->Accept( visitor ) )
break; break;
} }
} }
}
return visitor->VisitExit( *this ); return visitor->VisitExit( *this );
} }
@ -588,10 +599,12 @@ XMLNode::~XMLNode()
void XMLNode::SetValue( const char* str, bool staticMem ) void XMLNode::SetValue( const char* str, bool staticMem )
{ {
if ( staticMem ) if ( staticMem ) {
value.SetInternedStr( str ); value.SetInternedStr( str );
else }
else {
value.SetStr( str ); value.SetStr( str );
}
} }
@ -610,10 +623,12 @@ void XMLNode::DeleteChildren()
void XMLNode::Unlink( XMLNode* child ) void XMLNode::Unlink( XMLNode* child )
{ {
TIXMLASSERT( child->parent == this ); TIXMLASSERT( child->parent == this );
if ( child == firstChild ) if ( child == firstChild ) {
firstChild = firstChild->next; firstChild = firstChild->next;
if ( child == lastChild ) }
if ( child == lastChild ) {
lastChild = lastChild->prev; lastChild = lastChild->prev;
}
if ( child->prev ) { if ( child->prev ) {
child->prev->next = child->next; child->prev->next = child->next;
@ -682,8 +697,9 @@ XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis ) XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
{ {
TIXMLASSERT( afterThis->parent == this ); TIXMLASSERT( afterThis->parent == this );
if ( afterThis->parent != this ) if ( afterThis->parent != this ) {
return 0; return 0;
}
if ( afterThis->next == 0 ) { if ( afterThis->next == 0 ) {
// The last node or the only node. // The last node or the only node.
@ -732,8 +748,7 @@ const XMLElement* XMLNode::NextSiblingElement( const char* value ) const
{ {
for( XMLNode* element=this->next; element; element = element->next ) { for( XMLNode* element=this->next; element; element = element->next ) {
if ( element->ToElement() if ( element->ToElement()
&& (!value || XMLUtil::StringEqual( value, element->Value() ))) && (!value || XMLUtil::StringEqual( value, element->Value() ))) {
{
return element->ToElement(); return element->ToElement();
} }
} }
@ -745,8 +760,7 @@ const XMLElement* XMLNode::PreviousSiblingElement( const char* value ) const
{ {
for( XMLNode* element=this->prev; element; element = element->prev ) { for( XMLNode* element=this->prev; element; element = element->prev ) {
if ( element->ToElement() if ( element->ToElement()
&& (!value || XMLUtil::StringEqual( value, element->Value() ))) && (!value || XMLUtil::StringEqual( value, element->Value() ))) {
{
return element->ToElement(); return element->ToElement();
} }
} }
@ -844,8 +858,9 @@ char* XMLText::ParseDeep( char* p, StrPair* )
} }
else { else {
int flags = document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES; int flags = document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES;
if ( document->WhitespaceMode() == COLLAPSE_WHITESPACE ) if ( document->WhitespaceMode() == COLLAPSE_WHITESPACE ) {
flags |= StrPair::COLLAPSE_WHITESPACE; flags |= StrPair::COLLAPSE_WHITESPACE;
}
p = value.ParseText( p, "<", flags ); p = value.ParseText( p, "<", flags );
if ( !p ) { if ( !p ) {
@ -1027,15 +1042,21 @@ char* XMLAttribute::ParseDeep( char* p, bool processEntities )
{ {
// Parse using the name rules: bug fix, was using ParseText before // Parse using the name rules: bug fix, was using ParseText before
p = name.ParseName( p ); p = name.ParseName( p );
if ( !p || !*p ) return 0; if ( !p || !*p ) {
return 0;
}
// Skip white space before = // Skip white space before =
p = XMLUtil::SkipWhiteSpace( p ); p = XMLUtil::SkipWhiteSpace( p );
if ( !p || *p != '=' ) return 0; if ( !p || *p != '=' ) {
return 0;
}
++p; // move up to opening quote ++p; // move up to opening quote
p = XMLUtil::SkipWhiteSpace( p ); p = XMLUtil::SkipWhiteSpace( p );
if ( *p != '\"' && *p != '\'' ) return 0; if ( *p != '\"' && *p != '\'' ) {
return 0;
}
char endTag[2] = { *p, 0 }; char endTag[2] = { *p, 0 };
++p; // move past opening quote ++p; // move past opening quote
@ -1053,16 +1074,18 @@ void XMLAttribute::SetName( const char* n )
int XMLAttribute::QueryIntValue( int* value ) const int XMLAttribute::QueryIntValue( int* value ) const
{ {
if ( XMLUtil::ToInt( Value(), value )) if ( XMLUtil::ToInt( Value(), value )) {
return XML_NO_ERROR; return XML_NO_ERROR;
}
return XML_WRONG_ATTRIBUTE_TYPE; return XML_WRONG_ATTRIBUTE_TYPE;
} }
int XMLAttribute::QueryUnsignedValue( unsigned int* value ) const int XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
{ {
if ( XMLUtil::ToUnsigned( Value(), value )) if ( XMLUtil::ToUnsigned( Value(), value )) {
return XML_NO_ERROR; return XML_NO_ERROR;
}
return XML_WRONG_ATTRIBUTE_TYPE; return XML_WRONG_ATTRIBUTE_TYPE;
} }
@ -1078,16 +1101,18 @@ int XMLAttribute::QueryBoolValue( bool* value ) const
int XMLAttribute::QueryFloatValue( float* value ) const int XMLAttribute::QueryFloatValue( float* value ) const
{ {
if ( XMLUtil::ToFloat( Value(), value )) if ( XMLUtil::ToFloat( Value(), value )) {
return XML_NO_ERROR; return XML_NO_ERROR;
}
return XML_WRONG_ATTRIBUTE_TYPE; return XML_WRONG_ATTRIBUTE_TYPE;
} }
int XMLAttribute::QueryDoubleValue( double* value ) const int XMLAttribute::QueryDoubleValue( double* value ) const
{ {
if ( XMLUtil::ToDouble( Value(), value )) if ( XMLUtil::ToDouble( Value(), value )) {
return XML_NO_ERROR; return XML_NO_ERROR;
}
return XML_WRONG_ATTRIBUTE_TYPE; return XML_WRONG_ATTRIBUTE_TYPE;
} }
@ -1158,9 +1183,10 @@ XMLAttribute* XMLElement::FindAttribute( const char* name )
{ {
XMLAttribute* a = 0; XMLAttribute* a = 0;
for( a=rootAttribute; a; a = a->next ) { for( a=rootAttribute; a; a = a->next ) {
if ( XMLUtil::StringEqual( a->Name(), name ) ) if ( XMLUtil::StringEqual( a->Name(), name ) ) {
return a; return a;
} }
}
return 0; return 0;
} }
@ -1169,9 +1195,10 @@ const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
{ {
XMLAttribute* a = 0; XMLAttribute* a = 0;
for( a=rootAttribute; a; a = a->next ) { for( a=rootAttribute; a; a = a->next ) {
if ( XMLUtil::StringEqual( a->Name(), name ) ) if ( XMLUtil::StringEqual( a->Name(), name ) ) {
return a; return a;
} }
}
return 0; return 0;
} }
@ -1179,10 +1206,12 @@ const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
const char* XMLElement::Attribute( const char* name, const char* value ) const const char* XMLElement::Attribute( const char* name, const char* value ) const
{ {
const XMLAttribute* a = FindAttribute( name ); const XMLAttribute* a = FindAttribute( name );
if ( !a ) if ( !a ) {
return 0; return 0;
if ( !value || XMLUtil::StringEqual( a->Value(), value )) }
if ( !value || XMLUtil::StringEqual( a->Value(), value )) {
return a->Value(); return a->Value();
}
return 0; return 0;
} }
@ -1268,8 +1297,7 @@ XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
XMLAttribute* attrib = 0; XMLAttribute* attrib = 0;
for( attrib = rootAttribute; for( attrib = rootAttribute;
attrib; attrib;
last = attrib, attrib = attrib->next ) last = attrib, attrib = attrib->next ) {
{
if ( XMLUtil::StringEqual( attrib->Name(), name ) ) { if ( XMLUtil::StringEqual( attrib->Name(), name ) ) {
break; break;
} }
@ -1372,7 +1400,9 @@ char* XMLElement::ParseDeep( char* p, StrPair* strPair )
{ {
// Read the element name. // Read the element name.
p = XMLUtil::SkipWhiteSpace( p ); p = XMLUtil::SkipWhiteSpace( p );
if ( !p ) return 0; if ( !p ) {
return 0;
}
// The closing element is the </element> form. It is // The closing element is the </element> form. It is
// parsed just like a regular element then deleted from // parsed just like a regular element then deleted from
@ -1383,11 +1413,14 @@ char* XMLElement::ParseDeep( char* p, StrPair* strPair )
} }
p = value.ParseName( p ); p = value.ParseName( p );
if ( value.Empty() ) return 0; if ( value.Empty() ) {
return 0;
}
p = ParseAttributes( p ); p = ParseAttributes( p );
if ( !p || !*p || closingType ) if ( !p || !*p || closingType ) {
return p; return p;
}
p = XMLNode::ParseDeep( p, strPair ); p = XMLNode::ParseDeep( p, strPair );
return p; return p;
@ -1435,14 +1468,13 @@ bool XMLElement::ShallowEqual( const XMLNode* compare ) const
bool XMLElement::Accept( XMLVisitor* visitor ) const bool XMLElement::Accept( XMLVisitor* visitor ) const
{ {
if ( visitor->VisitEnter( *this, rootAttribute ) ) if ( visitor->VisitEnter( *this, rootAttribute ) ) {
{ for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) if ( !node->Accept( visitor ) ) {
{
if ( !node->Accept( visitor ) )
break; break;
} }
} }
}
return visitor->VisitExit( *this ); return visitor->VisitExit( *this );
} }
@ -1544,13 +1576,13 @@ int XMLDocument::LoadFile( const char* filename )
InitDocument(); InitDocument();
FILE* fp = 0; FILE* fp = 0;
#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
errno_t err = fopen_s(&fp, filename, "rb" ); errno_t err = fopen_s(&fp, filename, "rb" );
if ( !fp || err) { if ( !fp || err) {
#else #else
fp = fopen( filename, "rb" ); fp = fopen( filename, "rb" );
if ( !fp) { if ( !fp) {
#endif #endif
SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 ); SetError( XML_ERROR_FILE_NOT_FOUND, filename, 0 );
return errorID; return errorID;
} }
@ -1598,13 +1630,13 @@ int XMLDocument::LoadFile( FILE* fp )
int XMLDocument::SaveFile( const char* filename, bool compact ) int XMLDocument::SaveFile( const char* filename, bool compact )
{ {
FILE* fp = 0; FILE* fp = 0;
#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
errno_t err = fopen_s(&fp, filename, "w" ); errno_t err = fopen_s(&fp, filename, "w" );
if ( !fp || err) { if ( !fp || err) {
#else #else
fp = fopen( filename, "w" ); fp = fopen( filename, "w" );
if ( !fp) { if ( !fp) {
#endif #endif
SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 ); SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, filename, 0 );
return errorID; return errorID;
} }
@ -1653,8 +1685,9 @@ int XMLDocument::Parse( const char* p, size_t len )
void XMLDocument::Print( XMLPrinter* streamer ) void XMLDocument::Print( XMLPrinter* streamer )
{ {
XMLPrinter stdStreamer( stdout ); XMLPrinter stdStreamer( stdout );
if ( !streamer ) if ( !streamer ) {
streamer = &stdStreamer; streamer = &stdStreamer;
}
Accept( streamer ); Accept( streamer );
} }
@ -1724,7 +1757,7 @@ void XMLPrinter::Print( const char* format, ... )
else { else {
// This seems brutally complex. Haven't figured out a better // This seems brutally complex. Haven't figured out a better
// way on windows. // way on windows.
#ifdef _MSC_VER #ifdef _MSC_VER
int len = -1; int len = -1;
int expand = 1000; int expand = 1000;
while ( len < 0 ) { while ( len < 0 ) {
@ -1736,14 +1769,14 @@ void XMLPrinter::Print( const char* format, ... )
} }
char* p = buffer.PushArr( len ) - 1; char* p = buffer.PushArr( len ) - 1;
memcpy( p, accumulator.Mem(), len+1 ); memcpy( p, accumulator.Mem(), len+1 );
#else #else
int len = vsnprintf( 0, 0, format, va ); int len = vsnprintf( 0, 0, format, va );
// Close out and re-start the va-args // Close out and re-start the va-args
va_end( va ); va_end( va );
va_start( va, format ); va_start( va, format );
char* p = buffer.PushArr( len ) - 1; char* p = buffer.PushArr( len ) - 1;
vsnprintf( p, len+1, format, va ); vsnprintf( p, len+1, format, va );
#endif #endif
} }
va_end( va ); va_end( va );
} }
@ -1883,10 +1916,12 @@ void XMLPrinter::CloseElement()
Print( "</%s>", name ); Print( "</%s>", name );
} }
if ( textDepth == depth ) if ( textDepth == depth ) {
textDepth = -1; textDepth = -1;
if ( depth == 0 && !compactMode) }
if ( depth == 0 && !compactMode) {
Print( "\n" ); Print( "\n" );
}
elementJustOpened = false; elementJustOpened = false;
} }

758
tinyxml2.h Normal file → Executable file

File diff suppressed because it is too large Load Diff