]> git.mxchange.org Git - simgear.git/blobdiff - simgear/xml/easyxml.cxx
HTTP: Rename urlretrieve/urlload to save/load.
[simgear.git] / simgear / xml / easyxml.cxx
index 181e646ba3d7a9d45d07ee7a1ea7409aa9cd2717..08a39fa48b20b7b725f671659a4381ccacd89240 100644 (file)
@@ -1,15 +1,29 @@
-// 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.
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include <simgear_config.h>
+#endif
+     
 #include <simgear/compiler.h>
 
 #include <string.h>            // strcmp()
 
 #include "easyxml.hxx"
-#include "xmlparse.h"
-
-#include STL_FSTREAM
+     
+#ifdef SYSTEM_EXPAT
+#  include <expat.h>
+#else
+#  include "sg_expat.h"     
+#endif
+     
+#include <fstream>
+#include <iostream>
 
-SG_USING_STD(ifstream);
+using std::ifstream;
 
 
 \f
@@ -122,24 +136,10 @@ XMLAttributesDefault::setValue (const char * name, const char * value)
 }
 
 
-\f
 ////////////////////////////////////////////////////////////////////////
 // Attribute list wrapper for Expat.
 ////////////////////////////////////////////////////////////////////////
 
-class ExpatAtts : public XMLAttributes
-{
-public:
-  ExpatAtts (const char ** atts) : _atts(atts) {}
-  
-  virtual int size () const;
-  virtual const char * getName (int i) const;
-  virtual const char * getValue (int i) const;
-  
-private:
-  const char ** _atts;
-};
-
 int
 ExpatAtts::size () const
 {
@@ -161,6 +161,11 @@ ExpatAtts::getValue (int i) const
   return _atts[i*2+1];
 }
 
+const char * 
+ExpatAtts::getValue (const char * name) const
+{
+  return XMLAttributes::getValue(name);
+}
 
 \f
 ////////////////////////////////////////////////////////////////////////
@@ -219,37 +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)),
                            "SimGear XML Parser");
+      XML_ParserFree(parser);
+      throw ex;
     }
 
     input.read(buf,16384);
     if (!XML_Parse(parser, buf, input.gcount(), false)) {
-      XML_ParserFree(parser);
-      throw sg_io_exception(XML_ErrorString(XML_GetErrorCode(parser)),
+      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 ex;
     }
 
   }
 
                                // Verify end of document.
   if (!XML_Parse(parser, buf, 0, true)) {
-    XML_ParserFree(parser);
-    throw sg_io_exception(XML_ErrorString(XML_GetErrorCode(parser)),
+    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 ex;
   }
 
   XML_ParserFree(parser);
+  visitor.endXML();
 }
 
 void
@@ -259,12 +268,12 @@ readXML (const string &path, XMLVisitor &visitor)
   if (input.good()) {
     try {
       readXML(input, visitor, path);
-    } catch (sg_io_exception &e) {
+    } catch (sg_io_exception &) {
       input.close();
-      throw e;
-    } catch (sg_throwable &t) {
+      throw;
+    } catch (sg_throwable &) {
       input.close();
-      throw t;
+      throw;
     }
   } else {
     throw sg_io_exception("Failed to open file", sg_location(path),
@@ -273,4 +282,29 @@ 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)) {
+      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