[*_restore] Use a little wrapper class for the expat XML_Parser to ensure it gets destroyed.

This commit is contained in:
Joe Thornber
2014-08-26 11:23:29 +01:00
parent a7c96c0e1e
commit d17ad86a88
4 changed files with 42 additions and 28 deletions

View File

@ -3,6 +3,7 @@
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
#include <expat.h>
#include <map>
using namespace std;
@ -10,6 +11,29 @@ using namespace std;
//----------------------------------------------------------------
namespace xml_utils {
// Simple wrapper to ensure the parser gets freed if an exception
// is thrown during parsing.
class xml_parser {
public:
xml_parser()
: parser_(XML_ParserCreate(NULL)) {
if (!parser_)
throw runtime_error("couldn't create xml parser");
}
~xml_parser() {
XML_ParserFree(parser_);
}
XML_Parser get_parser() {
return parser_;
}
private:
XML_Parser parser_;
};
typedef std::map<std::string, std::string> attributes;
void build_attributes(attributes &a, char const **attr);