]> git.mxchange.org Git - flightgear.git/blob - src/Airports/simple.cxx
Tweak #includes to use double quotes for local files not <>
[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     a->id        = (const char *) pID(r); /// NHV fix wrong case crash
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     } else {
125         SG_LOG( SG_GENERAL, SG_ALERT, "opened: " << file );
126     }
127
128     // skip first line of file
129     char tmp[2048];
130     in.getline( tmp, 2048 );
131
132     // read in each line of the file
133
134 #ifdef __MWERKS__
135
136     in >> ::skipws;
137     char c = 0;
138     while ( in.get(c) && c != '\0' ) {
139         if ( c == 'A' ) {
140             in >> a;
141             SG_LOG( SG_GENERAL, SG_INFO, a.id );
142             in >> skipeol;
143             airports.insert(a);
144         } else if ( c == 'R' ) {
145             in >> skipeol;
146         } else {
147             in >> skipeol;
148         }
149         in >> ::skipws;
150     }
151
152 #else
153
154     in >> ::skipws;
155     string token;
156     while ( ! in.eof() ) {
157         in >> token;
158         if ( token == "A" ) {
159             in >> a;
160             SG_LOG( SG_GENERAL, SG_INFO, "in <- " << a.id );
161             in >> skipeol;
162             airports.insert(a);
163         } else if ( token == "R" ) {
164             in >> skipeol;
165         } else {
166             in >> skipeol;
167         }
168         in >> ::skipws;
169     }
170
171 #endif
172
173     return 1;
174 }
175
176
177 // save the data in gdbm format
178 bool FGAirportsUtil::dump_mk4( const string& file ) {
179
180     // open database for writing
181     c4_Storage storage( file.c_str(), true );
182
183     // need to do something about error handling here!
184
185     // define the properties
186     c4_StringProp pID ("ID");
187     c4_FloatProp pLon ("Longitude");
188     c4_FloatProp pLat ("Latitude");
189     c4_FloatProp pElev ("Elevation");
190
191     // Start with an empty view of the proper structure.
192     c4_View vAirport =
193         storage.GetAs("airport[ID:S,Longitude:F,Latitude:F,Elevation:F]");
194
195     c4_Row row;
196
197     const_iterator current = airports.begin();
198     const_iterator end = airports.end();
199     while ( current != end ) {
200         // add each airport record
201         cout << "out -> " << current->id << endl;
202         pID (row) = current->id.c_str();
203         pLon (row) = current->longitude;
204         pLat (row) = current->latitude;
205         pElev (row) = current->elevation;
206         vAirport.Add(row);
207
208         ++current;
209     }
210
211     // commit our changes
212     storage.Commit();
213
214     return true;
215 }
216
217
218 // search for the specified id
219 bool
220 FGAirportsUtil::search( const string& id, FGAirport* a ) const
221 {
222     const_iterator it = airports.find( FGAirport(id) );
223     if ( it != airports.end() )
224     {
225         *a = *it;
226         return true;
227     }
228     else
229     {
230         return false;
231     }
232 }
233
234
235 FGAirport
236 FGAirportsUtil::search( const string& id ) const
237 {
238     FGAirport a;
239     this->search( id, &a );
240     return a;
241 }
242
243
244 // Destructor
245 FGAirportsUtil::~FGAirportsUtil( void ) {
246 }
247
248