zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/////////////////////////////////////////////////////////////////////////////// 
// Copyright (C) 2002-2016, Open Design Alliance (the "Alliance"). 
// All rights reserved. 
// 
// This software and its documentation and related materials are owned by 
// the Alliance. The software may only be incorporated into application 
// programs owned by members of the Alliance, subject to a signed 
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable  
// trade secrets of the Alliance and its suppliers. The software is also 
// protected by copyright law and international treaty provisions. Application  
// programs incorporating this software must include the following statement 
// with their copyright notices:
//   
//   This application incorporates Teigha(R) software pursuant to a license 
//   agreement with Open Design Alliance.
//   Teigha(R) Copyright (C) 2002-2016 by Open Design Alliance. 
//   All rights reserved.
//
// By use of this software, its documentation or related materials, you 
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////
 
 
#ifndef _OD_PLATFORMSTREAMER_H_
#define _OD_PLATFORMSTREAMER_H_
 
#include "OdPlatform.h"
#include "OdStreamBuf.h"
#include "DbHandle.h"
#include "OdString.h"
 
/** \details
    This class implements platform dependent stream I/O for various data types.
    <group Other_Classes>
*/
class OdPlatformStreamer
{
public:
  /** \details
    Reads a 16-bit integer from the specified StreamBuf object.
    \param streamBuf [in]  StreamBuf object from which the data are to be read.
  */
  static OdInt16    rdInt16   (OdStreamBuf& streamBuf);
  /** \details
    Reads a 32-bit integer from the specified StreamBuf object.
    \param streamBuf [in]  StreamBuf object from which the data are to be read.
  */
  static OdInt32    rdInt32   (OdStreamBuf& streamBuf);
  /** \details
    Reads a 64-bit integer from the specified StreamBuf object.
    \param streamBuf [in]  StreamBuf object from which the data are to be read.
  */
  static OdInt64    rdInt64   (OdStreamBuf& streamBuf);
  /** \details
    Reads a 32-bit floating point number from the specified StreamBuf object.
    \param streamBuf [in]  StreamBuf object from which the data are to be read.
  */
  static float      rdFloat   (OdStreamBuf& streamBuf);
  /** \details
    Reads a double from the specified StreamBuf object.
    \param streamBuf [in]  StreamBuf object from which the data are to be read.
  */
  static double     rdDouble  (OdStreamBuf& streamBuf);
  /** \details
    Reads a handle from the specified StreamBuf object.
    \param streamBuf [in]  StreamBuf object from which the data are to be read.
  */
  static OdDbHandle rdDbHandle(OdStreamBuf& streamBuf);
  /** \details
    Reads two doubles from the specified StreamBuf object.
    \param streamBuf [in]  StreamBuf object from which the data are to be read.
    \param doubles [out]  Array of doubles to receive the data.
  */
  static void       rd2Doubles(OdStreamBuf& streamBuf, void* doubles);
 
  /** \details
    Reads three doubles from the specified StreamBuf object.
    \param streamBuf [in]  StreamBuf object from which the data are to be read.
    \param doubles [out]  Array of doubles to receive the data.
  */
  static void       rd3Doubles(OdStreamBuf& streamBuf, void* doubles);
  
  /** \details
    Reads the specified number of doubles from the specified StreamBuf object.
    \param streamBuf [in]  StreamBuf object from which the data are to be read.
    \param doubles [out]  Array of doubles to receive the data.
    \param numDoubles [in]  Number of doubles.
  */
  static void       rdDoubles (OdStreamBuf& streamBuf, int numDoubles, void* doubles);
 
