]> git.mxchange.org Git - flightgear.git/blob - src/Airports/xmlloader.cxx
Merge branch 'refs/heads/topics/loadfp' into next
[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 fileName(aFileName);
88   int extPos = fileName.size() - 4;
89   if ((int) fileName.rfind(".xml") != extPos) {
90     fileName.append(".xml");
91   }
92   
93   string_list sc = globals->get_fg_scenery();
94   char buffer[128];
95   ::snprintf(buffer, 128, "%c/%c/%c/%s.%s", 
96     aICAO[0], aICAO[1], aICAO[2], 
97     aICAO.c_str(), fileName.c_str());
98
99   for (string_list_iterator it = sc.begin(); it != sc.end(); ++it) {
100     SGPath path(*it);
101     path.append("Airports");
102     path.append(string(buffer));
103     if (path.exists()) {
104       aPath = path;
105       return true;
106     } // of path exists
107   } // of scenery path iteration
108   return false;
109 }
110
111 bool XMLLoader::loadAirportXMLDataIntoVisitor(const string& aICAO, 
112     const string& aFileName, XMLVisitor& aVisitor)
113 {
114   SGPath path;
115   if (!findAirportData(aICAO, aFileName, path)) {
116     SG_LOG(SG_GENERAL, SG_DEBUG, "loadAirportXMLDataIntoVisitor: failed to find data for " << aICAO << "/" << aFileName);
117     return false;
118   }
119
120   SG_LOG(SG_GENERAL, SG_DEBUG, "loadAirportXMLDataIntoVisitor: loading from " << path.str());
121   readXML(path.str(), aVisitor);
122   return true;
123 }
124