root/OpenSceneGraph/trunk/src/osgPlugins/OpenFlight/DataInputStream.cpp @ 13041

Revision 13041, 4.4 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14//
15// OpenFlight® loader for OpenSceneGraph
16//
17//  Copyright (C) 2005-2007  Brede Johansen
18//
19
20#include "DataInputStream.h"
21
22#include <osg/Endian>
23#include <osg/Notify>
24
25using namespace flt;
26
27
28DataInputStream::DataInputStream(std::streambuf* sb):
29    std::istream(sb)
30{
31    _byteswap = osg::getCpuByteOrder() == osg::LittleEndian;
32}
33
34
35int8 DataInputStream::readInt8(int8 def)
36{
37    int8 d;
38    read((char*)&d, sizeof(int8));
39
40    if (!good())
41        return def;
42
43    return d;
44}
45
46
47uint8 DataInputStream::readUInt8(uint8 def)
48{
49    uint8 d;
50    read((char*)&d, sizeof(uint8));
51
52    if (!good())
53        return def;
54
55    return d;
56}
57
58
59int16 DataInputStream::readInt16(int16 def)
60{
61    int16 d;
62    read((char*)&d, sizeof(int16));
63
64    if (!good())
65        return def;
66
67    if (_byteswap)
68        osg::swapBytes2((char *)&d);
69
70    return d;
71}
72
73
74uint16 DataInputStream::readUInt16(uint16 def)
75{
76    uint16 d;
77    read((char*)&d, sizeof(uint16));
78
79    if (!good())
80        return def;
81
82    if (_byteswap)
83        osg::swapBytes2((char *)&d);
84
85    return d;
86}
87
88
89int32 DataInputStream::readInt32(int32 def)
90{
91    int32 d;
92    read((char*)&d, sizeof(int32));
93
94    if (!good())
95        return def;
96
97    if (_byteswap)
98        osg::swapBytes4((char *)&d);
99
100    return d;
101}
102
103
104uint32 DataInputStream::readUInt32(uint32 def)
105{
106    uint32 d;
107    read((char*)&d, sizeof(uint32));
108
109    if (!good())
110        return def;
111
112    if (_byteswap)
113        osg::swapBytes4((char *)&d);
114
115    return d;
116}
117
118
119float32 DataInputStream::readFloat32(float32 def)
120{
121    float32 d;
122    read((char*)&d, sizeof(float32));
123
124    if (!good())
125        return def;
126
127    if (_byteswap)
128        osg::swapBytes4((char*)&d);
129
130    return d;
131}
132
133
134float64 DataInputStream::readFloat64(float64 def)
135{
136    float64 d;
137    read((char*)&d, sizeof(float64));
138
139    if (!good())
140        return def;
141
142    if (_byteswap)
143        osg::swapBytes8((char*)&d);
144
145    return d;
146}
147
148
149void DataInputStream::readCharArray(char* data, int size)
150{
151    read(data,size);
152}
153
154
155std::string DataInputStream::readString(int size)
156{
157    char* buf = new char[size+1];
158    read(buf,size);
159    buf[size] = '\0';
160    std::string str = buf;
161    delete [] buf;
162    return str;
163}
164
165
166osg::Vec4f DataInputStream::readColor32()
167{
168    uint8 alpha = readUInt8();
169    uint8 blue  = readUInt8();
170    uint8 green = readUInt8();
171    uint8 red   = readUInt8();
172
173    osg::Vec4f color((float)red/255,(float)green/255,(float)blue/255,(float)alpha/255);
174
175    return color;
176}
177
178
179osg::Vec2f DataInputStream::readVec2f()
180{
181    float32 x = readFloat32();
182    float32 y = readFloat32();
183
184    osg::Vec2f vec(x,y);
185
186    return vec;
187}
188
189
190osg::Vec3f DataInputStream::readVec3f()
191{
192    float32 x = readFloat32();
193    float32 y = readFloat32();
194    float32 z = readFloat32();
195
196    osg::Vec3f vec(x,y,z);
197
198    return vec;
199}
200
201
202osg::Vec4f DataInputStream::readVec4f()
203{
204    float32 x = readFloat32();
205    float32 y = readFloat32();
206    float32 z = readFloat32();
207    float32 w = readFloat32();
208
209    osg::Vec4f vec(x,y,z,w);
210
211    return vec;
212}
213
214
215osg::Vec3d DataInputStream::readVec3d()
216{
217    float64 x = readFloat64();
218    float64 y = readFloat64();
219    float64 z = readFloat64();
220
221    osg::Vec3d vec(x,y,z);
222
223    return vec;
224}
225
226
227int16 DataInputStream::peekInt16()
228{
229    // Get current read position in stream.
230    std::istream::pos_type pos = tellg();
231
232    int16 value = readInt16();
233
234    // Restore position
235    seekg(pos, std::ios_base::beg);
236
237    return value;
238}
239
240
241std::istream& DataInputStream::forward(std::istream::off_type off)
242{
243    //return vforward(off);
244    return seekg(off, std::ios_base::cur);
245}
246
247#if 0
248std::istream& DataInputStream::vread(char_type *str, std::streamsize count)
249{
250    return read(str,count);
251}
252
253
254std::istream& DataInputStream::vforward(std::istream::off_type off)
255{
256    return seekg(off, std::ios_base::cur);
257}
258#endif
Note: See TracBrowser for help on using the browser.