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 // return a human readable form of the value "type"
75 static string getValueTypeString( const SGPropertyNode *node ) {
82 SGPropertyNode::Type type = node->getType();
83 if ( type == SGPropertyNode::UNKNOWN ) {
85 } else if ( type == SGPropertyNode::BOOL ) {
87 } else if ( type == SGPropertyNode::INT ) {
89 } else if ( type == SGPropertyNode::LONG ) {
91 } else if ( type == SGPropertyNode::FLOAT ) {
93 } else if ( type == SGPropertyNode::DOUBLE ) {
95 } else if ( type == SGPropertyNode::STRING ) {
103 bool FGProps::process_command( const char *cmd ) {
104 SGIOChannel *io = get_io_channel();
106 cout << "processing command = " << cmd;
112 while ( !in.eof() ) {
115 tokens.push_back( token );
118 string command = tokens[0];
120 SGPropertyNode * node = globals->get_props()->getNode(path);
122 if ( command == "ls" ) {
123 for (int i = 0; i < (int)node->nChildren(); i++) {
124 SGPropertyNode * child = node->getChild(i);
125 string name = child->getName();
127 if ( child->nChildren() > 0 ) {
130 string value = node->getStringValue ( name, "" );
131 line += " =\t'" + value + "'\t(";
132 line += getValueTypeString( node->getNode( name ) );
135 io->writestring( line.c_str() );
137 } else if ( command == "dump" ) {
139 if ( tokens.size() <= 1 ) {
140 writeProperties ( buf, node);
141 io->writestring( buf.str() );
144 SGPropertyNode *child = node->getNode(tokens[1]);
146 writeProperties ( buf, child );
147 io->writestring( buf.str() );
149 tokens[1] += " Not Found\n";
150 io->writestring( tokens[1].c_str() );
153 } else if ( command == "cd" ) {
154 // string tmp = "current path = " + node.getPath() + "\n";
155 // io->writestring( tmp.c_str() );
157 if ( tokens.size() <= 1 ) {
160 SGPropertyNode *child = node->getNode(tokens[1]);
163 path = node->getPath();
165 tokens[1] += " Not Found\n";
166 io->writestring( tokens[1].c_str() );
169 } else if ( command == "pwd" ) {
170 string ttt = node->getPath();
175 io->writestring( ttt.c_str() );
176 } else if ( command == "get" || command == "show" ) {
177 if ( tokens.size() <= 1 ) {
180 string ttt = "debug = '" + tokens[1] + "'\n";
181 io->writestring( ttt.c_str() );
183 string value = node->getStringValue ( tokens[1], "" );
184 string tmp = tokens[1] + " = '" + value + "' (";
185 tmp += getValueTypeString( node->getNode( tokens[1] ) );
188 io->writestring( tmp.c_str() );
190 } else if ( command == "set" ) {
191 if ( tokens.size() <= 2 ) {
194 node->getNode( tokens[1], true )->setStringValue(tokens[2]);
196 // now fetch and write out the new value as confirmation
198 string value = node->getStringValue ( tokens[1], "" );
199 string tmp = tokens[1] + " = '" + value + "' (";
200 tmp += getValueTypeString( node->getNode( tokens[1] ) );
203 io->writestring( tmp.c_str() );
205 } else if ( command == "quit" ) {
208 io->writestring( "\n" );
209 io->writestring( "Valid commands are:\n" );
210 io->writestring( "\n" );
211 io->writestring( "help show help message\n" );
212 io->writestring( "ls list current directory\n" );
213 io->writestring( "dump dump current state (in xml)\n" );
214 io->writestring( "cd <dir> cd to a directory, '..' to move back\n" );
215 io->writestring( "pwd display your current path\n" );
216 io->writestring( "get <var> show the value of a parameter\n" );
217 io->writestring( "show <var> synonym for get\n" );
218 io->writestring( "set <var> <val> set <var> to a new <val>\n" );
219 io->writestring( "quit terminate connection\n" );
220 io->writestring( "\n" );
223 string prompt = node->getPath();
224 if ( prompt == "" ) {
228 io->writestring( prompt.c_str() );
234 // process work for this port
235 bool FGProps::process() {
236 SGIOChannel *io = get_io_channel();
238 // cout << "processing incoming props command" << endl;
240 if ( get_direction() == SG_IO_BI ) {
241 // cout << " (bi directional)" << endl;
242 while ( io->readline( buf, max_cmd_len ) > 0 ) {
243 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
244 process_command( buf );
247 SG_LOG( SG_IO, SG_ALERT,
248 "in or out direction not supported for FGProps." );
256 bool FGProps::close() {
257 SGIOChannel *io = get_io_channel();
259 set_enabled( false );
261 if ( ! io->close() ) {
265 cout << "successfully closed channel\n";