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.
This commit is contained in:
Wayne Stambaugh 2011-11-11 15:10:24 -05:00
parent 77521c4b60
commit 7a5e6a2d66
9 changed files with 260 additions and 158 deletions

View File

@ -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() ) if( description.IsEmpty() && keyWords.IsEmpty() && docFileName.IsEmpty() )
return true; return true;
if( fprintf( aFile, "#\n$CMP %s\n", TO_UTF8( name ) ) < 0 ) try
return false; {
aFormatter.Print( 0, "#\n$CMP %s\n", TO_UTF8( name ) );
if( ! description.IsEmpty() if( !description.IsEmpty() )
&& fprintf( aFile, "D %s\n", TO_UTF8( description ) ) < 0 ) aFormatter.Print( 0, "D %s\n", TO_UTF8( description ) );
return false;
if( ! keyWords.IsEmpty() if( !keyWords.IsEmpty() )
&& fprintf( aFile, "K %s\n", TO_UTF8( keyWords ) ) < 0 ) aFormatter.Print( 0, "K %s\n", TO_UTF8( keyWords ) );
return false;
if( ! docFileName.IsEmpty() if( !docFileName.IsEmpty() )
&& fprintf( aFile, "F %s\n", TO_UTF8( docFileName ) ) < 0 ) aFormatter.Print( 0, "F %s\n", TO_UTF8( docFileName ) );
return false;
if( fprintf( aFile, "$ENDCMP\n" ) < 0 ) aFormatter.Print( 0, "$ENDCMP\n" );
}
catch( IO_ERROR ioe )
{
return false; return false;
}
return true; return true;
} }

View File

@ -149,12 +149,13 @@ public:
wxString GetDocFileName() const { return docFileName; } 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. * @return True if success writing else false.
*/ */
bool SaveDoc( FILE* aFile ); bool SaveDoc( OUTPUTFORMATTER& aFormatter );
/** /**
* Case insensitive comparison of the component entry name. * 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: * 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 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. * output format to write to.
* @return True if the date and time were successfully written to \a aFormatter. * @return True if the date and time were successfully written to \a aFormatter.
*/ */

View File

