2 // simple.cxx -- a really simplistic class to manage airport ID,
3 // lat, lon of the center of one of it's runways, and
6 // Written by Curtis Olson, started April 1998.
8 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <simgear/compiler.h>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/misc/sgstream.hxx>
36 #include STL_FUNCTIONAL
37 #include STL_ALGORITHM
41 SG_USING_NAMESPACE(std);
44 # define NDEBUG // she don't work without it.
52 #ifdef SG_HAVE_STD_INCLUDES
54 #elif defined( __BORLANDC__ ) || defined (__APPLE__)
59 SG_USING_STD(istream);
63 operator >> ( istream& in, FGAirport& a )
65 return in >> a.id >> a.latitude >> a.longitude >> a.elevation;
69 FGAirports::FGAirports( const string& file ) {
70 // open the specified database readonly
71 storage = new c4_Storage( file.c_str(), false );
73 if ( !storage->Strategy().IsValid() ) {
74 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
78 vAirport = new c4_View;
80 storage->GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
84 // search for the specified id
86 FGAirports::search( const string& id, FGAirport* a ) const
88 c4_StringProp pID ("ID");
89 c4_FloatProp pLon ("Longitude");
90 c4_FloatProp pLat ("Latitude");
91 c4_FloatProp pElev ("Elevation");
93 int idx = vAirport->Find(pID[id.c_str()]);
94 SG_LOG( SG_TERRAIN, SG_INFO, "idx = " << idx );
100 c4_RowRef r = vAirport->GetAt(idx);
101 a->id = (const char *) pID(r); /// NHV fix wrong case crash
102 a->longitude = (double) pLon(r);
103 a->latitude = (double) pLat(r);
104 a->elevation = (double) pElev(r);
111 FGAirports::search( const string& id ) const
120 FGAirports::~FGAirports( void ) {
126 FGAirportsUtil::FGAirportsUtil() {
131 int FGAirportsUtil::load( const string& file ) {
134 airports.erase( airports.begin(), airports.end() );
136 sg_gzifstream in( file );
137 if ( !in.is_open() ) {
138 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
141 SG_LOG( SG_GENERAL, SG_ALERT, "opened: " << file );
144 // skip first line of file
146 in.getline( tmp, 2048 );
148 // read in each line of the file
154 while ( in.get(c) && c != '\0' ) {
157 SG_LOG( SG_GENERAL, SG_INFO, a.id );
160 } else if ( c == 'R' ) {
172 while ( ! in.eof() ) {
174 if ( token == "A" ) {
176 SG_LOG( SG_GENERAL, SG_INFO, "in <- " << a.id );
179 } else if ( token == "R" ) {
193 // save the data in gdbm format
194 bool FGAirportsUtil::dump_mk4( const string& file ) {
196 // open database for writing
197 c4_Storage storage( file.c_str(), true );
199 // need to do something about error handling here!
201 // define the properties
202 c4_StringProp pID ("ID");
203 c4_FloatProp pLon ("Longitude");
204 c4_FloatProp pLat ("Latitude");
205 c4_FloatProp pElev ("Elevation");
207 // Start with an empty view of the proper structure.
209 storage.GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
213 const_iterator current = airports.begin();
214 const_iterator end = airports.end();
215 while ( current != end ) {
216 // add each airport record
217 SG_LOG( SG_TERRAIN, SG_BULK, "out -> " << current->id );
218 pID (row) = current->id.c_str();
219 pLon (row) = current->longitude;
220 pLat (row) = current->latitude;
221 pElev (row) = current->elevation;
227 // commit our changes
234 // search for the specified id
236 FGAirportsUtil::search( const string& id, FGAirport* a ) const
238 const_iterator it = airports.find( FGAirport(id) );
239 if ( it != airports.end() )
252 FGAirportsUtil::search( const string& id ) const
255 this->search( id, &a );
261 FGAirportsUtil::~FGAirportsUtil( void ) {