From de955d2ed8b9fef0d682cc93a3cd81ac4feeed85 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 29 Jul 2010 16:09:49 +0000 Subject: [PATCH] From Wang Rui, fixes to handling of indentation. --- src/osgPlugins/osg/AsciiStreamOperator.h | 62 +++++++++++++----------- src/osgPlugins/osg/ReaderWriterOSG2.cpp | 40 +++++++++------ 2 files changed, 57 insertions(+), 45 deletions(-) diff --git a/src/osgPlugins/osg/AsciiStreamOperator.h b/src/osgPlugins/osg/AsciiStreamOperator.h index 68a684584..e57674c6c 100644 --- a/src/osgPlugins/osg/AsciiStreamOperator.h +++ b/src/osgPlugins/osg/AsciiStreamOperator.h @@ -7,7 +7,7 @@ class AsciiOutputIterator : public osgDB::OutputIterator { public: AsciiOutputIterator( std::ostream* ostream ) - : _readyForEndBracket(false), _indent(0) { _out = ostream; } + : _readyForIndent(false), _indent(0) { _out = ostream; } virtual ~AsciiOutputIterator() {} @@ -15,64 +15,63 @@ public: virtual void writeBool( bool b ) { + indentIfRequired(); if ( b ) *_out << "TRUE "; else *_out << "FALSE "; } virtual void writeChar( char c ) - { *_out << (short)c << ' '; } + { indentIfRequired(); *_out << (short)c << ' '; } virtual void writeUChar( unsigned char c ) - { *_out << (unsigned short)c << ' '; } + { indentIfRequired(); *_out << (unsigned short)c << ' '; } virtual void writeShort( short s ) - { *_out << s << ' '; } + { indentIfRequired(); *_out << s << ' '; } virtual void writeUShort( unsigned short s ) - { *_out << s << ' '; } + { indentIfRequired(); *_out << s << ' '; } virtual void writeInt( int i ) - { *_out << i << ' '; } + { indentIfRequired(); *_out << i << ' '; } virtual void writeUInt( unsigned int i ) - { *_out << i << ' '; } + { indentIfRequired(); *_out << i << ' '; } virtual void writeLong( long l ) - { *_out << l << ' '; } + { indentIfRequired(); *_out << l << ' '; } virtual void writeULong( unsigned long l ) - { *_out << l << ' '; } + { indentIfRequired(); *_out << l << ' '; } virtual void writeFloat( float f ) - { *_out << f << ' '; } + { indentIfRequired(); *_out << f << ' '; } virtual void writeDouble( double d ) - { *_out << d << ' '; } + { indentIfRequired(); *_out << d << ' '; } virtual void writeString( const std::string& s ) - { *_out << s << ' '; } + { indentIfRequired(); *_out << s << ' '; } virtual void writeStream( std::ostream& (*fn)(std::ostream&) ) { - *_out << fn; + indentIfRequired(); *_out << fn; if ( fn==static_cast(std::endl) ) { - _readyForEndBracket = true; - for (int i=0; i<_indent; ++i) - *_out << ' '; + _readyForIndent = true; } } virtual void writeBase( std::ios_base& (*fn)(std::ios_base&) ) { - *_out << fn; + indentIfRequired(); *_out << fn; } virtual void writeGLenum( const osgDB::ObjectGLenum& value ) { GLenum e = value.get(); const std::string& enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString("GL", e); - *_out << enumString << ' '; + indentIfRequired(); *_out << enumString << ' '; } virtual void writeProperty( const osgDB::ObjectProperty& prop ) @@ -82,20 +81,13 @@ public: { enumString = osgDB::Registry::instance()->getObjectWrapperManager()->getString(prop._name, prop._value); } - *_out << enumString << ' '; + indentIfRequired(); *_out << enumString << ' '; } virtual void writeMark( const osgDB::ObjectMark& mark ) { - int delta = mark._indentDelta; - if ( delta<0 && _readyForEndBracket ) - { - if ( _indent<-delta ) delta = -_indent; - _readyForEndBracket = false; - _out->seekp( delta, std::ios::cur ); - } - _indent += delta; - *_out << mark._name << ' '; + _indent += mark._indentDelta; + indentIfRequired(); *_out << mark._name; } virtual void writeCharArray( const char* s, unsigned int size ) {} @@ -115,11 +107,23 @@ public: wrappedStr.insert( 0, 1, '\"' ); wrappedStr += '\"'; + indentIfRequired(); writeString( wrappedStr ); } protected: - bool _readyForEndBracket; + + inline void indentIfRequired() + { + if ( _readyForIndent ) + { + for (int i=0; i<_indent; ++i) + *_out << ' '; + _readyForIndent = false; + } + } + + bool _readyForIndent; int _indent; }; diff --git a/src/osgPlugins/osg/ReaderWriterOSG2.cpp b/src/osgPlugins/osg/ReaderWriterOSG2.cpp index c7d9233c7..d585a39f3 100644 --- a/src/osgPlugins/osg/ReaderWriterOSG2.cpp +++ b/src/osgPlugins/osg/ReaderWriterOSG2.cpp @@ -116,7 +116,7 @@ public: virtual const char* className() const { return "OpenSceneGraph Native Format Reader/Writer"; } - Options* prepareReading( ReadResult& result, std::string& fileName, const Options* options ) const + Options* prepareReading( ReadResult& result, std::string& fileName, std::ios::openmode& mode, const Options* options ) const { std::string ext = osgDB::getLowerCaseFileExtension( fileName ); if ( !acceptsExtension(ext) ) @@ -135,7 +135,8 @@ public: static_cast(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); if ( ext=="osgt" ) local_opt->setOptionString( local_opt->getOptionString() + " Ascii" ); - if ( ext=="osgx" ) local_opt->setOptionString( local_opt->getOptionString() + " XML" ); + else if ( ext=="osgx" ) local_opt->setOptionString( local_opt->getOptionString() + " XML" ); + else mode |= std::ios::binary; return local_opt.release(); } @@ -144,10 +145,11 @@ public: { ReadResult result = ReadResult::FILE_LOADED; std::string fileName = file; - Options* local_opt = prepareReading( result, fileName, options ); + std::ios::openmode mode = std::ios::in; + Options* local_opt = prepareReading( result, fileName, mode, options ); if ( !result.success() ) return result; - osgDB::ifstream istream( fileName.c_str(), std::ios::out|std::ios::binary ); + osgDB::ifstream istream( fileName.c_str(), mode ); return readObject( istream, local_opt ); } @@ -171,10 +173,11 @@ public: { ReadResult result = ReadResult::FILE_LOADED; std::string fileName = file; - Options* local_opt = prepareReading( result, fileName, options ); + std::ios::openmode mode = std::ios::in; + Options* local_opt = prepareReading( result, fileName, mode, options ); if ( !result.success() ) return result; - osgDB::ifstream istream( fileName.c_str(), std::ios::out|std::ios::binary ); + osgDB::ifstream istream( fileName.c_str(), mode ); return readImage( istream, local_opt ); } @@ -198,10 +201,11 @@ public: { ReadResult result = ReadResult::FILE_LOADED; std::string fileName = file; - Options* local_opt = prepareReading( result, fileName, options ); + std::ios::openmode mode = std::ios::in; + Options* local_opt = prepareReading( result, fileName, mode, options ); if ( !result.success() ) return result; - osgDB::ifstream istream( fileName.c_str(), std::ios::out|std::ios::binary ); + osgDB::ifstream istream( fileName.c_str(), mode ); return readNode( istream, local_opt ); } @@ -222,7 +226,7 @@ public: return node; } - Options* prepareWriting( WriteResult& result, const std::string& fileName, const Options* options ) const + Options* prepareWriting( WriteResult& result, const std::string& fileName, std::ios::openmode& mode, const Options* options ) const { std::string ext = osgDB::getFileExtension( fileName ); if ( !acceptsExtension(ext) ) result = WriteResult::FILE_NOT_HANDLED; @@ -231,7 +235,8 @@ public: static_cast(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); if ( ext=="osgt" ) local_opt->setOptionString( local_opt->getOptionString() + " Ascii" ); - if ( ext=="osgx" ) local_opt->setOptionString( local_opt->getOptionString() + " XML" ); + else if ( ext=="osgx" ) local_opt->setOptionString( local_opt->getOptionString() + " XML" ); + else mode |= std::ios::binary; return local_opt.release(); } @@ -239,10 +244,11 @@ public: virtual WriteResult writeObject( const osg::Object& object, const std::string& fileName, const Options* options ) const { WriteResult result = WriteResult::FILE_SAVED; - osg::ref_ptr local_opt = prepareWriting( result, fileName, options ); + std::ios::openmode mode = std::ios::out; + osg::ref_ptr local_opt = prepareWriting( result, fileName, mode, options ); if ( !result.success() ) return result; - osgDB::ofstream fout( fileName.c_str(), std::ios::out|std::ios::binary ); + osgDB::ofstream fout( fileName.c_str(), mode ); if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE; result = writeObject( object, fout, local_opt.get() ); @@ -274,10 +280,11 @@ public: virtual WriteResult writeImage( const osg::Image& image, const std::string& fileName, const Options* options ) const { WriteResult result = WriteResult::FILE_SAVED; - osg::ref_ptr local_opt = prepareWriting( result, fileName, options ); + std::ios::openmode mode = std::ios::out; + osg::ref_ptr local_opt = prepareWriting( result, fileName, mode, options ); if ( !result.success() ) return result; - osgDB::ofstream fout( fileName.c_str(), std::ios::out|std::ios::binary ); + osgDB::ofstream fout( fileName.c_str(), mode ); if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE; result = writeImage( image, fout, local_opt.get() ); @@ -309,10 +316,11 @@ public: virtual WriteResult writeNode( const osg::Node& node, const std::string& fileName, const Options* options ) const { WriteResult result = WriteResult::FILE_SAVED; - osg::ref_ptr local_opt = prepareWriting( result, fileName, options ); + std::ios::openmode mode = std::ios::out; + osg::ref_ptr local_opt = prepareWriting( result, fileName, mode, options ); if ( !result.success() ) return result; - osgDB::ofstream fout( fileName.c_str(), std::ios::out|std::ios::binary ); + osgDB::ofstream fout( fileName.c_str(), mode ); if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE; result = writeNode( node, fout, local_opt.get() );