]> git.mxchange.org Git - flightgear.git/blob - src/Airports/xmlloader.cxx
Fix file access mode for newnavradio.[ch]xx
[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 #ifdef HAVE_CONFIG_H
17 #  include "config.h"
18 #endif
19
20 #include <simgear/misc/sg_path.hxx>
21 #include <simgear/xml/easyxml.hxx>
22 #include <simgear/misc/strutils.hxx>
23
24 #include <Main/globals.hxx>
25 #include <Main/fg_props.hxx>
26
27 #include "xmlloader.hxx"
28 #include "dynamicloader.hxx"
29 #include "runwayprefloader.hxx"
30
31 #include "dynamics.hxx"
32 #include "runwayprefs.hxx"
33
34 using std::string;
35
36 XMLLoader::XMLLoader() {}
37 XMLLoader::~XMLLoader() {}
38
39 void XMLLoader::load(FGAirportDynamics* d) {
40   FGAirportDynamicsXMLLoader visitor(d);
41   if (fgGetBool("/sim/paths/use-custom-scenery-data") == false) {
42    SGPath parkpath( globals->get_fg_root() );
43    parkpath.append( "/AI/Airports/" );
44    parkpath.append( d->getId() );
45    parkpath.append( "parking.xml" );
46    SG_LOG(SG_GENERAL, SG_DEBUG, "running old loader:" << parkpath.c_str());
47    if (parkpath.exists()) {
48        try {
49            readXML(parkpath.str(), visitor);
50            d->init();
51        } 
52        catch (const sg_exception &) {
53        }
54    }
55   } else {
56     if(loadAirportXMLDataIntoVisitor(d->getId(), "groundnet", visitor)) {
57         d->init();
58     }
59   }
60 }
61
62 void XMLLoader::load(FGRunwayPreference* p) {
63   FGRunwayPreferenceXMLLoader visitor(p);
64   if (fgGetBool("/sim/paths/use-custom-scenery-data") == false) {
65     SGPath rwyPrefPath( globals->get_fg_root() );
66     rwyPrefPath.append( "AI/Airports/" );
67     rwyPrefPath.append( p->getId() );
68     rwyPrefPath.append( "rwyuse.xml" );
69     if (rwyPrefPath.exists()) {
70         try {
71             readXML(rwyPrefPath.str(), visitor);
72         } 
73         catch (const sg_exception &) {
74         }
75      }
76   } else {
77     loadAirportXMLDataIntoVisitor(p->getId(), "rwyuse", visitor);
78   }
79 }
80
81 void XMLLoader::load(FGSidStar* p) {
82   SGPath path;
83   if (findAirportData(p->getId(), "SID", path)) {
84     p->load(path);
85   }
86 }
87
88 bool XMLLoader::findAirportData(const std::string& aICAO, 
89     const std::string& aFileName, SGPath& aPath)
90 {
91   string fileName(aFileName);
92   if (!simgear::strutils::ends_with(aFileName, ".xml")) {
93     fileName.append(".xml");
94   }
95   
96   string_list sc = globals->get_fg_scenery();
97   char buffer[128];
98   ::snprintf(buffer, 128, "%c/%c/%c/%s.%s", 
99     aICAO[0], aICAO[1], aICAO[2], 
100     aICAO.c_str(), fileName.c_str());
101
102   for (string_list_iterator it = sc.begin(); it != sc.end(); ++it) {
103     // fg_senery contains empty strings as "markers" (see FGGlobals::set_fg_scenery)
104     if (!it->empty()) {
105         SGPath path(*it);
106         path.append("Airports");
107         path.append(string(buffer));
108         if (path.exists()) {
109           aPath = path;
110           return true;
111         } // of path exists
112     }
113   } // of scenery path iteration
114   return false;
115 }
116
117 bool XMLLoader::loadAirportXMLDataIntoVisitor(const string& aICAO, 
118     const string& aFileName, XMLVisitor& aVisitor)
119 {
120   SGPath path;
121   if (!findAirportData(aICAO, aFileName, path)) {
122     SG_LOG(SG_GENERAL, SG_DEBUG, "loadAirportXMLDataIntoVisitor: failed to find data for " << aICAO << "/" << aFileName);
123     return false;
124   }
125
126   SG_LOG(SG_GENERAL, SG_DEBUG, "loadAirportXMLDataIntoVisitor: loading from " << path.str());
127   readXML(path.str(), aVisitor);
128   return true;
129 }
130