Show
Ignore:
Timestamp:
01/27/10 18:09:05 (5 years ago)
Author:
robert
Message:

From Wang Rui, "Changes:

1. Rewrite the reading/writing exception handlers to work like the ive
plugin exceptions.
2. Write a header writing/checking function in ReaderWriterOSG2.cpp,
which may help decide if the stream is ascii or binary. The
readInputIterator() function will return null pointer if the input
file is nither osgb nor osgt format, which indicates that the old .osg
format could be used here, in case we've merged the two plugins
together.
3. Add a new ForceReadingImage? option in the InputStream?, which will
allocate an empty image object with the filename if specifed external
image file is missed. It may be useful for format converting in some
cases.
4. Add new osgParticle wrappers, as well as some modification to the
osgParticle headers, for instance, change isEnabled() to getEnabled().
5. Some fixes to the osg serialization wrappers."

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/include/osgDB/InputStream

    r10986 r11018  
    3232{ 
    3333 
    34 class InputException 
     34class InputException : public osg::Referenced 
    3535{ 
    3636public: 
    37     InputException( const std::string& field, const std::string& err ) 
    38     : _field(field), _error(err) {} 
     37    InputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err) 
     38    { 
     39        for ( unsigned int i=0; i<fields.size(); ++i ) 
     40        { 
     41            _field += fields[i]; 
     42            _field += " "; 
     43        } 
     44    } 
    3945     
    4046    const std::string& getField() const { return _field; } 
     
    6672     
    6773    // 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; } 
     74    InputStream& operator>>( bool& b ) { _in->readBool(b); checkStream(); return *this; } 
     75    InputStream& operator>>( char& c ) { _in->readChar(c); checkStream(); return *this; } 
     76    InputStream& operator>>( signed char& c ) { _in->readSChar(c); checkStream(); return *this; } 
     77    InputStream& operator>>( unsigned char& c ) { _in->readUChar(c); checkStream(); return *this; } 
     78    InputStream& operator>>( short& s ) { _in->readShort(s); checkStream(); return *this; } 
     79    InputStream& operator>>( unsigned short& s ) { _in->readUShort(s); checkStream(); return *this; } 
     80    InputStream& operator>>( int& i ) { _in->readInt(i); checkStream(); return *this; } 
     81    InputStream& operator>>( unsigned int& i ) { _in->readUInt(i); checkStream(); return *this; } 
     82    InputStream& operator>>( long& l ) { _in->readLong(l); checkStream(); return *this; } 
     83    InputStream& operator>>( unsigned long& l ) { _in->readULong(l); checkStream(); return *this; } 
     84    InputStream& operator>>( float& f ) { _in->readFloat(f); checkStream(); return *this; } 
     85    InputStream& operator>>( double& d ) { _in->readDouble(d); checkStream(); return *this; } 
     86    InputStream& operator>>( std::string& s ) { _in->readString(s); checkStream(); return *this; } 
     87    InputStream& operator>>( std::istream& (*fn)(std::istream&) ) { _in->readStream(fn); checkStream(); return *this; } 
     88    InputStream& operator>>( std::ios_base& (*fn)(std::ios_base&) ) { _in->readBase(fn); checkStream(); return *this; } 
    8389     
    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; } 
     90    InputStream& operator>>( ObjectGLenum& value ) { _in->readGLenum(value); checkStream(); return *this; } 
     91    InputStream& operator>>( ObjectProperty& prop ) { _in->readProperty(prop); checkStream(); return *this; } 
     92    InputStream& operator>>( ObjectMark& mark ) { _in->readMark(mark); checkStream(); return *this; } 
    8793     
    8894    InputStream& operator>>( osg::Vec2b& v ); 
     
    117123     
    118124    // Convenient methods for reading 
    119     inline bool matchString( const std::string& str ); 
    120     inline void advanceToCurrentEndBracket(); 
    121     inline void readWrappedString( std::string& str ); 
     125    bool matchString( const std::string& str ); 
     126    void advanceToCurrentEndBracket(); 
     127    void readWrappedString( std::string& str ); 
    122128    void readCharArray( char* s, unsigned int size ) { _in->readCharArray(s, size); } 
    123129     
     
    135141    void resetSchema(); 
    136142     
     143    // Exception handlers 
     144    inline void throwException( const std::string& msg ); 
     145    const InputException* getException() const { return _exception.get(); } 
     146     
    137147protected: 
    138     inline void checkStream() const; 
     148    inline void checkStream(); 
    139149    void setWrapperSchema( const std::string& name, const std::string& properties ); 
    140150     
     
    147157    int _byteSwap; 
    148158    bool _useFloatMatrix; 
    149     std::string _currentField; 
    150     InputIterator* _in; 
     159    bool _forceReadingImage; 
     160    std::vector<std::string> _fields; 
     161    osg::ref_ptr<InputIterator> _in; 
     162    osg::ref_ptr<InputException> _exception; 
    151163}; 
    152164 
    153 bool InputStream::matchString( const std::string& str ) 
     165void InputStream::throwException( const std::string& msg ) 
    154166{ 
    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; 
     167    _exception = new InputException(_fields, msg); 
    162168} 
    163169 
    164 void InputStream::advanceToCurrentEndBracket() 
     170void InputStream::checkStream() 
    165171{ 
    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  
    186 void 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  
    207 void InputStream::checkStream() const 
    208 { 
     172    _in->checkStream(); 
    209173    if ( _in->isFailed() ) 
    210         throw InputException(_currentField, "InputStream: Failed to read from stream."); 
     174        throwException( "InputStream: Failed to read from stream." ); 
    211175} 
    212176