]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
9ac98469b2389117ad5cabccb6233c7dff2eff47
[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
54 FGAirports::FGAirports( const string& file ) {
55     dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_READER, 0, NULL );
56     if ( dbf == NULL ) {
57         cout << "Error opening " << file << endl;
58         exit(-1);
59     } else {
60         cout << "successfully opened " << file << endl;
61     }
62 }
63
64
65 // search for the specified id
66 bool
67 FGAirports::search( const string& id, FGAirport* a ) const
68 {
69     FGAirport *tmp;
70     datum content;
71     datum key;
72
73     key.dptr = (char *)id.c_str();
74     key.dsize = id.length();
75
76     content = gdbm_fetch( dbf, key );
77
78     cout << "gdbm_fetch() finished" << endl;
79
80     if ( content.dptr != NULL ) {
81         tmp = (FGAirport *)content.dptr;
82
83         // a->id = tmp->id;
84         a->longitude = tmp->longitude;
85         a->latitude = tmp->latitude;
86         a->elevation = tmp->elevation;
87
88         free( content.dptr );
89
90     } else {
91         return false;
92     }
93
94     return true;
95 }
96
97
98 FGAirport
99 FGAirports::search( const string& id ) const
100 {
101     FGAirport a, *tmp;
102     datum content;
103     datum key;
104
105     key.dptr = (char *)id.c_str();
106     key.dsize = id.length();
107
108     content = gdbm_fetch( dbf, key );
109
110     if ( content.dptr != NULL ) {
111         tmp = (FGAirport *)content.dptr;
112         a = *tmp;
113     }
114
115     return a;
116 }
117
118
119 // Destructor
120 FGAirports::~FGAirports( void ) {
121     gdbm_close( dbf );
122 }
123
124
125 // Constructor
126 FGAirportsUtil::FGAirportsUtil() {
127 }
128
129
130 // load the data
131 int FGAirportsUtil::load( const string& file ) {
132     FGAirport a;
133
134     airports.erase( airports.begin(), airports.end() );
135
136     fg_gzifstream in( file );
137     if ( !in.is_open() ) {
138         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << file );
139         exit(-1);
140     }
141
142     /*
143     // We can use the STL copy algorithm because the input
144     // file doesn't contain and comments or blank lines.
145     copy( istream_iterator<FGAirport,ptrdiff_t>(in.stream()),
146           istream_iterator<FGAirport,ptrdiff_t>(),
147           inserter( airports, airports.begin() ) );
148     */
149
150     // read in each line of the file
151
152 #ifdef __MWERKS__
153
154     in >> skipcomment;
155     char c = 0;
156     while ( in.get(c) && c != '\0' ) {
157         in.putback(c);
158         in >> a;
159         airports.insert(a);
160         in >> skipcomment;
161     }
162
163 #else
164
165     in >> skipcomment;
166     while ( ! in.eof() ) {
167         in >> a;
168         airports.insert(a);
169         in >> skipcomment;
170     }
171
172 #endif
173
174     return 1;
175 }
176
177
178 // save the data in gdbm format
179 bool FGAirportsUtil::dump_gdbm( const string& file ) {
180
181     GDBM_FILE dbf;
182     dbf = gdbm_open( (char *)file.c_str(), 0, GDBM_NEWDB | GDBM_FAST, 
183                      S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
184                      NULL );
185     if ( dbf == NULL ) {
186         cout << "Error opening " << file << endl;
187         exit(-1);
188     } else {
189         cout << "successfully opened " << file << endl;
190     }
191
192     iterator current = airports.begin();
193     const_iterator end = airports.end();
194     while ( current != end ) {
195         datum key;
196         key.dptr = (char *)current->id.c_str();
197         key.dsize = current->id.length();
198
199         datum content;
200         FGAirport tmp = *current;
201         content.dptr = (char *)(& tmp);
202         content.dsize = sizeof( *current );
203
204         gdbm_store( dbf, key, content, GDBM_REPLACE );
205
206         ++current;
207     }
208
209     gdbm_close( dbf );
210
211     return true;
212 }
213
214
215 // search for the specified id
216 bool
217 FGAirportsUtil::search( const string& id, FGAirport* a ) const
218 {
219     const_iterator it = airports.find( FGAirport(id) );
220     if ( it != airports.end() )
221     {
222         *a = *it;
223         return true;
224     }
225     else
226     {
227         return false;
228     }
229 }
230
231
232 FGAirport
233 FGAirportsUtil::search( const string& id ) const
234 {
235     FGAirport a;
236     this->search( id, &a );
237     return a;
238 }
239
240
241 // Destructor
242 FGAirportsUtil::~FGAirportsUtil( void ) {
243 }
244
245