]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
First quick hack at panel shading.
[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     /*
129     // We can use the STL copy algorithm because the input
130     // file doesn't contain and comments or blank lines.
131     copy( istream_iterator<FGAirport,ptrdiff_t>(in.stream()),
132           istream_iterator<FGAirport,ptrdiff_t>(),
133           inserter( airports, airports.begin() ) );
134     */
135
136     // read in each line of the file
137
138 #ifdef __MWERKS__
139
140     in >> skipcomment;
141     char c = 0;
142     while ( in.get(c) && c != '\0' ) {
143         in.putback(c);
144         in >> a;
145         airports.insert(a);
146         in >> skipcomment;
147     }
148
149 #else
150
151     in >> skipcomment;
152     while ( ! in.eof() ) {
153         in >> a;
154         airports.insert(a);
155         in >> skipcomment;
156     }
157
158 #endif
159
160     return 1;
161 }
162
163
164 // save the data in gdbm format
165 bool FGAirportsUtil::dump_mk4( const string& file ) {
166
167     // open database for writing
168     c4_Storage storage( file.c_str(), true );
169
170     // need to do something about error handling here!
171
172     // define the properties
173     c4_StringProp pID ("ID");
174     c4_FloatProp pLon ("Longitude");
175     c4_FloatProp pLat ("Latitude");
176     c4_FloatProp pElev ("Elevation");
177
178     // Start with an empty view of the proper structure.
179     c4_View vAirport =
180         storage.GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
181
182     c4_Row row;
183
184     iterator current = airports.begin();
185     const_iterator end = airports.end();
186     while ( current != end ) {
187         // add each airport record
188         pID (row) = current->id.c_str();
189         pLon (row) = current->longitude;
190         pLat (row) = current->latitude;
191         pElev (row) = current->elevation;
192         vAirport.Add(row);
193
194         ++current;
195     }
196
197     // commit our changes
198     storage.Commit();
199
200     return true;
201 }
202
203
204 // search for the specified id
205 bool
206 FGAirportsUtil::search( const string& id, FGAirport* a ) const
207 {
208     const_iterator it = airports.find( FGAirport(id) );
209     if ( it != airports.end() )
210     {
211         *a = *it;
212         return true;
213     }
214     else
215     {
216         return false;
217     }
218 }
219
220
221 FGAirport
222 FGAirportsUtil::search( const string& id ) const
223 {
224     FGAirport a;
225     this->search( id, &a );
226     return a;
227 }
228
229
230 // Destructor
231 FGAirportsUtil::~FGAirportsUtil( void ) {
232 }
233
234