1 // props.hxx -- FGFS property manager interaction class
3 // Written by Curtis Olson, started September 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/compiler.h>
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/io/iochannel.hxx>
27 #include <simgear/math/sg_types.hxx>
28 #include <simgear/misc/props.hxx>
30 #include <stdlib.h> // atoi() atof()
37 FG_USING_STD(istrstream);
46 // open hailing frequencies
47 bool FGProps::open() {
51 FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel "
52 << "is already in use, ignoring" );
56 SGIOChannel *io = get_io_channel();
58 if ( ! io->open( get_direction() ) ) {
59 FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
69 // return a human readable form of the value "type"
70 static string getValueTypeString( const SGValue *v ) {
77 SGValue::Type type = v->getType();
78 if ( type == SGValue::UNKNOWN ) {
80 } else if ( type == SGValue::BOOL ) {
82 } else if ( type == SGValue::INT ) {
84 } else if ( type == SGValue::FLOAT ) {
86 } else if ( type == SGValue::DOUBLE ) {
88 } else if ( type == SGValue::STRING ) {
96 bool FGProps::process_command( const char *cmd ) {
97 SGIOChannel *io = get_io_channel();
99 cout << "processing command = " << cmd;
105 while ( !in.eof() ) {
108 tokens.push_back( token );
111 string command = tokens[0];
113 SGPropertyNode node( path, ¤t_properties );
114 if ( node.getPath() == "" ) {
118 if ( command == "ls" ) {
119 for (int i = 0; i < (int)node.size(); i++) {
120 SGPropertyNode child = node.getChild(i);
121 string name = child.getName();
123 if ( child.size() > 0 ) {
126 string value = node.getStringValue ( name, "" );
127 line += " =\t'" + value + "'\t(";
128 line += getValueTypeString( node.getValue( name ) );
131 io->writestring( line.c_str() );
133 } else if ( command == "cd" ) {
134 // string tmp = "current path = " + node.getPath() + "\n";
135 // io->writestring( tmp.c_str() );
137 if ( tokens.size() <= 1 ) {
139 } else if ( tokens[1] == "." ) {
141 } else if ( tokens[1] == ".." ) {
143 string current = node.getPath();
144 int pos = current.rfind("/");
145 // cout << "path = " << current << endl;
146 // cout << "new path = " << current.substr(0, pos) << endl;d
147 string tmp = current.substr(0, pos);
151 // decend to specified child
152 string tmp = node.getPath();
153 tmp += "/" + tokens[1];
157 } else if ( command == "show" ) {
158 if ( tokens.size() <= 1 ) {
161 string ttt = "debug = '" + tokens[1] + "'\n";
162 io->writestring( ttt.c_str() );
164 string value = node.getStringValue ( tokens[1], "" );
165 string tmp = tokens[1] + " = '" + value + "' (";
166 tmp += getValueTypeString( node.getValue( tokens[1] ) );
169 io->writestring( tmp.c_str() );
171 } else if ( command == "set" ) {
172 if ( tokens.size() <= 2 ) {
175 SGValue *v = node.getValue( tokens[1] );
177 SGValue::Type type = v->getType();
178 if ( type == SGValue::UNKNOWN ) {
179 v->setUnknownValue( tokens[2] );
180 } else if ( type == SGValue::BOOL ) {
181 if ( tokens[2] == "true" ) {
182 v->setBoolValue( true );
183 } else if ( tokens[2] == "false" ) {
184 v->setBoolValue( false );
186 v->setBoolValue( atoi( tokens[2].c_str() ) );
188 } else if ( type == SGValue::INT ) {
189 v->setIntValue( atoi( tokens[2].c_str() ) );
190 } else if ( type == SGValue::FLOAT ) {
191 v->setFloatValue( atof( tokens[2].c_str() ) );
192 } else if ( type == SGValue::DOUBLE ) {
193 v->setDoubleValue( atof( tokens[2].c_str() ) );
194 } else if ( type == SGValue::STRING ) {
195 v->setStringValue( tokens[2] );
198 // now fetch and write out the new value as confirmation
200 string value = node.getStringValue ( tokens[1], "" );
201 string tmp = tokens[1] + " = '" + value + "' (";
202 tmp += getValueTypeString( node.getValue( tokens[1] ) );
205 io->writestring( tmp.c_str() );
207 string tmp = tokens[1] + " is unknown.\n";
209 io->writestring( tmp.c_str() );
213 } else if ( command == "quit" ) {
216 io->writestring( "\n" );
217 io->writestring( "Valid commands are:\n" );
218 io->writestring( "\n" );
219 io->writestring( "help show help message\n" );
220 io->writestring( "ls list current directory\n" );
221 io->writestring( "cd <dir> cd to a directory, '..' to move back\n" );
222 io->writestring( "show <var> show the value of a parameter\n" );
223 io->writestring( "set <var> <val> set <var> to a new <val>\n" );
224 io->writestring( "quit terminate connection\n" );
225 io->writestring( "\n" );
228 string prompt = node.getPath();
229 if ( prompt == "" ) {
233 io->writestring( prompt.c_str() );
239 // process work for this port
240 bool FGProps::process() {
241 SGIOChannel *io = get_io_channel();
243 // cout << "processing incoming props command" << endl;
245 if ( get_direction() == SG_IO_BI ) {
246 // cout << " (bi directional)" << endl;
247 while ( io->readline( buf, max_cmd_len ) > 0 ) {
248 FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
249 process_command( buf );
252 FG_LOG( FG_IO, FG_ALERT,
253 "in or out direction not supported for FGProps." );
261 bool FGProps::close() {
262 SGIOChannel *io = get_io_channel();
264 set_enabled( false );
266 if ( ! io->close() ) {
270 cout << "successfully closed channel\n";