Commit d32333a0 authored by Dick Hollenbeck's avatar Dick Hollenbeck

streamline the FILE_OUTPUTFORMATTER API for ease of use

parent 9cd011ab
......@@ -453,6 +453,42 @@ void STRING_FORMATTER::StripUseless()
}
}
//-----<FILE_OUTPUTFORMATTER>----------------------------------------
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 );
}
}
//-----<STREAM_OUTPUTFORMATTER>--------------------------------------
......
......@@ -590,25 +590,24 @@ protected:
class FILE_OUTPUTFORMATTER : public OUTPUTFORMATTER
{
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:
//-----<OUTPUTFORMATTER>------------------------------------------------
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 );
//-----</OUTPUTFORMATTER>-----------------------------------------------
};
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment