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

Use output formatter to save component library files and objects.

parent fcb482df
Loading
Loading
Loading
Loading
+29 −45
Original line number Diff line number Diff line
@@ -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;
    LIB_FIELD&  value = GetValueField();

    /* First line: it s a comment (component name for readers) */
    if( fprintf( aFile, "#\n# %s\n#\n", TO_UTF8( value.m_Text ) ) < 0 )
        return false;
    aFormatter.Print( 0, "#\n# %s\n#\n", TO_UTF8( value.m_Text ) );

    /* Save data */
    if( fprintf( aFile, "DEF" ) < 0 )
        return false;
    aFormatter.Print( 0, "DEF" );

    if( value.IsVisible() )
    {
        if( fprintf( aFile, " %s", TO_UTF8( value.m_Text ) ) < 0 )
            return false;
        aFormatter.Print( 0, " %s", TO_UTF8( value.m_Text ) );
    }
    else
    {
        if( fprintf( aFile, " ~%s", TO_UTF8( value.m_Text ) ) < 0 )
            return false;
        aFormatter.Print( 0, " ~%s", TO_UTF8( value.m_Text ) );
    }

    LIB_FIELD& reference = GetReferenceField();

    if( !reference.m_Text.IsEmpty() )
    {
        if( fprintf( aFile, " %s", TO_UTF8( reference.m_Text ) ) < 0 )
            return false;
        aFormatter.Print( 0, " %s", TO_UTF8( reference.m_Text ) );
    }
    else
    {
        if( fprintf( aFile, " ~" ) < 0 )
            return false;
        aFormatter.Print( 0, " ~" );
    }

    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,
                      m_showPinNumbers ? 'Y' : 'N',
                      m_showPinNames ? 'Y' : 'N',
                      m_unitCount, m_unitsLocked ? 'L' : 'F',
                 m_options == ENTRY_POWER ? 'P' : 'N' ) < 0 )
        return false;
                      m_options == ENTRY_POWER ? 'P' : 'N' );

    if( !SaveDateAndTime( aFile ) )
    if( !SaveDateAndTime( aFormatter ) )
        return false;

    LIB_FIELDS fields;
@@ -584,7 +577,7 @@ bool LIB_COMPONENT::Save( FILE* aFile )
    {
        if( !fields[i].m_Text.IsEmpty() )
        {
            if( !fields[i].Save( aFile ) )
            if( !fields[i].Save( aFormatter ) )
                return false;
        }
    }
@@ -603,7 +596,7 @@ bool LIB_COMPONENT::Save( FILE* aFile )
        {
            fields[i].SetId( fieldId++ );

            if( !fields[i].Save( aFile ) )
            if( !fields[i].Save( aFormatter ) )
                return false;
        }
    }
@@ -613,33 +606,27 @@ bool LIB_COMPONENT::Save( FILE* aFile )
    // alias does not get added to the alias list.
    if( m_aliases.size() > 1 )
    {
        if( fprintf( aFile, "ALIAS" ) < 0 )
            return false;
        aFormatter.Print( 0, "ALIAS" );

        for( i = 1; i < m_aliases.size(); i++ )
        {
            if( fprintf( aFile, " %s", TO_UTF8( m_aliases[i]->GetName() ) ) < 0 )
                return false;
            aFormatter.Print( 0, " %s", TO_UTF8( m_aliases[i]->GetName() ) );
        }

        if( fprintf( aFile, "\n" ) < 0 )
            return false;
        aFormatter.Print( 0, "\n" );
    }

    /* Write the footprint filter list */
    if( m_FootprintList.GetCount() != 0 )
    {
        if( fprintf( aFile, "$FPLIST\n" ) < 0 )
            return false;
        aFormatter.Print( 0, "$FPLIST\n" );

        for( i = 0; i < m_FootprintList.GetCount(); i++ )
        {
            if( fprintf( aFile, " %s\n", TO_UTF8( m_FootprintList[i] ) ) < 0 )
                return false;
            aFormatter.Print( 0, " %s\n", TO_UTF8( m_FootprintList[i] ) );
        }

        if( fprintf( aFile, "$ENDFPLIST\n" ) < 0 )
            return false;
        aFormatter.Print( 0, "$ENDFPLIST\n" );
    }

    /* Save graphics items (including pins) */
@@ -649,24 +636,21 @@ bool LIB_COMPONENT::Save( FILE* aFile )
         *  when a file editing "by hand" is made */
        drawings.sort();

        if( fprintf( aFile, "DRAW\n" ) < 0 )
            return false;
        aFormatter.Print( 0, "DRAW\n" );

        BOOST_FOREACH( LIB_ITEM& item, drawings )
        {
            if( item.Type() == LIB_FIELD_T )
                continue;

            if( !item.Save( aFile ) )
            if( !item.Save( aFormatter ) )
                return false;
        }

        if( fprintf( aFile, "ENDDRAW\n" ) < 0 )
            return false;
        aFormatter.Print( 0, "ENDDRAW\n" );
    }

    if( fprintf( aFile, "ENDDEF\n" ) < 0 )
        return false;
    aFormatter.Print( 0, "ENDDEF\n" );

    return true;
}
@@ -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;

@@ -1157,12 +1141,12 @@ bool LIB_COMPONENT::SaveDateAndTime( FILE* aFile )
    mon  = ( m_dateModified >> 22 ) & 15;
    year = ( m_dateModified >> 26 ) + 1990;

    if ( fprintf( aFile, "Ti %d/%d/%d %d:%d:%d\n", year, mon, day, hour, min, sec ) < 0 )
        return false;
    aFormatter.Print( 0, "Ti %d/%d/%d %d:%d:%d\n", year, mon, day, hour, min, sec );

    return true;
}


