| 1 | #ifndef OSGDB_ASCIISTREAMOPERATOR |
|---|
| 2 | #define OSGDB_ASCIISTREAMOPERATOR |
|---|
| 3 | |
|---|
| 4 | #include <ostream> |
|---|
| 5 | #include <osgDB/StreamOperator> |
|---|
| 6 | |
|---|
| 7 | class AsciiOutputIterator : public osgDB::OutputIterator |
|---|
| 8 | { |
|---|
| 9 | public: |
|---|
| 10 | AsciiOutputIterator( std::ostream* ostream, int precision ) |
|---|
| 11 | : _readyForIndent(false), _indent(0) |
|---|
| 12 | { |
|---|
| 13 | _out = ostream; |
|---|
| 14 | if (precision>0) _out->precision(precision); |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | virtual ~AsciiOutputIterator() {} |
|---|
| 18 | |
|---|
| 19 | virtual bool isBinary() const { return false; } |
|---|
| 20 | |
|---|
| 21 | virtual void writeBool( bool b ) |
|---|
| 22 | { |
|---|
| 23 | indentIfRequired(); |
|---|
| 24 | if ( b ) *_out << "TRUE "; |
|---|
| 25 | else *_out << "FALSE "; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | virtual void writeChar( char c ) |
|---|
| 29 | { indentIfRequired(); *_out << (short)c << ' '; } |
|---|
| 30 | |
|---|
| 31 | virtual void writeUChar( unsigned char c ) |
|---|
| 32 | { indentIfRequired(); *_out << (unsigned short)c << ' '; } |
|---|
| 33 | |
|---|
| 34 | virtual void writeShort( short s ) |
|---|
| 35 | { indentIfRequired(); *_out << s << ' '; } |
|---|
| 36 | |
|---|
| 37 | virtual void writeUShort( unsigned short s ) |
|---|
| 38 | { indentIfRequired(); *_out << s << ' '; } |
|---|
| 39 | |
|---|
| 40 | virtual void writeInt( int i ) |
|---|
| 41 | { indentIfRequired(); *_out << i << ' '; } |
|---|
| 42 | |
|---|
| 43 | virtual void writeUInt( unsigned int i ) |
|---|
| 44 | { indentIfRequired(); *_out << i << ' '; } |
|---|
| 45 | |
|---|
| 46 | virtual void writeLong( long l ) |
|---|
| 47 | { indentIfRequired(); *_out << l << ' '; } |
|---|
| 48 | |
|---|
| 49 | virtual void writeULong( unsigned long l ) |
|---|
| 50 | { indentIfRequired(); *_out << l << ' '; } |
|---|
| 51 | |
|---|
| 52 | virtual void writeFloat( float f ) |
|---|
| 53 | { indentIfRequired(); *_out << f << ' '; } |
|---|
| 54 | |
|---|
| 55 | virtual void writeDouble( double d ) |
|---|
| 56 | { indentIfRequired(); *_out << d << ' '; } |
|---|
| 57 | |
|---|
| 58 | virtual void writeString( const std::string& s ) |
|---|
| 59 | { indentIfRequired(); *_out << s << ' '; } |
|---|
| 60 | |
|---|
| 61 | virtual void writeStream( std::ostream& (*fn)(std::ostream&) ) |
|---|
| 62 | { |
|---|
| 63 | indentIfRequired(); *_out << fn; |
|---|
| 64 | if ( isEndl( fn ) ) |
|---|
| 65 | { |
|---|
| 66 | _readyForIndent = true; |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | virtual void writeBase( std::ios_base& (*fn)(std::ios_base&) ) |
|---|
| 71 | { |
|---|
| 72 | indentIfRequired(); *_out << fn; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | virtual void writeGLenum( const osgDB::ObjectGLenum& value ) |
|---|
| 76 | { |
|---|
| 77 | GLenum e = value.get(); |
|---|
| 78 | const std::string& enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString("GL", e); |
|---|
| 79 | indentIfRequired(); *_out << enumString << ' '; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | virtual void writeProperty( const osgDB::ObjectProperty& prop ) |
|---|
| 83 | { |
|---|
| 84 | std::string enumString = prop._name; |
|---|
| 85 | if ( prop._mapProperty ) |
|---|
| 86 | { |
|---|
| 87 | enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString(prop._name, prop._value); |
|---|
| 88 | } |
|---|
| 89 | indentIfRequired(); *_out << enumString << ' '; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | virtual void writeMark( const osgDB::ObjectMark& mark ) |
|---|
| 93 | { |
|---|
| 94 | _indent += mark._indentDelta; |
|---|
| 95 | indentIfRequired(); *_out << mark._name; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | virtual void writeCharArray( const char* s, unsigned int size ) {} |
|---|
| 99 | |
|---|
| 100 | virtual void writeWrappedString( const std::string& str ) |
|---|
| 101 | { |
|---|
| 102 | std::string wrappedStr; |
|---|
| 103 | unsigned int size = str.size(); |
|---|
| 104 | for ( unsigned int i=0; i<size; ++i ) |
|---|
| 105 | { |
|---|
| 106 | char ch = str[i]; |
|---|
| 107 | if ( ch=='\"' ) wrappedStr += '\\'; |
|---|
| 108 | else if ( ch=='\\' ) wrappedStr += '\\'; |
|---|
| 109 | wrappedStr += ch; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | wrappedStr.insert( std::string::size_type(0), 1, '\"' ); |
|---|
| 113 | wrappedStr += '\"'; |
|---|
| 114 | |
|---|
| 115 | indentIfRequired(); |
|---|
| 116 | writeString( wrappedStr ); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | protected: |
|---|
| 120 | |
|---|
| 121 | inline void indentIfRequired() |
|---|
| 122 | { |
|---|
| 123 | if ( _readyForIndent ) |
|---|
| 124 | { |
|---|
| 125 | for (int i=0; i<_indent; ++i) |
|---|
| 126 | *_out << ' '; |
|---|
| 127 | _readyForIndent = false; |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | bool _readyForIndent; |
|---|
| 132 | int _indent; |
|---|
| 133 | }; |
|---|
| 134 | |
|---|
| 135 | class AsciiInputIterator : public osgDB::InputIterator |
|---|
| 136 | { |
|---|
| 137 | public: |
|---|
| 138 | AsciiInputIterator( std::istream* istream ) { _in = istream; } |
|---|
| 139 | virtual ~AsciiInputIterator() {} |
|---|
| 140 | |
|---|
| 141 | virtual bool isBinary() const { return false; } |
|---|
| 142 | |
|---|
| 143 | virtual void readBool( bool& b ) |
|---|
| 144 | { |
|---|
| 145 | std::string boolString; |
|---|
| 146 | readString( boolString ); |
|---|
| 147 | if ( boolString=="TRUE" ) b = true; |
|---|
| 148 | else b = false; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | virtual void readChar( char& c ) |
|---|
| 152 | { |
|---|
| 153 | short s = 0; |
|---|
| 154 | readShort( s ); |
|---|
| 155 | c = (char)s; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | virtual void readSChar( signed char& c ) |
|---|
| 159 | { |
|---|
| 160 | short s = 0; |
|---|
| 161 | readShort( s ); |
|---|
| 162 | c = (signed char)s; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | virtual void readUChar( unsigned char& c ) |
|---|
| 166 | { |
|---|
| 167 | short s = 0; |
|---|
| 168 | readShort( s ); |
|---|
| 169 | c = (unsigned char)s; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | virtual void readShort( short& s ) |
|---|
| 173 | { std::string str; readString(str); s = static_cast<short>(strtol(str.c_str(), NULL, 0)); } |
|---|
| 174 | |
|---|
| 175 | virtual void readUShort( unsigned short& s ) |
|---|
| 176 | { std::string str; readString(str); s = static_cast<unsigned short>(strtoul(str.c_str(), NULL, 0)); } |
|---|
| 177 | |
|---|
| 178 | virtual void readInt( int& i ) |
|---|
| 179 | { std::string str; readString(str); i = static_cast<int>(strtol(str.c_str(), NULL, 0)); } |
|---|
| 180 | |
|---|
| 181 | virtual void readUInt( unsigned int& i ) |
|---|
| 182 | { std::string str; readString(str); i = static_cast<unsigned int>(strtoul(str.c_str(), NULL, 0)); } |
|---|
| 183 | |
|---|
| 184 | virtual void readLong( long& l ) |
|---|
| 185 | { std::string str; readString(str); l = strtol(str.c_str(), NULL, 0); } |
|---|
| 186 | |
|---|
| 187 | virtual void readULong( unsigned long& l ) |
|---|
| 188 | { std::string str; readString(str); l = strtoul(str.c_str(), NULL, 0); } |
|---|
| 189 | |
|---|
| 190 | virtual void readFloat( float& f ) |
|---|
| 191 | { std::string str; readString(str); f = osg::asciiToFloat(str.c_str()); } |
|---|
| 192 | |
|---|
| 193 | virtual void readDouble( double& d ) |
|---|
| 194 | { std::string str; readString(str); d = osg::asciiToDouble(str.c_str()); } |
|---|
| 195 | |
|---|
| 196 | virtual void readString( std::string& s ) |
|---|
| 197 | { |
|---|
| 198 | if ( _preReadString.empty() ) |
|---|
| 199 | *_in >> s; |
|---|
| 200 | else |
|---|
| 201 | { |
|---|
| 202 | s = _preReadString; |
|---|
| 203 | _preReadString.clear(); |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | virtual void readStream( std::istream& (*fn)(std::istream&) ) |
|---|
| 208 | { *_in >> fn; } |
|---|
| 209 | |
|---|
| 210 | virtual void readBase( std::ios_base& (*fn)(std::ios_base&) ) |
|---|
| 211 | { *_in >> fn; } |
|---|
| 212 | |
|---|
| 213 | virtual void readGLenum( osgDB::ObjectGLenum& value ) |
|---|
| 214 | { |
|---|
| 215 | GLenum e = 0; |
|---|
| 216 | std::string enumString; |
|---|
| 217 | readString( enumString ); |
|---|
| 218 | e = osgDB::Registry::instance()->getObjectWrapperManager()->getValue("GL", enumString); |
|---|
| 219 | value.set( e ); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | virtual void readProperty( osgDB::ObjectProperty& prop ) |
|---|
| 223 | { |
|---|
| 224 | int value = 0; |
|---|
| 225 | std::string enumString; |
|---|
| 226 | readString( enumString ); |
|---|
| 227 | if ( prop._mapProperty ) |
|---|
| 228 | { |
|---|
| 229 | value = osgDB::Registry::instance()->getObjectWrapperManager()->getValue(prop._name, enumString); |
|---|
| 230 | } |
|---|
| 231 | else |
|---|
| 232 | { |
|---|
| 233 | if ( prop._name!=enumString ) |
|---|
| 234 | { |
|---|
| 235 | OSG_WARN << "AsciiInputIterator::readProperty(): Unmatched property " |
|---|
| 236 | << enumString << ", expecting " << prop._name << std::endl; |
|---|
| 237 | } |
|---|
| 238 | prop._name = enumString; |
|---|
| 239 | } |
|---|
| 240 | prop.set( value ); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | virtual void readMark( osgDB::ObjectMark& mark ) |
|---|
| 244 | { |
|---|
| 245 | std::string markString; |
|---|
| 246 | readString( markString ); |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | virtual void readCharArray( char* s, unsigned int size ) {} |
|---|
| 250 | |
|---|
| 251 | virtual void readWrappedString( std::string& str ) |
|---|
| 252 | { |
|---|
| 253 | char ch; |
|---|
| 254 | getCharacter( ch ); |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | while ( ch==' ' || (ch=='\n') || (ch=='\r')) |
|---|
| 258 | { |
|---|
| 259 | getCharacter( ch ); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | if (ch=='"') |
|---|
| 263 | { |
|---|
| 264 | |
|---|
| 265 | getCharacter( ch ); |
|---|
| 266 | while ( ch!='"' ) |
|---|
| 267 | { |
|---|
| 268 | if (ch=='\\') |
|---|
| 269 | { |
|---|
| 270 | getCharacter( ch ); |
|---|
| 271 | str += ch; |
|---|
| 272 | } |
|---|
| 273 | else str += ch; |
|---|
| 274 | |
|---|
| 275 | getCharacter( ch ); |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | else |
|---|
| 279 | { |
|---|
| 280 | |
|---|
| 281 | while ( (ch!=' ') && (ch!=0) && (ch!='\n') ) |
|---|
| 282 | { |
|---|
| 283 | str += ch; |
|---|
| 284 | getCharacter( ch ); |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | virtual bool matchString( const std::string& str ) |
|---|
| 290 | { |
|---|
| 291 | if ( _preReadString.empty() ) |
|---|
| 292 | *_in >> _preReadString; |
|---|
| 293 | |
|---|
| 294 | if ( _preReadString==str ) |
|---|
| 295 | { |
|---|
| 296 | _preReadString.clear(); |
|---|
| 297 | return true; |
|---|
| 298 | } |
|---|
| 299 | return false; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | virtual void advanceToCurrentEndBracket() |
|---|
| 303 | { |
|---|
| 304 | std::string passString; |
|---|
| 305 | unsigned int blocks = 0; |
|---|
| 306 | while ( !_in->eof() ) |
|---|
| 307 | { |
|---|
| 308 | passString.clear(); |
|---|
| 309 | readString( passString ); |
|---|
| 310 | |
|---|
| 311 | if ( passString=="}" ) |
|---|
| 312 | { |
|---|
| 313 | if ( blocks<=0 ) return; |
|---|
| 314 | else blocks--; |
|---|
| 315 | } |
|---|
| 316 | else if ( passString=="{" ) |
|---|
| 317 | blocks++; |
|---|
| 318 | } |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | protected: |
|---|
| 322 | void getCharacter( char& ch ) |
|---|
| 323 | { |
|---|
| 324 | if ( !_preReadString.empty() ) |
|---|
| 325 | { |
|---|
| 326 | ch = _preReadString[0]; |
|---|
| 327 | _preReadString.erase( _preReadString.begin() ); |
|---|
| 328 | } |
|---|
| 329 | else |
|---|
| 330 | { |
|---|
| 331 | _in->get( ch ); |
|---|
| 332 | checkStream(); |
|---|
| 333 | } |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | std::string _preReadString; |
|---|
| 337 | }; |
|---|
| 338 | |
|---|
| 339 | #endif |
|---|