Added s/getByteSwap to teh InputStreamOperator base class and use of this method in the InputStream::start(InputStreamOperator*) method to ensure the bytes are swapped consistently.

This commit is contained in:
Robert Osfield
2012-02-24 21:07:02 +00:00
parent c3fb8dc714
commit e8ac276451
5 changed files with 52 additions and 16 deletions

View File

@@ -140,6 +140,9 @@ public:
osg::Object* readObject( osg::Object* existingObj=0 );
osg::Object* readObjectFields( const std::string& className, unsigned int id, osg::Object* existingObj=0);
void setByteSwap(int byteSwap) { _byteSwap = byteSwap; }
int getByteSwap() const { return _byteSwap; }
/// set an input iterator, used directly when not using InputStream with a traditional file releated stream.
void setInputIterator( InputIterator* ii ) { _in = ii; }

View File

@@ -69,7 +69,7 @@ protected:
class OSGDB_EXPORT InputIterator : public osg::Referenced
{
public:
InputIterator() : _in(0), _inputStream(0), _failed(false) {}
InputIterator() : _in(0), _inputStream(0), _byteSwap(0), _failed(false) {}
virtual ~InputIterator() {}
void setStream( std::istream* istream ) { _in = istream; }
@@ -79,7 +79,10 @@ public:
void setInputStream( InputStream* inputStream) { _inputStream = inputStream; }
InputStream* getInputStream() { return _inputStream; }
const InputStream* getInputStream() const { return _inputStream; }
void setByteSwap(int byteSwap) { _byteSwap = byteSwap; }
int getByteSwap() const { return _byteSwap; }
void checkStream() const { if (_in->rdstate()&_in->failbit) _failed = true; }
bool isFailed() const { return _failed; }
@@ -115,7 +118,8 @@ public:
protected:
std::istream* _in;
osgDB::InputStream* _inputStream;
mutable bool _failed;
int _byteSwap;
mutable bool _failed;
};
}

View File

@@ -683,6 +683,9 @@ InputStream::ReadType InputStream::start( InputIterator* inIterator )
{
_fields.clear();
_fields.push_back( "Start" );
OSG_INFO<<"InputStream::start( InputIterator* inIterator ) calling setByteSwap("<<inIterator->getByteSwap()<<")"<<std::endl;
setByteSwap(inIterator->getByteSwap());
ReadType type = READ_UNKNOWN;
_in = inIterator;

View File

@@ -3,6 +3,14 @@
#include <osgDB/StreamOperator>
#if defined(_MSC_VER)
typedef unsigned __int32 uint32_t;
typedef __int32 int32_t;
#else
#include <stdint.h>
#endif
class BinaryOutputIterator : public osgDB::OutputIterator
{
public:
@@ -33,10 +41,18 @@ public:
{ _out->write( (char*)&i, osgDB::INT_SIZE ); }
virtual void writeLong( long l )
{ _out->write( (char*)&l, osgDB::LONG_SIZE ); }
{
// On 64-bit systems a long may not be the same size as the file value
int32_t value=(int32_t)l;
_out->write( (char*)&value, osgDB::LONG_SIZE );
}
virtual void writeULong( unsigned long l )
{ _out->write( (char*)&l, osgDB::LONG_SIZE ); }
{
// On 64-bit systems a long may not be the same size as the file value
uint32_t value=(int32_t)l;
_out->write( (char*)&value, osgDB::LONG_SIZE );
}
virtual void writeFloat( float f )
{ _out->write( (char*)&f, osgDB::FLOAT_SIZE ); }
@@ -73,9 +89,14 @@ public:
class BinaryInputIterator : public osgDB::InputIterator
{
public:
BinaryInputIterator( std::istream* istream, bool byteSwap ) : _byteSwap(byteSwap) { _in = istream; }
virtual ~BinaryInputIterator() {}
BinaryInputIterator( std::istream* istream, int byteSwap )
{
_in = istream;
setByteSwap(byteSwap);
}
virtual ~BinaryInputIterator() {}
virtual bool isBinary() const { return true; }
virtual void readBool( bool& b )
@@ -120,14 +141,19 @@ public:
virtual void readLong( long& l )
{
_in->read( (char*)&l, osgDB::LONG_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&l, osgDB::LONG_SIZE );
// On 64-bit systems a long may not be the same size as the file value
int32_t value;
_in->read( (char*)&value, osgDB::LONG_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&value, osgDB::LONG_SIZE );
l = (long)value;
}
virtual void readULong( unsigned long& l )
{
_in->read( (char*)&l, osgDB::LONG_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&l, osgDB::LONG_SIZE );
uint32_t value;
_in->read( (char*)&value, osgDB::LONG_SIZE );
if ( _byteSwap ) osg::swapBytes( (char*)&value, osgDB::LONG_SIZE );
l = (unsigned long)value;
}
virtual void readFloat( float& f )
@@ -144,7 +170,8 @@ public:
virtual void readString( std::string& s )
{
int size = 0; readInt( size );
int size = 0;
readInt( size );
if ( size>0 )
{
s.resize( size );
@@ -188,7 +215,6 @@ public:
{ readString( str ); }
protected:
bool _byteSwap;
};
#endif

View File

@@ -46,12 +46,12 @@ InputIterator* readInputIterator( std::istream& fin, const Options* options )
if ( headerLow==OSG_HEADER_LOW && headerHigh==OSG_HEADER_HIGH )
{
OSG_INFO<<"Reading OpenSceneGraph binary file with the same endian as this computer."<<std::endl;
return new BinaryInputIterator(&fin, false); // endian the same so no byte swap required
return new BinaryInputIterator(&fin, 0); // endian the same so no byte swap required
}
else if ( headerLow==OSG_REVERSE(OSG_HEADER_LOW) && headerHigh==OSG_REVERSE(OSG_HEADER_HIGH) )
{
OSG_INFO<<"Reading OpenSceneGraph binary file with the different endian to this computer, doing byte swap."<<std::endl;
return new BinaryInputIterator(&fin, true); // endian different so byte swap required
return new BinaryInputIterator(&fin, 1); // endian different so byte swap required
}
fin.seekg( 0, std::ios::beg );