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()
34 #include STL_STRSTREAM
38 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
40 SG_USING_STD(istrstream);
50 // open hailing frequencies
51 bool FGProps::open() {
55 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
56 << "is already in use, ignoring" );
60 SGIOChannel *io = get_io_channel();
62 if ( ! io->open( get_direction() ) ) {
63 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
68 SG_LOG( SG_IO, SG_INFO, "Opening properties channel communication layer." );
74 // prepare for new connection
75 bool FGProps::reset() {
82 // return a human readable form of the value "type"
83 static string getValueTypeString( const SGPropertyNode *node ) {
90 SGPropertyNode::Type type = node->getType();
91 if ( type == SGPropertyNode::UNSPECIFIED ) {
92 result = "unspecified";
93 } else if ( type == SGPropertyNode::NONE ) {
95 } else if ( type == SGPropertyNode::BOOL ) {
97 } else if ( type == SGPropertyNode::INT ) {
99 } else if ( type == SGPropertyNode::LONG ) {
101 } else if ( type == SGPropertyNode::FLOAT ) {
103 } else if ( type == SGPropertyNode::DOUBLE ) {
105 } else if ( type == SGPropertyNode::STRING ) {
113 bool FGProps::process_command( const char *cmd ) {
114 SGIOChannel *io = get_io_channel();
116 cout << "processing command = " << cmd;
122 while ( !in.eof() ) {
125 tokens.push_back( token );
128 string command = tokens[0];
130 SGPropertyNode * node = globals->get_props()->getNode(path);
132 if ( command == "ls" ) {
133 SGPropertyNode * dir = node;
134 if ( tokens.size() > 2 ) {
135 if ( tokens[1][0] == '/' ) {
136 dir = globals->get_props()->getNode(tokens[1]);
138 dir = globals->get_props()->getNode(path + "/" + tokens[1]);
141 tokens[1] = "ERR Node \"" + tokens[1] + "\" not found.\n";
142 io->writestring( tokens[1].c_str() );
147 for (int i = 0; i < (int)dir->nChildren(); i++) {
148 SGPropertyNode * child = dir->getChild(i);
149 string name = child->getName();
151 if ( dir->getChild(name, 1) ) {
153 sprintf(buf, "[%d]", child->getIndex());
156 if ( child->nChildren() > 0 ) {
159 if ( mode == PROMPT ) {
160 string value = dir->getStringValue ( name, "" );
161 line += " =\t'" + value + "'\t(";
162 line += getValueTypeString( dir->getNode( name ) );
167 io->writestring( line.c_str() );
169 } else if ( command == "dump" ) {
171 if ( tokens.size() <= 1 ) {
172 writeProperties ( buf, node);
173 io->writestring( buf.str() );
176 SGPropertyNode *child = node->getNode(tokens[1]);
178 writeProperties ( buf, child );
179 io->writestring( buf.str() );
181 tokens[1] = "ERR Node \"" + tokens[1] + "\" not found.\n";
182 io->writestring( tokens[1].c_str() );
185 } else if ( command == "cd" ) {
186 // string tmp = "current path = " + node.getPath() + "\n";
187 // io->writestring( tmp.c_str() );
189 if ( tokens.size() <= 1 ) {
192 SGPropertyNode *child = node->getNode(tokens[1]);
195 path = node->getPath();
197 tokens[1] = "ERR Node \"" + tokens[1] + "\" not found.\n";
198 io->writestring( tokens[1].c_str() );
201 } else if ( command == "pwd" ) {
202 string ttt = node->getPath();
207 io->writestring( ttt.c_str() );
208 } else if ( command == "get" || command == "show" ) {
209 if ( tokens.size() <= 1 ) {
213 string value = node->getStringValue ( tokens[1], "" );
214 if ( mode == PROMPT ) {
215 //string ttt = "debug = '" + tokens[1] + "'\n";
216 //io->writestring( ttt.c_str() );
218 tmp = tokens[1] + " = '" + value + "' (";
219 tmp += getValueTypeString( node->getNode( tokens[1] ) );
224 io->writestring( tmp.c_str() );
226 } else if ( command == "set" ) {
227 if ( tokens.size() <= 2 ) {
230 node->getNode( tokens[1], true )->setStringValue(tokens[2]);
232 // now fetch and write out the new value as confirmation
234 string value = node->getStringValue ( tokens[1], "" );
235 string tmp = tokens[1] + " = '" + value + "' (";
236 tmp += getValueTypeString( node->getNode( tokens[1] ) );
239 io->writestring( tmp.c_str() );
241 } else if ( command == "quit" ) {
245 } else if ( command == "data" ) {
247 } else if ( command == "prompt" ) {
250 io->writestring( "\n" );
251 io->writestring( "Valid commands are:\n" );
252 io->writestring( "\n" );
253 io->writestring( "help show help message\n" );
254 io->writestring( "ls [<dir>] list directory\n" );
255 io->writestring( "dump dump current state (in xml)\n" );
256 io->writestring( "cd <dir> cd to a directory, '..' to move back\n" );
257 io->writestring( "pwd display your current path\n" );
258 io->writestring( "get <var> show the value of a parameter\n" );
259 io->writestring( "show <var> synonym for get\n" );
260 io->writestring( "set <var> <val> set <var> to a new <val>\n" );
261 io->writestring( "data switch to raw data mode\n" );
262 io->writestring( "prompt switch to interactive mode (default)\n" );
263 io->writestring( "quit terminate connection\n" );
264 io->writestring( "\n" );
267 if ( mode == PROMPT ) {
268 string prompt = node->getPath();
269 if ( prompt == "" ) {
273 io->writestring( prompt.c_str() );
279 // process work for this port
280 bool FGProps::process() {
281 SGIOChannel *io = get_io_channel();
282 char buf[max_cmd_len];
284 // cout << "processing incoming props command" << endl;
286 if ( get_direction() == SG_IO_BI ) {
287 // cout << " (bi directional)" << endl;
288 while ( io->readline( buf, max_cmd_len ) > 0 ) {
289 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
290 process_command( buf );
293 SG_LOG( SG_IO, SG_ALERT,
294 "in or out direction not supported for FGProps." );
302 bool FGProps::close() {
303 SGIOChannel *io = get_io_channel();
305 if ( ! io->close() ) {
309 cout << "successfully closed channel\n";