]> git.mxchange.org Git - simgear.git/blob - simgear/misc/props.cxx
7c07c203bc519c1eea200898ffa61cc5979124d6
[simgear.git] / simgear / misc / 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 <config.h>
11 #endif
12
13 #include <simgear/compiler.h>
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include STL_IOSTREAM
18 #include <algorithm>
19 #include "props.hxx"
20
21 FG_USING_STD(sort);
22
23
24 \f
25 ////////////////////////////////////////////////////////////////////////
26 // Local classes.
27 ////////////////////////////////////////////////////////////////////////
28
29 /**
30  * Comparator class for sorting by index.
31  */
32 class CompareIndices
33 {
34 public:
35   int operator() (const SGPropertyNode * n1, const SGPropertyNode *n2) const {
36     return (n1->getIndex() < n2->getIndex());
37   }
38 };
39
40
41 \f
42 ////////////////////////////////////////////////////////////////////////
43 // Convenience macros for value access.
44 ////////////////////////////////////////////////////////////////////////
45
46 #define GET_BOOL (_value.bool_val->getValue())
47 #define GET_INT (_value.int_val->getValue())
48 #define GET_LONG (_value.long_val->getValue())
49 #define GET_FLOAT (_value.float_val->getValue())
50 #define GET_DOUBLE (_value.double_val->getValue())
51 #define GET_STRING (_value.string_val->getValue())
52
53 #define SET_BOOL(val) (_value.bool_val->setValue(val))
54 #define SET_INT(val) (_value.int_val->setValue(val))
55 #define SET_LONG(val) (_value.long_val->setValue(val))
56 #define SET_FLOAT(val) (_value.float_val->setValue(val))
57 #define SET_DOUBLE(val) (_value.double_val->setValue(val))
58 #define SET_STRING(val) (_value.string_val->setValue(val))
59
60
61 \f
62 ////////////////////////////////////////////////////////////////////////
63 // Default values for every type.
64 ////////////////////////////////////////////////////////////////////////
65
66 const bool SGRawValue<bool>::DefaultValue = false;
67 const int SGRawValue<int>::DefaultValue = 0;
68 const long SGRawValue<long>::DefaultValue = 0L;
69 const float SGRawValue<float>::DefaultValue = 0.0;
70 const double SGRawValue<double>::DefaultValue = 0.0L;
71 const string SGRawValue<string>::DefaultValue = "";
72
73
74 \f
75 ////////////////////////////////////////////////////////////////////////
76 // Local path normalization code.
77 ////////////////////////////////////////////////////////////////////////
78
79 /**
80  * A component in a path.
81  */
82 struct PathComponent
83 {
84   string name;
85   int index;
86 };
87
88 /**
89  * Parse the name for a path component.
90  *
91  * Name: [_a-zA-Z][-._a-zA-Z0-9]*
92  */
93 static inline string
94 parse_name (const string &path, int &i)
95 {
96   string name = "";
97   int max = path.size();
98
99   if (path[i] == '.') {
100     i++;
101     if (i < max && path[i] == '.') {
102       i++;
103       name = "..";
104     } else {
105       name = ".";
106     }
107     if (i < max && path[i] != '/')
108       throw string(string("Illegal character after ") + name);
109   }
110
111   else if (isalpha(path[i]) || path[i] == '_') {
112     name += path[i];
113     i++;
114
115                                 // The rules inside a name are a little
116                                 // less restrictive.
117     while (i < max) {
118       if (isalpha(path[i]) || isdigit(path[i]) || path[i] == '_' ||
119           path[i] == '-' || path[i] == '.') {
120         name += path[i];
121       } else if (path[i] == '[' || path[i] == '/') {
122         break;
123       } else {
124         throw string("name may contain only ._- and alphanumeric characters");
125       }
126       i++;
127     }
128   }
129
130   else {
131     if (name.size() == 0)
132       throw string("name must begin with alpha or '_'");
133   }
134
135   return name;
136 }
137
138
139 /**
140  * Parse the optional integer index for a path component.
141  *
142  * Index: "[" [0-9]+ "]"
143  */
144 static inline int
145 parse_index (const string &path, int &i)
146 {
147   int index = 0;
148
149   if (path[i] != '[')
150     return 0;
151   else
152     i++;
153
154   for (int max = path.size(); i < max; i++) {
155     if (isdigit(path[i])) {
156       index = (index * 10) + (path[i] - '0');
157     } else if (path[i] == ']') {
158       i++;
159       return index;
160     } else {
161       break;
162     }
163   }
164
165   throw string("unterminated index (looking for ']')");
166 }
167
168
169 /**
170  * Parse a single path component.
171  *
172  * Component: Name Index?
173  */
174 static inline PathComponent
175 parse_component (const string &path, int &i)
176 {
177   PathComponent component;
178   component.name = parse_name(path, i);
179   if (component.name[0] != '.')
180     component.index = parse_index(path, i);
181   else
182     component.index = -1;
183   return component;
184 }
185
186
187 /**
188  * Parse a path into its components.
189  */
190 static void
191 parse_path (const string &path, vector<PathComponent> &components)
192 {
193   int pos = 0;
194   int max = path.size();
195
196                                 // Check for initial '/'
197   if (path[pos] == '/') {
198     PathComponent root;
199     root.name = "";
200     root.index = -1;
201     components.push_back(root);
202     pos++;
203     while (pos < max && path[pos] == '/')
204       pos++;
205   }
206
207   while (pos < max) {
208     components.push_back(parse_component(path, pos));
209     while (pos < max && path[pos] == '/')
210       pos++;
211   }
212 }
213
214
215 \f
216 ////////////////////////////////////////////////////////////////////////
217 // Other static utility functions.
218 ////////////////////////////////////////////////////////////////////////
219
220
221 /**
222  * Locate a child node by name and index.
223  */
224 static int
225 find_child (const string &name, int index, vector<SGPropertyNode *> nodes)
226 {
227   int nNodes = nodes.size();
228   for (int i = 0; i < nNodes; i++) {
229     SGPropertyNode * node = nodes[i];
230     if (node->getName() == name && node->getIndex() == index)
231       return i;
232   }
233   return -1;
234 }
235
236
237 /**
238  * Locate another node, given a relative path.
239  */
240 static SGPropertyNode *
241 find_node (SGPropertyNode * current,
242            const vector<PathComponent> &components,
243            int position,
244            bool create)
245 {
246                                 // Run off the end of the list
247   if (current == 0) {
248     return 0;
249   }
250
251                                 // Success! This is the one we want.
252   else if (position >= components.size()) {
253     return current;
254   }
255
256                                 // Empty component means root.
257   else if (components[position].name == "") {
258     return find_node(current->getRootNode(), components, position + 1, create);
259   }
260
261                                 // . means current directory
262   else if (components[position].name == ".") {
263     return find_node(current, components, position + 1, create);
264   }
265
266                                 // .. means parent directory
267   else if (components[position].name == "..") {
268     SGPropertyNode * parent = current->getParent();
269     if (parent == 0)
270       throw string("Attempt to move past root with '..'");
271     else
272       return find_node(parent, components, position + 1, create);
273   }
274
275                                 // Otherwise, a child name
276   else {
277     SGPropertyNode * child =
278       current->getChild(components[position].name,
279                         components[position].index,
280                         create);
281     return find_node(child, components, position + 1, create);
282   }
283 }
284
285
286 \f
287 ////////////////////////////////////////////////////////////////////////
288 // Implementation of SGValue.
289 ////////////////////////////////////////////////////////////////////////
290
291
292 /**
293  * Default constructor.
294  *
295  * The type will be UNKNOWN and the raw value will be "".
296  */
297 SGValue::SGValue ()
298   : _type(UNKNOWN), _tied(false)
299 {
300   _value.string_val = new SGRawValueInternal<string>;
301 }
302
303
304 /**
305  * Copy constructor.
306  */
307 SGValue::SGValue (const SGValue &source)
308 {
309   _type = source._type;
310   _tied = source._tied;
311   switch (source._type) {
312   case ALIAS:
313                                 // FIXME!!!
314     _value.alias = (SGValue *)(source.getAlias());
315     break;
316   case BOOL:
317     _value.bool_val = source._value.bool_val->clone();
318     break;
319   case INT:
320     _value.int_val = source._value.int_val->clone();
321     break;
322   case LONG:
323     _value.long_val = source._value.long_val->clone();
324     break;
325   case FLOAT:
326     _value.float_val = source._value.float_val->clone();
327     break;
328   case DOUBLE:
329     _value.double_val = source._value.double_val->clone();
330     break;
331   case STRING:
332   case UNKNOWN:
333     _value.string_val = source._value.string_val->clone();
334     break;
335   }
336 }
337
338
339 /**
340  * Destructor.
341  */
342 SGValue::~SGValue ()
343 {
344   if (_type != ALIAS)
345     clear_value();
346 }
347
348
349 /**
350  * Delete and clear the current value.
351  */
352 void
353 SGValue::clear_value ()
354 {
355   switch (_type) {
356   case ALIAS:
357     _value.alias->clear_value();
358   case BOOL:
359     delete _value.bool_val;
360     _value.bool_val = 0;
361     break;
362   case INT:
363     delete _value.int_val;
364     _value.int_val = 0;
365     break;
366   case LONG:
367     delete _value.long_val;
368     _value.long_val = 0L;
369     break;
370   case FLOAT:
371     delete _value.float_val;
372     _value.float_val = 0;
373     break;
374   case DOUBLE:
375     delete _value.double_val;
376     _value.double_val = 0;
377     break;
378   case STRING:
379   case UNKNOWN:
380     delete _value.string_val;
381     _value.string_val = 0;
382     break;
383   }
384 }
385
386
387 /**
388  * Get the current type.
389  *
390  * Does not return a type of ALIAS.
391  */
392 SGValue::Type
393 SGValue::getType () const
394 {
395   if (_type == ALIAS)
396     return _value.alias->getType();
397   else
398     return (Type)_type;
399 }
400
401
402 /**
403  * Get the current aliased value.
404  */
405 SGValue *
406 SGValue::getAlias ()
407 {
408   return (_type == ALIAS ? _value.alias : 0);
409 }
410
411
412 /**
413  * Get the current aliased value.
414  */
415 const SGValue *
416 SGValue::getAlias () const
417 {
418   return (_type == ALIAS ? _value.alias : 0);
419 }
420
421
422 /**
423  * Alias to another value.
424  */
425 bool
426 SGValue::alias (SGValue * alias)
427 {
428   if (alias == 0 || _type == ALIAS || _tied)
429     return false;
430   clear_value();
431   _value.alias = alias;
432   _type = ALIAS;
433   return true;
434 }
435
436
437 /**
438  * Unalias from another value.
439  */
440 bool
441 SGValue::unalias ()
442 {
443                                 // FIXME: keep copy of previous value,
444                                 // as with untie()
445   if (_type != ALIAS)
446     return false;
447   _value.string_val = new SGRawValueInternal<string>;
448   _type = UNKNOWN;
449   return true;
450 }
451
452
453 /**
454  * Get a boolean value.
455  */
456 bool
457 SGValue::getBoolValue () const
458 {
459   switch (_type) {
460   case ALIAS:
461     return _value.alias->getBoolValue();
462   case BOOL:
463     return GET_BOOL;
464   case INT:
465     return GET_INT == 0 ? false : true;
466   case LONG:
467     return GET_LONG == 0L ? false : true;
468   case FLOAT:
469     return GET_FLOAT == 0.0 ? false : true;
470   case DOUBLE:
471     return GET_DOUBLE == 0.0L ? false : true;
472   case STRING:
473   case UNKNOWN:
474     return (GET_STRING == "true" || getDoubleValue() != 0.0L);
475   }
476 }
477
478
479 /**
480  * Get an integer value.
481  */
482 int
483 SGValue::getIntValue () const
484 {
485   switch (_type) {
486   case ALIAS:
487     return _value.alias->getIntValue();
488   case BOOL:
489     return int(GET_BOOL);
490   case INT:
491     return GET_INT;
492   case LONG:
493     return int(GET_LONG);
494   case FLOAT:
495     return int(GET_FLOAT);
496   case DOUBLE:
497     return int(GET_DOUBLE);
498   case STRING:
499   case UNKNOWN:
500     return atoi(GET_STRING.c_str());
501   }
502 }
503
504
505 /**
506  * Get a long integer value.
507  */
508 long
509 SGValue::getLongValue () const
510 {
511   switch (_type) {
512   case ALIAS:
513     return _value.alias->getLongValue();
514   case BOOL:
515     return long(GET_BOOL);
516   case INT:
517     return long(GET_INT);
518   case LONG:
519     return GET_LONG;
520   case FLOAT:
521     return long(GET_FLOAT);
522   case DOUBLE:
523     return long(GET_DOUBLE);
524   case STRING:
525   case UNKNOWN:
526     return strtol(GET_STRING.c_str(), 0, 0);
527   }
528 }
529
530
531 /**
532  * Get a float value.
533  */
534 float
535 SGValue::getFloatValue () const
536 {
537   switch (_type) {
538   case ALIAS:
539     return _value.alias->getFloatValue();
540   case BOOL:
541     return float(GET_BOOL);
542   case INT:
543     return float(GET_INT);
544   case LONG:
545     return float(GET_LONG);
546   case FLOAT:
547     return GET_FLOAT;
548   case DOUBLE:
549     return float(GET_DOUBLE);
550   case STRING:
551   case UNKNOWN:
552     return atof(GET_STRING.c_str());
553   }
554 }
555
556
557 /**
558  * Get a double value.
559  */
560 double
561 SGValue::getDoubleValue () const
562 {
563   switch (_type) {
564   case ALIAS:
565     return _value.alias->getDoubleValue();
566   case BOOL:
567     return double(GET_BOOL);
568   case INT:
569     return double(GET_INT);
570   case LONG:
571     return double(GET_LONG);
572   case FLOAT:
573     return double(GET_FLOAT);
574   case DOUBLE:
575     return GET_DOUBLE;
576   case STRING:
577   case UNKNOWN:
578     return strtod(GET_STRING.c_str(), 0);
579   }
580 }
581
582
583 /**
584  * Get a string value.
585  */
586 string
587 SGValue::getStringValue () const
588 {
589   char buf[128];
590
591   switch (_type) {
592   case ALIAS:
593     return _value.alias->getStringValue();
594   case BOOL:
595     if (GET_BOOL)
596       return "true";
597     else
598       return "false";
599   case INT:
600     sprintf(buf, "%d", GET_INT);
601     return buf;
602   case LONG:
603     sprintf(buf, "%ld", GET_LONG);
604     return buf;
605   case FLOAT:
606     sprintf(buf, "%f", GET_FLOAT);
607     return buf;
608   case DOUBLE:
609     sprintf(buf, "%lf", GET_DOUBLE);
610     return buf;
611   case STRING:
612   case UNKNOWN:
613     return GET_STRING;
614   }
615 }
616
617
618 /**
619  * Set a bool value.
620  */
621 bool
622 SGValue::setBoolValue (bool value)
623 {
624   if (_type == UNKNOWN) {
625     clear_value();
626     _value.bool_val = new SGRawValueInternal<bool>;
627     _type = BOOL;
628   }
629
630   switch (_type) {
631   case ALIAS:
632     return _value.alias->setBoolValue(value);
633   case BOOL:
634     return SET_BOOL(value);
635   case INT:
636     return SET_INT(int(value));
637   case LONG:
638     return SET_LONG(long(value));
639   case FLOAT:
640     return SET_FLOAT(float(value));
641   case DOUBLE:
642     return SET_DOUBLE(double(value));
643   case STRING:
644     return SET_STRING(value ? "true" : "false");
645   }
646
647   return false;
648 }
649
650
651 /**
652  * Set an int value.
653  */
654 bool
655 SGValue::setIntValue (int value)
656 {
657   if (_type == UNKNOWN) {
658     clear_value();
659     _value.int_val = new SGRawValueInternal<int>;
660     _type = INT;
661   }
662
663   switch (_type) {
664   case ALIAS:
665     return _value.alias->setIntValue(value);
666   case BOOL:
667     return SET_BOOL(value == 0 ? false : true);
668   case INT:
669     return SET_INT(value);
670   case LONG:
671     return SET_LONG(long(value));
672   case FLOAT:
673     return SET_FLOAT(float(value));
674   case DOUBLE:
675     return SET_DOUBLE(double(value));
676   case STRING: {
677     char buf[128];
678     sprintf(buf, "%d", value);
679     return SET_STRING(buf);
680   }
681   }
682
683   return false;
684 }
685
686
687 /**
688  * Set a long value.
689  */
690 bool
691 SGValue::setLongValue (long value)
692 {
693   if (_type == UNKNOWN) {
694     clear_value();
695     _value.long_val = new SGRawValueInternal<long>;
696     _type = LONG;
697   }
698
699   switch (_type) {
700   case ALIAS:
701     return _value.alias->setLongValue(value);
702   case BOOL:
703     return SET_BOOL(value == 0L ? false : true);
704   case INT:
705     return SET_INT(int(value));
706   case LONG:
707     return SET_LONG(value);
708   case FLOAT:
709     return SET_FLOAT(float(value));
710   case DOUBLE:
711     return SET_DOUBLE(double(value));
712   case STRING: {
713     char buf[128];
714     sprintf(buf, "%d", value);
715     return SET_STRING(buf);
716   }
717   }
718
719   return false;
720 }
721
722
723 /**
724  * Set a float value.
725  */
726 bool
727 SGValue::setFloatValue (float value)
728 {
729   if (_type == UNKNOWN) {
730     clear_value();
731     _value.float_val = new SGRawValueInternal<float>;
732     _type = FLOAT;
733   }
734
735   switch (_type) {
736   case ALIAS:
737     return _value.alias->setFloatValue(value);
738   case BOOL:
739     return SET_BOOL(value == 0.0 ? false : true);
740   case INT:
741     return SET_INT(int(value));
742   case LONG:
743     return SET_LONG(long(value));
744   case FLOAT:
745     return SET_FLOAT(value);
746   case DOUBLE:
747     return SET_DOUBLE(double(value));
748   case STRING: {
749     char buf[128];
750     sprintf(buf, "%f", value);
751     return SET_STRING(buf);
752   }
753   }
754
755   return false;
756 }
757
758
759 /**
760  * Set a double value.
761  */
762 bool
763 SGValue::setDoubleValue (double value)
764 {
765   if (_type == UNKNOWN) {
766     clear_value();
767     _value.double_val = new SGRawValueInternal<double>;
768     _type = DOUBLE;
769   }
770
771   switch (_type) {
772   case ALIAS:
773     return _value.alias->setDoubleValue(value);
774   case BOOL:
775     return SET_BOOL(value == 0.0L ? false : true);
776   case INT:
777     return SET_INT(int(value));
778   case LONG:
779     return SET_LONG(long(value));
780   case FLOAT:
781     return SET_FLOAT(float(value));
782   case DOUBLE:
783     return SET_DOUBLE(value);
784   case STRING: {
785     char buf[128];
786     sprintf(buf, "%lf", value);
787     return SET_STRING(buf);
788   }
789   }
790
791   return false;
792 }
793
794
795 /**
796  * Set a string value.
797  */
798 bool
799 SGValue::setStringValue (string value)
800 {
801   if (_type == UNKNOWN) {
802     clear_value();
803     _value.string_val = new SGRawValueInternal<string>;
804     _type = STRING;
805   }
806
807   switch (_type) {
808   case ALIAS:
809     return _value.alias->setStringValue(value);
810   case BOOL:
811     return SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
812   case INT:
813     return SET_INT(atoi(value.c_str()));
814   case LONG:
815     return SET_LONG(strtol(value.c_str(), 0, 0));
816   case FLOAT:
817     return SET_FLOAT(atof(value.c_str()));
818   case DOUBLE:
819     return SET_DOUBLE(strtod(value.c_str(), 0));
820   case STRING:
821     return SET_STRING(value);
822   }
823
824   return false;
825 }
826
827
828 /**
829  * Set a value of unknown type (stored as a string).
830  */
831 bool
832 SGValue::setUnknownValue (string value)
833 {
834   switch (_type) {
835   case ALIAS:
836     return _value.alias->setUnknownValue(value);
837   case BOOL:
838     return SET_BOOL((value == "true" || atoi(value.c_str())) ? true : false);
839   case INT:
840     return SET_INT(atoi(value.c_str()));
841   case LONG:
842     return SET_LONG(strtol(value.c_str(), 0, 0));
843   case FLOAT:
844     return SET_FLOAT(atof(value.c_str()));
845   case DOUBLE:
846     return SET_DOUBLE(strtod(value.c_str(), 0));
847   case STRING:
848   case UNKNOWN:
849     return SET_STRING(value);
850   }
851
852   return false;
853 }
854
855
856 /**
857  * Tie a bool value.
858  */
859 bool
860 SGValue::tie (const SGRawValue<bool> &value, bool use_default)
861 {
862   if (_type == ALIAS)
863     return _value.alias->tie(value, use_default);
864   else if (_tied)
865     return false;
866
867   bool old_val;
868   if (use_default)
869     old_val = getBoolValue();
870
871   clear_value();
872   _type = BOOL;
873   _tied = true;
874   _value.bool_val = value.clone();
875
876   if (use_default)
877     setBoolValue(old_val);
878
879   return true;
880 }
881
882
883 /**
884  * Tie an int value.
885  */
886 bool
887 SGValue::tie (const SGRawValue<int> &value, bool use_default)
888 {
889   if (_type == ALIAS)
890     return _value.alias->tie(value, use_default);
891   else if (_tied)
892     return false;
893
894   int old_val;
895   if (use_default)
896     old_val = getIntValue();
897
898   clear_value();
899   _type = INT;
900   _tied = true;
901   _value.int_val = value.clone();
902
903   if (use_default)
904     setIntValue(old_val);
905
906   return true;
907 }
908
909
910 /**
911  * Tie a long value.
912  */
913 bool
914 SGValue::tie (const SGRawValue<long> &value, bool use_default)
915 {
916   if (_type == ALIAS)
917     return _value.alias->tie(value, use_default);
918   else if (_tied)
919     return false;
920
921   long old_val;
922   if (use_default)
923     old_val = getLongValue();
924
925   clear_value();
926   _type = LONG;
927   _tied = true;
928   _value.long_val = value.clone();
929
930   if (use_default)
931     setLongValue(old_val);
932
933   return true;
934 }
935
936
937 /**
938  * Tie a float value.
939  */
940 bool
941 SGValue::tie (const SGRawValue<float> &value, bool use_default)
942 {
943   if (_type == ALIAS)
944     return _value.alias->tie(value, use_default);
945   else if (_tied)
946     return false;
947
948   float old_val;
949   if (use_default)
950     old_val = getFloatValue();
951
952   clear_value();
953   _type = FLOAT;
954   _tied = true;
955   _value.float_val = value.clone();
956
957   if (use_default)
958     setFloatValue(old_val);
959
960   return true;
961 }
962
963
964 /**
965  * Tie a double value.
966  */
967 bool
968 SGValue::tie (const SGRawValue<double> &value, bool use_default)
969 {
970   if (_type == ALIAS)
971     return _value.alias->tie(value, use_default);
972   else if (_tied)
973     return false;
974
975   double old_val;
976   if (use_default)
977     old_val = getDoubleValue();
978
979   clear_value();
980   _type = DOUBLE;
981   _tied = true;
982   _value.double_val = value.clone();
983
984   if (use_default)
985     setDoubleValue(old_val);
986
987   return true;
988 }
989
990
991 /**
992  * Tie a string value.
993  */
994 bool
995 SGValue::tie (const SGRawValue<string> &value, bool use_default)
996 {
997   if (_type == ALIAS)
998     return _value.alias->tie(value, use_default);
999   else if (_tied)
1000     return false;
1001
1002   string old_val;
1003   if (use_default)
1004     old_val = getStringValue();
1005
1006   clear_value();
1007   _type = STRING;
1008   _tied = true;
1009   _value.string_val = value.clone();
1010
1011   if (use_default)
1012     setStringValue(old_val);
1013
1014   return true;
1015 }
1016
1017
1018 /**
1019  * Untie a value.
1020  */
1021 bool
1022 SGValue::untie ()
1023 {
1024   if (!_tied)
1025     return false;
1026
1027   switch (_type) {
1028   case ALIAS: {
1029     return _value.alias->untie();
1030   }
1031   case BOOL: {
1032     bool val = getBoolValue();
1033     clear_value();
1034     _value.bool_val = new SGRawValueInternal<bool>;
1035     SET_BOOL(val);
1036     break;
1037   }
1038   case INT: {
1039     int val = getIntValue();
1040     clear_value();
1041     _value.int_val = new SGRawValueInternal<int>;
1042     SET_INT(val);
1043     break;
1044   }
1045   case LONG: {
1046     long val = getLongValue();
1047     clear_value();
1048     _value.long_val = new SGRawValueInternal<long>;
1049     SET_LONG(val);
1050     break;
1051   }
1052   case FLOAT: {
1053     float val = getFloatValue();
1054     clear_value();
1055     _value.float_val = new SGRawValueInternal<float>;
1056     SET_FLOAT(val);
1057     break;
1058   }
1059   case DOUBLE: {
1060     double val = getDoubleValue();
1061     clear_value();
1062     _value.double_val = new SGRawValueInternal<double>;
1063     SET_DOUBLE(val);
1064     break;
1065   }
1066   case STRING: {
1067     string val = getStringValue();
1068     clear_value();
1069     _value.string_val = new SGRawValueInternal<string>;
1070     SET_STRING(val);
1071     break;
1072   }
1073   }
1074
1075   _tied = false;
1076   return true;
1077 }
1078
1079
1080 \f
1081 ////////////////////////////////////////////////////////////////////////
1082 // Implementation of SGPropertyNode.
1083 ////////////////////////////////////////////////////////////////////////
1084
1085
1086 /**
1087  * Utility function: given a value, find the property node.
1088  */
1089 static SGPropertyNode *
1090 find_node_by_value (SGPropertyNode * start_node, const SGValue * value)
1091 {
1092   if (start_node->getValue() == value) {
1093     return start_node;
1094   } else for (int i = 0; i < start_node->nChildren(); i++) {
1095     SGPropertyNode * child =
1096       find_node_by_value(start_node->getChild(i), value);
1097     if (child != 0)
1098       return child;
1099   }
1100   return 0;
1101 }
1102
1103
1104 /**
1105  * Default constructor: always creates a root node.
1106  */
1107 SGPropertyNode::SGPropertyNode ()
1108   : _value(0), _name(""), _index(0), _parent(0), _target(0)
1109 {
1110 }
1111
1112
1113 /**
1114  * Convenience constructor.
1115  */
1116 SGPropertyNode::SGPropertyNode (const string &name,
1117                                 int index, SGPropertyNode * parent)
1118   : _value(0), _name(name), _index(index), _parent(parent), _target(0)
1119 {
1120 }
1121
1122
1123 /**
1124  * Destructor.
1125  */
1126 SGPropertyNode::~SGPropertyNode ()
1127 {
1128   delete _value;
1129   for (int i = 0; i < _children.size(); i++)
1130     delete _children[i];
1131 }
1132
1133
1134 /**
1135  * Get a value, optionally creating it if not present.
1136  */
1137 SGValue *
1138 SGPropertyNode::getValue (bool create)
1139 {
1140   if (_value == 0 && create)
1141     _value = new SGValue();
1142   return _value;
1143 }
1144
1145
1146 /**
1147  * Alias to another node.
1148  */
1149 bool
1150 SGPropertyNode::alias (SGPropertyNode * target)
1151 {
1152   if (_value == 0)
1153     _value = new SGValue();
1154   _target = target;
1155   return _value->alias(target->getValue(true));
1156 }
1157
1158
1159 /**
1160  * Alias to another node by path.
1161  */
1162 bool
1163 SGPropertyNode::alias (const string &path)
1164 {
1165   return alias(getNode(path, true));
1166 }
1167
1168
1169 /**
1170  * Remove an alias.
1171  */
1172 bool
1173 SGPropertyNode::unalias ()
1174 {
1175   _target = 0;
1176   return (_value != 0 ? _value->unalias() : false);
1177 }
1178
1179
1180 /**
1181  * Test whether this node is aliased.
1182  */
1183 bool
1184 SGPropertyNode::isAlias () const
1185 {
1186   return (_value != 0 ? _value->isAlias() : false);
1187 }
1188
1189
1190 /**
1191  * Get the target of an alias.
1192  *
1193  * This is tricky, because it is actually the value that is aliased,
1194  * and someone could realias or unalias the value directly without
1195  * going through the property node.  The node caches its best guess,
1196  * but it may have to search the whole property tree.
1197  *
1198  * @return The target node for the alias, or 0 if the node is not aliased.
1199  */
1200 SGPropertyNode *
1201 SGPropertyNode::getAliasTarget ()
1202 {
1203   if (_value == 0 || !_value->isAlias()) {
1204     return 0;
1205   } else if (_target != 0 && _target->getValue() == _value->getAlias()) {
1206     return _target;
1207   } else {
1208     _target = find_node_by_value(getRootNode(), _value->getAlias());
1209   }
1210 }
1211
1212
1213 const SGPropertyNode *
1214 SGPropertyNode::getAliasTarget () const
1215 {
1216   if (_value == 0 || !_value->isAlias()) {
1217     return 0;
1218   } else if (_target != 0 && _target->getValue() == _value->getAlias()) {
1219     return _target;
1220   } else {
1221                                 // FIXME: const cast
1222     _target =
1223       find_node_by_value((SGPropertyNode *)getRootNode(), _value->getAlias());
1224   }
1225 }
1226
1227
1228 /**
1229  * Get a non-const child by index.
1230  */
1231 SGPropertyNode *
1232 SGPropertyNode::getChild (int position)
1233 {
1234   if (position >= 0 && position < nChildren())
1235     return _children[position];
1236   else
1237     return 0;
1238 }
1239
1240
1241 /**
1242  * Get a const child by index.
1243  */
1244 const SGPropertyNode *
1245 SGPropertyNode::getChild (int position) const
1246 {
1247   if (position >= 0 && position < nChildren())
1248     return _children[position];
1249   else
1250     return 0;
1251 }
1252
1253
1254 /**
1255  * Get a non-const child by name and index, creating if necessary.
1256  */
1257 SGPropertyNode *
1258 SGPropertyNode::getChild (const string &name, int index, bool create)
1259 {
1260   int pos = find_child(name, index, _children);
1261   if (pos >= 0) {
1262     return _children[pos];
1263   } else if (create) {
1264     _children.push_back(new SGPropertyNode(name, index, this));
1265     return _children[_children.size()-1];
1266   } else {
1267     return 0;
1268   }
1269 }
1270
1271
1272 /**
1273  * Get a const child by name and index.
1274  */
1275 const SGPropertyNode *
1276 SGPropertyNode::getChild (const string &name, int index) const
1277 {
1278   int pos = find_child(name, index, _children);
1279   if (pos >= 0)
1280     return _children[pos];
1281   else
1282     return 0;
1283 }
1284
1285
1286 /**
1287  * Get all children with the same name (but different indices).
1288  */
1289 vector<SGPropertyNode *>
1290 SGPropertyNode::getChildren (const string &name)
1291 {
1292   vector<SGPropertyNode *> children;
1293   int max = _children.size();
1294
1295   for (int i = 0; i < max; i++)
1296     if (_children[i]->getName() == name)
1297       children.push_back(_children[i]);
1298
1299   sort(children.begin(), children.end(), CompareIndices());
1300   return children;
1301 }
1302
1303
1304 /**
1305  * Get all children const with the same name (but different indices).
1306  */
1307 vector<const SGPropertyNode *>
1308 SGPropertyNode::getChildren (const string &name) const
1309 {
1310   vector<const SGPropertyNode *> children;
1311   int max = _children.size();
1312
1313   for (int i = 0; i < max; i++)
1314     if (_children[i]->getName() == name)
1315       children.push_back(_children[i]);
1316
1317   sort(children.begin(), children.end(), CompareIndices());
1318   return children;
1319 }
1320
1321
1322 string
1323 SGPropertyNode::getPath (bool simplify) const
1324 {
1325   if (_parent == 0)
1326     return "";
1327
1328   string path = _parent->getPath(simplify);
1329   path += '/';
1330   path += _name;
1331   if (_index != 0 || !simplify) {
1332     char buffer[128];
1333     sprintf(buffer, "[%d]", _index);
1334     path += buffer;
1335   }
1336   return path;
1337 }
1338
1339 SGValue::Type
1340 SGPropertyNode::getType () const
1341 {
1342   if (_value != 0)
1343     return _value->getType();
1344   else
1345     return SGValue::UNKNOWN;
1346 }
1347
1348
1349 bool 
1350 SGPropertyNode::getBoolValue () const
1351 {
1352   return (_value == 0 ? SGRawValue<bool>::DefaultValue
1353           : _value->getBoolValue());
1354 }
1355
1356 int 
1357 SGPropertyNode::getIntValue () const
1358 {
1359   return (_value == 0 ? SGRawValue<int>::DefaultValue
1360           : _value->getIntValue());
1361 }
1362
1363 long 
1364 SGPropertyNode::getLongValue () const
1365 {
1366   return (_value == 0 ? SGRawValue<long>::DefaultValue
1367           : _value->getLongValue());
1368 }
1369
1370 float 
1371 SGPropertyNode::getFloatValue () const
1372 {
1373   return (_value == 0 ? SGRawValue<float>::DefaultValue
1374           : _value->getFloatValue());
1375 }
1376
1377 double 
1378 SGPropertyNode::getDoubleValue () const
1379 {
1380   return (_value == 0 ? SGRawValue<double>::DefaultValue
1381           : _value->getDoubleValue());
1382 }
1383
1384 string
1385 SGPropertyNode::getStringValue () const
1386 {
1387   return (_value == 0 ? SGRawValue<string>::DefaultValue
1388           : _value->getStringValue());
1389 }
1390
1391 bool
1392 SGPropertyNode::setBoolValue (bool val)
1393 {
1394   if (_value == 0)
1395     _value = new SGValue();
1396   return _value->setBoolValue(val);
1397 }
1398
1399 bool
1400 SGPropertyNode::setIntValue (int val)
1401 {
1402   if (_value == 0)
1403     _value = new SGValue();
1404   return _value->setIntValue(val);
1405 }
1406
1407 bool
1408 SGPropertyNode::setLongValue (long val)
1409 {
1410   if (_value == 0)
1411     _value = new SGValue();
1412   return _value->setLongValue(val);
1413 }
1414
1415 bool
1416 SGPropertyNode::setFloatValue (float val)
1417 {
1418   if (_value == 0)
1419     _value = new SGValue();
1420   return _value->setFloatValue(val);
1421 }
1422
1423 bool
1424 SGPropertyNode::setDoubleValue (double val)
1425 {
1426   if (_value == 0)
1427     _value = new SGValue();
1428   return _value->setDoubleValue(val);
1429 }
1430
1431 bool
1432 SGPropertyNode::setStringValue (string val)
1433 {
1434   if (_value == 0)
1435     _value = new SGValue();
1436   return _value->setStringValue(val);
1437 }
1438
1439 bool
1440 SGPropertyNode::setUnknownValue (string val)
1441 {
1442   if (_value == 0)
1443     _value = new SGValue();
1444   return _value->setUnknownValue(val);
1445 }
1446
1447 bool
1448 SGPropertyNode::isTied () const
1449 {
1450   return (_value == 0 ? false : _value->isTied());
1451 }
1452
1453 bool
1454 SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
1455 {
1456   if (_value == 0)
1457     _value = new SGValue();
1458   return _value->tie(rawValue, useDefault);
1459 }
1460
1461 bool
1462 SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
1463 {
1464   if (_value == 0)
1465     _value = new SGValue();
1466   return _value->tie(rawValue, useDefault);
1467 }
1468
1469 bool
1470 SGPropertyNode::tie (const SGRawValue<long> &rawValue, bool useDefault)
1471 {
1472   if (_value == 0)
1473     _value = new SGValue();
1474   return _value->tie(rawValue, useDefault);
1475 }
1476
1477 bool
1478 SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
1479 {
1480   if (_value == 0)
1481     _value = new SGValue();
1482   return _value->tie(rawValue, useDefault);
1483 }
1484
1485 bool
1486 SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
1487 {
1488   if (_value == 0)
1489     _value = new SGValue();
1490   return _value->tie(rawValue, useDefault);
1491 }
1492
1493 bool
1494 SGPropertyNode::tie (const SGRawValue<string> &rawValue, bool useDefault)
1495 {
1496   if (_value == 0)
1497     _value = new SGValue();
1498   return _value->tie(rawValue, useDefault);
1499 }
1500
1501 bool
1502 SGPropertyNode::untie ()
1503 {
1504   return (_value == 0 ? false : _value->untie());
1505 }
1506
1507 SGPropertyNode *
1508 SGPropertyNode::getRootNode ()
1509 {
1510   if (_parent == 0)
1511     return this;
1512   else
1513     return _parent->getRootNode();
1514 }
1515
1516 const SGPropertyNode *
1517 SGPropertyNode::getRootNode () const
1518 {
1519   if (_parent == 0)
1520     return this;
1521   else
1522     return _parent->getRootNode();
1523 }
1524
1525 SGPropertyNode *
1526 SGPropertyNode::getNode (const string &relative_path, bool create)
1527 {
1528   vector<PathComponent> components;
1529   parse_path(relative_path, components);
1530   return find_node(this, components, 0, create);
1531 }
1532
1533 const SGPropertyNode *
1534 SGPropertyNode::getNode (const string &relative_path) const
1535 {
1536   vector<PathComponent> components;
1537   parse_path(relative_path, components);
1538                                 // FIXME: cast away const
1539   return find_node((SGPropertyNode *)this, components, 0, false);
1540 }
1541
1542
1543 \f
1544 ////////////////////////////////////////////////////////////////////////
1545 // Convenience methods using relative paths.
1546 ////////////////////////////////////////////////////////////////////////
1547
1548
1549 /**
1550  * Test whether another node has a value attached.
1551  */
1552 bool
1553 SGPropertyNode::hasValue (const string &relative_path) const
1554 {
1555   const SGPropertyNode * node = getNode(relative_path);
1556   return (node == 0 ? false : node->hasValue());
1557 }
1558
1559
1560 /**
1561  * Get the value for another node.
1562  */
1563 SGValue *
1564 SGPropertyNode::getValue (const string &relative_path, bool create)
1565 {
1566   SGPropertyNode * node = getNode(relative_path, create);
1567   if (node != 0 && !node->hasValue())
1568     node->setUnknownValue("");
1569   return (node == 0 ? 0 : node->getValue(create));
1570 }
1571
1572
1573 /**
1574  * Get the value for another node.
1575  */
1576 const SGValue *
1577 SGPropertyNode::getValue (const string &relative_path) const
1578 {
1579   const SGPropertyNode * node = getNode(relative_path);
1580   return (node == 0 ? 0 : node->getValue());
1581 }
1582
1583
1584 /**
1585  * Get the value type for another node.
1586  */
1587 SGValue::Type
1588 SGPropertyNode::getType (const string &relative_path) const
1589 {
1590   const SGPropertyNode * node = getNode(relative_path);
1591   return (node == 0 ? SGValue::UNKNOWN : node->getType());
1592 }
1593
1594
1595 /**
1596  * Get a bool value for another node.
1597  */
1598 bool
1599 SGPropertyNode::getBoolValue (const string &relative_path,
1600                               bool defaultValue) const
1601 {
1602   const SGPropertyNode * node = getNode(relative_path);
1603   return (node == 0 ? defaultValue : node->getBoolValue());
1604 }
1605
1606
1607 /**
1608  * Get an int value for another node.
1609  */
1610 int
1611 SGPropertyNode::getIntValue (const string &relative_path,
1612                              int defaultValue) const
1613 {
1614   const SGPropertyNode * node = getNode(relative_path);
1615   return (node == 0 ? defaultValue : node->getIntValue());
1616 }
1617
1618
1619 /**
1620  * Get a long value for another node.
1621  */
1622 long
1623 SGPropertyNode::getLongValue (const string &relative_path,
1624                               long defaultValue) const
1625 {
1626   const SGPropertyNode * node = getNode(relative_path);
1627   return (node == 0 ? defaultValue : node->getLongValue());
1628 }
1629
1630
1631 /**
1632  * Get a float value for another node.
1633  */
1634 float
1635 SGPropertyNode::getFloatValue (const string &relative_path,
1636                                float defaultValue) const
1637 {
1638   const SGPropertyNode * node = getNode(relative_path);
1639   return (node == 0 ? defaultValue : node->getFloatValue());
1640 }
1641
1642
1643 /**
1644  * Get a double value for another node.
1645  */
1646 double
1647 SGPropertyNode::getDoubleValue (const string &relative_path,
1648                                 double defaultValue) const
1649 {
1650   const SGPropertyNode * node = getNode(relative_path);
1651   return (node == 0 ? defaultValue : node->getDoubleValue());
1652 }
1653
1654
1655 /**
1656  * Get a string value for another node.
1657  */
1658 string
1659 SGPropertyNode::getStringValue (const string &relative_path,
1660                                 string defaultValue) const
1661 {
1662   const SGPropertyNode * node = getNode(relative_path);
1663   return (node == 0 ? defaultValue : node->getStringValue());
1664 }
1665
1666
1667 /**
1668  * Set a bool value for another node.
1669  */
1670 bool
1671 SGPropertyNode::setBoolValue (const string &relative_path, bool value)
1672 {
1673   return getNode(relative_path, true)->setBoolValue(value);
1674 }
1675
1676
1677 /**
1678  * Set an int value for another node.
1679  */
1680 bool
1681 SGPropertyNode::setIntValue (const string &relative_path, int value)
1682 {
1683   return getNode(relative_path, true)->setIntValue(value);
1684 }
1685
1686
1687 /**
1688  * Set a long value for another node.
1689  */
1690 bool
1691 SGPropertyNode::setLongValue (const string &relative_path, long value)
1692 {
1693   return getNode(relative_path, true)->setLongValue(value);
1694 }
1695
1696
1697 /**
1698  * Set a float value for another node.
1699  */
1700 bool
1701 SGPropertyNode::setFloatValue (const string &relative_path, float value)
1702 {
1703   return getNode(relative_path, true)->setFloatValue(value);
1704 }
1705
1706
1707 /**
1708  * Set a double value for another node.
1709  */
1710 bool
1711 SGPropertyNode::setDoubleValue (const string &relative_path, double value)
1712 {
1713   return getNode(relative_path, true)->setDoubleValue(value);
1714 }
1715
1716
1717 /**
1718  * Set a string value for another node.
1719  */
1720 bool
1721 SGPropertyNode::setStringValue (const string &relative_path, string value)
1722 {
1723   return getNode(relative_path, true)->setStringValue(value);
1724 }
1725
1726
1727 /**
1728  * Set an unknown value for another node.
1729  */
1730 bool
1731 SGPropertyNode::setUnknownValue (const string &relative_path, string value)
1732 {
1733   return getNode(relative_path, true)->setUnknownValue(value);
1734 }
1735
1736
1737 /**
1738  * Test whether another node is tied.
1739  */
1740 bool
1741 SGPropertyNode::isTied (const string &relative_path) const
1742 {
1743   const SGPropertyNode * node = getNode(relative_path);
1744   return (node == 0 ? false : node->isTied());
1745 }
1746
1747
1748 /**
1749  * Tie a node reached by a relative path, creating it if necessary.
1750  */
1751 bool
1752 SGPropertyNode::tie (const string &relative_path,
1753                      const SGRawValue<bool> &rawValue,
1754                      bool useDefault)
1755 {
1756   return getNode(relative_path, true)->tie(rawValue, useDefault);
1757 }
1758
1759
1760 /**
1761  * Tie a node reached by a relative path, creating it if necessary.
1762  */
1763 bool
1764 SGPropertyNode::tie (const string &relative_path,
1765                      const SGRawValue<int> &rawValue,
1766                      bool useDefault)
1767 {
1768   return getNode(relative_path, true)->tie(rawValue, useDefault);
1769 }
1770
1771
1772 /**
1773  * Tie a node reached by a relative path, creating it if necessary.
1774  */
1775 bool
1776 SGPropertyNode::tie (const string &relative_path,
1777                      const SGRawValue<long> &rawValue,
1778                      bool useDefault)
1779 {
1780   return getNode(relative_path, true)->tie(rawValue, useDefault);
1781 }
1782
1783
1784 /**
1785  * Tie a node reached by a relative path, creating it if necessary.
1786  */
1787 bool
1788 SGPropertyNode::tie (const string &relative_path,
1789                      const SGRawValue<float> &rawValue,
1790                      bool useDefault)
1791 {
1792   return getNode(relative_path, true)->tie(rawValue, useDefault);
1793 }
1794
1795
1796 /**
1797  * Tie a node reached by a relative path, creating it if necessary.
1798  */
1799 bool
1800 SGPropertyNode::tie (const string &relative_path,
1801                      const SGRawValue<double> &rawValue,
1802                      bool useDefault)
1803 {
1804   return getNode(relative_path, true)->tie(rawValue, useDefault);
1805 }
1806
1807
1808 /**
1809  * Tie a node reached by a relative path, creating it if necessary.
1810  */
1811 bool
1812 SGPropertyNode::tie (const string &relative_path,
1813                      const SGRawValue<string> &rawValue,
1814                      bool useDefault)
1815 {
1816   return getNode(relative_path, true)->tie(rawValue, useDefault);
1817 }
1818
1819
1820 /**
1821  * Attempt to untie another node reached by a relative path.
1822  */
1823 bool
1824 SGPropertyNode::untie (const string &relative_path)
1825 {
1826   SGPropertyNode * node = getNode(relative_path);
1827   return (node == 0 ? false : node->untie());
1828 }
1829
1830 // end of props.cxx