add class FILE_OUTPUTFORMATTER and use it in PCB_IO, since it is about 8-10 faster than STREAM_OUTPUTFORMATTER

This commit is contained in:
Dick Hollenbeck 2012-10-25 00:52:55 -05:00
parent ea4b3877e2
commit 9cd011abbb
2 changed files with 49 additions and 14 deletions

View File

@ -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:
//-----<OUTPUTFORMATTER>------------------------------------------------
void write( const char* aOutBuf, int aCount ) throw( IO_ERROR )
{
fwrite( aOutBuf, aCount, 1, m_fp );
}
//-----</OUTPUTFORMATTER>-----------------------------------------------
};
/**
* Class STREAM_OUTPUTFORMATTER
* implements OUTPUTFORMATTER to a wxWidgets wxOutputStream. The stream is
@ -613,4 +644,5 @@ protected:
//-----</OUTPUTFORMATTER>-----------------------------------------------
};
#endif // RICHIO_H_

View File

@ -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