  /** \details
    Writes a 16-bit integer to the specified StreamBuf object.
    \param streamBuf [out]  StreamBuf object to which the data are to be written.
    \param value [in]  Value to be written.
  */
  static void       wrInt16   (OdStreamBuf& streamBuf, OdInt16 value);
  /** \details
    Writes a 32-bit integer to the specified StreamBuf object.
    \param streamBuf [out]  StreamBuf object to which the data are to be written.
    \param value [in]  Value to be written.
  */
  static void       wrInt32   (OdStreamBuf& streamBuf, OdInt32 value);
  /** \details
    Writes a 64-bit integer to the specified StreamBuf object.
    \param streamBuf [out]  StreamBuf object to which the data are to be written.
    \param value [in]  Value to be written.
  */
  static void       wrInt64   (OdStreamBuf& streamBuf, OdInt64 value);
  /** \details
    Writes a 32-bit floating point number to the specified StreamBuf object.
    \param streamBuf [out]  StreamBuf object to which the data are to be written.
    \param value [in]  Value to be written.
  */
  static void       wrFloat   (OdStreamBuf& out, float val);
  /** \details
    Writes a double to the specified StreamBuf object.
    \param streamBuf [out]  StreamBuf object to which the data are to be written.
    \param value [in]  Value to be written.
  */
  static void       wrDouble  (OdStreamBuf& streamBuf, double value);
  /** \details
    Writes a handle to the specified StreamBuf object.
    \param streamBuf [out]  StreamBuf object to which the data are to be written.
    \param value [in]  Value to be written.
  */
  static void       wrDbHandle(OdStreamBuf& streamBuf, OdDbHandle value);
  /** \details
    Writes two doubles to the specified StreamBuf object.
    \param streamBuf [out]  StreamBuf object to which the data are to be written.
    \param doubles [in]  Array of doubles to be written.
  */
  static void       wr2Doubles(OdStreamBuf& streamBuf, const void* doubles);
  /** \details
    Writes three doubles to the specified StreamBuf object.
    \param streamBuf [out]  StreamBuf object to which the data are to be written.
    \param doubles [in]  Array of doubles to be written.
  */
  static void       wr3Doubles(OdStreamBuf& streamBuf, const void* doubles);
  /** \details
    Writes the specified number of doubles to the specified StreamBuf object.
    \param streamBuf [out]  StreamBuf object to which the data are to be written.
    \param doubles [in]  Array of doubles to be written.
    \param numDoubles [in]  Number of doubles.
  */
  static void       wrDoubles (OdStreamBuf& streamBuf, int numDoubles, const void* doubles);
 
  /** \details
      Extracts a Unicode string from buffer, and returns it as an OdString.
 
      \param buffer [in/out] Buffer pointer to read the Unicode string from.
      \param size [in] Length of the Unicode string in characters (assumes 2 byte characters in buffer
               stored in little endian format), or -1 if the string is null terminated.
 
      \remarks
      The buffer pointer is incremented to the next byte past the string that
      was read in.  If size is > 0, size * 2 bytes will be read.  If size is -1,
      a null terminated string will be read from buffer, including the null byte.
  */
  inline static OdString getUnicodeStrFromBuffer(const OdUInt8*& buffer, OdInt32 size);
 
  /** \details
      Writes a Unicode string to the passed in buffer, in little endian format, 
      using 2 bytes per character.  The trailing null character is NOT written.
 
      \param val [in]  Unicode string to be written.
      \param buffer [in]  Char buffer to which will receive the Unicode data.
 
      \remarks
      The buffer pointer is incremented to the next byte past the string that
      was written. 
  */
  inline static void putUnicodeStrToBuffer(const OdString& val, OdUInt8*& buffer);
 
};
 
