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.
31 // #include <sys/types.h> // for gdbm open flags
32 // #include <sys/stat.h> // for gdbm open flags
37 // # include <simgear/gdbm/gdbm.h>
40 #include <simgear/compiler.h>
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/misc/fgstream.hxx>
46 #include STL_FUNCTIONAL
47 #include STL_ALGORITHM
51 FG_USING_NAMESPACE(std);
53 FGAirports::FGAirports( const string& file ) {
54 // open the specified database readonly
55 storage = new c4_Storage( file.c_str(), false );
57 if ( !storage->Strategy().IsValid() ) {
58 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
62 vAirport = new c4_View;
64 storage->GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
68 // search for the specified id
70 FGAirports::search( const string& id, FGAirport* a ) const
72 c4_StringProp pID ("ID");
73 c4_FloatProp pLon ("Longitude");
74 c4_FloatProp pLat ("Latitude");
75 c4_FloatProp pElev ("Elevation");
77 int idx = vAirport->Find(pID[id.c_str()]);
78 cout << "idx = " << idx << endl;
84 c4_RowRef r = vAirport->GetAt(idx);
86 a->longitude = (double) pLon(r);
87 a->latitude = (double) pLat(r);
88 a->elevation = (double) pElev(r);
95 FGAirports::search( const string& id ) const
104 FGAirports::~FGAirports( void ) {
110 FGAirportsUtil::FGAirportsUtil() {
115 int FGAirportsUtil::load( const string& file ) {
118 airports.erase( airports.begin(), airports.end() );
120 fg_gzifstream in( file );
121 if ( !in.is_open() ) {
122 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
126 // skip first line of file
128 in.getline( tmp, 256 );
131 // read in each line of the file
137 while ( in.get(c) && c != '\0' ) {
142 } else if ( c == 'R' ) {
153 while ( ! in.eof() ) {
158 cout << "in <- " << a.id << endl;
161 } else if ( c == 'R' ) {
175 // save the data in gdbm format
176 bool FGAirportsUtil::dump_mk4( const string& file ) {
178 // open database for writing
179 c4_Storage storage( file.c_str(), true );
181 // need to do something about error handling here!
183 // define the properties
184 c4_StringProp pID ("ID");
185 c4_FloatProp pLon ("Longitude");
186 c4_FloatProp pLat ("Latitude");
187 c4_FloatProp pElev ("Elevation");
189 // Start with an empty view of the proper structure.
191 storage.GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
195 const_iterator current = airports.begin();
196 const_iterator end = airports.end();
197 while ( current != end ) {
198 // add each airport record
199 cout << "out -> " << current->id << endl;
200 pID (row) = current->id.c_str();
201 pLon (row) = current->longitude;
202 pLat (row) = current->latitude;
203 pElev (row) = current->elevation;
209 // commit our changes
216 // search for the specified id
218 FGAirportsUtil::search( const string& id, FGAirport* a ) const
220 const_iterator it = airports.find( FGAirport(id) );
221 if ( it != airports.end() )
234 FGAirportsUtil::search( const string& id ) const
237 this->search( id, &a );
243 FGAirportsUtil::~FGAirportsUtil( void ) {