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