CFx SDK Documentation  2023 SP0
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++ = (OdUInt16)rdInt16(streamBuf); // FELIX_CHANGE: OdUInt16 cast added, XENON-42909, CORE-16382
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 
#define OD_INT16_TO_BUFFPTR(pBuffPtr, val)
Definition: OdPlatform.h:811
#define odSwap4BytesNumber(n)
Definition: OdPlatform.h:680
bool isValidNonZeroIEEEDouble(const OdUInt8 *buf)
Definition: OdPlatform.h:698
#define odSwap4Bytes(bytes)
Definition: OdPlatform.h:682
#define OD_BYTES_TO_BUFFPTR(pBuffPtr, FromBuff, nCount)
Definition: OdPlatform.h:749
void fixDouble(double *pD)
Definition: OdPlatform.h:714
#define OD_INT16_FROM_BUFFPTR(pBuffPtr)
Definition: OdPlatform.h:804
#define odSwap8Bytes(bytes)
Definition: OdPlatform.h:681
#define OD_BYTES_FROM_BUFFPTR(pBuffPtr, ResBuff, nCount)
Definition: OdPlatform.h:746
#define odSwap2BytesNumber(n)
Definition: OdPlatform.h:679
unsigned int OdUInt32
short OdInt16
unsigned short OdUInt16
int OdInt32
unsigned char OdUInt8
wchar_t OdChar
ODA_ASSUME(sizeof(OdRxValue)==32)
Definition: Int64.h:43
static void rd3Doubles(OdStreamBuf &streamBuf, void *doubles)
static OdString getUnicodeStrFromBuffer(const OdUInt8 *&buffer, OdInt32 size)
static OdInt64 rdInt64(OdStreamBuf &streamBuf)
static OdString rdString(OdStreamBuf &streamBuf)
static OdInt16 rdInt16(OdStreamBuf &streamBuf)
static float rdFloat(OdStreamBuf &streamBuf)
static void wr2Doubles(OdStreamBuf &streamBuf, const void *doubles)
static void wrInt16(OdStreamBuf &streamBuf, OdInt16 value)
static void wrFloat(OdStreamBuf &out, float val)
static OdInt32 rdInt32(OdStreamBuf &streamBuf)
static void wrDoubles(OdStreamBuf &streamBuf, int numDoubles, const void *doubles)
static void rd2Doubles(OdStreamBuf &streamBuf, void *doubles)
static double rdDouble(OdStreamBuf &streamBuf)
static void wrInt64(OdStreamBuf &streamBuf, OdInt64 value)
static void wr3Doubles(OdStreamBuf &streamBuf, const void *doubles)
static void wrString(OdStreamBuf &streamBuf, const OdString &val)
static void rdDoubles(OdStreamBuf &streamBuf, int numDoubles, void *doubles)
static void wrDouble(OdStreamBuf &streamBuf, double value)
static void wrInt32(OdStreamBuf &streamBuf, OdInt32 value)
static void putUnicodeStrToBuffer(const OdString &val, OdUInt8 *&buffer)
virtual void getBytes(void *buffer, OdUInt32 numBytes)
virtual void putBytes(const void *buffer, OdUInt32 numBytes)
const OdChar * c_str() const
Definition: OdString.h:200
OdChar * getBufferSetLength(int length)
OdChar * getBuffer(int minBufLength)
void releaseBuffer(int newLength=-1)
int getLength() const
Definition: OdString.h:130
GLuint buffer
Definition: gles2_ext.h:178
GLsizeiptr size
Definition: gles2_ext.h:182
GLsizei const GLfloat * value
Definition: gles2_ext.h:302