]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
Replaced gdbm with metakit. Involves a new simgear version and a new database
[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     // need to do something about error handling here!
60
61     vAirport = new c4_View;
62     *vAirport = 
63         storage->GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
64 }
65
66
67 // search for the specified id
68 bool
69 FGAirports::search( const string& id, FGAirport* a ) const
70 {
71     c4_StringProp pID ("ID");
72     c4_FloatProp pLon ("Longitude");
73     c4_FloatProp pLat ("Latitude");
74     c4_FloatProp pElev ("Elevation");
75
76     int idx = vAirport->Find(pID[id.c_str()]);
77     cout << "idx = " << idx << endl;
78
79     if ( idx == -1 ) {
80         return false;
81     }
82
83     c4_RowRef r = vAirport->GetAt(idx);
84
85     a->longitude = (double) pLon(r);
86     a->latitude =  (double) pLat(r);
87     a->elevation = (double) pElev(r);
88
89     return true;
90 }
91
92
93 FGAirport
94 FGAirports::search( const string& id ) const
95 {
96     FGAirport a;
97     search( id, &a );
98     return a;
99 }
100
101
102 // Destructor
103 FGAirports::~FGAirports( void ) {
104     // gdbm_close( dbf );
105 }
106
107
108 // Constructor
109 FGAirportsUtil::FGAirportsUtil() {
110 }
111
112
113 // load the data
114 int FGAirportsUtil::load( const string& file ) {
115     FGAirport a;
116
117     airports.erase( airports.begin(), airports.end() );
118
119     fg_gzifstream in( file );
120     if ( !in.is_open() ) {
121         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
122         exit(-1);
123     }
124
125     /*
126     // We can use the STL copy algorithm because the input
127     // file doesn't contain and comments or blank lines.
128     copy( istream_iterator<FGAirport,ptrdiff_t>(in.stream()),
129           istream_iterator<FGAirport,ptrdiff_t>(),
130           inserter( airports, airports.begin() ) );
131     */
132
133     // read in each line of the file
134
135 #ifdef __MWERKS__
136
137     in >> skipcomment;
138     char c = 0;
139     while ( in.get(c) && c != '\0' ) {
140         in.putback(c);
141         in >> a;
142         airports.insert(a);
143         in >> skipcomment;
144     }
145
146 #else
147
148     in >> skipcomment;
149     while ( ! in.eof() ) {
150         in >> a;
151         airports.insert(a);
152         in >> skipcomment;
153     }
154
155 #endif
156
157     return 1;
158 }
159
160
161 // save the data in gdbm format
162 bool FGAirportsUtil::dump_mk4( const string& file ) {
163
164     // open database for writing
165     c4_Storage storage( file.c_str(), true );
166
167     // need to do something about error handling here!
168
169     // define the properties
170     c4_StringProp pID ("ID");
171     c4_FloatProp pLon ("Longitude");
172     c4_FloatProp pLat ("Latitude");
173     c4_FloatProp pElev ("Elevation");
174
175     // Start with an empty view of the proper structure.
176     c4_View vAirport =
177         storage.GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
178
179     c4_Row row;
180
181     iterator current = airports.begin();
182     const_iterator end = airports.end();
183     while ( current != end ) {
184         // add each airport record
185         pID (row) = current->id.c_str();
186         pLon (row) = current->longitude;
187         pLat (row) = current->latitude;
188         pElev (row) = current->elevation;
189         vAirport.Add(row);
190
191         ++current;
192     }
193
194     // commit our changes
195     storage.Commit();
196
197     return true;
198 }
199
200
201 // search for the specified id
202 bool
203 FGAirportsUtil::search( const string& id, FGAirport* a ) const
204 {
205     const_iterator it = airports.find( FGAirport(id) );
206     if ( it != airports.end() )
207     {
208         *a = *it;
209         return true;
210     }
211     else
212     {
213         return false;
214     }
215 }
216
217
218 FGAirport
219 FGAirportsUtil::search( const string& id ) const
220 {
221     FGAirport a;
222     this->search( id, &a );
223     return a;
224 }
225
226
227 // Destructor
228 FGAirportsUtil::~FGAirportsUtil( void ) {
229 }
230
231