Commit 7a5e6a2d authored by Wayne Stambaugh's avatar Wayne Stambaugh
Browse files

Remove message dialogs from COMPONENT_LIBRARY class.

* Move the component library save file creation and write error dialogs
  into the appropriate frame object.
* Change the save component library and document definitions take an
  OUTPUTFORMATTER object instead of a file name.
* Change the component alias save document definition function to take
  an OUTPUTFORMATTER object instead of a file handle.
parent 77521c4b
Loading
Loading
Loading
Loading
+15 −13
Original line number Diff line number Diff line
@@ -110,28 +110,30 @@ CMP_LIBRARY* LIB_ALIAS::GetLibrary()
}


bool LIB_ALIAS::SaveDoc( FILE* aFile )
bool LIB_ALIAS::SaveDoc( OUTPUTFORMATTER& aFormatter )
{
    if( description.IsEmpty() && keyWords.IsEmpty() && docFileName.IsEmpty() )
        return true;

    if( fprintf( aFile, "#\n$CMP %s\n", TO_UTF8( name ) ) < 0 )
        return false;
    try
    {
        aFormatter.Print( 0, "#\n$CMP %s\n", TO_UTF8( name ) );

    if( ! description.IsEmpty()
        && fprintf( aFile, "D %s\n", TO_UTF8( description ) ) < 0 )
        return false;
        if( !description.IsEmpty() )
            aFormatter.Print( 0, "D %s\n", TO_UTF8( description ) );

    if( ! keyWords.IsEmpty()
        && fprintf( aFile, "K %s\n", TO_UTF8( keyWords ) ) < 0 )
        return false;
        if( !keyWords.IsEmpty() )
            aFormatter.Print( 0, "K %s\n", TO_UTF8( keyWords ) );

    if( ! docFileName.IsEmpty()
        && fprintf( aFile, "F %s\n", TO_UTF8( docFileName ) ) < 0 )
        return false;
        if( !docFileName.IsEmpty() )
            aFormatter.Print( 0, "F %s\n", TO_UTF8( docFileName ) );

    if( fprintf( aFile, "$ENDCMP\n" ) < 0 )
        aFormatter.Print( 0, "$ENDCMP\n" );
    }
    catch( IO_ERROR ioe )
    {
        return false;
    }

    return true;
}
+5 −4
Original line number Diff line number Diff line
@@ -149,12 +149,13 @@ public:
    wxString GetDocFileName() const { return docFileName; }

    /**
     * Write the entry document information to a FILE in "*.dcm" format.
     * Function SaveDocs
     * rrite the entry document information to \a aFormatter in "*.dcm" format.
     *
     * @param aFile The FILE to write to.
     * @param aFormatter The #OUTPUTFORMATTER to write the alias documents to.
     * @return True if success writing else false.
     */
    bool SaveDoc( FILE* aFile );
    bool SaveDoc( OUTPUTFORMATTER& aFormatter );

    /**
     * Case insensitive comparison of the component entry name.
@@ -291,7 +292,7 @@ public:
     * write the date and time of component to \a aFile in the format:
     * "Ti yy/mm/jj hh:mm:ss"
     *
     * @param aFormatter A reference to an OUTPUT_FORMATER object containing the
     * @param aFormatter A reference to an #OUTPUTFORMATTER object containing the
     *                   output format to write to.
     * @return True if the date and time were successfully written to \a aFormatter.
     */
+14 −82
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@
#include "gr_basic.h"
#include "macros.h"
#include "kicad_string.h"
#include "confirm.h"
#include "gestfich.h"
#include "eda_doc.h"
#include "wxstruct.h"
@@ -670,39 +669,8 @@ bool CMP_LIBRARY::LoadDocs( wxString& aErrorMsg )
}


