]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/input_output/FGPropertyManager.cpp
Merge branch 'topic/pu-crash'
[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     cout << "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
158 bool FGPropertyManager::GetBool (const string &name, bool defaultValue)
159 {
160   return getBoolValue(name.c_str(), defaultValue);
161 }
162
163 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164
165 int FGPropertyManager::GetInt (const string &name, int defaultValue )
166 {
167   return getIntValue(name.c_str(), defaultValue);
168 }
169
170 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171
172 int FGPropertyManager::GetLong (const string &name, long defaultValue )
173 {
174   return getLongValue(name.c_str(), defaultValue);
175 }
176
177 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178
179 float FGPropertyManager::GetFloat (const string &name, float defaultValue )
180 {
181   return getFloatValue(name.c_str(), defaultValue);
182 }
183
184 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185
186 double FGPropertyManager::GetDouble (const string &name, double defaultValue )
187 {
188   return getDoubleValue(name.c_str(), defaultValue);
189 }
190
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192
193 string FGPropertyManager::GetString (const string &name, string defaultValue )
194 {
195   return string(getStringValue(name.c_str(), defaultValue.c_str()));
196 }
197
198 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199
200 bool FGPropertyManager::SetBool (const string &name, bool val)
201 {
202   return setBoolValue(name.c_str(), val);
203 }
204
205 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206
207 bool FGPropertyManager::SetInt (const string &name, int val)
208 {
209   return setIntValue(name.c_str(), val);
210 }
211
212 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213
214 bool FGPropertyManager::SetLong (const string &name, long val)
215 {
216   return setLongValue(name.c_str(), val);
217 }
218
219 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220
221 bool FGPropertyManager::SetFloat (const string &name, float val)
222 {
223   return setFloatValue(name.c_str(), val);
224 }
225
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227
228 bool FGPropertyManager::SetDouble (const string &name, double val)
229 {
230   return setDoubleValue(name.c_str(), val);
231 }
232
233 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234
235 bool FGPropertyManager::SetString (const string &name, const string &val)
236 {
237   return setStringValue(name.c_str(), val.c_str());
238 }
239
240 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241
242 void FGPropertyManager::SetArchivable (const string &name, bool state )
243 {
244   SGPropertyNode * node = getNode(name.c_str());
245   if (node == 0)
246     cout <<
247            "Attempt to set archive flag for non-existant property "
248            << name << endl;
249   else
250     node->setAttribute(SGPropertyNode::ARCHIVE, state);
251 }
252
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254
255 void FGPropertyManager::SetReadable (const string &name, bool state )
256 {
257   SGPropertyNode * node = getNode(name.c_str());
258   if (node == 0)
259     cout <<
260            "Attempt to set read flag for non-existant property "
261            << name << endl;
262   else
263     node->setAttribute(SGPropertyNode::READ, state);
264 }
265
266 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267
268 void FGPropertyManager::SetWritable (const string &name, bool state )
269 {
270   SGPropertyNode * node = getNode(name.c_str());
271   if (node == 0)
272     cout <<
273            "Attempt to set write flag for non-existant property "
274            << name << endl;
275   else
276     node->setAttribute(SGPropertyNode::WRITE, state);
277 }
278
279 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280
281 void FGPropertyManager::Untie (const string &name)
282 {
283   if (!untie(name.c_str()))
284     cout << "Failed to untie property " << name << endl;
285 }
286
287 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288
289 void FGPropertyManager::Tie (const string &name, bool *pointer, bool useDefault)
290 {
291   if (!tie(name.c_str(), SGRawValuePointer<bool>(pointer), useDefault))
292     cout << "Failed to tie property " << name << " to a pointer" << endl;
293   else if (debug_lvl & 0x20)
294     cout << name << endl;
295 }
296
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298
299 void FGPropertyManager::Tie (const string &name, int *pointer,
300                                           bool useDefault )
301 {
302   if (!tie(name.c_str(), SGRawValuePointer<int>(pointer), useDefault))
303     cout << "Failed to tie property " << name << " to a pointer" << endl;
304   else if (debug_lvl & 0x20)
305     cout << name << endl;
306 }
307
308 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309
310 void FGPropertyManager::Tie (const string &name, long *pointer,
311                                           bool useDefault )
312 {
313   if (!tie(name.c_str(), SGRawValuePointer<long>(pointer), useDefault))
314     cout << "Failed to tie property " << name << " to a pointer" << endl;
315   else if (debug_lvl & 0x20)
316     cout << name << endl;
317 }
318
319 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
320
321 void FGPropertyManager::Tie (const string &name, float *pointer,
322                                           bool useDefault )
323 {
324   if (!tie(name.c_str(), SGRawValuePointer<float>(pointer), useDefault))
325     cout << "Failed to tie property " << name << " to a pointer" << endl;
326   else if (debug_lvl & 0x20)
327     cout << name << endl;
328 }
329
330 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331
332 void FGPropertyManager::Tie (const string &name, double *pointer, bool useDefault)
333 {
334   if (!tie(name.c_str(), SGRawValuePointer<double>(pointer), useDefault))
335     cout << "Failed to tie property " << name << " to a pointer" << endl;
336   else if (debug_lvl & 0x20)
337     cout << name << endl;
338 }
339
340 } // namespace JSBSim