2012-02-26 05:11:20 +00:00
|
|
|
/*
|
|
|
|
Original code by Lee Thomason (www.grinninglizard.com)
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
|
|
|
damages arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product documentation
|
|
|
|
would be appreciated but is not required.
|
|
|
|
|
|
|
|
2. Altered source versions must be plainly marked as such, and
|
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
3. This notice may not be removed or altered from any source
|
|
|
|
distribution.
|
|
|
|
*/
|
2012-02-25 05:50:50 +00:00
|
|
|
|
2011-12-29 03:42:49 +00:00
|
|
|
#include "tinyxml2.h"
|
|
|
|
|
2012-08-21 05:00:38 +00:00
|
|
|
#include <new> // yes, this one new style header, is in the Android SDK.
|
2016-03-25 13:01:15 +00:00
|
|
|
#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
|
2012-10-11 23:56:51 +00:00
|
|
|
# include <stddef.h>
|
2015-07-30 14:02:26 +00:00
|
|
|
# include <stdarg.h>
|
2012-08-21 05:00:38 +00:00
|
|
|
#else
|
2012-10-11 23:56:51 +00:00
|
|
|
# include <cstddef>
|
2015-07-30 14:02:26 +00:00
|
|
|
# include <cstdarg>
|
2012-08-21 05:00:38 +00:00
|
|
|
#endif
|
2011-12-29 03:42:49 +00:00
|
|
|
|
2015-06-12 05:52:08 +00:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
|
2015-06-22 09:49:32 +00:00
|
|
|
// Microsoft Visual Studio, version 2005 and higher. Not WinCE.
|
2015-06-12 05:52:08 +00:00
|
|
|
/*int _snprintf_s(
|
|
|
|
char *buffer,
|
|
|
|
size_t sizeOfBuffer,
|
|
|
|
size_t count,
|
|
|
|
const char *format [,
|
|
|
|
argument] ...
|
|
|
|
);*/
|
2015-08-15 16:17:27 +00:00
|
|
|
static inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... )
|
2015-06-12 05:52:08 +00:00
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
va_start( va, format );
|
|
|
|
int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
|
|
|
|
va_end( va );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-15 16:17:27 +00:00
|
|
|
static inline int TIXML_VSNPRINTF( char* buffer, size_t size, const char* format, va_list va )
|
2015-06-12 05:52:08 +00:00
|
|
|
{
|
|
|
|
int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TIXML_VSCPRINTF _vscprintf
|
|
|
|
#define TIXML_SSCANF sscanf_s
|
|
|
|
#elif defined _MSC_VER
|
|
|
|
// Microsoft Visual Studio 2003 and earlier or WinCE
|
|
|
|
#define TIXML_SNPRINTF _snprintf
|
|
|
|
#define TIXML_VSNPRINTF _vsnprintf
|
|
|
|
#define TIXML_SSCANF sscanf
|
2015-06-19 23:52:40 +00:00
|
|
|
#if (_MSC_VER < 1400 ) && (!defined WINCE)
|
2015-06-12 05:52:08 +00:00
|
|
|
// Microsoft Visual Studio 2003 and not WinCE.
|
|
|
|
#define TIXML_VSCPRINTF _vscprintf // VS2003's C runtime has this, but VC6 C runtime or WinCE SDK doesn't have.
|
|
|
|
#else
|
|
|
|
// Microsoft Visual Studio 2003 and earlier or WinCE.
|
2015-08-15 16:17:27 +00:00
|
|
|
static inline int TIXML_VSCPRINTF( const char* format, va_list va )
|
2015-06-12 05:52:08 +00:00
|
|
|
{
|
|
|
|
int len = 512;
|
|
|
|
for (;;) {
|
|
|
|
len = len*2;
|
|
|
|
char* str = new char[len]();
|
|
|
|
const int required = _vsnprintf(str, len, format, va);
|
|
|
|
delete[] str;
|
|
|
|
if ( required != -1 ) {
|
2015-07-27 14:11:51 +00:00
|
|
|
TIXMLASSERT( required >= 0 );
|
2015-06-12 05:52:08 +00:00
|
|
|
len = required;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-27 14:11:51 +00:00
|
|
|
TIXMLASSERT( len >= 0 );
|
2015-06-12 05:52:08 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
// GCC version 3 and higher
|
|
|
|
//#warning( "Using sn* functions." )
|
|
|
|
#define TIXML_SNPRINTF snprintf
|
|
|
|
#define TIXML_VSNPRINTF vsnprintf
|
2015-08-15 16:17:27 +00:00
|
|
|
static inline int TIXML_VSCPRINTF( const char* format, va_list va )
|
2015-06-12 05:52:08 +00:00
|
|
|
{
|
|
|
|
int len = vsnprintf( 0, 0, format, va );
|
2015-07-27 14:11:51 +00:00
|
|
|
TIXMLASSERT( len >= 0 );
|
2015-06-12 05:52:08 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
#define TIXML_SSCANF sscanf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-01-21 01:59:50 +00:00
|
|
|
static const char LINE_FEED = (char)0x0a; // all line endings are normalized to LF
|
2012-01-15 02:08:12 +00:00
|
|
|
static const char LF = LINE_FEED;
|
|
|
|
static const char CARRIAGE_RETURN = (char)0x0d; // CR gets filtered out
|
|
|
|
static const char CR = CARRIAGE_RETURN;
|
2012-01-21 01:59:50 +00:00
|
|
|
static const char SINGLE_QUOTE = '\'';
|
|
|
|
static const char DOUBLE_QUOTE = '\"';
|
2012-01-15 02:08:12 +00:00
|
|
|
|
2012-02-24 06:27:28 +00:00
|
|
|
// Bunch of unicode info at:
|
|
|
|
// http://www.unicode.org/faq/utf_bom.html
|
|
|
|
// ef bb bf (Microsoft "lead bytes") - designates UTF-8
|
2012-02-21 17:08:12 +00:00
|
|
|
|
2012-02-24 06:27:28 +00:00
|
|
|
static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
|
|
|
|
static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
|
|
|
|
static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
|
2012-02-21 17:08:12 +00:00
|
|
|
|
2012-11-08 19:02:22 +00:00
|
|
|
namespace tinyxml2
|
|
|
|
{
|
|
|
|
|
2012-01-26 01:44:30 +00:00
|
|
|
struct Entity {
|
2012-10-11 23:56:51 +00:00
|
|
|
const char* pattern;
|
|
|
|
int length;
|
|
|
|
char value;
|
2012-01-26 01:44:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const int NUM_ENTITIES = 5;
|
2012-10-11 23:56:51 +00:00
|
|
|
static const Entity entities[NUM_ENTITIES] = {
|
|
|
|
{ "quot", 4, DOUBLE_QUOTE },
|
|
|
|
{ "amp", 3, '&' },
|
|
|
|
{ "apos", 4, SINGLE_QUOTE },
|
|
|
|
{ "lt", 2, '<' },
|
|
|
|
{ "gt", 2, '>' }
|
2012-01-26 01:44:30 +00:00
|
|
|
};
|
|
|
|
|
2012-01-15 02:08:12 +00:00
|
|
|
|
2012-02-15 17:09:25 +00:00
|
|
|
StrPair::~StrPair()
|
|
|
|
{
|
2012-10-11 23:56:51 +00:00
|
|
|
Reset();
|
2012-02-15 17:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-28 06:31:11 +00:00
|
|
|
void StrPair::TransferTo( StrPair* other )
|
2014-11-10 08:17:21 +00:00
|
|
|
{
|
2014-11-28 06:31:11 +00:00
|
|
|
if ( this == other ) {
|
2014-11-10 08:17:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// This in effect implements the assignment operator by "moving"
|
|
|
|
// ownership (as in auto_ptr).
|
|
|
|
|
2016-08-04 14:16:05 +00:00
|
|
|
TIXMLASSERT( other != 0 );
|
2014-11-28 06:31:11 +00:00
|
|
|
TIXMLASSERT( other->_flags == 0 );
|
|
|
|
TIXMLASSERT( other->_start == 0 );
|
|
|
|
TIXMLASSERT( other->_end == 0 );
|
2014-11-10 08:17:21 +00:00
|
|
|
|
2014-11-28 06:31:11 +00:00
|
|
|
other->Reset();
|
2014-11-10 08:17:21 +00:00
|
|
|
|
2014-11-28 06:31:11 +00:00
|
|
|
other->_flags = _flags;
|
|
|
|
other->_start = _start;
|
|
|
|
other->_end = _end;
|
2014-11-10 08:17:21 +00:00
|
|
|
|
|
|
|
_flags = 0;
|
|
|
|
_start = 0;
|
|
|
|
_end = 0;
|
|
|
|
}
|
|
|
|
|
2017-09-20 00:54:31 +00:00
|
|
|
|
2012-02-15 17:09:25 +00:00
|
|
|
void StrPair::Reset()
|
|
|
|
{
|
2012-10-12 17:06:59 +00:00
|
|
|
if ( _flags & NEEDS_DELETE ) {
|
|
|
|
delete [] _start;
|
2012-10-11 23:56:51 +00:00
|
|
|
}
|
2012-10-12 17:06:59 +00:00
|
|
|
_flags = 0;
|
|
|
|
_start = 0;
|
|
|
|
_end = 0;
|
2012-02-15 17:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StrPair::SetStr( const char* str, int flags )
|
|
|
|
{
|
2015-12-09 08:54:06 +00:00
|
|
|
TIXMLASSERT( str );
|
2012-10-11 23:56:51 +00:00
|
|
|
Reset();
|
|
|
|
size_t len = strlen( str );
|
2015-08-10 13:45:12 +00:00
|
|
|
TIXMLASSERT( _start == 0 );
|
2012-10-12 17:06:59 +00:00
|
|
|
_start = new char[ len+1 ];
|
|
|
|
memcpy( _start, str, len+1 );
|
|
|
|
_end = _start + len;
|
|
|
|
_flags = flags | NEEDS_DELETE;
|
2012-02-15 17:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-29 09:46:27 +00:00
|
|
|
char* StrPair::ParseText( char* p, const char* endTag, int strFlags, int* curLineNumPtr )
|
2012-02-15 17:09:25 +00:00
|
|
|
{
|
2016-08-30 12:51:55 +00:00
|
|
|
TIXMLASSERT( p );
|
2012-10-11 23:56:51 +00:00
|
|
|
TIXMLASSERT( endTag && *endTag );
|
2016-12-24 15:34:39 +00:00
|
|
|
TIXMLASSERT(curLineNumPtr);
|
2012-02-15 17:09:25 +00:00
|
|
|
|
2014-08-25 07:05:55 +00:00
|
|
|
char* start = p;
|
2012-10-11 23:56:51 +00:00
|
|
|
char endChar = *endTag;
|
|
|
|
size_t length = strlen( endTag );
|
2012-02-15 17:09:25 +00:00
|
|
|
|
2012-10-11 23:56:51 +00:00
|
|
|
// Inner loop of text parsing.
|
|
|
|
while ( *p ) {
|
|
|
|
if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
|
|
|
|
Set( start, p, strFlags );
|
|
|
|
return p + length;
|
2016-11-26 07:21:43 +00:00
|
|
|
} else if (*p == '\n') {
|
2016-11-29 09:46:27 +00:00
|
|
|
++(*curLineNumPtr);
|
2012-10-11 23:56:51 +00:00
|
|
|
}
|
|
|
|
++p;
|
2016-08-30 12:51:55 +00:00
|
|
|
TIXMLASSERT( p );
|
2012-10-11 23:56:51 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2012-02-15 17:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char* StrPair::ParseName( char* p )
|
|
|
|
{
|
2014-09-23 13:27:39 +00:00
|
|
|
if ( !p || !(*p) ) {
|
2012-10-11 23:56:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-12-24 09:01:42 +00:00
|
|
|
if ( !XMLUtil::IsNameStartChar( *p ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-15 17:09:25 +00:00
|
|
|
|
2014-09-23 13:27:39 +00:00
|
|
|
char* const start = p;
|
2014-12-24 09:01:42 +00:00
|
|
|
++p;
|
|
|
|
while ( *p && XMLUtil::IsNameChar( *p ) ) {
|
2012-10-11 23:56:51 +00:00
|
|
|
++p;
|
|
|
|
}
|
2012-02-15 17:09:25 +00:00
|
|
|
|
2014-12-24 09:01:42 +00:00
|
|
|
Set( start, p, 0 );
|
|
|
|
return p;
|
2012-02-15 17:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-21 05:00:38 +00:00
|
|
|
void StrPair::CollapseWhitespace()
|
|
|
|
{
|
2014-08-20 06:01:53 +00:00
|
|
|
// Adjusting _start would cause undefined behavior on delete[]
|
|
|
|
TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 );
|
2012-10-11 23:56:51 +00:00
|
|
|
// Trim leading space.
|
2016-12-24 15:34:39 +00:00
|
|
|
_start = XMLUtil::SkipWhiteSpace( _start, 0 );
|
2012-08-21 05:00:38 +00:00
|
|
|
|
2014-12-24 08:54:05 +00:00
|
|
|
if ( *_start ) {
|
2016-09-02 13:53:32 +00:00
|
|
|
const char* p = _start; // the read pointer
|
2012-10-12 17:06:59 +00:00
|
|
|
char* q = _start; // the write pointer
|
2012-08-21 05:00:38 +00:00
|
|
|
|
2012-10-11 23:56:51 +00:00
|
|
|
while( *p ) {
|
|
|
|
if ( XMLUtil::IsWhiteSpace( *p )) {
|
2016-12-24 15:34:39 +00:00
|
|
|
p = XMLUtil::SkipWhiteSpace( p, 0 );
|
2012-10-11 23:56:51 +00:00
|
|
|
if ( *p == 0 ) {
|
|
|
|
break; // don't write to q; this trims the trailing space.
|
|
|
|
}
|
|
|
|
*q = ' ';
|
|
|
|
++q;
|
|
|
|
}
|
|
|
|
*q = *p;
|
|
|
|
++q;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
*q = 0;
|
|
|
|
}
|
2012-08-21 05:00:38 +00:00
|
|
|
}
|
|
|
|
|
2012-02-21 17:08:12 +00:00
|
|
|
|
2012-01-21 01:59:50 +00:00
|
|
|
const char* StrPair::GetStr()
|
|
|
|
{
|
2015-01-01 14:47:40 +00:00
|
|
|
TIXMLASSERT( _start );
|
|
|
|
TIXMLASSERT( _end );
|
2012-10-12 17:06:59 +00:00
|
|
|
if ( _flags & NEEDS_FLUSH ) {
|
|
|
|
*_end = 0;
|
|
|
|
_flags ^= NEEDS_FLUSH;
|
2012-10-11 23:56:51 +00:00
|
|
|
|
2012-10-12 17:06:59 +00:00
|
|
|
if ( _flags ) {
|
2016-09-02 13:53:32 +00:00
|
|
|
const char* p = _start; // the read pointer
|
2012-10-12 17:06:59 +00:00
|
|
|
char* q = _start; // the write pointer
|
2012-10-11 23:56:51 +00:00
|
|
|
|
2012-10-12 17:06:59 +00:00
|
|
|
while( p < _end ) {
|
|
|
|
if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
|
2012-10-11 23:56:51 +00:00
|
|
|
// CR-LF pair becomes LF
|
|
|
|
// CR alone becomes LF
|
|
|
|
// LF-CR becomes LF
|
|
|
|
if ( *(p+1) == LF ) {
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++p;
|
|
|
|
}
|
2016-09-06 15:08:55 +00:00
|
|
|
*q = LF;
|
|
|
|
++q;
|
2012-10-11 23:56:51 +00:00
|
|
|
}
|
2012-10-12 17:06:59 +00:00
|
|
|
else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
|
2012-10-11 23:56:51 +00:00
|
|
|
if ( *(p+1) == CR ) {
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++p;
|
|
|
|
}
|
2016-09-06 15:08:55 +00:00
|
|
|
*q = LF;
|
|
|
|
++q;
|
2012-10-11 23:56:51 +00:00
|
|
|
}
|
2012-10-12 17:06:59 +00:00
|
|
|
else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
|
2012-10-11 23:56:51 +00:00
|
|
|
// Entities handled by tinyXML2:
|
|
|
|
// - special entities in the entity table [in/out]
|
|
|
|
// - numeric character reference [in]
|
|
|
|
// 中 or 中
|
|
|
|
|
|
|
|
if ( *(p+1) == '#' ) {
|
2014-08-21 08:33:19 +00:00
|
|
|
const int buflen = 10;
|
|
|
|
char buf[buflen] = { 0 };
|
|
|
|
int len = 0;
|
2015-03-14 10:25:03 +00:00
|
|
|
char* adjusted = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
|
|
|
|
if ( adjusted == 0 ) {
|
|
|
|
*q = *p;
|
|
|
|
++p;
|
|
|
|
++q;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
TIXMLASSERT( 0 <= len && len <= buflen );
|
|
|
|
TIXMLASSERT( q + len <= adjusted );
|
|
|
|
p = adjusted;
|
|
|
|
memcpy( q, buf, len );
|
|
|
|
q += len;
|
|
|
|
}
|
2012-10-11 23:56:51 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-05-20 07:29:24 +00:00
|
|
|
bool entityFound = false;
|
|
|
|
for( int i = 0; i < NUM_ENTITIES; ++i ) {
|
2014-10-01 06:30:16 +00:00
|
|
|
const Entity& entity = entities[i];
|
|
|
|
if ( strncmp( p + 1, entity.pattern, entity.length ) == 0
|
|
|
|
&& *( p + entity.length + 1 ) == ';' ) {
|
|
|
|
// Found an entity - convert.
|
|
|
|
*q = entity.value;
|
2012-10-11 23:56:51 +00:00
|
|
|
++q;
|
2014-10-01 06:30:16 +00:00
|
|
|
p += entity.length + 2;
|
2015-05-20 07:29:24 +00:00
|
|
|
entityFound = true;
|
2012-10-11 23:56:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-20 07:29:24 +00:00
|
|
|
if ( !entityFound ) {
|
2012-10-11 23:56:51 +00:00
|
|
|
// fixme: treat as error?
|
|
|
|
++p;
|
|
|
|
++q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*q = *p;
|
|
|
|
++p;
|
|
|
|
++q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q = 0;
|
|
|
|
}
|
|
|
|
// The loop below has plenty going on, and this
|
|
|
|
// is a less useful mode. Break it out.
|
2015-05-20 07:51:26 +00:00
|
|
|
if ( _flags & NEEDS_WHITESPACE_COLLAPSING ) {
|
2012-10-11 23:56:51 +00:00
|
|
|
CollapseWhitespace();
|
|
|
|
}
|
2012-10-12 17:06:59 +00:00
|
|
|
_flags = (_flags & NEEDS_DELETE);
|
2012-10-11 23:56:51 +00:00
|
|
|
}
|
2015-01-01 14:47:40 +00:00
|
|
|
TIXMLASSERT( _start );
|
2012-10-12 17:06:59 +00:00
|
|
|
return _start;
|
2012-01-21 01:59:50 +00:00
|
|
|
}
|
|
|
|
|
2012-01-31 16:24:24 +00:00
|
|
|
|
2012-01-21 01:59:50 +00:00
|
|
|
|
2012-02-21 17:08:12 +00:00
|
|
|
|
2012-02-10 02:16:58 +00:00
|
|
|
// --------- XMLUtil ----------- //
|
2012-02-06 16:41:24 +00:00
|
|
|
|
2016-12-27 06:47:25 +00:00
|
|
|
const char* XMLUtil::writeBoolTrue = "true";
|
|
|
|
const char* XMLUtil::writeBoolFalse = "false";
|
2016-12-27 00:45:30 +00:00
|
|
|
|
2016-12-29 19:19:17 +00:00
|
|
|
void XMLUtil::SetBoolSerialization(const char* writeTrue, const char* writeFalse)
|
2016-12-27 00:45:30 +00:00
|
|
|
{
|
2016-12-29 19:19:17 +00:00
|
|
|
static const char* defTrue = "true";
|
2016-12-27 00:45:30 +00:00
|
|
|
static const char* defFalse = "false";
|
2016-12-29 19:19:17 +00:00
|
|
|
|
|
|
|
writeBoolTrue = (writeTrue) ? writeTrue : defTrue;
|
|
|
|
writeBoolFalse = (writeFalse) ? writeFalse : defFalse;
|
2016-12-27 00:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-21 17:08:12 +00:00
|
|
|
const char* XMLUtil::ReadBOM( const char* p, bool* bom )
|
|
|
|
{
|
2014-12-24 08:54:05 +00:00
|
|
|
TIXMLASSERT( p );
|
|
|
|
TIXMLASSERT( bom );
|
2012-10-11 23:56:51 +00:00
|
|
|
*bom = false;
|
|
|
|
const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);
|
|
|
|
// Check for BOM:
|
|
|
|
if ( *(pu+0) == TIXML_UTF_LEAD_0
|
|
|
|
&& *(pu+1) == TIXML_UTF_LEAD_1
|
|
|
|
&& *(pu+2) == TIXML_UTF_LEAD_2 ) {
|
|
|
|
*bom = true;
|
|
|
|
p += 3;
|
|
|
|
}
|
2014-12-24 08:54:05 +00:00
|
|
|
TIXMLASSERT( p );
|
2012-10-11 23:56:51 +00:00
|
|
|
return p;
|
2012-02-21 17:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-24 06:27:28 +00:00
|
|
|
void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
|
|
|
|
{
|
2012-10-11 23:56:51 +00:00
|
|
|
const unsigned long BYTE_MASK = 0xBF;
|
|
|
|
const unsigned long BYTE_MARK = 0x80;
|
|
|
|
const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
|
|
|
|
|
|
|
|
if (input < 0x80) {
|
|
|
|
*length = 1;
|
|
|
|
}
|
|
|
|
else if ( input < 0x800 ) {
|
|
|
|
*length = 2;
|
|
|
|
}
|
|
|
|
else if ( input < 0x10000 ) {
|
|
|
|
*length = 3;
|
|
|
|
}
|
|
|
|
else if ( input < 0x200000 ) {
|
|
|
|
*length = 4;
|
|
|
|
}
|
|
|
|
else {
|
2015-03-16 08:08:23 +00:00
|
|
|
*length = 0; // This code won't convert this correctly anyway.
|
2012-10-11 23:56:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
output += *length;
|
|
|
|
|
2017-08-03 15:42:20 +00:00
|
|
|
// Scary scary fall throughs are annotated with carefully designed comments
|
|
|
|
// to suppress compiler warnings such as -Wimplicit-fallthrough in gcc
|
2012-10-11 23:56:51 +00:00
|
|
|
switch (*length) {
|
|
|
|
case 4:
|
|
|
|
--output;
|
|
|
|
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
|
|
|
input >>= 6;
|
2017-08-03 15:42:20 +00:00
|
|
|
//fall through
|
2012-10-11 23:56:51 +00:00
|
|
|
case 3:
|
|
|
|
--output;
|
|
|
|
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
|
|
|
input >>= 6;
|
2017-08-03 15:42:20 +00:00
|
|
|
//fall through
|
2012-10-11 23:56:51 +00:00
|
|
|
case 2:
|
|
|
|
--output;
|
|
|
|
*output = (char)((input | BYTE_MARK) & BYTE_MASK);
|
|
|
|
input >>= 6;
|
2017-08-03 15:42:20 +00:00
|
|
|
//fall through
|
2012-10-11 23:56:51 +00:00
|
|
|
case 1:
|
|
|
|
--output;
|
|
|
|
*output = (char)(input | FIRST_BYTE_MARK[*length]);
|
2013-01-14 19:03:14 +00:00
|
|
|
break;
|
2015-03-14 14:14:00 +00:00
|
|
|
default:
|
|
|
|
TIXMLASSERT( false );
|
2012-10-11 23:56:51 +00:00
|
|
|
}
|
2012-02-24 06:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
|
|
|
|
{
|
2012-10-11 23:56:51 +00:00
|
|
|
// Presume an entity, and pull it out.
|
|
|
|
*length = 0;
|
|
|
|
|
|
|
|
if ( *(p+1) == '#' && *(p+2) ) {
|
|
|
|
unsigned long ucs = 0;
|
2015-01-12 11:07:10 +00:00
|
|
|
TIXMLASSERT( sizeof( ucs ) >= 4 );
|
2012-10-11 23:56:51 +00:00
|
|
|
ptrdiff_t delta = 0;
|
|
|
|
unsigned mult = 1;
|
2015-01-12 22:05:12 +00:00
|
|
|
static const char SEMICOLON = ';';
|
2012-10-11 23:56:51 +00:00
|
|
|
|
|
|
|
if ( *(p+2) == 'x' ) {
|
|
|
|
// Hexadecimal.
|
2015-01-15 10:27:47 +00:00
|
|
|
const char* q = p+3;
|
|
|
|
if ( !(*q) ) {
|
2012-10-11 23:56:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-12 22:05:12 +00:00
|
|
|
q = strchr( q, SEMICOLON );
|
2012-10-11 23:56:51 +00:00
|
|
|
|
2015-01-12 07:07:54 +00:00
|
|
|
if ( !q ) {
|
2012-10-11 23:56:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-12 22:05:12 +00:00
|
|
|
TIXMLASSERT( *q == SEMICOLON );
|
2012-10-11 23:56:51 +00:00
|
|
|
|
|
|
|
delta = q-p;
|
|
|
|
--q;
|
|
|
|
|
|
|
|
while ( *q != 'x' ) {
|
2015-03-15 23:11:47 +00:00
|
|
|
unsigned int digit = 0;
|
|
|
|
|
2012-10-11 23:56:51 +00:00
|
|
|
if ( *q >= '0' && *q <= '9' ) {
|
2015-03-14 13:41:46 +00:00
|
|
|
digit = *q - '0';
|
2012-10-11 23:56:51 +00:00
|
|
|
}
|
|
|
|
else if ( *q >= 'a' && *q <= 'f' ) {
|
2015-03-14 13:41:46 +00:00
|
|
|
digit = *q - 'a' + 10;
|
2012-10-11 23:56:51 +00:00
|
|
|
}
|
|
|
|
else if ( *q >= 'A' && *q <= 'F' ) {
|
2015-03-14 13:41:46 +00:00
|
|
|
digit = *q - 'A' + 10;
|
2012-10-11 23:56:51 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-25 13:14:03 +00:00
|
|
|
TIXMLASSERT( digit < 16 );
|
2015-03-14 13:41:46 +00:00
|
|
|
TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
|
|
|
|
const unsigned int digitScaled = mult * digit;
|
|
|
|
TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );
|
|
|
|
ucs += digitScaled;
|
2015-01-12 11:07:10 +00:00
|
|
|
TIXMLASSERT( mult <= UINT_MAX / 16 );
|
2012-10-11 23:56:51 +00:00
|
|
|
mult *= 16;
|
|
|
|
--q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Decimal.
|
2015-01-15 10:27:47 +00:00
|
|
|
const char* q = p+2;
|
|
|
|
if ( !(*q) ) {
|
2012-10-11 23:56:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-12 22:05:12 +00:00
|
|
|
q = strchr( q, SEMICOLON );
|
2012-10-11 23:56:51 +00:00
|
|
|
|
2015-01-12 07:07:54 +00:00
|
|
|
if ( !q ) {
|
2012-10-11 23:56:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-12 22:05:12 +00:00
|
|
|
TIXMLASSERT( *q == SEMICOLON );
|
2012-10-11 23:56:51 +00:00
|
|
|
|
|
|
|
delta = q-p;
|
|
|
|
--q;
|
|
|
|
|
|
|
|
while ( *q != '#' ) {
|
|
|
|
if ( *q >= '0' && *q <= '9' ) {
|
2015-03-14 13:41:46 +00:00
|
|
|
const unsigned int digit = *q - '0';
|
2016-03-25 13:14:03 +00:00
|
|
|
TIXMLASSERT( digit < 10 );
|
2015-03-14 13:41:46 +00:00
|
|
|
TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
|
|
|
|
const unsigned int digitScaled = mult * digit;
|
|
|
|
TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );
|
|
|
|
ucs += digitScaled;
|
2012-10-11 23:56:51 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-01-12 11:07:10 +00:00
|
|
|
TIXMLASSERT( mult <= UINT_MAX / 10 );
|
2012-10-11 23:56:51 +00:00
|
|
|
mult *= 10;
|
|
|
|
--q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// convert the UCS to UTF-8
|
|
|
|
ConvertUTF32ToUTF8( ucs, value, length );
|
|
|
|
return p + delta + 1;
|
|
|
|
}
|
|
|
|
return p+1;
|
2012-02-24 06:27:28 +00:00
|
|
|
}
|
2012-02-21 17:08:12 +00:00
|
|
|
|
|
|
|
|
2012-09-16 18:32:34 +00:00
|
|
|
void XMLUtil::ToStr( int v, char* buffer, int bufferSize )
|
2012-07-16 00:27:22 +00:00
|
|
|
{
|
2012-10-11 23:56:51 +00:00
|
|
|
TIXML_SNPRINTF( buffer, bufferSize, "%d", v );
|
2012-07-16 00:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize )
|
|
|
|
{
|
2012-10-11 23:56:51 +00:00
|
|
|
TIXML_SNPRINTF( buffer, bufferSize, "%u", v );
|
2012-07-16 00:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void XMLUtil::ToStr( bool v, char* buffer, int bufferSize )
|
|
|
|
{
|
2016-12-27 00:45:30 +00:00
|
|
|
TIXML_SNPRINTF( buffer, bufferSize, "%s", v ? writeBoolTrue : writeBoolFalse);
|
2012-07-16 00:27:22 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 20:30:03 +00:00
|
|
|
/*
|
|
|
|
ToStr() of a number is a very tricky topic.
|
|
|
|
https://github.com/leethomason/tinyxml2/issues/106
|
|
|
|
*/
|
2012-07-16 00:27:22 +00:00
|
|
|
void XMLUtil::ToStr( float v, char* buffer, int bufferSize )
|
|
|
|
{
|
2014-01-14 20:30:03 +00:00
|
|
|
TIXML_SNPRINTF( buffer, bufferSize, "%.8g", v );
|
2012-07-16 00:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void XMLUtil::ToStr( double v, char* buffer, int bufferSize )
|
|
|
|
{
|
2014-01-14 20:30:03 +00:00
|
|
|
TIXML_SNPRINTF( buffer, bufferSize, "%.17g", v );
|
2012-07-16 00:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-05 03:18:49 +00:00
|
|
|
void XMLUtil::ToStr(int64_t v, char* buffer, int bufferSize)
|
|
|
|
{
|
2016-07-18 05:49:40 +00:00
|
|
|
// horrible syntax trick to make the compiler happy about %lld
|
|
|
|
TIXML_SNPRINTF(buffer, bufferSize, "%lld", (long long)v);
|
2016-06-05 03:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-16 00:27:22 +00:00
|
|
|
bool XMLUtil::ToInt( const char* str, int* value )
|
|
|
|
{
|
2012-10-11 23:56:51 +00:00
|
|
|
if ( TIXML_SSCANF( str, "%d", value ) == 1 ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-07-16 00:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool XMLUtil::ToUnsigned( const char* str, unsigned *value )
|
|
|
|
{
|
2012-10-11 23:56:51 +00:00
|
|
|
if ( TIXML_SSCANF( str, "%u", value ) == 1 ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-07-16 00:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool XMLUtil::ToBool( const char* str, bool* value )
|
|
|
|
{
|
2012-10-11 23:56:51 +00:00
|
|
|
int ival = 0;
|
|
|
|
if ( ToInt( str, &ival )) {
|
|
|
|
*value = (ival==0) ? false : true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( StringEqual( str, "true" ) ) {
|
|
|
|
*value = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if ( StringEqual( str, "false" ) ) {
|
|
|
|
*value = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-07-16 00:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool XMLUtil::ToFloat( const char* str, float* value )
|
|
|
|
{
|
2012-10-11 23:56:51 +00:00
|
|
|
if ( TIXML_SSCANF( str, "%f", value ) == 1 ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-07-16 00:27:22 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 03:18:49 +00:00
|
|
|
|
2012-07-16 00:27:22 +00:00
|
|
|
bool XMLUtil::ToDouble( const char* str, double* value )
|
|
|
|
{
|
2012-10-11 23:56:51 +00:00
|
|
|
if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-07-16 00:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-05 03:18:49 +00:00
|
|
|
bool XMLUtil::ToInt64(const char* str, int64_t* value)
|
|
|
|
{
|
2016-07-18 05:49:40 +00:00
|
|
|
long long v = 0; // horrible syntax trick to make the compiler happy about %lld
|
|
|
|
if (TIXML_SSCANF(str, "%lld", &v) == 1) {
|
|
|
|
*value = (int64_t)v;
|
2016-06-05 03:18:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-16 18:32:34 +00:00
|
|
|
char* XMLDocument::Identify( char* p, XMLNode** node )
|
2012-01-19 01:43:40 +00:00
|
|
|
{
|
2015-03-03 13:02:13 +00:00
|
|
|
TIXMLASSERT( node );
|
|
|
|
TIXMLASSERT( p );
|
2014-09-23 13:27:39 +00:00
|
|
|
char* const start = p;
|
2016-11-26 07:21:43 +00:00
|
|
|
int const startLine = _parseCurLineNum;
|
2016-11-29 09:46:27 +00:00
|
|
|
p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum );
|
2014-12-24 08:54:05 +00:00
|
|
|
if( !*p ) {
|
2015-03-05 14:53:34 +00:00
|
|
|
*node = 0;
|
|
|
|
TIXMLASSERT( p );
|
2012-10-11 23:56:51 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2015-05-26 08:38:30 +00:00
|
|
|
// These strings define the matching patterns:
|
2012-10-11 23:56:51 +00:00
|
|
|
static const char* xmlHeader = { "<?" };
|
|
|
|
static const char* commentHeader = { "<!--" };
|
|
|
|
static const char* cdataHeader = { "<![CDATA[" };
|
2015-03-30 06:54:36 +00:00
|
|
|
static const char* dtdHeader = { "<!" };
|
2012-10-11 23:56:51 +00:00
|
|
|
static const char* elementHeader = { "<" }; // and a header for everything else; check last.
|
|
|
|
|
|
|
|
static const int xmlHeaderLen = 2;
|
|
|
|
static const int commentHeaderLen = 4;
|
|
|
|
static const int cdataHeaderLen = 9;
|
2015-03-30 06:54:36 +00:00
|
|
|
static const int dtdHeaderLen = 2;
|
2012-10-11 23:56:51 +00:00
|
|
|
static const int elementHeaderLen = 1;
|
2012-01-19 01:43:40 +00:00
|
|
|
|
2012-10-11 23:56:51 +00:00
|
|
|
TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
|
|
|
|
TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
|
2014-09-23 13:27:39 +00:00
|
|
|
XMLNode* returnNode = 0;
|
2012-10-11 23:56:51 +00:00
|
|
|
if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
|
2017-02-27 12:53:40 +00:00
|
|
|
returnNode = CreateUnlinkedNode<XMLDeclaration>( _commentPool );
|
2016-11-26 07:21:43 +00:00
|
|
|
returnNode->_parseLineNum = _parseCurLineNum;
|
2012-10-11 23:56:51 +00:00
|
|
|
p += xmlHeaderLen;
|
|
|
|
}
|
|
|
|
else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
|
2017-02-27 12:53:40 +00:00
|
|
|
returnNode = CreateUnlinkedNode<XMLComment>( _commentPool );
|
2016-11-26 07:21:43 +00:00
|
|
|
returnNode->_parseLineNum = _parseCurLineNum;
|
2012-10-11 23:56:51 +00:00
|
|
|
p += commentHeaderLen;
|
|
|
|
}
|
|
|
|
else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
|
2017-02-27 12:53:40 +00:00
|
|
|
XMLText* text = CreateUnlinkedNode<XMLText>( _textPool );
|
2012-10-11 23:56:51 +00:00
|
|
|
returnNode = text;
|
2016-11-26 07:21:43 +00:00
|
|
|
returnNode->_parseLineNum = _parseCurLineNum;
|
2012-10-11 23:56:51 +00:00
|
|
|
p += cdataHeaderLen;
|
|
|
|
text->SetCData( true );
|
|
|
|
}
|
|
|
|
else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
|
2017-02-27 12:53:40 +00:00
|
|
|
returnNode = CreateUnlinkedNode<XMLUnknown>( _commentPool );
|
2016-11-26 07:21:43 +00:00
|
|
|
returnNode->_parseLineNum = _parseCurLineNum;
|
2012-10-11 23:56:51 +00:00
|
|
|
p += dtdHeaderLen;
|
|
|
|
}
|
|
|
|
else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
|
2017-02-27 12:53:40 +00:00
|
|
|
returnNode = CreateUnlinkedNode<XMLElement>( _elementPool );
|
2016-11-26 07:21:43 +00:00
|
|
|
returnNode->_parseLineNum = _parseCurLineNum;
|
2012-10-11 23:56:51 +00:00
|
|
|
p += elementHeaderLen;
|
|
|
|
}
|
|
|
|
else {
|
|