CFx SDK Documentation  2020SP3
OdPlatformStreamer.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 
25 #ifndef _OD_PLATFORMSTREAMER_H_
26 #define _OD_PLATFORMSTREAMER_H_
27 
28 #include "OdPlatform.h"
29 #include "OdStreamBuf.h"
30 #include "OdString.h"
31 
37 {
38 public:
43  static OdInt16 rdInt16 (OdStreamBuf& streamBuf);
48  static OdInt32 rdInt32 (OdStreamBuf& streamBuf);
53  static OdInt64 rdInt64 (OdStreamBuf& streamBuf);
58  static float rdFloat (OdStreamBuf& streamBuf);
63  static double rdDouble (OdStreamBuf& streamBuf);
64 
70  static void rd2Doubles(OdStreamBuf& streamBuf, void* doubles);
71 
77  static void rd3Doubles(OdStreamBuf& streamBuf, void* doubles);
78 
85  static void rdDoubles (OdStreamBuf& streamBuf, int numDoubles, void* doubles);
86 
92  static void wrInt16 (OdStreamBuf& streamBuf, OdInt16 value);
98  static void wrInt32 (OdStreamBuf& streamBuf, OdInt32 value);
104  static void wrInt64 (OdStreamBuf& streamBuf, OdInt64 value);
110  static void wrFloat (OdStreamBuf& out, float val);
116  static void wrDouble (OdStreamBuf& streamBuf, double value);
122  static void wr2Doubles(OdStreamBuf& streamBuf, const void* doubles);
128  static void wr3Doubles(OdStreamBuf& streamBuf, const void* doubles);
135  static void wrDoubles (OdStreamBuf& streamBuf, int numDoubles, const void* doubles);
136 
149  inline static OdString getUnicodeStrFromBuffer(const OdUInt8*& buffer, OdInt32 size);
150 
162  inline static void putUnicodeStrToBuffer(const OdString& val, OdUInt8*& buffer);
163 
167  inline static void wrString(OdStreamBuf& streamBuf, const OdString& val);
168 
172  inline static OdString rdString(OdStreamBuf& streamBuf);
173 };
174 
176 {
177  OdInt16 res;
178  streamBuf.getBytes(&res, sizeof(res));
179  odSwap2BytesNumber(res);
180  return res;
181 }
183 {
184  OdInt32 res;
185  streamBuf.getBytes(&res, sizeof(res));
186  odSwap4BytesNumber(res);
187  return res;
188 }
190 {
191  OdInt64 res;
192  streamBuf.getBytes(&res, sizeof(res));
193  odSwap8Bytes(&res);
194  return res;
195 }
196 inline float OdPlatformStreamer::rdFloat(OdStreamBuf& streamBuf)
197 {
198  float res;
199  streamBuf.getBytes(&res, sizeof(res));
200  odSwap4Bytes(&res);
201  return res;
202 }
203 inline double OdPlatformStreamer::rdDouble(OdStreamBuf& streamBuf)
204 {
205  double res;
206  ODA_ASSUME(sizeof(res) == 8)
207  streamBuf.getBytes(&res, 8);
208  odSwap8Bytes(&res);
209  // if unnormalized or NaN or infinity, set it to 0.0
210  if (!isValidNonZeroIEEEDouble((OdUInt8 *)&res))
211  return 0.0;
212  return res;
213 }
214 
215 inline void OdPlatformStreamer::rd2Doubles(OdStreamBuf& streamBuf, void* pRes2Doubles)
216 {
217  ODA_ASSUME(sizeof(double) == 8)
218  streamBuf.getBytes(pRes2Doubles, sizeof(double)*2);
219  fixDouble((double*)pRes2Doubles);
220  fixDouble((double*)pRes2Doubles+1);
221 }
222 inline void OdPlatformStreamer::rd3Doubles(OdStreamBuf& streamBuf, void* pRes3Doubles)
223 {
224  ODA_ASSUME(sizeof(double) == 8)
225  streamBuf.getBytes(pRes3Doubles, sizeof(double)*3);
226  fixDouble((double*)pRes3Doubles);
227  fixDouble((double*)pRes3Doubles+1);
228  fixDouble((double*)pRes3Doubles+2);
229 }
230 inline void OdPlatformStreamer::rdDoubles(OdStreamBuf& streamBuf, int n, void* pResDoubles)
231 {
232  ODA_ASSUME(sizeof(double) == 8)
233  streamBuf.getBytes(pResDoubles, sizeof(double)*n);
234  double* pD = (double*)pResDoubles;
235  while (n--) { fixDouble(pD++); }
236 }
237 
238 inline void OdPlatformStreamer::wrInt16(OdStreamBuf& streamBuf, OdInt16 val)
239 {
240  odSwap2BytesNumber(val);
241  streamBuf.putBytes(&val, sizeof(val));
242 }
243 inline void OdPlatformStreamer::wrInt32(OdStreamBuf& streamBuf, OdInt32 val)
244 {
245  odSwap4BytesNumber(val);
246  streamBuf.putBytes(&val, sizeof(val));
247 }
248 inline void OdPlatformStreamer::wrInt64(OdStreamBuf& streamBuf, OdInt64 val)
249 {
250  odSwap8Bytes(&val);
251  streamBuf.putBytes(&val, sizeof(val));
252 }
253 inline void OdPlatformStreamer::wrFloat(OdStreamBuf& streamBuf, float val)
254 {
255  odSwap4Bytes(&val);
256  streamBuf.putBytes(&val, sizeof(val));
257 }
258 inline void OdPlatformStreamer::wrDouble(OdStreamBuf& streamBuf, double val)
259 {
260  ODA_ASSUME(sizeof(double) == 8)
261  odSwap8Bytes(&val);
262  streamBuf.putBytes(&val, sizeof(val));
263 }
264 
266 {
267  OdString ret;
268 #if ODCHAR_IS_INT16LE
269  if (size == -1)
270  {
271  // null terminated
272  ret = (OdChar*)buffer;
273  buffer += ((ret.getLength() + 1) * 2);
274  }
275  else
276  {
277  OdUInt8* pBuf = (OdUInt8*)ret.getBuffer(size+1);
278  OD_BYTES_FROM_BUFFPTR(buffer, pBuf, (size<<1));
279  ret.releaseBuffer(size);
280  }
281 #else
282  OdInt32 finalSize = size;
283  if (size == -1 && buffer)
284  {
285  // null terminated
286  size = 1;
287  OdUInt16* tmp16 = (OdUInt16*)buffer;
288  for ( ; *tmp16; tmp16++)
289  size++;
290  finalSize = size - 1;
291  }
292  OdChar* pBuf = ret.getBuffer(size + 1);
293  for (OdInt32 i = 0; i < size; i++)
294  {
296  }
297  *pBuf = L'\0';
298  ret.releaseBuffer(finalSize);
299 #endif
300 
301  return ret;
302 }
303 
304 #ifndef ODCHAR_IS_INT16LE
305 #error "OdaDefs.h must be included before OdPlatformStreamer.h"
306 #endif
307 
309 {
310  OdInt32 nLen = val.getLength();
311 #if ODCHAR_IS_INT16LE
312  OD_BYTES_TO_BUFFPTR(buffer, val.c_str(), nLen*sizeof(OdChar));
313 #else
314  const OdChar* p = val.c_str();
315  for (OdUInt32 i = 0; i < nLen; i++)
316  {
318  }
319 #endif
320 }
321 
323 {
324  OdInt32 nLen = val.getLength();
325  wrInt32(streamBuf, nLen);
326 #if ODCHAR_IS_INT16LE
327  streamBuf.putBytes(val.c_str(), nLen * 2);
328 #else
329  const OdChar* p = val.c_str();
330  for (OdUInt32 i = 0; i < nLen; ++i)
331  wrInt16(streamBuf, (OdInt16)*p++);
332 #endif
333 }
334 
336 {
337  OdString ret;
338  OdInt32 nLen = rdInt32(streamBuf);
339  OdChar* pBuf = ret.getBufferSetLength(nLen);
340 #if ODCHAR_IS_INT16LE
341  streamBuf.getBytes(pBuf, nLen * 2);
342 #else
343  for (OdUInt32 i = 0; i < nLen; ++i)
344  *pBuf++ = rdInt16(streamBuf);
345 #endif
346  return ret;
347 }
348 
349 
350 // Verify that preprocessor symbol ODCHAR_IS_INT16LE has been correctly defined
351 #if ODCHAR_IS_INT16LE
352 #ifdef ODA_BIGENDIAN
353 #error "ODCHAR_IS_INT16LE is defined for a big endian platform"
354 #endif
355 ODA_ASSUME(sizeof(OdChar) == 2);
356 #endif
357 
358 
359 #ifdef ODA_BIGENDIAN
360 
361 inline void OdPlatformStreamer::wr2Doubles(OdStreamBuf& streamBuf, const void* p2Doubles)
362 {
363  wrDouble(streamBuf, *(((double*)p2Doubles)+0));
364  wrDouble(streamBuf, *(((double*)p2Doubles)+1));
365 }
366 inline void OdPlatformStreamer::wr3Doubles(OdStreamBuf& streamBuf, const void* p3Doubles)
367 {
368  wrDouble(streamBuf, *(((double*)p3Doubles)+0));
369  wrDouble(streamBuf, *(((double*)p3Doubles)+1));
370  wrDouble(streamBuf, *(((double*)p3Doubles)+2));
371 }
372 inline void OdPlatformStreamer::wrDoubles(OdStreamBuf& streamBuf, int n, const void* pDoubles)
373 {
374  while(n--) wrDouble(streamBuf, ((double*)pDoubles)[n]);
375 }
376 
377 #else // #ifdef ODA_BIGENDIAN
378 
379 inline void OdPlatformStreamer::wr2Doubles(OdStreamBuf& streamBuf, const void* p2Doubles)
380 {
381  ODA_ASSUME(sizeof(double) == 8)
382  streamBuf.putBytes(p2Doubles, sizeof(double)*2);
383 }
384 inline void OdPlatformStreamer::wr3Doubles(OdStreamBuf& streamBuf, const void* p3Doubles)
385 {
386  ODA_ASSUME(sizeof(double) == 8)
387  streamBuf.putBytes(p3Doubles, sizeof(double)*3);
388 }
389 inline void OdPlatformStreamer::wrDoubles(OdStreamBuf& streamBuf, int n, const void* pDoubles)
390 {
391  ODA_ASSUME(sizeof(double) == 8)
392  streamBuf.putBytes(pDoubles, sizeof(double) * n);
393 }
394 
395 #endif // ODA_BIGENDIAN
396 
397 #endif // _OD_PLATFORMSTREAMER_H_
398 
OdPlatformStreamer::rdInt32
static OdInt32 rdInt32(OdStreamBuf &streamBuf)
Definition: OdPlatformStreamer.h:182
OdPlatformStreamer::wrString
static void wrString(OdStreamBuf &streamBuf, const OdString &val)
Definition: OdPlatformStreamer.h:322
OD_BYTES_TO_BUFFPTR
#define OD_BYTES_TO_BUFFPTR(pBuffPtr, FromBuff, nCount)
Definition: OdPlatform.h:749
OdPlatformStreamer::wrDouble
static void wrDouble(OdStreamBuf &streamBuf, double value)
Definition: OdPlatformStreamer.h:258
OdString
Definition: OdString.h:95
OdStreamBuf::getBytes
virtual void getBytes(void *buffer, OdUInt32 numBytes)
OdPlatformStreamer::rdDouble
static double rdDouble(OdStreamBuf &streamBuf)
Definition: OdPlatformStreamer.h:203
OdPlatform.h
OdUInt8
unsigned char OdUInt8
Definition: OdPlatformSettings.h:759
OdPlatformStreamer::rdInt16
static OdInt16 rdInt16(OdStreamBuf &streamBuf)
Definition: OdPlatformStreamer.h:175
OdInt64
Definition: Int64.h:43
OdPlatformStreamer::rd3Doubles
static void rd3Doubles(OdStreamBuf &streamBuf, void *doubles)
Definition: OdPlatformStreamer.h:222
OdPlatformStreamer::rdInt64
static OdInt64 rdInt64(OdStreamBuf &streamBuf)
Definition: OdPlatformStreamer.h:189
buffer
GLuint buffer
Definition: gles2_ext.h:178
OdPlatformStreamer::rdFloat
static float rdFloat(OdStreamBuf &streamBuf)
Definition: OdPlatformStreamer.h:196
OdPlatformStreamer::wrInt32
static void wrInt32(OdStreamBuf &streamBuf, OdInt32 value)
Definition: OdPlatformStreamer.h:243
OdString::getBuffer
OdChar * getBuffer(int minBufLength)
OdPlatformStreamer::wr2Doubles
static void wr2Doubles(OdStreamBuf &streamBuf, const void *doubles)
Definition: OdPlatformStreamer.h:379
OdPlatformStreamer::putUnicodeStrToBuffer
static void putUnicodeStrToBuffer(const OdString &val, OdUInt8 *&buffer)
Definition: OdPlatformStreamer.h:308
OdUInt16
unsigned short OdUInt16
Definition: OdPlatformSettings.h:760
odSwap2BytesNumber
#define odSwap2BytesNumber(n)
Definition: OdPlatform.h:679
OdPlatformStreamer::wrInt64
static void wrInt64(OdStreamBuf &streamBuf, OdInt64 value)
Definition: OdPlatformStreamer.h:248
OdUInt32
unsigned int OdUInt32
Definition: OdPlatformSettings.h:783
OdInt16
short OdInt16
Definition: OdPlatformSettings.h:756
OdString::releaseBuffer
void releaseBuffer(int newLength=-1)
size
GLsizeiptr size
Definition: gles2_ext.h:182
ODA_ASSUME
ODA_ASSUME(sizeof(OdRxValue)==32)
OdPlatformStreamer::rd2Doubles
static void rd2Doubles(OdStreamBuf &streamBuf, void *doubles)
Definition: OdPlatformStreamer.h:215
OD_INT16_TO_BUFFPTR
#define OD_INT16_TO_BUFFPTR(pBuffPtr, val)
Definition: OdPlatform.h:811
OdString::getBufferSetLength
OdChar * getBufferSetLength(int length)
odSwap4BytesNumber
#define odSwap4BytesNumber(n)
Definition: OdPlatform.h:680
OdStreamBuf.h
OdString.h
OdPlatformStreamer::rdString
static OdString rdString(OdStreamBuf &streamBuf)
Definition: OdPlatformStreamer.h:335
OD_INT16_FROM_BUFFPTR
#define OD_INT16_FROM_BUFFPTR(pBuffPtr)
Definition: OdPlatform.h:804
OdInt32
int OdInt32
Definition: OdPlatformSettings.h:782
OdStreamBuf::putBytes
virtual void putBytes(const void *buffer, OdUInt32 numBytes)
OdPlatformStreamer::getUnicodeStrFromBuffer
static OdString getUnicodeStrFromBuffer(const OdUInt8 *&buffer, OdInt32 size)
Definition: OdPlatformStreamer.h:265
OdChar
wchar_t OdChar
Definition: OdPlatformSettings.h:745
odSwap8Bytes
#define odSwap8Bytes(bytes)
Definition: OdPlatform.h:681
OdPlatformStreamer::wrFloat
static void wrFloat(OdStreamBuf &out, float val)
Definition: OdPlatformStreamer.h:253
odSwap4Bytes
#define odSwap4Bytes(bytes)
Definition: OdPlatform.h:682
OdPlatformStreamer::wrInt16
static void wrInt16(OdStreamBuf &streamBuf, OdInt16 value)
Definition: OdPlatformStreamer.h:238
OdPlatformStreamer::rdDoubles
static void rdDoubles(OdStreamBuf &streamBuf, int numDoubles, void *doubles)
Definition: OdPlatformStreamer.h:230
OdPlatformStreamer
Definition: OdPlatformStreamer.h:37
OdPlatformStreamer::wrDoubles
static void wrDoubles(OdStreamBuf &streamBuf, int numDoubles, const void *doubles)
Definition: OdPlatformStreamer.h:389
OdPlatformStreamer::wr3Doubles
static void wr3Doubles(OdStreamBuf &streamBuf, const void *doubles)
Definition: OdPlatformStreamer.h:384
OD_BYTES_FROM_BUFFPTR
#define OD_BYTES_FROM_BUFFPTR(pBuffPtr, ResBuff, nCount)
Definition: OdPlatform.h:746
fixDouble
void fixDouble(double *pD)
Definition: OdPlatform.h:714
OdStreamBuf
Definition: OdStreamBuf.h:67
value
GLsizei const GLfloat * value
Definition: gles2_ext.h:302
OdString::c_str
const OdChar * c_str() const
Definition: OdString.h:200
isValidNonZeroIEEEDouble
bool isValidNonZeroIEEEDouble(const OdUInt8 *buf)
Definition: OdPlatform.h:698
OdString::getLength
int getLength() const
Definition: OdString.h:130