From 090f79b951f563c406de3cb597f0c47c40756043 Mon Sep 17 00:00:00 2001 From: curt Date: Fri, 10 Sep 2004 15:57:52 +0000 Subject: [PATCH] Add support for parsing an xml stream from an in memory buffer, rather than just from a specified file name. --- simgear/props/props_io.cxx | 17 +++++++++++++++++ simgear/props/props_io.hxx | 7 +++++++ simgear/xml/easyxml.cxx | 23 +++++++++++++++++++++++ simgear/xml/easyxml.hxx | 22 ++++++++++++++++++++++ 4 files changed, 69 insertions(+) diff --git a/simgear/props/props_io.cxx b/simgear/props/props_io.cxx index 997ac971..e936f5b0 100644 --- a/simgear/props/props_io.cxx +++ b/simgear/props/props_io.cxx @@ -322,6 +322,23 @@ readProperties (const string &file, SGPropertyNode * start_node) } +/** + * Read properties from an in-memory buffer. + * + * @param buf A character buffer containing the xml data. + * @param size The size/length of the buffer in bytes + * @param start_node The root node for reading properties. + * @return true if the read succeeded, false otherwise. + */ +void readProperties (const char *buf, const int size, + SGPropertyNode * start_node) +{ + PropsVisitor visitor(start_node, ""); + readXML(buf, size, visitor); + if (visitor.hasException()) + throw visitor.getException(); +} + //////////////////////////////////////////////////////////////////////// // Property list writer. diff --git a/simgear/props/props_io.hxx b/simgear/props/props_io.hxx index d541e1ce..2706740c 100644 --- a/simgear/props/props_io.hxx +++ b/simgear/props/props_io.hxx @@ -41,6 +41,13 @@ void readProperties (istream &input, SGPropertyNode * start_node, void readProperties (const string &file, SGPropertyNode * start_node); +/** + * Read properties from an in-memory buffer. + */ +void readProperties (const char *buf, const int size, + SGPropertyNode * start_node); + + /** * Write properties to an XML output stream. */ diff --git a/simgear/xml/easyxml.cxx b/simgear/xml/easyxml.cxx index 43054d16..3636fa9d 100644 --- a/simgear/xml/easyxml.cxx +++ b/simgear/xml/easyxml.cxx @@ -274,4 +274,27 @@ readXML (const string &path, XMLVisitor &visitor) 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)) { + XML_ParserFree(parser); + throw sg_io_exception(XML_ErrorString(XML_GetErrorCode(parser)), + sg_location("In-memory XML buffer", + XML_GetCurrentLineNumber(parser), + XML_GetCurrentColumnNumber(parser)), + "SimGear XML Parser"); + } + + XML_ParserFree(parser); +} + // end of easyxml.cxx diff --git a/simgear/xml/easyxml.hxx b/simgear/xml/easyxml.hxx index d3f6a90a..6b91a867 100644 --- a/simgear/xml/easyxml.hxx +++ b/simgear/xml/easyxml.hxx @@ -401,5 +401,27 @@ extern void readXML (istream &input, XMLVisitor &visitor, extern void readXML (const string &path, XMLVisitor &visitor); +/** + * @relates XMLVisitor + * Read an XML document. + * + * This function reads an XML document from the buffer provided, + * and invokes the callback methods in the visitor object to pass the + * parsing events back to the application. When this function + * returns, the parser will have reported all of the data in the XML + * document to the application through the visitor callback methods, + * and XML processing will be complete. + * + * @param buf The xml data buffer. + * @param size The size of the data buffer in bytes + * @param visitor An object that contains callbacks for XML parsing + * events. + * @exception Throws sg_io_exception or sg_xml_exception if there + * is a problem reading the file. + * @see XMLVisitor + */ +extern void readXML (const char *buf, const int size, XMLVisitor &visitor); + + #endif // __EASYXML_HXX -- 2.39.5