inline OdInt16 OdPlatformStreamer::rdInt16(OdStreamBuf& streamBuf)
{
  OdInt16 res;
  streamBuf.getBytes(&res, sizeof(res));
  odSwap2BytesNumber(res);
  return res;
}
inline OdInt32 OdPlatformStreamer::rdInt32(OdStreamBuf& streamBuf)
{
  OdInt32 res;
  streamBuf.getBytes(&res, sizeof(res));
  odSwap4BytesNumber(res);
  return res;
}
inline OdInt64 OdPlatformStreamer::rdInt64(OdStreamBuf& streamBuf)
{
  OdInt64 res;
  streamBuf.getBytes(&res, sizeof(res));
  odSwap8Bytes(&res);
  return res;
}
inline float OdPlatformStreamer::rdFloat(OdStreamBuf& streamBuf)
{
  float res;
  streamBuf.getBytes(&res, sizeof(res));
  odSwap4Bytes(&res);
  return res;
}
inline double OdPlatformStreamer::rdDouble(OdStreamBuf& streamBuf)
{
  double res;
  ODA_ASSUME(sizeof(res) == 8)
  streamBuf.getBytes(&res, 8);
  odSwap8Bytes(&res);
  // if unnormalized or NaN or infinity, set it to 0.0
  if (!isValidNonZeroIEEEDouble((OdUInt8 *)&res))
    return 0.0;
  return res;
}
inline OdDbHandle OdPlatformStreamer::rdDbHandle(OdStreamBuf& streamBuf)
{
  OdDbHandle res;
  ODA_ASSUME(sizeof(res) == 8)
  streamBuf.getBytes(&res, 8);
  odSwapInt64(&res);
  return res;
}
inline void OdPlatformStreamer::rd2Doubles(OdStreamBuf& streamBuf, void* pRes2Doubles)
{
  ODA_ASSUME(sizeof(double) == 8)
  streamBuf.getBytes(pRes2Doubles, sizeof(double)*2);
  fixDouble((double*)pRes2Doubles);
  fixDouble((double*)pRes2Doubles+1);
}
inline void OdPlatformStreamer::rd3Doubles(OdStreamBuf& streamBuf, void* pRes3Doubles)
{
  ODA_ASSUME(sizeof(double) == 8)
  streamBuf.getBytes(pRes3Doubles, sizeof(double)*3);
  fixDouble((double*)pRes3Doubles);
  fixDouble((double*)pRes3Doubles+1);
  fixDouble((double*)pRes3Doubles+2);
}
inline void OdPlatformStreamer::rdDoubles(OdStreamBuf& streamBuf, int n, void* pResDoubles)
{
  ODA_ASSUME(sizeof(double) == 8)
  streamBuf.getBytes(pResDoubles, sizeof(double)*n);
  double* pD = (double*)pResDoubles;
  while (n--) { fixDouble(pD++); }
}
 
inline void OdPlatformStreamer::wrInt16(OdStreamBuf& streamBuf, OdInt16 val)
{
  odSwap2BytesNumber(val);
  streamBuf.putBytes(&val, sizeof(val));
}
inline void OdPlatformStreamer::wrInt32(OdStreamBuf& streamBuf, OdInt32 val)
{
  odSwap4BytesNumber(val);
  streamBuf.putBytes(&val, sizeof(val));
}
inline void OdPlatformStreamer::wrInt64(OdStreamBuf& streamBuf, OdInt64 val)
{
  odSwap8Bytes(&val);
  streamBuf.putBytes(&val, sizeof(val));
}
inline void OdPlatformStreamer::wrFloat(OdStreamBuf& streamBuf, float val)
{
  odSwap4Bytes(&val);
  streamBuf.putBytes(&val, sizeof(val));
}
inline void OdPlatformStreamer::wrDouble(OdStreamBuf& streamBuf, double val)
{
  ODA_ASSUME(sizeof(double) == 8)
  odSwap8Bytes(&val);
  streamBuf.putBytes(&val, sizeof(val));
}
inline void OdPlatformStreamer::wrDbHandle(OdStreamBuf& streamBuf, OdDbHandle val)
{
  odSwapInt64(&val);
  streamBuf.putBytes(&val, sizeof(val));
}
 
