X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FAirports%2Fsimple.cxx;h=ab3b47baf97de74471856feaf2eb147b50c9ddba;hb=d14bba8458d6b1276b33467610d3dbc5a3f8d939;hp=583e54e001551a9c8073358017b190ef305f3ceb;hpb=2653a8dc7e873ac4b865f0d986309b4e1b3208f3;p=flightgear.git diff --git a/src/Airports/simple.cxx b/src/Airports/simple.cxx index 583e54e00..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,226 +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 STL_STRING -#include STL_FUNCTIONAL -#include STL_ALGORITHM +#include STL_IOSTREAM #include "simple.hxx" SG_USING_NAMESPACE(std); - -FGAirports::FGAirports( const string& file ) { - // open the specified database readonly - storage = new c4_Storage( file.c_str(), false ); - - if ( !storage->Strategy().IsValid() ) { - SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file ); - exit(-1); - } - - vAirport = new c4_View; - *vAirport = - storage->GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]"); -} +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 ) { - c4_StringProp pID ("ID"); - c4_FloatProp pLon ("Longitude"); - c4_FloatProp pLat ("Latitude"); - c4_FloatProp pElev ("Elevation"); + string junk; + in >> junk >> a.id >> a.latitude >> a.longitude >> a.elevation + >> a.code; - int idx = vAirport->Find(pID[id.c_str()]); - cout << "idx = " << idx << endl; + char name[256]; // should never be longer than this, right? :-) + in.getline( name, 256 ); + a.name = name; - if ( idx == -1 ) { - return false; - } - - c4_RowRef r = vAirport->GetAt(idx); - a->id = (const char *) pID(r); /// NHV fix wrong case crash - a->longitude = (double) pLon(r); - a->latitude = (double) pLat(r); - a->elevation = (double) pElev(r); - - return true; + return in; } -FGAirport -FGAirports::search( const string& id ) const -{ - FGAirport a; - search( id, &a ); - return a; -} - - -// Destructor -FGAirports::~FGAirports( void ) { - delete storage; -} - - -// Constructor -FGAirportsUtil::FGAirportsUtil() { -} - - -// 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 ); + // open the specified file for reading sg_gzifstream in( file ); if ( !in.is_open() ) { - SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file ); + SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file ); exit(-1); - } else { - SG_LOG( SG_GENERAL, SG_ALERT, "opened: " << file ); } - // skip first line of file - char tmp[2048]; - in.getline( tmp, 2048 ); - - // read in each line of the file - -#ifdef __MWERKS__ - - in >> ::skipws; - char c = 0; - while ( in.get(c) && c != '\0' ) { - if ( c == 'A' ) { - in >> a; - SG_LOG( SG_GENERAL, SG_INFO, a.id ); - in >> skipeol; - airports.insert(a); - } else if ( c == 'R' ) { - in >> skipeol; - } else { - in >> skipeol; - } - in >> ::skipws; - } + // skip header line + in >> skipeol; -#else - - in >> ::skipws; - string token; - while ( ! in.eof() ) { - in >> token; - if ( token == "A" ) { - in >> a; - SG_LOG( SG_GENERAL, SG_INFO, "in <- " << a.id ); - in >> skipeol; - airports.insert(a); - } else if ( token == "R" ) { - in >> skipeol; - } else { - in >> skipeol; - } - in >> ::skipws; + 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_mk4( const string& file ) { - - // open database for writing - c4_Storage storage( file.c_str(), true ); - - // need to do something about error handling here! - - // 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; - - const_iterator current = airports.begin(); - const_iterator end = airports.end(); - while ( current != end ) { - // add each airport record - cout << "out -> " << current->id << endl; - pID (row) = current->id.c_str(); - pLon (row) = current->longitude; - pLat (row) = current->latitude; - pElev (row) = current->elevation; - vAirport.Add(row); - - ++current; - } - - // commit our changes - storage.Commit(); - - 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]; } - -