diff --git a/eeschema/class_libentry.cpp b/eeschema/class_libentry.cpp index f51c64aa5f..8d6266e9fa 100644 --- a/eeschema/class_libentry.cpp +++ b/eeschema/class_libentry.cpp @@ -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; } diff --git a/eeschema/class_libentry.h b/eeschema/class_libentry.h index 674b820b1d..71ae5d17ef 100644 --- a/eeschema/class_libentry.h +++ b/eeschema/class_libentry.h @@ -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. */ diff --git a/eeschema/class_library.cpp b/eeschema/class_library.cpp index 75d7588e27..d95615c2ee 100644 --- a/eeschema/class_library.cpp +++ b/eeschema/class_library.cpp @@ -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,83 +681,47 @@ 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 ) +bool CMP_LIBRARY::SaveDocs( OUTPUTFORMATTER& aFormatter ) { - FILE* docfile; - wxString msg; - wxFileName backupFileName = aFullFileName; - wxFileName docFileName = aFullFileName; - - docFileName.SetExt( DOC_EXT ); - - /* Save current doc file as .bck */ - if( docFileName.FileExists() ) - { - backupFileName = docFileName; - backupFileName.SetExt( wxT( "bck" ) ); - wxRemoveFile( backupFileName.GetFullPath() ); - - if( !wxRenameFile( docFileName.GetFullPath(), backupFileName.GetFullPath() ) ) - { - msg = wxT( "Failed to save old library document file " ) + - backupFileName.GetFullPath(); - DisplayError( NULL, msg ); - } - } - - docfile = wxFopen( docFileName.GetFullPath(), wxT( "wt" ) ); - - if( docfile == NULL ) - { - docFileName.MakeAbsolute(); - msg = wxT( "Failed to create component document library file " ) + - docFileName.GetFullPath(); - DisplayError( NULL, msg ); - return false; - } - - if( fprintf( docfile, "%s Date: %s\n", DOCFILE_IDENT, TO_UTF8( DateAndTime() ) ) < 0 ) - { - fclose( docfile ); - return false; - } - bool success = true; - for( LIB_ALIAS_MAP::iterator it=aliases.begin(); it!=aliases.end(); it++ ) + try { - if ( !(*it).second->SaveDoc( docfile ) ) - success = false; + aFormatter.Print( 0, "%s Date: %s\n", DOCFILE_IDENT, TO_UTF8( DateAndTime() ) ); + + for( LIB_ALIAS_MAP::iterator it=aliases.begin(); it!=aliases.end(); it++ ) + { + if ( !(*it).second->SaveDoc( aFormatter ) ) + success = false; + } + + aFormatter.Print( 0, "#\n#End Doc Library\n" ); } - - if ( fprintf( docfile, "#\n#End Doc Library\n" ) < 0 ) + catch( IO_ERROR ioe ) + { success = false; - - fclose( docfile ); + } return success; } diff --git a/eeschema/class_library.h b/eeschema/class_library.h index f0e18a7cd4..58777b0001 100644 --- a/eeschema/class_library.h +++ b/eeschema/class_library.h @@ -1,3 +1,28 @@ +/* + * 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 + * 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. - *

- * 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. + * Function SaveDocs + * write the library document information to \a aFormatter. * - * If the document definition file* already exists, it is backed up in - * file *.bck. - * - * @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. diff --git a/eeschema/files-io.cpp b/eeschema/files-io.cpp index 5960d86daf..3238f254bc 100644 --- a/eeschema/files-io.cpp +++ b/eeschema/files-io.cpp @@ -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() ); } diff --git a/eeschema/lib_export.cpp b/eeschema/lib_export.cpp index 4337306b9f..17ecc148ec 100644 --- a/eeschema/lib_export.cpp +++ b/eeschema/lib_export.cpp @@ -1,3 +1,28 @@ +/* + * 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 + * 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 lib_export.cpp * @brief Eeschema library maintenance routines to backup modified libraries and @@ -10,6 +35,7 @@ #include "confirm.h" #include "gestfich.h" #include "eeschema_id.h" +#include "richio.h" #include "general.h" #include "protos.h" @@ -17,6 +43,7 @@ #include "class_library.h" #include +#include extern int ExportPartId; @@ -101,7 +128,19 @@ void LIB_EDIT_FRAME::OnExportPart( wxCommandEvent& event ) SaveOnePartInMemory(); - bool success = m_library->Save( fn.GetFullPath() ); + wxFFileOutputStream os( fn.GetFullPath(), wxT( "wt" ) ); + + if( !os.IsOk() ) + { + fn.MakeAbsolute(); + msg = wxT( "Failed to create component library file " ) + fn.GetFullPath(); + DisplayError( this, msg ); + return; + } + + STREAM_OUTPUTFORMATTER formatter( os ); + + bool success = m_library->Save( formatter ); if( success ) m_LastLibExportPath = fn.GetPath(); @@ -119,9 +158,14 @@ until it is loaded by Eeschema.\n\nModify the Eeschema library configuration \ if you want to include it as part of this project." ) ); } else + { msg = fn.GetFullPath() + _( " - Export OK" ); + } } // Error else + { msg = _( "Error creating " ) + fn.GetFullName(); + } + SetStatusText( msg ); } diff --git a/eeschema/libarch.cpp b/eeschema/libarch.cpp index 5f8cb98fa2..5e5138c281 100644 --- a/eeschema/libarch.cpp +++ b/eeschema/libarch.cpp @@ -1,3 +1,28 @@ +/* + * 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 + * 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 libarch.cpp * @brief Module for generation of component archive files. @@ -8,6 +33,7 @@ #include "class_sch_screen.h" #include "wxstruct.h" #include "sch_item_struct.h" +#include "wxEeschemaStruct.h" #include "general.h" #include "netlist.h" @@ -15,51 +41,59 @@ #include "class_library.h" #include "sch_component.h" +#include -/* - * Creates a library that contains all components used in the schematic. - * - * return true if success - */ -bool LibArchive( wxWindow* frame, const wxString& ArchFullFileName ) + +bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName ) { - wxString msg; - LIB_COMPONENT* Entry; + wxString msg; + LIB_COMPONENT* libComponent; CMP_LIBRARY* libCache; - SCH_SCREENS ScreenList; + SCH_SCREENS screens; - libCache = new CMP_LIBRARY( LIBRARY_TYPE_EESCHEMA, ArchFullFileName ); + libCache = new CMP_LIBRARY( LIBRARY_TYPE_EESCHEMA, aFileName ); libCache->SetCache(); /* examine all screens (not sheets) used and build the list of components * found in lib complex hierarchies are not a problem because we just want * to know used components in libraries */ - for( SCH_SCREEN* screen = ScreenList.GetFirst(); screen != NULL; - screen = ScreenList.GetNext() ) + for( SCH_SCREEN* screen = screens.GetFirst(); screen != NULL; screen = screens.GetNext() ) { - for( SCH_ITEM* SchItem = screen->GetDrawItems(); SchItem; SchItem = SchItem->Next() ) + for( SCH_ITEM* item = screen->GetDrawItems(); item; item = item->Next() ) { - if( SchItem->Type() != SCH_COMPONENT_T ) + if( item->Type() != SCH_COMPONENT_T ) continue; - SCH_COMPONENT* component = (SCH_COMPONENT*) SchItem; + SCH_COMPONENT* component = (SCH_COMPONENT*) item; // If not already saved in the new cache, put it: + if( libCache->FindEntry( component->GetLibName()) == NULL ) { - Entry = CMP_LIBRARY::FindLibraryComponent( component->GetLibName() ); + libComponent = CMP_LIBRARY::FindLibraryComponent( component->GetLibName() ); - if( Entry ) // if NULL : component not found, cannot be stored - libCache->AddComponent( Entry ); + if( libComponent ) // if NULL : component not found, cannot be stored + libCache->AddComponent( libComponent ); } } } - if( !libCache->Save( ArchFullFileName ) ) + wxFFileOutputStream os( aFileName, wxT( "wt" ) ); + + if( !os.IsOk() ) + { + msg = wxT( "Failed to create component library file " ) + aFileName; + DisplayError( this, msg ); + return false; + } + + STREAM_OUTPUTFORMATTER formatter( os ); + + if( !libCache->Save( formatter ) ) { msg.Printf( _( "An error occurred attempting to save component \ -library <%s>." ), GetChars( ArchFullFileName ) ); - DisplayError( frame, msg ); +library <%s>." ), GetChars( aFileName ) ); + DisplayError( this, msg ); return false; } diff --git a/eeschema/libedit.cpp b/eeschema/libedit.cpp index 42f70c006f..d432ec8f95 100644 --- a/eeschema/libedit.cpp +++ b/eeschema/libedit.cpp @@ -46,6 +46,8 @@ #include "dialogs/dialog_lib_new_component.h" +#include + void LIB_EDIT_FRAME::DisplayLibInfos() { @@ -311,23 +313,90 @@ void LIB_EDIT_FRAME::SaveActiveLibrary( wxCommandEvent& event ) if( !IsWritable( fn ) ) return; - bool success = m_library->Save( fn.GetFullPath(), true ); - ClearMsgPanel(); - if( !success ) + wxFileName libFileName = fn; + wxFileName backupFileName = fn; + + // Rename the old .lib file to .bak. + if( libFileName.FileExists() ) { - msg = _( "Error while saving library file \"" ) + fn.GetFullPath() + _( "\"." ); + 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( this, msg ); + } + } + + wxFFileOutputStream libStream( libFileName.GetFullPath(), wxT( "wt" ) ); + + if( !libStream.IsOk() ) + { + libFileName.MakeAbsolute(); + msg = wxT( "Failed to create component library file " ) + libFileName.GetFullPath(); + DisplayError( this, msg ); + 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; } - else + + wxFileName docFileName = libFileName; + + docFileName.SetExt( DOC_EXT ); + + // Rename .doc file to .bck. + if( docFileName.FileExists() ) { - msg = _( "Library file \"" ) + fn.GetFullName() + wxT( "\" Ok" ); - fn.SetExt( DOC_EXT ); - wxString msg1 = _( "Document file \"" ) + fn.GetFullPath() + wxT( "\" Ok" ); - AppendMsgPanel( msg, msg1, BLUE ); + backupFileName.SetExt( wxT( "bck" ) ); + wxRemoveFile( backupFileName.GetFullPath() ); + + if( !wxRenameFile( docFileName.GetFullPath(), backupFileName.GetFullPath() ) ) + { + msg = wxT( "Failed to save old library document file " ) + + backupFileName.GetFullPath(); + DisplayError( this, msg ); + } } + + wxFFileOutputStream docStream( docFileName.GetFullPath(), wxT( "wt" ) ); + + if( !docStream.IsOk() ) + { + docFileName.MakeAbsolute(); + msg = wxT( "Failed to create component document library file " ) + + docFileName.GetFullPath(); + DisplayError( this, msg ); + 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" ); + AppendMsgPanel( msg, msg1, BLUE ); } diff --git a/include/wxEeschemaStruct.h b/include/wxEeschemaStruct.h index 4462b1b87f..8b1c0d8912 100644 --- a/include/wxEeschemaStruct.h +++ b/include/wxEeschemaStruct.h @@ -1079,6 +1079,15 @@ public: */ void LoadLibraries( void ); + /** + * Function CreateArchiveLibrary + * creates a library \a aFileName that contains all components used in the current schematic. + * + * @param aFileName The full path and file name of the archive library. + * @return True if \a aFileName was written successfully. + */ + bool CreateArchiveLibrary( const wxString& aFileName ); + /** * Function PrintPage * plots or prints the current sheet to the clipboard.