X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fmisc%2Fprops.cxx;h=8fbbbd585f7952ec14bcca81b45057203516b854;hb=101fdb359871a14726a83cdb5a8bb2c0f6ee5af1;hp=5c835c50cb84f8e82554af7ef6d6b5ddf3c3ddef;hpb=d063b319e8084847bcec80f3778f5493d356227c;p=simgear.git diff --git a/simgear/misc/props.cxx b/simgear/misc/props.cxx index 5c835c50..8fbbbd58 100644 --- a/simgear/misc/props.cxx +++ b/simgear/misc/props.cxx @@ -6,16 +6,28 @@ // // $Id$ -#include +#include "props.hxx" + +#if PROPS_STANDALONE + +#include +using std::cerr; +using std::endl; +using std::sort; + +#else -#include -#include -#include STL_IOSTREAM #include -#include "props.hxx" +#include +#include SG_USING_STD(sort); +#endif + +#include +#include + //////////////////////////////////////////////////////////////////////// @@ -42,20 +54,6 @@ public: #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt #define TEST_WRITE if (!getAttribute(WRITE)) return false -#define GET_BOOL (_value.bool_val->getValue()) -#define GET_INT (_value.int_val->getValue()) -#define GET_LONG (_value.long_val->getValue()) -#define GET_FLOAT (_value.float_val->getValue()) -#define GET_DOUBLE (_value.double_val->getValue()) -#define GET_STRING (_value.string_val->getValue()) - -#define SET_BOOL(val) (_value.bool_val->setValue(val)) -#define SET_INT(val) (_value.int_val->setValue(val)) -#define SET_LONG(val) (_value.long_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)) - //////////////////////////////////////////////////////////////////////// @@ -67,7 +65,7 @@ const int SGRawValue::DefaultValue = 0; const long SGRawValue::DefaultValue = 0L; const float SGRawValue::DefaultValue = 0.0; const double SGRawValue::DefaultValue = 0.0L; -const string SGRawValue::DefaultValue = ""; +const char * const SGRawValue::DefaultValue = ""; @@ -89,7 +87,7 @@ struct PathComponent * * Name: [_a-zA-Z][-._a-zA-Z0-9]* */ -static inline string +static inline const string parse_name (const string &path, int &i) { string name = ""; @@ -217,16 +215,34 @@ parse_path (const string &path, vector &components) //////////////////////////////////////////////////////////////////////// +static const char * +copy_string (const char * s) +{ + // FIXME: potential buffer overflow. + // For some reason, strnlen and + // strncpy cause all kinds of crashes. + string str = s; + char * copy = new char[str.size() + 1]; + strcpy(copy, str.c_str()); + return copy; +} + +static bool +compare_strings (const char * s1, const char * s2) +{ + return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN); +} + /** * Locate a child node by name and index. */ static int -find_child (const string &name, int index, vector nodes) +find_child (const char * name, int index, vector nodes) { int nNodes = nodes.size(); for (int i = 0; i < nNodes; i++) { SGPropertyNode * node = nodes[i]; - if (node->getName() == name && node->getIndex() == index) + if (compare_strings(node->getName(), name) && node->getIndex() == index) return i; } return -1; @@ -274,7 +290,7 @@ find_node (SGPropertyNode * current, // Otherwise, a child name else { SGPropertyNode * child = - current->getChild(components[position].name, + current->getChild(components[position].name.c_str(), components[position].index, create); return find_node(child, components, position + 1, create); @@ -284,124 +300,388 @@ find_node (SGPropertyNode * current, //////////////////////////////////////////////////////////////////////// -// Implementation of SGPropertyNode. +// Private methods from SGPropertyNode (may be inlined for speed). //////////////////////////////////////////////////////////////////////// +inline bool +SGPropertyNode::get_bool () const +{ + if (_tied) + return _value.bool_val->getValue(); + else + return _local_val.bool_val; +} -/** - * Default constructor: always creates a root node. - */ -SGPropertyNode::SGPropertyNode () - : _name(""), - _index(0), - _parent(0), - _type(NONE), - _tied(false), - _attr(READ|WRITE|ARCHIVE) +inline int +SGPropertyNode::get_int () const { + if (_tied) + return _value.int_val->getValue(); + else + return _local_val.int_val; } +inline long +SGPropertyNode::get_long () const +{ + if (_tied) + return _value.long_val->getValue(); + else + return _local_val.long_val; +} -/** - * Copy constructor. - */ -SGPropertyNode::SGPropertyNode (const SGPropertyNode &node) - : _name(node._name), - _index(node._index), - _parent(0), // don't copy the parent - _type(node._type), - _tied(node._tied), - _attr(node._attr) +inline float +SGPropertyNode::get_float () const +{ + if (_tied) + return _value.float_val->getValue(); + else + return _local_val.float_val; +} + +inline double +SGPropertyNode::get_double () const +{ + if (_tied) + return _value.double_val->getValue(); + else + return _local_val.double_val; +} + +inline const char * +SGPropertyNode::get_string () const +{ + if (_tied) + return _value.string_val->getValue(); + else + return _local_val.string_val; +} + +inline bool +SGPropertyNode::set_bool (bool val) +{ + if (_tied) { + return _value.bool_val->setValue(val); + } else { + _local_val.bool_val = val; + return true; + } +} + +inline bool +SGPropertyNode::set_int (int val) +{ + if (_tied) { + return _value.int_val->setValue(val); + } else { + _local_val.int_val = val; + return true; + } +} + +inline bool +SGPropertyNode::set_long (long val) +{ + if (_tied) { + return _value.long_val->setValue(val); + } else { + _local_val.long_val = val; + return true; + } +} + +inline bool +SGPropertyNode::set_float (float val) +{ + if (_tied) { + return _value.float_val->setValue(val); + } else { + _local_val.float_val = val; + return true; + } +} + +inline bool +SGPropertyNode::set_double (double val) +{ + if (_tied) { + return _value.double_val->setValue(val); + } else { + _local_val.double_val = val; + return true; + } +} + +inline bool +SGPropertyNode::set_string (const char * val) +{ + if (_tied) { + return _value.string_val->setValue(val); + } else { + delete (char *)_local_val.string_val; + _local_val.string_val = copy_string(val); + return true; + } +} + +void +SGPropertyNode::clear_value () { switch (_type) { case NONE: break; case ALIAS: - _value.alias = node._value.alias; + _value.alias = 0; break; case BOOL: - _value.bool_val = node._value.bool_val->clone(); + if (_tied) { + delete _value.bool_val; + _value.bool_val = 0; + } + _local_val.bool_val = SGRawValue::DefaultValue; break; case INT: - _value.int_val = node._value.int_val->clone(); + if (_tied) { + delete _value.int_val; + _value.int_val = 0; + } + _local_val.int_val = SGRawValue::DefaultValue; break; case LONG: - _value.long_val = node._value.long_val->clone(); + if (_tied) { + delete _value.long_val; + _value.long_val = 0L; + } + _local_val.long_val = SGRawValue::DefaultValue; break; case FLOAT: - _value.float_val = node._value.float_val->clone(); + if (_tied) { + delete _value.float_val; + _value.float_val = 0; + } + _local_val.float_val = SGRawValue::DefaultValue; break; case DOUBLE: - _value.double_val = node._value.double_val->clone(); + if (_tied) { + delete _value.double_val; + _value.double_val = 0; + } + _local_val.double_val = SGRawValue::DefaultValue; break; case STRING: case UNSPECIFIED: - _value.string_val = node._value.string_val->clone(); + if (_tied) { + delete _value.string_val; + _value.string_val = 0; + } else { + delete (char *)_local_val.string_val; + } + _local_val.string_val = 0; break; } + _tied = false; + _type = NONE; } /** - * Convenience constructor. + * Get the value as a string. */ -SGPropertyNode::SGPropertyNode (const string &name, - int index, SGPropertyNode * parent) - : _name(name), _index(index), _parent(parent), _type(NONE), - _tied(false), _attr(READ|WRITE|ARCHIVE) +const char * +SGPropertyNode::make_string () const { + if (!getAttribute(READ)) + return ""; + + switch (_type) { + case ALIAS: + return _value.alias->getStringValue(); + case BOOL: + if (get_bool()) + return "true"; + else + return "false"; + case INT: + sprintf(_buffer, "%d", get_int()); + return _buffer; + case LONG: + sprintf(_buffer, "%ld", get_long()); + return _buffer; + case FLOAT: + sprintf(_buffer, "%f", get_float()); + return _buffer; + case DOUBLE: + sprintf(_buffer, "%f", get_double()); + return _buffer; + case STRING: + case UNSPECIFIED: + return get_string(); + case NONE: + default: + return ""; + } } +/** + * Trace a write access for a property. + */ +void +SGPropertyNode::trace_write () const +{ +#if PROPS_STANDALONE + cerr << "TRACE: Write node " << getPath () << ", value\"" + << make_string() << '"' << endl; +#else + SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Write node " << getPath() + << ", value\"" << make_string() << '"'); +#endif +} /** - * Destructor. + * Trace a read access for a property. */ -SGPropertyNode::~SGPropertyNode () +void +SGPropertyNode::trace_read () const { - for (int i = 0; i < (int)_children.size(); i++) - delete _children[i]; - clear_value(); +#if PROPS_STANDALONE + cerr << "TRACE: Write node " << getPath () << ", value \"" + << make_string() << '"' << endl; +#else + SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Read node " << getPath() + << ", value \"" << make_string() << '"'); +#endif } + +//////////////////////////////////////////////////////////////////////// +// Public methods from SGPropertyNode. +//////////////////////////////////////////////////////////////////////// + /** - * Delete and clear the current value. + * Default constructor: always creates a root node. */ -void -SGPropertyNode::clear_value () +SGPropertyNode::SGPropertyNode () + : _name(copy_string("")), + _index(0), + _parent(0), + _path_cache(0), + _type(NONE), + _tied(false), + _attr(READ|WRITE) { + _local_val.string_val = 0; +} + + +/** + * Copy constructor. + */ +SGPropertyNode::SGPropertyNode (const SGPropertyNode &node) + : _index(node._index), + _parent(0), // don't copy the parent + _path_cache(0), + _type(node._type), + _tied(node._tied), + _attr(node._attr) +{ + _name = copy_string(node._name); + _local_val.string_val = 0; switch (_type) { case NONE: + break; case ALIAS: - _value.alias = 0; + _value.alias = node._value.alias; + _tied = false; break; case BOOL: - delete _value.bool_val; - _value.bool_val = 0; + if (_tied) { + _tied = true; + _value.bool_val = node._value.bool_val->clone(); + } else { + _tied = false; + set_bool(node.get_bool()); + } break; case INT: - delete _value.int_val; - _value.int_val = 0; + if (_tied) { + _tied = true; + _value.int_val = node._value.int_val->clone(); + } else { + _tied = false; + set_int(node.get_int()); + } break; case LONG: - delete _value.long_val; - _value.long_val = 0L; + if (_tied) { + _tied = true; + _value.long_val = node._value.long_val->clone(); + } else { + _tied = false; + set_long(node.get_long()); + } break; case FLOAT: - delete _value.float_val; - _value.float_val = 0; + if (_tied) { + _tied = true; + _value.float_val = node._value.float_val->clone(); + } else { + _tied = false; + set_float(node.get_float()); + } break; case DOUBLE: - delete _value.double_val; - _value.double_val = 0; + if (_tied) { + _tied = true; + _value.double_val = node._value.double_val->clone(); + } else { + _tied = false; + set_double(node.get_double()); + } break; case STRING: case UNSPECIFIED: - delete _value.string_val; - _value.string_val = 0; + if (_tied) { + _tied = true; + _value.string_val = node._value.string_val->clone(); + } else { + _tied = false; + set_string(node.get_string()); + } break; } - _type = NONE; +} + + +/** + * Convenience constructor. + */ +SGPropertyNode::SGPropertyNode (const char * name, + int index, + SGPropertyNode * parent) + : _index(index), + _parent(parent), + _path_cache(0), + _type(NONE), + _tied(false), + _attr(READ|WRITE) +{ + _name = copy_string(name); + _local_val.string_val = 0; +} + + +/** + * Destructor. + */ +SGPropertyNode::~SGPropertyNode () +{ + delete (char *)_name; + for (int i = 0; i < (int)_children.size(); i++) { + delete _children[i]; + } +// delete _path_cache; + clear_value(); } @@ -424,7 +704,7 @@ SGPropertyNode::alias (SGPropertyNode * target) * Alias to another node by path. */ bool -SGPropertyNode::alias (const string &path) +SGPropertyNode::alias (const char * path) { return alias(getNode(path, true)); } @@ -491,7 +771,7 @@ SGPropertyNode::getChild (int position) const * Get a non-const child by name and index, creating if necessary. */ SGPropertyNode * -SGPropertyNode::getChild (const string &name, int index, bool create) +SGPropertyNode::getChild (const char * name, int index, bool create) { int pos = find_child(name, index, _children); if (pos >= 0) { @@ -509,7 +789,7 @@ SGPropertyNode::getChild (const string &name, int index, bool create) * Get a const child by name and index. */ const SGPropertyNode * -SGPropertyNode::getChild (const string &name, int index) const +SGPropertyNode::getChild (const char * name, int index) const { int pos = find_child(name, index, _children); if (pos >= 0) @@ -523,13 +803,13 @@ SGPropertyNode::getChild (const string &name, int index) const * Get all children with the same name (but different indices). */ vector -SGPropertyNode::getChildren (const string &name) +SGPropertyNode::getChildren (const char * name) { vector children; int max = _children.size(); for (int i = 0; i < max; i++) - if (_children[i]->getName() == name) + if (compare_strings(_children[i]->getName(), name)) children.push_back(_children[i]); sort(children.begin(), children.end(), CompareIndices()); @@ -541,13 +821,13 @@ SGPropertyNode::getChildren (const string &name) * Get all children const with the same name (but different indices). */ vector -SGPropertyNode::getChildren (const string &name) const +SGPropertyNode::getChildren (const char * name) const { vector children; int max = _children.size(); for (int i = 0; i < max; i++) - if (_children[i]->getName() == name) + if (compare_strings(_children[i]->getName(), name)) children.push_back(_children[i]); sort(children.begin(), children.end(), CompareIndices()); @@ -555,7 +835,7 @@ SGPropertyNode::getChildren (const string &name) const } -string +const char * SGPropertyNode::getPath (bool simplify) const { if (_parent == 0) @@ -569,7 +849,7 @@ SGPropertyNode::getPath (bool simplify) const sprintf(buffer, "[%d]", _index); path += buffer; } - return path; + return path.c_str(); } SGPropertyNode::Type @@ -585,383 +865,525 @@ SGPropertyNode::getType () const bool SGPropertyNode::getBoolValue () const { - TEST_READ(false); + // Shortcut for common case + if (_attr == (READ|WRITE) && _type == BOOL) + return get_bool(); + + if (getAttribute(TRACE_READ)) + trace_read(); + if (!getAttribute(READ)) + return SGRawValue::DefaultValue; switch (_type) { case ALIAS: return _value.alias->getBoolValue(); case BOOL: - return GET_BOOL; + return get_bool(); case INT: - return GET_INT == 0 ? false : true; + return get_int() == 0 ? false : true; case LONG: - return GET_LONG == 0L ? false : true; + return get_long() == 0L ? false : true; case FLOAT: - return GET_FLOAT == 0.0 ? false : true; + return get_float() == 0.0 ? false : true; case DOUBLE: - return GET_DOUBLE == 0.0L ? false : true; + return get_double() == 0.0L ? false : true; case STRING: case UNSPECIFIED: - return (GET_STRING == "true" || getDoubleValue() != 0.0L); + return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L); + case NONE: + default: + return SGRawValue::DefaultValue; } - - return false; // if NONE } int SGPropertyNode::getIntValue () const { - TEST_READ(0); + // Shortcut for common case + if (_attr == (READ|WRITE) && _type == INT) + return get_int(); + + if (getAttribute(TRACE_READ)) + trace_read(); + if (!getAttribute(READ)) + return SGRawValue::DefaultValue; switch (_type) { case ALIAS: return _value.alias->getIntValue(); case BOOL: - return int(GET_BOOL); + return int(get_bool()); case INT: - return GET_INT; + return get_int(); case LONG: - return int(GET_LONG); + return int(get_long()); case FLOAT: - return int(GET_FLOAT); + return int(get_float()); case DOUBLE: - return int(GET_DOUBLE); + return int(get_double()); case STRING: case UNSPECIFIED: - return atoi(GET_STRING.c_str()); + return atoi(get_string()); + case NONE: + default: + return SGRawValue::DefaultValue; } - - return 0; // if NONE } long SGPropertyNode::getLongValue () const { - TEST_READ(0L); + // Shortcut for common case + if (_attr == (READ|WRITE) && _type == LONG) + return get_long(); + + if (getAttribute(TRACE_READ)) + trace_read(); + if (!getAttribute(READ)) + return SGRawValue::DefaultValue; switch (_type) { case ALIAS: return _value.alias->getLongValue(); case BOOL: - return long(GET_BOOL); + return long(get_bool()); case INT: - return long(GET_INT); + return long(get_int()); case LONG: - return GET_LONG; + return get_long(); case FLOAT: - return long(GET_FLOAT); + return long(get_float()); case DOUBLE: - return long(GET_DOUBLE); + return long(get_double()); case STRING: case UNSPECIFIED: - return strtol(GET_STRING.c_str(), 0, 0); + return strtol(get_string(), 0, 0); + case NONE: + default: + return SGRawValue::DefaultValue; } - - return 0L; // if NONE } float SGPropertyNode::getFloatValue () const { - TEST_READ(0.0); + // Shortcut for common case + if (_attr == (READ|WRITE) && _type == FLOAT) + return get_float(); + + if (getAttribute(TRACE_READ)) + trace_read(); + if (!getAttribute(READ)) + return SGRawValue::DefaultValue; switch (_type) { case ALIAS: return _value.alias->getFloatValue(); case BOOL: - return float(GET_BOOL); + return float(get_bool()); case INT: - return float(GET_INT); + return float(get_int()); case LONG: - return float(GET_LONG); + return float(get_long()); case FLOAT: - return GET_FLOAT; + return get_float(); case DOUBLE: - return float(GET_DOUBLE); + return float(get_double()); case STRING: case UNSPECIFIED: - return atof(GET_STRING.c_str()); + return atof(get_string()); + case NONE: + default: + return SGRawValue::DefaultValue; } - - return 0.0; // if NONE } double SGPropertyNode::getDoubleValue () const { - TEST_READ(0.0L); + // Shortcut for common case + if (_attr == (READ|WRITE) && _type == DOUBLE) + return get_double(); + + if (getAttribute(TRACE_READ)) + trace_read(); + if (!getAttribute(READ)) + return SGRawValue::DefaultValue; + switch (_type) { case ALIAS: return _value.alias->getDoubleValue(); case BOOL: - return double(GET_BOOL); + return double(get_bool()); case INT: - return double(GET_INT); + return double(get_int()); case LONG: - return double(GET_LONG); + return double(get_long()); case FLOAT: - return double(GET_FLOAT); + return double(get_float()); case DOUBLE: - return GET_DOUBLE; + return get_double(); case STRING: case UNSPECIFIED: - return strtod(GET_STRING.c_str(), 0); + return strtod(get_string(), 0); + case NONE: + default: + return SGRawValue::DefaultValue; } - - return 0.0L; // if NONE } -string +const char * SGPropertyNode::getStringValue () const { - TEST_READ(""); - char buf[128]; - - switch (_type) { - case ALIAS: - return _value.alias->getStringValue(); - case BOOL: - if (GET_BOOL) - return "true"; - else - return "false"; - case INT: - sprintf(buf, "%d", GET_INT); - return buf; - case LONG: - sprintf(buf, "%ld", GET_LONG); - return buf; - case FLOAT: - sprintf(buf, "%f", GET_FLOAT); - return buf; - case DOUBLE: - sprintf(buf, "%f", GET_DOUBLE); - return buf; - case STRING: - case UNSPECIFIED: - return GET_STRING; - } + // Shortcut for common case + if (_attr == (READ|WRITE) && _type == STRING) + return get_string(); - return ""; // if NONE + if (getAttribute(TRACE_READ)) + trace_read(); + if (!getAttribute(READ)) + return SGRawValue::DefaultValue; + return make_string(); } bool SGPropertyNode::setBoolValue (bool value) { + // Shortcut for common case + if (_attr == (READ|WRITE) && _type == BOOL) + return set_bool(value); + + bool result = false; TEST_WRITE; if (_type == NONE || _type == UNSPECIFIED) { clear_value(); - _value.bool_val = new SGRawValueInternal; + _tied = false; _type = BOOL; } switch (_type) { case ALIAS: - return _value.alias->setBoolValue(value); + result = _value.alias->setBoolValue(value); + break; case BOOL: - return SET_BOOL(value); + result = set_bool(value); + break; case INT: - return SET_INT(int(value)); + result = set_int(int(value)); + break; case LONG: - return SET_LONG(long(value)); + result = set_long(long(value)); + break; case FLOAT: - return SET_FLOAT(float(value)); + result = set_float(float(value)); + break; case DOUBLE: - return SET_DOUBLE(double(value)); + result = set_double(double(value)); + break; case STRING: - return SET_STRING(value ? "true" : "false"); + case UNSPECIFIED: + result = set_string(value ? "true" : "false"); + break; + case NONE: + default: + break; } - return false; // should never happen + if (getAttribute(TRACE_WRITE)) + trace_write(); + return result; } bool SGPropertyNode::setIntValue (int value) { + // Shortcut for common case + if (_attr == (READ|WRITE) && _type == INT) + return set_int(value); + + bool result = false; TEST_WRITE; if (_type == NONE || _type == UNSPECIFIED) { clear_value(); - _value.int_val = new SGRawValueInternal; _type = INT; + _local_val.int_val = 0; } switch (_type) { case ALIAS: - return _value.alias->setIntValue(value); + result = _value.alias->setIntValue(value); + break; case BOOL: - return SET_BOOL(value == 0 ? false : true); + result = set_bool(value == 0 ? false : true); + break; case INT: - return SET_INT(value); + result = set_int(value); + break; case LONG: - return SET_LONG(long(value)); + result = set_long(long(value)); + break; case FLOAT: - return SET_FLOAT(float(value)); + result = set_float(float(value)); + break; case DOUBLE: - return SET_DOUBLE(double(value)); - case STRING: { + result = set_double(double(value)); + break; + case STRING: + case UNSPECIFIED: { char buf[128]; sprintf(buf, "%d", value); - return SET_STRING(buf); + result = set_string(buf); + break; } + case NONE: + default: + break; } - return false; // should never happen + if (getAttribute(TRACE_WRITE)) + trace_write(); + return result; } bool SGPropertyNode::setLongValue (long value) { + // Shortcut for common case + if (_attr == (READ|WRITE) && _type == LONG) + return set_long(value); + + bool result = false; TEST_WRITE; if (_type == NONE || _type == UNSPECIFIED) { clear_value(); - _value.long_val = new SGRawValueInternal; _type = LONG; + _local_val.long_val = 0L; } switch (_type) { case ALIAS: - return _value.alias->setLongValue(value); + result = _value.alias->setLongValue(value); + break; case BOOL: - return SET_BOOL(value == 0L ? false : true); + result = set_bool(value == 0L ? false : true); + break; case INT: - return SET_INT(int(value)); + result = set_int(int(value)); + break; case LONG: - return SET_LONG(value); + result = set_long(value); + break; case FLOAT: - return SET_FLOAT(float(value)); + result = set_float(float(value)); + break; case DOUBLE: - return SET_DOUBLE(double(value)); - case STRING: { + result = set_double(double(value)); + break; + case STRING: + case UNSPECIFIED: { char buf[128]; - sprintf(buf, "%d", value); - return SET_STRING(buf); + sprintf(buf, "%ld", value); + result = set_string(buf); + break; } + case NONE: + default: + break; } - return false; // should never happen + if (getAttribute(TRACE_WRITE)) + trace_write(); + return result; } bool SGPropertyNode::setFloatValue (float value) { + // Shortcut for common case + if (_attr == (READ|WRITE) && _type == FLOAT) + return set_float(value); + + bool result = false; TEST_WRITE; if (_type == NONE || _type == UNSPECIFIED) { clear_value(); - _value.float_val = new SGRawValueInternal; _type = FLOAT; + _local_val.float_val = 0; } switch (_type) { case ALIAS: - return _value.alias->setFloatValue(value); + result = _value.alias->setFloatValue(value); + break; case BOOL: - return SET_BOOL(value == 0.0 ? false : true); + result = set_bool(value == 0.0 ? false : true); + break; case INT: - return SET_INT(int(value)); + result = set_int(int(value)); + break; case LONG: - return SET_LONG(long(value)); + result = set_long(long(value)); + break; case FLOAT: - return SET_FLOAT(value); + result = set_float(value); + break; case DOUBLE: - return SET_DOUBLE(double(value)); - case STRING: { + result = set_double(double(value)); + break; + case STRING: + case UNSPECIFIED: { char buf[128]; sprintf(buf, "%f", value); - return SET_STRING(buf); + result = set_string(buf); + break; } + case NONE: + default: + break; } - return false; // should never happen + if (getAttribute(TRACE_WRITE)) + trace_write(); + return result; } bool SGPropertyNode::setDoubleValue (double value) { + // Shortcut for common case + if (_attr == (READ|WRITE) && _type == DOUBLE) + return set_double(value); + + bool result = false; TEST_WRITE; if (_type == NONE || _type == UNSPECIFIED) { clear_value(); - _value.double_val = new SGRawValueInternal; + _local_val.double_val = value; _type = DOUBLE; } switch (_type) { case ALIAS: - return _value.alias->setDoubleValue(value); + result = _value.alias->setDoubleValue(value); + break; case BOOL: - return SET_BOOL(value == 0.0L ? false : true); + result = set_bool(value == 0.0L ? false : true); + break; case INT: - return SET_INT(int(value)); + result = set_int(int(value)); + break; case LONG: - return SET_LONG(long(value)); + result = set_long(long(value)); + break; case FLOAT: - return SET_FLOAT(float(value)); + result = set_float(float(value)); + break; case DOUBLE: - return SET_DOUBLE(value); - case STRING: { + result = set_double(value); + break; + case STRING: + case UNSPECIFIED: { char buf[128]; - sprintf(buf, "%lf", value); - return SET_STRING(buf); + sprintf(buf, "%f", value); + result = set_string(buf); + break; } + case NONE: + default: + break; } - return false; // should never happen + if (getAttribute(TRACE_WRITE)) + trace_write(); + return result; } bool -SGPropertyNode::setStringValue (string value) +SGPropertyNode::setStringValue (const char * value) { + // Shortcut for common case + if (_attr == (READ|WRITE) && _type == STRING) + return set_string(value); + + bool result = false; TEST_WRITE; if (_type == NONE || _type == UNSPECIFIED) { clear_value(); - _value.string_val = new SGRawValueInternal; _type = STRING; } switch (_type) { case ALIAS: - return _value.alias->setStringValue(value); + result = _value.alias->setStringValue(value); + break; case BOOL: - return SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false); + result = set_bool((compare_strings(value, "true") + || atoi(value)) ? true : false); + break; case INT: - return SET_INT(atoi(value.c_str())); + result = set_int(atoi(value)); + break; case LONG: - return SET_LONG(strtol(value.c_str(), 0, 0)); + result = set_long(strtol(value, 0, 0)); + break; case FLOAT: - return SET_FLOAT(atof(value.c_str())); + result = set_float(atof(value)); + break; case DOUBLE: - return SET_DOUBLE(strtod(value.c_str(), 0)); + result = set_double(strtod(value, 0)); + break; case STRING: - return SET_STRING(value); + case UNSPECIFIED: + result = set_string(value); + break; + case NONE: + default: + break; } - return false; // should never happen + if (getAttribute(TRACE_WRITE)) + trace_write(); + return result; } bool -SGPropertyNode::setUnspecifiedValue (string value) +SGPropertyNode::setUnspecifiedValue (const char * value) { + bool result = false; TEST_WRITE; if (_type == NONE) { clear_value(); - _value.string_val = new SGRawValueInternal; _type = UNSPECIFIED; } switch (_type) { case ALIAS: - return _value.alias->setUnspecifiedValue(value); + result = _value.alias->setUnspecifiedValue(value); + break; case BOOL: - return SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false); + result = set_bool((compare_strings(value, "true") + || atoi(value)) ? true : false); + break; case INT: - return SET_INT(atoi(value.c_str())); + result = set_int(atoi(value)); + break; case LONG: - return SET_LONG(strtol(value.c_str(), 0, 0)); + result = set_long(strtol(value, 0, 0)); + break; case FLOAT: - return SET_FLOAT(atof(value.c_str())); + result = set_float(atof(value)); + break; case DOUBLE: - return SET_DOUBLE(strtod(value.c_str(), 0)); + result = set_double(strtod(value, 0)); + break; case STRING: case UNSPECIFIED: - return SET_STRING(value); + result = set_string(value); + break; + case NONE: + default: + break; } - return false; // should never happen + if (getAttribute(TRACE_WRITE)) + trace_write(); + return result; } bool @@ -970,6 +1392,7 @@ SGPropertyNode::tie (const SGRawValue &rawValue, bool useDefault) if (_type == ALIAS || _tied) return false; + useDefault = useDefault && hasValue(); bool old_val = false; if (useDefault) old_val = getBoolValue(); @@ -991,6 +1414,7 @@ SGPropertyNode::tie (const SGRawValue &rawValue, bool useDefault) if (_type == ALIAS || _tied) return false; + useDefault = useDefault && hasValue(); int old_val = 0; if (useDefault) old_val = getIntValue(); @@ -1012,7 +1436,8 @@ SGPropertyNode::tie (const SGRawValue &rawValue, bool useDefault) if (_type == ALIAS || _tied) return false; - long old_val; + useDefault = useDefault && hasValue(); + long old_val = 0; if (useDefault) old_val = getLongValue(); @@ -1033,6 +1458,7 @@ SGPropertyNode::tie (const SGRawValue &rawValue, bool useDefault) if (_type == ALIAS || _tied) return false; + useDefault = useDefault && hasValue(); float old_val = 0.0; if (useDefault) old_val = getFloatValue(); @@ -1054,6 +1480,7 @@ SGPropertyNode::tie (const SGRawValue &rawValue, bool useDefault) if (_type == ALIAS || _tied) return false; + useDefault = useDefault && hasValue(); double old_val = 0.0; if (useDefault) old_val = getDoubleValue(); @@ -1071,11 +1498,12 @@ SGPropertyNode::tie (const SGRawValue &rawValue, bool useDefault) } bool -SGPropertyNode::tie (const SGRawValue &rawValue, bool useDefault) +SGPropertyNode::tie (const SGRawValue &rawValue, bool useDefault) { if (_type == ALIAS || _tied) return false; + useDefault = useDefault && hasValue(); string old_val; if (useDefault) old_val = getStringValue(); @@ -1086,7 +1514,7 @@ SGPropertyNode::tie (const SGRawValue &rawValue, bool useDefault) _value.string_val = rawValue.clone(); if (useDefault) - setStringValue(old_val); + setStringValue(old_val.c_str()); return true; } @@ -1102,50 +1530,48 @@ SGPropertyNode::untie () bool val = getBoolValue(); clear_value(); _type = BOOL; - _value.bool_val = new SGRawValueInternal; - SET_BOOL(val); + _local_val.bool_val = val; break; } case INT: { int val = getIntValue(); clear_value(); _type = INT; - _value.int_val = new SGRawValueInternal; - SET_INT(val); + _local_val.int_val = val; break; } case LONG: { long val = getLongValue(); clear_value(); _type = LONG; - _value.long_val = new SGRawValueInternal; - SET_LONG(val); + _local_val.long_val = val; break; } case FLOAT: { float val = getFloatValue(); clear_value(); _type = FLOAT; - _value.float_val = new SGRawValueInternal; - SET_FLOAT(val); + _local_val.float_val = val; break; } case DOUBLE: { double val = getDoubleValue(); clear_value(); _type = DOUBLE; - _value.double_val = new SGRawValueInternal; - SET_DOUBLE(val); + _local_val.double_val = val; break; } - case STRING: { + case STRING: + case UNSPECIFIED: { string val = getStringValue(); clear_value(); _type = STRING; - _value.string_val = new SGRawValueInternal; - SET_STRING(val); + _local_val.string_val = copy_string(val.c_str()); break; } + case NONE: + default: + break; } _tied = false; @@ -1171,22 +1597,47 @@ SGPropertyNode::getRootNode () const } SGPropertyNode * -SGPropertyNode::getNode (const string &relative_path, bool create) -{ +SGPropertyNode::getNode (const char * relative_path, bool create) +{ +// if (_path_cache == 0) +// _path_cache = new cache_map; + +// SGPropertyNode * result = (*_path_cache)[relative_path]; +// if (result == 0) { +// vector components; +// parse_path(relative_path, components); +// result = find_node(this, components, 0, create); +// if (result != 0) +// (*_path_cache)[relative_path] = result; +// } + +// return result; vector components; parse_path(relative_path, components); return find_node(this, components, 0, create); } -const SGPropertyNode * -SGPropertyNode::getNode (const string &relative_path) const +SGPropertyNode * +SGPropertyNode::getNode (const char * relative_path, int index, bool create) { vector components; parse_path(relative_path, components); - // FIXME: cast away const - return find_node((SGPropertyNode *)this, components, 0, false); + if (components.size() > 0) + components[components.size()-1].index = index; + return find_node(this, components, 0, create); } +const SGPropertyNode * +SGPropertyNode::getNode (const char * relative_path) const +{ + return ((SGPropertyNode *)this)->getNode(relative_path, false); +} + +const SGPropertyNode * +SGPropertyNode::getNode (const char * relative_path, int index) const +{ + return ((SGPropertyNode *)this)->getNode(relative_path, index, false); +} //////////////////////////////////////////////////////////////////////// @@ -1198,7 +1649,7 @@ SGPropertyNode::getNode (const string &relative_path) const * Test whether another node has a value attached. */ bool -SGPropertyNode::hasValue (const string &relative_path) const +SGPropertyNode::hasValue (const char * relative_path) const { const SGPropertyNode * node = getNode(relative_path); return (node == 0 ? false : node->hasValue()); @@ -1209,7 +1660,7 @@ SGPropertyNode::hasValue (const string &relative_path) const * Get the value type for another node. */ SGPropertyNode::Type -SGPropertyNode::getType (const string &relative_path) const +SGPropertyNode::getType (const char * relative_path) const { const SGPropertyNode * node = getNode(relative_path); return (node == 0 ? UNSPECIFIED : (Type)(node->getType())); @@ -1220,7 +1671,7 @@ SGPropertyNode::getType (const string &relative_path) const * Get a bool value for another node. */ bool -SGPropertyNode::getBoolValue (const string &relative_path, +SGPropertyNode::getBoolValue (const char * relative_path, bool defaultValue) const { const SGPropertyNode * node = getNode(relative_path); @@ -1232,7 +1683,7 @@ SGPropertyNode::getBoolValue (const string &relative_path, * Get an int value for another node. */ int -SGPropertyNode::getIntValue (const string &relative_path, +SGPropertyNode::getIntValue (const char * relative_path, int defaultValue) const { const SGPropertyNode * node = getNode(relative_path); @@ -1244,7 +1695,7 @@ SGPropertyNode::getIntValue (const string &relative_path, * Get a long value for another node. */ long -SGPropertyNode::getLongValue (const string &relative_path, +SGPropertyNode::getLongValue (const char * relative_path, long defaultValue) const { const SGPropertyNode * node = getNode(relative_path); @@ -1256,7 +1707,7 @@ SGPropertyNode::getLongValue (const string &relative_path, * Get a float value for another node. */ float -SGPropertyNode::getFloatValue (const string &relative_path, +SGPropertyNode::getFloatValue (const char * relative_path, float defaultValue) const { const SGPropertyNode * node = getNode(relative_path); @@ -1268,7 +1719,7 @@ SGPropertyNode::getFloatValue (const string &relative_path, * Get a double value for another node. */ double -SGPropertyNode::getDoubleValue (const string &relative_path, +SGPropertyNode::getDoubleValue (const char * relative_path, double defaultValue) const { const SGPropertyNode * node = getNode(relative_path); @@ -1279,9 +1730,9 @@ SGPropertyNode::getDoubleValue (const string &relative_path, /** * Get a string value for another node. */ -string -SGPropertyNode::getStringValue (const string &relative_path, - string defaultValue) const +const char * +SGPropertyNode::getStringValue (const char * relative_path, + const char * defaultValue) const { const SGPropertyNode * node = getNode(relative_path); return (node == 0 ? defaultValue : node->getStringValue()); @@ -1292,7 +1743,7 @@ SGPropertyNode::getStringValue (const string &relative_path, * Set a bool value for another node. */ bool -SGPropertyNode::setBoolValue (const string &relative_path, bool value) +SGPropertyNode::setBoolValue (const char * relative_path, bool value) { return getNode(relative_path, true)->setBoolValue(value); } @@ -1302,7 +1753,7 @@ SGPropertyNode::setBoolValue (const string &relative_path, bool value) * Set an int value for another node. */ bool -SGPropertyNode::setIntValue (const string &relative_path, int value) +SGPropertyNode::setIntValue (const char * relative_path, int value) { return getNode(relative_path, true)->setIntValue(value); } @@ -1312,7 +1763,7 @@ SGPropertyNode::setIntValue (const string &relative_path, int value) * Set a long value for another node. */ bool -SGPropertyNode::setLongValue (const string &relative_path, long value) +SGPropertyNode::setLongValue (const char * relative_path, long value) { return getNode(relative_path, true)->setLongValue(value); } @@ -1322,7 +1773,7 @@ SGPropertyNode::setLongValue (const string &relative_path, long value) * Set a float value for another node. */ bool -SGPropertyNode::setFloatValue (const string &relative_path, float value) +SGPropertyNode::setFloatValue (const char * relative_path, float value) { return getNode(relative_path, true)->setFloatValue(value); } @@ -1332,7 +1783,7 @@ SGPropertyNode::setFloatValue (const string &relative_path, float value) * Set a double value for another node. */ bool -SGPropertyNode::setDoubleValue (const string &relative_path, double value) +SGPropertyNode::setDoubleValue (const char * relative_path, double value) { return getNode(relative_path, true)->setDoubleValue(value); } @@ -1342,7 +1793,7 @@ SGPropertyNode::setDoubleValue (const string &relative_path, double value) * Set a string value for another node. */ bool -SGPropertyNode::setStringValue (const string &relative_path, string value) +SGPropertyNode::setStringValue (const char * relative_path, const char * value) { return getNode(relative_path, true)->setStringValue(value); } @@ -1352,7 +1803,8 @@ SGPropertyNode::setStringValue (const string &relative_path, string value) * Set an unknown value for another node. */ bool -SGPropertyNode::setUnspecifiedValue (const string &relative_path, string value) +SGPropertyNode::setUnspecifiedValue (const char * relative_path, + const char * value) { return getNode(relative_path, true)->setUnspecifiedValue(value); } @@ -1362,7 +1814,7 @@ SGPropertyNode::setUnspecifiedValue (const string &relative_path, string value) * Test whether another node is tied. */ bool -SGPropertyNode::isTied (const string &relative_path) const +SGPropertyNode::isTied (const char * relative_path) const { const SGPropertyNode * node = getNode(relative_path); return (node == 0 ? false : node->isTied()); @@ -1373,7 +1825,7 @@ SGPropertyNode::isTied (const string &relative_path) const * Tie a node reached by a relative path, creating it if necessary. */ bool -SGPropertyNode::tie (const string &relative_path, +SGPropertyNode::tie (const char * relative_path, const SGRawValue &rawValue, bool useDefault) { @@ -1385,7 +1837,7 @@ SGPropertyNode::tie (const string &relative_path, * Tie a node reached by a relative path, creating it if necessary. */ bool -SGPropertyNode::tie (const string &relative_path, +SGPropertyNode::tie (const char * relative_path, const SGRawValue &rawValue, bool useDefault) { @@ -1397,7 +1849,7 @@ SGPropertyNode::tie (const string &relative_path, * Tie a node reached by a relative path, creating it if necessary. */ bool -SGPropertyNode::tie (const string &relative_path, +SGPropertyNode::tie (const char * relative_path, const SGRawValue &rawValue, bool useDefault) { @@ -1409,7 +1861,7 @@ SGPropertyNode::tie (const string &relative_path, * Tie a node reached by a relative path, creating it if necessary. */ bool -SGPropertyNode::tie (const string &relative_path, +SGPropertyNode::tie (const char * relative_path, const SGRawValue &rawValue, bool useDefault) { @@ -1421,7 +1873,7 @@ SGPropertyNode::tie (const string &relative_path, * Tie a node reached by a relative path, creating it if necessary. */ bool -SGPropertyNode::tie (const string &relative_path, +SGPropertyNode::tie (const char * relative_path, const SGRawValue &rawValue, bool useDefault) { @@ -1433,8 +1885,8 @@ SGPropertyNode::tie (const string &relative_path, * Tie a node reached by a relative path, creating it if necessary. */ bool -SGPropertyNode::tie (const string &relative_path, - const SGRawValue &rawValue, +SGPropertyNode::tie (const char * relative_path, + const SGRawValue &rawValue, bool useDefault) { return getNode(relative_path, true)->tie(rawValue, useDefault); @@ -1445,7 +1897,7 @@ SGPropertyNode::tie (const string &relative_path, * Attempt to untie another node reached by a relative path. */ bool -SGPropertyNode::untie (const string &relative_path) +SGPropertyNode::untie (const char * relative_path) { SGPropertyNode * node = getNode(relative_path); return (node == 0 ? false : node->untie());