root/OpenSceneGraph/trunk/src/osgPlugins/3ds/readwrite.cpp @ 10128

Revision 10128, 10.1 kB (checked in by robert, 4 years ago)

Fixed warning + bug

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * The 3D Studio File Format Library
3 * Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
4 * All rights reserved.
5 *
6 * This program is  free  software;  you can redistribute it and/or modify it
7 * under the terms of the  GNU Lesser General Public License  as published by
8 * the  Free Software Foundation;  either version 2.1 of the License,  or (at
9 * your option) any later version.
10 *
11 * This  program  is  distributed in  the  hope that it will  be useful,  but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or  FITNESS FOR A  PARTICULAR PURPOSE.  See the  GNU Lesser General Public 
14 * License for more details.
15 *
16 * You should  have received  a copy of the GNU Lesser General Public License
17 * along with  this program;  if not, write to the  Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * $Id$
21 */
22#define LIB3DS_EXPORT
23#include "readwrite.h"
24#include <osg/Endian>
25
26
27/*!
28 * \defgroup readwrite Portable Binary Input/Ouput
29 *
30 * \author J.E. Hoffmann <je-h@gmx.net>
31 */
32
33static bool s_requiresByteSwap = false;
34
35extern LIB3DSAPI void setByteOrder()
36{
37    s_requiresByteSwap = osg::getCpuByteOrder()==osg::BigEndian;
38}
39
40
41
42/*!
43 * \ingroup readwrite
44 *
45 * Read a byte from a file stream. 
46 *
47 * \param f  Input file stream.
48 *
49 * \return The byte read.
50 */
51Lib3dsByte
52lib3ds_byte_read(iostream *strm)
53{
54  Lib3dsByte b;
55
56  ASSERT(strm);
57  strm->read((char*)&b,1);
58  return(b);
59}
60
61
62/**
63 * Read a word from a file stream in little endian format.   
64 *
65 * \param f  Input file stream.
66 *
67 * \return The word read.
68 */
69Lib3dsWord
70lib3ds_word_read(iostream *strm)
71{
72  Lib3dsByte b[2];
73  Lib3dsWord w;
74
75  ASSERT(strm); 
76  strm->read((char*)&b,2);
77  w=((Lib3dsWord)b[1] << 8) |
78    ((Lib3dsWord)b[0]);
79  return(w);
80}
81
82
83/*!
84 * \ingroup readwrite
85 *
86 * Read a dword from file a stream in little endian format.   
87 *
88 * \param f  Input file stream.
89 *
90 * \return The dword read.
91 */
92Lib3dsDword
93lib3ds_dword_read(iostream *strm)
94{
95  Lib3dsByte b[4];
96  Lib3dsDword d;       
97                         
98  ASSERT(strm); 
99  strm->read((char*)&b,4);
100  d=((Lib3dsDword)b[3] << 24) |
101    ((Lib3dsDword)b[2] << 16) |
102    ((Lib3dsDword)b[1] << 8) |
103    ((Lib3dsDword)b[0]);
104  return(d);
105}
106
107
108/*!
109 * \ingroup readwrite
110 *
111 * Read a signed byte from a file stream.   
112 *
113 * \param f  Input file stream.
114 *
115 * \return The signed byte read.
116 */
117Lib3dsIntb
118lib3ds_intb_read(iostream *strm)
119{
120  Lib3dsIntb b;
121
122  ASSERT(strm); 
123  strm->read((char*)&b,1);
124  return(b);
125}
126
127
128/*!
129 * \ingroup readwrite
130 *
131 * Read a signed word from a file stream in little endian format.   
132 *
133 * \param f  Input file stream.
134 *
135 * \return The signed word read.
136 */
137Lib3dsIntw
138lib3ds_intw_read(iostream *strm)
139{
140  Lib3dsByte b[2];
141
142  ASSERT(strm);
143  strm->read((char*)&b,2);
144
145  if (s_requiresByteSwap)
146  {
147    osg::swapBytes2((char*)b);
148  }
149
150  return (*((Lib3dsIntw*)b));
151}
152
153
154/*!
155 * \ingroup readwrite
156 *
157 * Read a signed dword a from file stream in little endian format.   
158 *
159 * \param f  Input file stream.
160 *
161 * \return The signed dword read.
162 */
163Lib3dsIntd
164lib3ds_intd_read(iostream *strm)
165{
166  Lib3dsByte b[4];     
167                         
168  ASSERT(strm);
169  strm->read((char*)&b,4);
170
171  if (s_requiresByteSwap)
172  {
173    osg::swapBytes4((char*)b);
174  }
175
176  return (*((Lib3dsIntd*)b));
177
178}
179
180
181/*!
182 * \ingroup readwrite
183 *
184 * Read a float from a file stream in little endian format.   
185 *
186 * \param f  Input file stream.
187 *
188 * \return The float read.
189 */
190Lib3dsFloat
191lib3ds_float_read(iostream *strm)
192{
193  Lib3dsByte b[4];
194
195  ASSERT(strm);
196  b[0]=b[1]=b[2]=b[3]=0;
197  strm->read((char*)&b,4); 
198
199  if (s_requiresByteSwap)
200  {
201    osg::swapBytes4((char*)b);
202  }
203
204  return (*((Lib3dsFloat*)b));
205}
206
207
208/*!
209 * \ingroup readwrite
210 * \ingroup vector
211 *
212 * Read a vector from a file stream in little endian format.   
213 *
214 * \param v  The vector to store the data.
215 * \param f  Input file stream.
216 *
217 * \return The float read.
218 */
219Lib3dsBool
220lib3ds_vector_read(Lib3dsVector v, iostream *strm)
221{
222  v[0]=lib3ds_float_read(strm);
223  v[1]=lib3ds_float_read(strm);
224  v[2]=lib3ds_float_read(strm);
225
226  if (strm->fail()) {
227    return(LIB3DS_FALSE);
228  }
229
230  /*printf("lib3ds_vector_read %f %f %f\n",v[0],v[1],v[2]);*/
231
232  return(LIB3DS_TRUE);
233}
234
235
236/*!
237 * \ingroup readwrite
238 */
239Lib3dsBool
240lib3ds_rgb_read(Lib3dsRgb rgb, iostream *strm)
241{
242  rgb[0]=lib3ds_float_read(strm);
243  rgb[1]=lib3ds_float_read(strm);
244  rgb[2]=lib3ds_float_read(strm);
245
246  if (strm->fail()) {
247    return(LIB3DS_FALSE);
248  }
249  /*printf("lib3ds_rgb_read %f %f %f\n",rgb[0],rgb[1],rgb[2]);*/
250
251  return(LIB3DS_TRUE);
252}
253
254
255/*!
256 * \ingroup readwrite
257 *
258 * Read a zero-terminated string from a file stream.
259 *
260 * \param s       The buffer to store the read string.
261 * \param buflen  Buffer length.
262 * \param f       The input file stream.
263 *
264 * \return        True on success, False otherwise.
265 */
266Lib3dsBool
267lib3ds_string_read(char *s, int buflen, iostream *strm)
268{
269  int k=0;
270  ASSERT(s);
271  s--;
272  do
273  {
274      s++;
275      k++;
276      strm->read(s,1);
277  } while ((*s!=0) && (k<buflen));
278
279  if (strm->fail()) {
280    return(LIB3DS_FALSE);
281  }
282  return(LIB3DS_TRUE);
283}
284
285/*!
286 * \ingroup readwrite
287 *
288 * Writes a byte into a file stream.
289 *
290 * \param b  The byte to write to the file stream.
291 * \param f  The input file stream.
292 *
293 * \return   True on success, False otherwise.
294 */
295Lib3dsBool
296lib3ds_byte_write(Lib3dsByte b, iostream *strm)
297{
298  ASSERT(strm);
299  strm->write((char*)&b,1);
300  if (strm->fail()) {
301    return(LIB3DS_FALSE);
302  }
303  return(LIB3DS_TRUE);
304}
305
306
307/*!
308 * \ingroup readwrite
309 *
310 * Writes a word into a little endian file stream.
311 *
312 * \param w  The word to write to the file stream.
313 * \param f  The input file stream.
314 *
315 * \return   True on success, False otherwise.
316 */
317Lib3dsBool
318lib3ds_word_write(Lib3dsWord w, iostream *strm)
319{
320  Lib3dsByte b[2];
321
322  ASSERT(strm);
323  b[1]=(Lib3dsByte)(((Lib3dsWord)w & 0xFF00) >> 8);
324  b[0]=(Lib3dsByte)((Lib3dsWord)w & 0x00FF);
325  strm->write((char*)b,2);
326  if (strm->fail()) {
327    return(LIB3DS_FALSE);
328  }
329  return(LIB3DS_TRUE);
330}
331
332
333/*!
334 * \ingroup readwrite
335 *
336 * Writes a dword into a little endian file stream.
337 *
338 * \param d  The dword to write to the file stream.
339 * \param f  The input file stream.
340 *
341 * \return   True on success, False otherwise.
342 */
343Lib3dsBool
344lib3ds_dword_write(Lib3dsDword d, iostream *strm)
345{
346  Lib3dsByte b[4];
347
348  ASSERT(strm);
349  b[3]=(Lib3dsByte)(((Lib3dsDword)d & 0xFF000000) >> 24);
350  b[2]=(Lib3dsByte)(((Lib3dsDword)d & 0x00FF0000) >> 16);
351  b[1]=(Lib3dsByte)(((Lib3dsDword)d & 0x0000FF00) >> 8);
352  b[0]=(Lib3dsByte)(((Lib3dsDword)d & 0x000000FF));
353
354  strm->write((char*)b,4);
355  if (strm->fail()) {
356    return(LIB3DS_FALSE);
357  }
358  return(LIB3DS_TRUE);
359}
360
361
362/*!
363 * \ingroup readwrite
364 *
365 * Writes a signed byte in a file stream.
366 *
367 * \param b  The signed byte to write to the file stream.
368 * \param f  The input file stream.
369 *
370 * \return   True on success, False otherwise.
371 */
372Lib3dsBool
373lib3ds_intb_write(Lib3dsIntb b, iostream *strm)
374{
375  ASSERT(strm);
376  strm->write((const char*)&b,1);
377  if (strm->fail()) {
378    return(LIB3DS_FALSE);
379  }
380  return(LIB3DS_TRUE);
381}
382
383
384/*!
385 * \ingroup readwrite
386 *
387 * Writes a signed word into a little endian file stream.
388 *
389 * \param w  The signed word to write to the file stream.
390 * \param f  The input file stream.
391 *
392 * \return   True on success, False otherwise.
393 */
394Lib3dsBool
395lib3ds_intw_write(Lib3dsIntw w, iostream *strm)
396{
397  Lib3dsByte b[2];
398
399  ASSERT(strm);
400  b[1]=(Lib3dsByte)(((Lib3dsWord)w & 0xFF00) >> 8);
401  b[0]=(Lib3dsByte)((Lib3dsWord)w & 0x00FF);
402
403  strm->write((const char*)b,2);
404  if (strm->fail()) {
405    return(LIB3DS_FALSE);
406  }
407  return(LIB3DS_TRUE);
408}
409
410
411/*!
412 * \ingroup readwrite
413 *
414 * Writes a signed dword into a little endian file stream.
415 *
416 * \param d  The signed dword to write to the file stream.
417 * \param f  The input file stream.
418 *
419 * \return   True on success, False otherwise.
420 */
421Lib3dsBool
422lib3ds_intd_write(Lib3dsIntd d, iostream *strm)
423{
424  Lib3dsByte b[4];
425
426  ASSERT(strm);
427  b[3]=(Lib3dsByte)(((Lib3dsDword)d & 0xFF000000) >> 24);
428  b[2]=(Lib3dsByte)(((Lib3dsDword)d & 0x00FF0000) >> 16);
429  b[1]=(Lib3dsByte)(((Lib3dsDword)d & 0x0000FF00) >> 8);
430  b[0]=(Lib3dsByte)(((Lib3dsDword)d & 0x000000FF));
431
432  strm->write((const char*)b,4);
433  if (strm->fail()) {
434    return(LIB3DS_FALSE);
435  }
436  return(LIB3DS_TRUE);
437}
438
439
440/*!
441 * \ingroup readwrite
442 *
443 * Writes a float into a little endian file stream.
444 *
445 * \param f  The float to write to the file stream.
446 * \param f  The input file stream.
447 *
448 * \return   True on success, False otherwise.
449 */
450Lib3dsBool
451lib3ds_float_write(Lib3dsFloat l, iostream *strm)
452{
453  ASSERT(strm);
454
455  Lib3dsByte b[4];
456  Lib3dsByte* ptr = (Lib3dsByte*) (&l);
457
458  if (s_requiresByteSwap)
459  {
460      b[3] = *ptr++;
461      b[2] = *ptr++;
462      b[1] = *ptr++;
463      b[0] = *ptr++;
464  }
465  else
466  {
467      b[0] = *ptr++;
468      b[1] = *ptr++;
469      b[2] = *ptr++;
470      b[3] = *ptr++;
471  }
472
473  strm->write((char*)b,4);
474  if (strm->fail()) {
475    return(LIB3DS_FALSE);
476  }
477  return(LIB3DS_TRUE);
478}
479
480
481/*!
482 * \ingroup readwrite
483 * \ingroup vector
484 *
485 * Writes a vector into a file stream in little endian format.   
486 *
487 * \param v  The vector to write to the file stream.
488 * \param f  Input file stream.
489 */
490Lib3dsBool
491lib3ds_vector_write(Lib3dsVector v, iostream *strm)
492{
493  if (!lib3ds_float_write(v[0], strm)) {
494    return(LIB3DS_FALSE);
495  }
496  if (!lib3ds_float_write(v[1], strm)) {
497    return(LIB3DS_FALSE);
498  }
499  if (!lib3ds_float_write(v[2], strm)) {
500    return(LIB3DS_FALSE);
501  }
502  return(LIB3DS_TRUE);
503}
504
505
506/*!
507 * \ingroup readwrite
508 */
509Lib3dsBool
510lib3ds_rgb_write(Lib3dsRgb rgb, iostream *strm)
511{
512  if (!lib3ds_float_write(rgb[0], strm)) {
513    return(LIB3DS_FALSE);
514  }
515  if (!lib3ds_float_write(rgb[1], strm)) {
516    return(LIB3DS_FALSE);
517  }
518  if (!lib3ds_float_write(rgb[2], strm)) {
519    return(LIB3DS_FALSE);
520  }
521  return(LIB3DS_TRUE);
522}
523
524
525/*!
526 * \ingroup readwrite
527 *
528 * Writes a zero-terminated string into a file stream.
529 *
530 * \param f  The float to write to the file stream.
531 * \param f  The input file stream.
532 *
533 * \return   True on success, False otherwise.
534 */
535Lib3dsBool
536lib3ds_string_write(const char *s, iostream *strm)
537{
538  ASSERT(s);
539  ASSERT(strm);
540  do strm->write(s,1); while (*s++);
541  if (strm->fail()) {
542    return(LIB3DS_FALSE);
543  }
544  return(LIB3DS_TRUE);
545}
Note: See TracBrowser for help on using the browser.