]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/input_output/FGPropertyReader.cpp
Fix for bug 1304 - crash loading XML route
[flightgear.git] / src / FDM / JSBSim / input_output / FGPropertyReader.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGPropertyReader.cpp
4  Author:       Bertrand Coconnier
5  Date started: 12/30/13
6  Purpose:      Read and manage properties from XML data
7
8   ------------- Copyright (C) 2013 Bertrand Coconnier -------------
9
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU Lesser General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
18  details.
19
20  You should have received a copy of the GNU Lesser General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA  02111-1307, USA.
23
24  Further information about the GNU Lesser General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 --------------------------------------------------------------------------------
29 This class reads and manages properties defined in XML data
30
31 HISTORY
32 --------------------------------------------------------------------------------
33 12/30/13   BC    Created
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 INCLUDES
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38
39 #include "FGPropertyReader.h"
40 #include "FGPropertyManager.h"
41 #include "FGXMLElement.h"
42 #include "FGJSBBase.h"
43
44 using namespace std;
45
46 namespace JSBSim {
47
48 IDENT(IdSrc,"$Id: FGPropertyReader.cpp,v 1.3 2014/01/13 10:46:00 ehofman Exp $");
49 IDENT(IdHdr,ID_PROPERTYREADER);
50
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 CLASS IMPLEMENTATION
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54
55 bool FGPropertyReader::ResetToIC(void)
56 {
57   map<FGPropertyNode_ptr, double>::iterator it = interface_prop_initial_value.begin();
58   for (;it != interface_prop_initial_value.end(); ++it) {
59     FGPropertyNode* node = it->first;
60     if (!node->getAttribute(SGPropertyNode::PRESERVE))
61       node->setDoubleValue(it->second);
62   }
63
64   return true;
65 }
66
67 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68
69 void FGPropertyReader::LoadProperties(Element* el, FGPropertyManager* PM,
70                                       bool override)
71 {
72   // Interface properties are all stored in the interface properties array.
73   string interface_property_string = "";
74
75   Element *property_element = el->FindElement("property");
76   if (property_element && FGJSBBase::debug_lvl > 0) {
77     cout << endl << "    ";
78     if (override)
79       cout << "Overriding";
80     else
81       cout << "Declared";
82     cout << " properties" << endl << endl;
83   }
84
85   while (property_element) {
86     FGPropertyNode* node = 0;
87     double value=0.0;
88     if ( ! property_element->GetAttributeValue("value").empty())
89       value = property_element->GetAttributeValueAsNumber("value");
90
91     interface_property_string = property_element->GetDataLine();
92     if (PM->HasNode(interface_property_string)) {
93       if (override) {
94         node = PM->GetNode(interface_property_string);
95
96         if (FGJSBBase::debug_lvl > 0) {
97           if (interface_prop_initial_value.find(node) == interface_prop_initial_value.end()) {
98             cout << property_element->ReadFrom()
99                  << "  The following property will be overridden but it has not been" << endl
100                  << "  defined in the current model '" << el->GetName() << "'" << endl;
101           }
102
103           cout << "      " << "Overriding value for property " << interface_property_string << endl
104                << "       (old value: " << node->getDoubleValue() << "  new value: " << value << ")"
105                << endl << endl;
106         }
107
108         node->setDoubleValue(value);
109       }
110       else {
111         cerr << property_element->ReadFrom()
112              << "      Property " << interface_property_string 
113              << " is already defined." << endl;
114         property_element = el->FindNextElement("property");
115         continue;
116       }
117     } else {
118       interface_properties.push_back(value);
119       PM->Tie(interface_property_string, &interface_properties.back());
120       if (FGJSBBase::debug_lvl > 0)
121         cout << "      " << interface_property_string << " (initial value: " 
122              << value << ")" << endl << endl;
123       node = PM->GetNode(interface_property_string);
124     }
125     interface_prop_initial_value[node] = value;
126     if (property_element->GetAttributeValue("persistent") == string("true"))
127       node->setAttribute(SGPropertyNode::PRESERVE, true);
128
129     property_element = el->FindNextElement("property");
130   }
131   
132   // End of interface property loading logic
133 }
134 }