| 1 | #ifndef OSG2_BINARYSTREAMOPERATOR |
|---|
| 2 | #define OSG2_BINARYSTREAMOPERATOR |
|---|
| 3 | |
|---|
| 4 | #include <osgDB/StreamOperator> |
|---|
| 5 | |
|---|
| 6 | class BinaryOutputIterator : public osgDB::OutputIterator |
|---|
| 7 | { |
|---|
| 8 | public: |
|---|
| 9 | BinaryOutputIterator( std::ostream* ostream ) { _out = ostream; } |
|---|
| 10 | virtual ~BinaryOutputIterator() {} |
|---|
| 11 | |
|---|
| 12 | virtual bool isBinary() const { return true; } |
|---|
| 13 | |
|---|
| 14 | virtual void writeBool( bool b ) |
|---|
| 15 | { char c = b?1:0; _out->write( &c, CHAR_SIZE ); } |
|---|
| 16 | |
|---|
| 17 | virtual void writeChar( char c ) |
|---|
| 18 | { _out->write( &c, CHAR_SIZE ); } |
|---|
| 19 | |
|---|
| 20 | virtual void writeUChar( unsigned char c ) |
|---|
| 21 | { _out->write( (char*)&c, CHAR_SIZE ); } |
|---|
| 22 | |
|---|
| 23 | virtual void writeShort( short s ) |
|---|
| 24 | { _out->write( (char*)&s, SHORT_SIZE ); } |
|---|
| 25 | |
|---|
| 26 | virtual void writeUShort( unsigned short s ) |
|---|
| 27 | { _out->write( (char*)&s, SHORT_SIZE ); } |
|---|
| 28 | |
|---|
| 29 | virtual void writeInt( int i ) |
|---|
| 30 | { _out->write( (char*)&i, INT_SIZE ); } |
|---|
| 31 | |
|---|
| 32 | virtual void writeUInt( unsigned int i ) |
|---|
| 33 | { _out->write( (char*)&i, INT_SIZE ); } |
|---|
| 34 | |
|---|
| 35 | virtual void writeLong( long l ) |
|---|
| 36 | { _out->write( (char*)&l, LONG_SIZE ); } |
|---|
| 37 | |
|---|
| 38 | virtual void writeULong( unsigned long l ) |
|---|
| 39 | { _out->write( (char*)&l, LONG_SIZE ); } |
|---|
| 40 | |
|---|
| 41 | virtual void writeFloat( float f ) |
|---|
| 42 | { _out->write( (char*)&f, FLOAT_SIZE ); } |
|---|
| 43 | |
|---|
| 44 | virtual void writeDouble( double d ) |
|---|
| 45 | { _out->write((char*)&d, DOUBLE_SIZE); } |
|---|
| 46 | |
|---|
| 47 | virtual void writeString( const std::string& s ) |
|---|
| 48 | { |
|---|
| 49 | int size = s.size(); |
|---|
| 50 | _out->write( (char*)&size, INT_SIZE ); |
|---|
| 51 | _out->write( s.c_str(), s.size() ); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | virtual void writeStream( std::ostream& (*fn)(std::ostream&) ) {} |
|---|
| 55 | |
|---|
| 56 | virtual void writeBase( std::ios_base& (*fn)(std::ios_base&) ) {} |
|---|
| 57 | |
|---|
| 58 | virtual void writeGLenum( const osgDB::ObjectGLenum& value ) |
|---|
| 59 | { GLenum e = value.get(); _out->write((char*)&e, GLENUM_SIZE); } |
|---|
| 60 | |
|---|
| 61 | virtual void writeProperty( const osgDB::ObjectProperty& prop ) |
|---|
| 62 | { if (prop._mapProperty) _out->write((char*)&(prop._value), INT_SIZE); } |
|---|
| 63 | |
|---|
| 64 | virtual void writeMark( const osgDB::ObjectMark& mark ) {} |
|---|
| 65 | |
|---|
| 66 | virtual void writeCharArray( const char* s, unsigned int size ) |
|---|
| 67 | { if ( size>0 ) _out->write( s, size ); } |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | class BinaryInputIterator : public osgDB::InputIterator |
|---|
| 71 | { |
|---|
| 72 | public: |
|---|
| 73 | BinaryInputIterator( std::istream* istream ) : _byteSwap(0) { _in = istream; } |
|---|
| 74 | virtual ~BinaryInputIterator() {} |
|---|
| 75 | |
|---|
| 76 | virtual bool isBinary() const { return true; } |
|---|
| 77 | |
|---|
| 78 | virtual void readBool( bool& b ) |
|---|
| 79 | { |
|---|
| 80 | char c = 0; |
|---|
| 81 | _in->read( &c, CHAR_SIZE ); |
|---|
| 82 | b = (c!=0); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | virtual void readChar( char& c ) |
|---|
| 86 | { _in->read( &c, CHAR_SIZE ); } |
|---|
| 87 | |
|---|
| 88 | virtual void readSChar( signed char& c ) |
|---|
| 89 | { _in->read( (char*)&c, CHAR_SIZE ); } |
|---|
| 90 | |
|---|
| 91 | virtual void readUChar( unsigned char& c ) |
|---|
| 92 | { _in->read( (char*)&c, CHAR_SIZE ); } |
|---|
| 93 | |
|---|
| 94 | virtual void readShort( short& s ) |
|---|
| 95 | { |
|---|
| 96 | _in->read( (char*)&s, SHORT_SIZE ); |
|---|
| 97 | if ( _byteSwap ) osg::swapBytes( (char*)&s, SHORT_SIZE ); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | virtual void readUShort( unsigned short& s ) |
|---|
| 101 | { |
|---|
| 102 | _in->read( (char*)&s, SHORT_SIZE ); |
|---|
| 103 | if ( _byteSwap ) osg::swapBytes( (char*)&s, SHORT_SIZE ); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | virtual void readInt( int& i ) |
|---|
| 107 | { |
|---|
| 108 | _in->read( (char*)&i, INT_SIZE ); |
|---|
| 109 | if ( _byteSwap ) osg::swapBytes( (char*)&i, INT_SIZE ); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | virtual void readUInt( unsigned int& i ) |
|---|
| 113 | { |
|---|
| 114 | _in->read( (char*)&i, INT_SIZE ); |
|---|
| 115 | if ( _byteSwap ) osg::swapBytes( (char*)&i, INT_SIZE ); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | virtual void readLong( long& l ) |
|---|
| 119 | { |
|---|
| 120 | _in->read( (char*)&l, LONG_SIZE ); |
|---|
| 121 | if ( _byteSwap ) osg::swapBytes( (char*)&l, LONG_SIZE ); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | virtual void readULong( unsigned long& l ) |
|---|
| 125 | { |
|---|
| 126 | _in->read( (char*)&l, LONG_SIZE ); |
|---|
| 127 | if ( _byteSwap ) osg::swapBytes( (char*)&l, LONG_SIZE ); |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | virtual void readFloat( float& f ) |
|---|
| 131 | { |
|---|
| 132 | _in->read( (char*)&f, FLOAT_SIZE ); |
|---|
| 133 | if ( _byteSwap ) osg::swapBytes( (char*)&f, FLOAT_SIZE ); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | virtual void readDouble( double& d ) |
|---|
| 137 | { |
|---|
| 138 | _in->read( (char*)&d, DOUBLE_SIZE ); |
|---|
| 139 | if ( _byteSwap ) osg::swapBytes( (char*)&d, DOUBLE_SIZE ); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | virtual void readString( std::string& s ) |
|---|
| 143 | { |
|---|
| 144 | int size = 0; readInt( size ); |
|---|
| 145 | if ( size ) |
|---|
| 146 | { |
|---|
| 147 | s.resize( size ); |
|---|
| 148 | _in->read( (char*)s.c_str(), size ); |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | virtual void readStream( std::istream& (*fn)(std::istream&) ) {} |
|---|
| 153 | |
|---|
| 154 | virtual void readBase( std::ios_base& (*fn)(std::ios_base&) ) {} |
|---|
| 155 | |
|---|
| 156 | virtual void readGLenum( osgDB::ObjectGLenum& value ) |
|---|
| 157 | { |
|---|
| 158 | GLenum e = 0; |
|---|
| 159 | _in->read( (char*)&e, GLENUM_SIZE ); |
|---|
| 160 | if ( _byteSwap ) osg::swapBytes( (char*)&e, GLENUM_SIZE ); |
|---|
| 161 | value.set( e ); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | virtual void readProperty( osgDB::ObjectProperty& prop ) |
|---|
| 165 | { |
|---|
| 166 | int value = 0; |
|---|
| 167 | if ( prop._mapProperty ) |
|---|
| 168 | { |
|---|
| 169 | _in->read( (char*)&value, INT_SIZE ); |
|---|
| 170 | if ( _byteSwap ) osg::swapBytes( (char*)&value, INT_SIZE ); |
|---|
| 171 | } |
|---|
| 172 | prop.set( value ); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | virtual void readMark( osgDB::ObjectMark& mark ) {} |
|---|
| 176 | |
|---|
| 177 | virtual void readCharArray( char* s, unsigned int size ) |
|---|
| 178 | { if ( size>0 ) _in->read( s, size ); } |
|---|
| 179 | |
|---|
| 180 | protected: |
|---|
| 181 | int _byteSwap; |
|---|
| 182 | }; |
|---|
| 183 | |
|---|
| 184 | #endif |
|---|