Commit 7c5e42b4 authored by Dick Hollenbeck's avatar Dick Hollenbeck

switch from STREAM_OUTPUTFORMATTER to FILE_OUTPUTFORMATTER mostly...

switch from STREAM_OUTPUTFORMATTER to FILE_OUTPUTFORMATTER mostly throughout,and minor richio factoring
parent 58ae2e19
......@@ -289,12 +289,12 @@ const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee, const char* quote
if( *wrapee == '#' )
return quote_char;
if( strlen(wrapee)==0 )
if( strlen( wrapee ) == 0 )
return quote_char;
bool isFirst = true;
for( ; *wrapee; ++wrapee, isFirst=false )
for( ; *wrapee; ++wrapee, isFirst = false )
{
static const char quoteThese[] = "\t ()"
"%" // per Alfons of freerouting.net, he does not like this unquoted as of 1-Feb-2008
......@@ -314,12 +314,17 @@ const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee, const char* quote
}
const char* OUTPUTFORMATTER::GetQuoteChar( const char* wrapee )
{
return GetQuoteChar( wrapee, quoteChar );
}
int OUTPUTFORMATTER::vprint( const char* fmt, va_list ap ) throw( IO_ERROR )
{
int ret = vsnprintf( &buffer[0], buffer.size(), fmt, ap );
if( ret >= (int) buffer.size() )
{
buffer.resize( ret+2000 );
buffer.resize( ret + 2000 );
ret = vsnprintf( &buffer[0], buffer.size(), fmt, ap );
}
......@@ -422,7 +427,7 @@ std::string OUTPUTFORMATTER::Quotes( const std::string& aWrapee ) throw( IO_ERRO
std::string OUTPUTFORMATTER::Quotew( const wxString& aWrapee ) throw( IO_ERROR )
{
// s-expressions atoms are always encoded as UTF-8.
// wxStrings are always encoded as UTF-8 as we convert to a byte sequence.
// The non-virutal function calls the virtual workhorse function, and if
// a different quoting or escaping strategy is desired from the standard,
// a derived class can overload Quotes() above, but
......@@ -455,8 +460,9 @@ void STRING_FORMATTER::StripUseless()
//-----<FILE_OUTPUTFORMATTER>----------------------------------------
FILE_OUTPUTFORMATTER::FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode )
throw( IO_ERROR ) :
FILE_OUTPUTFORMATTER::FILE_OUTPUTFORMATTER( const wxString& aFileName,
const wxChar* aMode, char aQuoteChar ) throw( IO_ERROR ) :
OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
m_filename( aFileName )
{
m_fp = wxFopen( aFileName, aMode );
......@@ -492,12 +498,6 @@ void FILE_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount ) throw( IO_ER
//-----<STREAM_OUTPUTFORMATTER>--------------------------------------
const char* STREAM_OUTPUTFORMATTER::GetQuoteChar( const char* wrapee )
{
return OUTPUTFORMATTER::GetQuoteChar( wrapee, quoteChar );
}
void STREAM_OUTPUTFORMATTER::write( const char* aOutBuf, int aCount ) throw( IO_ERROR )
{
int lastWrite;
......
......@@ -124,9 +124,15 @@ void LIB_EDIT_FRAME::OnExportPart( wxCommandEvent& event )
SaveOnePartInMemory();
wxFFileOutputStream os( fn.GetFullPath(), wxT( "wt" ) );
bool result = false;
if( !os.IsOk() )
try
{
FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
result = m_library->Save( formatter );
}
catch( ... /* IO_ERROR ioe */ )
{
fn.MakeAbsolute();
msg = wxT( "Failed to create component library file " ) + fn.GetFullPath();
......@@ -134,17 +140,13 @@ void LIB_EDIT_FRAME::OnExportPart( wxCommandEvent& event )
return;
}
STREAM_OUTPUTFORMATTER formatter( os );
bool success = m_library->Save( formatter );
if( success )
if( result )
m_lastLibExportPath = fn.GetPath();
delete m_library;
m_library = CurLibTmp;
if( success )
if( result )
{
if( createLib )
{
......
......@@ -78,21 +78,21 @@ bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName )
}
}
wxFFileOutputStream os( aFileName, wxT( "wt" ) );
if( !os.IsOk() )
try
{
msg = wxT( "Failed to create component library file " ) + aFileName;
DisplayError( this, msg );
return false;
}
FILE_OUTPUTFORMATTER formatter( aFileName );
STREAM_OUTPUTFORMATTER formatter( os );
if( !libCache->Save( formatter ) )
if( !libCache->Save( formatter ) )
{
msg.Printf( _( "An error occurred attempting to save component library <%s>." ),
GetChars( aFileName ) );
DisplayError( this, msg );
return false;
}
}
catch( ... /* IO_ERROR ioe */ )
{
msg.Printf( _( "An error occurred attempting to save component \
library <%s>." ), GetChars( aFileName ) );
msg = wxT( "Failed to create component library file " ) + aFileName;
DisplayError( this, msg );
return false;
}
......
......@@ -360,9 +360,19 @@ void LIB_EDIT_FRAME::SaveActiveLibrary( wxCommandEvent& event )
}
}
wxFFileOutputStream libStream( libFileName.GetFullPath(), wxT( "wt" ) );
try
{
FILE_OUTPUTFORMATTER libFormatter( libFileName.GetFullPath() );
if( !libStream.IsOk() )
if( !m_library->Save( libFormatter ) )
{
msg = _( "Error occurred while saving library file \"" ) + fn.GetFullPath() + _( "\"." );
AppendMsgPanel( _( "*** ERROR: ***" ), msg, RED );
DisplayError( this, msg );
return;
}
}
catch( ... /* IO_ERROR ioe */ )
{
libFileName.MakeAbsolute();
msg = wxT( "Failed to create component library file " ) + libFileName.GetFullPath();
......@@ -370,16 +380,6 @@ void LIB_EDIT_FRAME::SaveActiveLibrary( wxCommandEvent& event )
return;
}
STREAM_OUTPUTFORMATTER libFormatter( libStream );
if( !m_library->Save( libFormatter ) )
{
msg = _( "Error occurred while saving library file \"" ) + fn.GetFullPath() + _( "\"." );
AppendMsgPanel( _( "*** ERROR: ***" ), msg, RED );
DisplayError( this, msg );
return;
}
wxFileName docFileName = libFileName;
docFileName.SetExt( DOC_EXT );
......@@ -399,9 +399,20 @@ void LIB_EDIT_FRAME::SaveActiveLibrary( wxCommandEvent& event )
}
}
wxFFileOutputStream docStream( docFileName.GetFullPath(), wxT( "wt" ) );
try
{
FILE_OUTPUTFORMATTER docFormatter( docFileName.GetFullPath() );
if( !docStream.IsOk() )
if( !m_library->SaveDocs( docFormatter ) )
{
msg = _( "Error occurred while saving library document file \"" ) +
docFileName.GetFullPath() + _( "\"." );
AppendMsgPanel( _( "*** ERROR: ***" ), msg, RED );
DisplayError( this, msg );
return;
}
}
catch( ... /* IO_ERROR ioe */ )
{
docFileName.MakeAbsolute();
msg = wxT( "Failed to create component document library file " ) +
......@@ -410,17 +421,6 @@ void LIB_EDIT_FRAME::SaveActiveLibrary( wxCommandEvent& event )
return;
}
STREAM_OUTPUTFORMATTER docFormatter( docStream );
if( !m_library->SaveDocs( docFormatter ) )
{
msg = _( "Error occurred while saving library document file \"" ) +
docFileName.GetFullPath() + _( "\"." );
AppendMsgPanel( _( "*** ERROR: ***" ), msg, RED );
DisplayError( this, msg );
return;
}
msg = _( "Library file \"" ) + fn.GetFullName() + wxT( "\" Ok" );
fn.SetExt( DOC_EXT );
wxString msg1 = _( "Document file \"" ) + fn.GetFullPath() + wxT( "\" Ok" );
......
......@@ -1063,35 +1063,21 @@ bool NETLIST_EXPORT_TOOL::WriteKiCadNetList( const wxString& aOutFileName )
for( unsigned ii = 0; ii < g_NetObjectslist.size(); ii++ )
g_NetObjectslist[ii]->m_Flag = 0;
bool rc = false;
wxFFileOutputStream os( aOutFileName, wxT( "wt" ) );
std::auto_ptr<XNODE> xroot( makeGenericRoot() );
if( !os.IsOk() )
try
{
L_error:
wxString msg = _( "Failed to create file " ) + aOutFileName;
DisplayError( NULL, msg );
FILE_OUTPUTFORMATTER formatter( aOutFileName );
xroot->Format( &formatter, 0 );
}
else
catch( IO_ERROR ioe )
{
XNODE* xroot = makeGenericRoot();
try
{
STREAM_OUTPUTFORMATTER outputFormatter( os );
xroot->Format( &outputFormatter, 0 );
}
catch( IO_ERROR ioe )
{
delete xroot;
goto L_error;
}
delete xroot;
rc = true;
DisplayError( NULL, ioe.errorText );
return false;
}
return rc;
return true;
}
bool NETLIST_EXPORT_TOOL::WriteGENERICNetList( const wxString& aOutFileName )
......
......@@ -155,20 +155,9 @@ void LIB_EDIT_FRAME::SaveOneSymbol()
wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );
wxFile file( fn.GetFullPath(), wxFile::write );
if( !file.IsOpened() )
{
msg.Printf( _( "Unable to create file <%s>" ), GetChars( fn.GetFullPath() ) );
DisplayError( this, msg );
return;
}
msg.Printf( _( "Saving symbol in [%s]" ), GetChars( fn.GetPath() ) );
SetStatusText( msg );
wxFileOutputStream os( file );
STREAM_OUTPUTFORMATTER formatter( os );
wxString line;
/* File header */
......@@ -201,36 +190,46 @@ void LIB_EDIT_FRAME::SaveOneSymbol()
try
{
formatter.Print( 0, "%s", TO_UTF8( line ) );
m_component->GetReferenceField().Save( formatter );
m_component->GetValueField().Save( formatter );
formatter.Print( 0, "DRAW\n" );
FILE_OUTPUTFORMATTER formatter( fn.GetFullPath() );
LIB_ITEMS& drawList = m_component->GetDrawItemList();
BOOST_FOREACH( LIB_ITEM& item, drawList )
try
{
if( item.Type() == LIB_FIELD_T )
continue;
formatter.Print( 0, "%s", TO_UTF8( line ) );
m_component->GetReferenceField().Save( formatter );
m_component->GetValueField().Save( formatter );
formatter.Print( 0, "DRAW\n" );
/* Don't save unused parts or alternate body styles. */
if( m_unit && item.GetUnit() && ( item.GetUnit() != m_unit ) )
continue;
LIB_ITEMS& drawList = m_component->GetDrawItemList();
if( m_convert && item.GetConvert() && ( item.GetConvert() != m_convert ) )
continue;
BOOST_FOREACH( LIB_ITEM& item, drawList )
{
if( item.Type() == LIB_FIELD_T )
continue;
item.Save( formatter );
}
/* Don't save unused parts or alternate body styles. */
if( m_unit && item.GetUnit() && ( item.GetUnit() != m_unit ) )
continue;
formatter.Print( 0, "ENDDRAW\n" );
formatter.Print( 0, "ENDDEF\n" );
if( m_convert && item.GetConvert() && ( item.GetConvert() != m_convert ) )
continue;
item.Save( formatter );
}
formatter.Print( 0, "ENDDRAW\n" );
formatter.Print( 0, "ENDDEF\n" );
}
catch( IO_ERROR ioe )
{
msg.Printf( _( "An error occurred attempting to save symbol file <%s>" ),
GetChars( fn.GetFullPath() ) );
DisplayError( this, msg );
}
}
catch( IO_ERROR ioe )
{
msg.Printf( _( "An error occurred attempting to save symbol file <%s>" ),
GetChars( fn.GetFullPath() ) );
DisplayError( this, msg );
DisplayError( this, ioe.errorText );
return;
}
}
......
......@@ -406,9 +406,11 @@ public:
};
#define OUTPUTFMTBUFZ 500 ///< default buffer size for any OUTPUT_FORMATTER
/**
* Class OUTPUTFORMATTER
* is an important interface (abstract) class used to output UTF8 text in
* is an important interface (abstract class) used to output 8 bit text in
* a convenient way. The primary interface is "printf() - like" but
* with support for indentation control. The destination of the 8 bit
* wide text is up to the implementer.
......@@ -424,16 +426,19 @@ public:
*/
class OUTPUTFORMATTER
{
std::vector<char> buffer;
std::vector<char> buffer;
char quoteChar[2];
int sprint( const char* fmt, ... ) throw( IO_ERROR );
int vprint( const char* fmt, va_list ap ) throw( IO_ERROR );
protected:
OUTPUTFORMATTER( int aReserve = 300 ) :
OUTPUTFORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
buffer( aReserve, '\0' )
{
quoteChar[0] = aQuoteChar;
quoteChar[1] = '\0';
}
virtual ~OUTPUTFORMATTER() {}
......@@ -507,10 +512,7 @@ public:
* @return const char* - the quote_char as a single character string, or ""
* if the wrapee does not need to be wrapped.
*/
virtual const char* GetQuoteChar( const char* wrapee )
{
return GetQuoteChar( wrapee, "\"" );
}
virtual const char* GetQuoteChar( const char* wrapee );
/**
* Function Quotes
......@@ -550,8 +552,8 @@ public:
* Constructor STRING_FORMATTER
* reserves space in the buffer
*/
STRING_FORMATTER( int aReserve = 300 ) :
OUTPUTFORMATTER( aReserve )
STRING_FORMATTER( int aReserve = OUTPUTFMTBUFZ, char aQuoteChar = '"' ) :
OUTPUTFORMATTER( aReserve, aQuoteChar )
{
}
......@@ -575,8 +577,8 @@ public:
return mystring;
}
//-----<OUTPUTFORMATTER>------------------------------------------------
protected:
//-----<OUTPUTFORMATTER>------------------------------------------------
void write( const char* aOutBuf, int aCount ) throw( IO_ERROR );
//-----</OUTPUTFORMATTER>-----------------------------------------------
};
......@@ -589,9 +591,6 @@ protected:
*/
class FILE_OUTPUTFORMATTER : public OUTPUTFORMATTER
{
FILE* m_fp; ///< takes ownership
wxString m_filename;
public:
/**
......@@ -599,9 +598,14 @@ public:
* @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.
* @param qQuoteChar is a C string holding a single char used for quoting
problematic strings (with whitespace or special characters in them).
* @throw IO_ERROR if the file cannot be opened.
*/
FILE_OUTPUTFORMATTER( const wxString& aFileName, const wxChar* aMode = wxT( "wt" ) ) throw( IO_ERROR );
FILE_OUTPUTFORMATTER( const wxString& aFileName,
const wxChar* aMode = wxT( "wt" ),
char aQuoteChar = '"' )
throw( IO_ERROR );
~FILE_OUTPUTFORMATTER();
......@@ -609,6 +613,9 @@ protected:
//-----<OUTPUTFORMATTER>------------------------------------------------
void write( const char* aOutBuf, int aCount ) throw( IO_ERROR );
//-----</OUTPUTFORMATTER>-----------------------------------------------
FILE* m_fp; ///< takes ownership
wxString m_filename;
};
......@@ -620,7 +627,6 @@ protected:
class STREAM_OUTPUTFORMATTER : public OUTPUTFORMATTER
{
wxOutputStream& os;
char quoteChar[2];
public:
/**
......@@ -629,16 +635,13 @@ public:
* to a file, socket, or zip file.
*/
STREAM_OUTPUTFORMATTER( wxOutputStream& aStream, char aQuoteChar = '"' ) :
OUTPUTFORMATTER( OUTPUTFMTBUFZ, aQuoteChar ),
os( aStream )
{
quoteChar[0] = aQuoteChar;
quoteChar[1] = 0;
}
//-----<OUTPUTFORMATTER>------------------------------------------------
const char* GetQuoteChar( const char* wrapee );
protected:
//-----<OUTPUTFORMATTER>------------------------------------------------
void write( const char* aOutBuf, int aCount ) throw( IO_ERROR );
//-----</OUTPUTFORMATTER>-----------------------------------------------
};
......
......@@ -84,32 +84,27 @@ bool PCB_CALCULATOR_FRAME::ReadDataFile()
bool PCB_CALCULATOR_FRAME::WriteDataFile()
{
wxFFileOutputStream os( GetDataFilename(), wxT( "wt" ) );
if( !os.IsOk() )
return false;
// Switch the locale to standard C (needed to read/write floating point numbers
LOCALE_IO toggle;
PCB_CALCULATOR_DATAFILE * datafile = new PCB_CALCULATOR_DATAFILE( &m_RegulatorList );
std::auto_ptr<PCB_CALCULATOR_DATAFILE> datafile( new PCB_CALCULATOR_DATAFILE( &m_RegulatorList ) );
try
{
int nestlevel;
STREAM_OUTPUTFORMATTER outputFormatter( os );
nestlevel = datafile->WriteHeader( &outputFormatter );
datafile->Format( &outputFormatter, nestlevel );
FILE_OUTPUTFORMATTER formatter( GetDataFilename() );
int nestlevel = datafile->WriteHeader( &formatter );
datafile->Format( &formatter, nestlevel );
while( nestlevel-- )
outputFormatter.Print( nestlevel, ")\n" );
formatter.Print( nestlevel, ")\n" );
}
catch( IO_ERROR ioe )
{
delete datafile;
return false;
}
delete datafile;
m_RegulatorListChanged = false;
return true;
}
......
......@@ -3437,19 +3437,12 @@ void SPECCTRA_DB::ExportPCB( wxString filename, bool aNameChange ) throw( IO_ERR
{
if( pcb )
{
wxFFileOutputStream os( filename, wxT( "wt" ) );
if( !os.IsOk() )
{
ThrowIOError( _("Unable to open file \"%s\""), GetChars(filename) );
}
STREAM_OUTPUTFORMATTER outputFormatter( os, quote_char[0] );
FILE_OUTPUTFORMATTER formatter( filename, wxT( "wt" ), quote_char[0] );
if( aNameChange )
pcb->pcbname = TO_UTF8(filename);
pcb->pcbname = TO_UTF8( filename );
pcb->Format( &outputFormatter, 0 );
pcb->Format( &formatter, 0 );
}
}
......@@ -3458,16 +3451,9 @@ void SPECCTRA_DB::ExportSESSION( wxString filename )
{
if( session )
{
wxFFileOutputStream os( filename, wxT( "wt" ) );
if( !os.IsOk() )
{
ThrowIOError( _("Unable to open file \"%s\""), GetChars(filename) );
}
STREAM_OUTPUTFORMATTER outputFormatter( os, quote_char[0] );
FILE_OUTPUTFORMATTER formatter( filename, wxT( "wt" ), quote_char[0] );
session->Format( &outputFormatter, 0 );
session->Format( &formatter, 0 );
}
}
......
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