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.
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.
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.
21 #include <cstring> // for strcmp
23 #include "dynamicloader.hxx"
25 /*****************************************************************************
26 * Helper function for parsing position string
27 ****************************************************************************/
28 static double processPosition(const string &pos)
37 prefix= subs.substr(0,1);
38 if (prefix == string("S") || (prefix == string("W")))
40 subs = subs.substr(1, subs.length());
41 degree = subs.substr(0, subs.find(" ",0));
42 decimal = subs.substr(subs.find(" ",0), subs.length());
44 value = sign * (atof(degree.c_str()) + atof(decimal.c_str())/60.0);
48 FGAirportDynamicsXMLLoader::FGAirportDynamicsXMLLoader(FGAirportDynamics* dyn):
49 XMLVisitor(), _dynamics(dyn)
52 void FGAirportDynamicsXMLLoader::startXML () {
53 //cout << "FGAirportDynamicsLoader::Start XML" << endl;
56 void FGAirportDynamicsXMLLoader::endXML () {
57 //cout << "End XML" << endl;
60 void FGAirportDynamicsXMLLoader::startParking(const XMLAttributes &atts)
64 string gateName, gateNumber;
69 int pushBackRoute = 0;
71 for (int i = 0; i < atts.size(); i++)
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());
94 else if (attname == "airlineCodes")
95 airlineCodes = atts.getValue(i);
96 else if (attname == "pushBackRoute") {
97 pushBackRoute = std::atoi(atts.getValue(i));
101 SGGeod pos(SGGeod::fromDeg(processPosition(lon), processPosition(lat)));
103 FGParking* pk = new FGParking(0, index, pos, heading, radius,
104 gateName + gateNumber, type, airlineCodes);
105 pk->setPushBackPoint(pushBackRoute);
106 _dynamics->addParking(pk);
109 void FGAirportDynamicsXMLLoader::startNode(const XMLAttributes &atts)
113 bool onRunway = false;
114 int holdPointType = 0;
116 for (int i = 0; i < atts.size() ; i++)
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 = (bool) std::atoi(atts.getValue(i));
127 else if (attname == "holdPointType") {
128 string attval = atts.getValue(i);
129 if (attval=="none") {
131 } else if (attval=="normal") {
133 } else if (attval=="CAT II/III") {
135 } else if (attval=="PushBack") {
143 SGGeod pos(SGGeod::fromDeg(processPosition(lon), processPosition(lat)));
144 FGTaxiNode* taxiNode = new FGTaxiNode(0, index, pos, onRunway, holdPointType);
145 _dynamics->getGroundNetwork()->addNode(taxiNode);
148 void FGAirportDynamicsXMLLoader::startArc(const XMLAttributes &atts)
150 int begin = 0, end = 0;
151 bool isPushBackRoute = false;
153 for (int i = 0; i < atts.size() ; i++)
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 = (bool) std::atoi(atts.getValue(i));
164 _dynamics->getGroundNetwork()->addSegment(new FGTaxiSegment(begin, end, isPushBackRoute));
167 void FGAirportDynamicsXMLLoader::startElement (const char * name, const XMLAttributes &atts)
169 if (!strcmp("Parking", name)) {
171 } else if (!strcmp("node", name)) {
173 } else if (!strcmp("arc", name)) {
178 void FGAirportDynamicsXMLLoader::endElement (const char * name)
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);
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))
207 void FGAirportDynamicsXMLLoader::pi (const char * target, const char * data) {
208 //cout << "Processing instruction " << target << ' ' << data << endl;
211 void FGAirportDynamicsXMLLoader::warning (const char * message, int line, int column) {
212 SG_LOG(SG_IO, SG_WARN, "Warning: " << message << " (" << line << ',' << column << ')');
215 void FGAirportDynamicsXMLLoader::error (const char * message, int line, int column) {
216 SG_LOG(SG_IO, SG_ALERT, "Error: " << message << " (" << line << ',' << column << ')');