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.
25 #include <simgear/compiler.h>
26 #include <simgear/debug/logstream.hxx>
27 #include <simgear/io/iochannel.hxx>
28 #include <simgear/math/sg_types.hxx>
29 #include <simgear/misc/props.hxx>
30 #include <simgear/misc/props_io.hxx>
32 #include <Main/globals.hxx>
34 #include <stdlib.h> // atoi() atof()
36 #include STL_STRSTREAM
40 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
42 SG_USING_STD(istrstream);
43 SG_USING_STD(strstream);
53 // open hailing frequencies
54 bool FGProps::open() {
58 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
59 << "is already in use, ignoring" );
63 SGIOChannel *io = get_io_channel();
65 if ( ! io->open( get_direction() ) ) {
66 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
71 SG_LOG( SG_IO, SG_INFO, "Opening properties channel communication layer." );
77 // prepare for new connection
78 bool FGProps::reset() {
85 // return a human readable form of the value "type"
86 static string getValueTypeString( const SGPropertyNode *node ) {
93 SGPropertyNode::Type type = node->getType();
94 if ( type == SGPropertyNode::UNSPECIFIED ) {
95 result = "unspecified";
96 } else if ( type == SGPropertyNode::NONE ) {
98 } else if ( type == SGPropertyNode::BOOL ) {
100 } else if ( type == SGPropertyNode::INT ) {
102 } else if ( type == SGPropertyNode::LONG ) {
104 } else if ( type == SGPropertyNode::FLOAT ) {
106 } else if ( type == SGPropertyNode::DOUBLE ) {
108 } else if ( type == SGPropertyNode::STRING ) {
116 bool FGProps::process_command( const char *cmd ) {
117 SGIOChannel *io = get_io_channel();
119 cout << "processing command = " << cmd;
125 while ( !in.eof() ) {
128 tokens.push_back( token );
131 string command = tokens[0];
133 SGPropertyNode * node = globals->get_props()->getNode(path.c_str());
135 if ( command == "ls" ) {
136 SGPropertyNode * dir = node;
137 if ( tokens.size() > 2 ) {
138 if ( tokens[1][0] == '/' ) {
139 dir = globals->get_props()->getNode(tokens[1].c_str());
141 dir = globals->get_props()->getNode((path + "/" + tokens[1]).c_str());
144 tokens[1] = "ERR Node \"" + tokens[1] + "\" not found.\n";
145 io->writestring( tokens[1].c_str() );
150 for (int i = 0; i < (int)dir->nChildren(); i++) {
151 SGPropertyNode * child = dir->getChild(i);
152 string name = child->getName();
154 if ( dir->getChild(name.c_str(), 1) ) {
156 sprintf(buf, "[%d]", child->getIndex());
159 if ( child->nChildren() > 0 ) {
162 if ( mode == PROMPT ) {
163 string value = dir->getStringValue ( name.c_str(), "" );
164 line += " =\t'" + value + "'\t(";
165 line += getValueTypeString( dir->getNode( name.c_str() ) );
170 io->writestring( line.c_str() );
172 } else if ( command == "dump" ) {
174 if ( tokens.size() <= 1 ) {
175 writeProperties ( buf, node);
176 io->writestring( buf.str() );
179 SGPropertyNode *child = node->getNode(tokens[1].c_str());
181 writeProperties ( buf, child );
182 io->writestring( buf.str() );
184 tokens[1] = "ERR Node \"" + tokens[1] + "\" not found.\n";
185 io->writestring( tokens[1].c_str() );
188 } else if ( command == "cd" ) {
189 // string tmp = "current path = " + node.getPath() + "\n";
190 // io->writestring( tmp.c_str() );
192 if ( tokens.size() <= 1 ) {
195 SGPropertyNode *child = node->getNode(tokens[1].c_str());
198 path = node->getPath();
200 tokens[1] = "ERR Node \"" + tokens[1] + "\" not found.\n";
201 io->writestring( tokens[1].c_str() );
204 } else if ( command == "pwd" ) {
205 string ttt = node->getPath();
210 io->writestring( ttt.c_str() );
211 } else if ( command == "get" || command == "show" ) {
212 if ( tokens.size() <= 1 ) {
216 string value = node->getStringValue ( tokens[1].c_str(), "" );
217 if ( mode == PROMPT ) {
218 //string ttt = "debug = '" + tokens[1] + "'\n";
219 //io->writestring( ttt.c_str() );
221 tmp = tokens[1] + " = '" + value + "' (";
222 tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
227 io->writestring( tmp.c_str() );
229 } else if ( command == "set" ) {
230 if ( tokens.size() <= 2 ) {
233 node->getNode( tokens[1].c_str(), true )->setStringValue(tokens[2].c_str());
235 // now fetch and write out the new value as confirmation
237 string value = node->getStringValue ( tokens[1].c_str(), "" );
238 string tmp = tokens[1] + " = '" + value + "' (";
239 tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
242 io->writestring( tmp.c_str() );
244 } else if ( command == "quit" ) {
248 } else if ( command == "data" ) {
250 } else if ( command == "prompt" ) {
253 io->writestring( "\n" );
254 io->writestring( "Valid commands are:\n" );
255 io->writestring( "\n" );
256 io->writestring( "help show help message\n" );
257 io->writestring( "ls [<dir>] list directory\n" );
258 io->writestring( "dump dump current state (in xml)\n" );
259 io->writestring( "cd <dir> cd to a directory, '..' to move back\n" );
260 io->writestring( "pwd display your current path\n" );
261 io->writestring( "get <var> show the value of a parameter\n" );
262 io->writestring( "show <var> synonym for get\n" );
263 io->writestring( "set <var> <val> set <var> to a new <val>\n" );
264 io->writestring( "data switch to raw data mode\n" );
265 io->writestring( "prompt switch to interactive mode (default)\n" );
266 io->writestring( "quit terminate connection\n" );
267 io->writestring( "\n" );
270 if ( mode == PROMPT ) {
271 string prompt = node->getPath();
272 if ( prompt.empty() ) {
276 io->writestring( prompt.c_str() );
282 // process work for this port
283 bool FGProps::process() {
284 SGIOChannel *io = get_io_channel();
285 char buf[max_cmd_len];
287 // cout << "processing incoming props command" << endl;
289 if ( get_direction() == SG_IO_BI ) {
290 // cout << " (bi directional)" << endl;
291 while ( io->readline( buf, max_cmd_len ) > 0 ) {
292 SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
293 process_command( buf );
296 SG_LOG( SG_IO, SG_ALERT,
297 "in or out direction not supported for FGProps." );
305 bool FGProps::close() {
306 SGIOChannel *io = get_io_channel();
308 if ( ! io->close() ) {
312 cout << "successfully closed channel\n";