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( SG_HAVE_NATIVE_SGI_COMPILERS )
55 # include <iostream.h>
56 #elif defined( __BORLANDC__ ) || defined (__APPLE__)
61 #if ! defined( SG_HAVE_NATIVE_SGI_COMPILERS )
62 SG_USING_STD(istream);
67 operator >> ( istream& in, FGAirport& a )
69 return in >> a.id >> a.latitude >> a.longitude >> a.elevation;
73 FGAirports::FGAirports( const string& file ) {
74 // open the specified database readonly
75 storage = new c4_Storage( file.c_str(), false );
77 if ( !storage->Strategy().IsValid() ) {
78 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
82 vAirport = new c4_View;
84 storage->GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
88 // search for the specified id
90 FGAirports::search( const string& id, FGAirport* a ) const
92 c4_StringProp pID ("ID");
93 c4_FloatProp pLon ("Longitude");
94 c4_FloatProp pLat ("Latitude");
95 c4_FloatProp pElev ("Elevation");
97 int idx = vAirport->Find(pID[id.c_str()]);
98 cout << "idx = " << idx << endl;
104 c4_RowRef r = vAirport->GetAt(idx);
105 a->id = (const char *) pID(r); /// NHV fix wrong case crash
106 a->longitude = (double) pLon(r);
107 a->latitude = (double) pLat(r);
108 a->elevation = (double) pElev(r);
115 FGAirports::search( const string& id ) const
124 FGAirports::~FGAirports( void ) {
130 FGAirportsUtil::FGAirportsUtil() {
135 int FGAirportsUtil::load( const string& file ) {
138 airports.erase( airports.begin(), airports.end() );
140 sg_gzifstream in( file );
141 if ( !in.is_open() ) {
142 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
145 SG_LOG( SG_GENERAL, SG_ALERT, "opened: " << file );
148 // skip first line of file
150 in.getline( tmp, 2048 );
152 // read in each line of the file
158 while ( in.get(c) && c != '\0' ) {
161 SG_LOG( SG_GENERAL, SG_INFO, a.id );
164 } else if ( c == 'R' ) {
176 while ( ! in.eof() ) {
178 if ( token == "A" ) {
180 SG_LOG( SG_GENERAL, SG_INFO, "in <- " << a.id );
183 } else if ( token == "R" ) {
197 // save the data in gdbm format
198 bool FGAirportsUtil::dump_mk4( const string& file ) {
200 // open database for writing
201 c4_Storage storage( file.c_str(), true );
203 // need to do something about error handling here!
205 // define the properties
206 c4_StringProp pID ("ID");
207 c4_FloatProp pLon ("Longitude");
208 c4_FloatProp pLat ("Latitude");
209 c4_FloatProp pElev ("Elevation");
211 // Start with an empty view of the proper structure.
213 storage.GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
217 const_iterator current = airports.begin();
218 const_iterator end = airports.end();
219 while ( current != end ) {
220 // add each airport record
221 cout << "out -> " << current->id << endl;
222 pID (row) = current->id.c_str();
223 pLon (row) = current->longitude;
224 pLat (row) = current->latitude;
225 pElev (row) = current->elevation;
231 // commit our changes
238 // search for the specified id
240 FGAirportsUtil::search( const string& id, FGAirport* a ) const
242 const_iterator it = airports.find( FGAirport(id) );
243 if ( it != airports.end() )
256 FGAirportsUtil::search( const string& id ) const
259 this->search( id, &a );
265 FGAirportsUtil::~FGAirportsUtil( void ) {