]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
- adjusted for no-value constructor for FGPanel
[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/sgstream.hxx>
44
45 #include STL_STRING
46 #include STL_FUNCTIONAL
47 #include STL_ALGORITHM
48
49 #include "simple.hxx"
50
51 SG_USING_NAMESPACE(std);
52
53 FGAirports::FGAirports( 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     vAirport = new c4_View;
63     *vAirport = 
64         storage->GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
65 }
66
67
68 // search for the specified id
69 bool
70 FGAirports::search( const string& id, FGAirport* a ) const
71 {
72     c4_StringProp pID ("ID");
73     c4_FloatProp pLon ("Longitude");
74     c4_FloatProp pLat ("Latitude");
75     c4_FloatProp pElev ("Elevation");
76
77     int idx = vAirport->Find(pID[id.c_str()]);
78     cout << "idx = " << idx << endl;
79
80     if ( idx == -1 ) {
81         return false;
82     }
83
84     c4_RowRef r = vAirport->GetAt(idx);
85
86     a->longitude = (double) pLon(r);
87     a->latitude =  (double) pLat(r);
88     a->elevation = (double) pElev(r);
89
90     return true;
91 }
92
93
94 FGAirport
95 FGAirports::search( const string& id ) const
96 {
97     FGAirport a;
98     search( id, &a );
99     return a;
100 }
101
102
103 // Destructor
104 FGAirports::~FGAirports( void ) {
105     delete storage;
106 }
107
108
109 // Constructor
110 FGAirportsUtil::FGAirportsUtil() {
111 }
112
113
114 // load the data
115 int FGAirportsUtil::load( const string& file ) {
116     FGAirport a;
117
118     airports.erase( airports.begin(), airports.end() );
119
120     sg_gzifstream in( file );
121     if ( !in.is_open() ) {
122         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
123         exit(-1);
124     }
125
126     // skip first line of file
127     char tmp[256];
128     in.getline( tmp, 256 );
129
130
131     // read in each line of the file
132
133 #ifdef __MWERKS__
134
135     in >> ::skipws;
136     char c = 0;
137     while ( in.get(c) && c != '\0' ) {
138         if ( c == 'A' ) {
139             in >> a;
140             in >> skipeol;
141             airports.insert(a);
142         } else if ( c == 'R' ) {
143             in >> skipeol;
144         } else {
145             in >> skipeol;
146         }
147         in >> ::skipws;
148     }
149
150 #else
151
152     in >> ::skipws;
153     while ( ! in.eof() ) {
154         char c = 0;
155         in.get(c);
156         if ( c == 'A' ) {
157             in >> a;
158             cout << "in <- " << a.id << endl;
159             in >> skipeol;
160             airports.insert(a);
161         } else if ( c == 'R' ) {
162             in >> skipeol;
163         } else {
164             in >> skipeol;
165         }
166         in >> ::skipws;
167     }
168
169 #endif
170
171     return 1;
172 }
173
174
175 // save the data in gdbm format
176 bool FGAirportsUtil::dump_mk4( const string& file ) {
177
178     // open database for writing
179     c4_Storage storage( file.c_str(), true );
180
181     // need to do something about error handling here!
182
183     // define the properties
184     c4_StringProp pID ("ID");
185     c4_FloatProp pLon ("Longitude");
186     c4_FloatProp pLat ("Latitude");
187     c4_FloatProp pElev ("Elevation");
188
189     // Start with an empty view of the proper structure.
190     c4_View vAirport =
191         storage.GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
192
193     c4_Row row;
194
195     const_iterator current = airports.begin();
196     const_iterator end = airports.end();
197     while ( current != end ) {
198         // add each airport record
199         cout << "out -> " << current->id << endl;
200         pID (row) = current->id.c_str();
201         pLon (row) = current->longitude;
202         pLat (row) = current->latitude;
203         pElev (row) = current->elevation;
204         vAirport.Add(row);
205
206         ++current;
207     }
208
209     // commit our changes
210     storage.Commit();
211
212     return true;
213 }
214
215
216 // search for the specified id
217 bool
218 FGAirportsUtil::search( const string& id, FGAirport* a ) const
219 {
220     const_iterator it = airports.find( FGAirport(id) );
221     if ( it != airports.end() )
222     {
223         *a = *it;
224         return true;
225     }
226     else
227     {
228         return false;
229     }
230 }
231
232
233 FGAirport
234 FGAirportsUtil::search( const string& id ) const
235 {
236     FGAirport a;
237     this->search( id, &a );
238     return a;
239 }
240
241
242 // Destructor
243 FGAirportsUtil::~FGAirportsUtil( void ) {
244 }
245
246