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.
31 // #include <sys/types.h> // for gdbm open flags
32 // #include <sys/stat.h> // for gdbm open flags
37 // # include <simgear/gdbm/gdbm.h>
40 #include <simgear/compiler.h>
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/misc/fgstream.hxx>
45 #include <Main/options.hxx>
48 #include STL_FUNCTIONAL
49 #include STL_ALGORITHM
53 FG_USING_NAMESPACE(std);
55 FGAirports::FGAirports( const string& file ) {
56 // open the specified database readonly
57 storage = new c4_Storage( file.c_str(), false );
59 if ( !storage->Strategy().IsValid() ) {
60 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
64 vAirport = new c4_View;
66 storage->GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
70 // search for the specified id
72 FGAirports::search( const string& id, FGAirport* a ) const
74 c4_StringProp pID ("ID");
75 c4_FloatProp pLon ("Longitude");
76 c4_FloatProp pLat ("Latitude");
77 c4_FloatProp pElev ("Elevation");
79 int idx = vAirport->Find(pID[id.c_str()]);
80 cout << "idx = " << idx << endl;
86 c4_RowRef r = vAirport->GetAt(idx);
88 a->longitude = (double) pLon(r);
89 a->latitude = (double) pLat(r);
90 a->elevation = (double) pElev(r);
97 FGAirports::search( const string& id ) const
106 FGAirports::~FGAirports( void ) {
107 // gdbm_close( dbf );
112 FGAirportsUtil::FGAirportsUtil() {
117 int FGAirportsUtil::load( const string& file ) {
120 airports.erase( airports.begin(), airports.end() );
122 fg_gzifstream in( file );
123 if ( !in.is_open() ) {
124 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
128 // skip first line of file
130 in.getline( tmp, 256 );
133 // read in each line of the file
139 while ( in.get(c) && c != '\0' ) {
144 } else if ( c == 'R' ) {
155 while ( ! in.eof() ) {
160 cout << "in <- " << a.id << endl;
163 } else if ( c == 'R' ) {
177 // save the data in gdbm format
178 bool FGAirportsUtil::dump_mk4( const string& file ) {
180 // open database for writing
181 c4_Storage storage( file.c_str(), true );
183 // need to do something about error handling here!
185 // define the properties
186 c4_StringProp pID ("ID");
187 c4_FloatProp pLon ("Longitude");
188 c4_FloatProp pLat ("Latitude");
189 c4_FloatProp pElev ("Elevation");
191 // Start with an empty view of the proper structure.
193 storage.GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
197 const_iterator current = airports.begin();
198 const_iterator end = airports.end();
199 while ( current != end ) {
200 // add each airport record
201 cout << "out -> " << current->id << endl;
202 pID (row) = current->id.c_str();
203 pLon (row) = current->longitude;
204 pLat (row) = current->latitude;
205 pElev (row) = current->elevation;
211 // commit our changes
218 // search for the specified id
220 FGAirportsUtil::search( const string& id, FGAirport* a ) const
222 const_iterator it = airports.find( FGAirport(id) );
223 if ( it != airports.end() )
236 FGAirportsUtil::search( const string& id ) const
239 this->search( id, &a );
245 FGAirportsUtil::~FGAirportsUtil( void ) {