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