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>
21 ////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////
26 * Comparator class for sorting by index.
31 int operator() (const SGPropertyNode * n1, const SGPropertyNode *n2) const {
32 return (n1->getIndex() < n2->getIndex());
38 ////////////////////////////////////////////////////////////////////////
39 // Convenience macros for value access.
40 ////////////////////////////////////////////////////////////////////////
42 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
43 #define TEST_WRITE if (!getAttribute(WRITE)) return false
45 #define GET_BOOL (_value.bool_val->getValue())
46 #define GET_INT (_value.int_val->getValue())
47 #define GET_LONG (_value.long_val->getValue())
48 #define GET_FLOAT (_value.float_val->getValue())
49 #define GET_DOUBLE (_value.double_val->getValue())
50 #define GET_STRING (_value.string_val->getValue())
52 #define SET_BOOL(val) (_value.bool_val->setValue(val))
53 #define SET_INT(val) (_value.int_val->setValue(val))
54 #define SET_LONG(val) (_value.long_val->setValue(val))
55 #define SET_FLOAT(val) (_value.float_val->setValue(val))
56 #define SET_DOUBLE(val) (_value.double_val->setValue(val))
57 #define SET_STRING(val) (_value.string_val->setValue(val))
61 ////////////////////////////////////////////////////////////////////////
62 // Default values for every type.
63 ////////////////////////////////////////////////////////////////////////
65 const bool SGRawValue<bool>::DefaultValue = false;
66 const int SGRawValue<int>::DefaultValue = 0;
67 const long SGRawValue<long>::DefaultValue = 0L;
68 const float SGRawValue<float>::DefaultValue = 0.0;
69 const double SGRawValue<double>::DefaultValue = 0.0L;
70 const string SGRawValue<string>::DefaultValue = "";
74 ////////////////////////////////////////////////////////////////////////
75 // Local path normalization code.
76 ////////////////////////////////////////////////////////////////////////
79 * A component in a path.
88 * Parse the name for a path component.
90 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
93 parse_name (const string &path, int &i)
96 int max = path.size();
100 if (i < max && path[i] == '.') {
106 if (i < max && path[i] != '/')
107 throw string(string("Illegal character after ") + name);
110 else if (isalpha(path[i]) || path[i] == '_') {
114 // The rules inside a name are a little
117 if (isalpha(path[i]) || isdigit(path[i]) || path[i] == '_' ||
118 path[i] == '-' || path[i] == '.') {
120 } else if (path[i] == '[' || path[i] == '/') {
123 throw string("name may contain only ._- and alphanumeric characters");
130 if (name.size() == 0)
131 throw string("name must begin with alpha or '_'");
139 * Parse the optional integer index for a path component.
141 * Index: "[" [0-9]+ "]"
144 parse_index (const string &path, int &i)
153 for (int max = path.size(); i < max; i++) {
154 if (isdigit(path[i])) {
155 index = (index * 10) + (path[i] - '0');
156 } else if (path[i] == ']') {
164 throw string("unterminated index (looking for ']')");
169 * Parse a single path component.
171 * Component: Name Index?
173 static inline PathComponent
174 parse_component (const string &path, int &i)
176 PathComponent component;
177 component.name = parse_name(path, i);
178 if (component.name[0] != '.')
179 component.index = parse_index(path, i);
181 component.index = -1;
187 * Parse a path into its components.
190 parse_path (const string &path, vector<PathComponent> &components)
193 int max = path.size();
195 // Check for initial '/'
196 if (path[pos] == '/') {
200 components.push_back(root);
202 while (pos < max && path[pos] == '/')
207 components.push_back(parse_component(path, pos));
208 while (pos < max && path[pos] == '/')
215 ////////////////////////////////////////////////////////////////////////
216 // Other static utility functions.
217 ////////////////////////////////////////////////////////////////////////
221 * Locate a child node by name and index.
224 find_child (const string &name, int index, vector<SGPropertyNode *> nodes)
226 int nNodes = nodes.size();
227 for (int i = 0; i < nNodes; i++) {
228 SGPropertyNode * node = nodes[i];
229 if (node->getName() == name && node->getIndex() == index)
237 * Locate another node, given a relative path.
239 static SGPropertyNode *
240 find_node (SGPropertyNode * current,
241 const vector<PathComponent> &components,
245 // Run off the end of the list
250 // Success! This is the one we want.
251 else if (position >= (int)components.size()) {
255 // Empty component means root.
256 else if (components[position].name == "") {
257 return find_node(current->getRootNode(), components, position + 1, create);
260 // . means current directory
261 else if (components[position].name == ".") {
262 return find_node(current, components, position + 1, create);
265 // .. means parent directory
266 else if (components[position].name == "..") {
267 SGPropertyNode * parent = current->getParent();
269 throw string("Attempt to move past root with '..'");
271 return find_node(parent, components, position + 1, create);
274 // Otherwise, a child name
276 SGPropertyNode * child =
277 current->getChild(components[position].name,
278 components[position].index,
280 return find_node(child, components, position + 1, create);
286 ////////////////////////////////////////////////////////////////////////
287 // Implementation of SGPropertyNode.
288 ////////////////////////////////////////////////////////////////////////
292 * Default constructor: always creates a root node.
294 SGPropertyNode::SGPropertyNode ()
308 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
311 _parent(0), // don't copy the parent
320 _value.alias = node._value.alias;
323 _value.bool_val = node._value.bool_val->clone();
326 _value.int_val = node._value.int_val->clone();
329 _value.long_val = node._value.long_val->clone();
332 _value.float_val = node._value.float_val->clone();
335 _value.double_val = node._value.double_val->clone();
339 _value.string_val = node._value.string_val->clone();
346 * Convenience constructor.
348 SGPropertyNode::SGPropertyNode (const string &name,
349 int index, SGPropertyNode * parent)
350 : _name(name), _index(index), _parent(parent), _type(NONE),
351 _tied(false), _attr(READ|WRITE)
359 SGPropertyNode::~SGPropertyNode ()
361 for (int i = 0; i < (int)_children.size(); i++)
368 * Delete and clear the current value.
371 SGPropertyNode::clear_value ()
379 delete _value.bool_val;
383 delete _value.int_val;
387 delete _value.long_val;
388 _value.long_val = 0L;
391 delete _value.float_val;
392 _value.float_val = 0;
395 delete _value.double_val;
396 _value.double_val = 0;
400 delete _value.string_val;
401 _value.string_val = 0;
409 * Alias to another node.
412 SGPropertyNode::alias (SGPropertyNode * target)
414 if (target == 0 || _type == ALIAS || _tied)
417 _value.alias = target;
424 * Alias to another node by path.
427 SGPropertyNode::alias (const string &path)
429 return alias(getNode(path, true));
437 SGPropertyNode::unalias ()
448 * Get the target of an alias.
451 SGPropertyNode::getAliasTarget ()
453 return (_type == ALIAS ? _value.alias : 0);
457 const SGPropertyNode *
458 SGPropertyNode::getAliasTarget () const
460 return (_type == ALIAS ? _value.alias : 0);
465 * Get a non-const child by index.
468 SGPropertyNode::getChild (int position)
470 if (position >= 0 && position < nChildren())
471 return _children[position];
478 * Get a const child by index.
480 const SGPropertyNode *
481 SGPropertyNode::getChild (int position) const
483 if (position >= 0 && position < nChildren())
484 return _children[position];
491 * Get a non-const child by name and index, creating if necessary.
494 SGPropertyNode::getChild (const string &name, int index, bool create)
496 int pos = find_child(name, index, _children);
498 return _children[pos];
500 _children.push_back(new SGPropertyNode(name, index, this));
501 return _children[_children.size()-1];
509 * Get a const child by name and index.
511 const SGPropertyNode *
512 SGPropertyNode::getChild (const string &name, int index) const
514 int pos = find_child(name, index, _children);
516 return _children[pos];
523 * Get all children with the same name (but different indices).
525 vector<SGPropertyNode *>
526 SGPropertyNode::getChildren (const string &name)
528 vector<SGPropertyNode *> children;
529 int max = _children.size();
531 for (int i = 0; i < max; i++)
532 if (_children[i]->getName() == name)
533 children.push_back(_children[i]);
535 sort(children.begin(), children.end(), CompareIndices());
541 * Get all children const with the same name (but different indices).
543 vector<const SGPropertyNode *>
544 SGPropertyNode::getChildren (const string &name) const
546 vector<const SGPropertyNode *> children;
547 int max = _children.size();
549 for (int i = 0; i < max; i++)
550 if (_children[i]->getName() == name)
551 children.push_back(_children[i]);
553 sort(children.begin(), children.end(), CompareIndices());
559 SGPropertyNode::getPath (bool simplify) const
564 string path = _parent->getPath(simplify);
567 if (_index != 0 || !simplify) {
569 sprintf(buffer, "[%d]", _index);
576 SGPropertyNode::getType () const
579 return _value.alias->getType();
586 SGPropertyNode::getBoolValue () const
591 return _value.alias->getBoolValue();
595 return GET_INT == 0 ? false : true;
597 return GET_LONG == 0L ? false : true;
599 return GET_FLOAT == 0.0 ? false : true;
601 return GET_DOUBLE == 0.0L ? false : true;
604 return (GET_STRING == "true" || getDoubleValue() != 0.0L);
607 return false; // if NONE
611 SGPropertyNode::getIntValue () const
616 return _value.alias->getIntValue();
618 return int(GET_BOOL);
622 return int(GET_LONG);
624 return int(GET_FLOAT);
626 return int(GET_DOUBLE);
629 return atoi(GET_STRING.c_str());
636 SGPropertyNode::getLongValue () const
641 return _value.alias->getLongValue();
643 return long(GET_BOOL);
645 return long(GET_INT);
649 return long(GET_FLOAT);
651 return long(GET_DOUBLE);
654 return strtol(GET_STRING.c_str(), 0, 0);
657 return 0L; // if NONE
661 SGPropertyNode::getFloatValue () const
666 return _value.alias->getFloatValue();
668 return float(GET_BOOL);
670 return float(GET_INT);
672 return float(GET_LONG);
676 return float(GET_DOUBLE);
679 return atof(GET_STRING.c_str());
682 return 0.0; // if NONE
686 SGPropertyNode::getDoubleValue () const
691 return _value.alias->getDoubleValue();
693 return double(GET_BOOL);
695 return double(GET_INT);
697 return double(GET_LONG);
699 return double(GET_FLOAT);
704 return strtod(GET_STRING.c_str(), 0);
707 return 0.0L; // if NONE
711 SGPropertyNode::getStringValue () const
718 return _value.alias->getStringValue();
725 sprintf(buf, "%d", GET_INT);
728 sprintf(buf, "%ld", GET_LONG);
731 sprintf(buf, "%f", GET_FLOAT);
734 sprintf(buf, "%f", GET_DOUBLE);
741 return ""; // if NONE
745 SGPropertyNode::setBoolValue (bool value)
748 if (_type == NONE || _type == UNSPECIFIED) {
750 _value.bool_val = new SGRawValueInternal<bool>;
756 return _value.alias->setBoolValue(value);
758 return SET_BOOL(value);
760 return SET_INT(int(value));
762 return SET_LONG(long(value));
764 return SET_FLOAT(float(value));
766 return SET_DOUBLE(double(value));
768 return SET_STRING(value ? "true" : "false");
771 return false; // should never happen
775 SGPropertyNode::setIntValue (int value)
778 if (_type == NONE || _type == UNSPECIFIED) {
780 _value.int_val = new SGRawValueInternal<int>;
786 return _value.alias->setIntValue(value);
788 return SET_BOOL(value == 0 ? false : true);
790 return SET_INT(value);
792 return SET_LONG(long(value));
794 return SET_FLOAT(float(value));
796 return SET_DOUBLE(double(value));
799 sprintf(buf, "%d", value);
800 return SET_STRING(buf);
804 return false; // should never happen
808 SGPropertyNode::setLongValue (long value)
811 if (_type == NONE || _type == UNSPECIFIED) {
813 _value.long_val = new SGRawValueInternal<long>;
819 return _value.alias->setLongValue(value);
821 return SET_BOOL(value == 0L ? false : true);
823 return SET_INT(int(value));
825 return SET_LONG(value);
827 return SET_FLOAT(float(value));
829 return SET_DOUBLE(double(value));
832 sprintf(buf, "%d", value);
833 return SET_STRING(buf);
837 return false; // should never happen
841 SGPropertyNode::setFloatValue (float value)
844 if (_type == NONE || _type == UNSPECIFIED) {
846 _value.float_val = new SGRawValueInternal<float>;
852 return _value.alias->setFloatValue(value);
854 return SET_BOOL(value == 0.0 ? false : true);
856 return SET_INT(int(value));
858 return SET_LONG(long(value));
860 return SET_FLOAT(value);
862 return SET_DOUBLE(double(value));
865 sprintf(buf, "%f", value);
866 return SET_STRING(buf);
870 return false; // should never happen
874 SGPropertyNode::setDoubleValue (double value)
877 if (_type == NONE || _type == UNSPECIFIED) {
879 _value.double_val = new SGRawValueInternal<double>;
885 return _value.alias->setDoubleValue(value);
887 return SET_BOOL(value == 0.0L ? false : true);
889 return SET_INT(int(value));
891 return SET_LONG(long(value));
893 return SET_FLOAT(float(value));
895 return SET_DOUBLE(value);
898 sprintf(buf, "%lf", value);
899 return SET_STRING(buf);
903 return false; // should never happen
907 SGPropertyNode::setStringValue (string value)
910 if (_type == NONE || _type == UNSPECIFIED) {
912 _value.string_val = new SGRawValueInternal<string>;
918 return _value.alias->setStringValue(value);
920 return SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
922 return SET_INT(atoi(value.c_str()));
924 return SET_LONG(strtol(value.c_str(), 0, 0));
926 return SET_FLOAT(atof(value.c_str()));
928 return SET_DOUBLE(strtod(value.c_str(), 0));
930 return SET_STRING(value);
933 return false; // should never happen
937 SGPropertyNode::setUnspecifiedValue (string value)
942 _value.string_val = new SGRawValueInternal<string>;
948 return _value.alias->setUnspecifiedValue(value);
950 return SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
952 return SET_INT(atoi(value.c_str()));
954 return SET_LONG(strtol(value.c_str(), 0, 0));
956 return SET_FLOAT(atof(value.c_str()));
958 return SET_DOUBLE(strtod(value.c_str(), 0));
961 return SET_STRING(value);
964 return false; // should never happen
968 SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
970 if (_type == ALIAS || _tied)
973 bool old_val = false;
975 old_val = getBoolValue();
980 _value.bool_val = rawValue.clone();
983 setBoolValue(old_val);
989 SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
991 if (_type == ALIAS || _tied)
996 old_val = getIntValue();
1001 _value.int_val = rawValue.clone();
1004 setIntValue(old_val);
1010 SGPropertyNode::tie (const SGRawValue<long> &rawValue, bool useDefault)
1012 if (_type == ALIAS || _tied)
1017 old_val = getLongValue();
1022 _value.long_val = rawValue.clone();
1025 setLongValue(old_val);
1031 SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
1033 if (_type == ALIAS || _tied)
1036 float old_val = 0.0;
1038 old_val = getFloatValue();
1043 _value.float_val = rawValue.clone();
1046 setFloatValue(old_val);
1052 SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
1054 if (_type == ALIAS || _tied)
1057 double old_val = 0.0;
1059 old_val = getDoubleValue();
1064 _value.double_val = rawValue.clone();
1067 setDoubleValue(old_val);
1074 SGPropertyNode::tie (const SGRawValue<string> &rawValue, bool useDefault)
1076 if (_type == ALIAS || _tied)
1081 old_val = getStringValue();
1086 _value.string_val = rawValue.clone();
1089 setStringValue(old_val);
1095 SGPropertyNode::untie ()
1102 bool val = getBoolValue();
1105 _value.bool_val = new SGRawValueInternal<bool>;
1110 int val = getIntValue();
1113 _value.int_val = new SGRawValueInternal<int>;
1118 long val = getLongValue();
1121 _value.long_val = new SGRawValueInternal<long>;
1126 float val = getFloatValue();
1129 _value.float_val = new SGRawValueInternal<float>;
1134 double val = getDoubleValue();
1137 _value.double_val = new SGRawValueInternal<double>;
1142 string val = getStringValue();
1145 _value.string_val = new SGRawValueInternal<string>;
1156 SGPropertyNode::getRootNode ()
1161 return _parent->getRootNode();
1164 const SGPropertyNode *
1165 SGPropertyNode::getRootNode () const
1170 return _parent->getRootNode();
1174 SGPropertyNode::getNode (const string &relative_path, bool create)
1176 vector<PathComponent> components;
1177 parse_path(relative_path, components);
1178 return find_node(this, components, 0, create);
1181 const SGPropertyNode *
1182 SGPropertyNode::getNode (const string &relative_path) const
1184 vector<PathComponent> components;
1185 parse_path(relative_path, components);
1186 // FIXME: cast away const
1187 return find_node((SGPropertyNode *)this, components, 0, false);
1192 ////////////////////////////////////////////////////////////////////////
1193 // Convenience methods using relative paths.
1194 ////////////////////////////////////////////////////////////////////////
1198 * Test whether another node has a value attached.
1201 SGPropertyNode::hasValue (const string &relative_path) const
1203 const SGPropertyNode * node = getNode(relative_path);
1204 return (node == 0 ? false : node->hasValue());
1209 * Get the value type for another node.
1211 SGPropertyNode::Type
1212 SGPropertyNode::getType (const string &relative_path) const
1214 const SGPropertyNode * node = getNode(relative_path);
1215 return (node == 0 ? UNSPECIFIED : (Type)(node->getType()));
1220 * Get a bool value for another node.
1223 SGPropertyNode::getBoolValue (const string &relative_path,
1224 bool defaultValue) const
1226 const SGPropertyNode * node = getNode(relative_path);
1227 return (node == 0 ? defaultValue : node->getBoolValue());
1232 * Get an int value for another node.
1235 SGPropertyNode::getIntValue (const string &relative_path,
1236 int defaultValue) const
1238 const SGPropertyNode * node = getNode(relative_path);
1239 return (node == 0 ? defaultValue : node->getIntValue());
1244 * Get a long value for another node.
1247 SGPropertyNode::getLongValue (const string &relative_path,
1248 long defaultValue) const
1250 const SGPropertyNode * node = getNode(relative_path);
1251 return (node == 0 ? defaultValue : node->getLongValue());
1256 * Get a float value for another node.
1259 SGPropertyNode::getFloatValue (const string &relative_path,
1260 float defaultValue) const
1262 const SGPropertyNode * node = getNode(relative_path);
1263 return (node == 0 ? defaultValue : node->getFloatValue());
1268 * Get a double value for another node.
1271 SGPropertyNode::getDoubleValue (const string &relative_path,
1272 double defaultValue) const
1274 const SGPropertyNode * node = getNode(relative_path);
1275 return (node == 0 ? defaultValue : node->getDoubleValue());
1280 * Get a string value for another node.
1283 SGPropertyNode::getStringValue (const string &relative_path,
1284 string defaultValue) const
1286 const SGPropertyNode * node = getNode(relative_path);
1287 return (node == 0 ? defaultValue : node->getStringValue());
1292 * Set a bool value for another node.
1295 SGPropertyNode::setBoolValue (const string &relative_path, bool value)
1297 return getNode(relative_path, true)->setBoolValue(value);
1302 * Set an int value for another node.
1305 SGPropertyNode::setIntValue (const string &relative_path, int value)
1307 return getNode(relative_path, true)->setIntValue(value);
1312 * Set a long value for another node.
1315 SGPropertyNode::setLongValue (const string &relative_path, long value)
1317 return getNode(relative_path, true)->setLongValue(value);
1322 * Set a float value for another node.
1325 SGPropertyNode::setFloatValue (const string &relative_path, float value)
1327 return getNode(relative_path, true)->setFloatValue(value);
1332 * Set a double value for another node.
1335 SGPropertyNode::setDoubleValue (const string &relative_path, double value)
1337 return getNode(relative_path, true)->setDoubleValue(value);
1342 * Set a string value for another node.
1345 SGPropertyNode::setStringValue (const string &relative_path, string value)
1347 return getNode(relative_path, true)->setStringValue(value);
1352 * Set an unknown value for another node.
1355 SGPropertyNode::setUnspecifiedValue (const string &relative_path, string value)
1357 return getNode(relative_path, true)->setUnspecifiedValue(value);
1362 * Test whether another node is tied.
1365 SGPropertyNode::isTied (const string &relative_path) const
1367 const SGPropertyNode * node = getNode(relative_path);
1368 return (node == 0 ? false : node->isTied());
1373 * Tie a node reached by a relative path, creating it if necessary.
1376 SGPropertyNode::tie (const string &relative_path,
1377 const SGRawValue<bool> &rawValue,
1380 return getNode(relative_path, true)->tie(rawValue, useDefault);
1385 * Tie a node reached by a relative path, creating it if necessary.
1388 SGPropertyNode::tie (const string &relative_path,
1389 const SGRawValue<int> &rawValue,
1392 return getNode(relative_path, true)->tie(rawValue, useDefault);
1397 * Tie a node reached by a relative path, creating it if necessary.
1400 SGPropertyNode::tie (const string &relative_path,
1401 const SGRawValue<long> &rawValue,
1404 return getNode(relative_path, true)->tie(rawValue, useDefault);
1409 * Tie a node reached by a relative path, creating it if necessary.
1412 SGPropertyNode::tie (const string &relative_path,
1413 const SGRawValue<float> &rawValue,
1416 return getNode(relative_path, true)->tie(rawValue, useDefault);
1421 * Tie a node reached by a relative path, creating it if necessary.
1424 SGPropertyNode::tie (const string &relative_path,
1425 const SGRawValue<double> &rawValue,
1428 return getNode(relative_path, true)->tie(rawValue, useDefault);
1433 * Tie a node reached by a relative path, creating it if necessary.
1436 SGPropertyNode::tie (const string &relative_path,
1437 const SGRawValue<string> &rawValue,
1440 return getNode(relative_path, true)->tie(rawValue, useDefault);
1445 * Attempt to untie another node reached by a relative path.
1448 SGPropertyNode::untie (const string &relative_path)
1450 SGPropertyNode * node = getNode(relative_path);
1451 return (node == 0 ? false : node->untie());