diff --git a/common/richio.cpp b/common/richio.cpp index e264f6ba3d..0130d06786 100644 --- a/common/richio.cpp +++ b/common/richio.cpp @@ -453,6 +453,42 @@ void STRING_FORMATTER::StripUseless() } } +//--------------------------------------------- + +FILE_OUTPUTFORMATTER::FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode ) + throw( IO_ERROR ) : + m_filename( aFileName ) +{ + m_fp = wxFopen( aFileName, aMode ); + + if( !m_fp ) + { + wxString msg = wxString::Format( + _( "cannot open or save file '%s'" ), + m_filename.GetData() ); + THROW_IO_ERROR( msg ); + } +} + + +FILE_OUTPUTFORMATTER::~FILE_OUTPUTFORMATTER() +{ + if( m_fp ) + fclose( m_fp ); +} + + +void FILE_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount ) throw( IO_ERROR ) +{ + if( 1 != fwrite( aOutBuf, aCount, 1, m_fp ) ) + { + wxString msg = wxString::Format( + _( "error writing to file '%s'" ), + m_filename.GetData() ); + THROW_IO_ERROR( msg ); + } +} + //------------------------------------------- diff --git a/include/richio.h b/include/richio.h index e600f0e0d7..02bef292d6 100644 --- a/include/richio.h +++ b/include/richio.h @@ -589,26 +589,25 @@ protected: */ class FILE_OUTPUTFORMATTER : public OUTPUTFORMATTER { - FILE* m_fp; ///< takes ownership + FILE* m_fp; ///< takes ownership + wxString m_filename; public: - FILE_OUTPUTFORMATTER( FILE* fp ) : - m_fp( fp ) - { - } - ~FILE_OUTPUTFORMATTER() - { - if( m_fp ) - fclose( m_fp ); - } + /** + * Constructor + * @param aFileName is the full filename to open and save to as a text file. + * @param aMode is what you would pass to wxFopen()'s mode, defaults to wxT( "wt" ) + * for text files that are to be created here and now. + * @throw IO_ERROR if the file cannot be opened. + */ + FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ) ) throw( IO_ERROR ); + + ~FILE_OUTPUTFORMATTER(); protected: //----------------------------------------------------- - void write( const char* aOutBuf, int aCount ) throw( IO_ERROR ) - { - fwrite( aOutBuf, aCount, 1, m_fp ); - } + void write( const char* aOutBuf, int aCount ) throw( IO_ERROR ); //---------------------------------------------------- }; diff --git a/pcbnew/kicad_plugin.cpp b/pcbnew/kicad_plugin.cpp index b9e7da11e6..480cd837e7 100644 --- a/pcbnew/kicad_plugin.cpp +++ b/pcbnew/kicad_plugin.cpp @@ -184,18 +184,7 @@ void FP_CACHE::Save() wxLogTrace( traceFootprintLibrary, wxT( "Creating temporary library file %s" ), GetChars( tempFileName ) ); - FILE* fp = wxFopen( tempFileName, wxT( "wt" ) ); - - if( !fp ) - { - wxString msg = wxString::Format( - _( "cannot save footprint library file '%s'" ), - tempFileName.GetData() ); - - THROW_IO_ERROR( msg ); - } - - FILE_OUTPUTFORMATTER formatter( fp ); // gets fp ownership + FILE_OUTPUTFORMATTER formatter( tempFileName ); m_owner->SetOutputFormatter( &formatter ); m_owner->Format( (BOARD_ITEM*) it->second->GetModule() ); @@ -316,15 +305,7 @@ void PCB_IO::Save( const wxString& aFileName, BOARD* aBoard, PROPERTIES* aProper m_board = aBoard; - FILE* fp = wxFopen( aFileName, wxT( "wt" ) ); - - if( !fp ) - { - m_error.Printf( _( "cannot open file '%s'" ), aFileName.GetData() ); - THROW_IO_ERROR( m_error ); - } - - FILE_OUTPUTFORMATTER formatter( fp ); // gets ownership of fp + FILE_OUTPUTFORMATTER formatter( aFileName ); m_out = &formatter; // no ownership