bool LIB_COMPONENT::LoadDateAndTime( char* aLine )
{
    int   year, mon, day, hour, min, sec;
+10 −6
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@


class LINE_READER;
class OUTPUTFORMATTER;
class CMP_LIBRARY;
class LIB_ALIAS;
class LIB_COMPONENT;
@@ -290,20 +291,23 @@ public:
     * write the date and time of component to \a aFile in the format:
     * "Ti yy/mm/jj hh:mm:ss"
     *
     * @param aFile A point to a FILE object containing the file to write to.
     * @return True if the date and time were successfully written to \a aFile.
     * @param aFormatter A reference to an OUTPUT_FORMATER object containing the
     *                   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 );

    /**
     * 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.
     */
    bool Save( FILE* aFile );
    bool Save( OUTPUTFORMATTER& aFormatter );

    /**
     * Load component definition from \a aReader.
+32 −31
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@

#include <wx/tokenzr.h>
#include <wx/regex.h>
#include <wx/wfstream.h>


static const wxChar* duplicate_name_msg =
@@ -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. */
    if( aItem2.IsCache() )
        return true;

    if( aItem1.IsCache() )
        return false;

@@ -670,7 +672,6 @@ bool CMP_LIBRARY::LoadDocs( wxString& aErrorMsg )

bool CMP_LIBRARY::Save( const wxString& aFullFileName, bool aOldDocFormat )
{
    FILE* libfile;
    wxString msg;
    wxFileName libFileName = aFullFileName;
    wxFileName backupFileName = aFullFileName;
@@ -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();
        msg = wxT( "Failed to create component library file " ) + libFileName.GetFullPath();
@@ -700,31 +701,34 @@ bool CMP_LIBRARY::Save( const wxString& aFullFileName, bool aOldDocFormat )
        return false;
    }

    isModified = false;

    timeStamp = GetTimeStamp();
    STREAM_OUTPUTFORMATTER formatter( os );

    if( !SaveHeader( libfile ) )
    if( isModified )
    {
        fclose( libfile );
        return false;
        timeStamp = GetTimeStamp();
        isModified = false;
    }

    bool success = true;

    try
    {
        SaveHeader( formatter );

        for( LIB_ALIAS_MAP::iterator it=aliases.begin();  it!=aliases.end();  it++ )
        {
            if( !(*it).second->IsRoot() )
                continue;

        if ( !(*it).second->GetComponent()->Save( libfile ) )
            success = false;
            (*it).second->GetComponent()->Save( formatter );
        }

    if( fprintf( libfile, "#\n#End Library\n" ) < 0 )
        formatter.Print( 0, "#\n#End Library\n" );
    }
    catch( IO_ERROR ioe )
    {
        success = false;

    fclose( libfile );
    }

    if( USE_OLD_DOC_FILE_FORMAT( versionMajor, versionMinor ) && aOldDocFormat )
        success = SaveDocFile( 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];
    bool succes = true;

    DateAndTime( BufLine );

    if( fprintf( aFile, "%s %d.%d  Date: %s\n", LIBFILE_IDENT,
                 LIB_VERSION_MAJOR, LIB_VERSION_MINOR, BufLine ) < 0 )
        succes = false;
    aFormatter.Print( 0, "%s %d.%d  Date: %s\n", LIBFILE_IDENT,
                      LIB_VERSION_MAJOR, LIB_VERSION_MINOR, BufLine );

    if( fprintf( aFile, "#encoding utf-8\n") < 0 )
        succes = false;
    aFormatter.Print( 0, "#encoding utf-8\n");

#if 0
    if( ( fprintf( aFile, "$HEADER\n" ) < 0 )
        || ( fprintf( aFile, "TimeStamp %8.8lX\n", m_TimeStamp ) < 0 )
        || ( fprintf( aFile, "Parts %d\n", aliases.size() ) != 2 )
        || ( fprintf( aFile, "$ENDHEADER\n" ) != 1 ) )
        succes = false;
    aFormatter.Print( 0, "$HEADER\n" );
    aFormatter.Print( 0, "TimeStamp %8.8lX\n", m_TimeStamp );
    aFormatter.Print( 0, "Parts %d\n", aliases.size() );
    aFormatter.Print( 0, "$ENDHEADER\n" ) != 1 );
#endif
    return succes;

    return true;
}


+2 −1
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@


class LINE_READER;
class OUTPUTFORMATTER;


/*
@@ -130,7 +131,7 @@ public:
    bool LoadDocs( wxString& aErrorMsg );

private:
    bool SaveHeader( FILE* aFile );
    bool SaveHeader( OUTPUTFORMATTER& aFormatter );

    bool LoadHeader( LINE_READER& aLineReader );
    void LoadAliases( LIB_COMPONENT* aComponent );
+5 −6
Original line number Diff line number Diff line
@@ -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;

@@ -117,11 +117,10 @@ bool LIB_ARC::Save( FILE* aFile )
    if( x2 > 1800 )
        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,
                      fill_tab[m_Fill], m_ArcStart.x, m_ArcStart.y, m_ArcEnd.x,
                 m_ArcEnd.y ) < 0 )
        return false;
                      m_ArcEnd.y );

    return true;
}
Loading