1 // runways.hxx -- a simple class to manage airport runway info
3 // Written by Curtis Olson, started August 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 // #include <sys/types.h> // for gdbm open flags
29 // #include <sys/stat.h> // for gdbm open flags
31 #include <simgear/compiler.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/fgstream.hxx>
36 #include <Main/options.hxx>
39 #include STL_FUNCTIONAL
40 #include STL_ALGORITHM
42 #include "runways.hxx"
44 FG_USING_NAMESPACE(std);
47 FGRunway::FGRunway() {
51 FGRunway::~FGRunway() {
55 FGRunways::FGRunways( const string& file ) {
56 // open the specified database readonly
57 storage = new c4_Storage( file.c_str(), false );
59 if ( !storage->Strategy().IsValid() ) {
60 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
64 vRunway = new c4_View;
66 storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
72 // search for the specified id
73 bool FGRunways::search( const string& id, FGRunway* r ) {
74 c4_StringProp pID ("ID");
75 c4_StringProp pRwy ("Rwy");
76 c4_FloatProp pLon ("Longitude");
77 c4_FloatProp pLat ("Latitude");
78 c4_FloatProp pHdg ("Heading");
79 c4_FloatProp pLen ("Length");
80 c4_FloatProp pWid ("Width");
81 c4_StringProp pSurf ("SurfaceFlags");
82 c4_StringProp pEnd1 ("End1Flags");
83 c4_StringProp pEnd2 ("End2Flags");
85 int index = vRunway->Find(pID[id.c_str()]);
86 cout << "index = " << index << endl;
92 next_index = index + 1;
94 c4_RowRef row = vRunway->GetAt(index);
96 r->id = (string) pID(row);
97 r->rwy_no = (string) pRwy(row);
98 r->lon = (double) pLon(row);
99 r->lat = (double) pLat(row);
100 r->heading = (double) pHdg(row);
101 r->length = (double) pLen(row);
102 r->width = (double) pWid(row);
103 r->surface_flags = (string) pSurf(row);
104 r->end1_flags = (string) pEnd1(row);
105 r->end2_flags = (string) pEnd2(row);
111 FGRunway FGRunways::search( const string& id ) {
118 // search for the specified id
119 bool FGRunways::next( FGRunway* r ) {
120 c4_StringProp pID ("ID");
121 c4_StringProp pRwy ("Rwy");
122 c4_FloatProp pLon ("Longitude");
123 c4_FloatProp pLat ("Latitude");
124 c4_FloatProp pHdg ("Heading");
125 c4_FloatProp pLen ("Length");
126 c4_FloatProp pWid ("Width");
127 c4_StringProp pSurf ("SurfaceFlags");
128 c4_StringProp pEnd1 ("End1Flags");
129 c4_StringProp pEnd2 ("End2Flags");
131 int size = vRunway->GetSize();
132 // cout << "total records = " << size << endl;
134 int index = next_index;
135 // cout << "index = " << index << endl;
137 if ( index == -1 || index >= size ) {
141 next_index = index + 1;
143 c4_RowRef row = vRunway->GetAt(index);
145 r->id = (string) pID(row);
146 r->rwy_no = (string) pRwy(row);
147 r->lon = (double) pLon(row);
148 r->lat = (double) pLat(row);
149 r->heading = (double) pHdg(row);
150 r->length = (double) pLen(row);
151 r->width = (double) pWid(row);
152 r->surface_flags = (string) pSurf(row);
153 r->end1_flags = (string) pEnd1(row);
154 r->end2_flags = (string) pEnd2(row);
161 FGRunways::~FGRunways( void ) {
162 // gdbm_close( dbf );
167 FGRunwaysUtil::FGRunwaysUtil() {
172 int FGRunwaysUtil::load( const string& file ) {
176 runways.erase( runways.begin(), runways.end() );
178 fg_gzifstream in( file );
179 if ( !in.is_open() ) {
180 FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
184 // skip first line of file
186 in.getline( tmp, 256 );
188 // read in each line of the file
194 while ( in.get(c) && c != '\0' ) {
198 } else if ( c == 'R' ) {
201 runways.push_back(r);
211 while ( ! in.eof() ) {
217 } else if ( c == 'R' ) {
220 // cout << apt_id << " " << r.rwy_no << endl;
221 runways.push_back(r);
234 // save the data in gdbm format
235 bool FGRunwaysUtil::dump_mk4( const string& file ) {
237 // open database for writing
238 c4_Storage storage( file.c_str(), true );
240 // need to do something about error handling here!
242 // define the properties
243 c4_StringProp pID ("ID");
244 c4_StringProp pRwy ("Rwy");
245 c4_FloatProp pLon ("Longitude");
246 c4_FloatProp pLat ("Latitude");
247 c4_FloatProp pHdg ("Heading");
248 c4_FloatProp pLen ("Length");
249 c4_FloatProp pWid ("Width");
250 c4_StringProp pSurf ("SurfaceFlags");
251 c4_StringProp pEnd1 ("End1Flags");
252 c4_StringProp pEnd2 ("End2Flags");
254 // Start with an empty view of the proper structure.
256 storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
260 iterator current = runways.begin();
261 const_iterator end = runways.end();
262 while ( current != end ) {
263 // add each runway record
264 pID (row) = current->id.c_str();
265 pRwy (row) = current->rwy_no.c_str();
266 pLon (row) = current->lon;
267 pLat (row) = current->lat;
268 pHdg (row) = current->heading;
269 pLen (row) = current->length;
270 pWid (row) = current->width;
271 pSurf (row) = current->surface_flags.c_str();
272 pEnd1 (row) = current->end1_flags.c_str();
273 pEnd2 (row) = current->end2_flags.c_str();
279 // commit our changes
287 // search for the specified id
289 FGRunwaysUtil::search( const string& id, FGRunway* a ) const
291 const_iterator it = runways.find( FGRunway(id) );
292 if ( it != runways.end() )
305 FGRunwaysUtil::search( const string& id ) const
308 this->search( id, &a );
314 FGRunwaysUtil::~FGRunwaysUtil( void ) {