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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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( const 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.eof() ) {
78 transmissionlist_station[a.get_station()].push_back(a);
82 if ( a.get_station() < min ) {
83 min = a.get_station();
85 if ( a.get_station() > max ) {
86 max = a.get_station();
90 cout << a.get_station() << " " << a.get_code().c1 << " " << a.get_code().c2 << " "
91 << a.get_code().c3 << " " << a.get_transtext()
92 << " " << a.get_menutext() << endl;
97 fgSetBool("/sim/atc/menu",false);
102 // query the database for the specified station type;
103 // for station see FlightGear/ATC/default.transmissions
104 bool FGTransmissionList::query_station( const atc_type &station, FGTransmission *t,
105 int max_trans, int &num_trans )
107 transmission_list_type tmissions = transmissionlist_station[station];
108 transmission_list_iterator current = tmissions.begin();
109 transmission_list_iterator last = tmissions.end();
111 for ( ; current != last ; ++current ) {
112 if (num_trans < max_trans) {
113 t[num_trans] = *current;
117 SG_LOG(SG_GENERAL, SG_WARN, "Transmissionlist error: Too many transmissions");
121 if ( num_trans != 0 ) return true;
123 SG_LOG(SG_GENERAL, SG_WARN, "No transmission with station " << station << "found.");
129 string FGTransmissionList::gen_text(const atc_type &station, const TransCode code,
130 const TransPar &tpars, const bool ttext )
132 const int cmax = 300;
143 // if (current_transmissionlist->query_station( station, &t ) ) {
144 transmission_list_type tmissions = transmissionlist_station[station];
145 transmission_list_iterator current = tmissions.begin();
146 transmission_list_iterator last = tmissions.end();
148 for ( ; current != last ; ++current ) {
149 if ( current->get_code().c1 == code.c1 &&
150 current->get_code().c2 == code.c2 &&
151 current->get_code().c3 == code.c3 ) {
153 if ( ttext ) message = current->get_transtext();
154 else message = current->get_menutext();
155 strcpy( &mes[0], message.c_str() );
157 // Replace all the '@' parameters with the actual text.
158 int check = 0; // If mes gets overflowed the while loop can go infinite
159 while ( strchr(&mes[0], crej) != NULL ) { // ie. loop until no more occurances of crej ('@') found
160 pos = strchr( &mes[0], crej );
161 memmove(&tag[0], pos, 3);
165 for ( i=0; i<cmax; i++ ) {
166 if ( mes[i] == crej ) {
171 strncpy( &dum[0], &mes[0], len );
174 if ( strcmp ( tag, "@ST" ) == 0 )
175 strcat( &dum[0], tpars.station.c_str() );
176 else if ( strcmp ( tag, "@AP" ) == 0 )
177 strcat( &dum[0], tpars.airport.c_str() );
178 else if ( strcmp ( tag, "@CS" ) == 0 )
179 strcat( &dum[0], tpars.callsign.c_str() );
180 else if ( strcmp ( tag, "@TD" ) == 0 ) {
181 if ( tpars.tdir == 1 ) {
183 strcat( &dum[0], &buf[0] );
186 char buf[] = "right";
187 strcat( &dum[0], &buf[0] );
190 else if ( strcmp ( tag, "@HE" ) == 0 ) {
192 sprintf( buf, "%i", (int)(tpars.heading) );
193 strcat( &dum[0], &buf[0] );
195 else if ( strcmp ( tag, "@VD" ) == 0 ) {
196 if ( tpars.VDir == 1 ) {
197 char buf[] = "Descend and maintain";
198 strcat( &dum[0], &buf[0] );
200 else if ( tpars.VDir == 2 ) {
201 char buf[] = "Maintain";
202 strcat( &dum[0], &buf[0] );
204 else if ( tpars.VDir == 3 ) {
205 char buf[] = "Climb and maintain";
206 strcat( &dum[0], &buf[0] );
209 else if ( strcmp ( tag, "@AL" ) == 0 ) {
211 sprintf( buf, "%i", (int)(tpars.alt) );
212 strcat( &dum[0], &buf[0] );
214 else if ( strcmp ( tag, "@MI" ) == 0 ) {
216 sprintf( buf, "%3.1f", tpars.miles );
217 strcat( &dum[0], &buf[0] );
219 else if ( strcmp ( tag, "@FR" ) == 0 ) {
221 sprintf( buf, "%6.2f", tpars.freq );
222 strcat( &dum[0], &buf[0] );
224 else if ( strcmp ( tag, "@RW" ) == 0 )
225 strcat( &dum[0], tpars.runway.c_str() );
227 SG_LOG(SG_GENERAL, SG_WARN, "Tag " << tag << " not found");
230 strcat( &dum[0], &mes[len+3] );
231 strcpy( &mes[0], &dum[0] );
235 SG_LOG(SG_GENERAL, SG_WARN, "WARNING: Possibly endless loop terminated in FGTransmissionlist::gen_text(...)");
240 //cout << mes << endl;
244 return mes[0] ? mes : "No transmission found";