]> git.mxchange.org Git - flightgear.git/blob - src/ATC/transmissionlist.cxx
Fixed a compile problem that affected gcc-2.95 users on Cygwin and removed a few...
[flightgear.git] / src / ATC / transmissionlist.cxx
1 // transmissionlist.cxx -- transmission management class
2 //
3 // Written by Alexander Kappes, started March 2002.
4 // Based on navlist.cxx by Curtis Olson, started April 2000.
5 //
6 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <strings.h>    // bcopy()
30
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/misc/sgstream.hxx>
33 #include <simgear/math/sg_geodesy.hxx>
34
35 #include "transmissionlist.hxx"
36
37 #include <GUI/gui.h>
38
39
40 FGTransmissionList *current_transmissionlist;
41
42
43 // Constructor
44 FGTransmissionList::FGTransmissionList( void ) {
45 }
46
47
48 // Destructor
49 FGTransmissionList::~FGTransmissionList( void ) {
50 }
51
52 /*
53 // ============================================================================
54 // init menu window
55 // ============================================================================
56 void mkATCMenuInit (void)
57 {
58   int dx = 400;
59   int dy = 100;
60   int y = (fgGetInt("/sim/startup/ysize") - 10 - dy);
61   ATCMenuBox = new puDialogBox (10, y);
62   {
63     ATCMenuFrame = new puFrame (0,0,400,100);
64     ATCMenuBoxMessage  =  new puText         (10, 70);
65     ATCMenuBoxMessage  -> setLabel           ("");
66   }
67   fgSetBool("/sim/atc/menu",false);
68   fgSetBool("/sim/atc/opt1",false);
69   fgSetBool("/sim/atc/opt2",false);
70   fgSetBool("/sim/atc/opt3",false);
71   fgSetBool("/sim/atc/opt4",false);
72   fgSetBool("/sim/atc/opt5",false);
73   fgSetBool("/sim/atc/opt6",false);
74   fgSetBool("/sim/atc/opt7",false);
75   fgSetBool("/sim/atc/opt8",false);
76   fgSetBool("/sim/atc/opt9",false);
77   fgSetBool("/sim/atc/opt0",false);
78 }
79
80 // ATC Menu Message Box
81 void mkATCMenu ( const char *txt )
82 {
83   ATCMenuBoxMessage  =  new puText   (10, 70);
84   ATCMenuBoxMessage->setLabel( txt );
85
86   FG_PUSH_PUI_DIALOG( ATCMenuBox );
87 }
88 */
89
90 // load default.transmissions
91 bool FGTransmissionList::init( SGPath path ) {
92     FGTransmission a;
93
94     transmissionlist_station.erase( transmissionlist_station.begin(), transmissionlist_station.end() );
95
96     sg_gzifstream in( path.str() );
97     if ( !in.is_open() ) {
98         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
99         exit(-1);
100     }
101
102     // read in each line of the file
103
104     // in >> skipeol;
105     // in >> skipcomment;
106
107 #ifdef __MWERKS__
108
109     char c = 0;
110     while ( in.get(c) && c != '\0' ) {
111         in.putback(c);
112         in >> a;
113         if ( a.get_type() != '[' ) {
114             transmissionlist_code[a.get_station()].push_back(a);
115         }
116         in >> skipcomment;
117     }
118
119 #else
120
121     double min = 100000;
122     double max = 0;
123
124     while ( ! in.eof() ) {
125         in >> a;
126         transmissionlist_station[a.get_station()].push_back(a);
127         
128         in >> skipcomment;
129
130         if ( a.get_station() < min ) {
131           min = a.get_station();
132         }
133         if ( a.get_station() > max ) {
134           max = a.get_station();
135         }
136         cout << a.get_station() << " " << a.get_code().c1 << " " << a.get_code().c2 << " "
137              << a.get_code().c3 << " " << a.get_transtext() 
138              << " " << a.get_menutext() << endl;
139     }
140     
141 #endif
142
143     // init ATC menu
144     fgSetBool("/sim/atc/menu",false);
145
146     return true;
147 }
148
149 // query the database for the specified station type; 
150 // for station see FlightGear/ATC/default.transmissions
151 bool FGTransmissionList::query_station( const int &station, FGTransmission *t,
152                                         int max_trans, int &num_trans ) 
153 {
154   transmission_list_type     tmissions = transmissionlist_station[station];
155   transmission_list_iterator current   = tmissions.begin();
156   transmission_list_iterator last      = tmissions.end();
157
158   for ( ; current != last ; ++current ) {
159     if (num_trans < max_trans) {
160       t[num_trans] = *current;
161       num_trans += 1;
162     }
163     else {
164       cout << "Transmissionlist error: Too many transmissions" << endl; 
165     }
166   }
167
168   if ( num_trans != 0 ) return true;
169   else {
170     cout << "No transmission with station " << station << "found." << endl;
171     string empty;
172     return false;
173   }
174 }
175
176 string FGTransmissionList::gen_text(const int &station, const TransCode code, 
177                                     const TransPar &tpars, const bool ttext )
178 {
179   const int cmax = 100;
180   string message;
181   char tag[4];
182   char crej = '@';
183   char mes[cmax];
184   char dum[cmax];
185   //char buf[10];
186   char *pos;
187   int len;
188   FGTransmission t;
189
190   //  if (current_transmissionlist->query_station( station, &t ) ) {  
191   transmission_list_type     tmissions = transmissionlist_station[station];
192   transmission_list_iterator current   = tmissions.begin();
193   transmission_list_iterator last      = tmissions.end();
194   
195   for ( ; current != last ; ++current ) {
196     if ( current->get_code().c1 == code.c1 &&  
197          current->get_code().c2 == code.c2 &&
198          current->get_code().c3 == code.c3 ) {
199       
200       if ( ttext ) message = current->get_transtext();
201       else message = current->get_menutext();
202       strcpy( &mes[0], message.c_str() ); 
203       
204       while ( strchr(&mes[0], crej) != NULL  ) {
205         pos = strchr( &mes[0], crej );
206         bcopy(pos, &tag[0], 3);
207         tag[3] = '\0';
208         int i;
209         len = 0;
210         for ( i=0; i<cmax; i++ ) {
211           if ( mes[i] == crej ) {
212             len = i; 
213             break;
214           }
215         }
216         strncpy( &dum[0], &mes[0], len );
217         dum[len] = '\0';
218         
219         if ( strcmp ( tag, "@ST" ) == 0 )
220           strcat( &dum[0], tpars.station.c_str() );
221         else if ( strcmp ( tag, "@AP" ) == 0 )
222           strcat( &dum[0], tpars.airport.c_str() );
223         else if ( strcmp ( tag, "@CS" ) == 0 ) 
224           strcat( &dum[0], tpars.callsign.c_str() );
225         else if ( strcmp ( tag, "@TD" ) == 0 ) {
226           if ( tpars.tdir == 1 ) {
227             char buf[] = "left";
228             strcat( &dum[0], &buf[0] );
229           }
230           else {
231             char buf[] = "right";
232             strcat( &dum[0], &buf[0] );
233           }
234         }
235         else if ( strcmp ( tag, "@HE" ) == 0 ) {
236           char buf[10];
237           sprintf( buf, "%i", (int)(tpars.heading) );
238           strcat( &dum[0], &buf[0] );
239         }
240         else if ( strcmp ( tag, "@VD" ) == 0 ) {
241           if ( tpars.VDir == 1 ) {
242             char buf[] = "Descent and maintain";
243             strcat( &dum[0], &buf[0] );
244           }
245           else if ( tpars.VDir == 2 ) {
246             char buf[] = "Maintain";
247             strcat( &dum[0], &buf[0] );
248           }
249           else if ( tpars.VDir == 3 ) {
250             char buf[] = "Climb and maintain";
251             strcat( &dum[0], &buf[0] );
252           }  
253         }
254         else if ( strcmp ( tag, "@AL" ) == 0 ) {
255           char buf[10];
256           sprintf( buf, "%i", (int)(tpars.alt) );
257           strcat( &dum[0], &buf[0] );
258         }
259         else if ( strcmp ( tag, "@MI" ) == 0 ) {
260           char buf[10];
261           sprintf( buf, "%3.1f", tpars.miles );
262           strcat( &dum[0], &buf[0] );
263         }
264         else if ( strcmp ( tag, "@FR" ) == 0 ) {
265           char buf[10];
266           sprintf( buf, "%6.2f", tpars.freq );
267           strcat( &dum[0], &buf[0] );
268         }
269         else if ( strcmp ( tag, "@RW" ) == 0 )
270           strcat( &dum[0], tpars.runway.c_str() );
271         else {
272           cout << "Tag " << tag << " not found" << endl;
273           break;
274         }
275         strcat( &dum[0], &mes[len+3] );
276         strcpy( &mes[0], &dum[0] );
277       }
278
279       //cout << mes  << endl;  
280       break;
281     }
282   }
283   if ( mes != "" ) return mes;
284   else return "No transmission found";
285 }
286
287