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