]> git.mxchange.org Git - flightgear.git/blob - src/Airports/runways.cxx
SG-ified logstream.
[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 STL_STRING
37 #include STL_FUNCTIONAL
38 #include STL_ALGORITHM
39
40 #include "runways.hxx"
41
42 SG_USING_NAMESPACE(std);
43
44
45 FGRunway::FGRunway() {
46 }
47
48
49 FGRunway::~FGRunway() {
50 }
51
52
53 FGRunways::FGRunways( const string& file ) {
54     // open the specified database readonly
55     storage = new c4_Storage( file.c_str(), false );
56
57     if ( !storage->Strategy().IsValid() ) {
58         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
59         exit(-1);
60     }
61
62     vRunway = new c4_View;
63     *vRunway = 
64         storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
65
66     next_index = 0;
67 }
68
69
70 // search for the specified id
71 bool FGRunways::search( const string& id, FGRunway* r ) {
72     c4_StringProp pID ("ID");
73     c4_StringProp pRwy ("Rwy");
74     c4_FloatProp pLon ("Longitude");
75     c4_FloatProp pLat ("Latitude");
76     c4_FloatProp pHdg ("Heading");
77     c4_FloatProp pLen ("Length");
78     c4_FloatProp pWid ("Width");
79     c4_StringProp pSurf ("SurfaceFlags");
80     c4_StringProp pEnd1 ("End1Flags");
81     c4_StringProp pEnd2 ("End2Flags");
82
83     int index = vRunway->Find(pID[id.c_str()]);
84     cout << "index = " << index << endl;
85
86     if ( index == -1 ) {
87         return false;
88     }
89
90     next_index = index + 1;
91
92     c4_RowRef row = vRunway->GetAt(index);
93
94     r->id =      (const char *) pID(row);
95     r->rwy_no =  (const char *) pRwy(row);
96     r->lon =     (double) pLon(row);
97     r->lat =     (double) pLat(row);
98     r->heading = (double) pHdg(row);
99     r->length =  (double) pLen(row);
100     r->width =   (double) pWid(row);
101     r->surface_flags = (const char *) pSurf(row);
102     r->end1_flags =    (const char *) pEnd1(row);
103     r->end2_flags =    (const char *) pEnd2(row);
104
105     return true;
106 }
107
108
109 FGRunway FGRunways::search( const string& id ) {
110     FGRunway a;
111     search( id, &a );
112     return a;
113 }
114
115
116 // search for the specified id
117 bool FGRunways::next( FGRunway* r ) {
118     c4_StringProp pID ("ID");
119     c4_StringProp pRwy ("Rwy");
120     c4_FloatProp pLon ("Longitude");
121     c4_FloatProp pLat ("Latitude");
122     c4_FloatProp pHdg ("Heading");
123     c4_FloatProp pLen ("Length");
124     c4_FloatProp pWid ("Width");
125     c4_StringProp pSurf ("SurfaceFlags");
126     c4_StringProp pEnd1 ("End1Flags");
127     c4_StringProp pEnd2 ("End2Flags");
128
129     int size = vRunway->GetSize();
130     // cout << "total records = " << size << endl;
131
132     int index = next_index;
133     // cout << "index = " << index << endl;
134
135     if ( index == -1 || index >= size ) {
136         return false;
137     }
138
139     next_index = index + 1;
140
141     c4_RowRef row = vRunway->GetAt(index);
142
143     r->id =      (const char *) pID(row);
144     r->rwy_no =  (const char *) pRwy(row);
145     r->lon =     (double) pLon(row);
146     r->lat =     (double) pLat(row);
147     r->heading = (double) pHdg(row);
148     r->length =  (double) pLen(row);
149     r->width =   (double) pWid(row);
150     r->surface_flags = (const char *) pSurf(row);
151     r->end1_flags =    (const char *) pEnd1(row);
152     r->end2_flags =    (const char *) pEnd2(row);
153
154     return true;
155 }
156
157
158 // Destructor
159 FGRunways::~FGRunways( void ) {
160     delete storage;
161 }
162
163
164 // Constructor
165 FGRunwaysUtil::FGRunwaysUtil() {
166 }
167
168
169 // load the data
170 int FGRunwaysUtil::load( const string& file ) {
171     FGRunway r;
172     string apt_id;
173
174     runways.erase( runways.begin(), runways.end() );
175
176     fg_gzifstream in( file );
177     if ( !in.is_open() ) {
178         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
179         exit(-1);
180     }
181
182     // skip first line of file
183     char tmp[256];
184     in.getline( tmp, 256 );
185
186     // read in each line of the file
187
188 #ifdef __MWERKS__
189
190     in >> ::skipws;
191     char c = 0;
192     while ( in.get(c) && c != '\0' ) {
193         if ( c == 'A' ) {
194             in >> apt_id;
195             in >> skipeol;
196         } else if ( c == 'R' ) {
197             in >> r;
198             r.id = apt_id;
199             runways.push_back(r);
200         } else {
201             in >> skipeol;
202         }
203         in >> ::skipws;
204     }
205
206 #else
207
208     in >> ::skipws;
209     while ( ! in.eof() ) {
210         char c = 0;
211         in.get(c);
212         if ( c == 'A' ) {
213             in >> apt_id;
214             in >> skipeol;
215         } else if ( c == 'R' ) {
216             in >> r;
217             r.id = apt_id;
218             // cout << apt_id << " " << r.rwy_no << endl;
219             runways.push_back(r);
220         } else {
221             in >> skipeol;
222         }
223         in >> ::skipws;
224     }
225
226 #endif
227
228     return 1;
229 }
230
231
232 // save the data in gdbm format
233 bool FGRunwaysUtil::dump_mk4( const string& file ) {
234
235     // open database for writing
236     c4_Storage storage( file.c_str(), true );
237
238     // need to do something about error handling here!
239
240     // define the properties
241     c4_StringProp pID ("ID");
242     c4_StringProp pRwy ("Rwy");
243     c4_FloatProp pLon ("Longitude");
244     c4_FloatProp pLat ("Latitude");
245     c4_FloatProp pHdg ("Heading");
246     c4_FloatProp pLen ("Length");
247     c4_FloatProp pWid ("Width");
248     c4_StringProp pSurf ("SurfaceFlags");
249     c4_StringProp pEnd1 ("End1Flags");
250     c4_StringProp pEnd2 ("End2Flags");
251
252     // Start with an empty view of the proper structure.
253     c4_View vRunway =
254         storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
255
256     c4_Row row;
257
258     iterator current = runways.begin();
259     const_iterator end = runways.end();
260     while ( current != end ) {
261         // add each runway record
262         pID (row) = current->id.c_str();
263         pRwy (row) = current->rwy_no.c_str();
264         pLon (row) = current->lon;
265         pLat (row) = current->lat;
266         pHdg (row) = current->heading;
267         pLen (row) = current->length;
268         pWid (row) = current->width;
269         pSurf (row) = current->surface_flags.c_str();
270         pEnd1 (row) = current->end1_flags.c_str();
271         pEnd2 (row) = current->end2_flags.c_str();
272         vRunway.Add(row);
273
274         ++current;
275     }
276
277     // commit our changes
278     storage.Commit();
279
280     return true;
281 }
282
283
284 #if 0
285 // search for the specified id
286 bool
287 FGRunwaysUtil::search( const string& id, FGRunway* a ) const
288 {
289     const_iterator it = runways.find( FGRunway(id) );
290     if ( it != runways.end() )
291     {
292         *a = *it;
293         return true;
294     }
295     else
296     {
297         return false;
298     }
299 }
300
301
302 FGRunway
303 FGRunwaysUtil::search( const string& id ) const
304 {
305     FGRunway a;
306     this->search( id, &a );
307     return a;
308 }
309 #endif
310
311 // Destructor
312 FGRunwaysUtil::~FGRunwaysUtil( void ) {
313 }
314
315