From Wang Rui, refactored the InputStream/OutputStream operations so that the binar/ascii foramts are implemented via subclasses.

This commit is contained in:
Robert Osfield
2010-01-25 11:03:21 +00:00
parent f8fc4f66a8
commit a520e8b6bd
12 changed files with 647 additions and 660 deletions

View File

@@ -20,6 +20,7 @@ SET(LIB_NAME osgDB)
SET(HEADER_PATH ${OpenSceneGraph_SOURCE_DIR}/include/${LIB_NAME})
SET(LIB_PUBLIC_HEADERS
${HEADER_PATH}/DataTypes
${HEADER_PATH}/StreamOperator
${HEADER_PATH}/Serializer
${HEADER_PATH}/ObjectWrapper
${HEADER_PATH}/InputStream

View File

@@ -23,12 +23,10 @@ using namespace osgDB;
static std::string s_lastSchema;
InputStream::InputStream( std::istream* istream, const osgDB::Options* options )
: _readMode(READ_BINARY), _byteSwap(0), _useFloatMatrix(false),
_in(istream)
InputStream::InputStream( const osgDB::Options* options )
: _byteSwap(0), _useFloatMatrix(false),
_in(0)
{
if ( !_in )
throw InputException(_currentField, "InputStream: Null stream specified.");
if ( !options ) return;
std::string schema;
@@ -38,7 +36,9 @@ InputStream::InputStream( std::istream* istream, const osgDB::Options* options )
{
const std::string& option = *itr;
if ( option=="Ascii" )
_readMode = READ_ASCII;
{
// Omit this
}
else
{
StringList keyAndValues;
@@ -157,68 +157,6 @@ InputStream& InputStream::operator>>( osg::Matrixd& mat )
return *this;
}
InputStream& InputStream::operator>>( ObjectGLenum& value )
{
GLenum e = 0;
if ( isBinary() )
{
_in->read( (char*)&e, GLENUM_SIZE ); checkStream();
if ( _byteSwap ) osg::swapBytes( (char*)&e, GLENUM_SIZE );
}
else
{
std::string enumString;
*this >> enumString;
e = osgDB::Registry::instance()->getObjectWrapperManager()->getValue("GL", enumString);
}
value.set( e );
return *this;
}
InputStream& InputStream::operator>>( ObjectProperty& prop )
{
int value = 0;
if ( isBinary() )
{
if ( prop._mapProperty )
{
_in->read( (char*)&value, INT_SIZE ); checkStream();
if ( _byteSwap ) osg::swapBytes( (char*)&value, INT_SIZE );
}
}
else
{
std::string enumString;
*this >> enumString;
if ( prop._mapProperty )
{
value = osgDB::Registry::instance()->getObjectWrapperManager()->getValue(prop._name, enumString);
}
else
{
if ( prop._name!=enumString )
{
osg::notify(osg::WARN) << "InputStream::operator>>(ObjectProperty&): Unmatched property "
<< enumString << ", expecting " << prop._name
<< ". At " << _currentField << std::endl;
}
prop._name = enumString;
}
}
prop.set( value );
return *this;
}
InputStream& InputStream::operator>>( ObjectMark& mark )
{
if ( !isBinary() )
{
std::string markString;
*this >> markString;
}
return *this;
}
osg::Array* InputStream::readArray()
{
osg::ref_ptr<osg::Array> array = NULL;
@@ -635,32 +573,25 @@ void InputStream::readSchema( std::istream& fin )
}
}
InputStream::ReadType InputStream::start()
InputStream::ReadType InputStream::start( InputIterator* inIterator )
{
ReadType type = READ_UNKNOWN;
_currentField = "Header";
_in = inIterator;
if ( !_in )
throw InputException(_currentField, "InputStream: Null stream specified.");
// Check OSG header information
unsigned int version = 0;
if ( isBinary() )
{
unsigned int headerLow = 0, headerHigh = 0;
*this >> headerLow >> headerHigh;
if ( headerLow!=OSG_HEADER_LOW || headerHigh!=OSG_HEADER_HIGH )
{
_in->seekg( 0, std::ios::beg );
_readMode = READ_ASCII;
}
else
{
unsigned int typeValue;
*this >> typeValue >> version;
type = static_cast<ReadType>(typeValue);
unsigned int matrixValueType; *this >> matrixValueType;
if ( matrixValueType==0 ) _useFloatMatrix = true;
else _useFloatMatrix = false;
}
unsigned int typeValue;
*this >> typeValue >> version;
type = static_cast<ReadType>(typeValue);
unsigned int matrixValueType; *this >> matrixValueType;
if ( matrixValueType==0 ) _useFloatMatrix = true;
else _useFloatMatrix = false;
}
if ( !isBinary() )
{
@@ -702,9 +633,9 @@ void InputStream::decompress()
}
std::string data;
if ( !compressor->decompress(*_in, data) )
if ( !compressor->decompress(*(_in->getStream()), data) )
throw InputException(_currentField, "InputStream: Failed to decompress stream.");
_in = new std::stringstream(data);
_in->setStream( new std::stringstream(data) );
}
// PROTECTED METHODS
@@ -745,7 +676,7 @@ void InputStream::readArrayImplementation( T* a, int readSize, bool useByteSwap
a->resize( size );
if ( isBinary() )
{
_in->read( (char*)&((*a)[0]), readSize*size ); checkStream();
_in->getStream()->read( (char*)&((*a)[0]), readSize*size ); checkStream();
if ( useByteSwap && _byteSwap )
{
for ( int i=0; i<size; ++i )

View File

@@ -21,13 +21,10 @@
using namespace osgDB;
OutputStream::OutputStream( std::ostream* ostream, const osgDB::Options* options )
: _writeMode(WRITE_BINARY), _writeImageHint(WRITE_USE_IMAGE_HINT),
_readyForEndBracket(false), _indent(0),
_out(ostream)
OutputStream::OutputStream( const osgDB::Options* options )
: _writeImageHint(WRITE_USE_IMAGE_HINT),
_out(0)
{
if ( !_out )
throw OutputException(_currentField, "OutputStream: Null stream specified.");
if ( !options ) return;
StringList optionList;
@@ -36,7 +33,9 @@ OutputStream::OutputStream( std::ostream* ostream, const osgDB::Options* options
{
const std::string& option = *itr;
if ( option=="Ascii" )
_writeMode = WRITE_ASCII;
{
// Omit this
}
else
{
StringList keyAndValues;
@@ -137,57 +136,6 @@ OutputStream& OutputStream::operator<<( const osg::Matrixd& mat )
return *this;
}
OutputStream& OutputStream::operator<<( const ObjectGLenum& value )
{
GLenum e = value.get();
if ( isBinary() )
{
_out->write((char*)&e, GLENUM_SIZE);
}
else
{
const std::string& enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString("GL", e);
*_out << enumString << ' ';
}
return *this;
}
OutputStream& OutputStream::operator<<( const ObjectProperty& prop )
{
if ( isBinary() )
{
if ( prop._mapProperty )
_out->write( (char*)&(prop._value), INT_SIZE );
}
else
{
std::string enumString = prop._name;
if ( prop._mapProperty )
{
enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString(prop._name, prop._value);
}
*_out << enumString << ' ';
}
return *this;
}
OutputStream& OutputStream::operator<<( const ObjectMark& mark )
{
if ( !isBinary() )
{
int delta = mark._indentDelta;
if ( delta<0 && _readyForEndBracket )
{
if ( _indent<-delta ) delta = -_indent;
_readyForEndBracket = false;
_out->seekp( delta, std::ios::cur );
}
_indent += delta;
*this << mark._name;
}
return *this;
}
void OutputStream::writeArray( const osg::Array* a )
{
if ( !a ) return;
@@ -485,9 +433,12 @@ void OutputStream::writeObject( const osg::Object* obj )
*this << END_BRACKET << std::endl;
}
void OutputStream::start( OutputStream::WriteType type )
void OutputStream::start( OutputIterator* outIterator, OutputStream::WriteType type )
{
_currentField = "Header";
_out = outIterator;
if ( !_out )
throw OutputException(_currentField, "OutputStream: Null stream specified.");
if ( isBinary() )
{
@@ -508,8 +459,8 @@ void OutputStream::start( OutputStream::WriteType type )
else
{
*this << _compressorName;
_out->flush();
_out = &_compressSource;
_out->getStream()->flush();
_out->setStream( &_compressSource );
return;
}
}

View File

@@ -0,0 +1,217 @@
#ifndef OSGDB_ASCIISTREAMOPERATOR
#define OSGDB_ASCIISTREAMOPERATOR
#include <osgDB/StreamOperator>
class AsciiOutputIterator : public osgDB::OutputIterator
{
public:
AsciiOutputIterator( std::ostream* ostream )
: _readyForEndBracket(false), _indent(0) { _out = ostream; }
virtual ~AsciiOutputIterator() {}
virtual bool isBinary() const { return false; }
virtual void writeBool( bool b )
{
if ( b ) *_out << "TRUE ";
else *_out << "FALSE ";
}
virtual void writeChar( char c )
{ *_out << (short)c << ' '; }
virtual void writeUChar( unsigned char c )
{ *_out << (unsigned short)c << ' '; }
virtual void writeShort( short s )
{ *_out << s << ' '; }
virtual void writeUShort( unsigned short s )
{ *_out << s << ' '; }
virtual void writeInt( int i )
{ *_out << i << ' '; }
virtual void writeUInt( unsigned int i )
{ *_out << i << ' '; }
virtual void writeLong( long l )
{ *_out << l << ' '; }
virtual void writeULong( unsigned long l )
{ *_out << l << ' '; }
virtual void writeFloat( float f )
{ *_out << f << ' '; }
virtual void writeDouble( double d )
{ *_out << d << ' '; }
virtual void writeString( const std::string& s )
{ *_out << s << ' '; }
virtual void writeStream( std::ostream& (*fn)(std::ostream&) )
{
*_out << fn;
if ( fn==static_cast<std::ostream& (*)(std::ostream&)>(std::endl) )
{
_readyForEndBracket = true;
for (int i=0; i<_indent; ++i)
*_out << ' ';
}
}
virtual void writeBase( std::ios_base& (*fn)(std::ios_base&) )
{
*_out << fn;
}
virtual void writeGLenum( const osgDB::ObjectGLenum& value )
{
GLenum e = value.get();
const std::string& enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString("GL", e);
*_out << enumString << ' ';
}
virtual void writeProperty( const osgDB::ObjectProperty& prop )
{
std::string enumString = prop._name;
if ( prop._mapProperty )
{
enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString(prop._name, prop._value);
}
*_out << enumString << ' ';
}
virtual void writeMark( const osgDB::ObjectMark& mark )
{
int delta = mark._indentDelta;
if ( delta<0 && _readyForEndBracket )
{
if ( _indent<-delta ) delta = -_indent;
_readyForEndBracket = false;
_out->seekp( delta, std::ios::cur );
}
_indent += delta;
*_out << mark._name << ' ';
}
virtual void writeCharArray( const char* s, unsigned int size ) {}
protected:
bool _readyForEndBracket;
int _indent;
};
class AsciiInputIterator : public osgDB::InputIterator
{
public:
AsciiInputIterator( std::istream* istream ) { _in = istream; }
virtual ~AsciiInputIterator() {}
virtual bool isBinary() const { return false; }
virtual void readBool( bool& b )
{
std::string boolString;
*_in >> boolString; checkStream();
if ( boolString=="TRUE" ) b = true;
else b = false;
}
virtual void readChar( char& c )
{
short s = 0;
*_in >> s; checkStream();
c = (char)s;
}
virtual void readSChar( signed char& c )
{
short s = 0;
*_in >> s; checkStream();
c = (signed char)s;
}
virtual void readUChar( unsigned char& c )
{
short s = 0;
*_in >> s; checkStream();
c = (unsigned char)s;
}
virtual void readShort( short& s )
{ *_in >> s; checkStream(); }
virtual void readUShort( unsigned short& s )
{ *_in >> s; checkStream(); }
virtual void readInt( int& i )
{ *_in >> i; checkStream(); }
virtual void readUInt( unsigned int& i )
{ *_in >> i; checkStream(); }
virtual void readLong( long& l )
{ *_in >> l; checkStream(); }
virtual void readULong( unsigned long& l )
{ *_in >> l; checkStream(); }
virtual void readFloat( float& f )
{ *_in >> f; checkStream(); }
virtual void readDouble( double& d )
{ *_in >> d; checkStream(); }
virtual void readString( std::string& s )
{ *_in >> s; checkStream(); }
virtual void readStream( std::istream& (*fn)(std::istream&) )
{ *_in >> fn; }
virtual void readBase( std::ios_base& (*fn)(std::ios_base&) )
{ *_in >> fn; }
virtual void readGLenum( osgDB::ObjectGLenum& value )
{
GLenum e = 0;
std::string enumString;
*_in >> enumString; checkStream();
e = osgDB::Registry::instance()->getObjectWrapperManager()->getValue("GL", enumString);
value.set( e );
}
virtual void readProperty( osgDB::ObjectProperty& prop )
{
int value = 0;
std::string enumString;
*_in >> enumString; checkStream();
if ( prop._mapProperty )
{
value = osgDB::Registry::instance()->getObjectWrapperManager()->getValue(prop._name, enumString);
}
else
{
if ( prop._name!=enumString )
{
osg::notify(osg::WARN) << "AsciiInputIterator::readProperty(): Unmatched property "
<< enumString << ", expecting " << prop._name << std::endl;
}
prop._name = enumString;
}
prop.set( value );
}
virtual void readMark( osgDB::ObjectMark& mark )
{
std::string markString;
*_in >> markString; checkStream();
}
virtual void readCharArray( char* s, unsigned int size ) {}
};
#endif

View File

@@ -0,0 +1,184 @@
#ifndef OSG2_BINARYSTREAMOPERATOR
#define OSG2_BINARYSTREAMOPERATOR
#include <osgDB/StreamOperator>
class BinaryOutputIterator : public osgDB::OutputIterator
{
public:
BinaryOutputIterator( std::ostream* ostream ) { _out = ostream; }
virtual ~BinaryOutputIterator() {}
virtual bool isBinary() const { return true; }
virtual void writeBool( bool b )
{ char c = b?1:0; _out->write( &c, CHAR_SIZE ); }
virtual void writeChar( char c )
{ _out->write( &c, CHAR_SIZE ); }
virtual void writeUChar( unsigned char c )
{ _out->write( (char*)&c, CHAR_SIZE ); }
virtual void writeShort( short s )
{ _out->write( (char*)&s, SHORT_SIZE ); }
virtual void writeUShort( unsigned short s )
{ _out->write( (char*)&s, SHORT_SIZE ); }
virtual void writeInt( int i )
{ _out->write( (char*)&i, INT_SIZE ); }
virtual void writeUInt( unsigned int i )
{ _out->write( (char*)&i, INT_SIZE ); }
virtual void writeLong( long l )
{ _out->write( (char*)&l, LONG_SIZE ); }
virtual void writeULong( unsigned long l )
{ _out->write( (char*)&l, LONG_SIZE ); }
virtual void writeFloat( float f )
{ _out->write( (char*)&f, FLOAT_SIZE ); }
virtual void writeDouble( double d )
{ _out->write((char*)&d, DOUBLE_SIZE); }
virtual void writeString( const std::string& s )
{
int size = s.size();
_out->write( (char*)&size, INT_SIZE );
_out->write( s.c_str(), s.size() );
}
virtual void writeStream( std::ostream& (*fn)(std::ostream&) ) {}
virtual void writeBase( std::ios_base& (*fn)(std::ios_base&) ) {}
virtual void writeGLenum( const osgDB::ObjectGLenum& value )
{ GLenum e = value.get(); _out->write((char*)&e, GLENUM_SIZE); }
virtual void writeProperty( const osgDB::ObjectProperty& prop )
{ if (prop._mapProperty) _out->write((char*)&(prop._value), INT_SIZE); }
virtual void writeMark( const osgDB::ObjectMark& mark ) {}
virtual void writeCharArray( const char* s, unsigned int size )
{ if ( size>0 ) _out->write( s, size ); }
};
class BinaryInputIterator : public osgDB::InputIterator
{
public:
BinaryInputIterator( std::istream* istream ) : _byteSwap(0) { _in = istream; }
virtual ~BinaryInputIterator() {}
virtual bool isBinary() const { return true; }
virtual void readBool( bool& b )
{
char c = 0;
_in->read( &c, CHAR_SIZE ); checkStream();
b = (c!=0);
}
virtual void readChar( char& c )
{ _in->read( &c, CHAR_SIZE ); checkStream(); }
virtual void readSChar( signed char& c )
{ _in->read( (char*)&c, CHAR_SIZE ); checkStream(); }
virtual void readUChar( unsigned char& c )
{ _in->read( (char*)&c, CHAR_SIZE ); checkStream(); }
virtual void readShort( short& s )
{
_in->read( (char*)&s, SHORT_SIZE ); checkStream();
if ( _byteSwap ) osg::swapBytes( (char*)&s, SHORT_SIZE );
}
virtual void readUShort( unsigned short& s )
{
_in->read( (char*)&s, SHORT_SIZE ); checkStream();
if ( _byteSwap ) osg::swapBytes( (char*)&s, SHORT_SIZE );
}
virtual void readInt( int& i )
{
_in->read( (char*)&i, INT_SIZE ); checkStream();
if ( _byteSwap ) osg::swapBytes( (char*)&i, INT_SIZE );
}
virtual void readUInt( unsigned int& i )
{
_in->read( (char*)&i, INT_SIZE ); checkStream();
if ( _byteSwap ) osg::swapBytes( (char*)&i, INT_SIZE );
}
virtual void readLong( long& l )
{
_in->read( (char*)&l, LONG_SIZE ); checkStream();
if ( _byteSwap ) osg::swapBytes( (char*)&l, LONG_SIZE );
}
virtual void readULong( unsigned long& l )
{
_in->read( (char*)&l, LONG_SIZE ); checkStream();
if ( _byteSwap ) osg::swapBytes( (char*)&l, LONG_SIZE );
}
virtual void readFloat( float& f )
{
_in->read( (char*)&f, FLOAT_SIZE ); checkStream();
if ( _byteSwap ) osg::swapBytes( (char*)&f, FLOAT_SIZE );
}
virtual void readDouble( double& d )
{
_in->read( (char*)&d, DOUBLE_SIZE ); checkStream();
if ( _byteSwap ) osg::swapBytes( (char*)&d, DOUBLE_SIZE );
}
virtual void readString( std::string& s )
{
int size = 0; readInt( size );
if ( size )
{
s.resize( size );
_in->read( (char*)s.c_str(), size ); checkStream();
}
}
virtual void readStream( std::istream& (*fn)(std::istream&) ) {}
virtual void readBase( std::ios_base& (*fn)(std::ios_base&) ) {}
virtual void readGLenum( osgDB::ObjectGLenum& value )
{
GLenum e = 0;
_in->read( (char*)&e, GLENUM_SIZE ); checkStream();
if ( _byteSwap ) osg::swapBytes( (char*)&e, GLENUM_SIZE );
value.set( e );
}
virtual void readProperty( osgDB::ObjectProperty& prop )
{
int value = 0;
if ( prop._mapProperty )
{
_in->read( (char*)&value, INT_SIZE ); checkStream();
if ( _byteSwap ) osg::swapBytes( (char*)&value, INT_SIZE );
}
prop.set( value );
}
virtual void readMark( osgDB::ObjectMark& mark ) {}
virtual void readCharArray( char* s, unsigned int size )
{ if ( size>0 ) _in->read( s, size ); checkStream(); }
protected:
int _byteSwap;
};
#endif

View File

@@ -16,9 +16,24 @@
#include <osgDB/FileUtils>
#include <osgDB/Registry>
#include <osgDB/ObjectWrapper>
#include "AsciiStreamOperator.h"
#include "BinaryStreamOperator.h"
using namespace osgDB;
bool checkBinary( std::istream* fin )
{
unsigned int headerLow = 0, headerHigh = 0;
fin->read( (char*)&headerLow, INT_SIZE );
fin->read( (char*)&headerHigh, INT_SIZE );
if ( headerLow!=OSG_HEADER_LOW || headerHigh!=OSG_HEADER_HIGH )
{
fin->seekg( 0, std::ios::beg );
return false;
}
return true;
}
class ReaderWriterOSG2 : public osgDB::ReaderWriter
{
public:
@@ -76,8 +91,15 @@ public:
{
try
{
InputStream is( &fin, options );
if ( is.start()!=InputStream::READ_IMAGE )
InputStream is( options );
InputIterator* ii = NULL;
if ( !checkBinary(&fin) )
ii = new AsciiInputIterator(&fin);
else
ii = new BinaryInputIterator(&fin);
if ( is.start(ii)!=InputStream::READ_IMAGE )
return ReadResult::FILE_NOT_HANDLED;
is.decompress();
return is.readImage();
@@ -107,8 +129,15 @@ public:
{
try
{
InputStream is( &fin, options );
if ( is.start()!=InputStream::READ_SCENE )
InputStream is( options );
InputIterator* ii = NULL;
if ( !checkBinary(&fin) )
ii = new AsciiInputIterator(&fin);
else
ii = new BinaryInputIterator(&fin);
if ( is.start(ii)!=InputStream::READ_SCENE )
return ReadResult::FILE_NOT_HANDLED;
is.decompress();
return dynamic_cast<osg::Node*>( is.readObject() );
@@ -162,8 +191,15 @@ public:
{
try
{
OutputStream os( &fout, options );
os.start( OutputStream::WRITE_IMAGE );
OutputStream os( options );
osgDB::OutputIterator* oi = NULL;
if ( options && options->getOptionString().find("Ascii")!=std::string::npos )
oi = new AsciiOutputIterator(&fout);
else
oi = new BinaryOutputIterator(&fout);
os.start( oi, OutputStream::WRITE_IMAGE );
os.writeImage( &image );
os.compress( &fout );
@@ -201,8 +237,15 @@ public:
{
try
{
OutputStream os( &fout, options );
os.start( OutputStream::WRITE_SCENE );
OutputStream os( options );
osgDB::OutputIterator* oi = NULL;
if ( options && options->getOptionString().find("Ascii")!=std::string::npos )
oi = new AsciiOutputIterator(&fout);
else
oi = new BinaryOutputIterator(&fout);
os.start( oi, OutputStream::WRITE_SCENE );
os.writeObject( &node );
os.compress( &fout );