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"
36 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42 static const char *IdSrc = "$Id$";
43 static const char *IdHdr = ID_XMLELEMENT;
45 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49 Element::Element(string nm)
55 // convert ["from"]["to"] = factor, so: from * factor = to
57 convert["M"]["FT"] = 3.2808399;
58 convert["FT"]["M"] = 1.0/convert["M"]["FT"];
59 convert["FT"]["IN"] = 12.0;
60 convert["IN"]["FT"] = 1.0/convert["FT"]["IN"];
61 convert["IN"]["M"] = convert["IN"]["FT"] * convert["FT"]["M"];
62 convert["M"]["IN"] = convert["M"]["FT"] * convert["FT"]["IN"];
64 convert["M2"]["FT2"] = convert["M"]["FT"]*convert["M"]["FT"];
65 convert["FT2"]["M2"] = 1.0/convert["M2"]["FT2"];
66 convert["M2"]["IN2"] = convert["M"]["IN"]*convert["M"]["IN"];
67 convert["IN2"]["M2"] = 1.0/convert["M2"]["IN2"];
68 convert["FT2"]["IN2"] = 144.0;
69 convert["IN2"]["FT2"] = 1.0/convert["FT2"]["IN2"];
71 convert["IN3"]["CC"] = 16.387064;
72 convert["CC"]["IN3"] = 1.0/convert["IN3"]["CC"];
73 convert["FT3"]["IN3"] = 1728.0;
74 convert["IN3"]["FT3"] = 1.0/convert["FT3"]["IN3"];
75 convert["M3"]["FT3"] = 35.3146667;
76 convert["FT3"]["M3"] = 1.0/convert["M3"]["FT3"];
77 convert["LTR"]["IN3"] = 61.0237441;
78 convert["IN3"]["LTR"] = 1.0/convert["LTR"]["IN3"];
80 convert["LBS"]["KG"] = 0.45359237;
81 convert["KG"]["LBS"] = 1.0/convert["LBS"]["KG"];
82 convert["SLUG"]["KG"] = 14.59390;
83 convert["KG"]["SLUG"] = 1.0/convert["SLUG"]["KG"];
85 convert["SLUG*FT2"]["KG*M2"] = 1.35594;
86 convert["KG*M2"]["SLUG*FT2"] = 1.0/convert["SLUG*FT2"]["KG*M2"];
88 convert["RAD"]["DEG"] = 360.0/(2.0*3.1415926);
89 convert["DEG"]["RAD"] = 1.0/convert["RAD"]["DEG"];
91 convert["LBS/FT"]["N/M"] = 14.5939;
92 convert["N/M"]["LBS/FT"] = 1.0/convert["LBS/FT"]["N/M"];
94 convert["LBS/FT/SEC"]["N/M/SEC"] = 14.5939;
95 convert["N/M/SEC"]["LBS/FT/SEC"] = 1.0/convert["LBS/FT/SEC"]["N/M/SEC"];
96 // Damping force (Square Law)
97 convert["LBS/FT2/SEC2"]["N/M2/SEC2"] = 47.880259;
98 convert["N/M2/SEC2"]["LBS/FT2/SEC2"] = 1.0/convert["LBS/FT2/SEC2"]["N/M2/SEC2"];
100 convert["WATTS"]["HP"] = 0.001341022;
101 convert["HP"]["WATTS"] = 1.0/convert["WATTS"]["HP"];
103 convert["N"]["LBS"] = 0.22482;
104 convert["LBS"]["N"] = 1.0/convert["N"]["LBS"];
106 convert["KTS"]["FT/SEC"] = 1.68781;
107 convert["FT/SEC"]["KTS"] = 1.0/convert["KTS"]["FT/SEC"];
108 convert["M/S"]["FT/S"] = 3.2808399;
109 convert["FT/S"]["M/S"] = 1.0/convert["M/S"]["FT/S"];
111 convert["FT*LBS"]["N*M"] = 1.35581795;
112 convert["N*M"]["FT*LBS"] = 1/convert["FT*LBS"]["N*M"];
114 convert["M4*SEC/KG"]["FT4*SEC/SLUG"] = convert["M"]["FT"]*convert["M"]["FT"]*
115 convert["M"]["FT"]*convert["M"]["FT"]/convert["KG"]["SLUG"];
116 convert["FT4*SEC/SLUG"]["M4*SEC/KG"] =
117 1.0/convert["M4*SEC/KG"]["FT4*SEC/SLUG"];
119 convert["INHG"]["PSF"] = 70.7180803;
120 convert["PSF"]["INHG"] = 1.0/convert["INHG"]["PSF"];
121 convert["ATM"]["INHG"] = 29.9246899;
122 convert["INHG"]["ATM"] = 1.0/convert["ATM"]["INHG"];
123 convert["PSI"]["INHG"] = 2.03625437;
124 convert["INHG"]["PSI"] = 1.0/convert["PSI"]["INHG"];
125 convert["INHG"]["PA"] = 3386.0; // inches Mercury to pascals
126 convert["PA"]["INHG"] = 1.0/convert["INHG"]["PA"];
127 convert["LBS/FT2"]["N/M2"] = 14.5939/convert["FT"]["M"];
128 convert["N/M2"]["LBS/FT2"] = 1.0/convert["LBS/FT2"]["N/M2"];
129 convert["LBS/FT2"]["PA"] = convert["LBS/FT2"]["N/M2"];
130 convert["PA"]["LBS/FT2"] = 1.0/convert["LBS/FT2"]["PA"];
132 convert["KG/MIN"]["LBS/MIN"] = convert["KG"]["LBS"];
134 convert["LBS/HP*HR"]["KG/KW*HR"] = 0.6083;
135 convert["KG/KW*HR"]["LBS/HP*HR"] = 1.0/convert["LBS/HP*HR"]["KG/KW*HR"];
138 convert["M"]["M"] = 1.00;
139 convert["FT"]["FT"] = 1.00;
140 convert["IN"]["IN"] = 1.00;
142 convert["M2"]["M2"] = 1.00;
143 convert["FT2"]["FT2"] = 1.00;
145 convert["IN3"]["IN3"] = 1.00;
146 convert["CC"]["CC"] = 1.0;
147 convert["M3"]["M3"] = 1.0;
148 convert["FT3"]["FT3"] = 1.0;
149 convert["LTR"]["LTR"] = 1.0;
151 convert["KG"]["KG"] = 1.00;
152 convert["LBS"]["LBS"] = 1.00;
153 // Moments of Inertia
154 convert["KG*M2"]["KG*M2"] = 1.00;
155 convert["SLUG*FT2"]["SLUG*FT2"] = 1.00;
157 convert["DEG"]["DEG"] = 1.00;
158 convert["RAD"]["RAD"] = 1.00;
160 convert["LBS/FT"]["LBS/FT"] = 1.00;
161 convert["N/M"]["N/M"] = 1.00;
163 convert["LBS/FT/SEC"]["LBS/FT/SEC"] = 1.00;
164 convert["N/M/SEC"]["N/M/SEC"] = 1.00;
165 // Damping force (Square law)
166 convert["LBS/FT2/SEC2"]["LBS/FT2/SEC2"] = 1.00;
167 convert["N/M2/SEC2"]["N/M2/SEC2"] = 1.00;
169 convert["HP"]["HP"] = 1.00;
170 convert["WATTS"]["WATTS"] = 1.00;
172 convert["N"]["N"] = 1.00;
174 convert["FT/SEC"]["FT/SEC"] = 1.00;
175 convert["KTS"]["KTS"] = 1.00;
176 convert["M/S"]["M/S"] = 1.0;
178 convert["FT*LBS"]["FT*LBS"] = 1.00;
179 convert["N*M"]["N*M"] = 1.00;
181 convert["M4*SEC/KG"]["M4*SEC/KG"] = 1.0;
182 convert["FT4*SEC/SLUG"]["FT4*SEC/SLUG"] = 1.0;
184 convert["PSI"]["PSI"] = 1.00;
185 convert["PSF"]["PSF"] = 1.00;
186 convert["INHG"]["INHG"] = 1.00;
187 convert["ATM"]["ATM"] = 1.0;
188 convert["PA"]["PA"] = 1.0;
189 convert["N/M2"]["N/M2"] = 1.00;
190 convert["LBS/FT2"]["LBS/FT2"] = 1.00;
192 convert["LBS/SEC"]["LBS/SEC"] = 1.00;
193 convert["KG/MIN"]["KG/MIN"] = 1.0;
194 convert["LBS/MIN"]["LBS/MIN"] = 1.0;
196 convert["LBS/HP*HR"]["LBS/HP*HR"] = 1.0;
197 convert["KG/KW*HR"]["KG/KW*HR"] = 1.0;
200 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 Element::~Element(void)
204 for (unsigned int i=0; i<children.size(); i++) delete children[i];
207 attribute_key.clear();
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 string Element::GetAttributeValue(string attr)
215 for (unsigned int i=0; i<attribute_key.size(); i++) {
216 if (attribute_key[i] == attr) select = i;
218 if (select < 0) return string("");
219 else return attributes[attr];
222 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 double Element::GetAttributeValueAsNumber(string attr)
226 string attribute = GetAttributeValue(attr);
228 if (attribute.empty()) return HUGE_VAL;
229 else return (atof(attribute.c_str()));
232 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 Element* Element::GetElement(unsigned int el)
236 if (children.size() > el) {
246 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 Element* Element::GetNextElement(void)
250 if (children.size() > element_index+1) {
252 return children[element_index];
259 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 string Element::GetDataLine(unsigned int i)
263 if (data_lines.size() > 0) return data_lines[i];
264 else return string("");
267 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269 double Element::GetDataAsNumber(void)
271 if (data_lines.size() == 1) {
272 return atof(data_lines[0].c_str());
273 } else if (data_lines.size() == 0) {
276 cerr << "Attempting to get single data value from multiple lines in element " << name << endl;
281 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 unsigned int Element::GetNumElements(string element_name)
285 unsigned int number_of_elements=0;
286 Element* el=FindElement(element_name);
288 number_of_elements++;
289 el=FindNextElement(element_name);
291 return number_of_elements;
294 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 Element* Element::FindElement(string el)
298 if (el.empty() && children.size() >= 1) {
302 for (unsigned int i=0; i<children.size(); i++) {
303 if (el == children[i]->GetName()) {
312 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 Element* Element::FindNextElement(string el)
317 if (element_index < children.size()) {
318 return children[element_index++];
324 for (unsigned int i=element_index; i<children.size(); i++) {
325 if (el == children[i]->GetName()) {
334 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 double Element::FindElementValueAsNumber(string el)
338 Element* element = FindElement(el);
340 return element->GetDataAsNumber();
342 cerr << "Attempting to get single data value from multiple lines" << endl;
347 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 string Element::FindElementValue(string el)
351 Element* element = FindElement(el);
353 return element->GetDataLine();
359 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 double Element::FindElementValueAsNumberConvertTo(string el, string target_units)
363 Element* element = FindElement(el);
366 cerr << "Attempting to get non-existent element " << el << endl;
370 string supplied_units = element->GetAttributeValue("unit");
372 if (!supplied_units.empty()) {
373 if (convert.find(supplied_units) == convert.end()) {
374 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
375 << " conversion in FGXMLElement.cpp." << endl;
378 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
379 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
380 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
385 double value = element->GetDataAsNumber();
386 if (!supplied_units.empty()) {
387 value *= convert[supplied_units][target_units];
393 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395 double Element::FindElementValueAsNumberConvertFromTo( string el,
396 string supplied_units,
399 Element* element = FindElement(el);
402 cerr << "Attempting to get non-existent element " << el << endl;
406 if (!supplied_units.empty()) {
407 if (convert.find(supplied_units) == convert.end()) {
408 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
409 << " conversion in FGXMLElement.cpp." << endl;
412 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
413 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
414 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
419 double value = element->GetDataAsNumber();
420 if (!supplied_units.empty()) {
421 value *= convert[supplied_units][target_units];
427 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429 FGColumnVector3 Element::FindElementTripletConvertTo( string target_units)
431 FGColumnVector3 triplet;
434 string supplied_units = GetAttributeValue("unit");
436 if (!supplied_units.empty()) {
437 if (convert.find(supplied_units) == convert.end()) {
438 cerr << endl << "Supplied unit: \"" << supplied_units << "\" does not exist (typo?). Add new unit"
439 << " conversion in FGXMLElement.cpp." << endl;
442 if (convert[supplied_units].find(target_units) == convert[supplied_units].end()) {
443 cerr << endl << "Supplied unit: \"" << supplied_units << "\" cannot be converted to "
444 << target_units << ". Add new unit conversion in FGXMLElement.cpp or fix typo" << endl;
449 item = FindElement("x");
450 if (!item) item = FindElement("roll");
452 value = item->GetDataAsNumber();
453 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
456 cerr << "Could not find an X triplet item for this column vector." << endl;
460 item = FindElement("y");
461 if (!item) item = FindElement("pitch");
463 value = item->GetDataAsNumber();
464 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
467 cerr << "Could not find a Y triplet item for this column vector." << endl;
471 item = FindElement("z");
472 if (!item) item = FindElement("yaw");
474 value = item->GetDataAsNumber();
475 if (!supplied_units.empty()) value *= convert[supplied_units][target_units];
478 cerr << "Could not find a Z triplet item for this column vector." << endl;
485 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
487 void Element::Print(unsigned int level)
489 unsigned int i, spaces;
492 for (spaces=0; spaces<=level; spaces++) cout << " "; // format output
493 cout << "Element Name: " << name;
494 for (i=0; i<attributes.size(); i++) {
495 cout << " " << attribute_key[i] << " = " << attributes[attribute_key[i]];
498 for (i=0; i<data_lines.size(); i++) {
499 for (spaces=0; spaces<=level; spaces++) cout << " "; // format output
500 cout << data_lines[i] << endl;
502 for (i=0; i<children.size(); i++) {
503 children[i]->Print(level);
507 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
509 void Element::AddAttribute(string name, string value)
511 attribute_key.push_back(name);
512 attributes[name] = value;
515 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
517 void Element::AddData(string d)
519 unsigned int string_start = (unsigned int)d.find_first_not_of(" \t");
520 if (string_start > 0) {
521 d.erase(0,string_start);
523 data_lines.push_back(d);
526 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528 } // end namespace JSBSim