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