inline OdString OdPlatformStreamer::getUnicodeStrFromBuffer(const OdUInt8*& buffer, OdInt32 size)
{
  OdString ret;
#if ODCHAR_IS_INT16LE
  if (size == -1)
  {
    // null terminated
    ret = (OdChar*)buffer;
    buffer += ((ret.getLength() + 1) * 2);
  }
  else
  {
    OdUInt8* pBuf = (OdUInt8*)ret.getBuffer(size+1);
    OD_BYTES_FROM_BUFFPTR(buffer, pBuf, (size<<1));
    ret.releaseBuffer(size);
  }
#else
  OdInt32 finalSize = size;
  if (size == -1)
  {
    // null terminated
    size = 1; 
    OdUInt16* tmp16 = (OdUInt16*)buffer;
    for ( ; *tmp16; tmp16++)
      size++;
    finalSize = size - 1;
  }
  OdChar* pBuf = ret.getBuffer(size + 1);
  for (OdInt32 i = 0; i < size; i++)
  {
    *pBuf++ = OdUInt16(OD_INT16_FROM_BUFFPTR(buffer));
  }
  *pBuf = L'\0';
  ret.releaseBuffer(finalSize);
#endif
 
  return ret;
}
 
#ifndef ODCHAR_IS_INT16LE
#error "OdaDefs.h must be included before OdPlatformStreamer.h"
#endif
 
void OdPlatformStreamer::putUnicodeStrToBuffer(const OdString& val, OdUInt8*& buffer)
{
  OdInt32 nLen = val.getLength();
#if ODCHAR_IS_INT16LE
  OD_BYTES_TO_BUFFPTR(buffer, val.c_str(), nLen*sizeof(OdChar));
#else
  const OdChar* p = val.c_str();
  for (OdUInt32 i = 0; i < nLen; i++)
  {
    OD_INT16_TO_BUFFPTR(buffer, (OdInt16)*p++);
  }
#endif
}
 
 
// Verify that preprocessor symbol ODCHAR_IS_INT16LE has been correctly defined
#if ODCHAR_IS_INT16LE
#ifdef ODA_BIGENDIAN
#error "ODCHAR_IS_INT16LE is defined for a big endian platform"
#endif
ODA_ASSUME(sizeof(OdChar) == 2);
#endif
 
 
#ifdef ODA_BIGENDIAN
 
inline void OdPlatformStreamer::wr2Doubles(OdStreamBuf& streamBuf, const void* p2Doubles)
{
  wrDouble(streamBuf, *(((double*)p2Doubles)+0));
  wrDouble(streamBuf, *(((double*)p2Doubles)+1));
}
inline void OdPlatformStreamer::wr3Doubles(OdStreamBuf& streamBuf, const void* p3Doubles)
{
  wrDouble(streamBuf, *(((double*)p3Doubles)+0));
  wrDouble(streamBuf, *(((double*)p3Doubles)+1));
  wrDouble(streamBuf, *(((double*)p3Doubles)+2));
}
inline void OdPlatformStreamer::wrDoubles(OdStreamBuf& streamBuf, int n, const void* pDoubles)
{
  while(n--) wrDouble(streamBuf, ((double*)pDoubles)[n]);
}
 
#else // #ifdef ODA_BIGENDIAN
 
inline void OdPlatformStreamer::wr2Doubles(OdStreamBuf& streamBuf, const void* p2Doubles)
{
  ODA_ASSUME(sizeof(double) == 8)
  streamBuf.putBytes(p2Doubles, sizeof(double)*2);
}
inline void OdPlatformStreamer::wr3Doubles(OdStreamBuf& streamBuf, const void* p3Doubles)
{
  ODA_ASSUME(sizeof(double) == 8)
  streamBuf.putBytes(p3Doubles, sizeof(double)*3);
}
inline void OdPlatformStreamer::wrDoubles(OdStreamBuf& streamBuf, int n, const void* pDoubles)
{
  ODA_ASSUME(sizeof(double) == 8)
  streamBuf.putBytes(pDoubles, sizeof(double) * n);
}
 
#endif // ODA_BIGENDIAN
 
#endif // _OD_PLATFORMSTREAMER_H_