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.
27 #include <sys/types.h> // for gdbm open flags
28 #include <sys/stat.h> // for gdbm open flags
31 #include <simgear/compiler.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/fgstream.hxx>
36 #include <Main/options.hxx>
39 #include STL_FUNCTIONAL
40 #include STL_ALGORITHM
45 FGAirports::FGAirports( const string& file ) {
46 dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_READER, 0, NULL );
48 cout << "Error opening " << file << endl;
51 cout << "successfully opened " << file << endl;
56 // search for the specified id
58 FGAirports::search( const string& id, FGAirport* a ) const
64 key.dptr = (char *)id.c_str();
65 key.dsize = id.length();
67 content = gdbm_fetch( dbf, key );
69 cout << "gdbm_fetch() finished" << endl;
71 if ( content.dptr != NULL ) {
72 tmp = (FGAirport *)content.dptr;
75 a->longitude = tmp->longitude;
76 a->latitude = tmp->latitude;
77 a->elevation = tmp->elevation;
90 FGAirports::search( const string& id ) const
96 key.dptr = (char *)id.c_str();
97 key.dsize = id.length();
99 content = gdbm_fetch( dbf, key );
101 if ( content.dptr != NULL ) {
102 tmp = (FGAirport *)content.dptr;
111 FGAirports::~FGAirports( void ) {
117 FGAirportsUtil::FGAirportsUtil() {
122 int FGAirportsUtil::load( const string& file ) {
125 airports.erase( airports.begin(), airports.end() );
127 fg_gzifstream in( file );
128 if ( !in.is_open() ) {
129 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
134 // We can use the STL copy algorithm because the input
135 // file doesn't contain and comments or blank lines.
136 copy( istream_iterator<FGAirport,ptrdiff_t>(in.stream()),
137 istream_iterator<FGAirport,ptrdiff_t>(),
138 inserter( airports, airports.begin() ) );
141 // read in each line of the file
147 while ( in.get(c) && c != '\0' ) {
157 while ( ! in.eof() ) {
169 // save the data in gdbm format
170 bool FGAirportsUtil::dump_gdbm( const string& file ) {
173 dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_NEWDB | GDBM_FAST,
174 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
177 cout << "Error opening " << file << endl;
180 cout << "successfully opened " << file << endl;
183 iterator current = airports.begin();
184 const_iterator end = airports.end();
185 while ( current != end ) {
187 key.dptr = (char *)current->id.c_str();
188 key.dsize = current->id.length();
191 FGAirport tmp = *current;
192 content.dptr = (char *)(& tmp);
193 content.dsize = sizeof( *current );
195 gdbm_store( dbf, key, content, GDBM_REPLACE );
206 // search for the specified id
208 FGAirportsUtil::search( const string& id, FGAirport* a ) const
210 const_iterator it = airports.find( FGAirport(id) );
211 if ( it != airports.end() )
224 FGAirportsUtil::search( const string& id ) const
227 this->search( id, &a );
233 FGAirportsUtil::~FGAirportsUtil( void ) {