From e2bcb32494b9754b2e39098dbd120633d9319dbb Mon Sep 17 00:00:00 2001 From: "Lee Thomason (grinliz)" Date: Mon, 17 Sep 2012 17:58:25 -0700 Subject: [PATCH] accept a nBytes argument for Parse() --- tinyxml2.cpp | 14 ++++++++------ tinyxml2.h | 7 ++++++- xmltest.cpp | 8 ++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index ab1fbb0..3415b8a 100644 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1622,7 +1622,7 @@ int XMLDocument::SaveFile( FILE* fp, bool compact ) } -int XMLDocument::Parse( const char* p ) +int XMLDocument::Parse( const char* p, size_t len ) { DeleteChildren(); InitDocument(); @@ -1631,6 +1631,13 @@ int XMLDocument::Parse( const char* p ) SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 ); return errorID; } + if ( len == (size_t)(-1) ) { + len = strlen( p ); + } + charBuffer = new char[ len+1 ]; + memcpy( charBuffer, p, len ); + charBuffer[len] = 0; + p = XMLUtil::SkipWhiteSpace( p ); p = XMLUtil::ReadBOM( p, &writeBOM ); if ( !p || !*p ) { @@ -1638,11 +1645,6 @@ int XMLDocument::Parse( const char* p ) return errorID; } - size_t len = strlen( p ); - charBuffer = new char[ len+1 ]; - memcpy( charBuffer, p, len+1 ); - - ParseDeep( charBuffer, 0 ); return errorID; } diff --git a/tinyxml2.h b/tinyxml2.h index a573f94..b64f289 100644 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1072,8 +1072,13 @@ public: Parse an XML file from a character string. Returns XML_NO_ERROR (0) on success, or an errorID. + + You may optionally pass in the 'nBytes', which is + the number of bytes which will be parsed. If not + specified, TinyXML will assume 'xml' points to a + null terminated string. */ - int Parse( const char* xml ); + int Parse( const char* xml, size_t nBytes=(size_t)(-1) ); /** Load an XML file from disk. diff --git a/xmltest.cpp b/xmltest.cpp index 55300b8..7df8b47 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -947,6 +947,14 @@ int main( int /*argc*/, const char ** /*argv*/ ) XMLTest( "Non-alpha element lead letter parses.", doc.Error(), false ); } + { + const char* xml = "WOA THIS ISN'T GOING TO PARSE"; + XMLDocument doc; + doc.Parse( xml, 10 ); + //doc.Print(); + XMLTest( "Set length of incoming data", doc.Error(), false ); + } + // ----------- Whitespace ------------ { const char* xml = ""