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 <Main/globals.hxx>
26 #include <simgear/compiler.h>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/io/iochannel.hxx>
29 #include <simgear/math/sg_types.hxx>
30 #include <simgear/misc/props.hxx>
32 #include <stdlib.h> // atoi() atof()
39 FG_USING_STD(istrstream);
48 // open hailing frequencies
49 bool FGProps::open() {
53 FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel "
54 << "is already in use, ignoring" );
58 SGIOChannel *io = get_io_channel();
60 if ( ! io->open( get_direction() ) ) {
61 FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
71 // return a human readable form of the value "type"
72 static string getValueTypeString( const SGValue *v ) {
79 SGValue::Type type = v->getType();
80 if ( type == SGValue::UNKNOWN ) {
82 } else if ( type == SGValue::BOOL ) {
84 } else if ( type == SGValue::INT ) {
86 } else if ( type == SGValue::FLOAT ) {
88 } else if ( type == SGValue::DOUBLE ) {
90 } else if ( type == SGValue::STRING ) {
98 bool FGProps::process_command( const char *cmd ) {
99 SGIOChannel *io = get_io_channel();
101 cout << "processing command = " << cmd;
107 while ( !in.eof() ) {
110 tokens.push_back( token );
113 string command = tokens[0];
115 SGPropertyNode * node = globals->get_props()->getNode(path);
117 if ( command == "ls" ) {
118 for (int i = 0; i < (int)node->nChildren(); i++) {
119 SGPropertyNode * child = node->getChild(i);
120 string name = child->getName();
122 if ( child->nChildren() > 0 ) {
125 string value = node->getStringValue ( name, "" );
126 line += " =\t'" + value + "'\t(";
127 line += getValueTypeString( node->getValue( name ) );
130 io->writestring( line.c_str() );
132 } else if ( command == "cd" ) {
133 // string tmp = "current path = " + node.getPath() + "\n";
134 // io->writestring( tmp.c_str() );
136 if ( tokens.size() <= 1 ) {
139 node = node->getNode(tokens[1]);
140 path = node->getPath();
142 } else if ( command == "pwd" ) {
143 string ttt = node->getPath();
148 io->writestring( ttt.c_str() );
149 } else if ( command == "show" ) {
150 if ( tokens.size() <= 1 ) {
153 string ttt = "debug = '" + tokens[1] + "'\n";
154 io->writestring( ttt.c_str() );
156 string value = node->getStringValue ( tokens[1], "" );
157 string tmp = tokens[1] + " = '" + value + "' (";
158 tmp += getValueTypeString( node->getValue( tokens[1] ) );
161 io->writestring( tmp.c_str() );
163 } else if ( command == "set" ) {
164 if ( tokens.size() <= 2 ) {
167 node->getValue( tokens[1], true )->setStringValue(tokens[2]);
169 // now fetch and write out the new value as confirmation
171 string value = node->getStringValue ( tokens[1], "" );
172 string tmp = tokens[1] + " = '" + value + "' (";
173 tmp += getValueTypeString( node->getValue( tokens[1] ) );
176 io->writestring( tmp.c_str() );
178 } else if ( command == "quit" ) {
181 io->writestring( "\n" );
182 io->writestring( "Valid commands are:\n" );
183 io->writestring( "\n" );
184 io->writestring( "help show help message\n" );
185 io->writestring( "ls list current directory\n" );
186 io->writestring( "cd <dir> cd to a directory, '..' to move back\n" );
187 io->writestring( "pwd display your current path\n" );
188 io->writestring( "show <var> show the value of a parameter\n" );
189 io->writestring( "set <var> <val> set <var> to a new <val>\n" );
190 io->writestring( "quit terminate connection\n" );
191 io->writestring( "\n" );
194 string prompt = node->getPath();
195 if ( prompt == "" ) {
199 io->writestring( prompt.c_str() );
205 // process work for this port
206 bool FGProps::process() {
207 SGIOChannel *io = get_io_channel();
209 // cout << "processing incoming props command" << endl;
211 if ( get_direction() == SG_IO_BI ) {
212 // cout << " (bi directional)" << endl;
213 while ( io->readline( buf, max_cmd_len ) > 0 ) {
214 FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
215 process_command( buf );
218 FG_LOG( FG_IO, FG_ALERT,
219 "in or out direction not supported for FGProps." );
227 bool FGProps::close() {
228 SGIOChannel *io = get_io_channel();
230 set_enabled( false );
232 if ( ! io->close() ) {
236 cout << "successfully closed channel\n";