X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fxml%2Feasyxml.cxx;h=0779d872b38e9120a52cdb13db5e5dd1887f5590;hb=733e6fa14ff507a1022ecab8d55cc9bf587bee40;hp=12a87fe031307bb25460dfce78dbf3d030ea823a;hpb=5f17edbc72d4d8786b66345e8029b3c076cea702;p=simgear.git diff --git a/simgear/xml/easyxml.cxx b/simgear/xml/easyxml.cxx index 12a87fe0..0779d872 100644 --- a/simgear/xml/easyxml.cxx +++ b/simgear/xml/easyxml.cxx @@ -1,4 +1,8 @@ -// easyxml.cxx - implementation of EasyXML interfaces. +/** + * \file easyxml.cxx - implementation of EasyXML interfaces. + * Written by David Megginson, 2000-2001 + * This file is in the Public Domain, and comes with NO WARRANTY of any kind. + */ #include @@ -7,37 +11,12 @@ #include "easyxml.hxx" #include "xmlparse.h" -#include STL_FSTREAM +#include +#include SG_USING_STD(ifstream); - -//////////////////////////////////////////////////////////////////////// -// Implementation of sg_xml_exception. -//////////////////////////////////////////////////////////////////////// - -sg_xml_exception::sg_xml_exception () - : sg_io_exception("", "SimGear XML parser") -{ -} - -sg_xml_exception::sg_xml_exception (const string &message) - : sg_io_exception(message, "SimGear XML parser") -{ -} - -sg_xml_exception::sg_xml_exception (const string &message, - const sg_location &location) - : sg_io_exception(message, location, "SimGear XML parser") -{ -} - -sg_xml_exception::~sg_xml_exception () -{ -} - - //////////////////////////////////////////////////////////////////////// // Implementation of XMLAttributes. @@ -245,34 +224,41 @@ readXML (istream &input, XMLVisitor &visitor, const string &path) // FIXME: get proper error string from system if (!input.good()) { - XML_ParserFree(parser); - throw sg_io_exception("Problem reading file", + sg_io_exception ex ("Problem reading file", sg_location(path, XML_GetCurrentLineNumber(parser), - XML_GetCurrentColumnNumber(parser))); + XML_GetCurrentColumnNumber(parser)), + "SimGear XML Parser"); + XML_ParserFree(parser); + throw ex; } input.read(buf,16384); if (!XML_Parse(parser, buf, input.gcount(), false)) { + sg_io_exception ex (XML_ErrorString(XML_GetErrorCode(parser)), + sg_location(path, + XML_GetCurrentLineNumber(parser), + XML_GetCurrentColumnNumber(parser)), + "SimGear XML Parser"); XML_ParserFree(parser); - throw sg_xml_exception(XML_ErrorString(XML_GetErrorCode(parser)), - sg_location(path, - XML_GetCurrentLineNumber(parser), - XML_GetCurrentColumnNumber(parser))); + throw ex; } } // Verify end of document. if (!XML_Parse(parser, buf, 0, true)) { + sg_io_exception ex (XML_ErrorString(XML_GetErrorCode(parser)), + sg_location(path, + XML_GetCurrentLineNumber(parser), + XML_GetCurrentColumnNumber(parser)), + "SimGear XML Parser"); XML_ParserFree(parser); - throw sg_xml_exception(XML_ErrorString(XML_GetErrorCode(parser)), - sg_location(path, - XML_GetCurrentLineNumber(parser), - XML_GetCurrentColumnNumber(parser))); + throw ex; } XML_ParserFree(parser); + visitor.endXML(); } void @@ -280,10 +266,45 @@ readXML (const string &path, XMLVisitor &visitor) { ifstream input(path.c_str()); if (input.good()) { - readXML(input, visitor, path); + try { + readXML(input, visitor, path); + } catch (sg_io_exception &) { + input.close(); + throw; + } catch (sg_throwable &) { + input.close(); + throw; + } } else { - throw sg_io_exception("Failed to open file", sg_location(path)); + throw sg_io_exception("Failed to open file", sg_location(path), + "SimGear XML Parser"); } + input.close(); +} + +void +readXML (const char *buf, const int size, XMLVisitor &visitor) +{ + XML_Parser parser = XML_ParserCreate(0); + XML_SetUserData(parser, &visitor); + XML_SetElementHandler(parser, start_element, end_element); + XML_SetCharacterDataHandler(parser, character_data); + XML_SetProcessingInstructionHandler(parser, processing_instruction); + + visitor.startXML(); + + if (!XML_Parse(parser, buf, size, false)) { + sg_io_exception ex (XML_ErrorString(XML_GetErrorCode(parser)), + sg_location("In-memory XML buffer", + XML_GetCurrentLineNumber(parser), + XML_GetCurrentColumnNumber(parser)), + "SimGear XML Parser"); + XML_ParserFree(parser); + throw ex; + } + + XML_ParserFree(parser); + visitor.endXML(); } // end of easyxml.cxx