]> git.mxchange.org Git - flightgear.git/blob - src/Network/props.cxx
Substantial rewrite of FGNewMat, the material class. Most of the
[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 SGPropertyNode *node ) {
76     string result;
77
78     if ( node == NULL ) {
79         return "unknown";
80     }
81
82     SGPropertyNode::Type type = node->getType();
83     if ( type == SGPropertyNode::UNSPECIFIED ) {
84         result = "unspecified";
85     } else if ( type == SGPropertyNode::NONE ) {
86         result = "none";
87     } else if ( type == SGPropertyNode::BOOL ) {
88         result = "bool";
89     } else if ( type == SGPropertyNode::INT ) {
90         result = "int";
91     } else if ( type == SGPropertyNode::LONG ) {
92         result = "long";
93     } else if ( type == SGPropertyNode::FLOAT ) {
94         result = "float";
95     } else if ( type == SGPropertyNode::DOUBLE ) {
96         result = "double";
97     } else if ( type == SGPropertyNode::STRING ) {
98         result = "string";
99     }
100
101     return result;
102 }
103
104
105 bool FGProps::process_command( const char *cmd ) {
106     SGIOChannel *io = get_io_channel();
107
108     cout << "processing command = " << cmd;
109     string_list tokens;
110     tokens.clear();
111
112     istrstream in(cmd);
113     
114     while ( !in.eof() ) {
115         string token;
116         in >> token;
117         tokens.push_back( token );
118     }
119
120     string command = tokens[0];
121
122     SGPropertyNode * node = globals->get_props()->getNode(path);
123
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();
128             string line = name;
129             if ( child->nChildren() > 0 ) {
130                 line += "/\n";
131             } else {
132                 string value = node->getStringValue ( name, "" );
133                 line += " =\t'" + value + "'\t(";
134                 line += getValueTypeString( node->getNode( name ) );
135                 line += ")\n";
136             }
137             io->writestring( line.c_str() );
138         }
139     } else if ( command == "dump" ) {
140         strstream buf;
141         if ( tokens.size() <= 1 ) {
142             writeProperties ( buf, node);
143             io->writestring( buf.str() );
144         }
145         else {
146             SGPropertyNode *child = node->getNode(tokens[1]);
147             if ( child ) {
148                 writeProperties ( buf, child );
149                 io->writestring( buf.str() );
150             } else {
151                 tokens[1] += " Not Found\n";
152                 io->writestring( tokens[1].c_str() );
153             }
154         }
155     } else if ( command == "cd" ) {
156         // string tmp = "current path = " + node.getPath() + "\n";
157         // io->writestring( tmp.c_str() );
158
159         if ( tokens.size() <= 1 ) {
160             // do nothing
161         } else {
162             SGPropertyNode *child = node->getNode(tokens[1]);
163             if ( child ) {
164                 node = child;
165                 path = node->getPath();
166             } else {
167                 tokens[1] += " Not Found\n";
168                 io->writestring( tokens[1].c_str() );
169             }
170         }
171     } else if ( command == "pwd" ) {
172         string ttt = node->getPath();
173         if ( ttt == "" ) {
174             ttt = "/";
175         }
176         ttt += "\n";
177         io->writestring( ttt.c_str() );
178     } else if ( command == "get" || command == "show" ) {
179         if ( tokens.size() <= 1 ) {
180             // do nothing
181         } else {
182             string ttt = "debug = '" + tokens[1] + "'\n";
183             io->writestring( ttt.c_str() );
184
185             string value = node->getStringValue ( tokens[1], "" );
186             string tmp = tokens[1] + " = '" + value + "' (";
187             tmp += getValueTypeString( node->getNode( tokens[1] ) );
188             tmp += ")\n";
189  
190             io->writestring( tmp.c_str() );
191         }
192     } else if ( command == "set" ) {
193         if ( tokens.size() <= 2 ) {
194             // do nothing
195         } else {
196             node->getNode( tokens[1], true )->setStringValue(tokens[2]);
197
198             // now fetch and write out the new value as confirmation
199             // of the change
200             string value = node->getStringValue ( tokens[1], "" );
201             string tmp = tokens[1] + " = '" + value + "' (";
202             tmp += getValueTypeString( node->getNode( tokens[1] ) );
203             tmp += ")\n";
204  
205             io->writestring( tmp.c_str() );
206         }
207     } else if ( command == "quit" ) {
208         close();
209     } else {
210         io->writestring( "\n" );
211         io->writestring( "Valid commands are:\n" );
212         io->writestring( "\n" );
213         io->writestring( "help             show help message\n" );
214         io->writestring( "ls               list current directory\n" );
215         io->writestring( "dump             dump current state (in xml)\n" );
216         io->writestring( "cd <dir>         cd to a directory, '..' to move back\n" );
217         io->writestring( "pwd              display your current path\n" );
218         io->writestring( "get <var>        show the value of a parameter\n" );
219         io->writestring( "show <var>       synonym for get\n" );
220         io->writestring( "set <var> <val>  set <var> to a new <val>\n" );
221         io->writestring( "quit             terminate connection\n" );
222         io->writestring( "\n" );
223     }
224
225     string prompt = node->getPath();
226     if ( prompt == "" ) {
227         prompt = "/";
228     }
229     prompt += "> ";
230     io->writestring( prompt.c_str() );
231
232     return true;
233 }
234
235
236 // process work for this port
237 bool FGProps::process() {
238     SGIOChannel *io = get_io_channel();
239
240     // cout << "processing incoming props command" << endl;
241
242     if ( get_direction() == SG_IO_BI ) {
243         // cout << "  (bi directional)" << endl;
244         while ( io->readline( buf, max_cmd_len ) > 0 ) {
245             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
246             process_command( buf );
247         }
248     } else {
249         SG_LOG( SG_IO, SG_ALERT, 
250                 "in or out direction not supported for FGProps." );
251     }
252
253     return true;
254 }
255
256
257 // close the channel
258 bool FGProps::close() {
259     SGIOChannel *io = get_io_channel();
260
261     set_enabled( false );
262
263     if ( ! io->close() ) {
264         return false;
265     }
266
267     cout << "successfully closed channel\n";
268
269     return true;
270 }