]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/props_io.cxx
Patch from Cameron Moore:
[simgear.git] / simgear / misc / props_io.cxx
index ee385b5e2c8c2820a9b8012c6c251b7bc0b1843b..45f8829665f7d5f85b69d122066e0b5800dba413 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "sg_path.hxx"
 #include "props.hxx"
+#include "props_io.hxx"
 
 #include STL_IOSTREAM
 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
@@ -43,9 +44,9 @@ class PropsVisitor : public XMLVisitor
 public:
 
   PropsVisitor (SGPropertyNode * root, const string &base)
-    : _root(root), _level(0), _base(base), _exception(0) {}
+    : _root(root), _level(0), _base(base), _hasException(false) {}
 
-  virtual ~PropsVisitor () { delete _exception; }
+  virtual ~PropsVisitor () {}
 
   void startXML ();
   void endXML ();
@@ -54,8 +55,12 @@ public:
   void data (const char * s, int length);
   void warning (const char * message, int line, int column);
 
-  bool hasException () const { return (_exception != 0); }
-  const sg_io_exception * getException () const { return _exception; }
+  bool hasException () const { return _hasException; }
+  sg_io_exception &getException () { return _exception; }
+  void setException (const sg_io_exception &exception) {
+    _exception = exception;
+    _hasException = true;
+  }
 
 private:
 
@@ -91,7 +96,8 @@ private:
   int _level;
   vector<State> _state_stack;
   string _base;
-  sg_io_exception * _exception;
+  sg_io_exception _exception;
+  bool _hasException;
 };
 
 void
@@ -176,6 +182,12 @@ PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
     attval = atts.getValue("archive");
     if (checkFlag(attval, false))
       mode |= SGPropertyNode::ARCHIVE;
+    attval = atts.getValue("trace-read");
+    if (checkFlag(attval, false))
+      mode |= SGPropertyNode::TRACE_READ;
+    attval = atts.getValue("trace-write");
+    if (checkFlag(attval, false))
+      mode |= SGPropertyNode::TRACE_WRITE;
 
                                // Check for an alias.
     attval = atts.getValue("alias");
@@ -191,9 +203,8 @@ PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
       path.append(attval);
       try {
        readProperties(path.str(), node);
-      } catch (const sg_io_exception &t) {
-       cerr << "Caught exception\n";
-       _exception = t.clone();
+      } catch (sg_io_exception &e) {
+       setException(e);
       }
     }
 
@@ -224,9 +235,9 @@ PropsVisitor::endElement (const char * name)
     } else if (st.type == "double") {
       ret = st.node->setDoubleValue(strtod(_data.c_str(), 0));
     } else if (st.type == "string") {
-      ret = st.node->setStringValue(_data);
+      ret = st.node->setStringValue(_data.c_str());
     } else if (st.type == "unspecified") {
-      ret = st.node->setUnspecifiedValue(_data);
+      ret = st.node->setUnspecifiedValue(_data.c_str());
     } else {
       string message = "Unrecognized data type '";
       message += st.type;
@@ -284,7 +295,7 @@ readProperties (istream &input, SGPropertyNode * start_node,
   PropsVisitor visitor(start_node, base);
   readXML(input, visitor, base);
   if (visitor.hasException())
-    throw *(visitor.getException());
+    throw visitor.getException();
 }
 
 
@@ -301,7 +312,7 @@ readProperties (const string &file, SGPropertyNode * start_node)
   PropsVisitor visitor(start_node, file);
   readXML(file, visitor);
   if (visitor.hasException())
-    throw *(visitor.getException());
+    throw visitor.getException();
 }
 
 
@@ -333,6 +344,9 @@ getTypeName (SGPropertyNode::Type type)
     return "double";
   case SGPropertyNode::STRING:
     return "string";
+  case SGPropertyNode::ALIAS:
+  case SGPropertyNode::NONE:
+    return "unspecified";
   }
 
   // keep the compiler from squawking
@@ -415,21 +429,21 @@ isArchivable (const SGPropertyNode * node)
 
 
 static bool
-writeNode (ostream &output, const SGPropertyNode * node, int indent)
+writeNode (ostream &output, const SGPropertyNode * node,
+          bool write_all, int indent)
 {
                                // Don't write the node or any of
                                // its descendants unless it is
                                // allowed to be archived.
-  if (!isArchivable(node))
+  if (!write_all && !isArchivable(node))
     return true;               // Everything's OK, but we won't write.
 
-  const string &name = node->getName();
-  int index = node->getIndex();
+  const string name = node->getName();
   int nChildren = node->nChildren();
 
                                // If there is a literal value,
                                // write it first.
-  if (node->hasValue() && node->getAttribute(SGPropertyNode::ARCHIVE)) {
+  if (node->hasValue() && (write_all || node->getAttribute(SGPropertyNode::ARCHIVE))) {
     doIndent(output, indent);
     output << '<' << name;
     writeAtts(output, node);
@@ -445,15 +459,14 @@ writeNode (ostream &output, const SGPropertyNode * node, int indent)
     }
   }
 
-                               // If there are children, write them
-                               // next.
+                               // If there are children, write them next.
   if (nChildren > 0 || node->isAlias()) {
     doIndent(output, indent);
     output << '<' << name;
     writeAtts(output, node);
     output << '>' << endl;
     for (int i = 0; i < nChildren; i++)
-      writeNode(output, node->getChild(i), indent + INDENT_STEP);
+      writeNode(output, node->getChild(i), write_all, indent + INDENT_STEP);
     doIndent(output, indent);
     output << "</" << name << '>' << endl;
   }
@@ -463,7 +476,8 @@ writeNode (ostream &output, const SGPropertyNode * node, int indent)
 
 
 void
-writeProperties (ostream &output, const SGPropertyNode * start_node)
+writeProperties (ostream &output, const SGPropertyNode * start_node,
+                bool write_all)
 {
   int nChildren = start_node->nChildren();
 
@@ -471,7 +485,7 @@ writeProperties (ostream &output, const SGPropertyNode * start_node)
   output << "<PropertyList>" << endl;
 
   for (int i = 0; i < nChildren; i++) {
-    writeNode(output, start_node->getChild(i), INDENT_STEP);
+    writeNode(output, start_node->getChild(i), write_all, INDENT_STEP);
   }
 
   output << "</PropertyList>" << endl;
@@ -479,11 +493,12 @@ writeProperties (ostream &output, const SGPropertyNode * start_node)
 
 
 void
-writeProperties (const string &file, const SGPropertyNode * start_node)
+writeProperties (const string &file, const SGPropertyNode * start_node,
+                bool write_all)
 {
   ofstream output(file.c_str());
   if (output.good()) {
-    writeProperties(output, start_node);
+    writeProperties(output, start_node, write_all);
   } else {
     throw sg_io_exception("Cannot open file", sg_location(file));
   }