bool CMP_LIBRARY::Save( const wxString& aFullFileName, bool aOldDocFormat )
bool CMP_LIBRARY::Save( OUTPUTFORMATTER& aFormatter )
{
    wxString msg;
    wxFileName libFileName = aFullFileName;
    wxFileName backupFileName = aFullFileName;

    /* the old .lib file is renamed .bak */
    if( libFileName.FileExists() )
    {
        backupFileName.SetExt( wxT( "bak" ) );
        wxRemoveFile( backupFileName.GetFullPath() );

        if( !wxRenameFile( libFileName.GetFullPath(), backupFileName.GetFullPath() ) )
        {
            libFileName.MakeAbsolute();
            msg = wxT( "Failed to rename old component library file " ) +
                  backupFileName.GetFullPath();
            DisplayError( NULL, msg );
        }
    }

    wxFFileOutputStream os( libFileName.GetFullPath(), wxT( "wt" ) );

    if( !os.IsOk() )
    {
        libFileName.MakeAbsolute();
        msg = wxT( "Failed to create component library file " ) + libFileName.GetFullPath();
        DisplayError( NULL, msg );
        return false;
    }

    STREAM_OUTPUTFORMATTER formatter( os );

    if( isModified )
    {
        timeStamp = GetTimeStamp();
@@ -713,84 +681,48 @@ bool CMP_LIBRARY::Save( const wxString& aFullFileName, bool aOldDocFormat )

    try
    {
        SaveHeader( formatter );
        SaveHeader( aFormatter );

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

            (*it).second->GetComponent()->Save( formatter );
            (*it).second->GetComponent()->Save( aFormatter );
        }

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

    if( USE_OLD_DOC_FILE_FORMAT( versionMajor, versionMinor ) && aOldDocFormat )
        success = SaveDocFile( aFullFileName );

    return success;
}


bool CMP_LIBRARY::SaveDocFile( const wxString& aFullFileName )
{
    FILE* docfile;
    wxString msg;
    wxFileName backupFileName = aFullFileName;
    wxFileName docFileName = aFullFileName;

    docFileName.SetExt( DOC_EXT );

    /* Save current doc file as .bck */
    if( docFileName.FileExists() )
bool CMP_LIBRARY::SaveDocs( OUTPUTFORMATTER& aFormatter )
{
        backupFileName = docFileName;
        backupFileName.SetExt( wxT( "bck" ) );
        wxRemoveFile( backupFileName.GetFullPath() );
    bool success = true;

        if( !wxRenameFile( docFileName.GetFullPath(), backupFileName.GetFullPath() ) )
    try
    {
            msg = wxT( "Failed to save old library document file " ) +
                  backupFileName.GetFullPath();
            DisplayError( NULL, msg );
        }
    }

    docfile = wxFopen( docFileName.GetFullPath(), wxT( "wt" ) );
        aFormatter.Print( 0, "%s  Date: %s\n", DOCFILE_IDENT, TO_UTF8( DateAndTime() ) );

    if( docfile == NULL )
        for( LIB_ALIAS_MAP::iterator it=aliases.begin();  it!=aliases.end();  it++ )
        {
        docFileName.MakeAbsolute();
        msg = wxT( "Failed to create component document library file " ) +
              docFileName.GetFullPath();
        DisplayError( NULL, msg );
        return false;
            if ( !(*it).second->SaveDoc( aFormatter ) )
                success = false;
        }

    if( fprintf( docfile, "%s  Date: %s\n", DOCFILE_IDENT, TO_UTF8( DateAndTime() ) ) < 0 )
    {
        fclose( docfile );
        return false;
        aFormatter.Print( 0, "#\n#End Doc Library\n" );
    }

    bool success = true;

    for( LIB_ALIAS_MAP::iterator it=aliases.begin();  it!=aliases.end();  it++ )
    catch( IO_ERROR ioe )
    {
        if ( !(*it).second->SaveDoc( docfile ) )
        success = false;
    }

    if ( fprintf( docfile, "#\n#End Doc Library\n" ) < 0 )
        success = false;

    fclose( docfile );

    return success;
}

+34 −23
Original line number Diff line number Diff line
/*
 * 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 class_library.h
 * @brief Definition for component library class.
@@ -90,35 +115,21 @@ public:

    /**
     * Function Save
     * saves library to a file.
     * <p>
     * Prior to component library version 3.0, two files were created.  The
     * component objects are were as component library (*.lib) files.  The
     * library entry object document strings were save in library document
     * definition (*.dcm) files.  After version component library version 3.0,
     * the document string information is saved as part of the library file.
     * Saving separate document is maintained for backwards compatibility.
     * Please note that this behavior may change in the future.  If the
     * component library already exists, it is backup up in file *.bak.
     * writes library to \a aFormatter.
     *
     * @param aFullFileName - The library filename with path.
     * @param aOldDocFormat - Save the document information in a separate
     *                        file if true.  The default is to save as the
     *                        current library file format.
     * @return True if success writing else false.
     * @param aFormatter An #OUTPUTFORMATTER object to write the library to.
     * @return True if success writing to \a aFormatter.
     */
    bool Save( const wxString& aFullFileName, bool aOldDocFormat = false );
    bool Save( OUTPUTFORMATTER& aFormatter );

    /**
     * Save library document information to file.
     *
     * If the document definition file* already exists, it is backed up in
     * file *.bck.
     * Function SaveDocs
     * write the library document information to \a aFormatter.
     *
     * @param aFullFileName - The library filename with path.
     * @return True if success writing else false.
     * @param aFormatter An #OUTPUTFORMATTER object to write the library documentation to.
     * @return True if success writing to \a aFormatter.
     */
    bool SaveDocFile( const wxString& aFullFileName );
    bool SaveDocs( OUTPUTFORMATTER& aFormatter );

    /**
     * Load library from file.
+1 −1
Original line number Diff line number Diff line
@@ -379,7 +379,7 @@ void SCH_EDIT_FRAME::OnSaveProject( wxCommandEvent& aEvent )
    wxString cachename = fn.GetName() + wxT( "-cache" );
    fn.SetName( cachename );
    fn.SetExt( CompLibFileExtension );
    LibArchive( this, fn.GetFullPath() );
    CreateArchiveLibrary( fn.GetFullPath() );
}


Loading