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