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>
45 #include <Main/options.hxx>
48 #include STL_FUNCTIONAL
49 #include STL_ALGORITHM
53 FG_USING_NAMESPACE(std);
55 FGAirports::FGAirports( const string& file ) {
56 // open the specified database readonly
57 storage = new c4_Storage( file.c_str(), false );
59 // need to do something about error handling here!
61 vAirport = new c4_View;
63 storage->GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
67 // search for the specified id
69 FGAirports::search( const string& id, FGAirport* a ) const
71 c4_StringProp pID ("ID");
72 c4_FloatProp pLon ("Longitude");
73 c4_FloatProp pLat ("Latitude");
74 c4_FloatProp pElev ("Elevation");
76 int idx = vAirport->Find(pID[id.c_str()]);
77 cout << "idx = " << idx << endl;
83 c4_RowRef r = vAirport->GetAt(idx);
85 a->longitude = (double) pLon(r);
86 a->latitude = (double) pLat(r);
87 a->elevation = (double) pElev(r);
94 FGAirports::search( const string& id ) const
103 FGAirports::~FGAirports( void ) {
104 // gdbm_close( dbf );
109 FGAirportsUtil::FGAirportsUtil() {
114 int FGAirportsUtil::load( const string& file ) {
117 airports.erase( airports.begin(), airports.end() );
119 fg_gzifstream in( file );
120 if ( !in.is_open() ) {
121 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
126 // We can use the STL copy algorithm because the input
127 // file doesn't contain and comments or blank lines.
128 copy( istream_iterator<FGAirport,ptrdiff_t>(in.stream()),
129 istream_iterator<FGAirport,ptrdiff_t>(),
130 inserter( airports, airports.begin() ) );
133 // read in each line of the file
139 while ( in.get(c) && c != '\0' ) {
149 while ( ! in.eof() ) {
161 // save the data in gdbm format
162 bool FGAirportsUtil::dump_mk4( const string& file ) {
164 // open database for writing
165 c4_Storage storage( file.c_str(), true );
167 // need to do something about error handling here!
169 // define the properties
170 c4_StringProp pID ("ID");
171 c4_FloatProp pLon ("Longitude");
172 c4_FloatProp pLat ("Latitude");
173 c4_FloatProp pElev ("Elevation");
175 // Start with an empty view of the proper structure.
177 storage.GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
181 iterator current = airports.begin();
182 const_iterator end = airports.end();
183 while ( current != end ) {
184 // add each airport record
185 pID (row) = current->id.c_str();
186 pLon (row) = current->longitude;
187 pLat (row) = current->latitude;
188 pElev (row) = current->elevation;
194 // commit our changes
201 // search for the specified id
203 FGAirportsUtil::search( const string& id, FGAirport* a ) const
205 const_iterator it = airports.find( FGAirport(id) );
206 if ( it != airports.end() )
219 FGAirportsUtil::search( const string& id ) const
222 this->search( id, &a );
228 FGAirportsUtil::~FGAirportsUtil( void ) {