MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
bufmod.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbutil/bufmod.cc,v 1.12 2017/10/15 15:41:23 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 //#include "unistd.h"
35 //#include "ac/f2c.h"
36 
37 #include "parser.h"
38 #include "bufmod.h"
39 
40 // FIXME: conditional build?
41 // #include <arpa/inet.h>
42 // Some systems require the inclusion of <netinet/in.h> instead of <arpa/inet.h>.
43 // #include <netinet/in.h>
44 
45 BufCast::BufCast(size_t offset)
46 : m_offset(offset)
47 {
48  NO_OP;
49 }
50 
52 {
53  NO_OP;
54 }
55 
56 #if 0 // moved to bufmod.h
57 template <class T>
58 class TBufCast : public BufCast {
59 public:
60  TBufCast(size_t offset) : BufCast(offset) { NO_OP; };
61 
62  size_t size(void) const {
63  return sizeof(T);
64  };
65 
66  size_t offset(void) const {
67  return m_offset;
68  };
69 
70  // "cast" in the sense that whatever type comes from the stream,
71  // it is cast into a doublereal
72  doublereal cast(const void *pFrom) const {
73  const char *p = &((const char *)pFrom)[m_offset];
74  return doublereal(*((T *)p));
75  };
76 
77  // "uncast" in the sense that a doublereal is transformed into
78  // the correct type for the stream
79  void uncast(void *pTo, doublereal d) const {
80  char *p = &((char *)pTo)[m_offset];
81  ((T *)p)[0] = T(d);
82  };
83 
84  BufCast *copy(size_t offset) const {
85  return new TBufCast<T>(offset);
86  };
87 };
88 
89 template <typename T>
90 static T
91 mbswap(const T in)
92 {
93  const char *pin = (const char *)&in;
94  T out;
95  char *pout = (char *)&out;
96 
97  for (unsigned int i = 0; i < sizeof(T)/2; i++) {
98  pout[i] = pin[sizeof(T) - 1 - i];
99  pout[sizeof(T) - 1 - i] = pin[i];
100  }
101 
102  return out;
103 }
104 
105 template <int8_t>
106 static int8_t
107 mbswap(const int8_t in)
108 {
109  return in;
110 }
111 
112 template <uint8_t>
113 static uint8_t
114 mbswap(const uint8_t in)
115 {
116  return in;
117 }
118 
119 template <class T>
120 class TBufCastHToN : public TBufCast<T> {
121 public:
122  TBufCastHToN(size_t offset) : TBufCast<T>(offset) {};
123 
124  // "cast" in the sense that whatever type comes from the stream,
125  // it is cast into a doublereal
126  doublereal cast(const void *pFrom) const {
127  const char *p = &((const char *)pFrom)[TBufCast<T>::m_offset];
128  return mbswap<T>(*((T *)p));
129  };
130 
131  // "uncast" in the sense that a doublereal is transformed into
132  // the correct type for the stream
133  void uncast(void *pTo, doublereal d) const {
134  char *p = &((char *)pTo)[TBufCast<T>::m_offset];
135  ((T *)p)[0] = mbswap<T>(T(d));
136  };
137 
138  BufCast *copy(size_t offset) const {
139  return new TBufCastHToN<T>(offset);
140  };
141 };
142 #endif
143 
144 
145 static BufCast *
146 ReadOneBufCast(HighParser& HP, size_t& offset, TypeMap_t& swapmap, bool bNoSkip = false)
147 {
148  BufCast *pBC(0);
149 
150  if (HP.IsKeyWord("int8_t")) {
151  TypeMap_t::const_iterator i = swapmap.find(typeid(int8_t).name());
152  if (i->second) {
153  pBC = new TBufCastHToN<int8_t>(offset);
154 
155  } else {
156  pBC = new TBufCast<int8_t>(offset);
157  }
158  offset += sizeof(int8_t);
159 
160  } else if (HP.IsKeyWord("uint8_t")) {
161  TypeMap_t::const_iterator i = swapmap.find(typeid(uint8_t).name());
162  if (i->second) {
163  pBC = new TBufCastHToN<uint8_t>(offset);
164 
165  } else {
166  pBC = new TBufCast<uint8_t>(offset);
167  }
168  offset += sizeof(uint8_t);
169 
170  } else if (HP.IsKeyWord("int16_t")) {
171  TypeMap_t::const_iterator i = swapmap.find(typeid(int16_t).name());
172  if (i->second) {
173  pBC = new TBufCastHToN<int16_t>(offset);
174 
175  } else {
176  pBC = new TBufCast<int16_t>(offset);
177  }
178  offset += sizeof(int16_t);
179 
180  } else if (HP.IsKeyWord("uint16_t")) {
181  TypeMap_t::const_iterator i = swapmap.find(typeid(uint16_t).name());
182  if (i->second) {
183  pBC = new TBufCastHToN<uint16_t>(offset);
184 
185  } else {
186  pBC = new TBufCast<uint16_t>(offset);
187  }
188  offset += sizeof(uint16_t);
189 
190  } else if (HP.IsKeyWord("int32_t")) {
191  TypeMap_t::const_iterator i = swapmap.find(typeid(int32_t).name());
192  if (i->second) {
193  pBC = new TBufCastHToN<int32_t>(offset);
194 
195  } else {
196  pBC = new TBufCast<int32_t>(offset);
197  }
198  offset += sizeof(int32_t);
199 
200  } else if (HP.IsKeyWord("uint32_t")) {
201  TypeMap_t::const_iterator i = swapmap.find(typeid(uint32_t).name());
202  if (i->second) {
203  pBC = new TBufCastHToN<uint32_t>(offset);
204 
205  } else {
206  pBC = new TBufCast<uint32_t>(offset);
207  }
208  offset += sizeof(uint32_t);
209 
210  } else if (HP.IsKeyWord("float")) {
211  TypeMap_t::const_iterator i = swapmap.find(typeid(float).name());
212  if (i->second) {
213  pBC = new TBufCastHToN<float>(offset);
214 
215  } else {
216  pBC = new TBufCast<float>(offset);
217  }
218  offset += sizeof(float);
219 
220  } else if (HP.IsKeyWord("double")) {
221  TypeMap_t::const_iterator i = swapmap.find(typeid(double).name());
222  if (i->second) {
223  pBC = new TBufCastHToN<double>(offset);
224 
225  } else {
226  pBC = new TBufCast<double>(offset);
227  }
228  offset += sizeof(double);
229 
230  } else if (HP.IsKeyWord("skip") && !bNoSkip) {
231  integer skip = HP.GetInt();
232  if (skip < 0) {
233  silent_cerr("ReadOneBufCast: invalid number of bytes " << skip
234  << " to be skipped at line " << HP.GetLineData() << std::endl);
236  }
237  offset += skip;
238 
239 #if 0
240  } else if (HP.IsKeyWord("offset")) {
241  integer ioffset = HP.GetInt();
242  if (ioffset < 0) {
243  silent_cerr("ReadOneBufCast: invalid number of bytes " << ioffset
244  << " as offset at line " << HP.GetLineData() << std::endl);
246  }
247  offset = ioffset;
248 #endif
249 
250  } else {
251  ASSERT(0);
253  }
254 
255  return pBC;
256 }
257 
258 bool
260 {
261  const int i = 1;
262  return (*(char *)&i) != 0;
263 }
264 
265 void
267 {
268  swapmap.insert(TypeMap_t::value_type(typeid(int8_t).name(), false));
269  swapmap.insert(TypeMap_t::value_type(typeid(uint8_t).name(), false));
270  swapmap.insert(TypeMap_t::value_type(typeid(int16_t).name(), false));
271  swapmap.insert(TypeMap_t::value_type(typeid(uint16_t).name(), false));
272  swapmap.insert(TypeMap_t::value_type(typeid(int32_t).name(), false));
273  swapmap.insert(TypeMap_t::value_type(typeid(uint32_t).name(), false));
274  swapmap.insert(TypeMap_t::value_type(typeid(float).name(), false));
275  swapmap.insert(TypeMap_t::value_type(typeid(double).name(), false));
276 }
277 
280  TypeWordSet_t(const TypeMap_t& swapmap) : m_swapmap(swapmap) { NO_OP; };
281  bool IsWord(const std::string& s) const {
282  return m_swapmap.find(s) != m_swapmap.end();
283  };
284 };
285 
286 void
287 ReadBufCast(HighParser& HP, std::vector<BufCast *>& data)
288 {
289  TypeMap_t swapmap;
290  SwapMapInit(swapmap);
291 
292  if (HP.IsKeyWord("swap")) {
293  TypeWordSet_t typewordset(swapmap);
294  const char *s = HP.IsWord(typewordset);
295  if (s != 0) {
296  do {
297  TypeMap_t::iterator i = swapmap.find(std::string(s));
298  i->second = true;
299  } while ((s = HP.IsWord(typewordset)) != 0);
300 
301  } else {
302  bool bSwap(false);
303  if (HP.IsKeyWord("detect")) {
304  bSwap = bIsLittleEndian();
305 
306  } else {
307  bSwap = HP.GetYesNoOrBool();
308  }
309 
310  if (bSwap) {
311  for (TypeMap_t::iterator i = swapmap.begin(); i != swapmap.end(); ++i) {
312  i->second = true;
313  }
314  }
315  }
316  }
317 
318  if (HP.IsKeyWord("all")) {
319  size_t size(0), offset(0);
320  BufCast *pBC = 0;
321  pBC = ReadOneBufCast(HP, size, swapmap, true);
322 
323  data[0] = pBC;
324  for (size_t i = 1; i < data.size(); i++) {
325  offset += size;
326  data[i] = data[i - 1]->copy(offset);
327  }
328 
329  } else {
330  size_t offset(0);
331  for (size_t i = 0; i < data.size(); i++) {
332 retry:;
333  BufCast *pBC = 0;
334  pBC = ReadOneBufCast(HP, offset, swapmap);
335 
336  if (pBC == 0) {
337  // got skip
338  goto retry;
339  }
340  data[i] = pBC;
341  }
342  }
343 }
size_t size(void) const
Definition: bufmod.h:62
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual integer GetInt(integer iDefval=0)
Definition: parser.cc:1050
void SwapMapInit(TypeMap_t &swapmap)
Definition: bufmod.cc:266
doublereal cast(const void *pFrom) const
Definition: bufmod.h:126
virtual const char * IsWord(const HighParser::WordSet &ws)
Definition: parser.cc:977
virtual ~BufCast(void)
Definition: bufmod.cc:51
static BufCast * ReadOneBufCast(HighParser &HP, size_t &offset, TypeMap_t &swapmap, bool bNoSkip=false)
Definition: bufmod.cc:146
void uncast(void *pTo, doublereal d) const
Definition: bufmod.h:79
virtual HighParser::ErrOut GetLineData(void) const
Definition: parser.cc:681
TypeWordSet_t(const TypeMap_t &swapmap)
Definition: bufmod.cc:280
#define NO_OP
Definition: myassert.h:74
bool bIsLittleEndian(void)
Definition: bufmod.cc:259
virtual bool GetYesNoOrBool(bool bDefval=false)
Definition: parser.cc:1038
TBufCastHToN(size_t offset)
Definition: bufmod.h:122
size_t offset(void) const
Definition: bufmod.h:66
BufCast(size_t offset)
Definition: bufmod.cc:45
TBufCast(size_t offset)
Definition: bufmod.h:60
void uncast(void *pTo, doublereal d) const
Definition: bufmod.h:133
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
BufCast * copy(size_t offset) const
Definition: bufmod.h:138
doublereal cast(const void *pFrom) const
Definition: bufmod.h:72
BufCast * copy(size_t offset) const
Definition: bufmod.h:84
#define ASSERT(expression)
Definition: colamd.c:977
const TypeMap_t & m_swapmap
Definition: bufmod.cc:279
std::map< std::string, bool > TypeMap_t
Definition: bufmod.h:37
static T mbswap(const T in)
Definition: bufmod.h:91
size_t m_offset
Definition: bufmod.h:44
virtual BufCast * copy(size_t offset) const =0
bool IsWord(const std::string &s) const
Definition: bufmod.cc:281
double doublereal
Definition: colamd.c:52
void ReadBufCast(HighParser &HP, std::vector< BufCast * > &data)
Definition: bufmod.cc:287
long int integer
Definition: colamd.c:51
Definition: bufmod.h:42