Commit 3541335a authored by Wayne Stambaugh's avatar Wayne Stambaugh

Use output formatter to save component library files and objects.

parent fcb482df
...@@ -527,52 +527,45 @@ LIB_PIN* LIB_COMPONENT::GetPin( const wxString& aNumber, int aUnit, int aConvert ...@@ -527,52 +527,45 @@ LIB_PIN* LIB_COMPONENT::GetPin( const wxString& aNumber, int aUnit, int aConvert
} }
bool LIB_COMPONENT::Save( FILE* aFile ) bool LIB_COMPONENT::Save( OUTPUTFORMATTER& aFormatter )
{ {
size_t i; size_t i;
LIB_FIELD& value = GetValueField(); LIB_FIELD& value = GetValueField();
/* First line: it s a comment (component name for readers) */ /* First line: it s a comment (component name for readers) */
if( fprintf( aFile, "#\n# %s\n#\n", TO_UTF8( value.m_Text ) ) < 0 ) aFormatter.Print( 0, "#\n# %s\n#\n", TO_UTF8( value.m_Text ) );
return false;
/* Save data */ /* Save data */
if( fprintf( aFile, "DEF" ) < 0 ) aFormatter.Print( 0, "DEF" );
return false;
if( value.IsVisible() ) if( value.IsVisible() )
{ {
if( fprintf( aFile, " %s", TO_UTF8( value.m_Text ) ) < 0 ) aFormatter.Print( 0, " %s", TO_UTF8( value.m_Text ) );
return false;
} }
else else
{ {
if( fprintf( aFile, " ~%s", TO_UTF8( value.m_Text ) ) < 0 ) aFormatter.Print( 0, " ~%s", TO_UTF8( value.m_Text ) );
return false;
} }
LIB_FIELD& reference = GetReferenceField(); LIB_FIELD& reference = GetReferenceField();
if( !reference.m_Text.IsEmpty() ) if( !reference.m_Text.IsEmpty() )
{ {
if( fprintf( aFile, " %s", TO_UTF8( reference.m_Text ) ) < 0 ) aFormatter.Print( 0, " %s", TO_UTF8( reference.m_Text ) );
return false;
} }
else else
{ {
if( fprintf( aFile, " ~" ) < 0 ) aFormatter.Print( 0, " ~" );
return false;
} }
if( fprintf( aFile, " %d %d %c %c %d %c %c\n", aFormatter.Print( 0, " %d %d %c %c %d %c %c\n",
0, m_pinNameOffset, 0, m_pinNameOffset,
m_showPinNumbers ? 'Y' : 'N', m_showPinNumbers ? 'Y' : 'N',
m_showPinNames ? 'Y' : 'N', m_showPinNames ? 'Y' : 'N',
m_unitCount, m_unitsLocked ? 'L' : 'F', m_unitCount, m_unitsLocked ? 'L' : 'F',
m_options == ENTRY_POWER ? 'P' : 'N' ) < 0 ) m_options == ENTRY_POWER ? 'P' : 'N' );
return false;
if( !SaveDateAndTime( aFile ) ) if( !SaveDateAndTime( aFormatter ) )
return false; return false;
LIB_FIELDS fields; LIB_FIELDS fields;
...@@ -584,7 +577,7 @@ bool LIB_COMPONENT::Save( FILE* aFile ) ...@@ -584,7 +577,7 @@ bool LIB_COMPONENT::Save( FILE* aFile )
{ {
if( !fields[i].m_Text.IsEmpty() ) if( !fields[i].m_Text.IsEmpty() )
{ {
if( !fields[i].Save( aFile ) ) if( !fields[i].Save( aFormatter ) )
return false; return false;
} }
} }
...@@ -603,7 +596,7 @@ bool LIB_COMPONENT::Save( FILE* aFile ) ...@@ -603,7 +596,7 @@ bool LIB_COMPONENT::Save( FILE* aFile )
{ {
fields[i].SetId( fieldId++ ); fields[i].SetId( fieldId++ );
if( !fields[i].Save( aFile ) ) if( !fields[i].Save( aFormatter ) )
return false; return false;
} }
} }
...@@ -613,33 +606,27 @@ bool LIB_COMPONENT::Save( FILE* aFile ) ...@@ -613,33 +606,27 @@ bool LIB_COMPONENT::Save( FILE* aFile )
// alias does not get added to the alias list. // alias does not get added to the alias list.
if( m_aliases.size() > 1 ) if( m_aliases.size() > 1 )
{ {
if( fprintf( aFile, "ALIAS" ) < 0 ) aFormatter.Print( 0, "ALIAS" );
return false;
for( i = 1; i < m_aliases.size(); i++ ) for( i = 1; i < m_aliases.size(); i++ )
{ {
if( fprintf( aFile, " %s", TO_UTF8( m_aliases[i]->GetName() ) ) < 0 ) aFormatter.Print( 0, " %s", TO_UTF8( m_aliases[i]->GetName() ) );
return false;
} }
if( fprintf( aFile, "\n" ) < 0 ) aFormatter.Print( 0, "\n" );
return false;
} }
/* Write the footprint filter list */ /* Write the footprint filter list */
if( m_FootprintList.GetCount() != 0 ) if( m_FootprintList.GetCount() != 0 )
{ {
if( fprintf( aFile, "$FPLIST\n" ) < 0 ) aFormatter.Print( 0, "$FPLIST\n" );
return false;
for( i = 0; i < m_FootprintList.GetCount(); i++ ) for( i = 0; i < m_FootprintList.GetCount(); i++ )
{ {
if( fprintf( aFile, " %s\n", TO_UTF8( m_FootprintList[i] ) ) < 0 ) aFormatter.Print( 0, " %s\n", TO_UTF8( m_FootprintList[i] ) );
return false;
} }
if( fprintf( aFile, "$ENDFPLIST\n" ) < 0 ) aFormatter.Print( 0, "$ENDFPLIST\n" );
return false;
} }
/* Save graphics items (including pins) */ /* Save graphics items (including pins) */
...@@ -649,24 +636,21 @@ bool LIB_COMPONENT::Save( FILE* aFile ) ...@@ -649,24 +636,21 @@ bool LIB_COMPONENT::Save( FILE* aFile )
* when a file editing "by hand" is made */ * when a file editing "by hand" is made */
drawings.sort(); drawings.sort();
if( fprintf( aFile, "DRAW\n" ) < 0 ) aFormatter.Print( 0, "DRAW\n" );
return false;
BOOST_FOREACH( LIB_ITEM& item, drawings ) BOOST_FOREACH( LIB_ITEM& item, drawings )
{ {
if( item.Type() == LIB_FIELD_T ) if( item.Type() == LIB_FIELD_T )
continue; continue;
if( !item.Save( aFile ) ) if( !item.Save( aFormatter ) )
return false; return false;
} }
if( fprintf( aFile, "ENDDRAW\n" ) < 0 ) aFormatter.Print( 0, "ENDDRAW\n" );
return false;
} }
if( fprintf( aFile, "ENDDEF\n" ) < 0 ) aFormatter.Print( 0, "ENDDEF\n" );
return false;
return true; return true;
} }
...@@ -1143,7 +1127,7 @@ LIB_FIELD& LIB_COMPONENT::GetReferenceField() ...@@ -1143,7 +1127,7 @@ LIB_FIELD& LIB_COMPONENT::GetReferenceField()
} }
bool LIB_COMPONENT::SaveDateAndTime( FILE* aFile ) bool LIB_COMPONENT::SaveDateAndTime( OUTPUTFORMATTER& aFormatter )
{ {
int year, mon, day, hour, min, sec; int year, mon, day, hour, min, sec;
...@@ -1157,12 +1141,12 @@ bool LIB_COMPONENT::SaveDateAndTime( FILE* aFile ) ...@@ -1157,12 +1141,12 @@ bool LIB_COMPONENT::SaveDateAndTime( FILE* aFile )
mon = ( m_dateModified >> 22 ) & 15; mon = ( m_dateModified >> 22 ) & 15;
year = ( m_dateModified >> 26 ) + 1990; year = ( m_dateModified >> 26 ) + 1990;
if ( fprintf( aFile, "Ti %d/%d/%d %d:%d:%d\n", year, mon, day, hour, min, sec ) < 0 ) aFormatter.Print( 0, "Ti %d/%d/%d %d:%d:%d\n", year, mon, day, hour, min, sec );
return false;
return true; return true;
} }
bool LIB_COMPONENT::LoadDateAndTime( char* aLine ) bool LIB_COMPONENT::LoadDateAndTime( char* aLine )
{ {
int year, mon, day, hour, min, sec; int year, mon, day, hour, min, sec;
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
class LINE_READER; class LINE_READER;
class OUTPUTFORMATTER;
class CMP_LIBRARY; class CMP_LIBRARY;
class LIB_ALIAS; class LIB_ALIAS;
class LIB_COMPONENT; class LIB_COMPONENT;
...@@ -290,20 +291,23 @@ public: ...@@ -290,20 +291,23 @@ public:
* write the date and time of component to \a aFile in the format: * write the date and time of component to \a aFile in the format:
* "Ti yy/mm/jj hh:mm:ss" * "Ti yy/mm/jj hh:mm:ss"
* *
* @param aFile A point to a FILE object containing the file to write to. * @param aFormatter A reference to an OUTPUT_FORMATER object containing the
* @return True if the date and time were successfully written to \a aFile. * output format to write to.
* @return True if the date and time were successfully written to \a aFormatter.
*/ */
bool SaveDateAndTime( FILE* aFile ); bool SaveDateAndTime( OUTPUTFORMATTER& aFormatter );
bool LoadDateAndTime( char* aLine ); bool LoadDateAndTime( char* aLine );
/** /**
* Write the data structures out to a FILE in "*.lib" format. * Function Save
* writes the data structures out to \a aFormatter in the component library "*.lib"
* format.
* *
* @param aFile - The FILE to write to. * @param aFormatter A reference to an OUTPUTFORMATTER to write to.
* @return True if success writing else false. * @return True if success writing else false.
*/ */
bool Save( FILE* aFile ); bool Save( OUTPUTFORMATTER& aFormatter );
/** /**
* Load component definition from \a aReader. * Load component definition from \a aReader.
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
#include <wx/tokenzr.h> #include <wx/tokenzr.h>
#include <wx/regex.h> #include <wx/regex.h>
#include <wx/wfstream.h>
static const wxChar* duplicate_name_msg = static const wxChar* duplicate_name_msg =
...@@ -69,6 +70,7 @@ bool operator<( const CMP_LIBRARY& aItem1, const CMP_LIBRARY& aItem2 ) ...@@ -69,6 +70,7 @@ bool operator<( const CMP_LIBRARY& aItem1, const CMP_LIBRARY& aItem2 )
/* The cache library always is sorted to the end of the library list. */ /* The cache library always is sorted to the end of the library list. */
if( aItem2.IsCache() ) if( aItem2.IsCache() )
return true; return true;
if( aItem1.IsCache() ) if( aItem1.IsCache() )
return false; return false;
...@@ -670,7 +672,6 @@ bool CMP_LIBRARY::LoadDocs( wxString& aErrorMsg ) ...@@ -670,7 +672,6 @@ bool CMP_LIBRARY::LoadDocs( wxString& aErrorMsg )
bool CMP_LIBRARY::Save( const wxString& aFullFileName, bool aOldDocFormat ) bool CMP_LIBRARY::Save( const wxString& aFullFileName, bool aOldDocFormat )
{ {
FILE* libfile;
wxString msg; wxString msg;
wxFileName libFileName = aFullFileName; wxFileName libFileName = aFullFileName;
wxFileName backupFileName = aFullFileName; wxFileName backupFileName = aFullFileName;
...@@ -690,9 +691,9 @@ bool CMP_LIBRARY::Save( const wxString& aFullFileName, bool aOldDocFormat ) ...@@ -690,9 +691,9 @@ bool CMP_LIBRARY::Save( const wxString& aFullFileName, bool aOldDocFormat )
} }
} }
libfile = wxFopen( libFileName.GetFullPath(), wxT( "wt" ) ); wxFFileOutputStream os( libFileName.GetFullPath(), wxT( "wt" ) );
if( libfile == NULL ) if( !os.IsOk() )
{ {
libFileName.MakeAbsolute(); libFileName.MakeAbsolute();
msg = wxT( "Failed to create component library file " ) + libFileName.GetFullPath(); msg = wxT( "Failed to create component library file " ) + libFileName.GetFullPath();
...@@ -700,31 +701,34 @@ bool CMP_LIBRARY::Save( const wxString& aFullFileName, bool aOldDocFormat ) ...@@ -700,31 +701,34 @@ bool CMP_LIBRARY::Save( const wxString& aFullFileName, bool aOldDocFormat )
return false; return false;
} }
isModified = false; STREAM_OUTPUTFORMATTER formatter( os );
timeStamp = GetTimeStamp();
if( !SaveHeader( libfile ) ) if( isModified )
{ {
fclose( libfile ); timeStamp = GetTimeStamp();
return false; isModified = false;
} }
bool success = true; bool success = true;
for( LIB_ALIAS_MAP::iterator it=aliases.begin(); it!=aliases.end(); it++ ) try
{ {
if( !(*it).second->IsRoot() ) SaveHeader( formatter );
continue;
if ( !(*it).second->GetComponent()->Save( libfile ) ) for( LIB_ALIAS_MAP::iterator it=aliases.begin(); it!=aliases.end(); it++ )
success = false; {
} if( !(*it).second->IsRoot() )
continue;
if( fprintf( libfile, "#\n#End Library\n" ) < 0 ) (*it).second->GetComponent()->Save( formatter );
success = false; }
fclose( libfile ); formatter.Print( 0, "#\n#End Library\n" );
}
catch( IO_ERROR ioe )
{
success = false;
}
if( USE_OLD_DOC_FILE_FORMAT( versionMajor, versionMinor ) && aOldDocFormat ) if( USE_OLD_DOC_FILE_FORMAT( versionMajor, versionMinor ) && aOldDocFormat )
success = SaveDocFile( aFullFileName ); success = SaveDocFile( aFullFileName );
...@@ -793,28 +797,25 @@ bool CMP_LIBRARY::SaveDocFile( const wxString& aFullFileName ) ...@@ -793,28 +797,25 @@ bool CMP_LIBRARY::SaveDocFile( const wxString& aFullFileName )
} }
bool CMP_LIBRARY::SaveHeader( FILE* aFile ) bool CMP_LIBRARY::SaveHeader( OUTPUTFORMATTER& aFormatter )
{ {
char BufLine[1024]; char BufLine[1024];
bool succes = true;
DateAndTime( BufLine ); DateAndTime( BufLine );
if( fprintf( aFile, "%s %d.%d Date: %s\n", LIBFILE_IDENT, aFormatter.Print( 0, "%s %d.%d Date: %s\n", LIBFILE_IDENT,
LIB_VERSION_MAJOR, LIB_VERSION_MINOR, BufLine ) < 0 ) LIB_VERSION_MAJOR, LIB_VERSION_MINOR, BufLine );
succes = false;
if( fprintf( aFile, "#encoding utf-8\n") < 0 ) aFormatter.Print( 0, "#encoding utf-8\n");
succes = false;
#if 0 #if 0
if( ( fprintf( aFile, "$HEADER\n" ) < 0 ) aFormatter.Print( 0, "$HEADER\n" );
|| ( fprintf( aFile, "TimeStamp %8.8lX\n", m_TimeStamp ) < 0 ) aFormatter.Print( 0, "TimeStamp %8.8lX\n", m_TimeStamp );
|| ( fprintf( aFile, "Parts %d\n", aliases.size() ) != 2 ) aFormatter.Print( 0, "Parts %d\n", aliases.size() );
|| ( fprintf( aFile, "$ENDHEADER\n" ) != 1 ) ) aFormatter.Print( 0, "$ENDHEADER\n" ) != 1 );
succes = false;
#endif #endif
return succes;
return true;
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
class LINE_READER; class LINE_READER;
class OUTPUTFORMATTER;
/* /*
...@@ -130,7 +131,7 @@ public: ...@@ -130,7 +131,7 @@ public:
bool LoadDocs( wxString& aErrorMsg ); bool LoadDocs( wxString& aErrorMsg );
private: private:
bool SaveHeader( FILE* aFile ); bool SaveHeader( OUTPUTFORMATTER& aFormatter );
bool LoadHeader( LINE_READER& aLineReader ); bool LoadHeader( LINE_READER& aLineReader );
void LoadAliases( LIB_COMPONENT* aComponent ); void LoadAliases( LIB_COMPONENT* aComponent );
......
...@@ -105,7 +105,7 @@ LIB_ARC::LIB_ARC( const LIB_ARC& aArc ) : LIB_ITEM( aArc ) ...@@ -105,7 +105,7 @@ LIB_ARC::LIB_ARC( const LIB_ARC& aArc ) : LIB_ITEM( aArc )
} }
bool LIB_ARC::Save( FILE* aFile ) bool LIB_ARC::Save( OUTPUTFORMATTER& aFormatter )
{ {
int x1 = m_t1; int x1 = m_t1;
...@@ -117,11 +117,10 @@ bool LIB_ARC::Save( FILE* aFile ) ...@@ -117,11 +117,10 @@ bool LIB_ARC::Save( FILE* aFile )
if( x2 > 1800 ) if( x2 > 1800 )
x2 -= 3600; x2 -= 3600;
if( fprintf( aFile, "A %d %d %d %d %d %d %d %d %c %d %d %d %d\n", aFormatter.Print( 0, "A %d %d %d %d %d %d %d %d %c %d %d %d %d\n",
m_Pos.x, m_Pos.y, m_Radius, x1, x2, m_Unit, m_Convert, m_Width, m_Pos.x, m_Pos.y, m_Radius, x1, x2, m_Unit, m_Convert, m_Width,
fill_tab[m_Fill], m_ArcStart.x, m_ArcStart.y, m_ArcEnd.x, fill_tab[m_Fill], m_ArcStart.x, m_ArcStart.y, m_ArcEnd.x,
m_ArcEnd.y ) < 0 ) m_ArcEnd.y );
return false;
return true; return true;
} }
......
...@@ -30,12 +30,10 @@ ...@@ -30,12 +30,10 @@
#ifndef _LIB_ARC_H_ #ifndef _LIB_ARC_H_
#define _LIB_ARC_H_ #define _LIB_ARC_H_
#include "lib_draw_item.h" #include "lib_draw_item.h"
class TRANSFORM; class TRANSFORM;
class LINE_READER;
class LIB_ARC : public LIB_ITEM class LIB_ARC : public LIB_ITEM
...@@ -99,10 +97,11 @@ public: ...@@ -99,10 +97,11 @@ public:
/** /**
* Save arc object to a FILE in "*.lib" format. * Save arc object to a FILE in "*.lib" format.
* *
* @param aFile The FILE to write to. * @param aFormatter A reference to an OUTPUTFORMATTER to write the component library
* @return - True if success writing else false. * arc to.
* @return True if success writing else false.
*/ */
virtual bool Save( FILE* aFile ); virtual bool Save( OUTPUTFORMATTER& aFormatter );
virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg ); virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg );
......
...@@ -62,21 +62,18 @@ LIB_BEZIER::LIB_BEZIER( const LIB_BEZIER& aBezier ) : LIB_ITEM( aBezier ) ...@@ -62,21 +62,18 @@ LIB_BEZIER::LIB_BEZIER( const LIB_BEZIER& aBezier ) : LIB_ITEM( aBezier )
} }
bool LIB_BEZIER::Save( FILE* aFile ) bool LIB_BEZIER::Save( OUTPUTFORMATTER& aFormatter )
{ {
int ccount = GetCornerCount(); int ccount = GetCornerCount();
if( fprintf( aFile, "B %d %d %d %d", ccount, m_Unit, m_Convert, m_Width ) < 0 ) aFormatter.Print( 0, "B %d %d %d %d", ccount, m_Unit, m_Convert, m_Width );
return false;
for( unsigned i = 0; i < GetCornerCount(); i++ ) for( unsigned i = 0; i < GetCornerCount(); i++ )
{ {
if( fprintf( aFile, " %d %d", m_BezierPoints[i].x, m_BezierPoints[i].y ) < 0 ) aFormatter.Print( 0, " %d %d", m_BezierPoints[i].x, m_BezierPoints[i].y );
return false;
} }
if( fprintf( aFile, " %c\n", fill_tab[m_Fill] ) < 0 ) aFormatter.Print( 0, " %c\n", fill_tab[m_Fill] );
return false;
return true; return true;
} }
......
...@@ -33,12 +33,10 @@ ...@@ -33,12 +33,10 @@
#include "lib_draw_item.h" #include "lib_draw_item.h"
class LINE_READER; /**
* Class LIB_BEZIER
* defines bezier curve graphic body item.
/**************************************************/ */
/* Graphic Body Item: Bezier Curve (set of lines) */
/**************************************************/
class LIB_BEZIER : public LIB_ITEM class LIB_BEZIER : public LIB_ITEM
{ {
int m_Width; // Line width int m_Width; // Line width
...@@ -65,10 +63,11 @@ public: ...@@ -65,10 +63,11 @@ public:
/** /**
* Write bezier curve object out to a FILE in "*.lib" format. * Write bezier curve object out to a FILE in "*.lib" format.
* *
* @param aFile - The FILE to write to. * @param aFormatter A reference to an OUTPUTFORMATTER to write the component library
* @return true if success writing else false. * bezier curve to.
* @return True if success writing else false.
*/ */
virtual bool Save( FILE* aFile ); virtual bool Save( OUTPUTFORMATTER& aFormatter );
virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg ); virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg );
......
...@@ -64,11 +64,10 @@ LIB_CIRCLE::LIB_CIRCLE( const LIB_CIRCLE& aCircle ) : ...@@ -64,11 +64,10 @@ LIB_CIRCLE::LIB_CIRCLE( const LIB_CIRCLE& aCircle ) :
} }
bool LIB_CIRCLE::Save( FILE* aFile ) bool LIB_CIRCLE::Save( OUTPUTFORMATTER& aFormatter )
{ {
if( fprintf( aFile, "C %d %d %d %d %d %d %c\n", m_Pos.x, m_Pos.y, aFormatter.Print( 0, "C %d %d %d %d %d %d %c\n", m_Pos.x, m_Pos.y,
m_Radius, m_Unit, m_Convert, m_Width, fill_tab[m_Fill] ) < 0 ) m_Radius, m_Unit, m_Convert, m_Width, fill_tab[m_Fill] );
return false;
return true; return true;
} }
......
...@@ -33,9 +33,6 @@ ...@@ -33,9 +33,6 @@
#include "lib_draw_item.h" #include "lib_draw_item.h"
class LINE_READER;
class LIB_CIRCLE : public LIB_ITEM class LIB_CIRCLE : public LIB_ITEM
{ {
int m_Radius; int m_Radius;
...@@ -69,10 +66,11 @@ public: ...@@ -69,10 +66,11 @@ public:
/** /**
* Write circle object to a FILE in "*.lib" format. * Write circle object to a FILE in "*.lib" format.
* *
* @param aFile - The FILE to write to. * @param aFormatter A reference to an OUTPUTFORMATTER to write the component library
* @return - true if success writing else false. * circle to.
* @return True if success writing else false.
*/ */
virtual bool Save( FILE* aFile ); virtual bool Save( OUTPUTFORMATTER& aFormatter );
virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg ); virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg );
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
class LINE_READER; class LINE_READER;
class OUTPUTFORMATTER;
class LIB_COMPONENT; class LIB_COMPONENT;
class PLOTTER; class PLOTTER;
class LIB_ITEM; class LIB_ITEM;
...@@ -213,12 +214,15 @@ public: ...@@ -213,12 +214,15 @@ public:
virtual int GetPenSize() const = 0; virtual int GetPenSize() const = 0;
/** /**
* Write draw item object to \a aFile in "*.lib" format. * Function Save
* writes draw item object to \a aFormatter in component library "*.lib" format.
* *
* @param aFile - The file to write to. * @param aFormatter A referenct to an #OUTPUTFORMATTER object to write the
* @return - true if success writing else false. * component library item to.
* @return True if success writing else false.
*/ */
virtual bool Save( FILE* aFile ) = 0; virtual bool Save( OUTPUTFORMATTER& aFormatter ) = 0;
virtual bool Load( LINE_READER& aLine, wxString& aErrorMsg ) = 0; virtual bool Load( LINE_READER& aLine, wxString& aErrorMsg ) = 0;
LIB_COMPONENT* GetParent() LIB_COMPONENT* GetParent()
......
...@@ -97,7 +97,7 @@ void LIB_FIELD::Init( int id ) ...@@ -97,7 +97,7 @@ void LIB_FIELD::Init( int id )
} }
bool LIB_FIELD::Save( FILE* ExportFile ) bool LIB_FIELD::Save( OUTPUTFORMATTER& aFormatter )
{ {
int hjustify, vjustify; int hjustify, vjustify;
wxString text = m_Text; wxString text = m_Text;
...@@ -119,16 +119,15 @@ bool LIB_FIELD::Save( FILE* ExportFile ) ...@@ -119,16 +119,15 @@ bool LIB_FIELD::Save( FILE* ExportFile )
if( text.IsEmpty() ) if( text.IsEmpty() )
text = wxT( "~" ); text = wxT( "~" );
if( fprintf( ExportFile, "F%d %s %d %d %d %c %c %c %c%c%c", aFormatter.Print( 0, "F%d %s %d %d %d %c %c %c %c%c%c",
m_id, m_id,
EscapedUTF8( text ).c_str(), // wraps in quotes EscapedUTF8( text ).c_str(), // wraps in quotes
m_Pos.x, m_Pos.y, m_Size.x, m_Pos.x, m_Pos.y, m_Size.x,
m_Orient == 0 ? 'H' : 'V', m_Orient == 0 ? 'H' : 'V',
(m_Attributs & TEXT_NO_VISIBLE ) ? 'I' : 'V', (m_Attributs & TEXT_NO_VISIBLE ) ? 'I' : 'V',
hjustify, vjustify, hjustify, vjustify,
m_Italic ? 'I' : 'N', m_Italic ? 'I' : 'N',
m_Bold ? 'B' : 'N' ) < 0 ) m_Bold ? 'B' : 'N' );
return false;
/* Save field name, if necessary /* Save field name, if necessary
* Field name is saved only if it is not the default name. * Field name is saved only if it is not the default name.
...@@ -137,14 +136,10 @@ bool LIB_FIELD::Save( FILE* ExportFile ) ...@@ -137,14 +136,10 @@ bool LIB_FIELD::Save( FILE* ExportFile )
*/ */
wxString defName = TEMPLATE_FIELDNAME::GetDefaultFieldName( m_id ); wxString defName = TEMPLATE_FIELDNAME::GetDefaultFieldName( m_id );
if( m_id >= FIELD1 && !m_name.IsEmpty() && m_name != defName if( m_id >= FIELD1 && !m_name.IsEmpty() && m_name != defName )
&& fprintf( ExportFile, " %s", EscapedUTF8( m_name ).c_str() ) < 0 ) aFormatter.Print( 0, " %s", EscapedUTF8( m_name ).c_str() );
{
return false;
}
if( fprintf( ExportFile, "\n" ) < 0 ) aFormatter.Print( 0, "\n" );
return false;
return true; return true;
} }
......
...@@ -137,10 +137,11 @@ public: ...@@ -137,10 +137,11 @@ public:
/** /**
* Writes field object out to a FILE in "*.lib" format. * Writes field object out to a FILE in "*.lib" format.
* *
* @param aFile The FILE to write to. * @param aFormatter A reference to an OUTPUTFORMATTER to write the component library
* field to.
* @return True if success writing else false. * @return True if success writing else false.
*/ */
virtual bool Save( FILE* aFile ); virtual bool Save( OUTPUTFORMATTER& aFormatter );
virtual bool Load( LINE_READER& aLineReader, wxString& errorMsg ); virtual bool Load( LINE_READER& aLineReader, wxString& errorMsg );
......
...@@ -568,7 +568,7 @@ bool LIB_PIN::HitTest( wxPoint aPosition, int aThreshold, const TRANSFORM& aTran ...@@ -568,7 +568,7 @@ bool LIB_PIN::HitTest( wxPoint aPosition, int aThreshold, const TRANSFORM& aTran
} }
bool LIB_PIN::Save( FILE* ExportFile ) bool LIB_PIN::Save( OUTPUTFORMATTER& aFormatter )
{ {
wxString StringPinNum; wxString StringPinNum;
int Etype; int Etype;
...@@ -628,48 +628,49 @@ bool LIB_PIN::Save( FILE* ExportFile ) ...@@ -628,48 +628,49 @@ bool LIB_PIN::Save( FILE* ExportFile )
if( !m_name.IsEmpty() ) if( !m_name.IsEmpty() )
{ {
if( fprintf( ExportFile, "X %s", TO_UTF8( m_name ) ) < 0 ) if( aFormatter.Print( 0, "X %s", TO_UTF8( m_name ) ) < 0 )
return false; return false;
} }
else else
{ {
if( fprintf( ExportFile, "X ~" ) < 0 ) if( aFormatter.Print( 0, "X ~" ) < 0 )
return false; return false;
} }
if( fprintf( ExportFile, " %s %d %d %d %c %d %d %d %d %c", if( aFormatter.Print( 0, " %s %d %d %d %c %d %d %d %d %c",
TO_UTF8( StringPinNum ), m_position.x, m_position.y, TO_UTF8( StringPinNum ), m_position.x, m_position.y,
(int) m_length, (int) m_orientation, m_PinNumSize, m_PinNameSize, (int) m_length, (int) m_orientation, m_PinNumSize, m_PinNameSize,
m_Unit, m_Convert, Etype ) < 0 ) m_Unit, m_Convert, Etype ) < 0 )
return false; return false;
if( m_shape || !IsVisible() ) if( m_shape || !IsVisible() )
{ {
if( fprintf( ExportFile, " " ) < 0 ) if( aFormatter.Print( 0, " " ) < 0 )
return false; return false;
} }
if( !IsVisible() && fprintf( ExportFile, "N" ) < 0 )
if( !IsVisible() && aFormatter.Print( 0, "N" ) < 0 )
return false; return false;
if( m_shape & INVERT
&& fprintf( ExportFile, "I" ) < 0 ) if( m_shape & INVERT && aFormatter.Print( 0, "I" ) < 0 )
return false; return false;
if( m_shape & CLOCK
&& fprintf( ExportFile, "C" ) < 0 ) if( m_shape & CLOCK && aFormatter.Print( 0, "C" ) < 0 )
return false; return false;
if( m_shape & LOWLEVEL_IN
&& fprintf( ExportFile, "L" ) < 0 ) if( m_shape & LOWLEVEL_IN && aFormatter.Print( 0, "L" ) < 0 )
return false; return false;
if( m_shape & LOWLEVEL_OUT
&& fprintf( ExportFile, "V" ) < 0 ) if( m_shape & LOWLEVEL_OUT && aFormatter.Print( 0, "V" ) < 0 )
return false; return false;
if( m_shape & CLOCK_FALL
&& fprintf( ExportFile, "F" ) < 0 ) if( m_shape & CLOCK_FALL && aFormatter.Print( 0, "F" ) < 0 )
return false; return false;
if( m_shape & NONLOGIC
&& fprintf( ExportFile, "X" ) < 0 ) if( m_shape & NONLOGIC && aFormatter.Print( 0, "X" ) < 0 )
return false; return false;
if( fprintf( ExportFile, "\n" ) < 0 ) if( aFormatter.Print( 0, "\n" ) < 0 )
return false; return false;
m_Flags &= ~IS_CHANGED; m_Flags &= ~IS_CHANGED;
......
...@@ -32,9 +32,6 @@ ...@@ -32,9 +32,6 @@
#include "lib_draw_item.h" #include "lib_draw_item.h"
class SCH_COMPONENT;
class LINE_READER;
#define TARGET_PIN_RADIUS 12 /* Circle diameter drawn at the active end of pins */ #define TARGET_PIN_RADIUS 12 /* Circle diameter drawn at the active end of pins */
...@@ -153,10 +150,11 @@ public: ...@@ -153,10 +150,11 @@ public:
/** /**
* Write pin object to a FILE in "*.lib" format. * Write pin object to a FILE in "*.lib" format.
* *
* @param aFile The FILE to write to. * @param aFormatter A reference to an OUTPUTFORMATTER to write the component library
* @return - true if success writing else false. * pin to.
* @return True if success writing else false.
*/ */
virtual bool Save( FILE* aFile ); virtual bool Save( OUTPUTFORMATTER& aFormatter );
virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg ); virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg );
......
...@@ -62,21 +62,18 @@ LIB_POLYLINE::LIB_POLYLINE( const LIB_POLYLINE& polyline ) : ...@@ -62,21 +62,18 @@ LIB_POLYLINE::LIB_POLYLINE( const LIB_POLYLINE& polyline ) :
} }
bool LIB_POLYLINE::Save( FILE* aFile ) bool LIB_POLYLINE::Save( OUTPUTFORMATTER& aFormatter )
{ {
int ccount = GetCornerCount(); int ccount = GetCornerCount();
if( fprintf( aFile, "P %d %d %d %d", ccount, m_Unit, m_Convert, m_Width ) < 0 ) aFormatter.Print( 0, "P %d %d %d %d", ccount, m_Unit, m_Convert, m_Width );
return false;
for( unsigned i = 0; i < GetCornerCount(); i++ ) for( unsigned i = 0; i < GetCornerCount(); i++ )
{ {
if( fprintf( aFile, " %d %d", m_PolyPoints[i].x, m_PolyPoints[i].y ) < 0 ) aFormatter.Print( 0, " %d %d", m_PolyPoints[i].x, m_PolyPoints[i].y );
return false;
} }
if( fprintf( aFile, " %c\n", fill_tab[m_Fill] ) < 0 ) aFormatter.Print( 0, " %c\n", fill_tab[m_Fill] );
return false;
return true; return true;
} }
......
...@@ -32,8 +32,6 @@ ...@@ -32,8 +32,6 @@
#include "lib_draw_item.h" #include "lib_draw_item.h"
class LINE_READER;
class LIB_POLYLINE : public LIB_ITEM class LIB_POLYLINE : public LIB_ITEM
{ {
...@@ -69,10 +67,11 @@ public: ...@@ -69,10 +67,11 @@ public:
/** /**
* Write polyline object out to a FILE in "*.lib" format. * Write polyline object out to a FILE in "*.lib" format.
* *
* @param aFile - The FILE to write to. * @param aFormatter A reference to an OUTPUTFORMATTER to write the component library
* @return - true if success writing else false. * polyline to.
* @return True if success writing else false.
*/ */
virtual bool Save( FILE* aFile ); virtual bool Save( OUTPUTFORMATTER& aFormatter );
virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg ); virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg );
......
...@@ -65,11 +65,10 @@ LIB_RECTANGLE::LIB_RECTANGLE( const LIB_RECTANGLE& aRect ) : ...@@ -65,11 +65,10 @@ LIB_RECTANGLE::LIB_RECTANGLE( const LIB_RECTANGLE& aRect ) :
} }
bool LIB_RECTANGLE::Save( FILE* aFile ) bool LIB_RECTANGLE::Save( OUTPUTFORMATTER& aFormatter )
{ {
if( fprintf( aFile, "S %d %d %d %d %d %d %d %c\n", m_Pos.x, m_Pos.y, aFormatter.Print( 0, "S %d %d %d %d %d %d %d %c\n", m_Pos.x, m_Pos.y,
m_End.x, m_End.y, m_Unit, m_Convert, m_Width, fill_tab[m_Fill] ) < 0 ) m_End.x, m_End.y, m_Unit, m_Convert, m_Width, fill_tab[m_Fill] );
return false;
return true; return true;
} }
......
...@@ -33,9 +33,6 @@ ...@@ -33,9 +33,6 @@
#include "lib_draw_item.h" #include "lib_draw_item.h"
class LINE_READER;
class LIB_RECTANGLE : public LIB_ITEM class LIB_RECTANGLE : public LIB_ITEM
{ {
wxPoint m_End; // Rectangle end point. wxPoint m_End; // Rectangle end point.
...@@ -74,10 +71,11 @@ public: ...@@ -74,10 +71,11 @@ public:
/** /**
* Write rectangle object out to a FILE in "*.lib" format. * Write rectangle object out to a FILE in "*.lib" format.
* *
* @param aFile - The FILE to write to. * @param aFormatter A reference to an OUTPUTFORMATTER to write the component library
* @return - true if success writing else false. * rectangle to.
* @return True if success writing else false.
*/ */
virtual bool Save( FILE* aFile ); virtual bool Save( OUTPUTFORMATTER& aFormatter );
virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg ); virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg );
......
...@@ -55,7 +55,7 @@ LIB_TEXT::LIB_TEXT(LIB_COMPONENT * aParent) : ...@@ -55,7 +55,7 @@ LIB_TEXT::LIB_TEXT(LIB_COMPONENT * aParent) :
} }
bool LIB_TEXT::Save( FILE* ExportFile ) bool LIB_TEXT::Save( OUTPUTFORMATTER& aFormatter )
{ {
wxString text = m_Text; wxString text = m_Text;
...@@ -72,13 +72,10 @@ bool LIB_TEXT::Save( FILE* ExportFile ) ...@@ -72,13 +72,10 @@ bool LIB_TEXT::Save( FILE* ExportFile )
text.Replace( wxT( " " ), wxT( "~" ) ); text.Replace( wxT( " " ), wxT( "~" ) );
} }
if( fprintf( ExportFile, "T %d %d %d %d %d %d %d %s ", m_Orient, m_Pos.x, m_Pos.y, aFormatter.Print( 0, "T %d %d %d %d %d %d %d %s ", m_Orient, m_Pos.x, m_Pos.y,
m_Size.x, m_Attributs, m_Unit, m_Convert, TO_UTF8( text ) ) < 0 ) m_Size.x, m_Attributs, m_Unit, m_Convert, TO_UTF8( text ) );
return false;
if( fprintf( ExportFile, " %s %d", m_Italic ? "Italic" : "Normal", aFormatter.Print( 0, " %s %d", m_Italic ? "Italic" : "Normal", ( m_Bold > 0 ) ? 1 : 0 );
( m_Bold > 0 ) ? 1 : 0 ) < 0 )
return false;
char hjustify = 'C'; char hjustify = 'C';
...@@ -94,8 +91,7 @@ bool LIB_TEXT::Save( FILE* ExportFile ) ...@@ -94,8 +91,7 @@ bool LIB_TEXT::Save( FILE* ExportFile )
else if( m_VJustify == GR_TEXT_VJUSTIFY_TOP ) else if( m_VJustify == GR_TEXT_VJUSTIFY_TOP )
vjustify = 'T'; vjustify = 'T';
if( fprintf( ExportFile, " %c %c\n", hjustify, vjustify ) < 0 ) aFormatter.Print( 0, " %c %c\n", hjustify, vjustify );
return false;
return true; return true;
} }
......
...@@ -33,15 +33,14 @@ ...@@ -33,15 +33,14 @@
#include "lib_draw_item.h" #include "lib_draw_item.h"
class LINE_READER; /**
* Class LIB_TEXT
* defines a component library graphical text item.
/*********************************************/ * <p>
/* Graphic Body Item: Text */ * This is only a graphical text item. Field text like the reference designator,
/* This is only a graphic text. */ * component value, etc. are not LIB_TEXT items. See the #LIB_FIELD class for the
/* Fields like Ref , value... are not Text, */ * field item definition.
/* they are a separate class */ */
/*********************************************/
class LIB_TEXT : public LIB_ITEM, public EDA_TEXT class LIB_TEXT : public LIB_ITEM, public EDA_TEXT
{ {
wxString m_savedText; ///< Temporary storage for the string when edition. wxString m_savedText; ///< Temporary storage for the string when edition.
...@@ -86,10 +85,11 @@ public: ...@@ -86,10 +85,11 @@ public:
/** /**
* Write text object out to a FILE in "*.lib" format. * Write text object out to a FILE in "*.lib" format.
* *
* @param aFile - The FILE to write to. * @param aFormatter A reference to an OUTPUTFORMATTER to write the component library
* @return - true if success writing else false. * text to.
* @return True if success writing else false.
*/ */
virtual bool Save( FILE* aFile ); virtual bool Save( OUTPUTFORMATTER& aFormatter );
virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg ); virtual bool Load( LINE_READER& aLineReader, wxString& aErrorMsg );
......
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
* Copyright (C) 2008-2011 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 2004-2011 KiCad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/** /**
* @file symbedit.cpp * @file symbedit.cpp
* @brief Functions to load from and save to file component libraries and symbols. * @brief Functions to load from and save to file component libraries and symbols.
...@@ -11,6 +36,7 @@ ...@@ -11,6 +36,7 @@
#include "kicad_string.h" #include "kicad_string.h"
#include "gestfich.h" #include "gestfich.h"
#include "class_sch_screen.h" #include "class_sch_screen.h"
#include "richio.h"
#include "general.h" #include "general.h"
#include "protos.h" #include "protos.h"
...@@ -18,7 +44,7 @@ ...@@ -18,7 +44,7 @@
#include "class_library.h" #include "class_library.h"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <wx/ffile.h> #include <wx/wfstream.h>
void LIB_EDIT_FRAME::LoadOneSymbol() void LIB_EDIT_FRAME::LoadOneSymbol()
...@@ -82,10 +108,13 @@ void LIB_EDIT_FRAME::LoadOneSymbol() ...@@ -82,10 +108,13 @@ void LIB_EDIT_FRAME::LoadOneSymbol()
{ {
if( item.Type() == LIB_FIELD_T ) if( item.Type() == LIB_FIELD_T )
continue; continue;
if( item.GetUnit() ) if( item.GetUnit() )
item.SetUnit( m_unit ); item.SetUnit( m_unit );
if( item.GetConvert() ) if( item.GetConvert() )
item.SetConvert( m_convert ); item.SetConvert( m_convert );
item.m_Flags = IS_NEW; item.m_Flags = IS_NEW;
item.m_Selected = IS_SELECTED; item.m_Selected = IS_SELECTED;
...@@ -129,12 +158,11 @@ void LIB_EDIT_FRAME::SaveOneSymbol() ...@@ -129,12 +158,11 @@ void LIB_EDIT_FRAME::SaveOneSymbol()
wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() ); wxGetApp().SaveLastVisitedLibraryPath( fn.GetPath() );
wxFFile file( fn.GetFullPath(), wxT( "wt" ) ); wxFile file( fn.GetFullPath(), wxFile::write );
if( !file.IsOpened() ) if( !file.IsOpened() )
{ {
msg.Printf( _( "Unable to create file <%s>" ), msg.Printf( _( "Unable to create file <%s>" ), GetChars( fn.GetFullPath() ) );
GetChars( fn.GetFullPath() ) );
DisplayError( this, msg ); DisplayError( this, msg );
return; return;
} }
...@@ -142,6 +170,8 @@ void LIB_EDIT_FRAME::SaveOneSymbol() ...@@ -142,6 +170,8 @@ void LIB_EDIT_FRAME::SaveOneSymbol()
msg.Printf( _( "Saving symbol in [%s]" ), GetChars( fn.GetPath() ) ); msg.Printf( _( "Saving symbol in [%s]" ), GetChars( fn.GetPath() ) );
SetStatusText( msg ); SetStatusText( msg );
wxFileOutputStream os( file );
STREAM_OUTPUTFORMATTER formatter( os );
wxString line; wxString line;
/* File header */ /* File header */
...@@ -172,32 +202,39 @@ void LIB_EDIT_FRAME::SaveOneSymbol() ...@@ -172,32 +202,39 @@ void LIB_EDIT_FRAME::SaveOneSymbol()
line << wxT( "1 0 N\n" ); line << wxT( "1 0 N\n" );
if( !file.Write( line ) try
|| !m_component->GetReferenceField().Save( file.fp() ) {
|| !m_component->GetValueField().Save( file.fp() ) formatter.Print( 0, TO_UTF8( line ) );
|| !file.Write( wxT( "DRAW\n" ) ) ) m_component->GetReferenceField().Save( formatter );
return; m_component->GetValueField().Save( formatter );
formatter.Print( 0, "DRAW\n" );
LIB_ITEMS& drawList = m_component->GetDrawItemList(); LIB_ITEMS& drawList = m_component->GetDrawItemList();
BOOST_FOREACH( LIB_ITEM& item, drawList ) BOOST_FOREACH( LIB_ITEM& item, drawList )
{ {
if( item.Type() == LIB_FIELD_T ) if( item.Type() == LIB_FIELD_T )
continue; continue;
/* Don't save unused parts or alternate body styles. */ /* Don't save unused parts or alternate body styles. */
if( m_unit && item.GetUnit() && ( item.GetUnit() != m_unit ) ) if( m_unit && item.GetUnit() && ( item.GetUnit() != m_unit ) )
continue; continue;
if( m_convert && item.GetConvert() && ( item.GetConvert() != m_convert ) ) if( m_convert && item.GetConvert() && ( item.GetConvert() != m_convert ) )
continue; continue;
if( !item.Save( file.fp() ) ) item.Save( formatter );
return; }
}
if( !file.Write( wxT( "ENDDRAW\n" ) ) || !file.Write( wxT( "ENDDEF\n" ) ) ) formatter.Print( 0, "ENDDRAW\n" );
return; 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 );
}
} }
......
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