Skip to content

Commit

Permalink
XML: Use ScopedPtr and BOOST_SCOPE_EXIT in XMLParser
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 29, 2016
1 parent cae7f02 commit eebe067
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
37 changes: 15 additions & 22 deletions src/xml/xmlparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <libxml/parser.h>
#include <libxml/xmlerror.h>

#include <boost/scope_exit.hpp>

#include "src/common/util.h"
#include "src/common/error.h"
#include "src/common/readstream.h"
Expand Down Expand Up @@ -71,39 +73,30 @@ static void deinitXML() {
}


XMLParser::XMLParser(Common::ReadStream &stream, bool makeLower) : _rootNode(0) {
XMLParser::XMLParser(Common::ReadStream &stream, bool makeLower) {
initXML();
xmlDocPtr xml = 0;

try {

const int options = XML_PARSE_NOWARNING | XML_PARSE_NOBLANKS | XML_PARSE_NONET |
XML_PARSE_NSCLEAN | XML_PARSE_NOCDATA;
const int options = XML_PARSE_NOWARNING | XML_PARSE_NOBLANKS | XML_PARSE_NONET |
XML_PARSE_NSCLEAN | XML_PARSE_NOCDATA;

xml = xmlReadIO(readStream, closeStream, static_cast<void *>(&stream), "stream.xml", 0, options);
if (!xml)
throw Common::Exception("XML document failed to parse");
xmlDocPtr xml = xmlReadIO(readStream, closeStream, static_cast<void *>(&stream), "stream.xml", 0, options);
if (!xml)
throw Common::Exception("XML document failed to parse");

xmlNodePtr root = xmlDocGetRootElement(xml);
if (!root)
throw Common::Exception("XML document has no root node");
BOOST_SCOPE_EXIT( (&xml) ) {
xmlFreeDoc(xml);
} BOOST_SCOPE_EXIT_END

_rootNode = new XMLNode(*root, makeLower);
xmlNodePtr root = xmlDocGetRootElement(xml);
if (!root)
throw Common::Exception("XML document has no root node");

} catch (...) {
delete _rootNode;

xmlFreeDoc(xml);
deinitXML();
throw;
}
_rootNode.reset(new XMLNode(*root, makeLower));

xmlFreeDoc(xml);
deinitXML();
}

XMLParser::~XMLParser() {
delete _rootNode;
}

const XMLNode &XMLParser::getRoot() const {
Expand Down
6 changes: 5 additions & 1 deletion src/xml/xmlparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <boost/noncopyable.hpp>

#include "src/common/scopedptr.h"
#include "src/common/ustring.h"

struct _xmlNode;
Expand All @@ -52,7 +53,7 @@ class XMLParser : boost::noncopyable {
const XMLNode &getRoot() const;

private:
XMLNode *_rootNode;
Common::ScopedPtr<XMLNode> _rootNode;
};

class XMLNode : boost::noncopyable {
Expand Down Expand Up @@ -99,6 +100,9 @@ class XMLNode : boost::noncopyable {
void clean();

friend class XMLParser;

template<typename T>
friend void Common::DeallocatorDefault::destroy(T *);
};

} // End of namespace XML
Expand Down

0 comments on commit eebe067

Please sign in to comment.