]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
27d9c9baf720e60a13661006fc88cd8a966cf422
[flightgear.git] / src / Airports / simple.cxx
1 //
2 // simple.cxx -- a really simplistic class to manage airport ID,
3 //                 lat, lon of the center of one of it's runways, and 
4 //                 elevation in feet.
5 //
6 // Written by Curtis Olson, started April 1998.
7 //
8 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
9 //
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.
14 //
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.
19 //
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.
23 //
24 // $Id$
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 // #include <sys/types.h>               // for gdbm open flags
32 // #include <sys/stat.h>                // for gdbm open flags
33
34 // #ifdef HAVE_GDBM
35 // #  include <gdbm.h>
36 // #else
37 // #  include <simgear/gdbm/gdbm.h>
38 // #endif
39
40 #include <simgear/compiler.h>
41
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/misc/fgstream.hxx>
44
45 #include <Main/options.hxx>
46
47 #include STL_STRING
48 #include STL_FUNCTIONAL
49 #include STL_ALGORITHM
50
51 #include "simple.hxx"
52
53 FG_USING_NAMESPACE(std);
54
55 FGAirports::FGAirports( 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     vAirport = new c4_View;
65     *vAirport = 
66         storage->GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
67 }
68
69
70 // search for the specified id
71 bool
72 FGAirports::search( const string& id, FGAirport* a ) const
73 {
74     c4_StringProp pID ("ID");
75     c4_FloatProp pLon ("Longitude");
76     c4_FloatProp pLat ("Latitude");
77     c4_FloatProp pElev ("Elevation");
78
79     int idx = vAirport->Find(pID[id.c_str()]);
80     cout << "idx = " << idx << endl;
81
82     if ( idx == -1 ) {
83         return false;
84     }
85
86     c4_RowRef r = vAirport->GetAt(idx);
87
88     a->longitude = (double) pLon(r);
89     a->latitude =  (double) pLat(r);
90     a->elevation = (double) pElev(r);
91
92     return true;
93 }
94
95
96 FGAirport
97 FGAirports::search( const string& id ) const
98 {
99     FGAirport a;
100     search( id, &a );
101     return a;
102 }
103
104
105 // Destructor
106 FGAirports::~FGAirports( void ) {
107     // gdbm_close( dbf );
108 }
109
110
111 // Constructor
112 FGAirportsUtil::FGAirportsUtil() {
113 }
114
115
116 // load the data
117 int FGAirportsUtil::load( const string& file ) {
118     FGAirport a;
119
120     airports.erase( airports.begin(), airports.end() );
121
122     fg_gzifstream in( file );
123     if ( !in.is_open() ) {
124         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
125         exit(-1);
126     }
127
128     // skip first line of file
129     char tmp[256];
130     in.getline( tmp, 256 );
131
132
133     // read in each line of the file
134
135 #ifdef __MWERKS__
136
137     in >> ::skipws;
138     char c = 0;
139     while ( in.get(c) && c != '\0' ) {
140         if ( c == 'A' ) {
141             in >> a;
142             in >> skipeol;
143             airports.insert(a);
144         } else if ( c == 'R' ) {
145             in >> skipeol;
146         } else {
147             in >> skipeol;
148         }
149         in >> ::skipws;
150     }
151
152 #else
153
154     in >> ::skipws;
155     while ( ! in.eof() ) {
156         char c = 0;
157         in.get(c);
158         if ( c == 'A' ) {
159             in >> a;
160             cout << "in <- " << a.id << endl;
161             in >> skipeol;
162             airports.insert(a);
163         } else if ( c == 'R' ) {
164             in >> skipeol;
165         } else {
166             in >> skipeol;
167         }
168         in >> ::skipws;
169     }
170
171 #endif
172
173     return 1;
174 }
175
176
177 // save the data in gdbm format
178 bool FGAirportsUtil::dump_mk4( const string& file ) {
179
180     // open database for writing
181     c4_Storage storage( file.c_str(), true );
182
183     // need to do something about error handling here!
184
185     // define the properties
186     c4_StringProp pID ("ID");
187     c4_FloatProp pLon ("Longitude");
188     c4_FloatProp pLat ("Latitude");
189     c4_FloatProp pElev ("Elevation");
190
191     // Start with an empty view of the proper structure.
192     c4_View vAirport =
193         storage.GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
194
195     c4_Row row;
196
197     const_iterator current = airports.begin();
198     const_iterator end = airports.end();
199     while ( current != end ) {
200         // add each airport record
201         cout << "out -> " << current->id << endl;
202         pID (row) = current->id.c_str();
203         pLon (row) = current->longitude;
204         pLat (row) = current->latitude;
205         pElev (row) = current->elevation;
206         vAirport.Add(row);
207
208         ++current;
209     }
210
211     // commit our changes
212     storage.Commit();
213
214     return true;
215 }
216
217
218 // search for the specified id
219 bool
220 FGAirportsUtil::search( const string& id, FGAirport* a ) const
221 {
222     const_iterator it = airports.find( FGAirport(id) );
223     if ( it != airports.end() )
224     {
225         *a = *it;
226         return true;
227     }
228     else
229     {
230         return false;
231     }
232 }
233
234
235 FGAirport
236 FGAirportsUtil::search( const string& id ) const
237 {
238     FGAirport a;
239     this->search( id, &a );
240     return a;
241 }
242
243
244 // Destructor
245 FGAirportsUtil::~FGAirportsUtil( void ) {
246 }
247
248