]> git.mxchange.org Git - flightgear.git/blob - src/Airports/xmlloader.cxx
Some headers missing... ;)
[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 <cstdio>
21
22 #include <simgear/misc/sg_path.hxx>
23 #include <simgear/xml/easyxml.hxx>
24 #include <simgear/misc/strutils.hxx>
25 #include <simgear/timing/timestamp.hxx>
26
27 #include <Main/globals.hxx>
28 #include <Main/fg_props.hxx>
29
30 #include "xmlloader.hxx"
31 #include "dynamicloader.hxx"
32 #include "runwayprefloader.hxx"
33
34 #include "dynamics.hxx"
35 #include "simple.hxx"
36 #include "runwayprefs.hxx"
37
38 #include <Navaids/NavDataCache.hxx>
39
40 using std::string;
41
42 XMLLoader::XMLLoader() {}
43 XMLLoader::~XMLLoader() {}
44
45 void XMLLoader::load(FGAirportDynamics* d)
46 {
47   SGPath path;
48   if (!findAirportData(d->parent()->ident(), "groundnet", path)) {
49     return;
50   }
51
52   flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
53   if (!cache->isCachedFileModified(path)) {
54     return;
55   }
56   
57   SG_LOG(SG_NAVAID, SG_INFO, "reading groundnet data from " << path);
58   SGTimeStamp t;
59   try {
60     flightgear::NavDataCache::Transaction txn(cache);
61     t.stamp();
62     {
63       // drop all current data
64       cache->dropGroundnetFor(d->parent()->guid());
65       
66       FGAirportDynamicsXMLLoader visitor(d);
67       readXML(path.str(), visitor);
68     } // ensure visitor is destroyed so its destructor runs
69     cache->stampCacheFile(path);
70     txn.commit();
71   } catch (sg_exception& e) {
72     SG_LOG(SG_NAVAID, SG_INFO, "parsing groundnet XML failed:" << e.getFormattedMessage());
73   }
74
75   SG_LOG(SG_NAVAID, SG_INFO, "parsing groundnet XML took " << t.elapsedMSec());
76 }
77
78 void XMLLoader::load(FGRunwayPreference* p) {
79   FGRunwayPreferenceXMLLoader visitor(p);
80   loadAirportXMLDataIntoVisitor(p->getId(), "rwyuse", visitor);
81 }
82
83 bool XMLLoader::findAirportData(const std::string& aICAO, 
84     const std::string& aFileName, SGPath& aPath)
85 {
86   string fileName(aFileName);
87   if (!simgear::strutils::ends_with(aFileName, ".xml")) {
88     fileName.append(".xml");
89   }
90   
91   string_list sc = globals->get_fg_scenery();
92   char buffer[128];
93   ::snprintf(buffer, 128, "%c/%c/%c/%s.%s", 
94     aICAO[0], aICAO[1], aICAO[2], 
95     aICAO.c_str(), fileName.c_str());
96
97   for (string_list_iterator it = sc.begin(); it != sc.end(); ++it) {
98     // fg_senery contains empty strings as "markers" (see FGGlobals::set_fg_scenery)
99     if (!it->empty()) {
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     }
108   } // of scenery path iteration
109   return false;
110 }
111
112 bool XMLLoader::loadAirportXMLDataIntoVisitor(const string& aICAO, 
113     const string& aFileName, XMLVisitor& aVisitor)
114 {
115   SGPath path;
116   if (!findAirportData(aICAO, aFileName, path)) {
117     SG_LOG(SG_NAVAID, SG_DEBUG, "loadAirportXMLDataIntoVisitor: failed to find data for " << aICAO << "/" << aFileName);
118     return false;
119   }
120
121   SG_LOG(SG_GENERAL, SG_DEBUG, "loadAirportXMLDataIntoVisitor: loading from " << path.str());
122   readXML(path.str(), aVisitor);
123   return true;
124 }
125