]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/input_output/FGXMLElement.h
remove macintosh (note: not MacOS-X) and MWERKS workarounds.
[flightgear.git] / src / FDM / JSBSim / input_output / FGXMLElement.h
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  File:         FGXMLElement.h
4  Author:       Jon S. Berndt
5  Date started: 9/28/04
6
7  ------------- Copyright (C) 2004  Jon S. Berndt (jsb@hal-pc.org) -------------
8
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17  details.
18
19  You should have received a copy of the GNU Lesser General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
27 SENTRY
28 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
29
30 #ifndef XMLELEMENT_H
31 #define XMLELEMENT_H
32
33 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 INCLUDES
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
36
37 #include <string>
38 #include <map>
39 #include <iostream>
40 #include <vector>
41
42 using std::string;
43 using std::map;
44 using std::vector;
45 using std::cout;
46 using std::endl;
47
48 #include <math/FGColumnVector3.h>
49
50 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 DEFINITIONS
52 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53
54 #define ID_XMLELEMENT "$Id$"
55
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 FORWARD DECLARATIONS
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59
60 namespace JSBSim {
61
62 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 CLASS DOCUMENTATION
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
65
66 /** Encapsulates an XML element.
67     This class handles the creation, storage, and manipulation of XML elements.
68     This class also can convert supplied values as follows:
69
70     convert ["from"]["to"] = factor, so: from * factor = to
71     - convert["M"]["FT"] = 3.2808399;
72     - convert["FT"]["M"] = 1.0/convert["M"]["FT"];
73     - convert["M2"]["FT2"] = convert["M"]["FT"]*convert["M"]["FT"];
74     - convert["FT2"]["M2"] = 1.0/convert["M2"]["FT2"];
75     - convert["FT"]["IN"] = 12.0;
76     - convert["IN"]["FT"] = 1.0/convert["FT"]["IN"];
77     - convert["LBS"]["KG"] = 0.45359237;
78     - convert["KG"]["LBS"] = 1.0/convert["LBS"]["KG"];
79     - convert["SLUG*FT2"]["KG*M2"] = 1.35594;
80     - convert["KG*M2"]["SLUG*FT2"] = 1.0/convert["SLUG*FT2"]["KG*M2"];
81     - convert["RAD"]["DEG"] = 360.0/(2.0*3.1415926);
82     - convert["DEG"]["RAD"] = 1.0/convert["RAD"]["DEG"];
83     - convert["LBS/FT"]["N/M"] = 14.5939;
84     - convert["LBS/FT/SEC"]["N/M/SEC"] = 14.5939;
85     - convert["N/M"]["LBS/FT"] = 1.0/convert["LBS/FT"]["N/M"];
86     - convert["N/M/SEC"]["LBS/FT/SEC"] = 1.0/convert["LBS/FT/SEC"]["N/M/SEC"];
87     - convert["WATTS"]["HP"] = 0.001341022;
88     - convert["HP"]["WATTS"] = 1.0/convert["WATTS"]["HP"];
89     - convert["N"]["LBS"] = 0.22482;
90     - convert["LBS"]["N"] = 1.0/convert["N"]["LBS"];
91     - convert["KTS"]["FT/SEC"] = ktstofps;
92     - convert["KG/MIN"]["LBS/MIN"] = convert["KG"]["LBS"];
93
94     - convert["M"]["M"] = 1.00;
95     - convert["FT"]["FT"] = 1.00;
96     - convert["IN"]["IN"] = 1.00;
97     - convert["DEG"]["DEG"] = 1.00;
98     - convert["RAD"]["RAD"] = 1.00;
99     - convert["M2"]["M2"] = 1.00;
100     - convert["FT2"]["FT2"] = 1.00;
101     - convert["KG*M2"]["KG*M2"] = 1.00;
102     - convert["SLUG*FT2"]["SLUG*FT2"] = 1.00;
103     - convert["KG"]["KG"] = 1.00;
104     - convert["LBS"]["LBS"] = 1.00;
105     - convert["LBS/FT"]["LBS/FT"] = 1.00;
106     - convert["N/M"]["N/M"] = 1.00;
107     - convert["LBS/FT/SEC"]["LBS/FT/SEC"] = 1.00;
108     - convert["N/M/SEC"]["N/M/SEC"] = 1.00;
109     - convert["PSI"]["PSI"] = 1.00;
110     - convert["INHG"]["INHG"] = 1.00;
111     - convert["HP"]["HP"] = 1.00;
112     - convert["N"]["N"] = 1.00;
113     - convert["WATTS"]["WATTS"] = 1.00;
114     - convert["KTS"]["KTS"] = 1.0;
115     - convert["FT/SEC"]["FT/SEC"] = 1.0;
116     - convert["KG/MIN"]["KG/MIN"] = 1.0;
117     - convert["LBS/MIN"]["LBS/MIN"] = 1.0;
118
119     Where:
120     - N = newtons
121     - M = meters
122     - M2 = meters squared
123     - KG = kilograms
124     - LBS = pounds force
125     - FT = feet
126     - FT2 = feet squared
127     - SEC = seconds
128     - MIN = minutes
129     - SLUG = slug
130     - DEG = degrees
131     - RAD = radians
132     - WATTS = watts
133
134     @author Jon S. Berndt
135     @version $Id$
136 */
137
138 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 CLASS DECLARATION
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
141
142 class Element {
143 public:
144   /** Constructor
145       @param nm the name of this element (if given)
146       */
147   Element(string nm);
148   /// Destructor
149   ~Element(void);
150
151   /** Retrieves an attribute.
152       @param key specifies the attribute key to retrieve the value of.
153       @return the key value (as a string), or the empty string if no such
154               attribute exists. */
155   string GetAttributeValue(string key);
156
157   /** Retrieves an attribute value as a double precision real number.
158       @param key specifies the attribute key to retrieve the value of.
159       @return the key value (as a number), or the HUGE_VAL if no such
160               attribute exists. */
161   double GetAttributeValueAsNumber(string key);
162
163   /** Retrieves the element name.
164       @return the element name, or the empty string if no name has been set.*/
165   string GetName(void) {return name;}
166
167   /** Gets a line of data belonging to an element.
168       @param i the index of the data line to return (0 by default).
169       @return a string representing the data line requested, or the empty string
170               if none exists.*/
171   string GetDataLine(unsigned int i=0);
172
173   /// Returns the number of lines of data stored
174   unsigned int GetNumDataLines(void) {return (unsigned int)data_lines.size();}
175
176   /// Returns the number of child elements for this element.
177   unsigned int GetNumElements(void) {return (unsigned int)children.size();}
178
179   /// Returns the number of named child elements for this element.
180   unsigned int GetNumElements(string);
181
182   /** Converts the element data to a number.
183       This function attempts to convert the first (and presumably only) line of
184       data "owned" by the element into a real number. If there is not exactly one
185       line of data owned by the element, then HUGE_VAL is returned.
186       @return the numeric value of the data owned by the element.*/
187   double GetDataAsNumber(void);
188
189   /** Returns a pointer to the element requested by index.
190       This function also resets an internal counter to the index, so that
191       subsequent calls to GetNextElement() will return the following
192       elements sequentially, until the last element is reached. At that point,
193       GetNextElement() will return NULL.
194       @param el the index of the requested element (0 by default)
195       @return a pointer to the Element, or 0 if no valid element exists. */
196   Element* GetElement(unsigned int el=0);
197
198   /** Returns a pointer to the next element in the list.
199       The function GetElement() must be called first to be sure that this
200       function will return the correct element. The call to GetElement() resets
201       the internal counter to zero. Subsequent calls to GetNextElement() return
202       a pointer to subsequent elements in the list. When the final element is
203       reached, 0 is returned.
204       @return a pointer to the next Element in the sequence, or 0 if no valid
205               Element is present. */
206   Element* GetNextElement(void);
207
208   /** Returns a pointer to the parent of an element.
209       @return a pointer to the parent Element, or 0 if this is the top level Element. */
210   Element* GetParent(void) {return parent;}
211
212   /** Searches for a specified element.
213       Finds the first element that matches the supplied string, or simply the first
214       element if no search string is supplied. This function call resets the internal
215       element counter to the first element.
216       @param el the search string (empty string by default).
217       @return a pointer to the first element that matches the supplied search string. */
218   Element* FindElement(string el="");
219
220   /** Searches for the next element as specified.
221       This function would be called after FindElement() is first called (in order to
222       reset the internal counter). If no argument is supplied (or the empty string)
223       a pointer to the very next element is returned. Otherwise, the next occurence
224       of the named element is returned. If the end of the list is reached, 0 is
225       returned.
226       @param el the name of the next element to find.
227       @return the pointer to the found element, or 0 if no appropriate element us
228               found.*/
229   Element* FindNextElement(string el="");
230
231   /** Searches for the named element and returns the string data belonging to it.
232       This function allows the data belonging to a named element to be returned
233       as a string. If no element is found, the empty string is returned. If no
234       argument is supplied, the data string for the first element is returned.
235       @param el the name of the element being searched for (the empty string by
236       default)
237       @return the data value for the named element as a string, or the empty
238               string if the element cannot be found. */
239   string FindElementValue(string el="");
240
241   /** Searches for the named element and returns the data belonging to it as a number.
242       This function allows the data belonging to a named element to be returned
243       as a double. If no element is found, HUGE_VAL is returned. If no
244       argument is supplied, the data for the first element is returned.
245       @param el the name of the element being searched for (the empty string by
246       default)
247       @return the data value for the named element as a double, or HUGE_VAL if the
248               data is missing. */
249   double FindElementValueAsNumber(string el="");
250
251   /** Searches for the named element and converts and returns the data belonging to it.
252       This function allows the data belonging to a named element to be returned
253       as a double. If no element is found, HUGE_VAL is returned. If no
254       argument is supplied, the data for the first element is returned. Additionally,
255       this function converts the value from the units specified in the config file (via
256       the UNITS="" attribute in the element definition) to the native units used by
257       JSBSim itself, as specified by the target_units parameter. The currently
258       allowable unit conversions are seen in the source file FGXMLElement.cpp.
259       Also, see above in the main documentation for this class.
260       @param el the name of the element being searched for (the empty string by
261       default)
262       @param target_units the string representing the native units used by JSBSim
263              to which the value returned will be converted.
264       @return the unit-converted data value for the named element as a double,
265               or HUGE_VAL if the data is missing. */
266   double FindElementValueAsNumberConvertTo(string el, string target_units);
267
268   /** Searches for the named element and converts and returns the data belonging to it.
269       This function allows the data belonging to a named element to be returned
270       as a double. If no element is found, HUGE_VAL is returned. If no
271       argument is supplied, the data for the first element is returned. Additionally,
272       this function converts the value from the units specified in the supplied_units
273       parameter to the units specified in the target_units parameter. JSBSim itself,
274       as specified by the target_units parameter. The currently allowable unit
275       conversions are seen in the source file FGXMLElement.cpp. Also, see above
276       in the main documentation for this class.
277       @param el the name of the element being searched for (the empty string by
278       default)
279       @param supplied_units the string representing the units of the value as
280              supplied by the config file.
281       @param target_units the string representing the native units used by JSBSim
282              to which the value returned will be converted.
283       @return the unit-converted data value for the named element as a double,
284               or HUGE_VAL if the data is missing. */
285   double FindElementValueAsNumberConvertFromTo( string el,
286                                                 string supplied_units,
287                                                 string target_units);
288
289   /** Composes a 3-element column vector for the supplied location or orientation.
290       This function processes a LOCATION or ORIENTATION construct, returning a
291       filled-out 3-element column vector containing the X, Y, Z or ROLL, PITCH,
292       YAW elements found in the supplied element. If one of the mentioned components
293       is not found, that component is set to zero and a warning message is printed.
294       All three elements should be supplied.
295       @param target_units the string representing the native units used by JSBSim
296              to which the value returned will be converted.
297       @return a column vector object built from the LOCATION or ORIENT components. */
298   FGColumnVector3 FindElementTripletConvertTo( string target_units);
299
300   /** This function sets the value of the parent class attribute to the supplied
301       Element pointer.
302       @param p pointer to the parent Element. */
303   void SetParent(Element* p) {parent = p;}
304
305   /** Adds a child element to the list of children stored for this element.
306   *   @param el Child element to add. */
307   void AddChildElement(Element* el) {children.push_back(el);}
308
309   /** Stores an attribute belonging to this element.
310   *   @param name The string name of the attribute.
311   *   @param value The string value of the attribute. */
312   void AddAttribute(string name, string value);
313
314   /** Stores data belonging to this element.
315   *   @param d the data to store. */
316   void AddData(string d);
317
318   /** Prints the element.
319   *   Prints this element and calls the Print routine for child elements.
320   *   @param d The tab level. A level corresponds to a single space. */
321   void Print(unsigned int level=0);
322
323 private:
324   string name;
325   map <string, string> attributes;
326   vector <string> data_lines;
327   vector <Element*> children;
328   vector <string> attribute_key;
329   Element *parent;
330   unsigned int element_index;
331   typedef map <string, map <string, double> > tMapConvert;
332   tMapConvert convert;
333 };
334
335 } // namespace JSBSim
336
337 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338
339 #endif