1 /*
2  * CRRCsim - the Charles River Radio Control Club Flight Simulator Project
3  *   Copyright (C) 2010 - Jens Wilhelm Wulf (original author)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty off
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  */
20 #ifndef ROBOTFILE_H
21 # define ROBOTFILE_H
22 
23 # include <fstream>
24 # include <vector>
25 # include <string>
26 # include "../mod_misc/SimpleXMLTransfer.h"
27 
28 #define ROBOT_EULER_TO_INT16 (32767.0/2/M_PI)
29 
30 /**
31  * This reflects some parts of the specification of a mixed xml/binary file describing a
32  * fligth record or robot plane. There doesn't have to be a binary part!
33  *
34  * todo: care about endianess!
35  *
36  * @author Jens Wilhelm Wulf
37  */
38 class RobotFile
39 {
40 public:
41 
42   RobotFile(std::string filename);
43 
44   ~RobotFile();
45 
46   std::string ReadDescription();
47 
48   /**
49    *
50    */
ReadInt32(std::ifstream & in)51   static inline int ReadInt32(std::ifstream& in)
52   {
53     int nVal;
54     in.read((char*)&nVal, 4);
55     return(nVal);
56   }
57 
58   /**
59    *
60    */
ReadInt16(std::ifstream & in)61   static inline int ReadInt16(std::ifstream& in)
62   {
63     int nVal = 0;
64     in.read((char*)&nVal, 2);
65     if ((nVal & 0x8000) != 0)
66       nVal |= 0xFFFF0000;
67     return(nVal);
68   }
69 
70   /**
71    * returns double!
72    */
ReadFloat(std::ifstream & in)73   static inline double ReadFloat(std::ifstream& in)
74   {
75     float fVal;
76     in.read((char*)&fVal, 4);
77     return(fVal);
78   }
79 
80   /**
81    *
82    */
ReadDouble(std::ifstream & in)83   static inline double ReadDouble(std::ifstream& in)
84   {
85     double dVal;
86     in.read((char*)&dVal, 8);
87     return(dVal);
88   }
89 
90   /**
91    * store double as double
92    */
WriteDouble(std::ofstream & out,double dVal)93   static inline void WriteDouble(std::ofstream& out, double dVal)
94   {
95     // todo: endianess?
96     out.write((char*)&dVal, 8);
97   }
98 
99   /**
100    * store double as float
101    */
WriteFloat(std::ofstream & out,double dVal)102   static inline void WriteFloat(std::ofstream& out, double dVal)
103   {
104     float fVal = dVal;
105     // todo: endianess?
106     out.write((char*)&fVal, 4);
107   }
108 
109   /**
110    *
111    */
WriteInt32(std::ofstream & out,int nVal)112   static inline void WriteInt32(std::ofstream& out, int nVal)
113   {
114     out.write((char*)&nVal, 4);
115   }
116 
117   /**
118    *
119    */
WriteInt16(std::ofstream & out,double dVal)120   static inline void WriteInt16(std::ofstream& out, double dVal)
121   {
122     int nVal = (int)(dVal+0.5);
123     out.write((char*)&nVal, 2);
124   }
125 
126 private:
127   std::vector<SimpleXMLTransfer*> xmls;
128 };
129 #endif
130