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 dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_READER, 0, NULL );
58 cout << "Error opening " << file << endl;
61 cout << "successfully opened " << file << endl;
66 // search for the specified id
68 FGAirports::search( const string& id, FGAirport* a ) const
74 key.dptr = (char *)id.c_str();
75 key.dsize = id.length();
77 content = gdbm_fetch( dbf, key );
79 cout << "gdbm_fetch() finished" << endl;
81 if ( content.dptr != NULL ) {
82 tmp = (FGAirport *)content.dptr;
85 a->longitude = tmp->longitude;
86 a->latitude = tmp->latitude;
87 a->elevation = tmp->elevation;
100 FGAirports::search( const string& id ) const
106 key.dptr = (char *)id.c_str();
107 key.dsize = id.length();
109 content = gdbm_fetch( dbf, key );
111 if ( content.dptr != NULL ) {
112 tmp = (FGAirport *)content.dptr;
121 FGAirports::~FGAirports( void ) {
127 FGAirportsUtil::FGAirportsUtil() {
132 int FGAirportsUtil::load( const string& file ) {
135 airports.erase( airports.begin(), airports.end() );
137 fg_gzifstream in( file );
138 if ( !in.is_open() ) {
139 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
144 // We can use the STL copy algorithm because the input
145 // file doesn't contain and comments or blank lines.
146 copy( istream_iterator<FGAirport,ptrdiff_t>(in.stream()),
147 istream_iterator<FGAirport,ptrdiff_t>(),
148 inserter( airports, airports.begin() ) );
151 // read in each line of the file
157 while ( in.get(c) && c != '\0' ) {
167 while ( ! in.eof() ) {
179 // save the data in gdbm format
180 bool FGAirportsUtil::dump_gdbm( const string& file ) {
184 #if defined( MACOS ) || defined( _MSC_VER )
185 dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_NEWDB | GDBM_FAST,
188 dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_NEWDB | GDBM_FAST,
189 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
194 cout << "Error opening " << file << endl;
197 cout << "successfully opened " << file << endl;
200 iterator current = airports.begin();
201 const_iterator end = airports.end();
202 while ( current != end ) {
204 key.dptr = (char *)current->id.c_str();
205 key.dsize = current->id.length();
208 FGAirport tmp = *current;
209 content.dptr = (char *)(& tmp);
210 content.dsize = sizeof( *current );
212 gdbm_store( dbf, key, content, GDBM_REPLACE );
223 // search for the specified id
225 FGAirportsUtil::search( const string& id, FGAirport* a ) const
227 const_iterator it = airports.find( FGAirport(id) );
228 if ( it != airports.end() )
241 FGAirportsUtil::search( const string& id ) const
244 this->search( id, &a );
250 FGAirportsUtil::~FGAirportsUtil( void ) {