]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGPropertyManager.cpp
2a33172ad2bf63423a85031ea5baf403031c3912
[flightgear.git] / src / FDM / JSBSim / FGPropertyManager.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2  
3  Header:       FGPropertyManager.cpp
4  Author:       Tony Peden
5                Based on work originally by David Megginson
6  Date:         2/2002
7  
8  ------------- Copyright (C) 2002 -------------
9  
10  This program is free software; you can redistribute it and/or modify it under
11  the terms of the GNU General Public License as published by the Free Software
12  Foundation; either version 2 of the License, or (at your option) any later
13  version.
14  
15  This program is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  details.
19  
20  You should have received a copy of the GNU General Public License along with
21  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  Place - Suite 330, Boston, MA  02111-1307, USA.
23
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
28 INCLUDES
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
30
31 #include <simgear/misc/props.hxx>
32 #include "FGPropertyManager.h"
33
34 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 DEFINITIONS
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
37
38
39 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 FORWARD DECLARATIONS
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42
43 using namespace std;
44
45 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 */
49
50 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51
52 string FGPropertyManager::mkPropertyName(string name, bool lowercase) {
53   
54   /* do this two pass to avoid problems with characters getting skipped
55      because the index changed */
56
57   for(unsigned i=0;i<name.length();i++) {
58     if( lowercase && isupper(name[i]) )
59       name[i]=tolower(name[i]);
60     else if( isspace(name[i]) ) 
61       name[i]='-';
62   }
63   for(unsigned i=0;i<name.length();i++) {
64     if( name[i] == '/' )
65       name.erase(i,1);  
66   }
67
68   return name;
69 }
70
71 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72
73 FGPropertyManager*  
74 FGPropertyManager::GetNode (const string &path, bool create)
75 {
76   SGPropertyNode* node=this->getNode(path.c_str(), create);
77   if(node == 0) 
78     cout << "FGPropertyManager::GetNode() No node found for " 
79          << path << endl;
80   return (FGPropertyManager*)node;
81 }
82
83 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84
85 FGPropertyManager* 
86 FGPropertyManager::GetNode (const string &relpath, int index, bool create)
87 {
88     return (FGPropertyManager*)getNode(relpath.c_str(),index,create);
89 }    
90
91 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92
93
94 bool FGPropertyManager::HasNode (const string &path)
95 {
96   return (GetNode(path, false) != 0);
97 }
98
99 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100
101 string FGPropertyManager::GetName( void ) {
102   return string( getName() );
103 }  
104
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106
107 string FGPropertyManager::GetFullyQualifiedName(void) {
108     vector<string> stack;
109     stack.push_back( getDisplayName(true) );
110     SGPropertyNode* tmpn=getParent();
111     bool atroot=false;
112     while( !atroot ) {
113      stack.push_back( tmpn->getDisplayName(true) );
114      if( !tmpn->getParent() ) 
115       atroot=true;
116      else 
117       tmpn=tmpn->getParent();
118     }
119     
120     string fqname="";
121     for(unsigned i=stack.size()-1;i>0;i--) {
122       fqname+= stack[i];
123       fqname+= "/";
124     }
125     fqname+= stack[0];
126     return fqname;  
127        
128 }    
129     
130     
131 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132
133 bool FGPropertyManager::GetBool (const string &name, bool defaultValue)
134 {
135   return getBoolValue(name.c_str(), defaultValue);
136 }
137
138 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139
140 int FGPropertyManager::GetInt (const string &name, int defaultValue )
141 {
142   return getIntValue(name.c_str(), defaultValue);
143 }
144
145 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146
147 int FGPropertyManager::GetLong (const string &name, long defaultValue )
148 {
149   return getLongValue(name.c_str(), defaultValue);
150 }
151
152 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153
154 float FGPropertyManager::GetFloat (const string &name, float defaultValue )
155 {
156   return getFloatValue(name.c_str(), defaultValue);
157 }
158
159 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160
161 double FGPropertyManager::GetDouble (const string &name, double defaultValue )
162 {
163   return getDoubleValue(name.c_str(), defaultValue);
164 }
165
166 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167
168 string FGPropertyManager::GetString (const string &name, string defaultValue )
169 {
170   return string(getStringValue(name.c_str(), defaultValue.c_str()));
171 }
172
173 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174
175 bool FGPropertyManager::SetBool (const string &name, bool val)
176 {
177   return setBoolValue(name.c_str(), val);
178 }
179
180 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181
182 bool FGPropertyManager::SetInt (const string &name, int val)
183 {
184   return setIntValue(name.c_str(), val);
185 }
186
187 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188
189 bool FGPropertyManager::SetLong (const string &name, long val)
190 {
191   return setLongValue(name.c_str(), val);
192 }
193
194 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195
196 bool FGPropertyManager::SetFloat (const string &name, float val)
197 {
198   return setFloatValue(name.c_str(), val);
199 }
200
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202
203 bool FGPropertyManager::SetDouble (const string &name, double val)
204 {
205   return setDoubleValue(name.c_str(), val);
206 }
207
208 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209
210 bool FGPropertyManager::SetString (const string &name, const string &val)
211 {
212   return setStringValue(name.c_str(), val.c_str());
213 }
214
215 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216
217 void FGPropertyManager::SetArchivable (const string &name, bool state )
218 {
219   SGPropertyNode * node = getNode(name.c_str());
220   if (node == 0)
221     cout <<
222            "Attempt to set archive flag for non-existant property "
223            << name << endl;
224   else
225     node->setAttribute(SGPropertyNode::ARCHIVE, state);
226 }
227
228 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229
230 void FGPropertyManager::SetReadable (const string &name, bool state )
231 {
232   SGPropertyNode * node = getNode(name.c_str());
233   if (node == 0)
234     cout <<
235            "Attempt to set read flag for non-existant property "
236            << name << endl;
237   else
238     node->setAttribute(SGPropertyNode::READ, state);
239 }
240
241 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242
243 void FGPropertyManager::SetWritable (const string &name, bool state )
244 {
245   SGPropertyNode * node = getNode(name.c_str());
246   if (node == 0)
247     cout <<
248            "Attempt to set write flag for non-existant property "
249            << name << endl;
250   else
251     node->setAttribute(SGPropertyNode::WRITE, state);
252 }
253
254 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255
256 void FGPropertyManager::Untie (const string &name)
257 {
258   if (!untie(name.c_str()))
259     cout << "Failed to untie property " << name << endl;
260 }
261
262 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263
264 void FGPropertyManager::Tie (const string &name, bool *pointer, bool useDefault)
265 {
266   if (!tie(name.c_str(), SGRawValuePointer<bool>(pointer),
267                                  useDefault))
268     cout <<
269            "Failed to tie property " << name << " to a pointer" << endl;
270 }
271
272 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273
274 void FGPropertyManager::Tie (const string &name, int *pointer, 
275                                           bool useDefault )
276 {
277   if (!tie(name.c_str(), SGRawValuePointer<int>(pointer),
278                                  useDefault))
279     cout <<
280            "Failed to tie property " << name << " to a pointer" << endl;
281 }
282
283 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284
285 void FGPropertyManager::Tie (const string &name, long *pointer, 
286                                           bool useDefault )
287 {
288   if (!tie(name.c_str(), SGRawValuePointer<long>(pointer),
289                                  useDefault))
290     cout <<
291            "Failed to tie property " << name << " to a pointer" << endl;
292 }
293
294 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295
296 void FGPropertyManager::Tie (const string &name, float *pointer, 
297                                           bool useDefault )
298 {
299   if (!tie(name.c_str(), SGRawValuePointer<float>(pointer),
300                                  useDefault))
301     cout <<
302            "Failed to tie property " << name << " to a pointer" << endl;
303 }
304
305 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306
307 void FGPropertyManager::Tie (const string &name, double *pointer, 
308                                            bool useDefault)
309 {
310   if (!tie(name.c_str(), SGRawValuePointer<double>(pointer),
311                                  useDefault))
312     cout <<
313            "Failed to tie property " << name << " to a pointer" << endl;
314 }