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." );
66 FG_LOG( FG_IO, FG_INFO, "Opening properties channel communication layer." );
72 // return a human readable form of the value "type"
73 static string getValueTypeString( const SGValue *v ) {
80 SGValue::Type type = v->getType();
81 if ( type == SGValue::UNKNOWN ) {
83 } else if ( type == SGValue::BOOL ) {
85 } else if ( type == SGValue::INT ) {
87 } else if ( type == SGValue::FLOAT ) {
89 } else if ( type == SGValue::DOUBLE ) {
91 } else if ( type == SGValue::STRING ) {
99 bool FGProps::process_command( const char *cmd ) {
100 SGIOChannel *io = get_io_channel();
102 cout << "processing command = " << cmd;
108 while ( !in.eof() ) {
111 tokens.push_back( token );
114 string command = tokens[0];
116 SGPropertyNode * node = globals->get_props()->getNode(path);
118 if ( command == "ls" ) {
119 for (int i = 0; i < (int)node->nChildren(); i++) {
120 SGPropertyNode * child = node->getChild(i);
121 string name = child->getName();
123 if ( child->nChildren() > 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 == "dump" ) {
135 if ( tokens.size() <= 1 ) {
136 writeProperties ( buf, node);
137 io->writestring( buf.str() );
140 SGPropertyNode *child = node->getNode(tokens[1]);
142 writeProperties ( buf, child );
143 io->writestring( buf.str() );
145 tokens[1] += " Not Found\n";
146 io->writestring( tokens[1].c_str() );
149 } else if ( command == "cd" ) {
150 // string tmp = "current path = " + node.getPath() + "\n";
151 // io->writestring( tmp.c_str() );
153 if ( tokens.size() <= 1 ) {
156 SGPropertyNode *child = node->getNode(tokens[1]);
159 path = node->getPath();
161 tokens[1] += " Not Found\n";
162 io->writestring( tokens[1].c_str() );
165 } else if ( command == "pwd" ) {
166 string ttt = node->getPath();
171 io->writestring( ttt.c_str() );
172 } else if ( command == "get" || command == "show" ) {
173 if ( tokens.size() <= 1 ) {
176 string ttt = "debug = '" + tokens[1] + "'\n";
177 io->writestring( ttt.c_str() );
179 string value = node->getStringValue ( tokens[1], "" );
180 string tmp = tokens[1] + " = '" + value + "' (";
181 tmp += getValueTypeString( node->getValue( tokens[1] ) );
184 io->writestring( tmp.c_str() );
186 } else if ( command == "set" ) {
187 if ( tokens.size() <= 2 ) {
190 node->getValue( tokens[1], true )->setStringValue(tokens[2]);
192 // now fetch and write out the new value as confirmation
194 string value = node->getStringValue ( tokens[1], "" );
195 string tmp = tokens[1] + " = '" + value + "' (";
196 tmp += getValueTypeString( node->getValue( tokens[1] ) );
199 io->writestring( tmp.c_str() );
201 } else if ( command == "quit" ) {
204 io->writestring( "\n" );
205 io->writestring( "Valid commands are:\n" );
206 io->writestring( "\n" );
207 io->writestring( "help show help message\n" );
208 io->writestring( "ls list current directory\n" );
209 io->writestring( "dump dump current state (in xml)\n" );
210 io->writestring( "cd <dir> cd to a directory, '..' to move back\n" );
211 io->writestring( "pwd display your current path\n" );
212 io->writestring( "get <var> show the value of a parameter\n" );
213 io->writestring( "show <var> synonym for get\n" );
214 io->writestring( "set <var> <val> set <var> to a new <val>\n" );
215 io->writestring( "quit terminate connection\n" );
216 io->writestring( "\n" );
219 string prompt = node->getPath();
220 if ( prompt == "" ) {
224 io->writestring( prompt.c_str() );
230 // process work for this port
231 bool FGProps::process() {
232 SGIOChannel *io = get_io_channel();
234 // cout << "processing incoming props command" << endl;
236 if ( get_direction() == SG_IO_BI ) {
237 // cout << " (bi directional)" << endl;
238 while ( io->readline( buf, max_cmd_len ) > 0 ) {
239 FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
240 process_command( buf );
243 FG_LOG( FG_IO, FG_ALERT,
244 "in or out direction not supported for FGProps." );
252 bool FGProps::close() {
253 SGIOChannel *io = get_io_channel();
255 set_enabled( false );
257 if ( ! io->close() ) {
261 cout << "successfully closed channel\n";