1 // props.cxx - implementation of a property list.
2 // Started Fall 2000 by David Megginson, david@megginson.com
3 // This code is released into the Public Domain.
5 // See props.html for documentation [replace with URL when available].
9 #include <simgear/compiler.h>
10 #include <simgear/debug/logstream.hxx>
22 ////////////////////////////////////////////////////////////////////////
24 ////////////////////////////////////////////////////////////////////////
27 * Comparator class for sorting by index.
32 int operator() (const SGPropertyNode * n1, const SGPropertyNode *n2) const {
33 return (n1->getIndex() < n2->getIndex());
39 ////////////////////////////////////////////////////////////////////////
40 // Convenience macros for value access.
41 ////////////////////////////////////////////////////////////////////////
43 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
44 #define TEST_WRITE if (!getAttribute(WRITE)) return false
46 #define DO_TRACE_READ(type) if(getAttribute(TRACE_READ)) trace_read(type)
47 #define DO_TRACE_WRITE(type) if (getAttribute(TRACE_WRITE)) trace_write(type)
49 #define GET_BOOL (_value.bool_val->getValue())
50 #define GET_INT (_value.int_val->getValue())
51 #define GET_LONG (_value.long_val->getValue())
52 #define GET_FLOAT (_value.float_val->getValue())
53 #define GET_DOUBLE (_value.double_val->getValue())
54 #define GET_STRING (_value.string_val->getValue())
56 #define SET_BOOL(val) (_value.bool_val->setValue(val))
57 #define SET_INT(val) (_value.int_val->setValue(val))
58 #define SET_LONG(val) (_value.long_val->setValue(val))
59 #define SET_FLOAT(val) (_value.float_val->setValue(val))
60 #define SET_DOUBLE(val) (_value.double_val->setValue(val))
61 #define SET_STRING(val) (_value.string_val->setValue(val))
65 ////////////////////////////////////////////////////////////////////////
66 // Default values for every type.
67 ////////////////////////////////////////////////////////////////////////
69 const bool SGRawValue<bool>::DefaultValue = false;
70 const int SGRawValue<int>::DefaultValue = 0;
71 const long SGRawValue<long>::DefaultValue = 0L;
72 const float SGRawValue<float>::DefaultValue = 0.0;
73 const double SGRawValue<double>::DefaultValue = 0.0L;
74 const string SGRawValue<string>::DefaultValue = "";
78 ////////////////////////////////////////////////////////////////////////
79 // Local path normalization code.
80 ////////////////////////////////////////////////////////////////////////
83 * A component in a path.
92 * Parse the name for a path component.
94 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
97 parse_name (const string &path, int &i)
100 int max = path.size();
102 if (path[i] == '.') {
104 if (i < max && path[i] == '.') {
110 if (i < max && path[i] != '/')
111 throw string(string("Illegal character after ") + name);
114 else if (isalpha(path[i]) || path[i] == '_') {
118 // The rules inside a name are a little
121 if (isalpha(path[i]) || isdigit(path[i]) || path[i] == '_' ||
122 path[i] == '-' || path[i] == '.') {
124 } else if (path[i] == '[' || path[i] == '/') {
127 throw string("name may contain only ._- and alphanumeric characters");
134 if (name.size() == 0)
135 throw string("name must begin with alpha or '_'");
143 * Parse the optional integer index for a path component.
145 * Index: "[" [0-9]+ "]"
148 parse_index (const string &path, int &i)
157 for (int max = path.size(); i < max; i++) {
158 if (isdigit(path[i])) {
159 index = (index * 10) + (path[i] - '0');
160 } else if (path[i] == ']') {
168 throw string("unterminated index (looking for ']')");
173 * Parse a single path component.
175 * Component: Name Index?
177 static inline PathComponent
178 parse_component (const string &path, int &i)
180 PathComponent component;
181 component.name = parse_name(path, i);
182 if (component.name[0] != '.')
183 component.index = parse_index(path, i);
185 component.index = -1;
191 * Parse a path into its components.
194 parse_path (const string &path, vector<PathComponent> &components)
197 int max = path.size();
199 // Check for initial '/'
200 if (path[pos] == '/') {
204 components.push_back(root);
206 while (pos < max && path[pos] == '/')
211 components.push_back(parse_component(path, pos));
212 while (pos < max && path[pos] == '/')
219 ////////////////////////////////////////////////////////////////////////
220 // Other static utility functions.
221 ////////////////////////////////////////////////////////////////////////
225 * Locate a child node by name and index.
228 find_child (const string &name, int index, vector<SGPropertyNode *> nodes)
230 int nNodes = nodes.size();
231 for (int i = 0; i < nNodes; i++) {
232 SGPropertyNode * node = nodes[i];
233 if (node->getName() == name && node->getIndex() == index)
241 * Locate another node, given a relative path.
243 static SGPropertyNode *
244 find_node (SGPropertyNode * current,
245 const vector<PathComponent> &components,
249 // Run off the end of the list
254 // Success! This is the one we want.
255 else if (position >= (int)components.size()) {
259 // Empty component means root.
260 else if (components[position].name == "") {
261 return find_node(current->getRootNode(), components, position + 1, create);
264 // . means current directory
265 else if (components[position].name == ".") {
266 return find_node(current, components, position + 1, create);
269 // .. means parent directory
270 else if (components[position].name == "..") {
271 SGPropertyNode * parent = current->getParent();
273 throw string("Attempt to move past root with '..'");
275 return find_node(parent, components, position + 1, create);
278 // Otherwise, a child name
280 SGPropertyNode * child =
281 current->getChild(components[position].name,
282 components[position].index,
284 return find_node(child, components, position + 1, create);
290 ////////////////////////////////////////////////////////////////////////
291 // Implementation of SGPropertyNode.
292 ////////////////////////////////////////////////////////////////////////
296 * Default constructor: always creates a root node.
298 SGPropertyNode::SGPropertyNode ()
312 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
315 _parent(0), // don't copy the parent
324 _value.alias = node._value.alias;
327 _value.bool_val = node._value.bool_val->clone();
330 _value.int_val = node._value.int_val->clone();
333 _value.long_val = node._value.long_val->clone();
336 _value.float_val = node._value.float_val->clone();
339 _value.double_val = node._value.double_val->clone();
343 _value.string_val = node._value.string_val->clone();
350 * Convenience constructor.
352 SGPropertyNode::SGPropertyNode (const string &name,
353 int index, SGPropertyNode * parent)
354 : _name(name), _index(index), _parent(parent), _type(NONE),
355 _tied(false), _attr(READ|WRITE)
363 SGPropertyNode::~SGPropertyNode ()
365 for (int i = 0; i < (int)_children.size(); i++) {
373 * Delete and clear the current value.
376 SGPropertyNode::clear_value ()
384 delete _value.bool_val;
388 delete _value.int_val;
392 delete _value.long_val;
393 _value.long_val = 0L;
396 delete _value.float_val;
397 _value.float_val = 0;
400 delete _value.double_val;
401 _value.double_val = 0;
405 delete _value.string_val;
406 _value.string_val = 0;
414 * Get the value as a string.
417 SGPropertyNode::get_string () const
424 return _value.alias->getStringValue();
431 sprintf(buf, "%d", GET_INT);
434 sprintf(buf, "%ld", GET_LONG);
437 sprintf(buf, "%f", GET_FLOAT);
440 sprintf(buf, "%f", GET_DOUBLE);
447 return ""; // if NONE
452 * Trace a read access for a property.
455 SGPropertyNode::trace_read (SGPropertyNode::Type accessType) const
457 SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Read node " << getPath()
458 << ", value \"" << get_string() << '"');
463 * Trace a write access for a property.
466 SGPropertyNode::trace_write (SGPropertyNode::Type accessType) const
468 SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Write node " << getPath()
469 << ", value\"" << get_string() << '"');
474 * Alias to another node.
477 SGPropertyNode::alias (SGPropertyNode * target)
479 if (target == 0 || _type == ALIAS || _tied)
482 _value.alias = target;
489 * Alias to another node by path.
492 SGPropertyNode::alias (const string &path)
494 return alias(getNode(path, true));
502 SGPropertyNode::unalias ()
513 * Get the target of an alias.
516 SGPropertyNode::getAliasTarget ()
518 return (_type == ALIAS ? _value.alias : 0);
522 const SGPropertyNode *
523 SGPropertyNode::getAliasTarget () const
525 return (_type == ALIAS ? _value.alias : 0);
530 * Get a non-const child by index.
533 SGPropertyNode::getChild (int position)
535 if (position >= 0 && position < nChildren())
536 return _children[position];
543 * Get a const child by index.
545 const SGPropertyNode *
546 SGPropertyNode::getChild (int position) const
548 if (position >= 0 && position < nChildren())
549 return _children[position];
556 * Get a non-const child by name and index, creating if necessary.
559 SGPropertyNode::getChild (const string &name, int index, bool create)
561 int pos = find_child(name, index, _children);
563 return _children[pos];
565 _children.push_back(new SGPropertyNode(name, index, this));
566 return _children[_children.size()-1];
574 * Get a const child by name and index.
576 const SGPropertyNode *
577 SGPropertyNode::getChild (const string &name, int index) const
579 int pos = find_child(name, index, _children);
581 return _children[pos];
588 * Get all children with the same name (but different indices).
590 vector<SGPropertyNode *>
591 SGPropertyNode::getChildren (const string &name)
593 vector<SGPropertyNode *> children;
594 int max = _children.size();
596 for (int i = 0; i < max; i++)
597 if (_children[i]->getName() == name)
598 children.push_back(_children[i]);
600 sort(children.begin(), children.end(), CompareIndices());
606 * Get all children const with the same name (but different indices).
608 vector<const SGPropertyNode *>
609 SGPropertyNode::getChildren (const string &name) const
611 vector<const SGPropertyNode *> children;
612 int max = _children.size();
614 for (int i = 0; i < max; i++)
615 if (_children[i]->getName() == name)
616 children.push_back(_children[i]);
618 sort(children.begin(), children.end(), CompareIndices());
624 SGPropertyNode::getPath (bool simplify) const
629 string path = _parent->getPath(simplify);
632 if (_index != 0 || !simplify) {
634 sprintf(buffer, "[%d]", _index);
641 SGPropertyNode::getType () const
644 return _value.alias->getType();
651 SGPropertyNode::getBoolValue () const
657 return _value.alias->getBoolValue();
661 return GET_INT == 0 ? false : true;
663 return GET_LONG == 0L ? false : true;
665 return GET_FLOAT == 0.0 ? false : true;
667 return GET_DOUBLE == 0.0L ? false : true;
670 return (GET_STRING == "true" || getDoubleValue() != 0.0L);
673 return false; // if NONE
677 SGPropertyNode::getIntValue () const
683 return _value.alias->getIntValue();
685 return int(GET_BOOL);
689 return int(GET_LONG);
691 return int(GET_FLOAT);
693 return int(GET_DOUBLE);
696 return atoi(GET_STRING.c_str());
703 SGPropertyNode::getLongValue () const
709 return _value.alias->getLongValue();
711 return long(GET_BOOL);
713 return long(GET_INT);
717 return long(GET_FLOAT);
719 return long(GET_DOUBLE);
722 return strtol(GET_STRING.c_str(), 0, 0);
725 return 0L; // if NONE
729 SGPropertyNode::getFloatValue () const
731 DO_TRACE_READ(FLOAT);
735 return _value.alias->getFloatValue();
737 return float(GET_BOOL);
739 return float(GET_INT);
741 return float(GET_LONG);
745 return float(GET_DOUBLE);
748 return atof(GET_STRING.c_str());
751 return 0.0; // if NONE
755 SGPropertyNode::getDoubleValue () const
757 DO_TRACE_READ(DOUBLE);
761 return _value.alias->getDoubleValue();
763 return double(GET_BOOL);
765 return double(GET_INT);
767 return double(GET_LONG);
769 return double(GET_FLOAT);
774 return strtod(GET_STRING.c_str(), 0);
777 return 0.0L; // if NONE
781 SGPropertyNode::getStringValue () const
783 DO_TRACE_READ(STRING);
788 SGPropertyNode::setBoolValue (bool value)
792 if (_type == NONE || _type == UNSPECIFIED) {
794 _value.bool_val = new SGRawValueInternal<bool>;
800 result = _value.alias->setBoolValue(value);
803 result = SET_BOOL(value);
806 result = SET_INT(int(value));
809 result = SET_LONG(long(value));
812 result = SET_FLOAT(float(value));
815 result = SET_DOUBLE(double(value));
818 result = SET_STRING(value ? "true" : "false");
822 DO_TRACE_WRITE(BOOL);
827 SGPropertyNode::setIntValue (int value)
831 if (_type == NONE || _type == UNSPECIFIED) {
833 _value.int_val = new SGRawValueInternal<int>;
839 result = _value.alias->setIntValue(value);
842 result = SET_BOOL(value == 0 ? false : true);
845 result = SET_INT(value);
848 result = SET_LONG(long(value));
851 result = SET_FLOAT(float(value));
854 result = SET_DOUBLE(double(value));
858 sprintf(buf, "%d", value);
859 result = SET_STRING(buf);
869 SGPropertyNode::setLongValue (long value)
873 if (_type == NONE || _type == UNSPECIFIED) {
875 _value.long_val = new SGRawValueInternal<long>;
881 result = _value.alias->setLongValue(value);
884 result = SET_BOOL(value == 0L ? false : true);
887 result = SET_INT(int(value));
890 result = SET_LONG(value);
893 result = SET_FLOAT(float(value));
896 result = SET_DOUBLE(double(value));
900 sprintf(buf, "%d", value);
901 result = SET_STRING(buf);
906 DO_TRACE_WRITE(LONG);
911 SGPropertyNode::setFloatValue (float value)
915 if (_type == NONE || _type == UNSPECIFIED) {
917 _value.float_val = new SGRawValueInternal<float>;
923 result = _value.alias->setFloatValue(value);
926 result = SET_BOOL(value == 0.0 ? false : true);
929 result = SET_INT(int(value));
932 result = SET_LONG(long(value));
935 result = SET_FLOAT(value);
938 result = SET_DOUBLE(double(value));
942 sprintf(buf, "%f", value);
943 result = SET_STRING(buf);
948 DO_TRACE_WRITE(FLOAT);
953 SGPropertyNode::setDoubleValue (double value)
957 if (_type == NONE || _type == UNSPECIFIED) {
959 _value.double_val = new SGRawValueInternal<double>;
965 result = _value.alias->setDoubleValue(value);
968 result = SET_BOOL(value == 0.0L ? false : true);
971 result = SET_INT(int(value));
974 result = SET_LONG(long(value));
977 result = SET_FLOAT(float(value));
980 result = SET_DOUBLE(value);
984 sprintf(buf, "%lf", value);
985 result = SET_STRING(buf);
990 DO_TRACE_WRITE(DOUBLE);
995 SGPropertyNode::setStringValue (string value)
999 if (_type == NONE || _type == UNSPECIFIED) {
1001 _value.string_val = new SGRawValueInternal<string>;
1007 result = _value.alias->setStringValue(value);
1010 result = SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
1013 result = SET_INT(atoi(value.c_str()));
1016 result = SET_LONG(strtol(value.c_str(), 0, 0));
1019 result = SET_FLOAT(atof(value.c_str()));
1022 result = SET_DOUBLE(strtod(value.c_str(), 0));
1025 result = SET_STRING(value);
1029 DO_TRACE_WRITE(STRING);
1034 SGPropertyNode::setUnspecifiedValue (string value)
1036 bool result = false;
1038 if (_type == NONE) {
1040 _value.string_val = new SGRawValueInternal<string>;
1041 _type = UNSPECIFIED;
1046 result = _value.alias->setUnspecifiedValue(value);
1049 result = SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
1052 result = SET_INT(atoi(value.c_str()));
1055 result = SET_LONG(strtol(value.c_str(), 0, 0));
1058 result = SET_FLOAT(atof(value.c_str()));
1061 result = SET_DOUBLE(strtod(value.c_str(), 0));
1065 result = SET_STRING(value);
1069 DO_TRACE_WRITE(UNSPECIFIED);
1074 SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
1076 if (_type == ALIAS || _tied)
1079 bool old_val = false;
1081 old_val = getBoolValue();
1086 _value.bool_val = rawValue.clone();
1089 setBoolValue(old_val);
1095 SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
1097 if (_type == ALIAS || _tied)
1102 old_val = getIntValue();
1107 _value.int_val = rawValue.clone();
1110 setIntValue(old_val);
1116 SGPropertyNode::tie (const SGRawValue<long> &rawValue, bool useDefault)
1118 if (_type == ALIAS || _tied)
1123 old_val = getLongValue();
1128 _value.long_val = rawValue.clone();
1131 setLongValue(old_val);
1137 SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
1139 if (_type == ALIAS || _tied)
1142 float old_val = 0.0;
1144 old_val = getFloatValue();
1149 _value.float_val = rawValue.clone();
1152 setFloatValue(old_val);
1158 SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
1160 if (_type == ALIAS || _tied)
1163 double old_val = 0.0;
1165 old_val = getDoubleValue();
1170 _value.double_val = rawValue.clone();
1173 setDoubleValue(old_val);
1180 SGPropertyNode::tie (const SGRawValue<string> &rawValue, bool useDefault)
1182 if (_type == ALIAS || _tied)
1187 old_val = getStringValue();
1192 _value.string_val = rawValue.clone();
1195 setStringValue(old_val);
1201 SGPropertyNode::untie ()
1208 bool val = getBoolValue();
1211 _value.bool_val = new SGRawValueInternal<bool>;
1216 int val = getIntValue();
1219 _value.int_val = new SGRawValueInternal<int>;
1224 long val = getLongValue();
1227 _value.long_val = new SGRawValueInternal<long>;
1232 float val = getFloatValue();
1235 _value.float_val = new SGRawValueInternal<float>;
1240 double val = getDoubleValue();
1243 _value.double_val = new SGRawValueInternal<double>;
1248 string val = getStringValue();
1251 _value.string_val = new SGRawValueInternal<string>;
1262 SGPropertyNode::getRootNode ()
1267 return _parent->getRootNode();
1270 const SGPropertyNode *
1271 SGPropertyNode::getRootNode () const
1276 return _parent->getRootNode();
1280 SGPropertyNode::getNode (const string &relative_path, bool create)
1282 vector<PathComponent> components;
1283 parse_path(relative_path, components);
1284 return find_node(this, components, 0, create);
1287 const SGPropertyNode *
1288 SGPropertyNode::getNode (const string &relative_path) const
1290 vector<PathComponent> components;
1291 parse_path(relative_path, components);
1292 // FIXME: cast away const
1293 return find_node((SGPropertyNode *)this, components, 0, false);
1298 ////////////////////////////////////////////////////////////////////////
1299 // Convenience methods using relative paths.
1300 ////////////////////////////////////////////////////////////////////////
1304 * Test whether another node has a value attached.
1307 SGPropertyNode::hasValue (const string &relative_path) const
1309 const SGPropertyNode * node = getNode(relative_path);
1310 return (node == 0 ? false : node->hasValue());
1315 * Get the value type for another node.
1317 SGPropertyNode::Type
1318 SGPropertyNode::getType (const string &relative_path) const
1320 const SGPropertyNode * node = getNode(relative_path);
1321 return (node == 0 ? UNSPECIFIED : (Type)(node->getType()));
1326 * Get a bool value for another node.
1329 SGPropertyNode::getBoolValue (const string &relative_path,
1330 bool defaultValue) const
1332 const SGPropertyNode * node = getNode(relative_path);
1333 return (node == 0 ? defaultValue : node->getBoolValue());
1338 * Get an int value for another node.
1341 SGPropertyNode::getIntValue (const string &relative_path,
1342 int defaultValue) const
1344 const SGPropertyNode * node = getNode(relative_path);
1345 return (node == 0 ? defaultValue : node->getIntValue());
1350 * Get a long value for another node.
1353 SGPropertyNode::getLongValue (const string &relative_path,
1354 long defaultValue) const
1356 const SGPropertyNode * node = getNode(relative_path);
1357 return (node == 0 ? defaultValue : node->getLongValue());
1362 * Get a float value for another node.
1365 SGPropertyNode::getFloatValue (const string &relative_path,
1366 float defaultValue) const
1368 const SGPropertyNode * node = getNode(relative_path);
1369 return (node == 0 ? defaultValue : node->getFloatValue());
1374 * Get a double value for another node.
1377 SGPropertyNode::getDoubleValue (const string &relative_path,
1378 double defaultValue) const
1380 const SGPropertyNode * node = getNode(relative_path);
1381 return (node == 0 ? defaultValue : node->getDoubleValue());
1386 * Get a string value for another node.
1389 SGPropertyNode::getStringValue (const string &relative_path,
1390 string defaultValue) const
1392 const SGPropertyNode * node = getNode(relative_path);
1393 return (node == 0 ? defaultValue : node->getStringValue());
1398 * Set a bool value for another node.
1401 SGPropertyNode::setBoolValue (const string &relative_path, bool value)
1403 return getNode(relative_path, true)->setBoolValue(value);
1408 * Set an int value for another node.
1411 SGPropertyNode::setIntValue (const string &relative_path, int value)
1413 return getNode(relative_path, true)->setIntValue(value);
1418 * Set a long value for another node.
1421 SGPropertyNode::setLongValue (const string &relative_path, long value)
1423 return getNode(relative_path, true)->setLongValue(value);
1428 * Set a float value for another node.
1431 SGPropertyNode::setFloatValue (const string &relative_path, float value)
1433 return getNode(relative_path, true)->setFloatValue(value);
1438 * Set a double value for another node.
1441 SGPropertyNode::setDoubleValue (const string &relative_path, double value)
1443 return getNode(relative_path, true)->setDoubleValue(value);
1448 * Set a string value for another node.
1451 SGPropertyNode::setStringValue (const string &relative_path, string value)
1453 return getNode(relative_path, true)->setStringValue(value);
1458 * Set an unknown value for another node.
1461 SGPropertyNode::setUnspecifiedValue (const string &relative_path, string value)
1463 return getNode(relative_path, true)->setUnspecifiedValue(value);
1468 * Test whether another node is tied.
1471 SGPropertyNode::isTied (const string &relative_path) const
1473 const SGPropertyNode * node = getNode(relative_path);
1474 return (node == 0 ? false : node->isTied());
1479 * Tie a node reached by a relative path, creating it if necessary.
1482 SGPropertyNode::tie (const string &relative_path,
1483 const SGRawValue<bool> &rawValue,
1486 return getNode(relative_path, true)->tie(rawValue, useDefault);
1491 * Tie a node reached by a relative path, creating it if necessary.
1494 SGPropertyNode::tie (const string &relative_path,
1495 const SGRawValue<int> &rawValue,
1498 return getNode(relative_path, true)->tie(rawValue, useDefault);
1503 * Tie a node reached by a relative path, creating it if necessary.
1506 SGPropertyNode::tie (const string &relative_path,
1507 const SGRawValue<long> &rawValue,
1510 return getNode(relative_path, true)->tie(rawValue, useDefault);
1515 * Tie a node reached by a relative path, creating it if necessary.
1518 SGPropertyNode::tie (const string &relative_path,
1519 const SGRawValue<float> &rawValue,
1522 return getNode(relative_path, true)->tie(rawValue, useDefault);
1527 * Tie a node reached by a relative path, creating it if necessary.
1530 SGPropertyNode::tie (const string &relative_path,
1531 const SGRawValue<double> &rawValue,
1534 return getNode(relative_path, true)->tie(rawValue, useDefault);
1539 * Tie a node reached by a relative path, creating it if necessary.
1542 SGPropertyNode::tie (const string &relative_path,
1543 const SGRawValue<string> &rawValue,
1546 return getNode(relative_path, true)->tie(rawValue, useDefault);
1551 * Attempt to untie another node reached by a relative path.
1554 SGPropertyNode::untie (const string &relative_path)
1556 SGPropertyNode * node = getNode(relative_path);
1557 return (node == 0 ? false : node->untie());