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-05-27 05:54:02 +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
|
|
|
|
2000-05-27 05:54:02 +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 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"
|
|
|
|
|
2001-03-23 22:59:18 +00:00
|
|
|
SG_USING_NAMESPACE(std);
|
1998-09-02 14:35:38 +00:00
|
|
|
|
2000-03-29 20:21:31 +00:00
|
|
|
FGAirports::FGAirports( const string& file ) {
|
2000-05-27 05:54:02 +00:00
|
|
|
// open the specified database readonly
|
|
|
|
storage = new c4_Storage( file.c_str(), false );
|
|
|
|
|
2000-05-27 06:40:55 +00:00
|
|
|
if ( !storage->Strategy().IsValid() ) {
|
2001-03-24 06:03:11 +00:00
|
|
|
SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
|
2000-05-27 06:40:55 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
2000-05-27 05:54:02 +00:00
|
|
|
|
|
|
|
vAirport = new c4_View;
|
|
|
|
*vAirport =
|
|
|
|
storage->GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
|
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
|
|
|
|
{
|
2000-05-27 05:54:02 +00:00
|
|
|
c4_StringProp pID ("ID");
|
|
|
|
c4_FloatProp pLon ("Longitude");
|
|
|
|
c4_FloatProp pLat ("Latitude");
|
|
|
|
c4_FloatProp pElev ("Elevation");
|
2000-03-29 20:21:31 +00:00
|
|
|
|
2000-05-27 05:54:02 +00:00
|
|
|
int idx = vAirport->Find(pID[id.c_str()]);
|
|
|
|
cout << "idx = " << idx << endl;
|
2000-03-29 20:21:31 +00:00
|
|
|
|
2000-05-27 05:54:02 +00:00
|
|
|
if ( idx == -1 ) {
|
2000-03-29 20:21:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2000-05-27 05:54:02 +00:00
|
|
|
c4_RowRef r = vAirport->GetAt(idx);
|
|
|
|
|
|
|
|
a->longitude = (double) pLon(r);
|
|
|
|
a->latitude = (double) pLat(r);
|
|
|
|
a->elevation = (double) pElev(r);
|
|
|
|
|
2000-03-29 20:21:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FGAirport
|
|
|
|
FGAirports::search( const string& id ) const
|
|
|
|
{
|
2000-05-27 05:54:02 +00:00
|
|
|
FGAirport a;
|
|
|
|
search( id, &a );
|
2000-03-29 20:21:31 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
FGAirports::~FGAirports( void ) {
|
2001-01-31 15:58:53 +00:00
|
|
|
delete storage;
|
2000-03-29 20:21:31 +00:00
|
|
|
}
|
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() ) {
|
2001-03-24 06:03:11 +00:00
|
|
|
SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
|
1998-11-06 21:17:31 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
1998-09-01 19:02:53 +00:00
|
|
|
|
2000-09-20 23:26:17 +00:00
|
|
|
// skip first line of file
|
|
|
|
char tmp[256];
|
|
|
|
in.getline( tmp, 256 );
|
|
|
|
|
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__
|
|
|
|
|
2000-09-20 23:26:17 +00:00
|
|
|
in >> ::skipws;
|
1999-06-20 03:54:57 +00:00
|
|
|
char c = 0;
|
|
|
|
while ( in.get(c) && c != '\0' ) {
|
2000-09-20 23:26:17 +00:00
|
|
|
if ( c == 'A' ) {
|
|
|
|
in >> a;
|
|
|
|
in >> skipeol;
|
|
|
|
airports.insert(a);
|
|
|
|
} else if ( c == 'R' ) {
|
|
|
|
in >> skipeol;
|
|
|
|
} else {
|
|
|
|
in >> skipeol;
|
|
|
|
}
|
|
|
|
in >> ::skipws;
|
1999-06-20 01:52:31 +00:00
|
|
|
}
|
|
|
|
|
1999-06-20 03:54:57 +00:00
|
|
|
#else
|
|
|
|
|
2000-09-21 22:59:27 +00:00
|
|
|
in >> ::skipws;
|
1999-06-20 03:54:57 +00:00
|
|
|
while ( ! in.eof() ) {
|
2000-09-20 23:26:17 +00:00
|
|
|
char c = 0;
|
|
|
|
in.get(c);
|
|
|
|
if ( c == 'A' ) {
|
|
|
|
in >> a;
|
|
|
|
cout << "in <- " << a.id << endl;
|
|
|
|
in >> skipeol;
|
|
|
|
airports.insert(a);
|
|
|
|
} else if ( c == 'R' ) {
|
|
|
|
in >> skipeol;
|
|
|
|
} else {
|
|
|
|
in >> skipeol;
|
|
|
|
}
|
2000-09-21 22:59:27 +00:00
|
|
|
in >> ::skipws;
|
1999-06-20 03:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
2000-05-27 05:54:02 +00:00
|
|
|
bool FGAirportsUtil::dump_mk4( const string& file ) {
|
2000-03-29 20:21:31 +00:00
|
|
|
|
2000-05-27 05:54:02 +00:00
|
|
|
// open database for writing
|
|
|
|
c4_Storage storage( file.c_str(), true );
|
2000-04-27 21:57:08 +00:00
|
|
|
|
2000-05-27 05:54:02 +00:00
|
|
|
// need to do something about error handling here!
|
2000-04-27 21:57:08 +00:00
|
|
|
|
2000-05-27 05:54:02 +00:00
|
|
|
// define the properties
|
|
|
|
c4_StringProp pID ("ID");
|
|
|
|
c4_FloatProp pLon ("Longitude");
|
|
|
|
c4_FloatProp pLat ("Latitude");
|
|
|
|
c4_FloatProp pElev ("Elevation");
|
|
|
|
|
|
|
|
// Start with an empty view of the proper structure.
|
|
|
|
c4_View vAirport =
|
|
|
|
storage.GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
|
|
|
|
|
|
|
|
c4_Row row;
|
2000-03-29 20:21:31 +00:00
|
|
|
|
2000-10-02 21:49:04 +00:00
|
|
|
const_iterator current = airports.begin();
|
2000-03-29 20:21:31 +00:00
|
|
|
const_iterator end = airports.end();
|
|
|
|
while ( current != end ) {
|
2000-05-27 05:54:02 +00:00
|
|
|
// add each airport record
|
2000-09-20 23:26:17 +00:00
|
|
|
cout << "out -> " << current->id << endl;
|
2000-05-27 05:54:02 +00:00
|
|
|
pID (row) = current->id.c_str();
|
|
|
|
pLon (row) = current->longitude;
|
|
|
|
pLat (row) = current->latitude;
|
|
|
|
pElev (row) = current->elevation;
|
|
|
|
vAirport.Add(row);
|
2000-03-29 20:21:31 +00:00
|
|
|
|
|
|
|
++current;
|
|
|
|
}
|
|
|
|
|
2000-05-27 05:54:02 +00:00
|
|
|
// commit our changes
|
|
|
|
storage.Commit();
|
2000-03-29 20:21:31 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|