From edc877442fb4a8df153353568e4c8dde988e026c Mon Sep 17 00:00:00 2001 From: ThorstenB Date: Sun, 12 Jun 2011 13:41:38 +0200 Subject: [PATCH] Add optional attribute condition to "copyProperties". Option to only copy properties with specific attribute values. Default is copy all (as before). --- simgear/props/props_io.cxx | 36 ++++++++++++++++++++++++++++-------- simgear/props/props_io.hxx | 3 ++- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/simgear/props/props_io.cxx b/simgear/props/props_io.cxx index 48244c7b..e3ceaddb 100644 --- a/simgear/props/props_io.cxx +++ b/simgear/props/props_io.cxx @@ -621,11 +621,16 @@ writeProperties (const char* file, const SGPropertyNode * start_node) * * @param in The source property tree. * @param out The destination property tree. + * @param attr_value Only copy properties with given attribute values. + * @param attr_mask Mask for attributes to be considered by attr_value + * (default is 0 = attributes not considered, all + * properties copied). * @return true if all properties were copied, false if some failed * (for example, if the property's value is tied read-only). */ bool -copyProperties (const SGPropertyNode *in, SGPropertyNode *out) +copyProperties (const SGPropertyNode *in, SGPropertyNode *out, + int attr_value, int attr_mask) { using namespace simgear; bool retval = true; @@ -679,18 +684,33 @@ copyProperties (const SGPropertyNode *in, SGPropertyNode *out) } } - // copy the attributes. + // copy the attributes. out->setAttributes( in->getAttributes() ); - // Next, copy the children. + // Next, copy the children. int nChildren = in->nChildren(); for (int i = 0; i < nChildren; i++) { const SGPropertyNode * in_child = in->getChild(i); - SGPropertyNode * out_child = out->getChild(in_child->getNameString(), - in_child->getIndex(), - true); - if (!copyProperties(in_child, out_child)) - retval = false; + if (!in_child->hasValue() || + (in_child->getAttributes() & attr_mask) == attr_value) + { + SGPropertyNode * out_child = out->getChild(in_child->getNameString(), + in_child->getIndex(), + false); + if (!out_child) + { + out_child = out->getChild(in_child->getNameString(), + in_child->getIndex(), + true); + } + else + if (out_child->hasValue() && + (out_child->getAttributes() & attr_mask) != attr_value) + out_child = NULL; + if (out_child && + (!copyProperties(in_child, out_child, attr_value, attr_mask))) + retval = false; + } } return retval; diff --git a/simgear/props/props_io.hxx b/simgear/props/props_io.hxx index b619c04b..92c3bef3 100644 --- a/simgear/props/props_io.hxx +++ b/simgear/props/props_io.hxx @@ -65,7 +65,8 @@ void writeProperties (const std::string &file, /** * Copy properties from one node to another. */ -bool copyProperties (const SGPropertyNode *in, SGPropertyNode *out); +bool copyProperties (const SGPropertyNode *in, SGPropertyNode *out, + int attr_value=0, int attr_mask=0); #endif // __PROPS_IO_HXX -- 2.39.5