]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/input_output/FGPropertyManager.cpp
Sync w. JSBSim CVS (merge from PRE_OSG_PLIB_20061029 branch)
[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 {
111   return string( getName() );
112 }
113
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115
116 string FGPropertyManager::GetPrintableName( void )
117 {
118   string temp_string(getName());
119   size_t initial_location=0;
120   size_t found_location;
121
122   found_location = temp_string.rfind("/");
123   if (found_location != string::npos)
124   temp_string = temp_string.substr(found_location);
125
126   found_location = temp_string.find('_',initial_location);
127   while (found_location != string::npos) {
128     temp_string.replace(found_location,1," ");
129     initial_location = found_location+1;
130     found_location = temp_string.find('_',initial_location);
131   }
132   return temp_string;
133 }
134
135 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136
137 string FGPropertyManager::GetFullyQualifiedName(void) {
138     vector<string> stack;
139     stack.push_back( getDisplayName(true) );
140     SGPropertyNode* tmpn=getParent();
141     bool atroot=false;
142     while( !atroot ) {
143      stack.push_back( tmpn->getDisplayName(true) );
144      if( !tmpn->getParent() )
145       atroot=true;
146      else
147       tmpn=tmpn->getParent();
148     }
149
150     string fqname="";
151     for(unsigned i=stack.size()-1;i>0;i--) {
152       fqname+= stack[i];
153       fqname+= "/";
154     }
155     fqname+= stack[0];
156     return fqname;
157
158 }
159
160
161 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162
163 bool FGPropertyManager::GetBool (const string &name, bool defaultValue)
164 {
165   return getBoolValue(name.c_str(), defaultValue);
166 }
167
168 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169
170 int FGPropertyManager::GetInt (const string &name, int defaultValue )
171 {
172   return getIntValue(name.c_str(), defaultValue);
173 }
174
175 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
176
177 int FGPropertyManager::GetLong (const string &name, long defaultValue )
178 {
179   return getLongValue(name.c_str(), defaultValue);
180 }
181
182 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183
184 float FGPropertyManager::GetFloat (const string &name, float defaultValue )
185 {
186   return getFloatValue(name.c_str(), defaultValue);
187 }
188
189 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190
191 double FGPropertyManager::GetDouble (const string &name, double defaultValue )
192 {
193   return getDoubleValue(name.c_str(), defaultValue);
194 }
195
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197
198 string FGPropertyManager::GetString (const string &name, string defaultValue )
199 {
200   return string(getStringValue(name.c_str(), defaultValue.c_str()));
201 }
202
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204
205 bool FGPropertyManager::SetBool (const string &name, bool val)
206 {
207   return setBoolValue(name.c_str(), val);
208 }
209
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211
212 bool FGPropertyManager::SetInt (const string &name, int val)
213 {
214   return setIntValue(name.c_str(), val);
215 }
216
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218
219 bool FGPropertyManager::SetLong (const string &name, long val)
220 {
221   return setLongValue(name.c_str(), val);
222 }
223
224 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225
226 bool FGPropertyManager::SetFloat (const string &name, float val)
227 {
228   return setFloatValue(name.c_str(), val);
229 }
230
231 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232
233 bool FGPropertyManager::SetDouble (const string &name, double val)
234 {
235   return setDoubleValue(name.c_str(), val);
236 }
237
238 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239
240 bool FGPropertyManager::SetString (const string &name, const string &val)
241 {
242   return setStringValue(name.c_str(), val.c_str());
243 }
244
245 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246
247 void FGPropertyManager::SetArchivable (const string &name, bool state )
248 {
249   SGPropertyNode * node = getNode(name.c_str());
250   if (node == 0)
251     cout <<
252            "Attempt to set archive flag for non-existant property "
253            << name << endl;
254   else
255     node->setAttribute(SGPropertyNode::ARCHIVE, state);
256 }
257
258 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259
260 void FGPropertyManager::SetReadable (const string &name, bool state )
261 {
262   SGPropertyNode * node = getNode(name.c_str());
263   if (node == 0)
264     cout <<
265            "Attempt to set read flag for non-existant property "
266            << name << endl;
267   else
268     node->setAttribute(SGPropertyNode::READ, state);
269 }
270
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272
273 void FGPropertyManager::SetWritable (const string &name, bool state )
274 {
275   SGPropertyNode * node = getNode(name.c_str());
276   if (node == 0)
277     cout <<
278            "Attempt to set write flag for non-existant property "
279            << name << endl;
280   else
281     node->setAttribute(SGPropertyNode::WRITE, state);
282 }
283
284 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285
286 void FGPropertyManager::Untie (const string &name)
287 {
288   if (!untie(name.c_str()))
289     cout << "Failed to untie property " << name << endl;
290 }
291
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293
294 void FGPropertyManager::Tie (const string &name, bool *pointer, bool useDefault)
295 {
296   if (!tie(name.c_str(), SGRawValuePointer<bool>(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, int *pointer,
305                                           bool useDefault )
306 {
307   if (!tie(name.c_str(), SGRawValuePointer<int>(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, long *pointer,
316                                           bool useDefault )
317 {
318   if (!tie(name.c_str(), SGRawValuePointer<long>(pointer), useDefault))
319     cout << "Failed to tie property " << name << " to a pointer" << endl;
320   else if (debug_lvl & 0x20)
321     cout << name << endl;
322 }
323
324 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325
326 void FGPropertyManager::Tie (const string &name, float *pointer,
327                                           bool useDefault )
328 {
329   if (!tie(name.c_str(), SGRawValuePointer<float>(pointer), useDefault))
330     cout << "Failed to tie property " << name << " to a pointer" << endl;
331   else if (debug_lvl & 0x20)
332     cout << name << endl;
333 }
334
335 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336
337 void FGPropertyManager::Tie (const string &name, double *pointer, bool useDefault)
338 {
339   if (!tie(name.c_str(), SGRawValuePointer<double>(pointer), useDefault))
340     cout << "Failed to tie property " << name << " to a pointer" << endl;
341   else if (debug_lvl & 0x20)
342     cout << name << endl;
343 }
344
345 } // namespace JSBSim