]> git.mxchange.org Git - flightgear.git/blob - src/Airports/xmlloader.cxx
f2f81daf975178e4ae1fa589b872dbbaefd2ad52
[flightgear.git] / src / Airports / xmlloader.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 #include <simgear/misc/sg_path.hxx>
17
18 #include <simgear/xml/easyxml.hxx>
19
20 #include <Main/globals.hxx>
21 #include <Main/fg_props.hxx>
22
23 #include "xmlloader.hxx"
24 #include "dynamicloader.hxx"
25 #include "runwayprefloader.hxx"
26
27 #include "dynamics.hxx"
28 #include "runwayprefs.hxx"
29
30 using std::string;
31
32 XMLLoader::XMLLoader() {}
33 XMLLoader::~XMLLoader() {}
34
35 string XMLLoader::expandICAODirs(const string& in){
36      //cerr << "Expanding " << in << endl;
37      if (in.size() == 4) {
38           char buffer[11];
39           snprintf(buffer, 11, "%c/%c/%c", in[0], in[1], in[2]);
40           //cerr << "result: " << buffer << endl;
41           return string(buffer);
42      } else {
43            return in;
44      }
45      //exit(1);
46 }
47
48 void XMLLoader::load(FGAirportDynamics* d) {
49     FGAirportDynamicsXMLLoader visitor(d);
50     if (fgGetBool("/sim/traffic-manager/use-custom-scenery-data") == false) {
51        SGPath parkpath( globals->get_fg_root() );
52        parkpath.append( "/AI/Airports/" );
53        parkpath.append( d->getId() );
54        parkpath.append( "parking.xml" );
55        SG_LOG(SG_GENERAL, SG_DEBUG, "running old loader:" << parkpath.c_str());
56        if (parkpath.exists()) {
57            try {
58                readXML(parkpath.str(), visitor);
59                d->init();
60            } 
61            catch (const sg_exception &) {
62            }
63        }
64     } else {
65         string_list sc = globals->get_fg_scenery();
66         char buffer[32];
67         snprintf(buffer, 32, "%s.groundnet.xml", d->getId().c_str() );
68         string airportDir = XMLLoader::expandICAODirs(d->getId());
69         for (string_list_iterator i = sc.begin(); i != sc.end(); i++) {
70             SGPath parkpath( *i );
71             parkpath.append( "Airports" );
72             parkpath.append ( airportDir );
73             parkpath.append( string (buffer) );
74             SG_LOG(SG_GENERAL, SG_DEBUG, "Trying to read ground net:" << parkpath.c_str());
75             if (parkpath.exists()) {
76                 SG_LOG(SG_GENERAL, SG_DEBUG, "reading ground net:" << parkpath.c_str());
77                 try {
78                     readXML(parkpath.str(), visitor);
79                     d->init();
80                 } 
81                 catch (const sg_exception &) {
82                 }
83                 return;
84             }
85         }
86     }
87 }
88
89 void XMLLoader::load(FGRunwayPreference* p) {
90     FGRunwayPreferenceXMLLoader visitor(p);
91     if (fgGetBool("/sim/traffic-manager/use-custom-scenery-data") == false) {
92         SGPath rwyPrefPath( globals->get_fg_root() );
93         rwyPrefPath.append( "AI/Airports/" );
94         rwyPrefPath.append( p->getId() );
95         rwyPrefPath.append( "rwyuse.xml" );
96         if (rwyPrefPath.exists()) {
97             try {
98                 readXML(rwyPrefPath.str(), visitor);
99             } 
100             catch (const sg_exception &) {
101             }
102          }
103       }  else {
104        string_list sc = globals->get_fg_scenery();
105        char buffer[32];
106        snprintf(buffer, 32, "%s.rwyuse.xml", p->getId().c_str() );
107        string airportDir = expandICAODirs(p->getId());
108        for (string_list_iterator i = sc.begin(); i != sc.end(); i++) {
109            SGPath rwypath( *i );
110            rwypath.append( "Airports" );
111            rwypath.append ( airportDir );
112            rwypath.append( string(buffer) );
113            if (rwypath.exists()) {
114                try {
115                    readXML(rwypath.str(), visitor);
116                 } 
117                 catch (const sg_exception &) {
118                 }
119                 return;
120             }
121         }
122     }
123 }
124
125 void XMLLoader::load(FGSidStar* p) {
126     //FGRunwayPreferenceXMLLoader visitor(p);
127     if (fgGetBool("/sim/traffic-manager/use-custom-scenery-data") == true) {
128        string_list sc = globals->get_fg_scenery();
129        char buffer[32];
130        snprintf(buffer, 32, "%s.SID.xml", p->getId().c_str() );
131        string airportDir = expandICAODirs(p->getId());
132        for (string_list_iterator i = sc.begin(); i != sc.end(); i++) {
133            SGPath sidpath( *i );
134            sidpath.append( "Airports" );
135            sidpath.append ( airportDir );
136            sidpath.append( string(buffer) );
137            if (sidpath.exists()) {
138                try {
139                    //readXML(rwypath.str(), visitor);
140                    //cerr << "Reading SID procedure : " << sidpath.str() << endl;
141                    p->load(sidpath);
142                 } 
143                 catch (const sg_exception &) {
144                 }
145                 return;
146             }
147         }
148     }
149 }
150
151 bool XMLLoader::findAirportData(const std::string& aICAO, 
152     const std::string& aFileName, SGPath& aPath)
153 {
154   string_list sc = globals->get_fg_scenery();
155   char buffer[128];
156   ::snprintf(buffer, 128, "%c/%c/%c/%s.%s.xml", 
157     aICAO[0], aICAO[1], aICAO[2], 
158     aICAO.c_str(), aFileName.c_str());
159
160   for (string_list_iterator it = sc.begin(); it != sc.end(); ++it) {
161     SGPath path(*it);
162     path.append("Airports");
163     path.append(string(buffer));
164     if (path.exists()) {
165       aPath = path;
166       return true;
167     } // of path exists
168   } // of scenery path iteration
169
170
171   return false;
172 }
173
174 bool XMLLoader::loadAirportXMLDataIntoVisitor(const string& aICAO, 
175     const string& aFileName, XMLVisitor& aVisitor)
176 {
177   SGPath path;
178   if (!findAirportData(aICAO, aFileName, path)) {
179     return false;
180   }
181
182   readXML(path.str(), aVisitor);
183   return true;
184 }
185