1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Header: FGPropertyManager.cpp
5 Based on work originally by David Megginson
8 ------------- Copyright (C) 2002 -------------
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
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
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.
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.
27 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
31 #include "FGPropertyManager.h"
33 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
38 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 COMMENTS, REFERENCES, and NOTES [use "class documentation" below for API docs]
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 bool FGPropertyManager::suppress_warning = true;
53 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 string FGPropertyManager::mkPropertyName(string name, bool lowercase) {
57 /* do this two pass to avoid problems with characters getting skipped
58 because the index changed */
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]) )
70 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 FGPropertyManager::GetNode (const string &path, bool create)
75 SGPropertyNode* node=this->getNode(path.c_str(), create);
76 if (node == 0 && !suppress_warning) {
77 cerr << "FGPropertyManager::GetNode() No node found for " << path << endl;
79 return (FGPropertyManager*)node;
82 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 FGPropertyManager::GetNode (const string &relpath, int index, bool create)
87 return (FGPropertyManager*)getNode(relpath.c_str(),index,create);
90 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 bool FGPropertyManager::HasNode (const string &path)
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;
102 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 string FGPropertyManager::GetName( void )
106 return string( getName() );
109 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 string FGPropertyManager::GetPrintableName( void )
113 string temp_string(getName());
114 size_t initial_location=0;
115 size_t found_location;
117 found_location = temp_string.rfind("/");
118 if (found_location != string::npos)
119 temp_string = temp_string.substr(found_location);
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);
130 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132 string FGPropertyManager::GetFullyQualifiedName(void) {
133 vector<string> stack;
134 stack.push_back( getDisplayName(true) );
135 SGPropertyNode* tmpn=getParent();
138 stack.push_back( tmpn->getDisplayName(true) );
139 if( !tmpn->getParent() )
142 tmpn=tmpn->getParent();
146 for(unsigned i=stack.size()-1;i>0;i--) {
155 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 string FGPropertyManager::GetRelativeName( const string &path )
159 string temp_string = GetFullyQualifiedName();
160 size_t len = path.length();
161 if ( (len > 0) && (temp_string.substr(0,len) == path) ) {
162 temp_string = temp_string.erase(0,len);
169 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171 bool FGPropertyManager::GetBool (const string &name, bool defaultValue)
173 return getBoolValue(name.c_str(), defaultValue);
176 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 int FGPropertyManager::GetInt (const string &name, int defaultValue )
180 return getIntValue(name.c_str(), defaultValue);
183 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 int FGPropertyManager::GetLong (const string &name, long defaultValue )
187 return getLongValue(name.c_str(), defaultValue);
190 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 float FGPropertyManager::GetFloat (const string &name, float defaultValue )
194 return getFloatValue(name.c_str(), defaultValue);
197 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199 double FGPropertyManager::GetDouble (const string &name, double defaultValue )
201 return getDoubleValue(name.c_str(), defaultValue);
204 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206 string FGPropertyManager::GetString (const string &name, string defaultValue )
208 return string(getStringValue(name.c_str(), defaultValue.c_str()));
211 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213 bool FGPropertyManager::SetBool (const string &name, bool val)
215 return setBoolValue(name.c_str(), val);
218 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 bool FGPropertyManager::SetInt (const string &name, int val)
222 return setIntValue(name.c_str(), val);
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 bool FGPropertyManager::SetLong (const string &name, long val)
229 return setLongValue(name.c_str(), val);
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 bool FGPropertyManager::SetFloat (const string &name, float val)
236 return setFloatValue(name.c_str(), val);
239 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 bool FGPropertyManager::SetDouble (const string &name, double val)
243 return setDoubleValue(name.c_str(), val);
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 bool FGPropertyManager::SetString (const string &name, const string &val)
250 return setStringValue(name.c_str(), val.c_str());
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 void FGPropertyManager::SetArchivable (const string &name, bool state )
257 SGPropertyNode * node = getNode(name.c_str());
260 "Attempt to set archive flag for non-existent property "
263 node->setAttribute(SGPropertyNode::ARCHIVE, state);
266 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 void FGPropertyManager::SetReadable (const string &name, bool state )
270 SGPropertyNode * node = getNode(name.c_str());
273 "Attempt to set read flag for non-existant property "
276 node->setAttribute(SGPropertyNode::READ, state);
279 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 void FGPropertyManager::SetWritable (const string &name, bool state )
283 SGPropertyNode * node = getNode(name.c_str());
286 "Attempt to set write flag for non-existant property "
289 node->setAttribute(SGPropertyNode::WRITE, state);
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 void FGPropertyManager::Untie (const string &name)
296 if (!untie(name.c_str()))
297 cerr << "Failed to untie property " << name << endl;
300 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 void FGPropertyManager::Tie (const string &name, bool *pointer, bool useDefault)
304 if (!tie(name.c_str(), SGRawValuePointer<bool>(pointer), useDefault))
305 cerr << "Failed to tie property " << name << " to a pointer" << endl;
306 else if (debug_lvl & 0x20)
307 cout << name << endl;
310 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 void FGPropertyManager::Tie (const string &name, int *pointer,
315 if (!tie(name.c_str(), SGRawValuePointer<int>(pointer), useDefault))
316 cerr << "Failed to tie property " << name << " to a pointer" << endl;
317 else if (debug_lvl & 0x20)
318 cout << name << endl;
321 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 void FGPropertyManager::Tie (const string &name, long *pointer,
326 if (!tie(name.c_str(), SGRawValuePointer<long>(pointer), useDefault))
327 cerr << "Failed to tie property " << name << " to a pointer" << endl;
328 else if (debug_lvl & 0x20)
329 cout << name << endl;
332 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 void FGPropertyManager::Tie (const string &name, float *pointer,
337 if (!tie(name.c_str(), SGRawValuePointer<float>(pointer), useDefault))
338 cerr << "Failed to tie property " << name << " to a pointer" << endl;
339 else if (debug_lvl & 0x20)
340 cout << name << endl;
343 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345 void FGPropertyManager::Tie (const string &name, double *pointer, bool useDefault)
347 if (!tie(name.c_str(), SGRawValuePointer<double>(pointer), useDefault))
348 cerr << "Failed to tie property " << name << " to a pointer" << endl;
349 else if (debug_lvl & 0x20)
350 cout << name << endl;
353 } // namespace JSBSim