]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/transmissionlist.cxx
Clean up header file use of iostream and "using" declarations
[flightgear.git] / src / ATCDCL / 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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #ifdef HAVE_STRINGS_H
30 #  include <strings.h>   // bcopy()
31 #else
32 #  include <string.h>    // MSVC doesn't have strings.h
33 #endif
34
35
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/misc/sgstream.hxx>
38 #include <simgear/math/sg_geodesy.hxx>
39
40 #include "transmissionlist.hxx"
41
42 #include <GUI/gui.h>
43
44
45 FGTransmissionList *current_transmissionlist;
46
47
48 FGTransmissionList::FGTransmissionList( void ) {
49 }
50
51
52 FGTransmissionList::~FGTransmissionList( void ) {
53 }
54
55
56 // load default.transmissions
57 bool FGTransmissionList::init( const SGPath& path ) {
58     FGTransmission a;
59
60     transmissionlist_station.erase( transmissionlist_station.begin(), transmissionlist_station.end() );
61
62     sg_gzifstream in( path.str() );
63     if ( !in.is_open() ) {
64         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
65         exit(-1);
66     }
67
68     // read in each line of the file
69
70     // in >> skipeol;
71     // in >> skipcomment;
72
73 #ifdef __MWERKS__
74
75     char c = 0;
76     while ( in.get(c) && c != '\0' ) {
77         in.putback(c);
78         in >> a;
79         if ( a.get_type() != '[' ) {
80             transmissionlist_code[a.get_station()].push_back(a);
81         }
82         in >> skipcomment;
83     }
84
85 #else
86
87     double min = 100000;
88     double max = 0;
89
90     while ( ! in.eof() ) {
91         in >> a;
92         transmissionlist_station[a.get_station()].push_back(a);
93
94         in >> skipcomment;
95
96         if ( a.get_station() < min ) {
97             min = a.get_station();
98         }
99         if ( a.get_station() > max ) {
100             max = a.get_station();
101         }
102
103         /*
104         cout << a.get_station() << " " << a.get_code().c1 << " " << a.get_code().c2 << " "
105              << a.get_code().c3 << " " << a.get_transtext() 
106              << " " << a.get_menutext() << endl;
107         */
108     }
109  
110 #endif
111
112     // init ATC menu
113     fgSetBool("/sim/atc/menu",false);
114
115     return true;
116 }
117
118 // query the database for the specified station type; 
119 // for station see FlightGear/ATC/default.transmissions
120 bool FGTransmissionList::query_station( const atc_type &station, FGTransmission *t,
121                                         int max_trans, int &num_trans ) 
122 {
123   transmission_list_type     tmissions = transmissionlist_station[station];
124   transmission_list_iterator current   = tmissions.begin();
125   transmission_list_iterator last      = tmissions.end();
126
127   for ( ; current != last ; ++current ) {
128     if (num_trans < max_trans) {
129       t[num_trans] = *current;
130       num_trans += 1;
131     }
132     else {
133       SG_LOG(SG_GENERAL, SG_WARN, "Transmissionlist error: Too many transmissions"); 
134     }
135   }
136
137   if ( num_trans != 0 ) return true;
138   else {
139     SG_LOG(SG_GENERAL, SG_WARN, "No transmission with station " << station << "found.");
140     string empty;
141     return false;
142   }
143 }
144
145 string FGTransmissionList::gen_text(const atc_type &station, const TransCode code, 
146                                     const TransPar &tpars, const bool ttext )
147 {
148         const int cmax = 300;
149         string message;
150         char tag[4];
151         char crej = '@';
152         char mes[cmax];
153         char dum[cmax];
154         //char buf[10];
155         char *pos;
156         int len;
157         FGTransmission t;
158         
159         //  if (current_transmissionlist->query_station( station, &t ) ) { 
160         transmission_list_type     tmissions = transmissionlist_station[station];
161         transmission_list_iterator current   = tmissions.begin();
162         transmission_list_iterator last      = tmissions.end();
163         
164         for ( ; current != last ; ++current ) {
165                 if ( current->get_code().c1 == code.c1 &&  
166                         current->get_code().c2 == code.c2 &&
167                     current->get_code().c3 == code.c3 ) {
168                         
169                         if ( ttext ) message = current->get_transtext();
170                         else message = current->get_menutext();
171                         strcpy( &mes[0], message.c_str() ); 
172                         
173                         // Replace all the '@' parameters with the actual text.
174                         int check = 0;  // If mes gets overflowed the while loop can go infinite
175                         while ( strchr(&mes[0], crej) != NULL  ) {      // ie. loop until no more occurances of crej ('@') found
176                                 pos = strchr( &mes[0], crej );
177                                 memmove(&tag[0], pos, 3);
178                                 tag[3] = '\0';
179                                 int i;
180                                 len = 0;
181                                 for ( i=0; i<cmax; i++ ) {
182                                         if ( mes[i] == crej ) {
183                                                 len = i; 
184                                                 break;
185                                         }
186                                 }
187                                 strncpy( &dum[0], &mes[0], len );
188                                 dum[len] = '\0';
189                                 
190                                 if ( strcmp ( tag, "@ST" ) == 0 )
191                                         strcat( &dum[0], tpars.station.c_str() );
192                                 else if ( strcmp ( tag, "@AP" ) == 0 )
193                                         strcat( &dum[0], tpars.airport.c_str() );
194                                 else if ( strcmp ( tag, "@CS" ) == 0 ) 
195                                         strcat( &dum[0], tpars.callsign.c_str() );
196                                 else if ( strcmp ( tag, "@TD" ) == 0 ) {
197                                         if ( tpars.tdir == 1 ) {
198                                                 char buf[] = "left";
199                                                 strcat( &dum[0], &buf[0] );
200                                         }
201                                         else {
202                                                 char buf[] = "right";
203                                                 strcat( &dum[0], &buf[0] );
204                                         }
205                                 }
206                                 else if ( strcmp ( tag, "@HE" ) == 0 ) {
207                                         char buf[10];
208                                         sprintf( buf, "%i", (int)(tpars.heading) );
209                                         strcat( &dum[0], &buf[0] );
210                                 }
211                                 else if ( strcmp ( tag, "@VD" ) == 0 ) {
212                                         if ( tpars.VDir == 1 ) {
213                                                 char buf[] = "Descend and maintain";
214                                                 strcat( &dum[0], &buf[0] );
215                                         }
216                                         else if ( tpars.VDir == 2 ) {
217                                                 char buf[] = "Maintain";
218                                                 strcat( &dum[0], &buf[0] );
219                                         }
220                                         else if ( tpars.VDir == 3 ) {
221                                                 char buf[] = "Climb and maintain";
222                                                 strcat( &dum[0], &buf[0] );
223                                         }  
224                                 }
225                                 else if ( strcmp ( tag, "@AL" ) == 0 ) {
226                                         char buf[10];
227                                         sprintf( buf, "%i", (int)(tpars.alt) );
228                                         strcat( &dum[0], &buf[0] );
229                                 }
230                                 else if ( strcmp ( tag, "@MI" ) == 0 ) {
231                                         char buf[10];
232                                         sprintf( buf, "%3.1f", tpars.miles );
233                                         strcat( &dum[0], &buf[0] );
234                                 }
235                                 else if ( strcmp ( tag, "@FR" ) == 0 ) {
236                                         char buf[10];
237                                         sprintf( buf, "%6.2f", tpars.freq );
238                                         strcat( &dum[0], &buf[0] );
239                                 }
240                                 else if ( strcmp ( tag, "@RW" ) == 0 )
241                                         strcat( &dum[0], tpars.runway.c_str() );
242                                 else {
243                                         SG_LOG(SG_GENERAL, SG_WARN, "Tag " << tag << " not found");
244                                         break;
245                                 }
246                                 strcat( &dum[0], &mes[len+3] );
247                                 strcpy( &mes[0], &dum[0] );
248                                 
249                                 ++check;
250                                 if(check > 10) {
251                                         SG_LOG(SG_GENERAL, SG_WARN, "WARNING: Possibly endless loop terminated in FGTransmissionlist::gen_text(...)"); 
252                                         break;
253                                 }
254                         }
255                         
256                         //cout << mes  << endl;  
257                         break;
258                 }
259         }
260         return mes[0] ? mes : "No transmission found";
261 }
262
263