]> git.mxchange.org Git - flightgear.git/blob - src/Airports/xmlloader.cxx
Merge branches 'jmt/dialog' and 'durk/scenery'
[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 void XMLLoader::load(FGAirportDynamics* d) {
36   FGAirportDynamicsXMLLoader visitor(d);
37   if (fgGetBool("/sim/paths/use-custom-scenery-data") == false) {
38    SGPath parkpath( globals->get_fg_root() );
39    parkpath.append( "/AI/Airports/" );
40    parkpath.append( d->getId() );
41    parkpath.append( "parking.xml" );
42    SG_LOG(SG_GENERAL, SG_DEBUG, "running old loader:" << parkpath.c_str());
43    if (parkpath.exists()) {
44        try {
45            readXML(parkpath.str(), visitor);
46            d->init();
47        } 
48        catch (const sg_exception &) {
49        }
50    }
51   } else {
52     if(loadAirportXMLDataIntoVisitor(d->getId(), "groundnet", visitor)) {
53         d->init();
54     }
55   }
56 }
57
58 void XMLLoader::load(FGRunwayPreference* p) {
59   FGRunwayPreferenceXMLLoader visitor(p);
60   if (fgGetBool("/sim/paths/use-custom-scenery-data") == false) {
61     SGPath rwyPrefPath( globals->get_fg_root() );
62     rwyPrefPath.append( "AI/Airports/" );
63     rwyPrefPath.append( p->getId() );
64     rwyPrefPath.append( "rwyuse.xml" );
65     if (rwyPrefPath.exists()) {
66         try {
67             readXML(rwyPrefPath.str(), visitor);
68         } 
69         catch (const sg_exception &) {
70         }
71      }
72   } else {
73     loadAirportXMLDataIntoVisitor(p->getId(), "rwyuse", visitor);
74   }
75 }
76
77 void XMLLoader::load(FGSidStar* p) {
78   SGPath path;
79   if (findAirportData(p->getId(), "SID", path)) {
80     p->load(path);
81   }
82 }
83
84 bool XMLLoader::findAirportData(const std::string& aICAO, 
85     const std::string& aFileName, SGPath& aPath)
86 {
87   string_list sc = globals->get_fg_scenery();
88   char buffer[128];
89   ::snprintf(buffer, 128, "%c/%c/%c/%s.%s.xml", 
90     aICAO[0], aICAO[1], aICAO[2], 
91     aICAO.c_str(), aFileName.c_str());
92
93   for (string_list_iterator it = sc.begin(); it != sc.end(); ++it) {
94     SGPath path(*it);
95     path.append("Airports");
96     path.append(string(buffer));
97     if (path.exists()) {
98       aPath = path;
99       return true;
100     } // of path exists
101   } // of scenery path iteration
102   return false;
103 }
104
105 bool XMLLoader::loadAirportXMLDataIntoVisitor(const string& aICAO, 
106     const string& aFileName, XMLVisitor& aVisitor)
107 {
108   SGPath path;
109   if (!findAirportData(aICAO, aFileName, path)) {
110     SG_LOG(SG_GENERAL, SG_DEBUG, "loadAirportXMLDataIntoVisitor: failed to find data for " << aICAO << "/" << aFileName);
111     return false;
112   }
113
114   SG_LOG(SG_GENERAL, SG_DEBUG, "loadAirportXMLDataIntoVisitor: loading from " << path.str());
115   readXML(path.str(), aVisitor);
116   return true;
117 }
118