From ce667c923324d277e10da40455ade87f199a7292 Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Mon, 26 Dec 2016 16:45:30 -0800 Subject: [PATCH 01/19] ability to set bool write values --- tinyxml2.cpp | 21 ++++++++++++++++++++- tinyxml2.h | 15 ++++++++++++++- xmltest.cpp | 12 ++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 018f9a9..7f5641e 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -368,6 +368,25 @@ const char* StrPair::GetStr() // --------- XMLUtil ----------- // +char* XMLUtil::writeBoolTrue = "true"; +char* XMLUtil::writeBoolFalse = "false"; + +void XMLUtil::SetBool(const char* writeTrue, const char* writeFalse) +{ + static const char* defTrue = "true"; + static const char* defFalse = "false"; + if (writeTrue) + writeBoolTrue = (char*) writeTrue; + else + writeBoolTrue = (char*) defTrue; + + if (writeFalse) + writeBoolFalse = (char*) writeFalse; + else + writeBoolFalse = (char*) defFalse; +} + + const char* XMLUtil::ReadBOM( const char* p, bool* bom ) { TIXMLASSERT( p ); @@ -545,7 +564,7 @@ void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize ) void XMLUtil::ToStr( bool v, char* buffer, int bufferSize ) { - TIXML_SNPRINTF( buffer, bufferSize, "%s", v ? "true" : "false" ); + TIXML_SNPRINTF( buffer, bufferSize, "%s", v ? writeBoolTrue : writeBoolFalse); } /* diff --git a/tinyxml2.h b/tinyxml2.h index de589bd..53f411a 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -527,7 +527,7 @@ enum XMLError { /* Utility functionality. */ -class XMLUtil +class TINYXML2_LIB XMLUtil { public: static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) { @@ -605,6 +605,19 @@ public: static bool ToFloat( const char* str, float* value ); static bool ToDouble( const char* str, double* value ); static bool ToInt64(const char* str, int64_t* value); + + // Default to "true" and "false". If you + // need different values, can assign them. (For instance + // if you need "true" to be "1".) + // Be careful: static, global, & not thread safe. + // Be sure to set static const memory as parameters. + // Shouldn't be set unless you have a special reason + // such as back-compatibility or testing. + static void SetBool(const char* writeTrue, const char* writeFalse); + +private: + static char* writeBoolTrue; + static char* writeBoolFalse; }; diff --git a/xmltest.cpp b/xmltest.cpp index b6bb76f..27c9665 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -752,6 +752,18 @@ int main( int argc, const char ** argv ) XMLTest("Attribute: bool", true, v, true); XMLTest("Attribute: bool", true, element->BoolAttribute("attrib"), true); } + { + element->SetAttribute("attrib", true); + const char* result = element->Attribute("attrib"); + XMLTest("Bool true is 'true'", "true", result); + + XMLUtil::SetBool("1", "0"); + element->SetAttribute("attrib", true); + result = element->Attribute("attrib"); + XMLTest("Bool true is '1'", "1", result); + + XMLUtil::SetBool(0, 0); + } { element->SetAttribute("attrib", 100.0); double v = 0; From f458d265c1697b9c473d97ebf68f00b1fd4d98cc Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Mon, 26 Dec 2016 22:47:25 -0800 Subject: [PATCH 02/19] fix const. hopefully. --- tinyxml2.cpp | 12 ++++++------ tinyxml2.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 7f5641e..40f6d82 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -368,22 +368,22 @@ const char* StrPair::GetStr() // --------- XMLUtil ----------- // -char* XMLUtil::writeBoolTrue = "true"; -char* XMLUtil::writeBoolFalse = "false"; +const char* XMLUtil::writeBoolTrue = "true"; +const char* XMLUtil::writeBoolFalse = "false"; void XMLUtil::SetBool(const char* writeTrue, const char* writeFalse) { static const char* defTrue = "true"; static const char* defFalse = "false"; if (writeTrue) - writeBoolTrue = (char*) writeTrue; + writeBoolTrue = writeTrue; else - writeBoolTrue = (char*) defTrue; + writeBoolTrue = defTrue; if (writeFalse) - writeBoolFalse = (char*) writeFalse; + writeBoolFalse = writeFalse; else - writeBoolFalse = (char*) defFalse; + writeBoolFalse = defFalse; } diff --git a/tinyxml2.h b/tinyxml2.h index 53f411a..8030e7d 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -616,8 +616,8 @@ public: static void SetBool(const char* writeTrue, const char* writeFalse); private: - static char* writeBoolTrue; - static char* writeBoolFalse; + static const char* writeBoolTrue; + static const char* writeBoolFalse; }; From c5c99c2ba0ce403154467049d7914364b797f230 Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Thu, 29 Dec 2016 11:19:17 -0800 Subject: [PATCH 03/19] tweaks to bool serialization --- tinyxml2.cpp | 14 ++++---------- tinyxml2.h | 10 ++++------ xmltest.cpp | 4 ++-- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 40f6d82..1f7a918 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -371,19 +371,13 @@ const char* StrPair::GetStr() const char* XMLUtil::writeBoolTrue = "true"; const char* XMLUtil::writeBoolFalse = "false"; -void XMLUtil::SetBool(const char* writeTrue, const char* writeFalse) +void XMLUtil::SetBoolSerialization(const char* writeTrue, const char* writeFalse) { - static const char* defTrue = "true"; + static const char* defTrue = "true"; static const char* defFalse = "false"; - if (writeTrue) - writeBoolTrue = writeTrue; - else - writeBoolTrue = defTrue; - if (writeFalse) - writeBoolFalse = writeFalse; - else - writeBoolFalse = defFalse; + writeBoolTrue = (writeTrue) ? writeTrue : defTrue; + writeBoolFalse = (writeFalse) ? writeFalse : defFalse; } diff --git a/tinyxml2.h b/tinyxml2.h index 8030e7d..00f6883 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -606,14 +606,12 @@ public: static bool ToDouble( const char* str, double* value ); static bool ToInt64(const char* str, int64_t* value); - // Default to "true" and "false". If you - // need different values, can assign them. (For instance - // if you need "true" to be "1".) + // Changes what is serialized for a boolean value. + // Default to "true" and "false". Shouldn't be changed + // unless you have a special testing or compatibility need. // Be careful: static, global, & not thread safe. // Be sure to set static const memory as parameters. - // Shouldn't be set unless you have a special reason - // such as back-compatibility or testing. - static void SetBool(const char* writeTrue, const char* writeFalse); + static void SetBoolSerialization(const char* writeTrue, const char* writeFalse); private: static const char* writeBoolTrue; diff --git a/xmltest.cpp b/xmltest.cpp index 27c9665..7c11389 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -757,12 +757,12 @@ int main( int argc, const char ** argv ) const char* result = element->Attribute("attrib"); XMLTest("Bool true is 'true'", "true", result); - XMLUtil::SetBool("1", "0"); + XMLUtil::SetBoolSerialization("1", "0"); element->SetAttribute("attrib", true); result = element->Attribute("attrib"); XMLTest("Bool true is '1'", "1", result); - XMLUtil::SetBool(0, 0); + XMLUtil::SetBoolSerialization(0, 0); } { element->SetAttribute("attrib", 100.0); From d120d64b867d65d4c5674d016b9b5acd600bf3b3 Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Fri, 27 Jan 2017 18:35:02 +0300 Subject: [PATCH 04/19] Initialize member variable --- tinyxml2.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tinyxml2.h b/tinyxml2.h index de589bd..034f002 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1177,7 +1177,7 @@ public: private: enum { BUF_SIZE = 200 }; - XMLAttribute() : _next( 0 ), _memPool( 0 ) {} + XMLAttribute() : _parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {} virtual ~XMLAttribute() {} XMLAttribute( const XMLAttribute& ); // not supported From 4a07484e47075da5303ee7bf60674045143d6251 Mon Sep 17 00:00:00 2001 From: Winestone Date: Sat, 11 Feb 2017 23:04:14 +1100 Subject: [PATCH 05/19] Fix typo in CMakeLists.txt --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e9b44d5..572597c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,7 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG") # Add targets # By Default shared libray is being built # To build static libs also - Do cmake . -DBUILD_STATIC_LIBS:BOOL=ON -# User can choose not to build shared library by using cmake -BUILD_SHARED_LIBS:BOOL:OFF +# User can choose not to build shared library by using cmake -DBUILD_SHARED_LIBS:BOOL=OFF # To build only static libs use cmake . -DBUILD_SHARED_LIBS:BOOL=OFF -DBUILD_STATIC_LIBS:BOOL=ON option(BUILD_SHARED_LIBS "build as shared library" ON) From 5277134efa63a33ead0ddbb1ae55badb19909573 Mon Sep 17 00:00:00 2001 From: Jimmy Nguyen Date: Mon, 13 Feb 2017 23:24:38 -0800 Subject: [PATCH 06/19] Removed empty install() command --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e9b44d5..96e892b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,7 +116,6 @@ else(BUILD_STATIC_LIBS) add_dependencies(xmltest ${TARGET_DATA_COPY}) target_link_libraries(xmltest tinyxml2_static) endif() -install(TARGETS DESTINATION ${CMAKE_INSTALL_BINDIR}) install(FILES tinyxml2.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) From 969b8c22343a19c0474395a0af701c8513413ed0 Mon Sep 17 00:00:00 2001 From: Jimmy Nguyen Date: Mon, 13 Feb 2017 23:40:16 -0800 Subject: [PATCH 07/19] Replaced DATA_COPY target with post build command to copy the resources directory --- CMakeLists.txt | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 96e892b..0a21405 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,25 +29,6 @@ set(GENERIC_LIB_SOVERSION "4") include_directories("${CMAKE_CURRENT_SOURCE_DIR}/.") -################################ -# Add custom target to copy all data - -set(TARGET_DATA_COPY DATA_COPY) -set(DATA_COPY_FILES) -if(NOT ${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR}) - foreach(data dream.xml empty.xml utf8test.xml utf8testverify.xml) - set(DATA_COPY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/resources/${data}) - set(DATA_COPY_DEST ${CMAKE_CURRENT_BINARY_DIR}/resources/${data}) - add_custom_command( - OUTPUT ${DATA_COPY_DEST} - COMMAND ${CMAKE_COMMAND} - ARGS -E copy ${DATA_COPY_SRC} ${DATA_COPY_DEST} - DEPENDS ${DATA_COPY_SRC}) - list(APPEND DATA_COPY_FILES ${DATA_COPY_DEST}) - endforeach(data) -endif(NOT ${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR}) -add_custom_target(${TARGET_DATA_COPY} DEPENDS ${DATA_COPY_FILES}) - ################################ # Add definitions @@ -109,14 +90,17 @@ endif() add_executable(xmltest xmltest.cpp) if(BUILD_SHARED_LIBS) add_dependencies(xmltest tinyxml2) - add_dependencies(xmltest ${TARGET_DATA_COPY}) target_link_libraries(xmltest tinyxml2) else(BUILD_STATIC_LIBS) add_dependencies(xmltest tinyxml2_static) - add_dependencies(xmltest ${TARGET_DATA_COPY}) target_link_libraries(xmltest tinyxml2_static) endif() +# Copy test resources +add_custom_command(TARGET xmltest POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/resources ${CMAKE_BINARY_DIR}/resources +) + install(FILES tinyxml2.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) foreach(p LIB INCLUDE) From 47c7d70064327bb419c37adf2a6060b4992376fa Mon Sep 17 00:00:00 2001 From: Jimmy Nguyen Date: Tue, 21 Feb 2017 11:27:38 -0800 Subject: [PATCH 08/19] Added BUILD_TESTS option to enable/disable building of xmltest --- CMakeLists.txt | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e9b44d5..41e31f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,9 +63,12 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG") # To build static libs also - Do cmake . -DBUILD_STATIC_LIBS:BOOL=ON # User can choose not to build shared library by using cmake -BUILD_SHARED_LIBS:BOOL:OFF # To build only static libs use cmake . -DBUILD_SHARED_LIBS:BOOL=OFF -DBUILD_STATIC_LIBS:BOOL=ON +# To build the tests, use cmake . -DBUILD_TESTS:BOOL=ON +# To disable the building of the tests, use cmake . -DBUILD_TESTS:BOOL=OFF option(BUILD_SHARED_LIBS "build as shared library" ON) option(BUILD_STATIC_LIBS "build as static library" OFF) +option(BUILD_TESTS "build xmltest" ON) set(CMAKE_CXX_VISIBILITY_PRESET hidden) set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) @@ -106,16 +109,21 @@ install(TARGETS tinyxml2_static ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) endif() -add_executable(xmltest xmltest.cpp) -if(BUILD_SHARED_LIBS) - add_dependencies(xmltest tinyxml2) - add_dependencies(xmltest ${TARGET_DATA_COPY}) - target_link_libraries(xmltest tinyxml2) -else(BUILD_STATIC_LIBS) - add_dependencies(xmltest tinyxml2_static) - add_dependencies(xmltest ${TARGET_DATA_COPY}) - target_link_libraries(xmltest tinyxml2_static) +if(BUILD_TESTS) + add_executable(xmltest xmltest.cpp) + if(BUILD_SHARED_LIBS) + add_dependencies(xmltest tinyxml2) + add_dependencies(xmltest ${TARGET_DATA_COPY}) + target_link_libraries(xmltest tinyxml2) + else(BUILD_STATIC_LIBS) + add_dependencies(xmltest tinyxml2_static) + add_dependencies(xmltest ${TARGET_DATA_COPY}) + target_link_libraries(xmltest tinyxml2_static) + endif() + + #add_test(xmltest ${SAMPLE_NAME} COMMAND $) endif() + install(TARGETS DESTINATION ${CMAKE_INSTALL_BINDIR}) install(FILES tinyxml2.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) @@ -130,8 +138,6 @@ endforeach() configure_file(tinyxml2.pc.in tinyxml2.pc @ONLY) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/tinyxml2.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) -#add_test(xmltest ${SAMPLE_NAME} COMMAND $) - # uninstall target configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in" From 6bf64fb149dae91946971ed39278bb315b975bef Mon Sep 17 00:00:00 2001 From: Jimmy Nguyen Date: Tue, 21 Feb 2017 12:00:38 -0800 Subject: [PATCH 09/19] Use CMake to create resources/out directory --- CMakeLists.txt | 4 +++- xmltest.cpp | 17 ----------------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a21405..9a4c0cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,9 +96,11 @@ else(BUILD_STATIC_LIBS) target_link_libraries(xmltest tinyxml2_static) endif() -# Copy test resources +# Copy test resources and create test output directory add_custom_command(TARGET xmltest POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/resources ${CMAKE_BINARY_DIR}/resources + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/resources/out + COMMENT "Configuring xmltest resources directory: ${CMAKE_BINARY_DIR}/resources" ) install(FILES tinyxml2.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) diff --git a/xmltest.cpp b/xmltest.cpp index b6bb76f..8221e68 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -10,16 +10,10 @@ #include #if defined( _MSC_VER ) - #include // _mkdir #include #define WIN32_LEAN_AND_MEAN #include _CrtMemState startMemState; - _CrtMemState endMemState; -#elif defined(MINGW32) || defined(__MINGW32__) - #include // mkdir -#else - #include // mkdir #endif using namespace tinyxml2; @@ -299,17 +293,6 @@ int main( int argc, const char ** argv ) _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF); #endif - #if defined(_MSC_VER) || defined(MINGW32) || defined(__MINGW32__) - #if defined __MINGW64_VERSION_MAJOR && defined __MINGW64_VERSION_MINOR - //MINGW64: both 32 and 64-bit - mkdir( "resources/out/" ); - #else - _mkdir( "resources/out/" ); - #endif - #else - mkdir( "resources/out/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - #endif - { TIXMLASSERT( true ); } From 7f2ce0dc0e1968a1d5e9ffcc905ea85e3015c66f Mon Sep 17 00:00:00 2001 From: Jimmy Nguyen Date: Tue, 21 Feb 2017 12:27:59 -0800 Subject: [PATCH 10/19] Updated Windows build script to change directory instead of copying files to run xmltest --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index aac8867..0c60b67 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -3,5 +3,5 @@ before_build: build_script: - msbuild tinyxml2.sln /m /p:Configuration=Release /t:ALL_BUILD - - copy Release\xmltest.exe .\ && copy Release\tinyxml2.dll .\ + - cd Release - xmltest.exe From 1e0b4e6b8aceaabb676adb8d1bc531bb2ff3129f Mon Sep 17 00:00:00 2001 From: Jimmy Nguyen Date: Tue, 21 Feb 2017 12:31:23 -0800 Subject: [PATCH 11/19] Use generator expression to specify target output directory for resources directory --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a4c0cc..dd09fe5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,8 +98,8 @@ endif() # Copy test resources and create test output directory add_custom_command(TARGET xmltest POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/resources ${CMAKE_BINARY_DIR}/resources - COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/resources/out + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/resources $/resources + COMMAND ${CMAKE_COMMAND} -E make_directory $/resources/out COMMENT "Configuring xmltest resources directory: ${CMAKE_BINARY_DIR}/resources" ) From 2aebfb71235b322e6841f805ad61400d4c2e460e Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Mon, 27 Feb 2017 15:53:40 +0300 Subject: [PATCH 12/19] Extract and reuse nodes creation code --- tinyxml2.cpp | 44 +++++++++++--------------------------------- tinyxml2.h | 13 +++++++++++++ 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index e9b275b..e54940f 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -665,46 +665,34 @@ char* XMLDocument::Identify( char* p, XMLNode** node ) TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool XMLNode* returnNode = 0; if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) { - TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() ); - returnNode = new (_commentPool.Alloc()) XMLDeclaration( this ); + returnNode = CreateUnlinkedNode( _commentPool ); returnNode->_parseLineNum = _parseCurLineNum; - returnNode->_memPool = &_commentPool; p += xmlHeaderLen; } else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) { - TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() ); - returnNode = new (_commentPool.Alloc()) XMLComment( this ); + returnNode = CreateUnlinkedNode( _commentPool ); returnNode->_parseLineNum = _parseCurLineNum; - returnNode->_memPool = &_commentPool; p += commentHeaderLen; } else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) { - TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() ); - XMLText* text = new (_textPool.Alloc()) XMLText( this ); + XMLText* text = CreateUnlinkedNode( _textPool ); returnNode = text; returnNode->_parseLineNum = _parseCurLineNum; - returnNode->_memPool = &_textPool; p += cdataHeaderLen; text->SetCData( true ); } else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) { - TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() ); - returnNode = new (_commentPool.Alloc()) XMLUnknown( this ); + returnNode = CreateUnlinkedNode( _commentPool ); returnNode->_parseLineNum = _parseCurLineNum; - returnNode->_memPool = &_commentPool; p += dtdHeaderLen; } else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) { - TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() ); - returnNode = new (_elementPool.Alloc()) XMLElement( this ); + returnNode = CreateUnlinkedNode( _elementPool ); returnNode->_parseLineNum = _parseCurLineNum; - returnNode->_memPool = &_elementPool; p += elementHeaderLen; } else { - TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() ); - returnNode = new (_textPool.Alloc()) XMLText( this ); - returnNode->_memPool = &_textPool; + returnNode = CreateUnlinkedNode( _textPool ); returnNode->_parseLineNum = _parseCurLineNum; // Report line of first non-whitespace character p = start; // Back it up, all the text counts. _parseCurLineNum = startLine; @@ -2008,9 +1996,7 @@ void XMLDocument::Clear() XMLElement* XMLDocument::NewElement( const char* name ) { - TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() ); - XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this ); - ele->_memPool = &_elementPool; + XMLElement* ele = CreateUnlinkedNode( _elementPool ); ele->SetName( name ); return ele; } @@ -2018,9 +2004,7 @@ XMLElement* XMLDocument::NewElement( const char* name ) XMLComment* XMLDocument::NewComment( const char* str ) { - TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() ); - XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this ); - comment->_memPool = &_commentPool; + XMLComment* comment = CreateUnlinkedNode( _commentPool ); comment->SetValue( str ); return comment; } @@ -2028,9 +2012,7 @@ XMLComment* XMLDocument::NewComment( const char* str ) XMLText* XMLDocument::NewText( const char* str ) { - TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() ); - XMLText* text = new (_textPool.Alloc()) XMLText( this ); - text->_memPool = &_textPool; + XMLText* text = CreateUnlinkedNode( _textPool ); text->SetValue( str ); return text; } @@ -2038,9 +2020,7 @@ XMLText* XMLDocument::NewText( const char* str ) XMLDeclaration* XMLDocument::NewDeclaration( const char* str ) { - TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() ); - XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this ); - dec->_memPool = &_commentPool; + XMLDeclaration* dec = CreateUnlinkedNode( _commentPool ); dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" ); return dec; } @@ -2048,9 +2028,7 @@ XMLDeclaration* XMLDocument::NewDeclaration( const char* str ) XMLUnknown* XMLDocument::NewUnknown( const char* str ) { - TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() ); - XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this ); - unk->_memPool = &_commentPool; + XMLUnknown* unk = CreateUnlinkedNode( _commentPool ); unk->SetValue( str ); return unk; } diff --git a/tinyxml2.h b/tinyxml2.h index 034f002..8f2f487 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1819,8 +1819,21 @@ private: static const char* _errorNames[XML_ERROR_COUNT]; void Parse(); + + template + NodeType* CreateUnlinkedNode( MemPoolT& pool ); }; +template +inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT& pool ) +{ + TIXMLASSERT( sizeof( NodeType ) == PoolElementSize ); + TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() ); + NodeType* returnNode = new (pool.Alloc()) NodeType( this ); + TIXMLASSERT( returnNode ); + returnNode->_memPool = &pool; + return returnNode; +} /** A XMLHandle is a class that wraps a node pointer with null checks; this is From ae8a82a7341200b51ecd19485ec276f2b9a5e47d Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Fri, 3 Mar 2017 15:40:32 +0300 Subject: [PATCH 13/19] Clearer variable name --- tinyxml2.cpp | 4 ++-- tinyxml2.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index d3adddc..adf7b5f 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1956,12 +1956,12 @@ const char* XMLDocument::_errorNames[XML_ERROR_COUNT] = { }; -XMLDocument::XMLDocument( bool processEntities, Whitespace whitespace ) : +XMLDocument::XMLDocument( bool processEntities, Whitespace whitespaceMode ) : XMLNode( 0 ), _writeBOM( false ), _processEntities( processEntities ), _errorID(XML_SUCCESS), - _whitespace( whitespace ), + _whitespaceMode( whitespaceMode ), _errorLineNum( 0 ), _charBuffer( 0 ), _parseCurLineNum( 0 ) diff --git a/tinyxml2.h b/tinyxml2.h index 55a22d9..bfd2204 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1613,7 +1613,7 @@ class TINYXML2_LIB XMLDocument : public XMLNode friend class XMLElement; public: /// constructor - XMLDocument( bool processEntities = true, Whitespace = PRESERVE_WHITESPACE ); + XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE ); ~XMLDocument(); virtual XMLDocument* ToDocument() { @@ -1677,7 +1677,7 @@ public: return _processEntities; } Whitespace WhitespaceMode() const { - return _whitespace; + return _whitespaceMode; } /** @@ -1815,7 +1815,7 @@ private: bool _writeBOM; bool _processEntities; XMLError _errorID; - Whitespace _whitespace; + Whitespace _whitespaceMode; mutable StrPair _errorStr1; mutable StrPair _errorStr2; int _errorLineNum; From 7221b49fea28eea474cc0e4ebf077cd68e88002e Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Fri, 3 Mar 2017 15:45:51 +0300 Subject: [PATCH 14/19] Suppress C6011 code analysis false positive warning --- tinyxml2.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index d3adddc..09d7682 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1838,6 +1838,7 @@ XMLAttribute* XMLElement::CreateAttribute() { TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() ); XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); + TIXMLASSERT( attrib ); attrib->_memPool = &_document->_attributePool; attrib->_memPool->SetTracked(); return attrib; From ba68a3aea6269b8fc9d276e3ae44c9d1883f2559 Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Tue, 21 Mar 2017 11:39:48 +0300 Subject: [PATCH 15/19] Pointer assertion --- tinyxml2.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 87c1a99..a645c57 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1133,6 +1133,7 @@ XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const bool XMLText::ShallowEqual( const XMLNode* compare ) const { + TIXMLASSERT( compare ); const XMLText* text = compare->ToText(); return ( text && XMLUtil::StringEqual( text->Value(), Value() ) ); } From e503563f479f72b0c32c8378abb0dac1f8619f71 Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Wed, 5 Apr 2017 18:02:40 +0300 Subject: [PATCH 16/19] Fully use enum --- tinyxml2.cpp | 4 ++-- tinyxml2.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index a645c57..0d6e2bc 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1449,7 +1449,7 @@ void XMLAttribute::SetAttribute( float v ) // --------- XMLElement ---------- // XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ), - _closingType( 0 ), + _closingType( OPEN ), _rootAttribute( 0 ) { } @@ -1868,7 +1868,7 @@ char* XMLElement::ParseDeep( char* p, StrPair* strPair, int* curLineNumPtr ) } p = ParseAttributes( p, curLineNumPtr ); - if ( !p || !*p || _closingType ) { + if ( !p || !*p || _closingType != OPEN ) { return p; } diff --git a/tinyxml2.h b/tinyxml2.h index bfd2204..17e576a 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1559,12 +1559,12 @@ public: float FloatText(float defaultValue = 0) const; // internal: - enum { + enum ElementClosingType { OPEN, // CLOSED, // CLOSING // }; - int ClosingType() const { + ElementClosingType ClosingType() const { return _closingType; } virtual XMLNode* ShallowClone( XMLDocument* document ) const; @@ -1589,7 +1589,7 @@ private: XMLAttribute* CreateAttribute(); enum { BUF_SIZE = 200 }; - int _closingType; + ElementClosingType _closingType; // The attribute list is ordered; there is no 'lastAttribute' // because the list needs to be scanned for dupes before adding // a new attribute. From 10b8ecc99b1f606a1e21afe832c0975ca58bd3fe Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Wed, 12 Apr 2017 17:57:44 +0300 Subject: [PATCH 17/19] Clarify variable names --- tinyxml2.cpp | 10 +++++----- tinyxml2.h | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 0d6e2bc..152e3d2 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -955,7 +955,7 @@ const XMLElement* XMLNode::PreviousSiblingElement( const char* name ) const } -char* XMLNode::ParseDeep( char* p, StrPair* parentEnd, int* curLineNumPtr ) +char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) { // This is a recursive method, but thinking about it "at the current level" // it is a pretty simple flat list: @@ -1020,8 +1020,8 @@ char* XMLNode::ParseDeep( char* p, StrPair* parentEnd, int* curLineNumPtr ) if ( ele ) { // We read the end tag. Return it to the parent. if ( ele->ClosingType() == XMLElement::CLOSING ) { - if ( parentEnd ) { - ele->_value.TransferTo( parentEnd ); + if ( parentEndTag ) { + ele->_value.TransferTo( parentEndTag ); } node->_memPool->SetTracked(); // created and then immediately deleted. DeleteNode( node ); @@ -1849,7 +1849,7 @@ XMLAttribute* XMLElement::CreateAttribute() // // foobar // -char* XMLElement::ParseDeep( char* p, StrPair* strPair, int* curLineNumPtr ) +char* XMLElement::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) { // Read the element name. p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr ); @@ -1872,7 +1872,7 @@ char* XMLElement::ParseDeep( char* p, StrPair* strPair, int* curLineNumPtr ) return p; } - p = XMLNode::ParseDeep( p, strPair, curLineNumPtr ); + p = XMLNode::ParseDeep( p, parentEndTag, curLineNumPtr ); return p; } diff --git a/tinyxml2.h b/tinyxml2.h index 17e576a..087b551 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -907,7 +907,7 @@ protected: XMLNode( XMLDocument* ); virtual ~XMLNode(); - virtual char* ParseDeep( char*, StrPair*, int* ); + virtual char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr); XMLDocument* _document; XMLNode* _parent; @@ -975,7 +975,7 @@ protected: XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {} virtual ~XMLText() {} - char* ParseDeep( char*, StrPair* endTag, int* curLineNumPtr ); + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ); private: bool _isCData; @@ -1006,7 +1006,7 @@ protected: XMLComment( XMLDocument* doc ); virtual ~XMLComment(); - char* ParseDeep( char*, StrPair* endTag, int* curLineNumPtr); + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr); private: XMLComment( const XMLComment& ); // not supported @@ -1045,7 +1045,7 @@ protected: XMLDeclaration( XMLDocument* doc ); virtual ~XMLDeclaration(); - char* ParseDeep( char*, StrPair* endTag, int* curLineNumPtr ); + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ); private: XMLDeclaration( const XMLDeclaration& ); // not supported @@ -1080,7 +1080,7 @@ protected: XMLUnknown( XMLDocument* doc ); virtual ~XMLUnknown(); - char* ParseDeep( char*, StrPair* endTag, int* curLineNumPtr ); + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ); private: XMLUnknown( const XMLUnknown& ); // not supported @@ -1571,7 +1571,7 @@ public: virtual bool ShallowEqual( const XMLNode* compare ) const; protected: - char* ParseDeep( char* p, StrPair* endTag, int* curLineNumPtr ); + char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ); private: XMLElement( XMLDocument* doc ); From 9333cfd394070a4e9b8fcbd5c6ecda5174b26620 Mon Sep 17 00:00:00 2001 From: Brad Anderson Date: Thu, 4 May 2017 12:25:56 -0600 Subject: [PATCH 18/19] Add "d" library suffix for debug builds --- CMakeLists.txt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bfbf31a..f17e4cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,9 +7,9 @@ IF(BIICODE) ENDIF(BIICODE) cmake_minimum_required(VERSION 2.6 FATAL_ERROR) cmake_policy(VERSION 2.6) -if(POLICY CMP0063) - cmake_policy(SET CMP0063 OLD) -endif() +if(POLICY CMP0063) + cmake_policy(SET CMP0063 OLD) +endif() project(tinyxml2) include(GNUInstallDirs) @@ -51,6 +51,9 @@ option(BUILD_STATIC_LIBS "build as static library" OFF) set(CMAKE_CXX_VISIBILITY_PRESET hidden) set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) +# to distinguish between debug and release lib +set(CMAKE_DEBUG_POSTFIX "d") + if(BUILD_SHARED_LIBS) add_library(tinyxml2 SHARED tinyxml2.cpp tinyxml2.h) From 243ddf5304b1c9e2c18896ea74870ae535b7875d Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Thu, 18 May 2017 17:27:14 +0300 Subject: [PATCH 19/19] Ensure no overrun before copying --- tinyxml2.h | 1 + 1 file changed, 1 insertion(+) diff --git a/tinyxml2.h b/tinyxml2.h index 17e576a..37871c5 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -284,6 +284,7 @@ private: TIXMLASSERT( cap <= INT_MAX / 2 ); int newAllocated = cap * 2; T* newMem = new T[newAllocated]; + TIXMLASSERT( newAllocated >= _size ); memcpy( newMem, _mem, sizeof(T)*_size ); // warning: not using constructors, only works for PODs if ( _mem != _pool ) { delete [] _mem;