]> git.mxchange.org Git - flightgear.git/blob - src/Network/props.cxx
Oops, fixed a typo.
[flightgear.git] / src / Network / props.cxx
1 // props.hxx -- FGFS property manager interaction class
2 //
3 // Written by Curtis Olson, started September 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24
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>
31
32 #include <Main/globals.hxx>
33
34 #include <stdlib.h>             // atoi() atof()
35
36 #include STL_STRSTREAM
37
38 #include "props.hxx"
39
40 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
41 SG_USING_STD(cout);
42 SG_USING_STD(istrstream);
43 SG_USING_STD(strstream);
44 #endif
45
46 FGProps::FGProps() {
47 }
48
49 FGProps::~FGProps() {
50 }
51
52
53 // open hailing frequencies
54 bool FGProps::open() {
55     reset();
56
57     if ( is_enabled() ) {
58         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
59                 << "is already in use, ignoring" );
60         return false;
61     }
62
63     SGIOChannel *io = get_io_channel();
64
65     if ( ! io->open( get_direction() ) ) {
66         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
67         return false;
68     }
69
70     set_enabled( true );
71     SG_LOG( SG_IO, SG_INFO, "Opening properties channel communication layer." );
72
73     return true;
74 }
75
76
77 // prepare for new connection
78 bool FGProps::reset() {
79     path = "/";
80     mode = PROMPT;
81     return true;
82 }
83
84
85 // return a human readable form of the value "type"
86 static string getValueTypeString( const SGPropertyNode *node ) {
87     string result;
88
89     if ( node == NULL ) {
90         return "unknown";
91     }
92
93     SGPropertyNode::Type type = node->getType();
94     if ( type == SGPropertyNode::UNSPECIFIED ) {
95         result = "unspecified";
96     } else if ( type == SGPropertyNode::NONE ) {
97         result = "none";
98     } else if ( type == SGPropertyNode::BOOL ) {
99         result = "bool";
100     } else if ( type == SGPropertyNode::INT ) {
101         result = "int";
102     } else if ( type == SGPropertyNode::LONG ) {
103         result = "long";
104     } else if ( type == SGPropertyNode::FLOAT ) {
105         result = "float";
106     } else if ( type == SGPropertyNode::DOUBLE ) {
107         result = "double";
108     } else if ( type == SGPropertyNode::STRING ) {
109         result = "string";
110     }
111
112     return result;
113 }
114
115
116 bool FGProps::process_command( const char *cmd ) {
117     SGIOChannel *io = get_io_channel();
118
119     cout << "processing command = " << cmd;
120     string_list tokens;
121     tokens.clear();
122
123     istrstream in(cmd);
124     
125     while ( !in.eof() ) {
126         string token;
127         in >> token;
128         tokens.push_back( token );
129     }
130
131     string command = tokens[0];
132
133     SGPropertyNode * node = globals->get_props()->getNode(path.c_str());
134
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());
140             } else {
141                 dir = globals->get_props()->getNode((path + "/" + tokens[1]).c_str());
142             }
143             if ( dir == 0 ) {
144                 tokens[1] = "ERR Node \"" + tokens[1] + "\" not found.\n";
145                 io->writestring( tokens[1].c_str() );
146                 return true;
147             }
148         }
149         
150         for (int i = 0; i < (int)dir->nChildren(); i++) {
151             SGPropertyNode * child = dir->getChild(i);
152             string name = child->getName();
153             string line = name;
154             if ( dir->getChild(name.c_str(), 1) ) {
155                 char buf[16];
156                 sprintf(buf, "[%d]", child->getIndex());
157                 line += buf;
158             }
159             if ( child->nChildren() > 0 ) {
160                 line += "/";
161             } else {
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() ) );
166                     line += ")";
167                 }
168             }
169             line += "\n";
170             io->writestring( line.c_str() );
171         }
172     } else if ( command == "dump" ) {
173         strstream buf;
174         if ( tokens.size() <= 1 ) {
175             writeProperties ( buf, node);
176             io->writestring( buf.str() );
177         }
178         else {
179             SGPropertyNode *child = node->getNode(tokens[1].c_str());
180             if ( child ) {
181                 writeProperties ( buf, child );
182                 io->writestring( buf.str() );
183             } else {
184                 tokens[1] = "ERR Node \"" + tokens[1] + "\" not found.\n";
185                 io->writestring( tokens[1].c_str() );
186             }
187         }
188     } else if ( command == "cd" ) {
189         // string tmp = "current path = " + node.getPath() + "\n";
190         // io->writestring( tmp.c_str() );
191
192         if ( tokens.size() <= 1 ) {
193             // do nothing
194         } else {
195             SGPropertyNode *child = node->getNode(tokens[1].c_str());
196             if ( child ) {
197                 node = child;
198                 path = node->getPath();
199             } else {
200                 tokens[1] = "ERR Node \"" + tokens[1] + "\" not found.\n";
201                 io->writestring( tokens[1].c_str() );
202             }
203         }
204     } else if ( command == "pwd" ) {
205         string ttt = node->getPath();
206         if ( ttt.empty() ) {
207             ttt = "/";
208         }
209         ttt += "\n";
210         io->writestring( ttt.c_str() );
211     } else if ( command == "get" || command == "show" ) {
212         if ( tokens.size() <= 1 ) {
213             // do nothing
214         } else {
215             string tmp; 
216             string value = node->getStringValue ( tokens[1].c_str(), "" );
217             if ( mode == PROMPT ) {
218                 tmp = tokens[1] + " = '" + value + "' (";
219                 tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
220                 tmp += ")\n";
221             } else {
222                 tmp = value + "\n";
223             }
224             io->writestring( tmp.c_str() );
225         }
226     } else if ( command == "set" ) {
227         if ( tokens.size() <= 2 ) {
228             // do nothing
229         } else {
230             string tmp = tokens[2];
231             for ( unsigned int i = 3; i < tokens.size() - 1; i++ ) {
232                 tmp += " " + tokens[i];
233             }
234             node->getNode( tokens[1].c_str(), true )->setStringValue(tmp.c_str());
235
236             if ( mode == PROMPT ) {
237                 // now fetch and write out the new value as confirmation
238                 // of the change
239                 string value = node->getStringValue ( tokens[1].c_str(), "" );
240                 string tmp = tokens[1] + " = '" + value + "' (";
241                 tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
242                 tmp += ")\n";
243
244                 io->writestring( tmp.c_str() );
245             }
246         }
247     } else if ( command == "quit" ) {
248         close();
249         reset();
250         return true;
251     } else if ( command == "data" ) {
252         mode = DATA;
253     } else if ( command == "prompt" ) {
254         mode = PROMPT;
255     } else {
256         io->writestring( "\n" );
257         io->writestring( "Valid commands are:\n" );
258         io->writestring( "\n" );
259         io->writestring( "help             show help message\n" );
260         io->writestring( "ls [<dir>]       list directory\n" );
261         io->writestring( "dump             dump current state (in xml)\n" );
262         io->writestring( "cd <dir>         cd to a directory, '..' to move back\n" );
263         io->writestring( "pwd              display your current path\n" );
264         io->writestring( "get <var>        show the value of a parameter\n" );
265         io->writestring( "show <var>       synonym for get\n" );
266         io->writestring( "set <var> <val>  set <var> to a new <val>\n" );
267         io->writestring( "data             switch to raw data mode\n" );
268         io->writestring( "prompt           switch to interactive mode (default)\n" );
269         io->writestring( "quit             terminate connection\n" );
270         io->writestring( "\n" );
271     }
272
273     if ( mode == PROMPT ) {
274         string prompt = node->getPath();
275         if ( prompt.empty() ) {
276             prompt = "/";
277         }
278         prompt += "> ";
279         io->writestring( prompt.c_str() );
280     }
281     return true;
282 }
283
284
285 // process work for this port
286 bool FGProps::process() {
287     SGIOChannel *io = get_io_channel();
288     char buf[max_cmd_len];
289
290     // cout << "processing incoming props command" << endl;
291
292     if ( get_direction() == SG_IO_BI ) {
293         // cout << "  (bi directional)" << endl;
294         while ( io->readline( buf, max_cmd_len ) > 0 ) {
295             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
296             process_command( buf );
297         }
298     } else {
299         SG_LOG( SG_IO, SG_ALERT, 
300                 "in or out direction not supported for FGProps." );
301     }
302
303     return true;
304 }
305
306
307 // close the channel
308 bool FGProps::close() {
309     SGIOChannel *io = get_io_channel();
310
311     if ( ! io->close() ) {
312         return false;
313     }
314
315     cout << "successfully closed channel\n";
316
317     return true;
318 }