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]) )
67 for(i=0;i<name.length();i++) {
75 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 FGPropertyManager::GetNode (const string &path, bool create)
80 SGPropertyNode* node=this->getNode(path.c_str(), create);
81 if (node == 0 && !suppress_warning)
82 cout << "FGPropertyManager::GetNode() No node found for "
84 return (FGPropertyManager*)node;
87 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 FGPropertyManager::GetNode (const string &relpath, int index, bool create)
92 return (FGPropertyManager*)getNode(relpath.c_str(),index,create);
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98 bool FGPropertyManager::HasNode (const string &path)
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;
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 string FGPropertyManager::GetName( void )
111 return string( getName() );
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 string FGPropertyManager::GetPrintableName( void )
118 string temp_string(getName());
119 size_t initial_location=0;
120 size_t found_location;
122 found_location = temp_string.rfind("/");
123 if (found_location != string::npos)
124 temp_string = temp_string.substr(found_location);
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);
135 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 string FGPropertyManager::GetFullyQualifiedName(void) {
138 vector<string> stack;
139 stack.push_back( getDisplayName(true) );
140 SGPropertyNode* tmpn=getParent();
143 stack.push_back( tmpn->getDisplayName(true) );
144 if( !tmpn->getParent() )
147 tmpn=tmpn->getParent();
151 for(unsigned i=stack.size()-1;i>0;i--) {
161 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 bool FGPropertyManager::GetBool (const string &name, bool defaultValue)
165 return getBoolValue(name.c_str(), defaultValue);
168 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 int FGPropertyManager::GetInt (const string &name, int defaultValue )
172 return getIntValue(name.c_str(), defaultValue);
175 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177 int FGPropertyManager::GetLong (const string &name, long defaultValue )
179 return getLongValue(name.c_str(), defaultValue);
182 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 float FGPropertyManager::GetFloat (const string &name, float defaultValue )
186 return getFloatValue(name.c_str(), defaultValue);
189 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 double FGPropertyManager::GetDouble (const string &name, double defaultValue )
193 return getDoubleValue(name.c_str(), defaultValue);
196 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 string FGPropertyManager::GetString (const string &name, string defaultValue )
200 return string(getStringValue(name.c_str(), defaultValue.c_str()));
203 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 bool FGPropertyManager::SetBool (const string &name, bool val)
207 return setBoolValue(name.c_str(), val);
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 bool FGPropertyManager::SetInt (const string &name, int val)
214 return setIntValue(name.c_str(), val);
217 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 bool FGPropertyManager::SetLong (const string &name, long val)
221 return setLongValue(name.c_str(), val);
224 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 bool FGPropertyManager::SetFloat (const string &name, float val)
228 return setFloatValue(name.c_str(), val);
231 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 bool FGPropertyManager::SetDouble (const string &name, double val)
235 return setDoubleValue(name.c_str(), val);
238 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 bool FGPropertyManager::SetString (const string &name, const string &val)
242 return setStringValue(name.c_str(), val.c_str());
245 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 void FGPropertyManager::SetArchivable (const string &name, bool state )
249 SGPropertyNode * node = getNode(name.c_str());
252 "Attempt to set archive flag for non-existant property "
255 node->setAttribute(SGPropertyNode::ARCHIVE, state);
258 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 void FGPropertyManager::SetReadable (const string &name, bool state )
262 SGPropertyNode * node = getNode(name.c_str());
265 "Attempt to set read flag for non-existant property "
268 node->setAttribute(SGPropertyNode::READ, state);
271 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 void FGPropertyManager::SetWritable (const string &name, bool state )
275 SGPropertyNode * node = getNode(name.c_str());
278 "Attempt to set write flag for non-existant property "
281 node->setAttribute(SGPropertyNode::WRITE, state);
284 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 void FGPropertyManager::Untie (const string &name)
288 if (!untie(name.c_str()))
289 cout << "Failed to untie property " << name << endl;
292 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 void FGPropertyManager::Tie (const string &name, bool *pointer, bool useDefault)
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;
302 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 void FGPropertyManager::Tie (const string &name, int *pointer,
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;
313 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 void FGPropertyManager::Tie (const string &name, long *pointer,
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;
324 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326 void FGPropertyManager::Tie (const string &name, float *pointer,
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;
335 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
337 void FGPropertyManager::Tie (const string &name, double *pointer, bool useDefault)
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;
345 } // namespace JSBSim