]> git.mxchange.org Git - flightgear.git/blob - src/Airports/dynamicloader.cxx
Alexis Bory:
[flightgear.git] / src / Airports / dynamicloader.cxx
1 // This program is free software; you can redistribute it and/or
2 // modify it under the terms of the GNU General Public License as
3 // published by the Free Software Foundation; either version 2 of the
4 // License, or (at your option) any later version.
5 //
6 // This program is distributed in the hope that it will be useful, but
7 // WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9 // General Public License for more details.
10 //
11 // You should have received a copy of the GNU General Public License
12 // along with this program; if not, write to the Free Software
13 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14 //
15
16 #ifdef HAVE_CONFIG_H
17 #  include <config.h>
18 #endif
19
20 #include <cstdlib>
21 #include <cstring> // for strcmp
22
23 #include "dynamicloader.hxx"
24
25 /*****************************************************************************
26  * Helper function for parsing position string
27  ****************************************************************************/
28 static double processPosition(const string &pos)
29 {
30   string prefix;
31   string subs;
32   string degree;
33   string decimal;
34   int sign = 1;
35   double value;
36   subs = pos;
37   prefix= subs.substr(0,1);
38   if (prefix == string("S") || (prefix == string("W")))
39     sign = -1;
40   subs    = subs.substr(1, subs.length());
41   degree  = subs.substr(0, subs.find(" ",0));
42   decimal = subs.substr(subs.find(" ",0), subs.length());
43   
44   value = sign * (atof(degree.c_str()) + atof(decimal.c_str())/60.0);
45   return value;
46 }
47
48 FGAirportDynamicsXMLLoader::FGAirportDynamicsXMLLoader(FGAirportDynamics* dyn):
49     XMLVisitor(), _dynamics(dyn)
50 {}
51
52 void  FGAirportDynamicsXMLLoader::startXML () {
53   //cout << "FGAirportDynamicsLoader::Start XML" << endl;
54 }
55
56 void  FGAirportDynamicsXMLLoader::endXML () {
57   //cout << "End XML" << endl;
58 }
59
60 void FGAirportDynamicsXMLLoader::startParking(const XMLAttributes &atts)
61 {
62   string type;
63   int index = 0;
64   string gateName, gateNumber;
65   string lat, lon;
66   double heading = 0.0;
67   double radius = 1.0;
68   string airlineCodes;
69   int pushBackRoute = 0;
70   
71   for (int i = 0; i < atts.size(); i++)
72         {
73     string attname(atts.getName(i));
74           if (attname == "index") {
75       index = std::atoi(atts.getValue(i));
76     } else if (attname == "type")
77             type = atts.getValue(i);
78     else if (attname == "name")
79       gateName = atts.getValue(i);
80           else if (attname == "number")
81             gateNumber = atts.getValue(i);
82           else if (attname == "lat")
83       lat = atts.getValue(i);
84           else if (attname == "lon")
85             lon = atts.getValue(i);
86           else if (attname == "heading")
87             heading = std::atof(atts.getValue(i));
88           else if (attname == "radius") {
89             string radiusStr = atts.getValue(i);
90             if (radiusStr.find("M") != string::npos)
91               radiusStr = radiusStr.substr(0, radiusStr.find("M",0));
92             radius = std::atof(radiusStr.c_str());
93           }
94           else if (attname == "airlineCodes")
95       airlineCodes = atts.getValue(i);
96     else if (attname == "pushBackRoute") {
97       pushBackRoute = std::atoi(atts.getValue(i));      
98     }
99         }
100  
101   SGGeod pos(SGGeod::fromDeg(processPosition(lon), processPosition(lat)));
102   
103   FGParking* pk = new FGParking(0, index, pos, heading, radius,
104                                 gateName + gateNumber, type, airlineCodes);
105   pk->setPushBackPoint(pushBackRoute);
106   _dynamics->addParking(pk);
107 }
108
109 void FGAirportDynamicsXMLLoader::startNode(const XMLAttributes &atts)
110 {
111   int index = 0;
112   string lat, lon;
113   bool onRunway = false;
114   int holdPointType = 0;
115   
116   for (int i = 0; i < atts.size() ; i++)
117         {
118           string attname(atts.getName(i));
119           if (attname == "index")
120             index = std::atoi(atts.getValue(i));
121           else if (attname == "lat")
122       lat = atts.getValue(i);
123           else if (attname == "lon")
124             lon = atts.getValue(i);
125     else if (attname == "isOnRunway")
126       onRunway = std::atoi(atts.getValue(i)) != 0;
127           else if (attname == "holdPointType") {
128       string attval = atts.getValue(i);
129       if (attval=="none") {
130         holdPointType=0;
131       } else if (attval=="normal") {
132         holdPointType=1;
133       } else if (attval=="CAT II/III") {
134         holdPointType=3;
135       } else if (attval=="PushBack") {
136         holdPointType=3;
137       } else {
138         holdPointType=0;
139       }
140     }
141         }
142   
143   SGGeod pos(SGGeod::fromDeg(processPosition(lon), processPosition(lat)));
144   FGTaxiNode* taxiNode = new FGTaxiNode(0, index, pos, onRunway, holdPointType);
145   _dynamics->getGroundNetwork()->addNode(taxiNode);
146 }
147
148 void FGAirportDynamicsXMLLoader::startArc(const XMLAttributes &atts)
149 {  
150   int begin = 0, end = 0;
151   bool isPushBackRoute = false;
152   
153   for (int i = 0; i < atts.size() ; i++)
154         {
155           string attname = atts.getName(i);
156           if (attname == "begin")
157             begin = std::atoi(atts.getValue(i));
158           else if (attname == "end")
159             end = std::atoi(atts.getValue(i));
160     else if (attname == "isPushBackRoute")
161             isPushBackRoute = std::atoi(atts.getValue(i)) != 0;
162         }
163   
164   _dynamics->getGroundNetwork()->addSegment(new FGTaxiSegment(begin, end, isPushBackRoute));
165 }
166
167 void FGAirportDynamicsXMLLoader::startElement (const char * name, const XMLAttributes &atts)
168 {
169   if (!strcmp("Parking", name)) {
170     startParking(atts);
171   } else if (!strcmp("node", name)) {
172     startNode(atts);
173   } else if (!strcmp("arc", name)) {
174     startArc(atts);
175   }
176 }
177
178 void  FGAirportDynamicsXMLLoader::endElement (const char * name)
179 {
180   int valueAsInt = atoi(value.c_str());
181   if (!strcmp("version", name)) {
182     _dynamics->getGroundNetwork()->addVersion(valueAsInt);
183   } else if (!strcmp("AWOS", name)) {
184     _dynamics->addAwosFreq(valueAsInt);
185   } else if (!strcmp("UNICOM", name)) {
186     _dynamics->addUnicomFreq(valueAsInt);
187   } else if (!strcmp("CLEARANCE", name)) {
188     _dynamics->addClearanceFreq(valueAsInt);
189   } else if (!strcmp("GROUND", name)) {
190     _dynamics->addGroundFreq(valueAsInt);
191   } else if (!strcmp("TOWER", name)) {
192     _dynamics->addTowerFreq(valueAsInt);
193   } else if (!strcmp("APPROACH", name)) {
194     _dynamics->addApproachFreq(valueAsInt);
195   }
196 }
197
198 void  FGAirportDynamicsXMLLoader::data (const char * s, int len) {
199   string token = string(s,len);
200   //cout << "Character data " << string(s,len) << endl;
201   if ((token.find(" ") == string::npos && (token.find('\n')) == string::npos))
202     value += token;
203   else
204     value = string("");
205 }
206
207 void  FGAirportDynamicsXMLLoader::pi (const char * target, const char * data) {
208   //cout << "Processing instruction " << target << ' ' << data << endl;
209 }
210
211 void  FGAirportDynamicsXMLLoader::warning (const char * message, int line, int column) {
212   SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
213 }
214
215 void  FGAirportDynamicsXMLLoader::error (const char * message, int line, int column) {
216   SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');
217 }