1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 Date started: 09/28/2004
5 Purpose: XML element class
8 ------------- Copyright (C) 2001 Jon S. Berndt (jsb@hal-pc.org) -------------
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 "FGXMLElement.h"
34 # include <simgear/compiler.h>
36 # ifdef SG_HAVE_STD_INCLUDES
44 # if defined (sgi) && !defined(__GNUC__)
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62 static const char *IdSrc = "$Id$";
63 static const char *IdHdr = ID_XMLELEMENT;
65 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
69 Element::Element(string nm)
75 // convert ["from"]["to"] = factor, so: from * factor = to
76 convert["M"]["FT"] = 3.2808399;
77 convert["FT"]["M"] = 1.0/convert["M"]["FT"];
78 convert["M2"]["FT2"] = convert["M"]["FT"]*convert["M"]["FT"];
79 convert["FT2"]["M2"] = 1.0/convert["M2"]["FT2"];
80 convert["FT"]["IN"] = 12.0;
81 convert["IN"]["FT"] = 1.0/convert["FT"]["IN"];
82 convert["LBS"]["KG"] = 0.45359237;
83 convert["KG"]["LBS"] = 1.0/convert["LBS"]["KG"];
84 convert["SLUG*FT2"]["KG*M2"] = 1.35594;
85 convert["KG*M2"]["SLUG*FT2"] = 1.0/convert["SLUG*FT2"]["KG*M2"];
86 convert["RAD"]["DEG"] = 360.0/(2.0*3.1415926);
87 convert["DEG"]["RAD"] = 1.0/convert["RAD"]["DEG"];
88 convert["LBS/FT"]["N/M"] = 14.5939;
89 convert["LBS/FT/SEC"]["N/M/SEC"] = 14.5939;
90 convert["N/M"]["LBS/FT"] = 1.0/convert["LBS/FT"]["N/M"];
91 convert["N/M/SEC"]["LBS/FT/SEC"] = 1.0/convert["LBS/FT/SEC"]["N/M/SEC"];
92 convert["WATTS"]["HP"] = 0.001341022;
93 convert["HP"]["WATTS"] = 1.0/convert["WATTS"]["HP"];
94 convert["N"]["LBS"] = 0.22482;
95 convert["LBS"]["N"] = 1.0/convert["N"]["LBS"];
96 convert["KTS"]["FT/SEC"] = 1.68781;
97 convert["FT/SEC"]["KTS"] = 1.0/convert["KTS"]["FT/SEC"];
98 convert["FT*LBS"]["N*M"] = 1.35581795;
99 convert["N*M"]["FT*LBS"] = 1/convert["FT*LBS"]["N*M"];
100 convert["IN"]["M"] = convert["IN"]["FT"] * convert["FT"]["M"];
101 convert["M"]["IN"] = convert["M"]["FT"] * convert["FT"]["IN"];
103 convert["M"]["M"] = 1.00;
104 convert["FT"]["FT"] = 1.00;
105 convert["IN"]["IN"] = 1.00;
106 convert["IN3"]["IN3"] = 1.00;
107 convert["DEG"]["DEG"] = 1.00;
108 convert["RAD"]["RAD"] = 1.00;
109 convert["M2"]["M2"] = 1.00;
110 convert["FT2"]["FT2"] = 1.00;
111 convert["KG*M2"]["KG*M2"] = 1.00;
112 convert["SLUG*FT2"]["SLUG*FT2"] = 1.00;
113 convert["KG"]["KG"] = 1.00;
114 convert["LBS"]["LBS"] = 1.00;
115 convert["LBS/FT"]["LBS/FT"] = 1.00;
116 convert["LBS/SEC"]["LBS/SEC"] = 1.00;
117 convert["LBS/FT/SEC"]["LBS/FT/SEC"] = 1.00;
118 convert["N/M"]["N/M"] = 1.00;
119 convert["N/M/SEC"]["N/M/SEC"] = 1.00;
120 convert["PSI"]["PSI"] = 1.00;
121 convert["PSF"]["PSF"] = 1.00;
122 convert["INHG"]["INHG"] = 1.00;
123 convert["HP"]["HP"] = 1.00;
124 convert["N"]["N"] = 1.00;
125 convert["WATTS"]["WATTS"] = 1.00;
126 convert["LBS/SEC"]["LBS/SEC"] = 1.00;
127 convert["FT/SEC"]["FT/SEC"] = 1.00;
128 convert["KTS"]["KTS"] = 1.00;
129 convert["FT*LBS"]["FT*LBS"] = 1.00;
130 convert["N*M"]["N*M"] = 1.00;
133 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 Element::~Element(void)
137 for (unsigned int i=0; i<children.size(); i++) delete children[i];
140 attribute_key.clear();
143 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 string Element::GetAttributeValue(string attr)
148 for (unsigned int i=0; i<attribute_key.size(); i++) {
149 if (attribute_key[i] == attr) select = i;
151 if (select < 0) return string("");
152 else return attributes[attr];
155 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 double Element::GetAttributeValueAsNumber(string attr)
159 string attribute = GetAttributeValue(attr);
161 if (attribute.empty()) return 99e99;
162 else return (atof(attribute.c_str()));
165 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 Element* Element::GetElement(unsigned int el)
169 if (children.size() > el) {
179 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 Element* Element::GetNextElement(void)
183 if (children.size() > element_index+1) {
185 return children[element_index];
192 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194 string Element::GetDataLine(unsigned int i)
196 if (data_lines.size() > 0) return data_lines[i];
197 else return string("");
200 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 double Element::GetDataAsNumber(void)
204 if (data_lines.size() == 1) {
205 return atof(data_lines[0].c_str());
207 cerr << "Attempting to get single data value from multiple lines" << endl;
212 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214 unsigned int Element::GetNumElements(string element_name)
216 unsigned int number_of_elements=0;
217 Element* el=FindElement(element_name);
219 number_of_elements++;
220 el=FindNextElement(element_name);
222 return number_of_elements;
225 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 Element* Element::FindElement(string el)
229 if (el.empty() && children.size() >= 1) {
233 for (unsigned int i=0; i<children.size(); i++) {
234 if (el == children[i]->GetName()) {
243 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 Element* Element::FindNextElement(string el)
248 if (element_index < children.size()) {
249 return children[element_index++];
255 for (unsigned int i=element_index; i<children.size(); i++) {
256 if (el == children[i]->GetName()) {
265 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 double Element::FindElementValueAsNumber(string el)
269 Element* element = FindElement(el);
271 return element->GetDataAsNumber();
273 cerr << "Attempting to get single data value from multiple lines" << endl;
278 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 string Element::FindElementValue(string el)
282 Element* element = FindElement(el);
284 return element->GetDataLine();
290 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 double Element::FindElementValueAsNumberConvertTo(string el, string target_units)
294 Element* element = FindElement(el);
297 cerr << "Attempting to get non-existent element " << el << endl;
301 string supplied_units = element->GetAttributeValue("unit");
303 if (!supplied_units.empty()) {
304 if (convert.find(supplied_units) == convert.end()) {
305 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
306 << " conversion in FGXMLElement.cpp." << endl;
309 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
310 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
311 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
316 double value = element->GetDataAsNumber();
317 if (!supplied_units.empty()) {
318 value *= convert[supplied_units][target_units];
324 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326 double Element::FindElementValueAsNumberConvertFromTo( string el,
327 string supplied_units,
330 Element* element = FindElement(el);
333 cerr << "Attempting to get non-existent element " << el << endl;
337 if (!supplied_units.empty()) {
338 if (convert.find(supplied_units) == convert.end()) {
339 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
340 << " conversion in FGXMLElement.cpp." << endl;
343 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
344 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
345 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
350 double value = element->GetDataAsNumber();
351 if (!supplied_units.empty()) {
352 value *= convert[supplied_units][target_units];
358 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 FGColumnVector3 Element::FindElementTripletConvertTo( string target_units)
362 FGColumnVector3 triplet;
365 string supplied_units = GetAttributeValue("unit");
367 if (!supplied_units.empty()) {
368 if (convert.find(supplied_units) == convert.end()) {
369 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
370 << " conversion in FGXMLElement.cpp." << endl;
373 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
374 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
375 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
380 item = FindElement("x");
381 if (!item) item = FindElement("roll");
383 value = item->GetDataAsNumber();
384 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
387 cerr << "Could not find an X triplet item for this column vector." << endl;
391 item = FindElement("y");
392 if (!item) item = FindElement("pitch");
394 value = item->GetDataAsNumber();
395 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
398 cerr << "Could not find a Y triplet item for this column vector." << endl;
402 item = FindElement("z");
403 if (!item) item = FindElement("yaw");
405 value = item->GetDataAsNumber();
406 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
409 cerr << "Could not find a Z triplet item for this column vector." << endl;
416 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418 void Element::Print(unsigned int level)
420 unsigned int i, spaces;
423 for (spaces=0; spaces<=level; spaces++) cout << " "; // format output
424 cout << "Element Name: " << name;
425 for (i=0; i<attributes.size(); i++) {
426 cout << " " << attribute_key[i] << " = " << attributes[attribute_key[i]];
429 for (i=0; i<data_lines.size(); i++) {
430 for (spaces=0; spaces<=level; spaces++) cout << " "; // format output
431 cout << data_lines[i] << endl;
433 for (i=0; i<children.size(); i++) {
434 children[i]->Print(level);
438 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440 void Element::AddAttribute(string name, string value)
442 attribute_key.push_back(name);
443 attributes[name] = value;
446 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
448 void Element::AddData(string d)
450 unsigned int string_start = (unsigned int)d.find_first_not_of(" \t");
451 if (string_start > 0) {
452 d.erase(0,string_start);
454 data_lines.push_back(d);
457 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
459 } // end namespace JSBSim