216 lines
7.3 KiB
C++
216 lines
7.3 KiB
C++
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
|
*
|
|
* This library is open source and may be redistributed and/or modified under
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* OpenSceneGraph Public License for more details.
|
|
*/
|
|
// Written by Wang Rui, (C) 2010
|
|
|
|
#ifndef OSGDB_INPUTSTREAM
|
|
#define OSGDB_INPUTSTREAM
|
|
|
|
#include <osg/Endian>
|
|
#include <osg/Vec2>
|
|
#include <osg/Vec3>
|
|
#include <osg/Vec4>
|
|
#include <osg/Quat>
|
|
#include <osg/Matrix>
|
|
#include <osg/Array>
|
|
#include <osg/PrimitiveSet>
|
|
#include <osgDB/ReaderWriter>
|
|
#include <osgDB/StreamOperator>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
|
|
namespace osgDB
|
|
{
|
|
|
|
class InputException
|
|
{
|
|
public:
|
|
InputException( const std::string& field, const std::string& err )
|
|
: _field(field), _error(err) {}
|
|
|
|
const std::string& getField() const { return _field; }
|
|
const std::string& getError() const { return _error; }
|
|
|
|
protected:
|
|
std::string _field;
|
|
std::string _error;
|
|
};
|
|
|
|
class OSGDB_EXPORT InputStream
|
|
{
|
|
public:
|
|
typedef std::map< unsigned int, osg::ref_ptr<osg::Array> > ArrayMap;
|
|
typedef std::map< unsigned int, osg::ref_ptr<osg::Object> > IdentifierMap;
|
|
|
|
enum ReadType
|
|
{
|
|
READ_UNKNOWN = 0,
|
|
READ_SCENE,
|
|
READ_IMAGE
|
|
};
|
|
|
|
InputStream( const osgDB::Options* options );
|
|
virtual ~InputStream();
|
|
|
|
bool isBinary() const { return _in->isBinary(); }
|
|
bool getUseFloatMatrix() const { return _useFloatMatrix; }
|
|
|
|
// Serialization related functions
|
|
InputStream& operator>>( bool& b ) { _in->readBool(b); return *this; }
|
|
InputStream& operator>>( char& c ) { _in->readChar(c); return *this; }
|
|
InputStream& operator>>( signed char& c ) { _in->readSChar(c); return *this; }
|
|
InputStream& operator>>( unsigned char& c ) { _in->readUChar(c); return *this; }
|
|
InputStream& operator>>( short& s ) { _in->readShort(s); return *this; }
|
|
InputStream& operator>>( unsigned short& s ) { _in->readUShort(s); return *this; }
|
|
InputStream& operator>>( int& i ) { _in->readInt(i); return *this; }
|
|
InputStream& operator>>( unsigned int& i ) { _in->readUInt(i); return *this; }
|
|
InputStream& operator>>( long& l ) { _in->readLong(l); return *this; }
|
|
InputStream& operator>>( unsigned long& l ) { _in->readULong(l); return *this; }
|
|
InputStream& operator>>( float& f ) { _in->readFloat(f); return *this; }
|
|
InputStream& operator>>( double& d ) { _in->readDouble(d); return *this; }
|
|
InputStream& operator>>( std::string& s ) { _in->readString(s); return *this; }
|
|
InputStream& operator>>( std::istream& (*fn)(std::istream&) ) { _in->readStream(fn); return *this; }
|
|
InputStream& operator>>( std::ios_base& (*fn)(std::ios_base&) ) { _in->readBase(fn); return *this; }
|
|
|
|
InputStream& operator>>( ObjectGLenum& value ) { _in->readGLenum(value); return *this; }
|
|
InputStream& operator>>( ObjectProperty& prop ) { _in->readProperty(prop); return *this; }
|
|
InputStream& operator>>( ObjectMark& mark ) { _in->readMark(mark); return *this; }
|
|
|
|
InputStream& operator>>( osg::Vec2b& v );
|
|
InputStream& operator>>( osg::Vec3b& v );
|
|
InputStream& operator>>( osg::Vec4b& v );
|
|
InputStream& operator>>( osg::Vec4ub& v );
|
|
InputStream& operator>>( osg::Vec2s& v );
|
|
InputStream& operator>>( osg::Vec3s& v );
|
|
InputStream& operator>>( osg::Vec4s& v );
|
|
InputStream& operator>>( osg::Vec2f& v );
|
|
InputStream& operator>>( osg::Vec3f& v );
|
|
InputStream& operator>>( osg::Vec4f& v );
|
|
InputStream& operator>>( osg::Vec2d& v );
|
|
InputStream& operator>>( osg::Vec3d& v );
|
|
InputStream& operator>>( osg::Vec4d& v );
|
|
InputStream& operator>>( osg::Quat& q );
|
|
InputStream& operator>>( osg::Plane& p );
|
|
InputStream& operator>>( osg::Matrixf& mat );
|
|
InputStream& operator>>( osg::Matrixd& mat );
|
|
|
|
InputStream& operator>>( osg::Array*& a ) { a = readArray(); return *this; }
|
|
InputStream& operator>>( osg::Image*& img ) { img = readImage(); return *this; }
|
|
InputStream& operator>>( osg::PrimitiveSet*& p ) { p = readPrimitiveSet(); return *this; }
|
|
InputStream& operator>>( osg::Object*& obj ) { obj = readObject(); return *this; }
|
|
|
|
InputStream& operator>>( osg::ref_ptr<osg::Array>& ptr ) { ptr = readArray(); return *this; }
|
|
InputStream& operator>>( osg::ref_ptr<osg::Image>& ptr ) { ptr = readImage(); return *this; }
|
|
InputStream& operator>>( osg::ref_ptr<osg::PrimitiveSet>& ptr ) { ptr = readPrimitiveSet(); return *this; }
|
|
|
|
template<typename T> InputStream& operator>>( osg::ref_ptr<T>& ptr )
|
|
{ ptr = static_cast<T*>(readObject()); return *this; }
|
|
|
|
// Convenient methods for reading
|
|
inline bool matchString( const std::string& str );
|
|
inline void advanceToCurrentEndBracket();
|
|
inline void readWrappedString( std::string& str );
|
|
void readCharArray( char* s, unsigned int size ) { _in->readCharArray(s, size); }
|
|
|
|
// Global reading functions
|
|
osg::Array* readArray();
|
|
osg::PrimitiveSet* readPrimitiveSet();
|
|
osg::Image* readImage();
|
|
osg::Object* readObject( osg::Object* existingObj=0 );
|
|
|
|
ReadType start( InputIterator* );
|
|
void decompress();
|
|
|
|
// Schema handlers
|
|
void readSchema( std::istream& fin );
|
|
void resetSchema();
|
|
|
|
protected:
|
|
inline void checkStream() const;
|
|
void setWrapperSchema( const std::string& name, const std::string& properties );
|
|
|
|
template<typename T>
|
|
void readArrayImplementation( T* a, int readSize, bool useByteSwap=false );
|
|
|
|
ArrayMap _arrayMap;
|
|
IdentifierMap _identifierMap;
|
|
|
|
int _byteSwap;
|
|
bool _useFloatMatrix;
|
|
std::string _currentField;
|
|
InputIterator* _in;
|
|
};
|
|
|
|
bool InputStream::matchString( const std::string& str )
|
|
{
|
|
if ( !isBinary() )
|
|
{
|
|
std::string s; *this >> s;
|
|
if ( s==str ) return true;
|
|
else _in->getStream()->seekg( -(int)(s.length()), std::ios::cur );
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void InputStream::advanceToCurrentEndBracket()
|
|
{
|
|
if ( isBinary() )
|
|
return;
|
|
|
|
std::string passString;
|
|
unsigned int blocks = 0;
|
|
while ( !_in->getStream()->eof() )
|
|
{
|
|
passString.clear();
|
|
*this >> passString;
|
|
|
|
if ( passString=="}" )
|
|
{
|
|
if ( blocks<=0 ) return;
|
|
else blocks--;
|
|
}
|
|
else if ( passString=="{" )
|
|
blocks++;
|
|
}
|
|
}
|
|
|
|
void InputStream::readWrappedString( std::string& str )
|
|
{
|
|
*this >> str;
|
|
if ( !isBinary() )
|
|
{
|
|
if ( str[0]=='\"' )
|
|
{
|
|
if ( str.size()==1 || (*str.rbegin())!='\"' )
|
|
{
|
|
char ch;
|
|
do
|
|
{
|
|
_in->getStream()->get( ch ); checkStream();
|
|
str.append( 1, ch );
|
|
} while ( ch!='\"' );
|
|
}
|
|
str = str.substr(1, str.size()-2);
|
|
}
|
|
}
|
|
}
|
|
|
|
void InputStream::checkStream() const
|
|
{
|
|
if ( _in->isFailed() )
|
|
throw InputException(_currentField, "InputStream: Failed to read from stream.");
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|