1 // transmissionlist.cxx -- transmission management class
3 // Written by Alexander Kappes, started March 2002.
4 // Based on navlist.cxx by Curtis Olson, started April 2000.
6 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
30 # include <strings.h> // bcopy()
32 # include <string.h> // MSVC doesn't have strings.h
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/misc/sgstream.hxx>
38 #include <simgear/math/sg_geodesy.hxx>
40 #include "transmissionlist.hxx"
45 FGTransmissionList *current_transmissionlist;
48 FGTransmissionList::FGTransmissionList( void ) {
52 FGTransmissionList::~FGTransmissionList( void ) {
56 // load default.transmissions
57 bool FGTransmissionList::init( SGPath path ) {
60 transmissionlist_station.erase( transmissionlist_station.begin(), transmissionlist_station.end() );
62 sg_gzifstream in( path.str() );
63 if ( !in.is_open() ) {
64 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
68 // read in each line of the file
76 while ( in.get(c) && c != '\0' ) {
79 if ( a.get_type() != '[' ) {
80 transmissionlist_code[a.get_station()].push_back(a);
90 while ( ! in.eof() ) {
92 transmissionlist_station[a.get_station()].push_back(a);
96 if ( a.get_station() < min ) {
97 min = a.get_station();
99 if ( a.get_station() > max ) {
100 max = a.get_station();
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;
113 fgSetBool("/sim/atc/menu",false);
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 )
123 transmission_list_type tmissions = transmissionlist_station[station];
124 transmission_list_iterator current = tmissions.begin();
125 transmission_list_iterator last = tmissions.end();
127 for ( ; current != last ; ++current ) {
128 if (num_trans < max_trans) {
129 t[num_trans] = *current;
133 cout << "Transmissionlist error: Too many transmissions" << endl;
137 if ( num_trans != 0 ) return true;
139 cout << "No transmission with station " << station << "found." << endl;
145 string FGTransmissionList::gen_text(const atc_type &station, const TransCode code,
146 const TransPar &tpars, const bool ttext )
148 const int cmax = 300;
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();
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 ) {
169 if ( ttext ) message = current->get_transtext();
170 else message = current->get_menutext();
171 strcpy( &mes[0], message.c_str() );
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);
181 for ( i=0; i<cmax; i++ ) {
182 if ( mes[i] == crej ) {
187 strncpy( &dum[0], &mes[0], len );
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 ) {
199 strcat( &dum[0], &buf[0] );
202 char buf[] = "right";
203 strcat( &dum[0], &buf[0] );
206 else if ( strcmp ( tag, "@HE" ) == 0 ) {
208 sprintf( buf, "%i", (int)(tpars.heading) );
209 strcat( &dum[0], &buf[0] );
211 else if ( strcmp ( tag, "@VD" ) == 0 ) {
212 if ( tpars.VDir == 1 ) {
213 char buf[] = "Descend and maintain";
214 strcat( &dum[0], &buf[0] );
216 else if ( tpars.VDir == 2 ) {
217 char buf[] = "Maintain";
218 strcat( &dum[0], &buf[0] );
220 else if ( tpars.VDir == 3 ) {
221 char buf[] = "Climb and maintain";
222 strcat( &dum[0], &buf[0] );
225 else if ( strcmp ( tag, "@AL" ) == 0 ) {
227 sprintf( buf, "%i", (int)(tpars.alt) );
228 strcat( &dum[0], &buf[0] );
230 else if ( strcmp ( tag, "@MI" ) == 0 ) {
232 sprintf( buf, "%3.1f", tpars.miles );
233 strcat( &dum[0], &buf[0] );
235 else if ( strcmp ( tag, "@FR" ) == 0 ) {
237 sprintf( buf, "%6.2f", tpars.freq );
238 strcat( &dum[0], &buf[0] );
240 else if ( strcmp ( tag, "@RW" ) == 0 )
241 strcat( &dum[0], tpars.runway.c_str() );
243 cout << "Tag " << tag << " not found" << endl;
246 strcat( &dum[0], &mes[len+3] );
247 strcpy( &mes[0], &dum[0] );
251 SG_LOG(SG_ATC, SG_WARN, "WARNING: Possibly endless loop terminated in FGTransmissionlist::gen_text(...)");
256 //cout << mes << endl;
260 if ( mes != "" ) return mes;
261 else return "No transmission found";