]> git.mxchange.org Git - flightgear.git/blob - src/ATC/transmissionlist.cxx
IRIX fixes from Erik Hofman
[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 static puDialogBox  *ATCMenuBox = 0;
40 static puFrame      *ATCMenuFrame = 0;
41 static puText       *ATCMenuBoxMessage = 0;
42
43 FGTransmissionList *current_transmissionlist;
44
45
46 // Constructor
47 FGTransmissionList::FGTransmissionList( void ) {
48 }
49
50
51 // Destructor
52 FGTransmissionList::~FGTransmissionList( void ) {
53 }
54
55 /*
56 // ============================================================================
57 // init menu window
58 // ============================================================================
59 void mkATCMenuInit (void)
60 {
61   int dx = 400;
62   int dy = 100;
63   int y = (fgGetInt("/sim/startup/ysize") - 10 - dy);
64   ATCMenuBox = new puDialogBox (10, y);
65   {
66     ATCMenuFrame = new puFrame (0,0,400,100);
67     ATCMenuBoxMessage  =  new puText         (10, 70);
68     ATCMenuBoxMessage  -> setLabel           ("");
69   }
70   fgSetBool("/sim/atc/menu",false);
71   fgSetBool("/sim/atc/opt1",false);
72   fgSetBool("/sim/atc/opt2",false);
73   fgSetBool("/sim/atc/opt3",false);
74   fgSetBool("/sim/atc/opt4",false);
75   fgSetBool("/sim/atc/opt5",false);
76   fgSetBool("/sim/atc/opt6",false);
77   fgSetBool("/sim/atc/opt7",false);
78   fgSetBool("/sim/atc/opt8",false);
79   fgSetBool("/sim/atc/opt9",false);
80   fgSetBool("/sim/atc/opt0",false);
81 }
82
83 // ATC Menu Message Box
84 void mkATCMenu ( const char *txt )
85 {
86   ATCMenuBoxMessage  =  new puText   (10, 70);
87   ATCMenuBoxMessage->setLabel( txt );
88
89   FG_PUSH_PUI_DIALOG( ATCMenuBox );
90 }
91 */
92
93 // load default.transmissions
94 bool FGTransmissionList::init( SGPath path ) {
95     FGTransmission a;
96
97     transmissionlist_station.erase( transmissionlist_station.begin(), transmissionlist_station.end() );
98
99     sg_gzifstream in( path.str() );
100     if ( !in.is_open() ) {
101         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
102         exit(-1);
103     }
104
105     // read in each line of the file
106
107     // in >> skipeol;
108     // in >> skipcomment;
109
110 #ifdef __MWERKS__
111
112     char c = 0;
113     while ( in.get(c) && c != '\0' ) {
114         in.putback(c);
115         in >> a;
116         if ( a.get_type() != '[' ) {
117             transmissionlist_code[a.get_station()].push_back(a);
118         }
119         in >> skipcomment;
120     }
121
122 #else
123
124     double min = 100000;
125     double max = 0;
126
127     while ( ! in.eof() ) {
128         in >> a;
129         transmissionlist_station[a.get_station()].push_back(a);
130         
131         in >> skipcomment;
132
133         if ( a.get_station() < min ) {
134           min = a.get_station();
135         }
136         if ( a.get_station() > max ) {
137           max = a.get_station();
138         }
139         cout << a.get_station() << " " << a.get_code().c1 << " " << a.get_code().c2 << " "
140              << a.get_code().c3 << " " << a.get_transtext() 
141              << " " << a.get_menutext() << endl;
142     }
143     
144 #endif
145
146     // init ATC menu
147     fgSetBool("/sim/atc/menu",false);
148
149     return true;
150 }
151
152 // query the database for the specified station type; 
153 // for station see FlightGear/ATC/default.transmissions
154 bool FGTransmissionList::query_station( const int &station, FGTransmission *t,
155                                         int max_trans, int &num_trans ) 
156 {
157   transmission_list_type     tmissions = transmissionlist_station[station];
158   transmission_list_iterator current   = tmissions.begin();
159   transmission_list_iterator last      = tmissions.end();
160
161   for ( ; current != last ; ++current ) {
162     if (num_trans < max_trans) {
163       t[num_trans] = *current;
164       num_trans += 1;
165     }
166     else {
167       cout << "Transmissionlist error: Too many transmissions" << endl; 
168     }
169   }
170
171   if ( num_trans != 0 ) return true;
172   else {
173     cout << "No transmission with station " << station << "found." << endl;
174     string empty;
175     return false;
176   }
177 }
178
179 string FGTransmissionList::gen_text(const int &station, const TransCode code, 
180                                     const TransPar &tpars, const bool ttext )
181 {
182   const int cmax = 100;
183   string message;
184   char tag[4];
185   char crej = '@';
186   char mes[cmax];
187   char dum[cmax];
188   char buf[10];
189   char *pos;
190   int len;
191   FGTransmission t;
192
193   //  if (current_transmissionlist->query_station( station, &t ) ) {  
194   transmission_list_type     tmissions = transmissionlist_station[station];
195   transmission_list_iterator current   = tmissions.begin();
196   transmission_list_iterator last      = tmissions.end();
197   
198   for ( ; current != last ; ++current ) {
199     if ( current->get_code().c1 == code.c1 &&  
200          current->get_code().c2 == code.c2 &&
201          current->get_code().c3 == code.c3 ) {
202       
203       if ( ttext ) message = current->get_transtext();
204       else message = current->get_menutext();
205       strcpy( &mes[0], message.c_str() ); 
206       
207       while ( strchr(&mes[0], crej) != NULL  ) {
208         pos = strchr( &mes[0], crej );
209         bcopy(pos, &tag, 3);
210         tag[3] = '\0';
211         int i;
212         len = 0;
213         for ( i=0; i<cmax; i++ ) {
214           if ( mes[i] == crej ) {
215             len = i; 
216             break;
217           }
218         }
219         strncpy( &dum[0], &mes[0], len );
220         dum[len] = '\0';
221         
222         if ( strcmp ( tag, "@ST" ) == 0 )
223           strcat( &dum[0], tpars.station.c_str() );
224         else if ( strcmp ( tag, "@AP" ) == 0 )
225           strcat( &dum[0], tpars.airport.c_str() );
226         else if ( strcmp ( tag, "@CS" ) == 0 ) 
227           strcat( &dum[0], tpars.callsign.c_str() );
228         else if ( strcmp ( tag, "@TD" ) == 0 ) {
229           if ( tpars.tdir == 1 ) {
230             char buf[] = "left";
231             strcat( &dum[0], &buf[0] );
232           }
233           else {
234             char buf[] = "right";
235             strcat( &dum[0], &buf[0] );
236           }
237         }
238         else if ( strcmp ( tag, "@HE" ) == 0 ) {
239           char buf[10];
240           sprintf( buf, "%i", (int)(tpars.heading) );
241           strcat( &dum[0], &buf[0] );
242         }
243         else if ( strcmp ( tag, "@VD" ) == 0 ) {
244           if ( tpars.VDir == 1 ) {
245             char buf[] = "Descent and maintain";
246             strcat( &dum[0], &buf[0] );
247           }
248           else if ( tpars.VDir == 2 ) {
249             char buf[] = "Maintain";
250             strcat( &dum[0], &buf[0] );
251           }
252           else if ( tpars.VDir == 3 ) {
253             char buf[] = "Climb and maintain";
254             strcat( &dum[0], &buf[0] );
255           }  
256         }
257         else if ( strcmp ( tag, "@AL" ) == 0 ) {
258           char buf[10];
259           sprintf( buf, "%i", (int)(tpars.alt) );
260           strcat( &dum[0], &buf[0] );
261         }
262         else if ( strcmp ( tag, "@MI" ) == 0 ) {
263           char buf[10];
264           sprintf( buf, "%3.1f", tpars.miles );
265           strcat( &dum[0], &buf[0] );
266         }
267         else if ( strcmp ( tag, "@FR" ) == 0 ) {
268           char buf[10];
269           sprintf( buf, "%6.2f", tpars.freq );
270           strcat( &dum[0], &buf[0] );
271         }
272         else if ( strcmp ( tag, "@RW" ) == 0 )
273           strcat( &dum[0], tpars.runway.c_str() );
274         else {
275           cout << "Tag " << tag << " not found" << endl;
276           break;
277         }
278         strcat( &dum[0], &mes[len+3] );
279         strcpy( &mes[0], &dum[0] );
280       }
281
282       //cout << mes  << endl;  
283       break;
284     }
285   }
286   if ( mes != "" ) return mes;
287   else return "No transmission found";
288 }
289
290