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
54 FGAirports::FGAirports( const string& file ) {
55 dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_READER, 0, NULL );
57 cout << "Error opening " << file << endl;
60 cout << "successfully opened " << file << endl;
65 // search for the specified id
67 FGAirports::search( const string& id, FGAirport* a ) const
73 key.dptr = (char *)id.c_str();
74 key.dsize = id.length();
76 content = gdbm_fetch( dbf, key );
78 cout << "gdbm_fetch() finished" << endl;
80 if ( content.dptr != NULL ) {
81 tmp = (FGAirport *)content.dptr;
84 a->longitude = tmp->longitude;
85 a->latitude = tmp->latitude;
86 a->elevation = tmp->elevation;
99 FGAirports::search( const string& id ) const
105 key.dptr = (char *)id.c_str();
106 key.dsize = id.length();
108 content = gdbm_fetch( dbf, key );
110 if ( content.dptr != NULL ) {
111 tmp = (FGAirport *)content.dptr;
120 FGAirports::~FGAirports( void ) {
126 FGAirportsUtil::FGAirportsUtil() {
131 int FGAirportsUtil::load( const string& file ) {
134 airports.erase( airports.begin(), airports.end() );
136 fg_gzifstream in( file );
137 if ( !in.is_open() ) {
138 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
143 // We can use the STL copy algorithm because the input
144 // file doesn't contain and comments or blank lines.
145 copy( istream_iterator<FGAirport,ptrdiff_t>(in.stream()),
146 istream_iterator<FGAirport,ptrdiff_t>(),
147 inserter( airports, airports.begin() ) );
150 // read in each line of the file
156 while ( in.get(c) && c != '\0' ) {
166 while ( ! in.eof() ) {
178 // save the data in gdbm format
179 bool FGAirportsUtil::dump_gdbm( const string& file ) {
183 #if !defined( MACOS )
184 dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_NEWDB | GDBM_FAST,
185 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
188 dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_NEWDB | GDBM_FAST,
193 cout << "Error opening " << file << endl;
196 cout << "successfully opened " << file << endl;
199 iterator current = airports.begin();
200 const_iterator end = airports.end();
201 while ( current != end ) {
203 key.dptr = (char *)current->id.c_str();
204 key.dsize = current->id.length();
207 FGAirport tmp = *current;
208 content.dptr = (char *)(& tmp);
209 content.dsize = sizeof( *current );
211 gdbm_store( dbf, key, content, GDBM_REPLACE );
222 // search for the specified id
224 FGAirportsUtil::search( const string& id, FGAirport* a ) const
226 const_iterator it = airports.find( FGAirport(id) );
227 if ( it != airports.end() )
240 FGAirportsUtil::search( const string& id ) const
243 this->search( id, &a );
249 FGAirportsUtil::~FGAirportsUtil( void ) {