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::UNSPECIFIED ) {
84 result = "unspecified";
85 } else if ( type == SGPropertyNode::NONE ) {
87 } else if ( type == SGPropertyNode::BOOL ) {
89 } else if ( type == SGPropertyNode::INT ) {
91 } else if ( type == SGPropertyNode::LONG ) {
93 } else if ( type == SGPropertyNode::FLOAT ) {
95 } else if ( type == SGPropertyNode::DOUBLE ) {
97 } else if ( type == SGPropertyNode::STRING ) {
105 bool FGProps::process_command( const char *cmd ) {
106 SGIOChannel *io = get_io_channel();
108 cout << "processing command = " << cmd;
114 while ( !in.eof() ) {
117 tokens.push_back( token );
120 string command = tokens[0];
122 SGPropertyNode * node = globals->get_props()->getNode(path);
124 if ( command == "ls" ) {
125 for (int i = 0; i < (int)node->nChildren(); i++) {
126 SGPropertyNode * child = node->getChild(i);
127 string name = child->getName();
129 if ( child->nChildren() > 0 ) {
132 string value = node->getStringValue ( name, "" );
133 line += " =\t'" + value + "'\t(";
134 line += getValueTypeString( node->getNode( name ) );
137 io->writestring( line.c_str() );
139 } else if ( command == "dump" ) {
141 if ( tokens.size() <= 1 ) {
142 writeProperties ( buf, node);
143 io->writestring( buf.str() );
146 SGPropertyNode *child = node->getNode(tokens[1]);
148 writeProperties ( buf, child );
149 io->writestring( buf.str() );
151 tokens[1] += " Not Found\n";
152 io->writestring( tokens[1].c_str() );
155 } else if ( command == "cd" ) {
156 // string tmp = "current path = " + node.getPath() + "\n";
157 // io->writestring( tmp.c_str() );
159 if ( tokens.size() <= 1 ) {
162 SGPropertyNode *child = node->getNode(tokens[1]);
165 path = node->getPath();
167 tokens[1] += " Not Found\n";
168 io->writestring( tokens[1].c_str() );
171 } else if ( command == "pwd" ) {
172 string ttt = node->getPath();
177 io->writestring( ttt.c_str() );
178 } else if ( command == "get" || command == "show" ) {
179 if ( tokens.size() <= 1 ) {
182 string ttt = "debug = '" + tokens[1] + "'\n";
183 io->writestring( ttt.c_str() );
185 string value = node->getStringValue ( tokens[1], "" );
186 string tmp = tokens[1] + " = '" + value + "' (";
187 tmp += getValueTypeString( node->getNode( tokens[1] ) );
190 io->writestring( tmp.c_str() );
192 } else if ( command == "set" ) {
193 if ( tokens.size() <= 2 ) {
196 node->getNode( tokens[1], true )->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->getNode( tokens[1] ) );
205 io->writestring( tmp.c_str() );
207 } else if ( command == "quit" ) {
212 io->writestring( "\n" );
213 io->writestring( "Valid commands are:\n" );
214 io->writestring( "\n" );
215 io->writestring( "help show help message\n" );
216 io->writestring( "ls list current directory\n" );
217 io->writestring( "dump dump current state (in xml)\n" );
218 io->writestring( "cd <dir> cd to a directory, '..' to move back\n" );
219 io->writestring( "pwd display your current path\n" );
220 io->writestring( "get <var> show the value of a parameter\n" );
221 io->writestring( "show <var> synonym for get\n" );
222 io->writestring( "set <var> <val> set <var> to a new <val>\n" );
223 io->writestring( "quit terminate connection\n" );
224 io->writestring( "\n" );
227 string prompt = node->getPath();
228 if ( prompt == "" ) {
232 io->writestring( prompt.c_str() );
238 // process work for this port
239 bool FGProps::process() {
240 SGIOChannel *io = get_io_channel();
242 // cout << "processing incoming props command" << endl;
244 if ( get_direction() == SG_IO_BI ) {
245 // cout << " (bi directional)" << endl;
246 while ( io->readline( buf, max_cmd_len ) > 0 ) {
247 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
248 process_command( buf );
251 SG_LOG( SG_IO, SG_ALERT,
252 "in or out direction not supported for FGProps." );
260 bool FGProps::close() {
261 SGIOChannel *io = get_io_channel();
263 if ( ! io->close() ) {
267 cout << "successfully closed channel\n";