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

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

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/*
2 * This library is open source and may be redistributed and/or modified under
3 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or (at
4 * your option) any later version. The full license is in the LICENSE file
5 * included with this distribution, and on the openscenegraph.org website.
6 *
7 * This library is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * OpenSceneGraph Public License for more details.
11*/
12
13//
14// Copyright(c) 2008 Skew Matrix Software LLC.
15//
16
17#include "DataOutputStream.h"
18
19#include <osg/Endian>
20#include <osg/Notify>
21
22
23using namespace flt;
24
25
26char DataOutputStream::_null( 0 );
27
28
29DataOutputStream::DataOutputStream( std::streambuf* sb, bool validate )
30  : std::ostream( sb ),
31    _validate( validate )
32{
33    _byteswap = osg::getCpuByteOrder() == osg::LittleEndian;
34}
35
36
37void
38DataOutputStream::writeInt8( const int8 val )
39{
40    vwrite( (char*)&val, sizeof( int8 ) );
41}
42
43void
44DataOutputStream::writeUInt8( const uint8 val )
45{
46    vwrite( (char*)&val, sizeof( uint8 ) );
47}
48
49
50void
51DataOutputStream::writeInt16( const int16 val )
52{
53    int16 data=val;
54    if (_byteswap && good())
55        osg::swapBytes2( (char*)&data );
56    vwrite( (char*)&data, sizeof( int16 ) );
57}
58
59void
60DataOutputStream::writeUInt16( const uint16 val )
61{
62    uint16 data=val;
63    if (_byteswap && good())
64        osg::swapBytes2( (char*)&data );
65    vwrite( (char*)&data, sizeof( uint16 ) );
66}
67
68
69void
70DataOutputStream::writeInt32( const int32 val )
71{
72    int32 data=val;
73    if (_byteswap && good())
74        osg::swapBytes4( (char*)&data );
75    vwrite( (char*)&data, sizeof( int32 ) );
76}
77
78void
79DataOutputStream::writeUInt32( const uint32 val )
80{
81    uint32 data=val;
82    if (_byteswap && good())
83        osg::swapBytes4( (char*)&data );
84    vwrite( (char*)&data, sizeof( uint32 ) );
85}
86
87
88void
89DataOutputStream::writeFloat32( const float32 val )
90{
91    float32 data=val;
92    if (_byteswap && good())
93        osg::swapBytes4( (char*)&data );
94    vwrite( (char*)&data, sizeof( float32 ) );
95}
96
97void
98DataOutputStream::writeFloat64( const float64 val )
99{
100    float64 data=val;
101    if (_byteswap && good())
102        osg::swapBytes8( (char*)&data );
103    vwrite( (char*)&data, sizeof( float64 ) );
104}
105
106
107void
108DataOutputStream::writeString( const std::string& val, bool nullTerminate )
109{
110    vwrite( const_cast<char*>( val.c_str() ), val.size() );
111    if (nullTerminate)
112        vwrite( &_null, 1 );
113}
114
115void
116DataOutputStream::writeString( const std::string& val, int size, char fill )
117{
118    if (val.size() > ((unsigned int)size)-1)
119    {
120        vwrite( const_cast<char*>( val.c_str() ), size-1 );
121        vwrite( &fill, 1 );
122    }
123    else
124    {
125        vwrite( const_cast<char*>( val.c_str() ), val.size() );
126        writeFill( size - val.size(), fill );
127    }
128}
129
130void
131DataOutputStream::writeID( const std::string& val )
132{
133    unsigned int len = val.size();
134
135    vwrite( const_cast<char*>( val.c_str() ), len );
136
137    while (len++ < 8)
138        vwrite( &_null, 1 );
139}
140
141
142void
143DataOutputStream::writeVec2f( const osg::Vec2f& val )
144{
145    writeFloat32( val.x() );
146    writeFloat32( val.y() );
147}
148
149void
150DataOutputStream::writeVec3f( const osg::Vec3f& val )
151{
152    writeFloat32( val.x() );
153    writeFloat32( val.y() );
154    writeFloat32( val.z() );
155}
156
157void
158DataOutputStream::writeVec4f( const osg::Vec4f& val )
159{
160    writeFloat32( val.x() );
161    writeFloat32( val.y() );
162    writeFloat32( val.z() );
163    writeFloat32( val.w() );
164}
165
166void
167DataOutputStream::writeVec3d( const osg::Vec3d& val )
168{
169    writeFloat64( val.x() );
170    writeFloat64( val.y() );
171    writeFloat64( val.z() );
172}
173
174
175void
176DataOutputStream::writeFill( int sizeBytes, const char val )
177{
178    for (int i = 0; i < sizeBytes; ++i)
179    {
180        put(val);
181    }
182}
183
184
185std::ostream&
186DataOutputStream::vwrite( char_type* str, std::streamsize count )
187{
188    if (_validate)
189        return *this;
190    else
191        return write( str, count );
192}
Note: See TracBrowser for help on using the browser.