| 1 | #ifndef OSGDB_ASCIISTREAMOPERATOR |
|---|
| 2 | #define OSGDB_ASCIISTREAMOPERATOR |
|---|
| 3 | |
|---|
| 4 | #include <osgDB/StreamOperator> |
|---|
| 5 | |
|---|
| 6 | class AsciiOutputIterator : public osgDB::OutputIterator |
|---|
| 7 | { |
|---|
| 8 | public: |
|---|
| 9 | AsciiOutputIterator( std::ostream* ostream ) |
|---|
| 10 | : _readyForEndBracket(false), _indent(0) { _out = ostream; } |
|---|
| 11 | |
|---|
| 12 | virtual ~AsciiOutputIterator() {} |
|---|
| 13 | |
|---|
| 14 | virtual bool isBinary() const { return false; } |
|---|
| 15 | |
|---|
| 16 | virtual void writeBool( bool b ) |
|---|
| 17 | { |
|---|
| 18 | if ( b ) *_out << "TRUE "; |
|---|
| 19 | else *_out << "FALSE "; |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | virtual void writeChar( char c ) |
|---|
| 23 | { *_out << (short)c << ' '; } |
|---|
| 24 | |
|---|
| 25 | virtual void writeUChar( unsigned char c ) |
|---|
| 26 | { *_out << (unsigned short)c << ' '; } |
|---|
| 27 | |
|---|
| 28 | virtual void writeShort( short s ) |
|---|
| 29 | { *_out << s << ' '; } |
|---|
| 30 | |
|---|
| 31 | virtual void writeUShort( unsigned short s ) |
|---|
| 32 | { *_out << s << ' '; } |
|---|
| 33 | |
|---|
| 34 | virtual void writeInt( int i ) |
|---|
| 35 | { *_out << i << ' '; } |
|---|
| 36 | |
|---|
| 37 | virtual void writeUInt( unsigned int i ) |
|---|
| 38 | { *_out << i << ' '; } |
|---|
| 39 | |
|---|
| 40 | virtual void writeLong( long l ) |
|---|
| 41 | { *_out << l << ' '; } |
|---|
| 42 | |
|---|
| 43 | virtual void writeULong( unsigned long l ) |
|---|
| 44 | { *_out << l << ' '; } |
|---|
| 45 | |
|---|
| 46 | virtual void writeFloat( float f ) |
|---|
| 47 | { *_out << f << ' '; } |
|---|
| 48 | |
|---|
| 49 | virtual void writeDouble( double d ) |
|---|
| 50 | { *_out << d << ' '; } |
|---|
| 51 | |
|---|
| 52 | virtual void writeString( const std::string& s ) |
|---|
| 53 | { *_out << s << ' '; } |
|---|
| 54 | |
|---|
| 55 | virtual void writeStream( std::ostream& (*fn)(std::ostream&) ) |
|---|
| 56 | { |
|---|
| 57 | *_out << fn; |
|---|
| 58 | if ( fn==static_cast<std::ostream& (*)(std::ostream&)>(std::endl) ) |
|---|
| 59 | { |
|---|
| 60 | _readyForEndBracket = true; |
|---|
| 61 | for (int i=0; i<_indent; ++i) |
|---|
| 62 | *_out << ' '; |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | virtual void writeBase( std::ios_base& (*fn)(std::ios_base&) ) |
|---|
| 67 | { |
|---|
| 68 | *_out << fn; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | virtual void writeGLenum( const osgDB::ObjectGLenum& value ) |
|---|
| 72 | { |
|---|
| 73 | GLenum e = value.get(); |
|---|
| 74 | const std::string& enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString("GL", e); |
|---|
| 75 | *_out << enumString << ' '; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | virtual void writeProperty( const osgDB::ObjectProperty& prop ) |
|---|
| 79 | { |
|---|
| 80 | std::string enumString = prop._name; |
|---|
| 81 | if ( prop._mapProperty ) |
|---|
| 82 | { |
|---|
| 83 | enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString(prop._name, prop._value); |
|---|
| 84 | } |
|---|
| 85 | *_out << enumString << ' '; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | virtual void writeMark( const osgDB::ObjectMark& mark ) |
|---|
| 89 | { |
|---|
| 90 | int delta = mark._indentDelta; |
|---|
| 91 | if ( delta<0 && _readyForEndBracket ) |
|---|
| 92 | { |
|---|
| 93 | if ( _indent<-delta ) delta = -_indent; |
|---|
| 94 | _readyForEndBracket = false; |
|---|
| 95 | _out->seekp( delta, std::ios::cur ); |
|---|
| 96 | } |
|---|
| 97 | _indent += delta; |
|---|
| 98 | *_out << mark._name << ' '; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | virtual void writeCharArray( const char* s, unsigned int size ) {} |
|---|
| 102 | |
|---|
| 103 | protected: |
|---|
| 104 | bool _readyForEndBracket; |
|---|
| 105 | int _indent; |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | class AsciiInputIterator : public osgDB::InputIterator |
|---|
| 109 | { |
|---|
| 110 | public: |
|---|
| 111 | AsciiInputIterator( std::istream* istream ) { _in = istream; } |
|---|
| 112 | virtual ~AsciiInputIterator() {} |
|---|
| 113 | |
|---|
| 114 | virtual bool isBinary() const { return false; } |
|---|
| 115 | |
|---|
| 116 | virtual void readBool( bool& b ) |
|---|
| 117 | { |
|---|
| 118 | std::string boolString; |
|---|
| 119 | *_in >> boolString; checkStream(); |
|---|
| 120 | if ( boolString=="TRUE" ) b = true; |
|---|
| 121 | else b = false; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | virtual void readChar( char& c ) |
|---|
| 125 | { |
|---|
| 126 | short s = 0; |
|---|
| 127 | *_in >> s; checkStream(); |
|---|
| 128 | c = (char)s; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | virtual void readSChar( signed char& c ) |
|---|
| 132 | { |
|---|
| 133 | short s = 0; |
|---|
| 134 | *_in >> s; checkStream(); |
|---|
| 135 | c = (signed char)s; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | virtual void readUChar( unsigned char& c ) |
|---|
| 139 | { |
|---|
| 140 | short s = 0; |
|---|
| 141 | *_in >> s; checkStream(); |
|---|
| 142 | c = (unsigned char)s; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | virtual void readShort( short& s ) |
|---|
| 146 | { *_in >> s; checkStream(); } |
|---|
| 147 | |
|---|
| 148 | virtual void readUShort( unsigned short& s ) |
|---|
| 149 | { *_in >> s; checkStream(); } |
|---|
| 150 | |
|---|
| 151 | virtual void readInt( int& i ) |
|---|
| 152 | { *_in >> i; checkStream(); } |
|---|
| 153 | |
|---|
| 154 | virtual void readUInt( unsigned int& i ) |
|---|
| 155 | { *_in >> i; checkStream(); } |
|---|
| 156 | |
|---|
| 157 | virtual void readLong( long& l ) |
|---|
| 158 | { *_in >> l; checkStream(); } |
|---|
| 159 | |
|---|
| 160 | virtual void readULong( unsigned long& l ) |
|---|
| 161 | { *_in >> l; checkStream(); } |
|---|
| 162 | |
|---|
| 163 | virtual void readFloat( float& f ) |
|---|
| 164 | { *_in >> f; checkStream(); } |
|---|
| 165 | |
|---|
| 166 | virtual void readDouble( double& d ) |
|---|
| 167 | { *_in >> d; checkStream(); } |
|---|
| 168 | |
|---|
| 169 | virtual void readString( std::string& s ) |
|---|
| 170 | { *_in >> s; checkStream(); } |
|---|
| 171 | |
|---|
| 172 | virtual void readStream( std::istream& (*fn)(std::istream&) ) |
|---|
| 173 | { *_in >> fn; } |
|---|
| 174 | |
|---|
| 175 | virtual void readBase( std::ios_base& (*fn)(std::ios_base&) ) |
|---|
| 176 | { *_in >> fn; } |
|---|
| 177 | |
|---|
| 178 | virtual void readGLenum( osgDB::ObjectGLenum& value ) |
|---|
| 179 | { |
|---|
| 180 | GLenum e = 0; |
|---|
| 181 | std::string enumString; |
|---|
| 182 | *_in >> enumString; checkStream(); |
|---|
| 183 | e = osgDB::Registry::instance()->getObjectWrapperManager()->getValue("GL", enumString); |
|---|
| 184 | value.set( e ); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | virtual void readProperty( osgDB::ObjectProperty& prop ) |
|---|
| 188 | { |
|---|
| 189 | int value = 0; |
|---|
| 190 | std::string enumString; |
|---|
| 191 | *_in >> enumString; checkStream(); |
|---|
| 192 | if ( prop._mapProperty ) |
|---|
| 193 | { |
|---|
| 194 | value = osgDB::Registry::instance()->getObjectWrapperManager()->getValue(prop._name, enumString); |
|---|
| 195 | } |
|---|
| 196 | else |
|---|
| 197 | { |
|---|
| 198 | if ( prop._name!=enumString ) |
|---|
| 199 | { |
|---|
| 200 | osg::notify(osg::WARN) << "AsciiInputIterator::readProperty(): Unmatched property " |
|---|
| 201 | << enumString << ", expecting " << prop._name << std::endl; |
|---|
| 202 | } |
|---|
| 203 | prop._name = enumString; |
|---|
| 204 | } |
|---|
| 205 | prop.set( value ); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | virtual void readMark( osgDB::ObjectMark& mark ) |
|---|
| 209 | { |
|---|
| 210 | std::string markString; |
|---|
| 211 | *_in >> markString; checkStream(); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | virtual void readCharArray( char* s, unsigned int size ) {} |
|---|
| 215 | }; |
|---|
| 216 | |
|---|
| 217 | #endif |
|---|