]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/input_output/FGPropertyManager.cpp
PAtch by Andreas Gaeb to eliminate NaN's in the location code
[flightgear.git] / src / FDM / JSBSim / input_output / 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 Lesser 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 Lesser General Public License for more
18  details.
19
20  You should have received a copy of the GNU Lesser 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 Lesser 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 "FGPropertyManager.h"
32
33 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 DEFINITIONS
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
36
37
38 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 FORWARD DECLARATIONS
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41
42 using namespace std;
43
44 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 */
48
49 namespace JSBSim {
50
51 bool FGPropertyManager::suppress_warning = true;
52
53 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54
55 string FGPropertyManager::mkPropertyName(string name, bool lowercase) {
56
57   /* do this two pass to avoid problems with characters getting skipped
58      because the index changed */
59   unsigned i;
60   for(i=0;i<name.length();i++) {
61     if( lowercase && isupper(name[i]) )
62       name[i]=tolower(name[i]);
63     else if( isspace(name[i]) )
64       name[i]='-';
65   }
66   
67   return name;
68 }
69
70 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71
72 FGPropertyManager*
73 FGPropertyManager::GetNode (const string &path, bool create)
74 {
75   SGPropertyNode* node=this->getNode(path.c_str(), create);
76   if (node == 0 && !suppress_warning) {
77     cerr << "FGPropertyManager::GetNode() No node found for " << path << endl;
78   }
79   return (FGPropertyManager*)node;
80 }
81
82 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83
84 FGPropertyManager*
85 FGPropertyManager::GetNode (const string &relpath, int index, bool create)
86 {
87     return (FGPropertyManager*)getNode(relpath.c_str(),index,create);
88 }
89
90 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91
92
93 bool FGPropertyManager::HasNode (const string &path)
94 {
95   // Checking if a node exists shouldn't write a warning if it doesn't exist
96   suppress_warning = true;
97   bool has_node = (GetNode(path, false) != 0);
98   suppress_warning = false;
99   return has_node;
100 }
101
102 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103
104 string FGPropertyManager::GetName( void )
105 {
106   return string( getName() );
107 }
108
109 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110
111 string FGPropertyManager::GetPrintableName( void )
112 {
113   string temp_string(getName());
114   size_t initial_location=0;
115   size_t found_location;
116
117   found_location = temp_string.rfind("/");
118   if (found_location != string::npos)
119   temp_string = temp_string.substr(found_location);
120
121   found_location = temp_string.find('_',initial_location);
122   while (found_location != string::npos) {
123     temp_string.replace(found_location,1," ");
124     initial_location = found_location+1;
125     found_location = temp_string.find('_',initial_location);
126   }
127   return temp_string;
128 }
129
130 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131
132 string FGPropertyManager::GetFullyQualifiedName(void) {
133     vector<string> stack;
134     stack.push_back( getDisplayName(true) );
135     SGPropertyNode* tmpn=getParent();
136     bool atroot=false;
137     while( !atroot ) {
138      stack.push_back( tmpn->getDisplayName(true) );
139      if( !tmpn->getParent() )
140       atroot=true;
141      else
142       tmpn=tmpn->getParent();
143     }
144
145     string fqname="";
146     for(unsigned i=stack.size()-1;i>0;i--) {
147       fqname+= stack[i];
148       fqname+= "/";
149     }
150     fqname+= stack[0];
151     return fqname;
152
153 }
154
155 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156
157 string FGPropertyManager::GetRelativeName( const string &path )
158 {
159   string temp_string = GetFullyQualifiedName();
160   size_t len = path.length();
161   if ( (len > 0) && (temp_string.substr(0,len) == path) ) {
162     temp_string = temp_string.erase(0,len);
163   }
164   return temp_string;
165 }
166
167
168
169 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170
171 bool FGPropertyManager::GetBool (const string &name, bool defaultValue)
172 {
173   return getBoolValue(name.c_str(), defaultValue);
174 }
175
176 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177
178 int FGPropertyManager::GetInt (const string &name, int defaultValue )
179 {
180   return getIntValue(name.c_str(), defaultValue);
181 }
182
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184
185 int FGPropertyManager::GetLong (const string &name, long defaultValue )
186 {
187   return getLongValue(name.c_str(), defaultValue);
188 }
189
190 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191
192 float FGPropertyManager::GetFloat (const string &name, float defaultValue )
193 {
194   return getFloatValue(name.c_str(), defaultValue);
195 }
196
197 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198
199 double FGPropertyManager::GetDouble (const string &name, double defaultValue )
200 {
201   return getDoubleValue(name.c_str(), defaultValue);
202 }
203
204 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205
206 string FGPropertyManager::GetString (const string &name, string defaultValue )
207 {
208   return string(getStringValue(name.c_str(), defaultValue.c_str()));
209 }
210
211 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212
213 bool FGPropertyManager::SetBool (const string &name, bool val)
214 {
215   return setBoolValue(name.c_str(), val);
216 }
217
218 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219
220 bool FGPropertyManager::SetInt (const string &name, int val)
221 {
222   return setIntValue(name.c_str(), val);
223 }
224
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226
227 bool FGPropertyManager::SetLong (const string &name, long val)
228 {
229   return setLongValue(name.c_str(), val);
230 }
231
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233
234 bool FGPropertyManager::SetFloat (const string &name, float val)
235 {
236   return setFloatValue(name.c_str(), val);
237 }
238
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240
241 bool FGPropertyManager::SetDouble (const string &name, double val)
242 {
243   return setDoubleValue(name.c_str(), val);
244 }
245
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247
248 bool FGPropertyManager::SetString (const string &name, const string &val)
249 {
250   return setStringValue(name.c_str(), val.c_str());
251 }
252
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254
255 void FGPropertyManager::SetArchivable (const string &name, bool state )
256 {
257   SGPropertyNode * node = getNode(name.c_str());
258   if (node == 0)
259     cerr <<
260            "Attempt to set archive flag for non-existent property "
261            << name << endl;
262   else
263     node->setAttribute(SGPropertyNode::ARCHIVE, state);
264 }
265
266 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267
268 void FGPropertyManager::SetReadable (const string &name, bool state )
269 {
270   SGPropertyNode * node = getNode(name.c_str());
271   if (node == 0)
272     cerr <<
273            "Attempt to set read flag for non-existant property "
274            << name << endl;
275   else
276     node->setAttribute(SGPropertyNode::READ, state);
277 }
278
279 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280
281 void FGPropertyManager::SetWritable (const string &name, bool state )
282 {
283   SGPropertyNode * node = getNode(name.c_str());
284   if (node == 0)
285     cerr <<
286            "Attempt to set write flag for non-existant property "
287            << name << endl;
288   else
289     node->setAttribute(SGPropertyNode::WRITE, state);
290 }
291
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293
294 void FGPropertyManager::Untie (const string &name)
295 {
296   if (!untie(name.c_str()))
297     cerr << "Failed to untie property " << name << endl;
298 }
299
300 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301
302 void FGPropertyManager::Tie (const string &name, bool *pointer, bool useDefault)
303 {
304   if (!tie(name.c_str(), SGRawValuePointer<bool>(pointer), useDefault))
305     cerr << "Failed to tie property " << name << " to a pointer" << endl;
306   else if (debug_lvl & 0x20)
307     cout << name << endl;
308 }
309
310 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311
312 void FGPropertyManager::Tie (const string &name, int *pointer,
313                                           bool useDefault )
314 {
315   if (!tie(name.c_str(), SGRawValuePointer<int>(pointer), useDefault))
316     cerr << "Failed to tie property " << name << " to a pointer" << endl;
317   else if (debug_lvl & 0x20)
318     cout << name << endl;
319 }
320
321 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322
323 void FGPropertyManager::Tie (const string &name, long *pointer,
324                                           bool useDefault )
325 {
326   if (!tie(name.c_str(), SGRawValuePointer<long>(pointer), useDefault))
327     cerr << "Failed to tie property " << name << " to a pointer" << endl;
328   else if (debug_lvl & 0x20)
329     cout << name << endl;
330 }
331
332 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333
334 void FGPropertyManager::Tie (const string &name, float *pointer,
335                                           bool useDefault )
336 {
337   if (!tie(name.c_str(), SGRawValuePointer<float>(pointer), useDefault))
338     cerr << "Failed to tie property " << name << " to a pointer" << endl;
339   else if (debug_lvl & 0x20)
340     cout << name << endl;
341 }
342
343 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344
345 void FGPropertyManager::Tie (const string &name, double *pointer, bool useDefault)
346 {
347   if (!tie(name.c_str(), SGRawValuePointer<double>(pointer), useDefault))
348     cerr << "Failed to tie property " << name << " to a pointer" << endl;
349   else if (debug_lvl & 0x20)
350     cout << name << endl;
351 }
352
353 } // namespace JSBSim