root/OpenSceneGraph/trunk/include/osgDB/InputStream @ 10986

Revision 10986, 7.3 kB (checked in by robert, 5 years ago)

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

Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13// Written by Wang Rui, (C) 2010
14
15#ifndef OSGDB_INPUTSTREAM
16#define OSGDB_INPUTSTREAM
17
18#include <osg/Endian>
19#include <osg/Vec2>
20#include <osg/Vec3>
21#include <osg/Vec4>
22#include <osg/Quat>
23#include <osg/Matrix>
24#include <osg/Array>
25#include <osg/PrimitiveSet>
26#include <osgDB/ReaderWriter>
27#include <osgDB/StreamOperator>
28#include <iostream>
29#include <sstream>
30
31namespace osgDB
32{
33
34class InputException
35{
36public:
37    InputException( const std::string& field, const std::string& err )
38    : _field(field), _error(err) {}
39   
40    const std::string& getField() const { return _field; }
41    const std::string& getError() const { return _error; }
42   
43protected:
44    std::string _field;
45    std::string _error;
46};
47
48class OSGDB_EXPORT InputStream
49{
50public:
51    typedef std::map< unsigned int, osg::ref_ptr<osg::Array> > ArrayMap;
52    typedef std::map< unsigned int, osg::ref_ptr<osg::Object> > IdentifierMap;
53   
54    enum ReadType
55    {
56        READ_UNKNOWN = 0,
57        READ_SCENE,
58        READ_IMAGE
59    };
60   
61    InputStream( const osgDB::Options* options );
62    virtual ~InputStream();
63   
64    bool isBinary() const { return _in->isBinary(); }
65    bool getUseFloatMatrix() const { return _useFloatMatrix; }
66   
67    // Serialization related functions
68    InputStream& operator>>( bool& b ) { _in->readBool(b); return *this; }
69    InputStream& operator>>( char& c ) { _in->readChar(c); return *this; }
70    InputStream& operator>>( signed char& c ) { _in->readSChar(c); return *this; }
71    InputStream& operator>>( unsigned char& c ) { _in->readUChar(c); return *this; }
72    InputStream& operator>>( short& s ) { _in->readShort(s); return *this; }
73    InputStream& operator>>( unsigned short& s ) { _in->readUShort(s); return *this; }
74    InputStream& operator>>( int& i ) { _in->readInt(i); return *this; }
75    InputStream& operator>>( unsigned int& i ) { _in->readUInt(i); return *this; }
76    InputStream& operator>>( long& l ) { _in->readLong(l); return *this; }
77    InputStream& operator>>( unsigned long& l ) { _in->readULong(l); return *this; }
78    InputStream& operator>>( float& f ) { _in->readFloat(f); return *this; }
79    InputStream& operator>>( double& d ) { _in->readDouble(d); return *this; }
80    InputStream& operator>>( std::string& s ) { _in->readString(s); return *this; }
81    InputStream& operator>>( std::istream& (*fn)(std::istream&) ) { _in->readStream(fn); return *this; }
82    InputStream& operator>>( std::ios_base& (*fn)(std::ios_base&) ) { _in->readBase(fn); return *this; }
83   
84    InputStream& operator>>( ObjectGLenum& value ) { _in->readGLenum(value); return *this; }
85    InputStream& operator>>( ObjectProperty& prop ) { _in->readProperty(prop); return *this; }
86    InputStream& operator>>( ObjectMark& mark ) { _in->readMark(mark); return *this; }
87   
88    InputStream& operator>>( osg::Vec2b& v );
89    InputStream& operator>>( osg::Vec3b& v );
90    InputStream& operator>>( osg::Vec4b& v );
91    InputStream& operator>>( osg::Vec4ub& v );
92    InputStream& operator>>( osg::Vec2s& v );
93    InputStream& operator>>( osg::Vec3s& v );
94    InputStream& operator>>( osg::Vec4s& v );
95    InputStream& operator>>( osg::Vec2f& v );
96    InputStream& operator>>( osg::Vec3f& v );
97    InputStream& operator>>( osg::Vec4f& v );
98    InputStream& operator>>( osg::Vec2d& v );
99    InputStream& operator>>( osg::Vec3d& v );
100    InputStream& operator>>( osg::Vec4d& v );
101    InputStream& operator>>( osg::Quat& q );
102    InputStream& operator>>( osg::Plane& p );
103    InputStream& operator>>( osg::Matrixf& mat );
104    InputStream& operator>>( osg::Matrixd& mat );
105   
106    InputStream& operator>>( osg::Array*& a ) { a = readArray(); return *this; }
107    InputStream& operator>>( osg::Image*& img ) { img = readImage(); return *this; }
108    InputStream& operator>>( osg::PrimitiveSet*& p ) { p = readPrimitiveSet(); return *this; }
109    InputStream& operator>>( osg::Object*& obj ) { obj = readObject(); return *this; }
110   
111    InputStream& operator>>( osg::ref_ptr<osg::Array>& ptr ) { ptr = readArray(); return *this; }
112    InputStream& operator>>( osg::ref_ptr<osg::Image>& ptr ) { ptr = readImage(); return *this; }
113    InputStream& operator>>( osg::ref_ptr<osg::PrimitiveSet>& ptr ) { ptr = readPrimitiveSet(); return *this; }
114   
115    template<typename T> InputStream& operator>>( osg::ref_ptr<T>& ptr )
116    { ptr = static_cast<T*>(readObject()); return *this; }
117   
118    // Convenient methods for reading
119    inline bool matchString( const std::string& str );
120    inline void advanceToCurrentEndBracket();
121    inline void readWrappedString( std::string& str );
122    void readCharArray( char* s, unsigned int size ) { _in->readCharArray(s, size); }
123   
124    // Global reading functions
125    osg::Array* readArray();
126    osg::PrimitiveSet* readPrimitiveSet();
127    osg::Image* readImage();
128    osg::Object* readObject( osg::Object* existingObj=0 );
129   
130    ReadType start( InputIterator* );
131    void decompress();
132   
133    // Schema handlers
134    void readSchema( std::istream& fin );
135    void resetSchema();
136   
137protected:
138    inline void checkStream() const;
139    void setWrapperSchema( const std::string& name, const std::string& properties );
140   
141    template<typename T>
142    void readArrayImplementation( T* a, int readSize, bool useByteSwap=false );
143   
144    ArrayMap _arrayMap;
145    IdentifierMap _identifierMap;
146   
147    int _byteSwap;
148    bool _useFloatMatrix;
149    std::string _currentField;
150    InputIterator* _in;
151};
152
153bool InputStream::matchString( const std::string& str )
154{
155    if ( !isBinary() )
156    {
157        std::string s; *this >> s;
158        if ( s==str ) return true;
159        else _in->getStream()->seekg( -(int)(s.length()), std::ios::cur );
160    }
161    return false;
162}
163
164void InputStream::advanceToCurrentEndBracket()
165{
166    if ( isBinary() )
167        return;
168   
169    std::string passString;
170    unsigned int blocks = 0;
171    while ( !_in->getStream()->eof() )
172    {
173        passString.clear();
174        *this >> passString;
175       
176        if ( passString=="}" )
177        {
178            if ( blocks<=0 ) return;
179            else blocks--;
180        }
181        else if ( passString=="{" )
182            blocks++;
183    }
184}
185
186void InputStream::readWrappedString( std::string& str )
187{
188    *this >> str;
189    if ( !isBinary() )
190    {
191        if ( str[0]=='\"' )
192        {
193            if ( str.size()==1 || (*str.rbegin())!='\"' )
194            {
195                char ch;
196                do
197                {
198                    _in->getStream()->get( ch ); checkStream();
199                    str.append( 1, ch );
200                } while ( ch!='\"' );
201            }
202            str = str.substr(1, str.size()-2);
203        }
204    }
205}
206
207void InputStream::checkStream() const
208{
209    if ( _in->isFailed() )
210        throw InputException(_currentField, "InputStream: Failed to read from stream.");
211}
212
213}
214
215#endif
Note: See TracBrowser for help on using the browser.