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.
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.
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.
20 #include <simgear/misc/sg_path.hxx>
21 #include <simgear/xml/easyxml.hxx>
22 #include <simgear/misc/strutils.hxx>
23 #include <simgear/timing/timestamp.hxx>
25 #include <Main/globals.hxx>
26 #include <Main/fg_props.hxx>
28 #include "xmlloader.hxx"
29 #include "dynamicloader.hxx"
30 #include "runwayprefloader.hxx"
32 #include "dynamics.hxx"
34 #include "runwayprefs.hxx"
36 #include <Navaids/NavDataCache.hxx>
40 XMLLoader::XMLLoader() {}
41 XMLLoader::~XMLLoader() {}
43 void XMLLoader::load(FGAirportDynamics* d)
46 if (!findAirportData(d->parent()->ident(), "groundnet", path)) {
50 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
51 if (!cache->isCachedFileModified(path)) {
55 SG_LOG(SG_GENERAL, SG_INFO, "reading groundnet data from " << path);
58 cache->beginTransaction();
61 FGAirportDynamicsXMLLoader visitor(d);
62 readXML(path.str(), visitor);
63 } // ensure visitor is destroyed so its destructor runs
64 cache->stampCacheFile(path);
65 cache->commitTransaction();
66 } catch (sg_exception& e) {
67 cache->abortTransaction();
70 SG_LOG(SG_GENERAL, SG_INFO, "parsing XML took " << t.elapsedMSec());
73 void XMLLoader::load(FGRunwayPreference* p) {
74 FGRunwayPreferenceXMLLoader visitor(p);
75 loadAirportXMLDataIntoVisitor(p->getId(), "rwyuse", visitor);
78 bool XMLLoader::findAirportData(const std::string& aICAO,
79 const std::string& aFileName, SGPath& aPath)
81 string fileName(aFileName);
82 if (!simgear::strutils::ends_with(aFileName, ".xml")) {
83 fileName.append(".xml");
86 string_list sc = globals->get_fg_scenery();
88 ::snprintf(buffer, 128, "%c/%c/%c/%s.%s",
89 aICAO[0], aICAO[1], aICAO[2],
90 aICAO.c_str(), fileName.c_str());
92 for (string_list_iterator it = sc.begin(); it != sc.end(); ++it) {
93 // fg_senery contains empty strings as "markers" (see FGGlobals::set_fg_scenery)
96 path.append("Airports");
97 path.append(string(buffer));
103 } // of scenery path iteration
107 bool XMLLoader::loadAirportXMLDataIntoVisitor(const string& aICAO,
108 const string& aFileName, XMLVisitor& aVisitor)
111 if (!findAirportData(aICAO, aFileName, path)) {
112 SG_LOG(SG_GENERAL, SG_DEBUG, "loadAirportXMLDataIntoVisitor: failed to find data for " << aICAO << "/" << aFileName);
116 SG_LOG(SG_GENERAL, SG_DEBUG, "loadAirportXMLDataIntoVisitor: loading from " << path.str());
117 readXML(path.str(), aVisitor);