2007-06-05 12:10:51 +00:00
|
|
|
/**********************************************************/
|
|
|
|
/* libclass.cpp */
|
|
|
|
/**********************************************************/
|
|
|
|
|
|
|
|
#include "fctsys.h"
|
|
|
|
#include "gr_basic.h"
|
2009-04-05 20:49:15 +00:00
|
|
|
#include "common.h"
|
2009-02-04 15:25:03 +00:00
|
|
|
#include "kicad_string.h"
|
2009-04-05 20:49:15 +00:00
|
|
|
#include "confirm.h"
|
|
|
|
#include "gestfich.h"
|
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
#include "program.h"
|
|
|
|
#include "libcmp.h"
|
|
|
|
#include "general.h"
|
|
|
|
#include "protos.h"
|
2009-04-05 20:49:15 +00:00
|
|
|
#include "class_library.h"
|
|
|
|
|
|
|
|
#include <wx/tokenzr.h>
|
|
|
|
#include <wx/wfstream.h>
|
|
|
|
#include <wx/txtstrm.h>
|
|
|
|
#include <wx/listimpl.cpp>
|
|
|
|
|
|
|
|
WX_DEFINE_LIST( LIB_CMP_LIST );
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
|
|
|
|
LibraryStruct::LibraryStruct( int type, const wxString& name,
|
|
|
|
const wxString& fullname )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2009-04-05 20:49:15 +00:00
|
|
|
m_Type = type; /* type indicator */
|
|
|
|
m_Name = name; /* Name of library loaded. */
|
|
|
|
m_FullFileName = fullname; /* File name (with path) of library loaded. */
|
|
|
|
m_NumOfParts = 0; /* Number of parts this library has. */
|
|
|
|
m_Entries = NULL; /* Parts themselves are saved here. */
|
|
|
|
m_Pnext = NULL; /* Point to next library in chain. */
|
|
|
|
m_Modified = FALSE; /* flag indicateur d'edition */
|
2007-08-24 15:10:46 +00:00
|
|
|
m_TimeStamp = 0;
|
|
|
|
m_Flags = 0;
|
|
|
|
m_IsLibCache = FALSE;
|
2009-04-05 20:49:15 +00:00
|
|
|
m_DateTime = wxDateTime::Now();
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
LibraryStruct::LibraryStruct( const wxChar* fileName )
|
|
|
|
{
|
|
|
|
if( fileName == NULL )
|
|
|
|
m_fileName = wxT( "unnamed.lib" );
|
|
|
|
else
|
|
|
|
m_fileName = fileName;
|
|
|
|
}
|
|
|
|
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
/*****************************************/
|
2007-06-05 12:10:51 +00:00
|
|
|
/* Used by PQFreeFunc() to delete all entries
|
2007-08-24 15:10:46 +00:00
|
|
|
*/
|
2009-04-05 20:49:15 +00:00
|
|
|
/*****************************************/
|
|
|
|
void FreeLibraryEntry( LibCmpEntry* Entry )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2008-11-24 06:53:43 +00:00
|
|
|
SAFE_DELETE( Entry );
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
/******************************/
|
|
|
|
LibraryStruct::~LibraryStruct()
|
|
|
|
{
|
2007-08-24 15:10:46 +00:00
|
|
|
if( m_Entries )
|
|
|
|
PQFreeFunc( m_Entries, ( void( * ) ( void* ) )FreeLibraryEntry );
|
2009-04-05 20:49:15 +00:00
|
|
|
|
|
|
|
m_componentList.DeleteContents( true );
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************/
|
|
|
|
/* Ecrit l'entete du fichier librairie
|
2007-08-24 15:10:46 +00:00
|
|
|
*/
|
2009-04-05 20:49:15 +00:00
|
|
|
/*******************************************/
|
|
|
|
bool LibraryStruct::WriteHeader( FILE* file )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2007-08-24 15:10:46 +00:00
|
|
|
char BufLine[1024];
|
|
|
|
bool succes = TRUE;
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2007-08-24 15:10:46 +00:00
|
|
|
DateAndTime( BufLine );
|
|
|
|
if( fprintf( file, "%s %d.%d Date: %s\n", LIBFILE_IDENT,
|
2009-04-05 20:49:15 +00:00
|
|
|
LIB_VERSION_MAJOR, LIB_VERSION_MINOR, BufLine ) != 5 )
|
2007-08-24 15:10:46 +00:00
|
|
|
succes = FALSE;
|
2007-06-05 12:10:51 +00:00
|
|
|
#if 0
|
2007-08-24 15:10:46 +00:00
|
|
|
if( fprintf( file, "$HEADER\n" ) != 1 )
|
|
|
|
succes = FALSE;
|
|
|
|
if( fprintf( file, "TimeStamp %8.8lX\n", m_TimeStamp ) != 2 )
|
|
|
|
succes = FALSE;
|
|
|
|
if( fprintf( file, "Parts %d\n", m_NumOfParts ) != 2 )
|
|
|
|
succes = FALSE;
|
|
|
|
if( fprintf( file, "$ENDHEADER\n" ) != 1 )
|
|
|
|
succes = FALSE;
|
2007-06-05 12:10:51 +00:00
|
|
|
#endif
|
2007-08-24 15:10:46 +00:00
|
|
|
return succes;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
/***********************************************************/
|
|
|
|
/* Ecrit l'entete du fichier librairie
|
2007-08-24 15:10:46 +00:00
|
|
|
*/
|
2009-04-05 20:49:15 +00:00
|
|
|
/***********************************************************/
|
|
|
|
bool LibraryStruct::ReadHeader( FILE* libfile, int* LineNum )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2007-08-24 15:10:46 +00:00
|
|
|
char Line[1024], * text, * data;
|
|
|
|
|
|
|
|
while( GetLine( libfile, Line, LineNum, sizeof(Line) ) )
|
|
|
|
{
|
|
|
|
text = strtok( Line, " \t\r\n" );
|
|
|
|
data = strtok( NULL, " \t\r\n" );
|
|
|
|
if( stricmp( text, "TimeStamp" ) == 0 )
|
|
|
|
m_TimeStamp = atol( data );
|
|
|
|
if( stricmp( text, "$ENDHEADER" ) == 0 )
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2007-08-24 15:10:46 +00:00
|
|
|
return FALSE;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function SaveLibrary
|
|
|
|
* writes the data structures for this object out to 2 file
|
|
|
|
* the library in "*.lib" format.
|
|
|
|
* the doc file in "*.dcm" format.
|
|
|
|
* creates a backup file for each file (.bak and .bck)
|
|
|
|
* @param aFullFileName The full lib filename.
|
|
|
|
* @return bool - true if success writing else false.
|
|
|
|
*/
|
|
|
|
bool LibraryStruct::SaveLibrary( const wxString& FullFileName )
|
|
|
|
{
|
|
|
|
FILE* libfile, *docfile;
|
|
|
|
EDA_LibComponentStruct* LibEntry;
|
|
|
|
wxString msg;
|
|
|
|
wxFileName libFileName = FullFileName;
|
|
|
|
wxFileName backupFileName = FullFileName;
|
|
|
|
wxFileName docFileName = FullFileName;
|
|
|
|
|
|
|
|
/* the old .lib file is renamed .bak */
|
|
|
|
if( libFileName.FileExists() )
|
|
|
|
{
|
|
|
|
backupFileName.SetExt( wxT( "bak" ) );
|
|
|
|
wxRemoveFile( backupFileName.GetFullPath() );
|
|
|
|
|
|
|
|
if( !wxRenameFile( libFileName.GetFullPath(),
|
|
|
|
backupFileName.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
msg = wxT( "Failed to rename old lib file " ) +
|
|
|
|
backupFileName.GetFullPath();
|
|
|
|
DisplayError( NULL, msg, 20 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
docFileName.SetExt( DOC_EXT );
|
|
|
|
/* L'ancien fichier doc lib est renomme en .bck */
|
|
|
|
if( wxFileExists( docFileName.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
backupFileName = docFileName;
|
|
|
|
backupFileName.SetExt( wxT( "bck" ) );
|
|
|
|
wxRemoveFile( backupFileName.GetFullPath() );
|
|
|
|
|
|
|
|
if( !wxRenameFile( docFileName.GetFullPath(),
|
|
|
|
backupFileName.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
msg = wxT( "Failed to save old doc lib file " ) +
|
|
|
|
backupFileName.GetFullPath();
|
|
|
|
DisplayError( NULL, msg, 20 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
libfile = wxFopen( libFileName.GetFullPath(), wxT( "wt" ) );
|
|
|
|
|
|
|
|
if( libfile == NULL )
|
|
|
|
{
|
|
|
|
msg = wxT( "Failed to create Lib File " ) + libFileName.GetFullPath();
|
|
|
|
DisplayError( NULL, msg, 20 );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
docfile = wxFopen( docFileName.GetFullPath(), wxT( "wt" ) );
|
|
|
|
|
|
|
|
if( docfile == NULL )
|
|
|
|
{
|
|
|
|
msg = wxT( "Failed to create DocLib File " ) +
|
|
|
|
docFileName.GetFullPath();
|
|
|
|
DisplayError( NULL, msg, 20 );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Modified = 0;
|
|
|
|
|
|
|
|
/* Creation de l'entete de la librairie */
|
|
|
|
m_TimeStamp = GetTimeStamp();
|
|
|
|
WriteHeader( libfile );
|
|
|
|
|
|
|
|
/* Sauvegarde des composant: */
|
|
|
|
PQCompFunc( (PQCompFuncType) LibraryEntryCompare );
|
|
|
|
LibEntry = (EDA_LibComponentStruct*) PQFirst( &m_Entries, FALSE );
|
|
|
|
char Line[256];
|
|
|
|
fprintf( docfile, "%s Date: %s\n", DOCFILE_IDENT,
|
|
|
|
DateAndTime( Line ) );
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
while( LibEntry )
|
|
|
|
{
|
|
|
|
if ( LibEntry->Type == ROOT )
|
|
|
|
{
|
|
|
|
if ( ! LibEntry->Save( libfile ) )
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
if ( docfile )
|
|
|
|
{
|
|
|
|
if ( ! LibEntry->SaveDoc( docfile ) )
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LibEntry = (EDA_LibComponentStruct*) PQNext( m_Entries,
|
|
|
|
LibEntry, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( libfile, "#\n#End Library\n" );
|
|
|
|
if ( docfile )
|
|
|
|
fprintf( docfile, "#\n#End Doc Library\n" );
|
|
|
|
fclose( libfile );
|
|
|
|
fclose( docfile );
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString LibraryStruct::GetName()
|
|
|
|
{
|
|
|
|
return m_fileName.GetName();
|
|
|
|
}
|