1998-08-25 17:19:13 +00:00
|
|
|
//
|
1999-02-26 22:08:34 +00:00
|
|
|
// simple.cxx -- a really simplistic class to manage airport ID,
|
1998-08-25 17:19:13 +00:00
|
|
|
// lat, lon of the center of one of it's runways, and
|
|
|
|
// elevation in feet.
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started April 1998.
|
|
|
|
//
|
|
|
|
// Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
2000-04-27 03:26:36 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2000-03-29 20:21:31 +00:00
|
|
|
#include <sys/types.h> // for gdbm open flags
|
|
|
|
#include <sys/stat.h> // for gdbm open flags
|
2000-04-27 03:26:36 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_GDBM
|
|
|
|
# include <gdbm.h>
|
|
|
|
#else
|
|
|
|
# include <simgear/gdbm/gdbm.h>
|
|
|
|
#endif
|
2000-03-29 20:21:31 +00:00
|
|
|
|
2000-02-15 03:30:01 +00:00
|
|
|
#include <simgear/compiler.h>
|
|
|
|
|
2000-02-16 23:01:03 +00:00
|
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
#include <simgear/misc/fgstream.hxx>
|
1998-08-25 17:19:13 +00:00
|
|
|
|
1999-02-26 22:08:34 +00:00
|
|
|
#include <Main/options.hxx>
|
1998-08-25 17:19:13 +00:00
|
|
|
|
1999-02-26 22:08:34 +00:00
|
|
|
#include STL_STRING
|
1998-09-01 19:02:53 +00:00
|
|
|
#include STL_FUNCTIONAL
|
|
|
|
#include STL_ALGORITHM
|
1998-08-25 17:19:13 +00:00
|
|
|
|
1999-02-26 22:08:34 +00:00
|
|
|
#include "simple.hxx"
|
|
|
|
|
1998-09-02 14:35:38 +00:00
|
|
|
|
2000-03-29 20:21:31 +00:00
|
|
|
FGAirports::FGAirports( const string& file ) {
|
|
|
|
dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_READER, 0, NULL );
|
|
|
|
if ( dbf == NULL ) {
|
|
|
|
cout << "Error opening " << file << endl;
|
|
|
|
exit(-1);
|
|
|
|
} else {
|
|
|
|
cout << "successfully opened " << file << endl;
|
|
|
|
}
|
1998-08-25 17:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-29 20:21:31 +00:00
|
|
|
// search for the specified id
|
|
|
|
bool
|
|
|
|
FGAirports::search( const string& id, FGAirport* a ) const
|
|
|
|
{
|
|
|
|
FGAirport *tmp;
|
|
|
|
datum content;
|
|
|
|
datum key;
|
|
|
|
|
|
|
|
key.dptr = (char *)id.c_str();
|
|
|
|
key.dsize = id.length();
|
|
|
|
|
|
|
|
content = gdbm_fetch( dbf, key );
|
|
|
|
|
|
|
|
cout << "gdbm_fetch() finished" << endl;
|
|
|
|
|
|
|
|
if ( content.dptr != NULL ) {
|
|
|
|
tmp = (FGAirport *)content.dptr;
|
|
|
|
|
|
|
|
// a->id = tmp->id;
|
|
|
|
a->longitude = tmp->longitude;
|
|
|
|
a->latitude = tmp->latitude;
|
|
|
|
a->elevation = tmp->elevation;
|
|
|
|
|
|
|
|
free( content.dptr );
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FGAirport
|
|
|
|
FGAirports::search( const string& id ) const
|
|
|
|
{
|
|
|
|
FGAirport a, *tmp;
|
|
|
|
datum content;
|
|
|
|
datum key;
|
|
|
|
|
|
|
|
key.dptr = (char *)id.c_str();
|
|
|
|
key.dsize = id.length();
|
|
|
|
|
|
|
|
content = gdbm_fetch( dbf, key );
|
|
|
|
|
|
|
|
if ( content.dptr != NULL ) {
|
|
|
|
tmp = (FGAirport *)content.dptr;
|
|
|
|
a = *tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
FGAirports::~FGAirports( void ) {
|
|
|
|
gdbm_close( dbf );
|
|
|
|
}
|
1998-09-02 14:35:38 +00:00
|
|
|
|
2000-03-29 20:21:31 +00:00
|
|
|
|
|
|
|
// Constructor
|
|
|
|
FGAirportsUtil::FGAirportsUtil() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// load the data
|
|
|
|
int FGAirportsUtil::load( const string& file ) {
|
|
|
|
FGAirport a;
|
1998-08-25 17:19:13 +00:00
|
|
|
|
1998-09-01 19:02:53 +00:00
|
|
|
airports.erase( airports.begin(), airports.end() );
|
1998-08-25 17:19:13 +00:00
|
|
|
|
2000-03-29 20:21:31 +00:00
|
|
|
fg_gzifstream in( file );
|
1999-06-20 03:54:57 +00:00
|
|
|
if ( !in.is_open() ) {
|
2000-03-29 20:21:31 +00:00
|
|
|
FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
|
1998-11-06 21:17:31 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
1998-09-01 19:02:53 +00:00
|
|
|
|
1998-09-02 14:35:38 +00:00
|
|
|
/*
|
1998-09-01 19:02:53 +00:00
|
|
|
// We can use the STL copy algorithm because the input
|
|
|
|
// file doesn't contain and comments or blank lines.
|
2000-03-29 20:21:31 +00:00
|
|
|
copy( istream_iterator<FGAirport,ptrdiff_t>(in.stream()),
|
|
|
|
istream_iterator<FGAirport,ptrdiff_t>(),
|
1998-09-01 19:02:53 +00:00
|
|
|
inserter( airports, airports.begin() ) );
|
1998-09-02 14:35:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// read in each line of the file
|
1999-06-20 03:54:57 +00:00
|
|
|
|
|
|
|
#ifdef __MWERKS__
|
|
|
|
|
1998-11-06 14:46:59 +00:00
|
|
|
in >> skipcomment;
|
1999-06-20 03:54:57 +00:00
|
|
|
char c = 0;
|
|
|
|
while ( in.get(c) && c != '\0' ) {
|
|
|
|
in.putback(c);
|
1999-06-20 01:52:31 +00:00
|
|
|
in >> a;
|
|
|
|
airports.insert(a);
|
|
|
|
in >> skipcomment;
|
|
|
|
}
|
|
|
|
|
1999-06-20 03:54:57 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
in >> skipcomment;
|
|
|
|
while ( ! in.eof() ) {
|
|
|
|
in >> a;
|
|
|
|
airports.insert(a);
|
|
|
|
in >> skipcomment;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1998-09-01 19:02:53 +00:00
|
|
|
return 1;
|
1998-08-25 17:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-29 20:21:31 +00:00
|
|
|
// save the data in gdbm format
|
|
|
|
bool FGAirportsUtil::dump_gdbm( const string& file ) {
|
|
|
|
|
|
|
|
GDBM_FILE dbf;
|
|
|
|
dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_NEWDB | GDBM_FAST,
|
|
|
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
|
|
|
|
NULL );
|
|
|
|
if ( dbf == NULL ) {
|
|
|
|
cout << "Error opening " << file << endl;
|
|
|
|
exit(-1);
|
|
|
|
} else {
|
|
|
|
cout << "successfully opened " << file << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator current = airports.begin();
|
|
|
|
const_iterator end = airports.end();
|
|
|
|
while ( current != end ) {
|
|
|
|
datum key;
|
|
|
|
key.dptr = (char *)current->id.c_str();
|
|
|
|
key.dsize = current->id.length();
|
|
|
|
|
|
|
|
datum content;
|
|
|
|
FGAirport tmp = *current;
|
|
|
|
content.dptr = (char *)(& tmp);
|
|
|
|
content.dsize = sizeof( *current );
|
|
|
|
|
|
|
|
gdbm_store( dbf, key, content, GDBM_REPLACE );
|
|
|
|
|
|
|
|
++current;
|
|
|
|
}
|
|
|
|
|
|
|
|
gdbm_close( dbf );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-08-25 17:19:13 +00:00
|
|
|
// search for the specified id
|
1998-09-01 19:02:53 +00:00
|
|
|
bool
|
2000-03-29 20:21:31 +00:00
|
|
|
FGAirportsUtil::search( const string& id, FGAirport* a ) const
|
1998-09-01 19:02:53 +00:00
|
|
|
{
|
2000-03-29 20:21:31 +00:00
|
|
|
const_iterator it = airports.find( FGAirport(id) );
|
1998-09-01 19:02:53 +00:00
|
|
|
if ( it != airports.end() )
|
|
|
|
{
|
|
|
|
*a = *it;
|
|
|
|
return true;
|
1998-08-25 17:19:13 +00:00
|
|
|
}
|
1998-09-01 19:02:53 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-08-25 17:19:13 +00:00
|
|
|
|
2000-03-29 20:21:31 +00:00
|
|
|
FGAirport
|
|
|
|
FGAirportsUtil::search( const string& id ) const
|
1998-09-01 19:02:53 +00:00
|
|
|
{
|
2000-03-29 20:21:31 +00:00
|
|
|
FGAirport a;
|
1998-09-01 19:02:53 +00:00
|
|
|
this->search( id, &a );
|
|
|
|
return a;
|
1998-08-25 17:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Destructor
|
2000-03-29 20:21:31 +00:00
|
|
|
FGAirportsUtil::~FGAirportsUtil( void ) {
|
1998-08-25 17:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|