+/**
+ * \file props_io.cxx
+ * Started Fall 2000 by David Megginson, david@megginson.com
+ * This code is released into the Public Domain.
+ *
+ * See props.html for documentation [replace with URL when available].
+ *
+ * $Id$
+ */
+
+#ifdef HAVE_CONFIG_H
+# include <simgear_config.h>
+#endif
#include <simgear/compiler.h>
{
public:
- PropsVisitor (SGPropertyNode * root, const string &base)
- : _root(root), _level(0), _base(base), _hasException(false) {}
+ PropsVisitor (SGPropertyNode * root, const string &base, int default_mode = 0)
+ : _default_mode(default_mode), _root(root), _level(0), _base(base), _hasException(false) {}
virtual ~PropsVisitor () {}
_level--;
}
+ int _default_mode;
string _data;
SGPropertyNode * _root;
int _level;
{
if (flag == 0)
return defaultState;
- else if (flag == "y")
+ else if (!strcmp(flag, "y"))
return true;
- else if (flag == "n")
+ else if (!strcmp(flag, "n"))
return false;
else {
string message = "Unrecognized flag value '";
void
PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
{
- State &st = state();
const char * attval;
if (_level == 0) {
- if (name != "PropertyList") {
+ if (strcmp(name, "PropertyList")) {
string message = "Root element name is ";
message += name;
message += "; expected PropertyList";
}
else {
+ State &st = state();
// Get the index.
attval = atts.getValue("n");
int index = 0;
// Get the access-mode attributes,
// but don't set yet (in case they
// prevent us from recording the value).
- int mode = 0;
+ int mode = _default_mode;
attval = atts.getValue("read");
if (checkFlag(attval, true))
attval = atts.getValue("trace-write");
if (checkFlag(attval, false))
mode |= SGPropertyNode::TRACE_WRITE;
+ attval = atts.getValue("userarchive");
+ if (checkFlag(attval, false))
+ mode |= SGPropertyNode::USERARCHIVE;
// Check for an alias.
attval = atts.getValue("alias");
} catch (sg_io_exception &e) {
setException(e);
}
+
+ const char *omit = atts.getValue("omit-node");
+ if (omit && !strcmp(omit, "y")) {
+ int nChildren = node->nChildren();
+ for (int i = 0; i < nChildren; i++) {
+ SGPropertyNode *src = node->getChild(i);
+ const char *name = src->getName();
+ int index = st.counters[name];
+ st.counters[name]++;
+ SGPropertyNode *dst = st.node->getChild(name, index, true);
+ copyProperties(src, dst);
+ }
+ st.node->removeChild(node->getName(), node->getIndex(), false);
+ node = st.node;
+ }
}
- push_state(node, atts.getValue("type"), mode);
+ const char *type = atts.getValue("type");
+ if (type)
+ node->clearValue();
+ push_state(node, type, mode);
}
}
ret = st.node->setStringValue(_data.c_str());
} else if (st.type == "unspecified") {
ret = st.node->setUnspecifiedValue(_data.c_str());
+ } else if (_level == 1) {
+ ret = true; // empty <PropertyList>
} else {
string message = "Unrecognized data type '";
message += st.type;
*/
void
readProperties (istream &input, SGPropertyNode * start_node,
- const string &base)
+ const string &base, int default_mode)
{
- PropsVisitor visitor(start_node, base);
+ PropsVisitor visitor(start_node, base, default_mode);
readXML(input, visitor, base);
if (visitor.hasException())
throw visitor.getException();
* @return true if the read succeeded, false otherwise.
*/
void
-readProperties (const string &file, SGPropertyNode * start_node)
+readProperties (const string &file, SGPropertyNode * start_node,
+ int default_mode)
{
- PropsVisitor visitor(start_node, file);
+ PropsVisitor visitor(start_node, file, default_mode);
readXML(file, visitor);
if (visitor.hasException())
throw visitor.getException();
}
+/**
+ * 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, int default_mode)
+{
+ PropsVisitor visitor(start_node, "", default_mode);
+ readXML(buf, size, visitor);
+ if (visitor.hasException())
+ throw visitor.getException();
+}
+
\f
////////////////////////////////////////////////////////////////////////
// Property list writer.
static void
-writeAtts (ostream &output, const SGPropertyNode * node)
+writeAtts (ostream &output, const SGPropertyNode * node, bool forceindex)
{
int index = node->getIndex();
- if (index != 0)
+ if (index != 0 || forceindex)
output << " n=\"" << index << '"';
#if 0
* Test whether a node is archivable or has archivable descendants.
*/
static bool
-isArchivable (const SGPropertyNode * node)
+isArchivable (const SGPropertyNode * node, SGPropertyNode::Attribute archive_flag)
{
// FIXME: it's inefficient to do this all the time
- if (node->getAttribute(SGPropertyNode::ARCHIVE))
+ if (node->getAttribute(archive_flag))
return true;
else {
int nChildren = node->nChildren();
for (int i = 0; i < nChildren; i++)
- if (isArchivable(node->getChild(i)))
+ if (isArchivable(node->getChild(i), archive_flag))
return true;
}
return false;
static bool
writeNode (ostream &output, const SGPropertyNode * node,
- bool write_all, int indent)
+ bool write_all, int indent, SGPropertyNode::Attribute archive_flag)
{
// Don't write the node or any of
// its descendants unless it is
// allowed to be archived.
- if (!write_all && !isArchivable(node))
+ if (!write_all && !isArchivable(node, archive_flag))
return true; // Everything's OK, but we won't write.
const string name = node->getName();
int nChildren = node->nChildren();
+ bool node_has_value = false;
// If there is a literal value,
// write it first.
- if (node->hasValue() && (write_all || node->getAttribute(SGPropertyNode::ARCHIVE))) {
+ if (node->hasValue() && (write_all || node->getAttribute(archive_flag))) {
doIndent(output, indent);
output << '<' << name;
- writeAtts(output, node);
+ writeAtts(output, node, nChildren != 0);
if (node->isAlias() && node->getAliasTarget() != 0) {
output << " alias=\"" << node->getAliasTarget()->getPath()
<< "\"/>" << endl;
writeData(output, node->getStringValue());
output << "</" << name << '>' << endl;
}
+ node_has_value = true;
}
// If there are children, write them next.
- if (nChildren > 0 || node->isAlias()) {
+ if (nChildren > 0) {
doIndent(output, indent);
output << '<' << name;
- writeAtts(output, node);
+ writeAtts(output, node, node_has_value);
output << '>' << endl;
for (int i = 0; i < nChildren; i++)
- writeNode(output, node->getChild(i), write_all, indent + INDENT_STEP);
+ writeNode(output, node->getChild(i), write_all, indent + INDENT_STEP, archive_flag);
doIndent(output, indent);
output << "</" << name << '>' << endl;
}
void
writeProperties (ostream &output, const SGPropertyNode * start_node,
- bool write_all)
+ bool write_all, SGPropertyNode::Attribute archive_flag)
{
int nChildren = start_node->nChildren();
output << "<PropertyList>" << endl;
for (int i = 0; i < nChildren; i++) {
- writeNode(output, start_node->getChild(i), write_all, INDENT_STEP);
+ writeNode(output, start_node->getChild(i), write_all, INDENT_STEP, archive_flag);
}
output << "</PropertyList>" << endl;
void
writeProperties (const string &file, const SGPropertyNode * start_node,
- bool write_all)
+ bool write_all, SGPropertyNode::Attribute archive_flag)
{
+ SGPath path(file.c_str());
+ path.create_dir(0777);
+
ofstream output(file.c_str());
if (output.good()) {
- writeProperties(output, start_node, write_all);
+ writeProperties(output, start_node, write_all, archive_flag);
} else {
throw sg_io_exception("Cannot open file", sg_location(file));
}
retval = false;
break;
default:
+ if (in->isAlias())
+ break;
string message = "Unknown internal SGPropertyNode type";
message += in->getType();
throw sg_error(message, "SimGear Property Reader");
}
}
+ // copy the attributes.
+ out->setAttributes( in->getAttributes() );
+
// Next, copy the children.
int nChildren = in->nChildren();
for (int i = 0; i < nChildren; i++) {