From 9cd011abbb768f2cc8874d97a054507c4d2e7505 Mon Sep 17 00:00:00 2001 From: Dick Hollenbeck Date: Thu, 25 Oct 2012 00:52:55 -0500 Subject: [PATCH] add class FILE_OUTPUTFORMATTER and use it in PCB_IO, since it is about 8-10 faster than STREAM_OUTPUTFORMATTER --- include/richio.h | 32 ++++++++++++++++++++++++++++++++ pcbnew/kicad_plugin.cpp | 31 +++++++++++++++++-------------- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/include/richio.h b/include/richio.h index 8b6f63f5ea..e600f0e0d7 100644 --- a/include/richio.h +++ b/include/richio.h @@ -582,6 +582,37 @@ protected: }; +/** + * Class FILE_OUTPUTFORMATTER + * may be used for text file output. It is about 8 times faster than + * STREAM_OUTPUTFORMATTER for file streams. + */ +class FILE_OUTPUTFORMATTER : public OUTPUTFORMATTER +{ + FILE* m_fp; ///< takes ownership + +public: + FILE_OUTPUTFORMATTER( FILE* fp ) : + m_fp( fp ) + { + } + + ~FILE_OUTPUTFORMATTER() + { + if( m_fp ) + fclose( m_fp ); + } + +protected: + //----------------------------------------------------- + void write( const char* aOutBuf, int aCount ) throw( IO_ERROR ) + { + fwrite( aOutBuf, aCount, 1, m_fp ); + } + //---------------------------------------------------- +}; + + /** * Class STREAM_OUTPUTFORMATTER * implements OUTPUTFORMATTER to a wxWidgets wxOutputStream. The stream is @@ -613,4 +644,5 @@ protected: //---------------------------------------------------- }; + #endif // RICHIO_H_ diff --git a/pcbnew/kicad_plugin.cpp b/pcbnew/kicad_plugin.cpp index 410bb889cf..b9e7da11e6 100644 --- a/pcbnew/kicad_plugin.cpp +++ b/pcbnew/kicad_plugin.cpp @@ -181,21 +181,24 @@ void FP_CACHE::Save() // Allow file output stream to go out of scope to close the file stream before // renaming the file. { - wxLogTrace( traceFootprintLibrary, wxT( "Creating temporary library file %s" ), - GetChars( tempFileName ) ); + wxLogTrace( traceFootprintLibrary, wxT( "Creating temporary library file %s" ), + GetChars( tempFileName ) ); - wxFFileOutputStream os( tempFileName ); + FILE* fp = wxFopen( tempFileName, wxT( "wt" ) ); - if( !os.IsOk() ) - { - THROW_IO_ERROR( wxString::Format( _( "cannot save footprint library file '%s'" ), - tempFileName.GetData() ) ); - } + if( !fp ) + { + wxString msg = wxString::Format( + _( "cannot save footprint library file '%s'" ), + tempFileName.GetData() ); - STREAM_OUTPUTFORMATTER formatter( os ); + THROW_IO_ERROR( msg ); + } - m_owner->SetOutputFormatter( &formatter ); - m_owner->Format( (BOARD_ITEM*) it->second->GetModule() ); + FILE_OUTPUTFORMATTER formatter( fp ); // gets fp ownership + + m_owner->SetOutputFormatter( &formatter ); + m_owner->Format( (BOARD_ITEM*) it->second->GetModule() ); } wxRemove( fn.GetFullPath() ); // it is not an error if this does not exist @@ -313,15 +316,15 @@ void PCB_IO::Save( const wxString& aFileName, BOARD* aBoard, PROPERTIES* aProper m_board = aBoard; - wxFileOutputStream fs( aFileName ); + FILE* fp = wxFopen( aFileName, wxT( "wt" ) ); - if( !fs.IsOk() ) + if( !fp ) { m_error.Printf( _( "cannot open file '%s'" ), aFileName.GetData() ); THROW_IO_ERROR( m_error ); } - STREAM_OUTPUTFORMATTER formatter( fs ); + FILE_OUTPUTFORMATTER formatter( fp ); // gets ownership of fp m_out = &formatter; // no ownership