@ -31,7 +31,6 @@
#include "gr_basic.h" #include "gr_basic.h"
#include "macros.h" #include "macros.h"
#include "kicad_string.h" #include "kicad_string.h"
#include "confirm.h"
#include "gestfich.h" #include "gestfich.h"
#include "eda_doc.h" #include "eda_doc.h"
#include "wxstruct.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 ) if( isModified )
{ {
timeStamp = GetTimeStamp(); timeStamp = GetTimeStamp();
@ -713,83 +681,47 @@ bool CMP_LIBRARY::Save( const wxString& aFullFileName, bool aOldDocFormat )
try try
{ {
SaveHeader( formatter ); SaveHeader( aFormatter );
for( LIB_ALIAS_MAP::iterator it=aliases.begin(); it!=aliases.end(); it++ ) for( LIB_ALIAS_MAP::iterator it=aliases.begin(); it!=aliases.end(); it++ )
{ {
if( !(*it).second->IsRoot() ) if( !(*it).second->IsRoot() )
continue; 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 ) catch( IO_ERROR ioe )
{ {
success = false; success = false;
} }
if( USE_OLD_DOC_FILE_FORMAT( versionMajor, versionMinor ) && aOldDocFormat )
success = SaveDocFile( aFullFileName );
return success; 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; bool success = true;
for( LIB_ALIAS_MAP::iterator it=aliases.begin(); it!=aliases.end(); it++ ) try
{ {
if ( !(*it).second->SaveDoc( docfile ) ) aFormatter.Print( 0, "%s Date: %s\n", DOCFILE_IDENT, TO_UTF8( DateAndTime() ) );
success = false;
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" );
} }
catch( IO_ERROR ioe )
if ( fprintf( docfile, "#\n#End Doc Library\n" ) < 0 ) {
success = false; success = false;
}
fclose( docfile );
return success; return success;
} }

View File

@ -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 <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 * @file class_library.h
* @brief Definition for component library class. * @brief Definition for component library class.
@ -90,35 +115,21 @@ public:
/** /**
* Function Save * Function Save
* saves library to a file. * writes library to \a aFormatter.
* <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.
* *
* @param aFullFileName - The library filename with path. * @param aFormatter An #OUTPUTFORMATTER object to write the library to.
* @param aOldDocFormat - Save the document information in a separate * @return True if success writing to \a aFormatter.
* file if true. The default is to save as the
* current library file format.
* @return True if success writing else false.
*/ */
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 * @param aFormatter An #OUTPUTFORMATTER object to write the library documentation to.
* file *.bck. * @return True if success writing to \a aFormatter.
*
* @param aFullFileName - The library filename with path.
* @return True if success writing else false.
*/ */
bool SaveDocFile( const wxString& aFullFileName ); bool SaveDocs( OUTPUTFORMATTER& aFormatter );
/** /**
* Load library from file. * Load library from file.

View File

@ -379,7 +379,7 @@ void SCH_EDIT_FRAME::OnSaveProject( wxCommandEvent& aEvent )
wxString cachename = fn.GetName() + wxT( "-cache" ); wxString cachename = fn.GetName() + wxT( "-cache" );
fn.SetName( cachename ); fn.SetName( cachename );
fn.SetExt( CompLibFileExtension ); fn.SetExt( CompLibFileExtension );
LibArchive( this, fn.GetFullPath() ); CreateArchiveLibrary( fn.GetFullPath() );
} }

View File

@ -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 <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 lib_export.cpp * @file lib_export.cpp
* @brief Eeschema library maintenance routines to backup modified libraries and * @brief Eeschema library maintenance routines to backup modified libraries and
@ -10,6 +35,7 @@
#include "confirm.h" #include "confirm.h"
#include "gestfich.h" #include "gestfich.h"
#include "eeschema_id.h" #include "eeschema_id.h"
#include "richio.h"
#include "general.h" #include "general.h"
#include "protos.h" #include "protos.h"
@ -17,6 +43,7 @@
#include "class_library.h" #include "class_library.h"
#include <wx/filename.h> #include <wx/filename.h>
#include <wx/wfstream.h>
extern int ExportPartId; extern int ExportPartId;
@ -101,7 +128,19 @@ void LIB_EDIT_FRAME::OnExportPart( wxCommandEvent& event )
SaveOnePartInMemory(); 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 ) if( success )
m_LastLibExportPath = fn.GetPath(); 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." ) ); if you want to include it as part of this project." ) );
} }
else else
{
msg = fn.GetFullPath() + _( " - Export OK" ); msg = fn.GetFullPath() + _( " - Export OK" );
}
} // Error } // Error
else else
{
msg = _( "Error creating " ) + fn.GetFullName(); msg = _( "Error creating " ) + fn.GetFullName();
}
SetStatusText( msg ); SetStatusText( msg );
} }

View File

@ -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 <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 libarch.cpp * @file libarch.cpp
* @brief Module for generation of component archive files. * @brief Module for generation of component archive files.
@ -8,6 +33,7 @@
#include "class_sch_screen.h" #include "class_sch_screen.h"
#include "wxstruct.h" #include "wxstruct.h"
#include "sch_item_struct.h" #include "sch_item_struct.h"
#include "wxEeschemaStruct.h"
#include "general.h" #include "general.h"
#include "netlist.h" #include "netlist.h"
@ -15,51 +41,59 @@
#include "class_library.h" #include "class_library.h"
#include "sch_component.h" #include "sch_component.h"
#include <wx/wfstream.h>
/*
* Creates a library that contains all components used in the schematic. bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName )
*
* return true if success
*/
bool LibArchive( wxWindow* frame, const wxString& ArchFullFileName )
{ {
wxString msg; wxString msg;
LIB_COMPONENT* Entry; LIB_COMPONENT* libComponent;
CMP_LIBRARY* libCache; 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(); libCache->SetCache();
/* examine all screens (not sheets) used and build the list of components /* 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 * found in lib complex hierarchies are not a problem because we just want
* to know used components in libraries * to know used components in libraries
*/ */
for( SCH_SCREEN* screen = ScreenList.GetFirst(); screen != NULL; for( SCH_SCREEN* screen = screens.GetFirst(); screen != NULL; screen = screens.GetNext() )
screen = ScreenList.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; continue;
SCH_COMPONENT* component = (SCH_COMPONENT*) SchItem; SCH_COMPONENT* component = (SCH_COMPONENT*) item;
// If not already saved in the new cache, put it: // If not already saved in the new cache, put it:
if( libCache->FindEntry( component->GetLibName()) == NULL ) 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 if( libComponent ) // if NULL : component not found, cannot be stored
libCache->AddComponent( Entry ); 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 \ msg.Printf( _( "An error occurred attempting to save component \
library <%s>." ), GetChars( ArchFullFileName ) ); library <%s>." ), GetChars( aFileName ) );
DisplayError( frame, msg ); DisplayError( this, msg );
return false; return false;
} }

View File

@ -46,6 +46,8 @@
#include "dialogs/dialog_lib_new_component.h" #include "dialogs/dialog_lib_new_component.h"
#include <wx/wfstream.h>
void LIB_EDIT_FRAME::DisplayLibInfos() void LIB_EDIT_FRAME::DisplayLibInfos()
{ {
@ -311,23 +313,90 @@ void LIB_EDIT_FRAME::SaveActiveLibrary( wxCommandEvent& event )
if( !IsWritable( fn ) ) if( !IsWritable( fn ) )
return; return;
bool success = m_library->Save( fn.GetFullPath(), true );
ClearMsgPanel(); 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 ); AppendMsgPanel( _( "*** ERROR: ***" ), msg, RED );
DisplayError( this, msg ); 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" ); backupFileName.SetExt( wxT( "bck" ) );
fn.SetExt( DOC_EXT ); wxRemoveFile( backupFileName.GetFullPath() );
wxString msg1 = _( "Document file \"" ) + fn.GetFullPath() + wxT( "\" Ok" );
AppendMsgPanel( msg, msg1, BLUE ); 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 );
} }

View File

@ -1079,6 +1079,15 @@ public:
*/ */
void LoadLibraries( void ); 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 * Function PrintPage
* plots or prints the current sheet to the clipboard. * plots or prints the current sheet to the clipboard.