Changeset 11018 for OpenSceneGraph/trunk/include/osgDB/InputStream
- Timestamp:
- 01/27/10 18:09:05 (3 years ago)
- Files:
-
- 1 modified
-
OpenSceneGraph/trunk/include/osgDB/InputStream (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/include/osgDB/InputStream
r10986 r11018 32 32 { 33 33 34 class InputException 34 class InputException : public osg::Referenced 35 35 { 36 36 public: 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 } 39 45 40 46 const std::string& getField() const { return _field; } … … 66 72 67 73 // 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; } 83 89 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; } 87 93 88 94 InputStream& operator>>( osg::Vec2b& v ); … … 117 123 118 124 // Convenient methods for reading 119 inlinebool matchString( const std::string& str );120 inlinevoid advanceToCurrentEndBracket();121 inlinevoid readWrappedString( std::string& str );125 bool matchString( const std::string& str ); 126 void advanceToCurrentEndBracket(); 127 void readWrappedString( std::string& str ); 122 128 void readCharArray( char* s, unsigned int size ) { _in->readCharArray(s, size); } 123 129 … … 135 141 void resetSchema(); 136 142 143 // Exception handlers 144 inline void throwException( const std::string& msg ); 145 const InputException* getException() const { return _exception.get(); } 146 137 147 protected: 138 inline void checkStream() const;148 inline void checkStream(); 139 149 void setWrapperSchema( const std::string& name, const std::string& properties ); 140 150 … … 147 157 int _byteSwap; 148 158 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; 151 163 }; 152 164 153 bool InputStream::matchString( const std::string& str)165 void InputStream::throwException( const std::string& msg ) 154 166 { 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); 162 168 } 163 169 164 void InputStream:: advanceToCurrentEndBracket()170 void InputStream::checkStream() 165 171 { 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(); 209 173 if ( _in->isFailed() ) 210 throw InputException(_currentField, "InputStream: Failed to read from stream.");174 throwException( "InputStream: Failed to read from stream." ); 211 175 } 212 176
