]> git.mxchange.org Git - simgear.git/blob - simgear/props/props.cxx
Merge branch 'ehofman/config' into next
[simgear.git] / simgear / props / props.cxx
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.
4 //
5 // See props.html for documentation [replace with URL when available].
6 //
7 // $Id$
8
9 #ifdef HAVE_CONFIG_H
10 #  include <simgear_config.h>
11 #endif
12
13 #include "props.hxx"
14
15 #include <algorithm>
16
17 #include <sstream>
18 #include <iomanip>
19 #include <iterator>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include <boost/algorithm/string/find_iterator.hpp>
24 #include <boost/algorithm/string/predicate.hpp>
25 #include <boost/algorithm/string/classification.hpp>
26 #include <boost/bind.hpp>
27 #include <boost/functional/hash.hpp>
28 #include <boost/range.hpp>
29
30 #include <simgear/math/SGMath.hxx>
31
32 #if PROPS_STANDALONE
33 #include <iostream>
34 #else
35
36 #include <simgear/compiler.h>
37 #include <simgear/debug/logstream.hxx>
38
39 #if ( _MSC_VER == 1200 )
40 // MSVC 6 is buggy, and needs something strange here
41 using std::vector<SGPropertyNode_ptr>;
42 using std::vector<SGPropertyChangeListener *>;
43 using std::vector<SGPropertyNode *>;
44 #endif
45 #endif
46
47 #if PROPS_STANDALONE
48 using std::cerr;
49 #endif
50 using std::endl;
51 using std::find;
52 using std::sort;
53 using std::string;
54 using std::vector;
55 using std::stringstream;
56
57 using namespace simgear;
58
59 \f
60 ////////////////////////////////////////////////////////////////////////
61 // Local classes.
62 ////////////////////////////////////////////////////////////////////////
63
64 /**
65  * Comparator class for sorting by index.
66  */
67 class CompareIndices
68 {
69 public:
70   int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
71     return (n1->getIndex() < n2->getIndex());
72   }
73 };
74
75
76 \f
77 ////////////////////////////////////////////////////////////////////////
78 // Convenience macros for value access.
79 ////////////////////////////////////////////////////////////////////////
80
81 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
82 #define TEST_WRITE if (!getAttribute(WRITE)) return false
83 \f
84
85 ////////////////////////////////////////////////////////////////////////
86 // Local path normalization code.
87 ////////////////////////////////////////////////////////////////////////
88
89 /**
90  * Parse the name for a path component.
91  *
92  * Name: [_a-zA-Z][-._a-zA-Z0-9]*
93  */
94
95 template<typename Range>
96 inline Range
97 parse_name (const Range &path)
98 {
99   typename Range::iterator i = path.begin();
100   typename Range::iterator max = path.end();
101
102   if (*i == '.') {
103     i++;
104     if (i != path.end() && *i == '.') {
105       i++;
106     }
107     if (i != max && *i != '/')
108       throw string("illegal character after . or ..");
109   } else if (isalpha(*i) || *i == '_') {
110     i++;
111
112                                 // The rules inside a name are a little
113                                 // less restrictive.
114     while (i != max) {
115       if (isalpha(*i) || isdigit(*i) || *i == '_' ||
116           *i == '-' || *i == '.') {
117         // name += path[i];
118       } else if (*i == '[' || *i == '/') {
119         break;
120       } else {
121         throw string("name may contain only ._- and alphanumeric characters");
122       }
123       i++;
124     }
125   }
126
127   else {
128     if (path.begin() == i)
129       throw string("name must begin with alpha or '_'");
130   }
131   return Range(path.begin(), i);
132 }
133
134 // Validate the name of a single node
135 inline bool validateName(const string& name)
136 {
137   using namespace boost;
138   if (name.empty())
139     return false;
140   if (!isalpha(name[0]) && name[0] != '_')
141     return false;
142   return all(make_iterator_range(name.begin(), name.end()),
143              is_alnum() || is_any_of("_-."));
144 }
145
146 \f
147 ////////////////////////////////////////////////////////////////////////
148 // Other static utility functions.
149 ////////////////////////////////////////////////////////////////////////
150
151
152 static char *
153 copy_string (const char * s)
154 {
155   unsigned long int slen = strlen(s);
156   char * copy = new char[slen + 1];
157
158   // the source string length is known so no need to check for '\0'
159   // when copying every single character
160   memcpy(copy, s, slen);
161   *(copy + slen) = '\0';
162   return copy;
163 }
164
165 static bool
166 compare_strings (const char * s1, const char * s2)
167 {
168   return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
169 }
170
171 /**
172  * Locate a child node by name and index.
173  */
174 template<typename Itr>
175 static int
176 find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
177 {
178   int nNodes = nodes.size();
179   boost::iterator_range<Itr> name(begin, end);
180   for (int i = 0; i < nNodes; i++) {
181     SGPropertyNode * node = nodes[i];
182
183     // searching for a mathing index is a lot less time consuming than
184     // comparing two strings so do that first.
185     if (node->getIndex() == index && boost::equals(node->getName(), name))
186       return i;
187   }
188   return -1;
189 }
190
191 /**
192  * Locate the child node with the highest index of the same name
193  */
194 static int
195 find_last_child (const char * name, const PropertyList& nodes)
196 {
197   int nNodes = nodes.size();
198   int index = 0;
199
200   for (int i = 0; i < nNodes; i++) {
201     SGPropertyNode * node = nodes[i];
202     if (compare_strings(node->getName(), name))
203     {
204       int idx = node->getIndex();
205       if (idx > index) index = idx;
206     }
207   }
208   return index;
209 }
210
211 template<typename Itr>
212 inline SGPropertyNode*
213 SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
214 {
215   int pos = find_child(begin, end, index, _children);
216   if (pos >= 0) {
217     return _children[pos];
218   } else if (create) {
219     SGPropertyNode_ptr node;
220     pos = find_child(begin, end, index, _removedChildren);
221     if (pos >= 0) {
222       PropertyList::iterator it = _removedChildren.begin();
223       it += pos;
224       node = _removedChildren[pos];
225       _removedChildren.erase(it);
226       node->setAttribute(REMOVED, false);
227       _children.push_back(node);
228       fireChildAdded(node);
229       return node;      
230     }
231   }
232   return 0;
233 }
234     
235 template<typename Itr>
236 SGPropertyNode *
237 SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
238 {
239     SGPropertyNode* node = getExistingChild(begin, end, index, create);
240
241     if (node) {
242       return node;
243     } else if (create) {
244       node = new SGPropertyNode(begin, end, index, this);
245       _children.push_back(node);
246       fireChildAdded(node);
247       return node;
248     } else {
249       return 0;
250     }
251 }
252
253 template<typename SplitItr>
254 SGPropertyNode*
255 find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
256               int last_index)
257 {
258   typedef typename SplitItr::value_type Range;
259   // Run off the end of the list
260   if (current == 0) {
261     return 0;
262   }
263
264   // Success! This is the one we want.
265   if (itr.eof())
266     return current;
267   Range token = *itr;
268   // Empty name at this point is empty, not root.
269   if (token.empty())
270     return find_node_aux(current, ++itr, create, last_index);
271   Range name = parse_name(token);
272   if (equals(name, "."))
273     return find_node_aux(current, ++itr, create, last_index);
274   if (equals(name, "..")) {
275     SGPropertyNode * parent = current->getParent();
276     if (parent == 0)
277       throw string("attempt to move past root with '..'");
278     return find_node_aux(parent, ++itr, create, last_index);
279   }
280   int index = -1;
281   if (last_index >= 0) {
282     // If we are at the last token and last_index is valid, use
283     // last_index as the index value
284     bool lastTok = true;
285     while (!(++itr).eof()) {
286       if (!itr->empty()) {
287         lastTok = false;
288         break;
289       }
290     }
291     if (lastTok)
292       index = last_index;
293   } else {
294     ++itr;
295   }
296
297   if (index < 0) {
298     index = 0;
299     if (name.end() != token.end()) {
300       if (*name.end() == '[') {
301         typename Range::iterator i = name.end() + 1, end = token.end();
302         for (;i != end; ++i) {
303           if (isdigit(*i)) {
304             index = (index * 10) + (*i - '0');
305           } else {
306             break;
307           }
308         }
309         if (i == token.end() || *i != ']')
310           throw string("unterminated index (looking for ']')");
311       } else {
312         throw string("illegal characters in token: ")
313           + string(name.begin(), name.end());
314       }
315     }
316   }
317   return find_node_aux(current->getChildImpl(name.begin(), name.end(),
318                                              index, create), itr, create,
319                        last_index);
320 }
321
322 // Internal function for parsing property paths. last_index provides
323 // and index value for the last node name token, if supplied.
324 template<typename Range>
325 SGPropertyNode*
326 find_node (SGPropertyNode * current,
327            const Range& path,
328            bool create,
329            int last_index = -1)
330 {
331   using namespace boost;
332   typedef split_iterator<typename range_result_iterator<Range>::type>
333     PathSplitIterator;
334   
335   PathSplitIterator itr
336     = make_split_iterator(path, first_finder("/", is_equal()));
337   if (*path.begin() == '/')
338     return find_node_aux(current->getRootNode(), itr, create, last_index);
339    else
340      return find_node_aux(current, itr, create, last_index);
341 }
342
343 \f
344 ////////////////////////////////////////////////////////////////////////
345 // Private methods from SGPropertyNode (may be inlined for speed).
346 ////////////////////////////////////////////////////////////////////////
347
348 inline bool
349 SGPropertyNode::get_bool () const
350 {
351   if (_tied)
352     return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
353   else
354     return _local_val.bool_val;
355 }
356
357 inline int
358 SGPropertyNode::get_int () const
359 {
360   if (_tied)
361       return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
362   else
363     return _local_val.int_val;
364 }
365
366 inline long
367 SGPropertyNode::get_long () const
368 {
369   if (_tied)
370     return static_cast<SGRawValue<long>*>(_value.val)->getValue();
371   else
372     return _local_val.long_val;
373 }
374
375 inline float
376 SGPropertyNode::get_float () const
377 {
378   if (_tied)
379     return static_cast<SGRawValue<float>*>(_value.val)->getValue();
380   else
381     return _local_val.float_val;
382 }
383
384 inline double
385 SGPropertyNode::get_double () const
386 {
387   if (_tied)
388     return static_cast<SGRawValue<double>*>(_value.val)->getValue();
389   else
390     return _local_val.double_val;
391 }
392
393 inline const char *
394 SGPropertyNode::get_string () const
395 {
396   if (_tied)
397       return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
398   else
399     return _local_val.string_val;
400 }
401
402 inline bool
403 SGPropertyNode::set_bool (bool val)
404 {
405   if (_tied) {
406     if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
407       fireValueChanged();
408       return true;
409     } else {
410       return false;
411     }
412   } else {
413     _local_val.bool_val = val;
414     fireValueChanged();
415     return true;
416   }
417 }
418
419 inline bool
420 SGPropertyNode::set_int (int val)
421 {
422   if (_tied) {
423     if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
424       fireValueChanged();
425       return true;
426     } else {
427       return false;
428     }
429   } else {
430     _local_val.int_val = val;
431     fireValueChanged();
432     return true;
433   }
434 }
435
436 inline bool
437 SGPropertyNode::set_long (long val)
438 {
439   if (_tied) {
440     if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
441       fireValueChanged();
442       return true;
443     } else {
444       return false;
445     }
446   } else {
447     _local_val.long_val = val;
448     fireValueChanged();
449     return true;
450   }
451 }
452
453 inline bool
454 SGPropertyNode::set_float (float val)
455 {
456   if (_tied) {
457     if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
458       fireValueChanged();
459       return true;
460     } else {
461       return false;
462     }
463   } else {
464     _local_val.float_val = val;
465     fireValueChanged();
466     return true;
467   }
468 }
469
470 inline bool
471 SGPropertyNode::set_double (double val)
472 {
473   if (_tied) {
474     if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
475       fireValueChanged();
476       return true;
477     } else {
478       return false;
479     }
480   } else {
481     _local_val.double_val = val;
482     fireValueChanged();
483     return true;
484   }
485 }
486
487 inline bool
488 SGPropertyNode::set_string (const char * val)
489 {
490   if (_tied) {
491       if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
492       fireValueChanged();
493       return true;
494     } else {
495       return false;
496     }
497   } else {
498     delete [] _local_val.string_val;
499     _local_val.string_val = copy_string(val);
500     fireValueChanged();
501     return true;
502   }
503 }
504
505 void
506 SGPropertyNode::clearValue ()
507 {
508     if (_type == props::ALIAS) {
509         put(_value.alias);
510         _value.alias = 0;
511     } else if (_type != props::NONE) {
512         switch (_type) {
513         case props::BOOL:
514             _local_val.bool_val = SGRawValue<bool>::DefaultValue();
515             break;
516         case props::INT:
517             _local_val.int_val = SGRawValue<int>::DefaultValue();
518             break;
519         case props::LONG:
520             _local_val.long_val = SGRawValue<long>::DefaultValue();
521             break;
522         case props::FLOAT:
523             _local_val.float_val = SGRawValue<float>::DefaultValue();
524             break;
525         case props::DOUBLE:
526             _local_val.double_val = SGRawValue<double>::DefaultValue();
527             break;
528         case props::STRING:
529         case props::UNSPECIFIED:
530             if (!_tied) {
531                 delete [] _local_val.string_val;
532             }
533             _local_val.string_val = 0;
534             break;
535         default: // avoid compiler warning
536             break;
537         }
538         delete _value.val;
539         _value.val = 0;
540     }
541     _tied = false;
542     _type = props::NONE;
543 }
544
545
546 /**
547  * Get the value as a string.
548  */
549 const char *
550 SGPropertyNode::make_string () const
551 {
552     if (!getAttribute(READ))
553         return "";
554     switch (_type) {
555     case props::ALIAS:
556         return _value.alias->getStringValue();
557     case props::BOOL:
558         return get_bool() ? "true" : "false";
559     case props::STRING:
560     case props::UNSPECIFIED:
561         return get_string();
562     case props::NONE:
563         return "";
564     default:
565         break;
566     }
567     stringstream sstr;
568     switch (_type) {
569     case props::INT:
570         sstr << get_int();
571         break;
572     case props::LONG:
573         sstr << get_long();
574         break;
575     case props::FLOAT:
576         sstr << get_float();
577         break;
578     case props::DOUBLE:
579         sstr << std::setprecision(10) << get_double();
580         break;
581     case props::EXTENDED:
582     {
583         props::Type realType = _value.val->getType();
584         // Perhaps this should be done for all types?
585         if (realType == props::VEC3D || realType == props::VEC4D)
586             sstr.precision(10);
587         static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
588     }
589         break;
590     default:
591         return "";
592     }
593     _buffer = sstr.str();
594     return _buffer.c_str();
595 }
596
597 /**
598  * Trace a write access for a property.
599  */
600 void
601 SGPropertyNode::trace_write () const
602 {
603 #if PROPS_STANDALONE
604   cerr << "TRACE: Write node " << getPath () << ", value \""
605        << make_string() << '"' << endl;
606 #else
607   SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
608          << ", value \"" << make_string() << '"');
609 #endif
610 }
611
612 /**
613  * Trace a read access for a property.
614  */
615 void
616 SGPropertyNode::trace_read () const
617 {
618 #if PROPS_STANDALONE
619   cerr << "TRACE: Write node " << getPath () << ", value \""
620        << make_string() << '"' << endl;
621 #else
622   SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
623          << ", value \"" << make_string() << '"');
624 #endif
625 }
626
627 \f
628 ////////////////////////////////////////////////////////////////////////
629 // Public methods from SGPropertyNode.
630 ////////////////////////////////////////////////////////////////////////
631
632 /**
633  * Last used attribute
634  * Update as needed when enum Attribute is changed
635  */
636 const int SGPropertyNode::LAST_USED_ATTRIBUTE = USERARCHIVE;
637
638 /**
639  * Default constructor: always creates a root node.
640  */
641 SGPropertyNode::SGPropertyNode ()
642   : _index(0),
643     _parent(0),
644     _path_cache(0),
645     _type(props::NONE),
646     _tied(false),
647     _attr(READ|WRITE),
648     _listeners(0)
649 {
650   _local_val.string_val = 0;
651   _value.val = 0;
652 }
653
654
655 /**
656  * Copy constructor.
657  */
658 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
659   : _index(node._index),
660     _name(node._name),
661     _parent(0),                 // don't copy the parent
662     _path_cache(0),
663     _type(node._type),
664     _tied(node._tied),
665     _attr(node._attr),
666     _listeners(0)               // CHECK!!
667 {
668   _local_val.string_val = 0;
669   _value.val = 0;
670   if (_type == props::NONE)
671     return;
672   if (_type == props::ALIAS) {
673     _value.alias = node._value.alias;
674     get(_value.alias);
675     _tied = false;
676     return;
677   }
678   if (_tied || _type == props::EXTENDED) {
679     _value.val = node._value.val->clone();
680     return;
681   }
682   switch (_type) {
683   case props::BOOL:
684     set_bool(node.get_bool());    
685     break;
686   case props::INT:
687     set_int(node.get_int());
688     break;
689   case props::LONG:
690     set_long(node.get_long());
691     break;
692   case props::FLOAT:
693     set_float(node.get_float());
694     break;
695   case props::DOUBLE:
696     set_double(node.get_double());
697     break;
698   case props::STRING:
699   case props::UNSPECIFIED:
700     set_string(node.get_string());
701     break;
702   default:
703     break;
704   }
705 }
706
707
708 /**
709  * Convenience constructor.
710  */
711 template<typename Itr>
712 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
713                                 int index,
714                                 SGPropertyNode * parent)
715   : _index(index),
716     _name(begin, end),
717     _parent(parent),
718     _path_cache(0),
719     _type(props::NONE),
720     _tied(false),
721     _attr(READ|WRITE),
722     _listeners(0)
723 {
724   _local_val.string_val = 0;
725   _value.val = 0;
726   if (!validateName(_name))
727     throw string("plain name expected instead of '") + _name + '\'';
728 }
729
730 SGPropertyNode::SGPropertyNode (const string& name,
731                                 int index,
732                                 SGPropertyNode * parent)
733   : _index(index),
734     _name(name),
735     _parent(parent),
736     _path_cache(0),
737     _type(props::NONE),
738     _tied(false),
739     _attr(READ|WRITE),
740     _listeners(0)
741 {
742   _local_val.string_val = 0;
743   _value.val = 0;
744   if (!validateName(name))
745     throw string("plain name expected instead of '") + _name + '\'';
746 }
747
748 /**
749  * Destructor.
750  */
751 SGPropertyNode::~SGPropertyNode ()
752 {
753   // zero out all parent pointers, else they might be dangling
754   for (unsigned i = 0; i < _children.size(); ++i)
755     _children[i]->_parent = 0;
756   for (unsigned i = 0; i < _removedChildren.size(); ++i)
757     _removedChildren[i]->_parent = 0;
758   delete _path_cache;
759   clearValue();
760
761   if (_listeners) {
762     vector<SGPropertyChangeListener*>::iterator it;
763     for (it = _listeners->begin(); it != _listeners->end(); ++it)
764       (*it)->unregister_property(this);
765     delete _listeners;
766   }
767 }
768
769
770 /**
771  * Alias to another node.
772  */
773 bool
774 SGPropertyNode::alias (SGPropertyNode * target)
775 {
776   if (target == 0 || _type == props::ALIAS || _tied)
777     return false;
778   clearValue();
779   get(target);
780   _value.alias = target;
781   _type = props::ALIAS;
782   return true;
783 }
784
785
786 /**
787  * Alias to another node by path.
788  */
789 bool
790 SGPropertyNode::alias (const char * path)
791 {
792   return alias(getNode(path, true));
793 }
794
795
796 /**
797  * Remove an alias.
798  */
799 bool
800 SGPropertyNode::unalias ()
801 {
802   if (_type != props::ALIAS)
803     return false;
804   clearValue();
805   return true;
806 }
807
808
809 /**
810  * Get the target of an alias.
811  */
812 SGPropertyNode *
813 SGPropertyNode::getAliasTarget ()
814 {
815   return (_type == props::ALIAS ? _value.alias : 0);
816 }
817
818
819 const SGPropertyNode *
820 SGPropertyNode::getAliasTarget () const
821 {
822   return (_type == props::ALIAS ? _value.alias : 0);
823 }
824
825 /**
826  * create a non-const child by name after the last node with the same name.
827  */
828 SGPropertyNode *
829 SGPropertyNode::addChild (const char * name)
830 {
831   int pos = find_last_child(name, _children)+1;
832
833   SGPropertyNode_ptr node;
834   node = new SGPropertyNode(name, name + strlen(name), pos, this);
835   _children.push_back(node);
836   fireChildAdded(node);
837   return node;
838 }
839
840
841 /**
842  * Get a non-const child by index.
843  */
844 SGPropertyNode *
845 SGPropertyNode::getChild (int position)
846 {
847   if (position >= 0 && position < nChildren())
848     return _children[position];
849   else
850     return 0;
851 }
852
853
854 /**
855  * Get a const child by index.
856  */
857 const SGPropertyNode *
858 SGPropertyNode::getChild (int position) const
859 {
860   if (position >= 0 && position < nChildren())
861     return _children[position];
862   else
863     return 0;
864 }
865
866
867 /**
868  * Get a non-const child by name and index, creating if necessary.
869  */
870
871 SGPropertyNode *
872 SGPropertyNode::getChild (const char * name, int index, bool create)
873 {
874   return getChildImpl(name, name + strlen(name), index, create);
875 }
876
877 SGPropertyNode *
878 SGPropertyNode::getChild (const string& name, int index, bool create)
879 {
880   SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
881                                           create);
882   if (node) {
883       return node;
884     } else if (create) {
885       node = new SGPropertyNode(name, index, this);
886       _children.push_back(node);
887       fireChildAdded(node);
888       return node;
889     } else {
890       return 0;
891     }
892 }
893
894 /**
895  * Get a const child by name and index.
896  */
897 const SGPropertyNode *
898 SGPropertyNode::getChild (const char * name, int index) const
899 {
900   int pos = find_child(name, name + strlen(name), index, _children);
901   if (pos >= 0)
902     return _children[pos];
903   else
904     return 0;
905 }
906
907
908 /**
909  * Get all children with the same name (but different indices).
910  */
911 PropertyList
912 SGPropertyNode::getChildren (const char * name) const
913 {
914   PropertyList children;
915   int max = _children.size();
916
917   for (int i = 0; i < max; i++)
918     if (compare_strings(_children[i]->getName(), name))
919       children.push_back(_children[i]);
920
921   sort(children.begin(), children.end(), CompareIndices());
922   return children;
923 }
924
925
926 /**
927  * Remove this node and all children from nodes that link to them
928  * in their path cache.
929  */
930 void
931 SGPropertyNode::remove_from_path_caches ()
932 {
933   for (unsigned int i = 0; i < _children.size(); ++i)
934     _children[i]->remove_from_path_caches();
935
936   for (unsigned int i = 0; i < _linkedNodes.size(); i++)
937     _linkedNodes[i]->erase(this);
938   _linkedNodes.clear();
939 }
940
941
942 /**
943  * Remove child by position.
944  */
945 SGPropertyNode_ptr
946 SGPropertyNode::removeChild (int pos, bool keep)
947 {
948   SGPropertyNode_ptr node;
949   if (pos < 0 || pos >= (int)_children.size())
950     return node;
951
952   PropertyList::iterator it = _children.begin();
953   it += pos;
954   node = _children[pos];
955   _children.erase(it);
956   if (keep) {
957     _removedChildren.push_back(node);
958   }
959
960   node->remove_from_path_caches();
961   node->setAttribute(REMOVED, true);
962   node->clearValue();
963   fireChildRemoved(node);
964   return node;
965 }
966
967
968 /**
969  * Remove a child node
970  */
971 SGPropertyNode_ptr
972 SGPropertyNode::removeChild (const char * name, int index, bool keep)
973 {
974   SGPropertyNode_ptr ret;
975   int pos = find_child(name, name + strlen(name), index, _children);
976   if (pos >= 0)
977     ret = removeChild(pos, keep);
978   return ret;
979 }
980
981
982 /**
983   * Remove all children with the specified name.
984   */
985 PropertyList
986 SGPropertyNode::removeChildren (const char * name, bool keep)
987 {
988   PropertyList children;
989
990   for (int pos = _children.size() - 1; pos >= 0; pos--)
991     if (compare_strings(_children[pos]->getName(), name))
992       children.push_back(removeChild(pos, keep));
993
994   sort(children.begin(), children.end(), CompareIndices());
995   return children;
996 }
997
998
999 /**
1000   * Remove a linked node.
1001   */
1002 bool
1003 SGPropertyNode::remove_linked_node (hash_table * node)
1004 {
1005   for (unsigned int i = 0; i < _linkedNodes.size(); i++) {
1006     if (_linkedNodes[i] == node) {
1007       vector<hash_table *>::iterator it = _linkedNodes.begin();
1008       it += i;
1009       _linkedNodes.erase(it);
1010       return true;
1011     }
1012   }
1013   return false;
1014 }
1015
1016
1017 string
1018 SGPropertyNode::getDisplayName (bool simplify) const
1019 {
1020   string display_name = _name;
1021   if (_index != 0 || !simplify) {
1022     stringstream sstr;
1023     sstr << '[' << _index << ']';
1024     display_name += sstr.str();
1025   }
1026   return display_name;
1027 }
1028
1029
1030 const char *
1031 SGPropertyNode::getPath (bool simplify) const
1032 {
1033   // Calculate the complete path only once.
1034   if (_parent != 0 && _path.empty()) {
1035     _path = _parent->getPath(simplify);
1036     _path += '/';
1037     _path += getDisplayName(simplify);
1038   }
1039
1040   return _path.c_str();
1041 }
1042
1043 props::Type
1044 SGPropertyNode::getType () const
1045 {
1046   if (_type == props::ALIAS)
1047     return _value.alias->getType();
1048   else if (_type == props::EXTENDED)
1049       return _value.val->getType();
1050   else
1051     return _type;
1052 }
1053
1054
1055 bool 
1056 SGPropertyNode::getBoolValue () const
1057 {
1058                                 // Shortcut for common case
1059   if (_attr == (READ|WRITE) && _type == props::BOOL)
1060     return get_bool();
1061
1062   if (getAttribute(TRACE_READ))
1063     trace_read();
1064   if (!getAttribute(READ))
1065     return SGRawValue<bool>::DefaultValue();
1066   switch (_type) {
1067   case props::ALIAS:
1068     return _value.alias->getBoolValue();
1069   case props::BOOL:
1070     return get_bool();
1071   case props::INT:
1072     return get_int() == 0 ? false : true;
1073   case props::LONG:
1074     return get_long() == 0L ? false : true;
1075   case props::FLOAT:
1076     return get_float() == 0.0 ? false : true;
1077   case props::DOUBLE:
1078     return get_double() == 0.0L ? false : true;
1079   case props::STRING:
1080   case props::UNSPECIFIED:
1081     return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1082   case props::NONE:
1083   default:
1084     return SGRawValue<bool>::DefaultValue();
1085   }
1086 }
1087
1088 int 
1089 SGPropertyNode::getIntValue () const
1090 {
1091                                 // Shortcut for common case
1092   if (_attr == (READ|WRITE) && _type == props::INT)
1093     return get_int();
1094
1095   if (getAttribute(TRACE_READ))
1096     trace_read();
1097   if (!getAttribute(READ))
1098     return SGRawValue<int>::DefaultValue();
1099   switch (_type) {
1100   case props::ALIAS:
1101     return _value.alias->getIntValue();
1102   case props::BOOL:
1103     return int(get_bool());
1104   case props::INT:
1105     return get_int();
1106   case props::LONG:
1107     return int(get_long());
1108   case props::FLOAT:
1109     return int(get_float());
1110   case props::DOUBLE:
1111     return int(get_double());
1112   case props::STRING:
1113   case props::UNSPECIFIED:
1114     return atoi(get_string());
1115   case props::NONE:
1116   default:
1117     return SGRawValue<int>::DefaultValue();
1118   }
1119 }
1120
1121 long 
1122 SGPropertyNode::getLongValue () const
1123 {
1124                                 // Shortcut for common case
1125   if (_attr == (READ|WRITE) && _type == props::LONG)
1126     return get_long();
1127
1128   if (getAttribute(TRACE_READ))
1129     trace_read();
1130   if (!getAttribute(READ))
1131     return SGRawValue<long>::DefaultValue();
1132   switch (_type) {
1133   case props::ALIAS:
1134     return _value.alias->getLongValue();
1135   case props::BOOL:
1136     return long(get_bool());
1137   case props::INT:
1138     return long(get_int());
1139   case props::LONG:
1140     return get_long();
1141   case props::FLOAT:
1142     return long(get_float());
1143   case props::DOUBLE:
1144     return long(get_double());
1145   case props::STRING:
1146   case props::UNSPECIFIED:
1147     return strtol(get_string(), 0, 0);
1148   case props::NONE:
1149   default:
1150     return SGRawValue<long>::DefaultValue();
1151   }
1152 }
1153
1154 float 
1155 SGPropertyNode::getFloatValue () const
1156 {
1157                                 // Shortcut for common case
1158   if (_attr == (READ|WRITE) && _type == props::FLOAT)
1159     return get_float();
1160
1161   if (getAttribute(TRACE_READ))
1162     trace_read();
1163   if (!getAttribute(READ))
1164     return SGRawValue<float>::DefaultValue();
1165   switch (_type) {
1166   case props::ALIAS:
1167     return _value.alias->getFloatValue();
1168   case props::BOOL:
1169     return float(get_bool());
1170   case props::INT:
1171     return float(get_int());
1172   case props::LONG:
1173     return float(get_long());
1174   case props::FLOAT:
1175     return get_float();
1176   case props::DOUBLE:
1177     return float(get_double());
1178   case props::STRING:
1179   case props::UNSPECIFIED:
1180     return atof(get_string());
1181   case props::NONE:
1182   default:
1183     return SGRawValue<float>::DefaultValue();
1184   }
1185 }
1186
1187 double 
1188 SGPropertyNode::getDoubleValue () const
1189 {
1190                                 // Shortcut for common case
1191   if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1192     return get_double();
1193
1194   if (getAttribute(TRACE_READ))
1195     trace_read();
1196   if (!getAttribute(READ))
1197     return SGRawValue<double>::DefaultValue();
1198
1199   switch (_type) {
1200   case props::ALIAS:
1201     return _value.alias->getDoubleValue();
1202   case props::BOOL:
1203     return double(get_bool());
1204   case props::INT:
1205     return double(get_int());
1206   case props::LONG:
1207     return double(get_long());
1208   case props::FLOAT:
1209     return double(get_float());
1210   case props::DOUBLE:
1211     return get_double();
1212   case props::STRING:
1213   case props::UNSPECIFIED:
1214     return strtod(get_string(), 0);
1215   case props::NONE:
1216   default:
1217     return SGRawValue<double>::DefaultValue();
1218   }
1219 }
1220
1221 const char *
1222 SGPropertyNode::getStringValue () const
1223 {
1224                                 // Shortcut for common case
1225   if (_attr == (READ|WRITE) && _type == props::STRING)
1226     return get_string();
1227
1228   if (getAttribute(TRACE_READ))
1229     trace_read();
1230   if (!getAttribute(READ))
1231     return SGRawValue<const char *>::DefaultValue();
1232   return make_string();
1233 }
1234
1235 bool
1236 SGPropertyNode::setBoolValue (bool value)
1237 {
1238                                 // Shortcut for common case
1239   if (_attr == (READ|WRITE) && _type == props::BOOL)
1240     return set_bool(value);
1241
1242   bool result = false;
1243   TEST_WRITE;
1244   if (_type == props::NONE || _type == props::UNSPECIFIED) {
1245     clearValue();
1246     _tied = false;
1247     _type = props::BOOL;
1248   }
1249
1250   switch (_type) {
1251   case props::ALIAS:
1252     result = _value.alias->setBoolValue(value);
1253     break;
1254   case props::BOOL:
1255     result = set_bool(value);
1256     break;
1257   case props::INT:
1258     result = set_int(int(value));
1259     break;
1260   case props::LONG:
1261     result = set_long(long(value));
1262     break;
1263   case props::FLOAT:
1264     result = set_float(float(value));
1265     break;
1266   case props::DOUBLE:
1267     result = set_double(double(value));
1268     break;
1269   case props::STRING:
1270   case props::UNSPECIFIED:
1271     result = set_string(value ? "true" : "false");
1272     break;
1273   case props::NONE:
1274   default:
1275     break;
1276   }
1277
1278   if (getAttribute(TRACE_WRITE))
1279     trace_write();
1280   return result;
1281 }
1282
1283 bool
1284 SGPropertyNode::setIntValue (int value)
1285 {
1286                                 // Shortcut for common case
1287   if (_attr == (READ|WRITE) && _type == props::INT)
1288     return set_int(value);
1289
1290   bool result = false;
1291   TEST_WRITE;
1292   if (_type == props::NONE || _type == props::UNSPECIFIED) {
1293     clearValue();
1294     _type = props::INT;
1295     _local_val.int_val = 0;
1296   }
1297
1298   switch (_type) {
1299   case props::ALIAS:
1300     result = _value.alias->setIntValue(value);
1301     break;
1302   case props::BOOL:
1303     result = set_bool(value == 0 ? false : true);
1304     break;
1305   case props::INT:
1306     result = set_int(value);
1307     break;
1308   case props::LONG:
1309     result = set_long(long(value));
1310     break;
1311   case props::FLOAT:
1312     result = set_float(float(value));
1313     break;
1314   case props::DOUBLE:
1315     result = set_double(double(value));
1316     break;
1317   case props::STRING:
1318   case props::UNSPECIFIED: {
1319     char buf[128];
1320     sprintf(buf, "%d", value);
1321     result = set_string(buf);
1322     break;
1323   }
1324   case props::NONE:
1325   default:
1326     break;
1327   }
1328
1329   if (getAttribute(TRACE_WRITE))
1330     trace_write();
1331   return result;
1332 }
1333
1334 bool
1335 SGPropertyNode::setLongValue (long value)
1336 {
1337                                 // Shortcut for common case
1338   if (_attr == (READ|WRITE) && _type == props::LONG)
1339     return set_long(value);
1340
1341   bool result = false;
1342   TEST_WRITE;
1343   if (_type == props::NONE || _type == props::UNSPECIFIED) {
1344     clearValue();
1345     _type = props::LONG;
1346     _local_val.long_val = 0L;
1347   }
1348
1349   switch (_type) {
1350   case props::ALIAS:
1351     result = _value.alias->setLongValue(value);
1352     break;
1353   case props::BOOL:
1354     result = set_bool(value == 0L ? false : true);
1355     break;
1356   case props::INT:
1357     result = set_int(int(value));
1358     break;
1359   case props::LONG:
1360     result = set_long(value);
1361     break;
1362   case props::FLOAT:
1363     result = set_float(float(value));
1364     break;
1365   case props::DOUBLE:
1366     result = set_double(double(value));
1367     break;
1368   case props::STRING:
1369   case props::UNSPECIFIED: {
1370     char buf[128];
1371     sprintf(buf, "%ld", value);
1372     result = set_string(buf);
1373     break;
1374   }
1375   case props::NONE:
1376   default:
1377     break;
1378   }
1379
1380   if (getAttribute(TRACE_WRITE))
1381     trace_write();
1382   return result;
1383 }
1384
1385 bool
1386 SGPropertyNode::setFloatValue (float value)
1387 {
1388                                 // Shortcut for common case
1389   if (_attr == (READ|WRITE) && _type == props::FLOAT)
1390     return set_float(value);
1391
1392   bool result = false;
1393   TEST_WRITE;
1394   if (_type == props::NONE || _type == props::UNSPECIFIED) {
1395     clearValue();
1396     _type = props::FLOAT;
1397     _local_val.float_val = 0;
1398   }
1399
1400   switch (_type) {
1401   case props::ALIAS:
1402     result = _value.alias->setFloatValue(value);
1403     break;
1404   case props::BOOL:
1405     result = set_bool(value == 0.0 ? false : true);
1406     break;
1407   case props::INT:
1408     result = set_int(int(value));
1409     break;
1410   case props::LONG:
1411     result = set_long(long(value));
1412     break;
1413   case props::FLOAT:
1414     result = set_float(value);
1415     break;
1416   case props::DOUBLE:
1417     result = set_double(double(value));
1418     break;
1419   case props::STRING:
1420   case props::UNSPECIFIED: {
1421     char buf[128];
1422     sprintf(buf, "%f", value);
1423     result = set_string(buf);
1424     break;
1425   }
1426   case props::NONE:
1427   default:
1428     break;
1429   }
1430
1431   if (getAttribute(TRACE_WRITE))
1432     trace_write();
1433   return result;
1434 }
1435
1436 bool
1437 SGPropertyNode::setDoubleValue (double value)
1438 {
1439                                 // Shortcut for common case
1440   if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1441     return set_double(value);
1442
1443   bool result = false;
1444   TEST_WRITE;
1445   if (_type == props::NONE || _type == props::UNSPECIFIED) {
1446     clearValue();
1447     _local_val.double_val = value;
1448     _type = props::DOUBLE;
1449   }
1450
1451   switch (_type) {
1452   case props::ALIAS:
1453     result = _value.alias->setDoubleValue(value);
1454     break;
1455   case props::BOOL:
1456     result = set_bool(value == 0.0L ? false : true);
1457     break;
1458   case props::INT:
1459     result = set_int(int(value));
1460     break;
1461   case props::LONG:
1462     result = set_long(long(value));
1463     break;
1464   case props::FLOAT:
1465     result = set_float(float(value));
1466     break;
1467   case props::DOUBLE:
1468     result = set_double(value);
1469     break;
1470   case props::STRING:
1471   case props::UNSPECIFIED: {
1472     char buf[128];
1473     sprintf(buf, "%f", value);
1474     result = set_string(buf);
1475     break;
1476   }
1477   case props::NONE:
1478   default:
1479     break;
1480   }
1481
1482   if (getAttribute(TRACE_WRITE))
1483     trace_write();
1484   return result;
1485 }
1486
1487 bool
1488 SGPropertyNode::setStringValue (const char * value)
1489 {
1490                                 // Shortcut for common case
1491   if (_attr == (READ|WRITE) && _type == props::STRING)
1492     return set_string(value);
1493
1494   bool result = false;
1495   TEST_WRITE;
1496   if (_type == props::NONE || _type == props::UNSPECIFIED) {
1497     clearValue();
1498     _type = props::STRING;
1499   }
1500
1501   switch (_type) {
1502   case props::ALIAS:
1503     result = _value.alias->setStringValue(value);
1504     break;
1505   case props::BOOL:
1506     result = set_bool((compare_strings(value, "true")
1507                        || atoi(value)) ? true : false);
1508     break;
1509   case props::INT:
1510     result = set_int(atoi(value));
1511     break;
1512   case props::LONG:
1513     result = set_long(strtol(value, 0, 0));
1514     break;
1515   case props::FLOAT:
1516     result = set_float(atof(value));
1517     break;
1518   case props::DOUBLE:
1519     result = set_double(strtod(value, 0));
1520     break;
1521   case props::STRING:
1522   case props::UNSPECIFIED:
1523     result = set_string(value);
1524     break;
1525   case props::EXTENDED:
1526   {
1527     stringstream sstr(value);
1528     static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1529   }
1530   break;
1531   case props::NONE:
1532   default:
1533     break;
1534   }
1535
1536   if (getAttribute(TRACE_WRITE))
1537     trace_write();
1538   return result;
1539 }
1540
1541 bool
1542 SGPropertyNode::setUnspecifiedValue (const char * value)
1543 {
1544   bool result = false;
1545   TEST_WRITE;
1546   if (_type == props::NONE) {
1547     clearValue();
1548     _type = props::UNSPECIFIED;
1549   }
1550   props::Type type = _type;
1551   if (type == props::EXTENDED)
1552       type = _value.val->getType();
1553   switch (type) {
1554   case props::ALIAS:
1555     result = _value.alias->setUnspecifiedValue(value);
1556     break;
1557   case props::BOOL:
1558     result = set_bool((compare_strings(value, "true")
1559                        || atoi(value)) ? true : false);
1560     break;
1561   case props::INT:
1562     result = set_int(atoi(value));
1563     break;
1564   case props::LONG:
1565     result = set_long(strtol(value, 0, 0));
1566     break;
1567   case props::FLOAT:
1568     result = set_float(atof(value));
1569     break;
1570   case props::DOUBLE:
1571     result = set_double(strtod(value, 0));
1572     break;
1573   case props::STRING:
1574   case props::UNSPECIFIED:
1575     result = set_string(value);
1576     break;
1577   case props::VEC3D:
1578       result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1579       break;
1580   case props::VEC4D:
1581       result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1582       break;
1583   case props::NONE:
1584   default:
1585     break;
1586   }
1587
1588   if (getAttribute(TRACE_WRITE))
1589     trace_write();
1590   return result;
1591 }
1592
1593 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1594 {
1595     if (!getAttribute(READ))
1596         return stream;
1597     switch (_type) {
1598     case props::ALIAS:
1599         return _value.alias->printOn(stream);
1600     case props::BOOL:
1601         stream << (get_bool() ? "true" : "false");
1602         break;
1603     case props::INT:
1604         stream << get_int();
1605         break;
1606     case props::LONG:
1607         stream << get_long();
1608         break;
1609     case props::FLOAT:
1610         stream << get_float();
1611         break;
1612     case props::DOUBLE:
1613         stream << get_double();
1614         break;
1615     case props::STRING:
1616     case props::UNSPECIFIED:
1617         stream << get_string();
1618         break;
1619     case props::EXTENDED:
1620         static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1621         break;
1622     case props::NONE:
1623         break;
1624     default: // avoid compiler warning
1625         break;
1626     }
1627     return stream;
1628 }
1629
1630 template<>
1631 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1632                           bool useDefault)
1633 {
1634     if (_type == props::ALIAS || _tied)
1635         return false;
1636
1637     useDefault = useDefault && hasValue();
1638     std::string old_val;
1639     if (useDefault)
1640         old_val = getStringValue();
1641     clearValue();
1642     _type = props::STRING;
1643     _tied = true;
1644     _value.val = rawValue.clone();
1645
1646     if (useDefault)
1647         setStringValue(old_val.c_str());
1648
1649     return true;
1650 }
1651 bool
1652 SGPropertyNode::untie ()
1653 {
1654   if (!_tied)
1655     return false;
1656
1657   switch (_type) {
1658   case props::BOOL: {
1659     bool val = getBoolValue();
1660     clearValue();
1661     _type = props::BOOL;
1662     _local_val.bool_val = val;
1663     break;
1664   }
1665   case props::INT: {
1666     int val = getIntValue();
1667     clearValue();
1668     _type = props::INT;
1669     _local_val.int_val = val;
1670     break;
1671   }
1672   case props::LONG: {
1673     long val = getLongValue();
1674     clearValue();
1675     _type = props::LONG;
1676     _local_val.long_val = val;
1677     break;
1678   }
1679   case props::FLOAT: {
1680     float val = getFloatValue();
1681     clearValue();
1682     _type = props::FLOAT;
1683     _local_val.float_val = val;
1684     break;
1685   }
1686   case props::DOUBLE: {
1687     double val = getDoubleValue();
1688     clearValue();
1689     _type = props::DOUBLE;
1690     _local_val.double_val = val;
1691     break;
1692   }
1693   case props::STRING:
1694   case props::UNSPECIFIED: {
1695     string val = getStringValue();
1696     clearValue();
1697     _type = props::STRING;
1698     _local_val.string_val = copy_string(val.c_str());
1699     break;
1700   }
1701   case props::EXTENDED: {
1702     SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1703     _value.val = 0;             // Prevent clearValue() from deleting
1704     clearValue();
1705     _type = props::EXTENDED;
1706     _value.val = val->makeContainer();
1707     delete val;
1708     break;
1709   }
1710   case props::NONE:
1711   default:
1712     break;
1713   }
1714
1715   _tied = false;
1716   return true;
1717 }
1718
1719 SGPropertyNode *
1720 SGPropertyNode::getRootNode ()
1721 {
1722   if (_parent == 0)
1723     return this;
1724   else
1725     return _parent->getRootNode();
1726 }
1727
1728 const SGPropertyNode *
1729 SGPropertyNode::getRootNode () const
1730 {
1731   if (_parent == 0)
1732     return this;
1733   else
1734     return _parent->getRootNode();
1735 }
1736
1737 SGPropertyNode *
1738 SGPropertyNode::getNode (const char * relative_path, bool create)
1739 {
1740   using namespace boost;
1741   if (_path_cache == 0)
1742     _path_cache = new hash_table;
1743
1744   SGPropertyNode * result = _path_cache->get(relative_path);
1745   if (result == 0) {
1746     result = find_node(this,
1747                        make_iterator_range(relative_path, relative_path
1748                                            + strlen(relative_path)),
1749                        create);
1750     if (result != 0)
1751       _path_cache->put(relative_path, result);
1752   }
1753
1754   return result;
1755 }
1756
1757 SGPropertyNode *
1758 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1759 {
1760   using namespace boost;
1761   return find_node(this, make_iterator_range(relative_path, relative_path
1762                                              + strlen(relative_path)),
1763                    create, index);
1764 }
1765
1766 const SGPropertyNode *
1767 SGPropertyNode::getNode (const char * relative_path) const
1768 {
1769   return ((SGPropertyNode *)this)->getNode(relative_path, false);
1770 }
1771
1772 const SGPropertyNode *
1773 SGPropertyNode::getNode (const char * relative_path, int index) const
1774 {
1775   return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1776 }
1777
1778 \f
1779 ////////////////////////////////////////////////////////////////////////
1780 // Convenience methods using relative paths.
1781 ////////////////////////////////////////////////////////////////////////
1782
1783
1784 /**
1785  * Test whether another node has a value attached.
1786  */
1787 bool
1788 SGPropertyNode::hasValue (const char * relative_path) const
1789 {
1790   const SGPropertyNode * node = getNode(relative_path);
1791   return (node == 0 ? false : node->hasValue());
1792 }
1793
1794
1795 /**
1796  * Get the value type for another node.
1797  */
1798 props::Type
1799 SGPropertyNode::getType (const char * relative_path) const
1800 {
1801   const SGPropertyNode * node = getNode(relative_path);
1802   return (node == 0 ? props::UNSPECIFIED : node->getType());
1803 }
1804
1805
1806 /**
1807  * Get a bool value for another node.
1808  */
1809 bool
1810 SGPropertyNode::getBoolValue (const char * relative_path,
1811                               bool defaultValue) const
1812 {
1813   const SGPropertyNode * node = getNode(relative_path);
1814   return (node == 0 ? defaultValue : node->getBoolValue());
1815 }
1816
1817
1818 /**
1819  * Get an int value for another node.
1820  */
1821 int
1822 SGPropertyNode::getIntValue (const char * relative_path,
1823                              int defaultValue) const
1824 {
1825   const SGPropertyNode * node = getNode(relative_path);
1826   return (node == 0 ? defaultValue : node->getIntValue());
1827 }
1828
1829
1830 /**
1831  * Get a long value for another node.
1832  */
1833 long
1834 SGPropertyNode::getLongValue (const char * relative_path,
1835                               long defaultValue) const
1836 {
1837   const SGPropertyNode * node = getNode(relative_path);
1838   return (node == 0 ? defaultValue : node->getLongValue());
1839 }
1840
1841
1842 /**
1843  * Get a float value for another node.
1844  */
1845 float
1846 SGPropertyNode::getFloatValue (const char * relative_path,
1847                                float defaultValue) const
1848 {
1849   const SGPropertyNode * node = getNode(relative_path);
1850   return (node == 0 ? defaultValue : node->getFloatValue());
1851 }
1852
1853
1854 /**
1855  * Get a double value for another node.
1856  */
1857 double
1858 SGPropertyNode::getDoubleValue (const char * relative_path,
1859                                 double defaultValue) const
1860 {
1861   const SGPropertyNode * node = getNode(relative_path);
1862   return (node == 0 ? defaultValue : node->getDoubleValue());
1863 }
1864
1865
1866 /**
1867  * Get a string value for another node.
1868  */
1869 const char *
1870 SGPropertyNode::getStringValue (const char * relative_path,
1871                                 const char * defaultValue) const
1872 {
1873   const SGPropertyNode * node = getNode(relative_path);
1874   return (node == 0 ? defaultValue : node->getStringValue());
1875 }
1876
1877
1878 /**
1879  * Set a bool value for another node.
1880  */
1881 bool
1882 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1883 {
1884   return getNode(relative_path, true)->setBoolValue(value);
1885 }
1886
1887
1888 /**
1889  * Set an int value for another node.
1890  */
1891 bool
1892 SGPropertyNode::setIntValue (const char * relative_path, int value)
1893 {
1894   return getNode(relative_path, true)->setIntValue(value);
1895 }
1896
1897
1898 /**
1899  * Set a long value for another node.
1900  */
1901 bool
1902 SGPropertyNode::setLongValue (const char * relative_path, long value)
1903 {
1904   return getNode(relative_path, true)->setLongValue(value);
1905 }
1906
1907
1908 /**
1909  * Set a float value for another node.
1910  */
1911 bool
1912 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1913 {
1914   return getNode(relative_path, true)->setFloatValue(value);
1915 }
1916
1917
1918 /**
1919  * Set a double value for another node.
1920  */
1921 bool
1922 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1923 {
1924   return getNode(relative_path, true)->setDoubleValue(value);
1925 }
1926
1927
1928 /**
1929  * Set a string value for another node.
1930  */
1931 bool
1932 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1933 {
1934   return getNode(relative_path, true)->setStringValue(value);
1935 }
1936
1937
1938 /**
1939  * Set an unknown value for another node.
1940  */
1941 bool
1942 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1943                                      const char * value)
1944 {
1945   return getNode(relative_path, true)->setUnspecifiedValue(value);
1946 }
1947
1948
1949 /**
1950  * Test whether another node is tied.
1951  */
1952 bool
1953 SGPropertyNode::isTied (const char * relative_path) const
1954 {
1955   const SGPropertyNode * node = getNode(relative_path);
1956   return (node == 0 ? false : node->isTied());
1957 }
1958
1959
1960 /**
1961  * Tie a node reached by a relative path, creating it if necessary.
1962  */
1963 bool
1964 SGPropertyNode::tie (const char * relative_path,
1965                      const SGRawValue<bool> &rawValue,
1966                      bool useDefault)
1967 {
1968   return getNode(relative_path, true)->tie(rawValue, useDefault);
1969 }
1970
1971
1972 /**
1973  * Tie a node reached by a relative path, creating it if necessary.
1974  */
1975 bool
1976 SGPropertyNode::tie (const char * relative_path,
1977                      const SGRawValue<int> &rawValue,
1978                      bool useDefault)
1979 {
1980   return getNode(relative_path, true)->tie(rawValue, useDefault);
1981 }
1982
1983
1984 /**
1985  * Tie a node reached by a relative path, creating it if necessary.
1986  */
1987 bool
1988 SGPropertyNode::tie (const char * relative_path,
1989                      const SGRawValue<long> &rawValue,
1990                      bool useDefault)
1991 {
1992   return getNode(relative_path, true)->tie(rawValue, useDefault);
1993 }
1994
1995
1996 /**
1997  * Tie a node reached by a relative path, creating it if necessary.
1998  */
1999 bool
2000 SGPropertyNode::tie (const char * relative_path,
2001                      const SGRawValue<float> &rawValue,
2002                      bool useDefault)
2003 {
2004   return getNode(relative_path, true)->tie(rawValue, useDefault);
2005 }
2006
2007
2008 /**
2009  * Tie a node reached by a relative path, creating it if necessary.
2010  */
2011 bool
2012 SGPropertyNode::tie (const char * relative_path,
2013                      const SGRawValue<double> &rawValue,
2014                      bool useDefault)
2015 {
2016   return getNode(relative_path, true)->tie(rawValue, useDefault);
2017 }
2018
2019
2020 /**
2021  * Tie a node reached by a relative path, creating it if necessary.
2022  */
2023 bool
2024 SGPropertyNode::tie (const char * relative_path,
2025                      const SGRawValue<const char *> &rawValue,
2026                      bool useDefault)
2027 {
2028   return getNode(relative_path, true)->tie(rawValue, useDefault);
2029 }
2030
2031
2032 /**
2033  * Attempt to untie another node reached by a relative path.
2034  */
2035 bool
2036 SGPropertyNode::untie (const char * relative_path)
2037 {
2038   SGPropertyNode * node = getNode(relative_path);
2039   return (node == 0 ? false : node->untie());
2040 }
2041
2042 void
2043 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2044                                    bool initial)
2045 {
2046   if (_listeners == 0)
2047     _listeners = new vector<SGPropertyChangeListener*>;
2048   _listeners->push_back(listener);
2049   listener->register_property(this);
2050   if (initial)
2051     listener->valueChanged(this);
2052 }
2053
2054 void
2055 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2056 {
2057   vector<SGPropertyChangeListener*>::iterator it =
2058     find(_listeners->begin(), _listeners->end(), listener);
2059   if (it != _listeners->end()) {
2060     _listeners->erase(it);
2061     listener->unregister_property(this);
2062     if (_listeners->empty()) {
2063       vector<SGPropertyChangeListener*>* tmp = _listeners;
2064       _listeners = 0;
2065       delete tmp;
2066     }
2067   }
2068 }
2069
2070 void
2071 SGPropertyNode::fireValueChanged ()
2072 {
2073   fireValueChanged(this);
2074 }
2075
2076 void
2077 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2078 {
2079   fireChildAdded(this, child);
2080 }
2081
2082 void
2083 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2084 {
2085   fireChildRemoved(this, child);
2086 }
2087
2088 void
2089 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2090 {
2091   if (_listeners != 0) {
2092     for (unsigned int i = 0; i < _listeners->size(); i++) {
2093       (*_listeners)[i]->valueChanged(node);
2094     }
2095   }
2096   if (_parent != 0)
2097     _parent->fireValueChanged(node);
2098 }
2099
2100 void
2101 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2102                                 SGPropertyNode * child)
2103 {
2104   if (_listeners != 0) {
2105     for (unsigned int i = 0; i < _listeners->size(); i++) {
2106       (*_listeners)[i]->childAdded(parent, child);
2107     }
2108   }
2109   if (_parent != 0)
2110     _parent->fireChildAdded(parent, child);
2111 }
2112
2113 void
2114 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2115                                   SGPropertyNode * child)
2116 {
2117   if (_listeners != 0) {
2118     for (unsigned int i = 0; i < _listeners->size(); i++) {
2119       (*_listeners)[i]->childRemoved(parent, child);
2120     }
2121   }
2122   if (_parent != 0)
2123     _parent->fireChildRemoved(parent, child);
2124 }
2125
2126
2127 \f
2128 ////////////////////////////////////////////////////////////////////////
2129 // Simplified hash table for caching paths.
2130 ////////////////////////////////////////////////////////////////////////
2131
2132 #define HASH_TABLE_SIZE 199
2133
2134 SGPropertyNode::hash_table::entry::entry ()
2135   : _value(0)
2136 {
2137 }
2138
2139 SGPropertyNode::hash_table::entry::~entry ()
2140 {
2141                                 // Don't delete the value; we don't own
2142                                 // the pointer.
2143 }
2144
2145 void
2146 SGPropertyNode::hash_table::entry::set_key (const char * key)
2147 {
2148   _key = key;
2149 }
2150
2151 void
2152 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2153 {
2154   _value = value;
2155 }
2156
2157 SGPropertyNode::hash_table::bucket::bucket ()
2158   : _length(0),
2159     _entries(0)
2160 {
2161 }
2162
2163 SGPropertyNode::hash_table::bucket::~bucket ()
2164 {
2165   for (int i = 0; i < _length; i++)
2166     delete _entries[i];
2167   delete [] _entries;
2168 }
2169
2170 SGPropertyNode::hash_table::entry *
2171 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2172 {
2173   int i;
2174   for (i = 0; i < _length; i++) {
2175     if (!strcmp(_entries[i]->get_key(), key))
2176       return _entries[i];
2177   }
2178   if (create) {
2179     entry ** new_entries = new entry*[_length+1];
2180     for (i = 0; i < _length; i++) {
2181       new_entries[i] = _entries[i];
2182     }
2183     delete [] _entries;
2184     _entries = new_entries;
2185     _entries[_length] = new entry;
2186     _entries[_length]->set_key(key);
2187     _length++;
2188     return _entries[_length - 1];
2189   } else {
2190     return 0;
2191   }
2192 }
2193
2194 bool
2195 SGPropertyNode::hash_table::bucket::erase (SGPropertyNode * node)
2196 {
2197   for (int i = 0; i < _length; i++) {
2198     if (_entries[i]->get_value() == node) {
2199       delete _entries[i];
2200       for (++i; i < _length; i++) {
2201         _entries[i-1] = _entries[i];
2202       }
2203       _length--;
2204       return true;
2205     }
2206   }
2207   return false;
2208 }
2209
2210 void
2211 SGPropertyNode::hash_table::bucket::clear (SGPropertyNode::hash_table * owner)
2212 {
2213   for (int i = 0; i < _length; i++) {
2214     SGPropertyNode * node = _entries[i]->get_value();
2215     if (node)
2216       node->remove_linked_node(owner);
2217   }
2218 }
2219
2220 SGPropertyNode::hash_table::hash_table ()
2221   : _data_length(0),
2222     _data(0)
2223 {
2224 }
2225
2226 SGPropertyNode::hash_table::~hash_table ()
2227 {
2228   for (unsigned int i = 0; i < _data_length; i++) {
2229     if (_data[i]) {
2230       _data[i]->clear(this);
2231       delete _data[i];
2232     }
2233   }
2234   delete [] _data;
2235 }
2236
2237 SGPropertyNode *
2238 SGPropertyNode::hash_table::get (const char * key)
2239 {
2240   if (_data_length == 0)
2241     return 0;
2242   unsigned int index = hashcode(key) % _data_length;
2243   if (_data[index] == 0)
2244     return 0;
2245   entry * e = _data[index]->get_entry(key);
2246   if (e == 0)
2247     return 0;
2248   else
2249     return e->get_value();
2250 }
2251
2252 void
2253 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2254 {
2255   if (_data_length == 0) {
2256     _data = new bucket*[HASH_TABLE_SIZE];
2257     _data_length = HASH_TABLE_SIZE;
2258     for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2259       _data[i] = 0;
2260   }
2261   unsigned int index = hashcode(key) % _data_length;
2262   if (_data[index] == 0) {
2263     _data[index] = new bucket;
2264   }
2265   entry * e = _data[index]->get_entry(key, true);
2266   e->set_value(value);
2267   value->add_linked_node(this);
2268 }
2269
2270 bool
2271 SGPropertyNode::hash_table::erase (SGPropertyNode * node)
2272 {
2273   for (unsigned int i = 0; i < _data_length; i++)
2274     if (_data[i] && _data[i]->erase(node))
2275       return true;
2276
2277   return false;
2278 }
2279
2280 unsigned int
2281 SGPropertyNode::hash_table::hashcode (const char * key)
2282 {
2283   unsigned int hash = 0;
2284   while (*key != 0) {
2285     hash = 31 * hash + *key;
2286     key++;
2287   }
2288   return hash;
2289 }
2290
2291
2292 \f
2293 ////////////////////////////////////////////////////////////////////////
2294 // Implementation of SGPropertyChangeListener.
2295 ////////////////////////////////////////////////////////////////////////
2296
2297 SGPropertyChangeListener::~SGPropertyChangeListener ()
2298 {
2299   for (int i = _properties.size() - 1; i >= 0; i--)
2300     _properties[i]->removeChangeListener(this);
2301 }
2302
2303 void
2304 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2305 {
2306   // NO-OP
2307 }
2308
2309 void
2310 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2311                                       SGPropertyNode * child)
2312 {
2313   // NO-OP
2314 }
2315
2316 void
2317 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2318                                         SGPropertyNode * child)
2319 {
2320   // NO-OP
2321 }
2322
2323 void
2324 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2325 {
2326   _properties.push_back(node);
2327 }
2328
2329 void
2330 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2331 {
2332   vector<SGPropertyNode *>::iterator it =
2333     find(_properties.begin(), _properties.end(), node);
2334   if (it != _properties.end())
2335     _properties.erase(it);
2336 }
2337
2338 template<>
2339 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2340 {
2341     const SGVec3d vec
2342         = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2343     for (int i = 0; i < 3; ++i) {
2344         stream << vec[i];
2345         if (i < 2)
2346             stream << ' ';
2347     }
2348     return stream;
2349 }
2350
2351 namespace simgear
2352 {
2353 template<>
2354 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2355 {
2356     for (int i = 0; i < 3; ++i) {
2357         stream >> result[i];
2358     }
2359     return stream;
2360 }
2361 }
2362 template<>
2363 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2364 {
2365     const SGVec4d vec
2366         = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();    
2367     for (int i = 0; i < 4; ++i) {
2368         stream << vec[i];
2369         if (i < 3)
2370             stream << ' ';
2371     }
2372     return stream;
2373 }
2374
2375 namespace simgear
2376 {
2377 template<>
2378 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2379 {
2380     for (int i = 0; i < 4; ++i) {
2381         stream >> result[i];
2382     }
2383     return stream;
2384 }
2385
2386 namespace
2387 {
2388 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2389 {
2390     props::Type ltype = lhs.getType();
2391     props::Type rtype = rhs.getType();
2392     if (ltype != rtype)
2393         return false;
2394     switch (ltype) {
2395     case props::NONE:
2396         return true;
2397     case props::ALIAS:
2398         return false;           // XXX Should we look in aliases?
2399     case props::BOOL:
2400         return lhs.getValue<bool>() == rhs.getValue<bool>();
2401     case props::INT:
2402         return lhs.getValue<int>() == rhs.getValue<int>();
2403     case props::LONG:
2404         return lhs.getValue<long>() == rhs.getValue<long>();
2405     case props::FLOAT:
2406         return lhs.getValue<float>() == rhs.getValue<float>();
2407     case props::DOUBLE:
2408         return lhs.getValue<double>() == rhs.getValue<double>();
2409     case props::STRING:
2410     case props::UNSPECIFIED:
2411         return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2412     case props::VEC3D:
2413         return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2414     case props::VEC4D:
2415         return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2416     default:
2417         return false;
2418     }
2419 }
2420 }
2421 }
2422
2423 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2424                              const SGPropertyNode& rhs)
2425 {
2426     if (&lhs == &rhs)
2427         return true;
2428     int lhsChildren = lhs.nChildren();
2429     int rhsChildren = rhs.nChildren();
2430     if (lhsChildren != rhsChildren)
2431         return false;
2432     if (lhsChildren == 0)
2433         return compareNodeValue(lhs, rhs);
2434     for (size_t i = 0; i < lhs._children.size(); ++i) {
2435         const SGPropertyNode* lchild = lhs._children[i];
2436         const SGPropertyNode* rchild = rhs._children[i];
2437         // I'm guessing that the nodes will usually be in the same
2438         // order.
2439         if (lchild->getIndex() != rchild->getIndex()
2440             || lchild->getNameString() != rchild->getNameString()) {
2441             rchild = 0;
2442             for (PropertyList::const_iterator itr = rhs._children.begin(),
2443                      end = rhs._children.end();
2444                  itr != end;
2445                 ++itr)
2446                 if (lchild->getIndex() == (*itr)->getIndex()
2447                     && lchild->getNameString() == (*itr)->getNameString()) {
2448                     rchild = *itr;
2449                     break;
2450                 }
2451             if (!rchild)
2452                 return false;
2453         }
2454         if (!compare(*lchild, *rchild))
2455             return false;
2456     }
2457     return true;
2458 }
2459
2460 struct PropertyPlaceLess {
2461     typedef bool result_type;
2462     bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2463     {
2464         int comp = lhs->getNameString().compare(rhs->getNameString());
2465         if (comp == 0)
2466             return lhs->getIndex() < rhs->getIndex();
2467         else
2468             return comp < 0;
2469     }
2470 };
2471
2472 size_t hash_value(const SGPropertyNode& node)
2473 {
2474     using namespace boost;
2475
2476     if (node.nChildren() == 0) {
2477         switch (node.getType()) {
2478         case props::NONE:
2479             return 0;
2480
2481         case props::BOOL:
2482             return hash_value(node.getValue<bool>());
2483         case props::INT:
2484             return hash_value(node.getValue<int>());
2485         case props::LONG:
2486             return hash_value(node.getValue<long>());
2487         case props::FLOAT:
2488             return hash_value(node.getValue<float>());
2489         case props::DOUBLE:
2490             return hash_value(node.getValue<double>());
2491         case props::STRING:
2492         case props::UNSPECIFIED:
2493         {
2494             const char *val = node.getStringValue();
2495             return hash_range(val, val + strlen(val));
2496         }
2497         case props::VEC3D:
2498         {
2499             const SGVec3d val = node.getValue<SGVec3d>();
2500             return hash_range(&val[0], &val[3]);
2501         }
2502         case props::VEC4D:
2503         {
2504             const SGVec4d val = node.getValue<SGVec4d>();
2505             return hash_range(&val[0], &val[4]);
2506         }
2507         case props::ALIAS:      // XXX Should we look in aliases?
2508         default:
2509             return 0;
2510         }
2511     } else {
2512         size_t seed = 0;
2513         PropertyList children(node._children.begin(), node._children.end());
2514         sort(children.begin(), children.end(), PropertyPlaceLess());
2515         for (PropertyList::const_iterator itr  = children.begin(),
2516                  end = children.end();
2517              itr != end;
2518              ++itr) {
2519             hash_combine(seed, (*itr)->_name);
2520             hash_combine(seed, (*itr)->_index);
2521             hash_combine(seed, hash_value(**itr));
2522         }
2523         return seed;
2524     }
2525 }
2526
2527 // end of props.cxx