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