X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FAirports%2Fsimple.cxx;h=ab3b47baf97de74471856feaf2eb147b50c9ddba;hb=d14bba8458d6b1276b33467610d3dbc5a3f8d939;hp=cad2e4671d7197eea1d0fb9f6ae39f2ce394ede1;hpb=949fc00815feb26e79e7498c82d4129b6961f2c8;p=flightgear.git diff --git a/src/Airports/simple.cxx b/src/Airports/simple.cxx index cad2e4671..ab3b47baf 100644 --- a/src/Airports/simple.cxx +++ b/src/Airports/simple.cxx @@ -1,7 +1,7 @@ // // simple.cxx -- a really simplistic class to manage airport ID, -// lat, lon of the center of one of it's runways, and -// elevation in feet. +// lat, lon of the center of one of it's runways, and +// elevation in feet. // // Written by Curtis Olson, started April 1998. // @@ -23,230 +23,80 @@ // // $Id$ - #ifdef HAVE_CONFIG_H # include #endif -#include // for gdbm open flags -#include // for gdbm open flags - -#ifdef HAVE_GDBM -# include -#else -# include -#endif - #include #include -#include - -#include
+#include #include STL_STRING -#include STL_FUNCTIONAL -#include STL_ALGORITHM +#include STL_IOSTREAM #include "simple.hxx" - -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; - } -} +SG_USING_NAMESPACE(std); +SG_USING_STD(istream); -// search for the specified id -bool -FGAirports::search( const string& id, FGAirport* a ) const +inline istream& +operator >> ( istream& in, FGAirport& a ) { - 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 ); -} + string junk; + in >> junk >> a.id >> a.latitude >> a.longitude >> a.elevation + >> a.code; + char name[256]; // should never be longer than this, right? :-) + in.getline( name, 256 ); + a.name = name; -// Constructor -FGAirportsUtil::FGAirportsUtil() { + return in; } -// load the data -int FGAirportsUtil::load( const string& file ) { - FGAirport a; - - airports.erase( airports.begin(), airports.end() ); +FGAirportList::FGAirportList( const string& file ) { + SG_LOG( SG_GENERAL, SG_DEBUG, "Reading simple airport list: " << file ); - fg_gzifstream in( file ); + // open the specified file for reading + sg_gzifstream in( file ); if ( !in.is_open() ) { - FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file ); + SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file ); exit(-1); } - /* - // We can use the STL copy algorithm because the input - // file doesn't contain and comments or blank lines. - copy( istream_iterator(in.stream()), - istream_iterator(), - inserter( airports, airports.begin() ) ); - */ - - // read in each line of the file - -#ifdef __MWERKS__ - - in >> skipcomment; - char c = 0; - while ( in.get(c) && c != '\0' ) { - in.putback(c); - in >> a; - airports.insert(a); - in >> skipcomment; - } + // skip header line + in >> skipeol; -#else - - in >> skipcomment; - while ( ! in.eof() ) { - in >> a; - airports.insert(a); - in >> skipcomment; + FGAirport a; + while ( in ) { + in >> a; + airports[a.id] = a; + airports2.push_back(&airports[a.id]); } -#endif - - return 1; } -// save the data in gdbm format -bool FGAirportsUtil::dump_gdbm( const string& file ) { - - GDBM_FILE dbf; - -#if !defined( MACOS ) - dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_NEWDB | GDBM_FAST, - S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, - NULL ); -#else - dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_NEWDB | GDBM_FAST, - NULL, NULL ); -#endif - - 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; +// search for the specified id +FGAirport FGAirportList::search( const string& id) { + return airports[id]; } -// search for the specified id -bool -FGAirportsUtil::search( const string& id, FGAirport* a ) const -{ - const_iterator it = airports.find( FGAirport(id) ); - if ( it != airports.end() ) - { - *a = *it; - return true; - } - else - { - return false; - } +// Destructor +FGAirportList::~FGAirportList( void ) { } - -FGAirport -FGAirportsUtil::search( const string& id ) const +int +FGAirportList::size () const { - FGAirport a; - this->search( id, &a ); - return a; + return airports2.size(); } - -// Destructor -FGAirportsUtil::~FGAirportsUtil( void ) { +const FGAirport * +FGAirportList::getAirport (int index) const +{ + return airports2[index]; } - -