-// props.cxx -- implementation of SimGear Property Manager.
-//
-// Written by David Megginson - david@megginson.com
-//
-// This module is in the PUBLIC DOMAIN.
-//
-// This program is distributed in the hope that it will be useful, but
-// WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+// props.hxx - interface definition for a property list.
+// 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 <config.h>
-#endif
-
-#include <simgear/debug/logstream.hxx>
-
+#include <stdio.h>
+#include <stdlib.h>
+#include <iostream>
+#include <algorithm>
#include "props.hxx"
-#include <stdlib.h>
+using std::cerr;
+using std::endl;
+using std::sort;
-#include <string>
-using std::string;
+\f
+////////////////////////////////////////////////////////////////////////
+// Convenience macros for value access.
+////////////////////////////////////////////////////////////////////////
-FGPropertyList current_properties;
+#define GET_BOOL (_value.bool_val->getValue())
+#define GET_INT (_value.int_val->getValue())
+#define GET_FLOAT (_value.float_val->getValue())
+#define GET_DOUBLE (_value.double_val->getValue())
+#define GET_STRING (_value.string_val->getValue())
-static string empty_string;
+#define SET_BOOL(val) (_value.bool_val->setValue(val))
+#define SET_INT(val) (_value.int_val->setValue(val))
+#define SET_FLOAT(val) (_value.float_val->setValue(val))
+#define SET_DOUBLE(val) (_value.double_val->setValue(val))
+#define SET_STRING(val) (_value.string_val->setValue(val))
\f
////////////////////////////////////////////////////////////////////////
-// Implementation of FGValue.
+// Default values for every type.
////////////////////////////////////////////////////////////////////////
+const bool SGRawValue<bool>::DefaultValue = false;
+const int SGRawValue<int>::DefaultValue = 0;
+const float SGRawValue<float>::DefaultValue = 0.0;
+const double SGRawValue<double>::DefaultValue = 0.0L;
+const string SGRawValue<string>::DefaultValue = "";
-/**
- * Construct a new value.
- */
-FGValue::FGValue ()
- : _type(UNKNOWN), _tied(false)
-{
-}
+\f
+////////////////////////////////////////////////////////////////////////
+// Local path normalization code.
+////////////////////////////////////////////////////////////////////////
/**
- * Destroy a value.
+ * A component in a path.
*/
-FGValue::~FGValue ()
+struct PathComponent
{
-}
-
+ string name;
+ int index;
+};
/**
- * Return a raw boolean value (no type coercion).
+ * Parse the name for a path component.
+ *
+ * Name: [_a-zA-Z][-._a-zA-Z0-9]*
*/
-bool
-FGValue::getRawBool () const
+static inline string
+parse_name (const string &path, int &i)
{
- if (_tied) {
- if (_value.bool_func.getter != 0)
- return (*(_value.bool_func.getter))();
- else
- return false;
- } else {
- return _value.bool_val;
+ string name = "";
+ int max = path.size();
+
+ if (path[i] == '.') {
+ i++;
+ if (i < max && path[i] == '.') {
+ i++;
+ name = "..";
+ } else {
+ name = ".";
+ }
+ if (i < max && path[i] != '/')
+ throw string(string("Illegal character after ") + name);
+ }
+
+ else if (isalpha(path[i]) || path[i] == '_') {
+ name += path[i];
+ i++;
+
+ // The rules inside a name are a little
+ // less restrictive.
+ while (i < max) {
+ if (isalpha(path[i]) || isdigit(path[i]) || path[i] == '_' ||
+ path[i] == '-' || path[i] == '.') {
+ name += path[i];
+ } else if (path[i] == '[' || path[i] == '/') {
+ break;
+ } else {
+ throw string("name may contain only ._- and alphanumeric characters");
+ }
+ i++;
+ }
}
+
+ else {
+ if (name.size() == 0)
+ throw string("name must begin with alpha or '_'");
+ }
+
+ return name;
}
/**
- * Return a raw integer value (no type coercion).
+ * Parse the optional integer index for a path component.
+ *
+ * Index: "[" [0-9]+ "]"
*/
-int
-FGValue::getRawInt () const
+static inline int
+parse_index (const string &path, int &i)
{
- if (_tied) {
- if (_value.int_func.getter != 0)
- return (*(_value.int_func.getter))();
- else
- return 0;
- } else {
- return _value.int_val;
+ int index = 0;
+
+ if (path[i] != '[')
+ return 0;
+ else
+ i++;
+
+ for (int max = path.size(); i < max; i++) {
+ if (isdigit(path[i])) {
+ index = (index * 10) + (path[i] - '0');
+ } else if (path[i] == ']') {
+ i++;
+ return index;
+ } else {
+ break;
+ }
}
+
+ throw string("unterminated index (looking for ']')");
}
/**
- * Return a raw floating-point value (no type coercion).
+ * Parse a single path component.
+ *
+ * Component: Name Index?
*/
-float
-FGValue::getRawFloat () const
+static inline PathComponent
+parse_component (const string &path, int &i)
{
- if (_tied) {
- if (_value.float_func.getter != 0)
- return (*(_value.float_func.getter))();
- else
- return 0.0;
- } else {
- return _value.float_val;
- }
+ PathComponent component;
+ component.name = parse_name(path, i);
+ if (component.name[0] != '.')
+ component.index = parse_index(path, i);
+ else
+ component.index = -1;
+ return component;
}
/**
- * Return a raw double-precision floating-point value (no type coercion).
+ * Parse a path into its components.
*/
-double
-FGValue::getRawDouble () const
+static void
+parse_path (const string &path, vector<PathComponent> &components)
{
- if (_tied) {
- if (_value.double_func.getter != 0)
- return (*(_value.double_func.getter))();
- else
- return 0.0L;
- } else {
- return _value.double_val;
+ int pos = 0;
+ int max = path.size();
+
+ // Check for initial '/'
+ if (path[pos] == '/') {
+ PathComponent root;
+ root.name = "";
+ root.index = -1;
+ components.push_back(root);
+ pos++;
+ while (pos < max && path[pos] == '/')
+ pos++;
+ }
+
+ while (pos < max) {
+ components.push_back(parse_component(path, pos));
+ while (pos < max && path[pos] == '/')
+ pos++;
}
}
+\f
+////////////////////////////////////////////////////////////////////////
+// Other static utility functions.
+////////////////////////////////////////////////////////////////////////
+
+
/**
- * Return a raw string value (no type coercion).
+ * Locate a child node by name and index.
*/
-const string &
-FGValue::getRawString () const
+static int
+find_child (const string &name, int index, vector<SGPropertyNode *> nodes)
{
- if (_tied && _value.string_func.getter != 0)
- return (*(_value.string_func.getter))();
- else
- return string_val;
+ int nNodes = nodes.size();
+ for (int i = 0; i < nNodes; i++) {
+ SGPropertyNode * node = nodes[i];
+ if (node->getName() == name && node->getIndex() == index)
+ return i;
+ }
+ return -1;
}
/**
- * Set a raw boolean value (no type coercion).
- *
- * Return false if the value could not be set, true otherwise.
+ * Locate another node, given a relative path.
*/
-bool
-FGValue::setRawBool (bool value)
+static SGPropertyNode *
+find_node (SGPropertyNode * current,
+ const vector<PathComponent> &components,
+ int position,
+ bool create)
{
- if (_tied) {
- if (_value.bool_func.setter != 0) {
- (*_value.bool_func.setter)(value);
- return true;
- } else {
- return false;
- }
- } else {
- _value.bool_val = value;
- return true;
+ if (current == 0) {
+ return 0;
+ }
+
+ else if (position >= components.size()) {
+ return current;
+ }
+
+ else if (components[position].name == "") {
+ return find_node(current->getRootNode(), components, position + 1, create);
+ }
+
+ else if (components[position].name == ".") {
+ return find_node(current, components, position + 1, create);
+ }
+
+ else if (components[position].name == "..") {
+ SGPropertyNode * parent = current->getParent();
+ if (parent == 0)
+ throw string("Attempt to move past root with '..'");
+ else
+ return find_node(parent, components, position + 1, create);
+ }
+
+ else {
+ SGPropertyNode * child =
+ current->getChild(components[position].name,
+ components[position].index,
+ create);
+ return find_node(child, components, position + 1, create);
}
}
+\f
+////////////////////////////////////////////////////////////////////////
+// Implementation of SGValue.
+////////////////////////////////////////////////////////////////////////
+
+
/**
- * Set a raw integer value (no type coercion).
+ * Default constructor.
*
- * Return false if the value could not be set, true otherwise.
+ * The type will be UNKNOWN and the raw value will be "".
*/
-bool
-FGValue::setRawInt (int value)
+SGValue::SGValue ()
+ : _type(UNKNOWN), _tied(false)
{
- if (_tied) {
- if (_value.int_func.setter != 0) {
- (*_value.int_func.setter)(value);
- return true;
- } else {
- return false;
- }
- } else {
- _value.int_val = value;
- return true;
- }
+ _value.string_val = new SGRawValueInternal<string>;
}
/**
- * Set a raw floating-point value (no type coercion).
- *
- * Return false if the value could not be set, true otherwise.
+ * Copy constructor.
*/
-bool
-FGValue::setRawFloat (float value)
+SGValue::SGValue (const SGValue &source)
{
- if (_tied) {
- if (_value.float_func.setter != 0) {
- (*_value.float_func.setter)(value);
- return true;
- } else {
- return false;
- }
- } else {
- _value.float_val = value;
- return true;
+ _type = source._type;
+ _tied = source._tied;
+ switch (source._type) {
+ case BOOL:
+ _value.bool_val = source._value.bool_val->clone();
+ break;
+ case INT:
+ _value.int_val = source._value.int_val->clone();
+ break;
+ case FLOAT:
+ _value.float_val = source._value.float_val->clone();
+ break;
+ case DOUBLE:
+ _value.double_val = source._value.double_val->clone();
+ break;
+ case STRING:
+ case UNKNOWN:
+ _value.string_val = source._value.string_val->clone();
+ break;
}
}
/**
- * Set a raw double-precision floating-point value (no type coercion).
- *
- * Return false if the value could not be set, true otherwise.
+ * Destructor.
*/
-bool
-FGValue::setRawDouble (double value)
+SGValue::~SGValue ()
{
- if (_tied) {
- if (_value.double_func.setter != 0) {
- (*_value.double_func.setter)(value);
- return true;
- } else {
- return false;
- }
- } else {
- _value.double_val = value;
- return true;
- }
+ clear_value();
}
/**
- * Set a raw string value (no type coercion).
- *
- * Return false if the value could not be set, true otherwise.
+ * Delete and clear the current value.
*/
-bool
-FGValue::setRawString (const string &value)
+void
+SGValue::clear_value ()
{
- if (_tied) {
- if (_value.string_func.setter != 0) {
- (*_value.string_func.setter)(value);
- return true;
- } else {
- return false;
- }
- } else {
- string_val = value;
- return true;
+ switch (_type) {
+ case BOOL:
+ delete _value.bool_val;
+ _value.bool_val = 0;
+ break;
+ case INT:
+ delete _value.int_val;
+ _value.int_val = 0;
+ break;
+ case FLOAT:
+ delete _value.float_val;
+ _value.float_val = 0;
+ break;
+ case DOUBLE:
+ delete _value.double_val;
+ _value.double_val = 0;
+ break;
+ case STRING:
+ case UNKNOWN:
+ delete _value.string_val;
+ _value.string_val = 0;
+ break;
}
}
/**
- * Get the boolean value of a property.
- *
- * If the native type is not boolean, attempt to coerce it.
+ * Get a boolean value.
*/
bool
-FGValue::getBoolValue () const
+SGValue::getBoolValue () const
{
switch (_type) {
case BOOL:
- return getRawBool();
+ return GET_BOOL;
case INT:
- return (getRawInt() == 0 ? false : true);
+ return GET_INT == 0 ? false : true;
case FLOAT:
- return (getRawFloat() == 0.0 ? false : true);
+ return GET_FLOAT == 0.0 ? false : true;
case DOUBLE:
- return (getRawDouble() == 0.0 ? false : true);
- case UNKNOWN:
+ return GET_DOUBLE == 0.0L ? false : true;
case STRING:
- return ((getRawString() == "false" || getIntValue() == 0) ? false : true);
+ case UNKNOWN:
+ return (GET_STRING == "true" || getDoubleValue() != 0.0L);
}
- return false;
}
/**
- * Get the integer value of a property.
- *
- * If the native type is not integer, attempt to coerce it.
+ * Get an integer value.
*/
int
-FGValue::getIntValue () const
+SGValue::getIntValue () const
{
switch (_type) {
case BOOL:
- return getRawBool();
+ return (int)GET_BOOL;
case INT:
- return getRawInt();
+ return GET_INT;
case FLOAT:
- return (int)(getRawFloat());
+ return (int)GET_FLOAT;
case DOUBLE:
- return (int)(getRawDouble());
- case UNKNOWN:
+ return (int)GET_DOUBLE;
case STRING:
- return atoi(getRawString().c_str());
+ case UNKNOWN:
+ return atoi(GET_STRING.c_str());
}
- return false;
}
/**
- * Get the floating-point value of a property.
- *
- * If the native type is not float, attempt to coerce it.
+ * Get a float value.
*/
float
-FGValue::getFloatValue () const
+SGValue::getFloatValue () const
{
switch (_type) {
- case UNKNOWN:
- return 0.0;
case BOOL:
- return (float)(getRawBool());
+ return (float)GET_BOOL;
case INT:
- return (float)(getRawInt());
+ return (float)GET_INT;
case FLOAT:
- return getRawFloat();
+ return GET_FLOAT;
case DOUBLE:
- return (float)(getRawDouble());
+ return GET_DOUBLE;
case STRING:
- return (float)atof(getRawString().c_str());
+ case UNKNOWN:
+ return atof(GET_STRING.c_str());
}
- return false;
}
/**
- * Get the double-precision floating-point value of a property.
- *
- * If the native type is not double, attempt to coerce it.
+ * Get a double value.
*/
double
-FGValue::getDoubleValue () const
+SGValue::getDoubleValue () const
{
switch (_type) {
- case UNKNOWN:
- return 0.0;
case BOOL:
- return (double)(getRawBool());
+ return (double)GET_BOOL;
case INT:
- return (double)(getRawInt());
+ return (double)GET_INT;
case FLOAT:
- return (double)(getRawFloat());
+ return (double)GET_FLOAT;
case DOUBLE:
- return getRawDouble();
+ return GET_DOUBLE;
case STRING:
- return atof(getRawString().c_str());
+ case UNKNOWN:
+ return (double)atof(GET_STRING.c_str());
}
- return false;
}
/**
- * Get the string value of a property.
- *
- * If the native type is not string, attempt to coerce it.
+ * Get a string value.
*/
-const string &
-FGValue::getStringValue () const
+string
+SGValue::getStringValue () const
{
- char buf[512];
+ char buf[128];
+
switch (_type) {
- case UNKNOWN:
- return getRawString();
case BOOL:
- if (getRawBool())
- string_val = "true";
+ if (GET_BOOL)
+ return "true";
else
- string_val = "false";
- return string_val;
+ return "false";
case INT:
- sprintf(buf, "%d", getRawInt());
- string_val = buf;
- return string_val;
+ sprintf(buf, "%d", GET_INT);
+ return buf;
case FLOAT:
- sprintf(buf, "%f", getRawFloat());
- string_val = buf;
- return string_val;
+ sprintf(buf, "%f", GET_FLOAT);
+ return buf;
case DOUBLE:
- sprintf(buf, "%f", getRawDouble());
- string_val = buf;
- return string_val;
+ sprintf(buf, "%lf", GET_DOUBLE);
+ return buf;
case STRING:
- return getRawString();
+ case UNKNOWN:
+ return GET_STRING;
}
- return empty_string;
}
/**
- * Set the boolean value and change the type if unknown.
- *
- * Returns true on success.
+ * Set a bool value.
*/
bool
-FGValue::setBoolValue (bool value)
+SGValue::setBoolValue (bool value)
{
- if (_type == UNKNOWN || _type == BOOL) {
+ if (_type == UNKNOWN) {
+ clear_value();
+ _value.bool_val = new SGRawValueInternal<bool>;
_type = BOOL;
- return setRawBool(value);
- } else {
- return false;
}
+
+ switch (_type) {
+ case BOOL:
+ return SET_BOOL(value);
+ case INT:
+ return SET_INT((int)value);
+ case FLOAT:
+ return SET_FLOAT((float)value);
+ case DOUBLE:
+ return SET_DOUBLE((double)value);
+ case STRING:
+ return SET_STRING(value ? "true" : "false");
+ }
+
+ return false;
}
/**
- * Set the integer value and change the type if unknown.
- *
- * Returns true on success.
+ * Set an int value.
*/
bool
-FGValue::setIntValue (int value)
+SGValue::setIntValue (int value)
{
- if (_type == UNKNOWN || _type == INT) {
+ if (_type == UNKNOWN) {
+ clear_value();
+ _value.int_val = new SGRawValueInternal<int>;
_type = INT;
- return setRawInt(value);
- } else {
- return false;
}
+
+ switch (_type) {
+ case BOOL:
+ return SET_BOOL(value == 0 ? false : true);
+ case INT:
+ return SET_INT(value);
+ case FLOAT:
+ return SET_FLOAT((float)value);
+ case DOUBLE:
+ return SET_DOUBLE((double)value);
+ case STRING: {
+ char buf[128];
+ sprintf(buf, "%d", value);
+ return SET_STRING(buf);
+ }
+ }
+
+ return false;
}
/**
- * Set the floating-point value and change the type if unknown.
- *
- * Returns true on success.
+ * Set a float value.
*/
bool
-FGValue::setFloatValue (float value)
+SGValue::setFloatValue (float value)
{
- if (_type == UNKNOWN || _type == FLOAT) {
+ if (_type == UNKNOWN) {
+ clear_value();
+ _value.float_val = new SGRawValueInternal<float>;
_type = FLOAT;
- return setRawFloat(value);
- } else {
- return false;
}
+
+ switch (_type) {
+ case BOOL:
+ return SET_BOOL(value == 0.0 ? false : true);
+ case INT:
+ return SET_INT((int)value);
+ case FLOAT:
+ return SET_FLOAT(value);
+ case DOUBLE:
+ return SET_DOUBLE((double)value);
+ case STRING: {
+ char buf[128];
+ sprintf(buf, "%f", value);
+ return SET_STRING(buf);
+ }
+ }
+
+ return false;
}
/**
- * Set the double-precision value and change the type if unknown.
- *
- * Returns true on success.
+ * Set a double value.
*/
bool
-FGValue::setDoubleValue (double value)
+SGValue::setDoubleValue (double value)
{
- if (_type == UNKNOWN || _type == DOUBLE) {
+ if (_type == UNKNOWN) {
+ clear_value();
+ _value.double_val = new SGRawValueInternal<double>;
_type = DOUBLE;
- return setRawDouble(value);
- } else {
- return false;
}
+
+ switch (_type) {
+ case BOOL:
+ return SET_BOOL(value == 0.0L ? false : true);
+ case INT:
+ return SET_INT((int)value);
+ case FLOAT:
+ return SET_FLOAT((float)value);
+ case DOUBLE:
+ return SET_DOUBLE(value);
+ case STRING: {
+ char buf[128];
+ sprintf(buf, "%lf", value);
+ return SET_STRING(buf);
+ }
+ }
+
+ return false;
}
/**
- * Set the string value and change the type if unknown.
- *
- * Returns true on success.
+ * Set a string value.
*/
bool
-FGValue::setStringValue (const string &value)
+SGValue::setStringValue (string value)
{
- if (_type == UNKNOWN || _type == STRING) {
+ if (_type == UNKNOWN) {
+ clear_value();
+ _value.string_val = new SGRawValueInternal<string>;
_type = STRING;
- return setRawString(value);
- } else {
- return false;
}
+
+ switch (_type) {
+ case BOOL:
+ return SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
+ case INT:
+ return SET_INT(atoi(value.c_str()));
+ case FLOAT:
+ return SET_FLOAT(atof(value.c_str()));
+ case DOUBLE:
+ return SET_DOUBLE((double)atof(value.c_str()));
+ case STRING:
+ return SET_STRING(value);
+ }
+
+ return false;
}
/**
- * Set a string value and don't modify the type.
- *
- * Returns true on success.
+ * Set a value of unknown type (stored as a string).
*/
bool
-FGValue::setUnknownValue (const string &value)
+SGValue::setUnknownValue (string value)
{
- if (_type == UNKNOWN || _type == STRING) {
- return setRawString(value);
- } else {
- return false;
+ switch (_type) {
+ case BOOL:
+ return SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
+ case INT:
+ return SET_INT(atoi(value.c_str()));
+ case FLOAT:
+ return SET_FLOAT(atof(value.c_str()));
+ case DOUBLE:
+ return SET_DOUBLE((double)atof(value.c_str()));
+ case STRING:
+ case UNKNOWN:
+ return SET_STRING(value);
}
+
+ return false;
}
/**
- * Tie a boolean value to external functions.
- *
- * If useDefault is true, attempt the assign the current value
- * (if any) after tying the functions.
- *
- * Returns true on success (i.e. the value is not currently tied).
+ * Tie a bool value.
*/
bool
-FGValue::tieBool (bool_getter getter, bool_setter setter = 0,
- bool useDefault = true)
+SGValue::tie (const SGRawValue<bool> &value, bool use_default)
{
- if (_tied) {
+ if (_tied)
return false;
- } else {
- if (useDefault && setter && _type != UNKNOWN)
- (*setter)(getBoolValue());
- _tied = true;
- _type = BOOL;
- _value.bool_func.getter = getter;
- _value.bool_func.setter = setter;
- return true;
- }
+
+ bool old_val;
+ if (use_default)
+ old_val = getBoolValue();
+
+ clear_value();
+ _type = BOOL;
+ _tied = true;
+ _value.bool_val = value.clone();
+
+ if (use_default)
+ setBoolValue(old_val);
+
+ return true;
}
/**
- * Tie an integer value to external functions.
- *
- * If useDefault is true, attempt the assign the current value
- * (if any) after tying the functions.
- *
- * Returns true on success (i.e. the value is not currently tied).
+ * Tie an int value.
*/
bool
-FGValue::tieInt (int_getter getter, int_setter setter = 0,
- bool useDefault = true)
+SGValue::tie (const SGRawValue<int> &value, bool use_default)
{
- if (_tied) {
+ if (_tied)
return false;
- } else {
- if (useDefault && setter && _type != UNKNOWN)
- (*setter)(getIntValue());
- _tied = true;
- _type = INT;
- _value.int_func.getter = getter;
- _value.int_func.setter = setter;
- return true;
- }
+
+ int old_val;
+ if (use_default)
+ old_val = getIntValue();
+
+ clear_value();
+ _type = INT;
+ _tied = true;
+ _value.int_val = value.clone();
+
+ if (use_default)
+ setIntValue(old_val);
+
+ return true;
}
/**
- * Tie a floating-point value to external functions.
- *
- * If useDefault is true, attempt the assign the current value
- * (if any) after tying the functions.
- *
- * Returns true on success (i.e. the value is not currently tied).
+ * Tie a float value.
*/
bool
-FGValue::tieFloat (float_getter getter, float_setter setter = 0,
- bool useDefault = true)
+SGValue::tie (const SGRawValue<float> &value, bool use_default)
{
- if (_tied) {
+ if (_tied)
return false;
- } else {
- if (useDefault && setter && _type != UNKNOWN)
- (*setter)(getFloatValue());
- _tied = true;
- _type = FLOAT;
- _value.float_func.getter = getter;
- _value.float_func.setter = setter;
- return true;
- }
+
+ float old_val;
+ if (use_default)
+ old_val = getFloatValue();
+
+ clear_value();
+ _type = FLOAT;
+ _tied = true;
+ _value.float_val = value.clone();
+
+ if (use_default)
+ setFloatValue(old_val);
+
+ return true;
}
/**
- * Tie a double-precision floating-point value to external functions.
- *
- * If useDefault is true, attempt the assign the current value
- * (if any) after tying the functions.
- *
- * Returns true on success (i.e. the value is not currently tied).
+ * Tie a double value.
*/
bool
-FGValue::tieDouble (double_getter getter, double_setter setter = 0,
- bool useDefault = true)
+SGValue::tie (const SGRawValue<double> &value, bool use_default)
{
- if (_tied) {
+ if (_tied)
return false;
- } else {
- if (useDefault && setter && _type != UNKNOWN)
- (*setter)(getDoubleValue());
- _tied = true;
- _type = DOUBLE;
- _value.double_func.getter = getter;
- _value.double_func.setter = setter;
- return true;
- }
+
+ double old_val;
+ if (use_default)
+ old_val = getDoubleValue();
+
+ clear_value();
+ _type = DOUBLE;
+ _tied = true;
+ _value.double_val = value.clone();
+
+ if (use_default)
+ setDoubleValue(old_val);
+
+ return true;
}
/**
- * Tie a string value to external functions.
- *
- * If useDefault is true, attempt the assign the current value
- * (if any) after tying the functions.
- *
- * Returns true on success (i.e. the value is not currently tied).
+ * Tie a string value.
*/
bool
-FGValue::tieString (string_getter getter, string_setter setter = 0,
- bool useDefault = true)
+SGValue::tie (const SGRawValue<string> &value, bool use_default)
{
- if (_tied) {
+ if (_tied)
return false;
- } else {
- if (useDefault && setter && _type != UNKNOWN)
- (*setter)(getStringValue());
- _tied = true;
- _type = STRING;
- _value.string_func.getter = getter;
- _value.string_func.setter = setter;
- return true;
- }
+
+ string old_val;
+ if (use_default)
+ old_val = getStringValue();
+
+ clear_value();
+ _type = STRING;
+ _tied = true;
+ _value.string_val = value.clone();
+
+ if (use_default)
+ setStringValue(old_val);
+
+ return true;
}
/**
- * Untie a value from external functions.
- *
- * Will always attempt to intialize the internal value from
- * the getter before untying.
- *
- * Returns true on success (i.e. the value had been tied).
+ * Untie a value.
*/
bool
-FGValue::untie ()
+SGValue::untie ()
{
if (!_tied)
return false;
switch (_type) {
case BOOL: {
- bool value = getRawBool();
- _tied = false;
- setRawBool(value);
+ bool val = getBoolValue();
+ clear_value();
+ _value.bool_val = new SGRawValueInternal<bool>;
+ SET_BOOL(val);
break;
}
case INT: {
- int value = getRawInt();
- _tied = false;
- setRawInt(value);
+ int val = getIntValue();
+ clear_value();
+ _value.int_val = new SGRawValueInternal<int>;
+ SET_INT(val);
break;
}
case FLOAT: {
- float value = getRawFloat();
- _tied = false;
- setRawFloat(value);
+ float val = getFloatValue();
+ clear_value();
+ _value.float_val = new SGRawValueInternal<float>;
+ SET_FLOAT(val);
break;
}
case DOUBLE: {
- double value = getRawDouble();
- _tied = false;
- setRawDouble(value);
+ double val = getDoubleValue();
+ clear_value();
+ _value.double_val = new SGRawValueInternal<double>;
+ SET_DOUBLE(val);
break;
}
case STRING: {
- string value = getRawString();
- _tied = false;
- setRawString(value);
+ string val = getStringValue();
+ clear_value();
+ _value.string_val = new SGRawValueInternal<string>;
+ SET_STRING(val);
break;
}
}
+ _tied = false;
return true;
}
\f
////////////////////////////////////////////////////////////////////////
-// Implementation of FGPropertyList.
+// Implementation of SGPropertyNode.
////////////////////////////////////////////////////////////////////////
/**
- * Constructor.
+ * Default constructor: always creates a root node.
*/
-FGPropertyList::FGPropertyList ()
+SGPropertyNode::SGPropertyNode ()
+ : _value(0), _name(""), _index(0), _parent(0)
{
}
/**
- * Destructor.
+ * Convenience constructor.
*/
-FGPropertyList::~FGPropertyList ()
+SGPropertyNode::SGPropertyNode (const string &name,
+ int index, SGPropertyNode * parent)
+ : _value(0), _name(name), _index(index), _parent(parent)
{
}
+SGPropertyNode::~SGPropertyNode ()
+{
+ delete _value;
+ for (int i = 0; i < _children.size(); i++)
+ delete _children[i];
+}
-/**
- * Look up the FGValue structure associated with a property.
- *
- * Run some basic validity checks on the property name: it must
- * not be empty, must begin with '/', must never have two '//' in a row,
- * and must not end with '/'.
- */
-FGValue *
-FGPropertyList::getValue (const string &name, bool create = false)
-{
- const_iterator el = _props.find(name);
- if (el == _props.end()) {
- if (!create)
- return 0;
- else {
- FG_LOG(FG_GENERAL, FG_INFO, "Creating new property '" << name << '\'');
- if (name.size() == 0 ||
- name[0] != '/' ||
- name[name.size()-1] == '/' ||
- name.find("//") != string::npos) {
- FG_LOG(FG_GENERAL, FG_ALERT, "Illegal property name: '"
- << name << '\'');
- return 0;
- }
- }
- }
- return &(_props[name]);
+SGPropertyNode *
+SGPropertyNode::getChild (int position)
+{
+ if (position >= 0 && position < nChildren())
+ return _children[position];
+ else
+ return 0;
}
+const SGPropertyNode *
+SGPropertyNode::getChild (int position) const
+{
+ if (position >= 0 && position < nChildren())
+ return _children[position];
+ else
+ return 0;
+}
-/**
- * Look up a const value (never created).
- */
-const FGValue *
-FGPropertyList::getValue (const string &name) const
+SGPropertyNode *
+SGPropertyNode::getChild (const string &name, int index, bool create)
{
- value_map::const_iterator el = _props.find(name);
- if (el == _props.end())
+ int pos = find_child(name, index, _children);
+ if (pos >= 0) {
+ return getChild(pos);
+ } else if (create) {
+ _children.push_back(new SGPropertyNode(name, index, this));
+ return _children[_children.size()-1];
+ } else {
return 0;
+ }
+}
+
+const SGPropertyNode *
+SGPropertyNode::getChild (const string &name, int index) const
+{
+ int pos = find_child(name, index, _children);
+ if (pos >= 0)
+ _children[_children.size()-1];
else
- return &(el->second);
+ return 0;
}
+class CompareIndices
+{
+public:
+ int operator() (const SGPropertyNode * n1, const SGPropertyNode *n2) const {
+ return (n1->getIndex() < n2->getIndex());
+ }
+};
+
+
/**
- * Extract a boolean from the value.
- *
- * Note that this is inefficient for use in a tight loop: it is
- * better to get the FGValue and query it repeatedly.
+ * Get all children with the same name (but different indices).
*/
-bool
-FGPropertyList::getBoolValue (const string &name) const
+vector<SGPropertyNode *>
+SGPropertyNode::getChildren (const string &name)
{
- const FGValue * val = getValue(name);
- if (val == 0)
- return false;
- else
- return val->getBoolValue();
+ vector<SGPropertyNode *> children;
+ int max = _children.size();
+
+ for (int i = 0; i < max; i++)
+ if (_children[i]->getName() == name)
+ children.push_back(_children[i]);
+
+ sort(children.begin(), children.end(), CompareIndices());
+ return children;
}
/**
- * Extract an integer from the value.
- *
- * Note that this is inefficient for use in a tight loop: it is
- * better to get the FGValue and query it repeatedly.
+ * Get all children with the same name (but different indices).
*/
-int
-FGPropertyList::getIntValue (const string &name) const
+vector<const SGPropertyNode *>
+SGPropertyNode::getChildren (const string &name) const
{
- const FGValue * val = getValue(name);
- if (val == 0)
- return 0;
- else
- return val->getIntValue();
+ vector<const SGPropertyNode *> children;
+ int max = _children.size();
+
+ for (int i = 0; i < max; i++)
+ if (_children[i]->getName() == name)
+ children.push_back(_children[i]);
+
+ sort(children.begin(), children.end(), CompareIndices());
+ return children;
}
-/**
- * Extract a float from the value.
- *
- * Note that this is inefficient for use in a tight loop: it is
- * better to get the FGValue and query it repeatedly.
- */
-float
-FGPropertyList::getFloatValue (const string &name) const
+string
+SGPropertyNode::getPath (bool simplify) const
{
- const FGValue * val = getValue(name);
- if (val == 0)
- return 0.0;
+ if (_parent == 0)
+ return "";
+
+ string path = _parent->getPath(simplify);
+ path += '/';
+ path += _name;
+ if (_index != 0 || !simplify) {
+ char buffer[128];
+ sprintf(buffer, "[%d]", _index);
+ path += buffer;
+ }
+ return path;
+}
+
+SGValue::Type
+SGPropertyNode::getType () const
+{
+ if (_value != 0)
+ return _value->getType();
else
- return val->getFloatValue();
+ return SGValue::UNKNOWN;
}
+bool
+SGPropertyNode::getBoolValue () const
+{
+ return (_value == 0 ? SGRawValue<bool>::DefaultValue
+ : _value->getBoolValue());
+}
-/**
- * Extract a double from the value.
- *
- * Note that this is inefficient for use in a tight loop: it is
- * better to get the FGValue and query it repeatedly.
- */
-double
-FGPropertyList::getDoubleValue (const string &name) const
+int
+SGPropertyNode::getIntValue () const
{
- const FGValue * val = getValue(name);
- if (val == 0)
- return 0.0;
- else
- return val->getDoubleValue();
+ return (_value == 0 ? SGRawValue<int>::DefaultValue
+ : _value->getIntValue());
}
+float
+SGPropertyNode::getFloatValue () const
+{
+ return (_value == 0 ? SGRawValue<float>::DefaultValue
+ : _value->getFloatValue());
+}
-/**
- * Extract a string from the value.
- *
- * Note that this is inefficient for use in a tight loop: it is
- * better to save the FGValue and query it repeatedly.
- */
-const string &
-FGPropertyList::getStringValue (const string &name) const
+double
+SGPropertyNode::getDoubleValue () const
{
- const FGValue * val = getValue(name);
- if (val == 0)
- return empty_string;
- else
- return val->getStringValue();
+ return (_value == 0 ? SGRawValue<double>::DefaultValue
+ : _value->getDoubleValue());
}
+string
+SGPropertyNode::getStringValue () const
+{
+ return (_value == 0 ? SGRawValue<string>::DefaultValue
+ : _value->getStringValue());
+}
-/**
- * Assign a bool to the value and change the type if unknown.
- *
- * Note that this is inefficient for use in a tight loop: it is
- * better to save the FGValue and modify it repeatedly.
- *
- * Returns true on success.
- */
bool
-FGPropertyList::setBoolValue (const string &name, bool value)
+SGPropertyNode::setBoolValue (bool val)
{
- return getValue(name, true)->setBoolValue(value);
+ if (_value == 0)
+ _value = new SGValue();
+ return _value->setBoolValue(val);
}
+bool
+SGPropertyNode::setIntValue (int val)
+{
+ if (_value == 0)
+ _value = new SGValue();
+ return _value->setIntValue(val);
+}
-/**
- * Assign an integer to the value and change the type if unknown.
- *
- * Note that this is inefficient for use in a tight loop: it is
- * better to save the FGValue and modify it repeatedly.
- *
- * Returns true on success.
- */
bool
-FGPropertyList::setIntValue (const string &name, int value)
+SGPropertyNode::setFloatValue (float val)
{
- return getValue(name, true)->setIntValue(value);
+ if (_value == 0)
+ _value = new SGValue();
+ return _value->setFloatValue(val);
}
+bool
+SGPropertyNode::setDoubleValue (double val)
+{
+ if (_value == 0)
+ _value = new SGValue();
+ return _value->setDoubleValue(val);
+}
-/**
- * Assign a float to the value and change the type if unknown.
- *
- * Note that this is inefficient for use in a tight loop: it is
- * better to save the FGValue and modify it repeatedly.
- *
- * Returns true on success.
- */
bool
-FGPropertyList::setFloatValue (const string &name, float value)
+SGPropertyNode::setStringValue (string val)
{
- return getValue(name, true)->setFloatValue(value);
+ if (_value == 0)
+ _value = new SGValue();
+ return _value->setStringValue(val);
}
+bool
+SGPropertyNode::setUnknownValue (string val)
+{
+ if (_value == 0)
+ _value = new SGValue();
+ return _value->setUnknownValue(val);
+}
-/**
- * Assign a double to the value and change the type if unknown.
- *
- * Note that this is inefficient for use in a tight loop: it is
- * better to save the FGValue and modify it repeatedly.
- *
- * Returns true on success.
- */
bool
-FGPropertyList::setDoubleValue (const string &name, double value)
+SGPropertyNode::isTied () const
{
- return getValue(name, true)->setDoubleValue(value);
+ return (_value == 0 ? false : _value->isTied());
}
+bool
+SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
+{
+ return (_value == 0 ? false : _value->tie(rawValue, useDefault));
+}
-/**
- * Assign a string to the value and change the type if unknown.
- *
- * Note that this is inefficient for use in a tight loop: it is
- * better to save the FGValue and modify it repeatedly.
- *
- * Returns true on success.
- */
bool
-FGPropertyList::setStringValue (const string &name, const string &value)
+SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
{
- return getValue(name, true)->setStringValue(value);
+ return (_value == 0 ? false : _value->tie(rawValue, useDefault));
}
+bool
+SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
+{
+ return (_value == 0 ? false : _value->tie(rawValue, useDefault));
+}
+
+bool
+SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
+{
+ return (_value == 0 ? false : _value->tie(rawValue, useDefault));
+}
+
+bool
+SGPropertyNode::tie (const SGRawValue<string> &rawValue, bool useDefault)
+{
+ return (_value == 0 ? false : _value->tie(rawValue, useDefault));
+}
-/**
- * Assign a string to the value, but don't change the type.
- *
- * Note that this is inefficient for use in a tight loop: it is
- * better to save the FGValue and modify it repeatedly.
- *
- * Returns true on success.
- */
bool
-FGPropertyList::setUnknownValue (const string &name, const string &value)
+SGPropertyNode::untie ()
+{
+ return (_value == 0 ? false : _value->untie());
+}
+
+SGPropertyNode *
+SGPropertyNode::getRootNode ()
+{
+ if (_parent == 0)
+ return this;
+ else
+ return _parent->getRootNode();
+}
+
+const SGPropertyNode *
+SGPropertyNode::getRootNode () const
+{
+ if (_parent == 0)
+ return this;
+ else
+ return _parent->getRootNode();
+}
+
+SGPropertyNode *
+SGPropertyNode::getNode (const string &relative_path, bool create)
+{
+ vector<PathComponent> components;
+ parse_path(relative_path, components);
+ return find_node(this, components, 0, create);
+}
+
+const SGPropertyNode *
+SGPropertyNode::getNode (const string &relative_path) const
{
- return getValue(name, true)->setUnknownValue(value);
+ vector<PathComponent> components;
+ parse_path(relative_path, components);
+ // FIXME: cast away const
+ return find_node((SGPropertyNode *)this, components, 0, false);
}
+\f
+////////////////////////////////////////////////////////////////////////
+// Convenience methods using relative paths.
+////////////////////////////////////////////////////////////////////////
+
+
/**
- * Tie a boolean value to external functions.
- *
- * Invokes FGValue::tieBool
+ * Test whether another node has a value attached.
*/
bool
-FGPropertyList::tieBool (const string &name,
- bool_getter getter,
- bool_setter setter,
- bool useDefault = true)
+SGPropertyNode::hasValue (const string &relative_path) const
{
- FG_LOG(FG_GENERAL, FG_INFO, "Tying bool property '" << name << '\'');
- return getValue(name, true)->tieBool(getter, setter, useDefault);
+ const SGPropertyNode * node = getNode(relative_path);
+ return (node == 0 ? false : node->hasValue());
}
/**
- * Tie an integer value to external functions.
- *
- * Invokes FGValue::tieInt
+ * Get the value for another node.
*/
-bool
-FGPropertyList::tieInt (const string &name,
- int_getter getter,
- int_setter setter,
- bool useDefault = true)
+SGValue *
+SGPropertyNode::getValue (const string &relative_path, bool create)
{
- FG_LOG(FG_GENERAL, FG_INFO, "Tying int property '" << name << '\'');
- return getValue(name, true)->tieInt(getter, setter, useDefault);
+ SGPropertyNode * node = getNode(relative_path, create);
+ if (node != 0 && !node->hasValue())
+ node->setUnknownValue("");
+ return (node == 0 ? 0 : node->getValue());
}
/**
- * Tie a float value to external functions.
- *
- * Invokes FGValue::tieFloat
+ * Get the value for another node.
*/
-bool
-FGPropertyList::tieFloat (const string &name,
- float_getter getter,
- float_setter setter,
- bool useDefault = true)
+const SGValue *
+SGPropertyNode::getValue (const string &relative_path) const
{
- FG_LOG(FG_GENERAL, FG_INFO, "Tying float property '" << name << '\'');
- return getValue(name, true)->tieFloat(getter, setter, useDefault);
+ const SGPropertyNode * node = getNode(relative_path);
+ return (node == 0 ? 0 : node->getValue());
}
/**
- * Tie a double value to external functions.
- *
- * Invokes FGValue::tieDouble
+ * Get the value type for another node.
*/
-bool
-FGPropertyList::tieDouble (const string &name,
- double_getter getter,
- double_setter setter,
- bool useDefault = true)
+SGValue::Type
+SGPropertyNode::getType (const string &relative_path) const
{
- FG_LOG(FG_GENERAL, FG_INFO, "Tying double property '" << name << '\'');
- return getValue(name, true)->tieDouble(getter, setter, useDefault);
+ const SGPropertyNode * node = getNode(relative_path);
+ return (node == 0 ? SGValue::UNKNOWN : node->getType());
}
/**
- * Tie a string value to external functions.
- *
- * Invokes FGValue::tieString
+ * Get a bool value for another node.
*/
bool
-FGPropertyList::tieString (const string &name,
- string_getter getter,
- string_setter setter,
- bool useDefault = true)
+SGPropertyNode::getBoolValue (const string &relative_path,
+ bool defaultValue) const
{
- FG_LOG(FG_GENERAL, FG_INFO, "Tying string property '" << name << '\'');
- return getValue(name, true)->tieString(getter, setter, useDefault);
+ const SGPropertyNode * node = getNode(relative_path);
+ return (node == 0 ? defaultValue : node->getBoolValue());
}
/**
- * Untie a value from external functions.
- *
- * Invokes FGValue::untie
+ * Get an int value for another node.
*/
-bool
-FGPropertyList::untie (const string &name)
+int
+SGPropertyNode::getIntValue (const string &relative_path,
+ int defaultValue) const
{
- FG_LOG(FG_GENERAL, FG_INFO, "Untying property '" << name << '\'');
- return getValue(name, true)->untie();
+ const SGPropertyNode * node = getNode(relative_path);
+ return (node == 0 ? defaultValue : node->getIntValue());
}
-\f
-////////////////////////////////////////////////////////////////////////
-// Implementation of FGPropertyNode.
-////////////////////////////////////////////////////////////////////////
+/**
+ * Get a float value for another node.
+ */
+float
+SGPropertyNode::getFloatValue (const string &relative_path,
+ float defaultValue) const
+{
+ const SGPropertyNode * node = getNode(relative_path);
+ return (node == 0 ? defaultValue : node->getFloatValue());
+}
/**
- * Extract the base name of the next level down from the parent.
- *
- * The parent must have a '/' appended. Note that basename may
- * be modified even if the test fails.
+ * Get a double value for another node.
*/
-static bool
-get_base (const string &parent, const string &child,
- string &basename)
-{
- // First, check that the parent name
- // is a prefix of the child name, and
- // extract the remainder
- if (child.find(parent) != 0)
- return false;
+double
+SGPropertyNode::getDoubleValue (const string &relative_path,
+ double defaultValue) const
+{
+ const SGPropertyNode * node = getNode(relative_path);
+ return (node == 0 ? defaultValue : node->getDoubleValue());
+}
- basename = child.substr(parent.size());
- string::size_type pos = basename.find('/');
- if (pos != string::npos) {
- basename.resize(pos);
- }
+/**
+ * Get a string value for another node.
+ */
+string
+SGPropertyNode::getStringValue (const string &relative_path,
+ string defaultValue) const
+{
+ const SGPropertyNode * node = getNode(relative_path);
+ return (node == 0 ? defaultValue : node->getStringValue());
+}
- if (basename.size() == 0)
- return false;
- else
- return true;
+
+/**
+ * Set a bool value for another node.
+ */
+bool
+SGPropertyNode::setBoolValue (const string &relative_path, bool value)
+{
+ return getNode(relative_path, true)->setBoolValue(value);
}
/**
- * Constructor.
+ * Set an int value for another node.
*/
-FGPropertyNode::FGPropertyNode (const string &path = "",
- FGPropertyList * props = 0)
- : _props(props)
+bool
+SGPropertyNode::setIntValue (const string &relative_path, int value)
{
- setPath(path);
+ return getNode(relative_path, true)->setIntValue(value);
}
/**
- * Destructor.
+ * Set a float value for another node.
*/
-FGPropertyNode::~FGPropertyNode ()
+bool
+SGPropertyNode::setFloatValue (const string &relative_path, float value)
{
+ return getNode(relative_path, true)->setFloatValue(value);
}
/**
- * Set the path.
- *
- * Strip the trailing '/', if any.
+ * Set a double value for another node.
*/
-void
-FGPropertyNode::setPath (const string &path)
+bool
+SGPropertyNode::setDoubleValue (const string &relative_path, double value)
{
- _path = path;
+ return getNode(relative_path, true)->setDoubleValue(value);
+}
+
- // Chop the final '/', if present.
- if (_path.size() > 0 && _path[_path.size()-1] == '/')
- _path.resize(_path.size()-1);
+/**
+ * Set a string value for another node.
+ */
+bool
+SGPropertyNode::setStringValue (const string &relative_path, string value)
+{
+ return getNode(relative_path, true)->setStringValue(value);
}
/**
- * Return the local name of the property.
- *
- * The local name is just everything after the last slash.
+ * Set an unknown value for another node.
*/
-const string &
-FGPropertyNode::getName () const
+bool
+SGPropertyNode::setUnknownValue (const string &relative_path, string value)
{
- string::size_type pos = _path.rfind('/');
- if (pos != string::npos) {
- _name = _path.substr(pos+1);
- return _name;
- } else {
- return empty_string;
- }
+ return getNode(relative_path, true)->setUnknownValue(value);
}
/**
- * Return the value of the current node.
- *
- * Currently, this does a lookup each time, but we could cache the
- * value safely as long as it's non-zero.
- *
- * Note that this will not create the value if it doesn't already exist.
+ * Test whether another node is tied.
*/
-FGValue *
-FGPropertyNode::getValue ()
+bool
+SGPropertyNode::isTied (const string &relative_path) const
{
- if (_props == 0 || _path.size() == 0)
- return 0;
- else
- return _props->getValue(_path);
+ const SGPropertyNode * node = getNode(relative_path);
+ return (node == 0 ? false : node->isTied());
}
/**
- * Return the number of children for the current node.
+ * Tie a node reached by a relative path, creating it if necessary.
*/
-int
-FGPropertyNode::size () const
+bool
+SGPropertyNode::tie (const string &relative_path,
+ const SGRawValue<bool> &rawValue,
+ bool useDefault = true)
{
- if (_props == 0)
- return 0;
+ return getNode(relative_path, true)->tie(rawValue, useDefault);
+}
- int s = 0;
- string base;
- string lastBase;
- string pattern = _path;
- pattern += '/';
+/**
+ * Tie a node reached by a relative path, creating it if necessary.
+ */
+bool
+SGPropertyNode::tie (const string &relative_path,
+ const SGRawValue<int> &rawValue,
+ bool useDefault = true)
+{
+ return getNode(relative_path, true)->tie(rawValue, useDefault);
+}
- FGPropertyList::const_iterator it = _props->begin();
- FGPropertyList::const_iterator end = _props->end();
- while (it != end) {
- if (get_base(pattern, it->first, base) && base != lastBase) {
- s++;
- lastBase = base;
- }
- it++;
- }
- return s;
+/**
+ * Tie a node reached by a relative path, creating it if necessary.
+ */
+bool
+SGPropertyNode::tie (const string &relative_path,
+ const SGRawValue<float> &rawValue,
+ bool useDefault = true)
+{
+ return getNode(relative_path, true)->tie(rawValue, useDefault);
}
/**
- * Initialize a node to represent this node's parent.
- *
- * A return value of true means success; otherwise, the node supplied
- * is unmodified.
+ * Tie a node reached by a relative path, creating it if necessary.
*/
bool
-FGPropertyNode::getParent (FGPropertyNode &parent) const
+SGPropertyNode::tie (const string &relative_path,
+ const SGRawValue<double> &rawValue,
+ bool useDefault = true)
{
- string::size_type pos = _path.rfind('/');
- if (pos != string::npos) {
- parent.setPath(_path.substr(0, pos-1));
- parent.setPropertyList(_props);
- return true;
- } else {
- return false;
- }
+ return getNode(relative_path, true)->tie(rawValue, useDefault);
}
/**
- * Initialize a node to represent this node's nth child.
- *
- * A return value of true means success; otherwise, the node supplied
- * is unmodified.
+ * Tie a node reached by a relative path, creating it if necessary.
*/
bool
-FGPropertyNode::getChild (FGPropertyNode &child, int n) const
+SGPropertyNode::tie (const string &relative_path,
+ const SGRawValue<string> &rawValue,
+ bool useDefault = true)
{
- if (_props == 0)
- return false;
+ return getNode(relative_path, true)->tie(rawValue, useDefault);
+}
- int s = 0;
- string base;
- string lastBase;
- string pattern = _path;
- pattern += '/';
-
- FGPropertyList::const_iterator it = _props->begin();
- FGPropertyList::const_iterator end = _props->end();
- while (it != end) {
- if (get_base(pattern, it->first, base) && base != lastBase) {
- if (s == n) {
- string path = _path;
- path += '/';
- path += base;
- child.setPath(path);
- child.setPropertyList(_props);
- return true;
- } else {
- s++;
- lastBase = base;
- }
- }
- it++;
- }
- return false;
+/**
+ * Attempt to untie another node reached by a relative path.
+ */
+bool
+SGPropertyNode::untie (const string &relative_path)
+{
+ SGPropertyNode * node = getNode(relative_path);
+ return (node == 0 ? false : node->untie());
}
// end of props.cxx