]> git.mxchange.org Git - flightgear.git/blob - src/Airports/runways.cxx
Fixes for MSVC++ issues.
[flightgear.git] / src / Airports / runways.cxx
1 // runways.hxx -- a simple class to manage airport runway info
2 //
3 // Written by Curtis Olson, started August 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 // #include <sys/types.h>               // for gdbm open flags
29 // #include <sys/stat.h>                // for gdbm open flags
30
31 #include <simgear/compiler.h>
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/misc/fgstream.hxx>
35
36 #include <Main/options.hxx>
37
38 #include STL_STRING
39 #include STL_FUNCTIONAL
40 #include STL_ALGORITHM
41
42 #include "runways.hxx"
43
44 FG_USING_NAMESPACE(std);
45
46
47 FGRunway::FGRunway() {
48 }
49
50
51 FGRunway::~FGRunway() {
52 }
53
54
55 FGRunways::FGRunways( const string& file ) {
56     // open the specified database readonly
57     storage = new c4_Storage( file.c_str(), false );
58
59     if ( !storage->Strategy().IsValid() ) {
60         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
61         exit(-1);
62     }
63
64     vRunway = new c4_View;
65     *vRunway = 
66         storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
67
68     next_index = 0;
69 }
70
71
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");
84
85     int index = vRunway->Find(pID[id.c_str()]);
86     cout << "index = " << index << endl;
87
88     if ( index == -1 ) {
89         return false;
90     }
91
92     next_index = index + 1;
93
94     c4_RowRef row = vRunway->GetAt(index);
95
96     r->id =      (const char *) pID(row);
97     r->rwy_no =  (const char *) 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 = (const char *) pSurf(row);
104     r->end1_flags =    (const char *) pEnd1(row);
105     r->end2_flags =    (const char *) pEnd2(row);
106
107     return true;
108 }
109
110
111 FGRunway FGRunways::search( const string& id ) {
112     FGRunway a;
113     search( id, &a );
114     return a;
115 }
116
117
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");
130
131     int size = vRunway->GetSize();
132     // cout << "total records = " << size << endl;
133
134     int index = next_index;
135     // cout << "index = " << index << endl;
136
137     if ( index == -1 || index >= size ) {
138         return false;
139     }
140
141     next_index = index + 1;
142
143     c4_RowRef row = vRunway->GetAt(index);
144
145     r->id =      (const char *) pID(row);
146     r->rwy_no =  (const char *) 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 = (const char *) pSurf(row);
153     r->end1_flags =    (const char *) pEnd1(row);
154     r->end2_flags =    (const char *) pEnd2(row);
155
156     return true;
157 }
158
159
160 // Destructor
161 FGRunways::~FGRunways( void ) {
162     // gdbm_close( dbf );
163 }
164
165
166 // Constructor
167 FGRunwaysUtil::FGRunwaysUtil() {
168 }
169
170
171 // load the data
172 int FGRunwaysUtil::load( const string& file ) {
173     FGRunway r;
174     string apt_id;
175
176     runways.erase( runways.begin(), runways.end() );
177
178     fg_gzifstream in( file );
179     if ( !in.is_open() ) {
180         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
181         exit(-1);
182     }
183
184     // skip first line of file
185     char tmp[256];
186     in.getline( tmp, 256 );
187
188     // read in each line of the file
189
190 #ifdef __MWERKS__
191
192     in >> ::skipws;
193     char c = 0;
194     while ( in.get(c) && c != '\0' ) {
195         if ( c == 'A' ) {
196             in >> apt_id;
197             in >> skipeol;
198         } else if ( c == 'R' ) {
199             in >> r;
200             r.id = apt_id;
201             runways.push_back(r);
202         } else {
203             in >> skipeol;
204         }
205         in >> ::skipws;
206     }
207
208 #else
209
210     in >> ::skipws;
211     while ( ! in.eof() ) {
212         char c = 0;
213         in.get(c);
214         if ( c == 'A' ) {
215             in >> apt_id;
216             in >> skipeol;
217         } else if ( c == 'R' ) {
218             in >> r;
219             r.id = apt_id;
220             // cout << apt_id << " " << r.rwy_no << endl;
221             runways.push_back(r);
222         } else {
223             in >> skipeol;
224         }
225         in >> ::skipws;
226     }
227
228 #endif
229
230     return 1;
231 }
232
233
234 // save the data in gdbm format
235 bool FGRunwaysUtil::dump_mk4( const string& file ) {
236
237     // open database for writing
238     c4_Storage storage( file.c_str(), true );
239
240     // need to do something about error handling here!
241
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");
253
254     // Start with an empty view of the proper structure.
255     c4_View vRunway =
256         storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
257
258     c4_Row row;
259
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();
274         vRunway.Add(row);
275
276         ++current;
277     }
278
279     // commit our changes
280     storage.Commit();
281
282     return true;
283 }
284
285
286 #if 0
287 // search for the specified id
288 bool
289 FGRunwaysUtil::search( const string& id, FGRunway* a ) const
290 {
291     const_iterator it = runways.find( FGRunway(id) );
292     if ( it != runways.end() )
293     {
294         *a = *it;
295         return true;
296     }
297     else
298     {
299         return false;
300     }
301 }
302
303
304 FGRunway
305 FGRunwaysUtil::search( const string& id ) const
306 {
307     FGRunway a;
308     this->search( id, &a );
309     return a;
310 }
311 #endif
312
313 // Destructor
314 FGRunwaysUtil::~FGRunwaysUtil( void ) {
315 }
316
317