From 9a9cf0e72470c2c954476b6762723278398357e4 Mon Sep 17 00:00:00 2001 From: "Zane U. Ji" Date: Fri, 10 Aug 2012 20:42:59 +0800 Subject: [PATCH] Made the result of first XPath evaluation the same as those of the others Fixed a potential memory leak --- src/wraplibxml.cpp | 17 +++++++++-------- src/xmlcopyeditor.cpp | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/wraplibxml.cpp b/src/wraplibxml.cpp index 27c8107..0694345 100755 --- a/src/wraplibxml.cpp +++ b/src/wraplibxml.cpp @@ -214,8 +214,7 @@ bool WrapLibxml::parse ( return false; } - if ( indent ) - xmlKeepBlanksDefault ( 0 ); + xmlKeepBlanksDefault ( indent ? 0 : 1 ); xmlChar *buf = NULL; int size; @@ -251,6 +250,8 @@ bool WrapLibxml::xpath ( const std::string& path, const std::string& fileName ) xmlParserCtxtPtr ctxt; xmlDocPtr docPtr; + xmlKeepBlanksDefault ( 0 ); + ctxt = xmlNewParserCtxt(); if ( ctxt == NULL ) { @@ -290,27 +291,27 @@ bool WrapLibxml::xpath ( const std::string& path, const std::string& fileName ) result = xmlXPathEvalExpression ( ( const xmlChar * ) path.c_str(), context ); - xmlKeepBlanksDefault ( 0 ); - bool xpathIsValid = ( result ) ? true : false; while ( result != NULL ) { if ( xmlXPathNodeSetIsEmpty ( result->nodesetval ) ) break; + xmlBufferPtr bufferPtr = xmlBufferCreate(); + if ( bufferPtr == NULL ) + break; nodeset = result->nodesetval; for ( int i = 0; i < nodeset->nodeNr; i++ ) { xmlNodePtr node = nodeset->nodeTab[i]; - xmlBufferPtr bufferPtr = xmlBufferCreate(); - if ( !node || !bufferPtr ) + if ( !node ) break; xmlNodeDump ( bufferPtr, NULL, node, 0, 1 ); - std::string nodeBuffer = ( const char * ) xmlBufferContent ( bufferPtr ); - output += nodeBuffer; + output += ( const char * ) xmlBufferContent ( bufferPtr ); output += '\n'; } + xmlBufferFree ( bufferPtr ); break; } if ( result ) diff --git a/src/xmlcopyeditor.cpp b/src/xmlcopyeditor.cpp index e7c8667..d2c69cf 100755 --- a/src/xmlcopyeditor.cpp +++ b/src/xmlcopyeditor.cpp @@ -5840,7 +5840,7 @@ void MyFrame::OnAssociate ( wxCommandEvent& event ) std::string origEncoding = XmlEncodingHandler::get ( utf8Buffer ); XmlEncodingHandler::setUtf8 ( utf8Buffer, true ); std::auto_ptr wellformedparser ( new WrapExpat() ); - if ( !wellformedparser->parse ( utf8Buffer ) ) // FIXME: Set encoding to UTF-8 before parsing. + if ( !wellformedparser->parse ( utf8Buffer ) ) { std::string error = wellformedparser->getLastError(); wxString wideError = wxString ( error.c_str(), wxConvUTF8, error.size() );