]> git.mxchange.org Git - flightgear.git/blob - src/Network/props.cxx
More property node optimizations.
[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 #include <Main/globals.hxx>
25
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>
31
32 #include <stdlib.h>             // atoi() atof()
33
34 #include STL_STRSTREAM
35
36 #include "props.hxx"
37
38 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
39 SG_USING_STD(cout);
40 SG_USING_STD(istrstream);
41 #endif
42
43 FGProps::FGProps() {
44 }
45
46 FGProps::~FGProps() {
47 }
48
49
50 // open hailing frequencies
51 bool FGProps::open() {
52     path = "/";
53
54     if ( is_enabled() ) {
55         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
56                 << "is already in use, ignoring" );
57         return false;
58     }
59
60     SGIOChannel *io = get_io_channel();
61
62     if ( ! io->open( get_direction() ) ) {
63         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
64         return false;
65     }
66
67     set_enabled( true );
68     SG_LOG( SG_IO, SG_INFO, "Opening properties channel communication layer." );
69
70     return true;
71 }
72
73
74 // return a human readable form of the value "type"
75 static string getValueTypeString( const SGValue *v ) {
76     string result;
77
78     if ( v == NULL ) {
79         return "unknown";
80     }
81
82     SGValue::Type type = v->getType();
83     if ( type == SGValue::UNKNOWN ) {
84         result = "unknown";
85     } else if ( type == SGValue::BOOL ) {
86         result = "bool";
87     } else if ( type == SGValue::INT ) {
88         result = "int";
89     } else if ( type == SGValue::FLOAT ) {
90         result = "float";
91     } else if ( type == SGValue::DOUBLE ) {
92         result = "double";
93     } else if ( type == SGValue::STRING ) {
94         result = "string";
95     }
96
97     return result;
98 }
99
100
101 bool FGProps::process_command( const char *cmd ) {
102     SGIOChannel *io = get_io_channel();
103
104     cout << "processing command = " << cmd;
105     string_list tokens;
106     tokens.clear();
107
108     istrstream in(cmd);
109     
110     while ( !in.eof() ) {
111         string token;
112         in >> token;
113         tokens.push_back( token );
114     }
115
116     string command = tokens[0];
117
118     SGPropertyNode * node = globals->get_props()->getNode(path);
119
120     if ( command == "ls" ) {
121         for (int i = 0; i < (int)node->nChildren(); i++) {
122             SGPropertyNode * child = node->getChild(i);
123             string name = child->getName();
124             string line = name;
125             if ( child->nChildren() > 0 ) {
126                 line += "/\n";
127             } else {
128                 string value = node->getStringValue ( name, "" );
129                 line += " =\t'" + value + "'\t(";
130                 line += getValueTypeString( node->getValue( name ) );
131                 line += ")\n";
132             }
133             io->writestring( line.c_str() );
134         }
135     } else if ( command == "dump" ) {
136         strstream buf;
137         if ( tokens.size() <= 1 ) {
138             writeProperties ( buf, node);
139             io->writestring( buf.str() );
140         }
141         else {
142             SGPropertyNode *child = node->getNode(tokens[1]);
143             if ( child ) {
144                 writeProperties ( buf, child );
145                 io->writestring( buf.str() );
146             } else {
147                 tokens[1] += " Not Found\n";
148                 io->writestring( tokens[1].c_str() );
149             }
150         }
151     } else if ( command == "cd" ) {
152         // string tmp = "current path = " + node.getPath() + "\n";
153         // io->writestring( tmp.c_str() );
154
155         if ( tokens.size() <= 1 ) {
156             // do nothing
157         } else {
158             SGPropertyNode *child = node->getNode(tokens[1]);
159             if ( child ) {
160                 node = child;
161                 path = node->getPath();
162             } else {
163                 tokens[1] += " Not Found\n";
164                 io->writestring( tokens[1].c_str() );
165             }
166         }
167     } else if ( command == "pwd" ) {
168         string ttt = node->getPath();
169         if ( ttt == "" ) {
170             ttt = "/";
171         }
172         ttt += "\n";
173         io->writestring( ttt.c_str() );
174     } else if ( command == "get" || command == "show" ) {
175         if ( tokens.size() <= 1 ) {
176             // do nothing
177         } else {
178             string ttt = "debug = '" + tokens[1] + "'\n";
179             io->writestring( ttt.c_str() );
180
181             string value = node->getStringValue ( tokens[1], "" );
182             string tmp = tokens[1] + " = '" + value + "' (";
183             tmp += getValueTypeString( node->getValue( tokens[1] ) );
184             tmp += ")\n";
185  
186             io->writestring( tmp.c_str() );
187         }
188     } else if ( command == "set" ) {
189         if ( tokens.size() <= 2 ) {
190             // do nothing
191         } else {
192             node->getValue( tokens[1], true )->setStringValue(tokens[2]);
193
194             // now fetch and write out the new value as confirmation
195             // of the change
196             string value = node->getStringValue ( tokens[1], "" );
197             string tmp = tokens[1] + " = '" + value + "' (";
198             tmp += getValueTypeString( node->getValue( tokens[1] ) );
199             tmp += ")\n";
200  
201             io->writestring( tmp.c_str() );
202         }
203     } else if ( command == "quit" ) {
204         close();
205     } else {
206         io->writestring( "\n" );
207         io->writestring( "Valid commands are:\n" );
208         io->writestring( "\n" );
209         io->writestring( "help             show help message\n" );
210         io->writestring( "ls               list current directory\n" );
211         io->writestring( "dump             dump current state (in xml)\n" );
212         io->writestring( "cd <dir>         cd to a directory, '..' to move back\n" );
213         io->writestring( "pwd              display your current path\n" );
214         io->writestring( "get <var>        show the value of a parameter\n" );
215         io->writestring( "show <var>       synonym for get\n" );
216         io->writestring( "set <var> <val>  set <var> to a new <val>\n" );
217         io->writestring( "quit             terminate connection\n" );
218         io->writestring( "\n" );
219     }
220
221     string prompt = node->getPath();
222     if ( prompt == "" ) {
223         prompt = "/";
224     }
225     prompt += "> ";
226     io->writestring( prompt.c_str() );
227
228     return true;
229 }
230
231
232 // process work for this port
233 bool FGProps::process() {
234     SGIOChannel *io = get_io_channel();
235
236     // cout << "processing incoming props command" << endl;
237
238     if ( get_direction() == SG_IO_BI ) {
239         // cout << "  (bi directional)" << endl;
240         while ( io->readline( buf, max_cmd_len ) > 0 ) {
241             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
242             process_command( buf );
243         }
244     } else {
245         SG_LOG( SG_IO, SG_ALERT, 
246                 "in or out direction not supported for FGProps." );
247     }
248
249     return true;
250 }
251
252
253 // close the channel
254 bool FGProps::close() {
255     SGIOChannel *io = get_io_channel();
256
257     set_enabled( false );
258
259     if ( ! io->close() ) {
260         return false;
261     }
262
263     cout << "successfully closed channel\n";
264
265     return true;
266 }