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 cout << "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--) {
156 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 bool FGPropertyManager::GetBool (const string &name, bool defaultValue)
160 return getBoolValue(name.c_str(), defaultValue);
163 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 int FGPropertyManager::GetInt (const string &name, int defaultValue )
167 return getIntValue(name.c_str(), defaultValue);
170 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 int FGPropertyManager::GetLong (const string &name, long defaultValue )
174 return getLongValue(name.c_str(), defaultValue);
177 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 float FGPropertyManager::GetFloat (const string &name, float defaultValue )
181 return getFloatValue(name.c_str(), defaultValue);
184 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 double FGPropertyManager::GetDouble (const string &name, double defaultValue )
188 return getDoubleValue(name.c_str(), defaultValue);
191 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 string FGPropertyManager::GetString (const string &name, string defaultValue )
195 return string(getStringValue(name.c_str(), defaultValue.c_str()));
198 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200 bool FGPropertyManager::SetBool (const string &name, bool val)
202 return setBoolValue(name.c_str(), val);
205 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 bool FGPropertyManager::SetInt (const string &name, int val)
209 return setIntValue(name.c_str(), val);
212 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214 bool FGPropertyManager::SetLong (const string &name, long val)
216 return setLongValue(name.c_str(), val);
219 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 bool FGPropertyManager::SetFloat (const string &name, float val)
223 return setFloatValue(name.c_str(), val);
226 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 bool FGPropertyManager::SetDouble (const string &name, double val)
230 return setDoubleValue(name.c_str(), val);
233 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 bool FGPropertyManager::SetString (const string &name, const string &val)
237 return setStringValue(name.c_str(), val.c_str());
240 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 void FGPropertyManager::SetArchivable (const string &name, bool state )
244 SGPropertyNode * node = getNode(name.c_str());
247 "Attempt to set archive flag for non-existant property "
250 node->setAttribute(SGPropertyNode::ARCHIVE, state);
253 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 void FGPropertyManager::SetReadable (const string &name, bool state )
257 SGPropertyNode * node = getNode(name.c_str());
260 "Attempt to set read flag for non-existant property "
263 node->setAttribute(SGPropertyNode::READ, state);
266 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 void FGPropertyManager::SetWritable (const string &name, bool state )
270 SGPropertyNode * node = getNode(name.c_str());
273 "Attempt to set write flag for non-existant property "
276 node->setAttribute(SGPropertyNode::WRITE, state);
279 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 void FGPropertyManager::Untie (const string &name)
283 if (!untie(name.c_str()))
284 cout << "Failed to untie property " << name << endl;
287 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 void FGPropertyManager::Tie (const string &name, bool *pointer, bool useDefault)
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;
297 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 void FGPropertyManager::Tie (const string &name, int *pointer,
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;
308 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 void FGPropertyManager::Tie (const string &name, long *pointer,
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;
319 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321 void FGPropertyManager::Tie (const string &name, float *pointer,
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;
330 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 void FGPropertyManager::Tie (const string &name, double *pointer, bool useDefault)
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;
340 } // namespace JSBSim