// easyxml.cxx - implementation of EasyXML interfaces.
+#include <simgear/compiler.h>
+
+#include <string.h> // strcmp()
+
#include "easyxml.hxx"
#include "xmlparse.h"
+#include STL_FSTREAM
+#include STL_IOSTREAM
+
+SG_USING_STD(ifstream);
+
+
\f
////////////////////////////////////////////////////////////////////////
// Implementation of XMLAttributes.
XMLAttributes::findAttribute (const char * name) const
{
int s = size();
- for (int i = 0; i < s; i++)
+ for (int i = 0; i < s; i++) {
if (strcmp(name, getName(i)) == 0)
return i;
+ }
return -1;
}
if (pos >= 0)
return getValue(pos);
else
- return getValue(0);
+ return 0;
}
\f
public:
ExpatAtts (const char ** atts) : _atts(atts) {}
- int size () const;
- const char * getName (int i) const;
- const char * getValue (int i) const;
+ virtual int size () const;
+ virtual const char * getName (int i) const;
+ virtual const char * getValue (int i) const;
private:
const char ** _atts;
// Static callback functions for Expat.
////////////////////////////////////////////////////////////////////////
-#define VISITOR (*((XMLVisitor*)userData))
+#define VISITOR (*((XMLVisitor *)userData))
static void
start_element (void * userData, const char * name, const char ** atts)
{
- ExpatAtts attributes(atts);
- VISITOR.startElement(name, attributes);
+ VISITOR.startElement(name, ExpatAtts(atts));
}
static void
// Implementation of XMLReader.
////////////////////////////////////////////////////////////////////////
-bool
-readXML (istream &input, XMLVisitor &visitor)
+void
+readXML (istream &input, XMLVisitor &visitor, const string &path)
{
- bool retval = true;
-
XML_Parser parser = XML_ParserCreate(0);
XML_SetUserData(parser, &visitor);
XML_SetElementHandler(parser, start_element, end_element);
char buf[16384];
while (!input.eof()) {
+ // FIXME: get proper error string from system
+ if (!input.good()) {
+ XML_ParserFree(parser);
+ throw sg_io_exception("Problem reading file",
+ sg_location(path,
+ XML_GetCurrentLineNumber(parser),
+ XML_GetCurrentColumnNumber(parser)),
+ "SimGear XML Parser");
+ }
+
input.read(buf,16384);
- // TODO: deal with bad stream.
if (!XML_Parse(parser, buf, input.gcount(), false)) {
- visitor.error(XML_ErrorString(XML_GetErrorCode(parser)),
- XML_GetCurrentColumnNumber(parser),
- XML_GetCurrentLineNumber(parser));
- retval = false;
+ XML_ParserFree(parser);
+ throw sg_io_exception(XML_ErrorString(XML_GetErrorCode(parser)),
+ sg_location(path,
+ XML_GetCurrentLineNumber(parser),
+ XML_GetCurrentColumnNumber(parser)),
+ "SimGear XML Parser");
}
+
}
- if (retval)
- visitor.endXML();
+ // Verify end of document.
+ if (!XML_Parse(parser, buf, 0, true)) {
+ XML_ParserFree(parser);
+ throw sg_io_exception(XML_ErrorString(XML_GetErrorCode(parser)),
+ sg_location(path,
+ XML_GetCurrentLineNumber(parser),
+ XML_GetCurrentColumnNumber(parser)),
+ "SimGear XML Parser");
+ }
XML_ParserFree(parser);
+}
- return retval;
+void
+readXML (const string &path, XMLVisitor &visitor)
+{
+ ifstream input(path.c_str());
+ if (input.good()) {
+ try {
+ readXML(input, visitor, path);
+ } catch (sg_io_exception &e) {
+ input.close();
+ throw e;
+ } catch (sg_throwable &t) {
+ input.close();
+ throw t;
+ }
+ } else {
+ 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)) {
+ 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