2010-01-21 04:13:33 +08:00
|
|
|
/* -*-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
|
|
|
|
|
2010-01-25 19:03:21 +08:00
|
|
|
#ifndef OSGDB_INPUTSTREAM
|
|
|
|
#define OSGDB_INPUTSTREAM
|
2010-01-21 04:13:33 +08:00
|
|
|
|
|
|
|
#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>
|
2010-01-25 19:03:21 +08:00
|
|
|
#include <osgDB/StreamOperator>
|
2010-01-21 04:13:33 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace osgDB
|
|
|
|
{
|
|
|
|
|
2010-01-28 01:09:05 +08:00
|
|
|
class InputException : public osg::Referenced
|
2010-01-21 04:13:33 +08:00
|
|
|
{
|
|
|
|
public:
|
2010-01-28 01:09:05 +08:00
|
|
|
InputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err)
|
|
|
|
{
|
|
|
|
for ( unsigned int i=0; i<fields.size(); ++i )
|
|
|
|
{
|
|
|
|
_field += fields[i];
|
|
|
|
_field += " ";
|
|
|
|
}
|
|
|
|
}
|
2010-01-21 04:13:33 +08:00
|
|
|
|
|
|
|
const std::string& getField() const { return _field; }
|
|
|
|
const std::string& getError() const { return _error; }
|
2010-01-21 17:25:45 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
protected:
|
|
|
|
std::string _field;
|
|
|
|
std::string _error;
|
|
|
|
};
|
|
|
|
|
2010-01-21 17:25:45 +08:00
|
|
|
class OSGDB_EXPORT InputStream
|
2010-01-21 04:13:33 +08:00
|
|
|
{
|
|
|
|
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,
|
2010-03-10 21:48:41 +08:00
|
|
|
READ_IMAGE,
|
|
|
|
READ_OBJECT
|
2010-01-21 04:13:33 +08:00
|
|
|
};
|
|
|
|
|
2010-01-25 19:03:21 +08:00
|
|
|
InputStream( const osgDB::Options* options );
|
2010-01-21 04:13:33 +08:00
|
|
|
virtual ~InputStream();
|
|
|
|
|
2010-01-25 19:03:21 +08:00
|
|
|
bool isBinary() const { return _in->isBinary(); }
|
2010-01-21 04:13:33 +08:00
|
|
|
bool getUseFloatMatrix() const { return _useFloatMatrix; }
|
2010-02-26 01:53:51 +08:00
|
|
|
const osgDB::Options* getOptions() const { return _options.get(); }
|
2010-01-21 04:13:33 +08:00
|
|
|
|
|
|
|
// Serialization related functions
|
2010-01-28 01:09:05 +08:00
|
|
|
InputStream& operator>>( bool& b ) { _in->readBool(b); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( char& c ) { _in->readChar(c); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( signed char& c ) { _in->readSChar(c); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( unsigned char& c ) { _in->readUChar(c); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( short& s ) { _in->readShort(s); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( unsigned short& s ) { _in->readUShort(s); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( int& i ) { _in->readInt(i); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( unsigned int& i ) { _in->readUInt(i); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( long& l ) { _in->readLong(l); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( unsigned long& l ) { _in->readULong(l); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( float& f ) { _in->readFloat(f); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( double& d ) { _in->readDouble(d); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( std::string& s ) { _in->readString(s); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( std::istream& (*fn)(std::istream&) ) { _in->readStream(fn); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( std::ios_base& (*fn)(std::ios_base&) ) { _in->readBase(fn); checkStream(); return *this; }
|
|
|
|
|
|
|
|
InputStream& operator>>( ObjectGLenum& value ) { _in->readGLenum(value); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( ObjectProperty& prop ) { _in->readProperty(prop); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( ObjectMark& mark ) { _in->readMark(mark); checkStream(); return *this; }
|
2010-01-21 04:13:33 +08:00
|
|
|
|
|
|
|
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
|
2010-03-10 21:48:41 +08:00
|
|
|
bool matchString( const std::string& str ) { return _in->matchString(str); }
|
|
|
|
void advanceToCurrentEndBracket() { _in->advanceToCurrentEndBracket(); }
|
|
|
|
void readWrappedString( std::string& str ) { _in->readWrappedString(str); checkStream(); }
|
2010-01-25 19:03:21 +08:00
|
|
|
void readCharArray( char* s, unsigned int size ) { _in->readCharArray(s, size); }
|
2010-02-11 01:03:09 +08:00
|
|
|
|
|
|
|
// readSize() use unsigned int for all sizes.
|
|
|
|
unsigned int readSize() { unsigned int size; *this>>size; return size; }
|
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
// Global reading functions
|
|
|
|
osg::Array* readArray();
|
|
|
|
osg::PrimitiveSet* readPrimitiveSet();
|
|
|
|
osg::Image* readImage();
|
|
|
|
osg::Object* readObject( osg::Object* existingObj=0 );
|
2010-09-24 00:12:05 +08:00
|
|
|
osg::Object* readObjectFields( const std::string& className, osg::Object* existingObj=0);
|
2010-01-21 04:13:33 +08:00
|
|
|
|
2010-01-25 19:03:21 +08:00
|
|
|
ReadType start( InputIterator* );
|
2010-01-21 04:13:33 +08:00
|
|
|
void decompress();
|
|
|
|
|
|
|
|
// Schema handlers
|
|
|
|
void readSchema( std::istream& fin );
|
|
|
|
void resetSchema();
|
|
|
|
|
2010-01-28 01:09:05 +08:00
|
|
|
// Exception handlers
|
|
|
|
inline void throwException( const std::string& msg );
|
|
|
|
const InputException* getException() const { return _exception.get(); }
|
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
protected:
|
2010-01-28 01:09:05 +08:00
|
|
|
inline void checkStream();
|
2010-01-21 04:13:33 +08:00
|
|
|
void setWrapperSchema( const std::string& name, const std::string& properties );
|
|
|
|
|
|
|
|
template<typename T>
|
2010-02-11 01:03:09 +08:00
|
|
|
void readArrayImplementation( T* a, int read_size, bool useByteSwap=false );
|
2010-01-21 04:13:33 +08:00
|
|
|
|
|
|
|
ArrayMap _arrayMap;
|
|
|
|
IdentifierMap _identifierMap;
|
|
|
|
|
|
|
|
int _byteSwap;
|
|
|
|
bool _useFloatMatrix;
|
2010-05-13 04:02:31 +08:00
|
|
|
bool _useSchemaData;
|
2010-01-28 01:09:05 +08:00
|
|
|
bool _forceReadingImage;
|
|
|
|
std::vector<std::string> _fields;
|
|
|
|
osg::ref_ptr<InputIterator> _in;
|
|
|
|
osg::ref_ptr<InputException> _exception;
|
2010-02-26 01:53:51 +08:00
|
|
|
osg::ref_ptr<const osgDB::Options> _options;
|
2010-06-15 18:02:34 +08:00
|
|
|
|
|
|
|
// store here to avoid a new and a leak in InputStream::decompress
|
|
|
|
std::stringstream* _dataDecompress;
|
2010-01-21 04:13:33 +08:00
|
|
|
};
|
|
|
|
|
2010-01-28 01:09:05 +08:00
|
|
|
void InputStream::throwException( const std::string& msg )
|
2010-01-21 04:13:33 +08:00
|
|
|
{
|
2010-01-28 01:09:05 +08:00
|
|
|
_exception = new InputException(_fields, msg);
|
2010-01-21 04:13:33 +08:00
|
|
|
}
|
|
|
|
|
2010-01-28 01:09:05 +08:00
|
|
|
void InputStream::checkStream()
|
2010-01-21 04:13:33 +08:00
|
|
|
{
|
2010-01-28 01:09:05 +08:00
|
|
|
_in->checkStream();
|
2010-01-25 19:03:21 +08:00
|
|
|
if ( _in->isFailed() )
|
2010-01-28 01:09:05 +08:00
|
|
|
throwException( "InputStream: Failed to read from stream." );
|
2010-01-21 04:13:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|