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