2007-06-05 12:10:51 +00:00
|
|
|
/**********************************************************/
|
2009-08-27 11:41:56 +00:00
|
|
|
/* libclass.cpp */
|
2007-06-05 12:10:51 +00:00
|
|
|
/**********************************************************/
|
|
|
|
|
|
|
|
#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"
|
2009-08-27 11:41:56 +00:00
|
|
|
#include "eda_doc.h"
|
2009-04-05 20:49:15 +00:00
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
#include "program.h"
|
|
|
|
#include "general.h"
|
|
|
|
#include "protos.h"
|
2009-04-05 20:49:15 +00:00
|
|
|
#include "class_library.h"
|
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
#include <boost/foreach.hpp>
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
#include <wx/tokenzr.h>
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
|
|
|
|
static const wxChar* duplicate_name_msg = _( "Component library <%s> has \
|
|
|
|
duplicate entry name <%s>. This may cause some expected behavior when \
|
|
|
|
loading components into a schematic." );
|
|
|
|
|
|
|
|
|
|
|
|
static bool DuplicateEntryName( const CMP_LIB_ENTRY& item1,
|
|
|
|
const CMP_LIB_ENTRY& item2 )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
return item1.m_Name.m_Text.CmpNoCase( item2.m_Name.m_Text ) == 0;
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
bool operator==( const CMP_LIBRARY& lib, const wxChar* name )
|
|
|
|
{
|
|
|
|
return lib.GetName().CmpNoCase( name ) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool operator!=( const CMP_LIBRARY& lib, const wxChar* name )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
|
|
|
return !( lib == name );
|
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
bool operator<( const CMP_LIBRARY& item1, const CMP_LIBRARY& item2 )
|
|
|
|
{
|
|
|
|
/* The cache library always is sorted to the end of the library list. */
|
|
|
|
if( item1.IsCache() )
|
|
|
|
return true;
|
|
|
|
if( item2.IsCache() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* If the sort order array isn't set, then sort alphabetically except. */
|
|
|
|
if( CMP_LIBRARY::GetSortOrder().IsEmpty() )
|
|
|
|
return item1.GetName().CmpNoCase( item2.GetName() ) < 0;
|
|
|
|
|
|
|
|
int i1 = CMP_LIBRARY::GetSortOrder().Index( item1.GetName(), false );
|
|
|
|
int i2 = CMP_LIBRARY::GetSortOrder().Index( item2.GetName(), false );
|
|
|
|
|
|
|
|
if( i1 == wxNOT_FOUND && i2 == wxNOT_FOUND )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( i1 == wxNOT_FOUND && i2 != wxNOT_FOUND )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( i1 != wxNOT_FOUND && i2 == wxNOT_FOUND )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return ( i1 - i2 ) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CMP_LIBRARY::CMP_LIBRARY( int type, const wxFileName& fileName )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2009-09-02 18:12:45 +00:00
|
|
|
m_Type = type; /* type indicator */
|
|
|
|
m_IsModified = false; /* flag indicateur d'edition */
|
2009-08-27 11:41:56 +00:00
|
|
|
m_TimeStamp = 0;
|
|
|
|
m_Flags = 0;
|
2009-09-18 14:56:05 +00:00
|
|
|
m_IsCache = false;
|
2009-08-27 11:41:56 +00:00
|
|
|
m_DateTime = wxDateTime::Now();
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
if( fileName.IsOk() )
|
2009-04-05 20:49:15 +00:00
|
|
|
m_fileName = fileName;
|
2009-09-18 14:56:05 +00:00
|
|
|
else
|
|
|
|
m_fileName = wxFileName( wxT( "unnamed.lib" ) );
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
|
|
|
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
CMP_LIBRARY::~CMP_LIBRARY()
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
void CMP_LIBRARY::GetEntryNames( wxArrayString& names, bool sort,
|
|
|
|
bool makeUpperCase )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
BOOST_FOREACH( CMP_LIB_ENTRY& entry, m_Entries )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-02 18:12:45 +00:00
|
|
|
if( makeUpperCase )
|
|
|
|
names.Add( entry.m_Name.m_Text.MakeUpper() );
|
|
|
|
else
|
2009-09-14 13:24:17 +00:00
|
|
|
names.Add( entry.GetName() );
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( sort )
|
|
|
|
names.Sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
void CMP_LIBRARY::SearchEntryNames( wxArrayString& names,
|
|
|
|
const wxString& nameSearch,
|
|
|
|
const wxString& keySearch,
|
|
|
|
bool sort )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
BOOST_FOREACH( CMP_LIB_ENTRY& entry, m_Entries )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-02 18:12:45 +00:00
|
|
|
if( !keySearch.IsEmpty() && KeyWordOk( keySearch, entry.m_KeyWord ) )
|
2009-09-14 13:24:17 +00:00
|
|
|
names.Add( entry.GetName() );
|
2009-08-27 11:41:56 +00:00
|
|
|
if( !nameSearch.IsEmpty() && WildCompareString( nameSearch,
|
2009-09-14 13:24:17 +00:00
|
|
|
entry.GetName(),
|
2009-08-27 11:41:56 +00:00
|
|
|
false ) )
|
2009-09-14 13:24:17 +00:00
|
|
|
names.Add( entry.GetName() );
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
2009-09-02 18:12:45 +00:00
|
|
|
|
|
|
|
if( sort )
|
|
|
|
names.Sort();
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
CMP_LIB_ENTRY* CMP_LIBRARY::FindEntry( const wxChar* name )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
BOOST_FOREACH( CMP_LIB_ENTRY& entry, m_Entries )
|
2009-09-02 18:12:45 +00:00
|
|
|
{
|
2009-09-14 13:24:17 +00:00
|
|
|
if( entry.GetName().CmpNoCase( name ) == 0 )
|
2009-09-02 18:12:45 +00:00
|
|
|
return &entry;
|
|
|
|
}
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
return NULL;
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
CMP_LIB_ENTRY* CMP_LIBRARY::FindEntry( const wxChar* name, LibrEntryType type )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
BOOST_FOREACH( CMP_LIB_ENTRY& entry, m_Entries )
|
2009-09-14 13:24:17 +00:00
|
|
|
{
|
|
|
|
if( entry.GetName().CmpNoCase( name ) == 0 && entry.Type == type )
|
|
|
|
return &entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
LIB_COMPONENT* CMP_LIBRARY::FindComponent( const wxChar* name )
|
2009-09-14 13:24:17 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
LIB_COMPONENT* component = NULL;
|
|
|
|
CMP_LIB_ENTRY* entry = FindEntry( name );
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-14 13:24:17 +00:00
|
|
|
if( entry != NULL && entry->Type == ALIAS )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
LIB_ALIAS* alias = (LIB_ALIAS*) entry;
|
|
|
|
component = alias->GetComponent();
|
2009-09-14 13:24:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
component = (LIB_COMPONENT*) entry;
|
2009-09-14 13:24:17 +00:00
|
|
|
}
|
2009-08-27 19:44:13 +00:00
|
|
|
|
2009-09-14 13:24:17 +00:00
|
|
|
return component;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
bool CMP_LIBRARY::AddAlias( LIB_ALIAS* alias )
|
2009-09-14 13:24:17 +00:00
|
|
|
{
|
|
|
|
wxASSERT( alias != NULL );
|
2009-08-27 19:44:13 +00:00
|
|
|
|
2009-09-14 13:24:17 +00:00
|
|
|
if( FindEntry( alias->GetName() ) != NULL )
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
|
|
|
|
msg.Printf( _( "Cannot add duplicate alias <%s> to library <%s>." ),
|
2009-09-18 14:56:05 +00:00
|
|
|
(const wxChar*) alias->GetName(),
|
|
|
|
(const wxChar*) m_fileName.GetName() );
|
2009-09-14 13:24:17 +00:00
|
|
|
return false;
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
m_Entries.push_back( (CMP_LIB_ENTRY*) alias );
|
2009-09-14 13:24:17 +00:00
|
|
|
m_IsModified = true;
|
|
|
|
return true;
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
LIB_COMPONENT* CMP_LIBRARY::AddComponent( LIB_COMPONENT* cmp )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
|
|
|
wxASSERT( cmp != NULL );
|
|
|
|
|
2009-09-25 18:49:04 +00:00
|
|
|
LIB_COMPONENT* newCmp = new LIB_COMPONENT( *cmp, this );
|
2009-08-27 11:41:56 +00:00
|
|
|
|
|
|
|
if( newCmp == NULL )
|
|
|
|
return NULL;
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
m_Entries.push_back( (CMP_LIB_ENTRY*) newCmp );
|
2009-09-02 18:12:45 +00:00
|
|
|
m_IsModified = true;
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
/* Cache libraries are component only libraries. Do not create alias
|
|
|
|
* entries. */
|
|
|
|
if( !m_IsCache )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
for( size_t i = 0; i < newCmp->m_AliasList.GetCount(); i++ )
|
2009-09-14 13:24:17 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
LIB_ALIAS* alias = FindAlias( newCmp->m_AliasList[ i ] );
|
|
|
|
|
|
|
|
if( alias == NULL )
|
|
|
|
{
|
|
|
|
alias = new LIB_ALIAS( newCmp->m_AliasList[ i ], newCmp );
|
|
|
|
m_Entries.push_back( alias );
|
|
|
|
}
|
|
|
|
else if( alias->GetComponent()->GetName().CmpNoCase( newCmp->GetName() ) != 0 )
|
|
|
|
{
|
|
|
|
wxLogError( _( "Conflict in library <%s>: alias <%s> already \
|
|
|
|
has root name <%s> and will not be assigned to root name <%s>." ),
|
|
|
|
(const wxChar*) m_fileName.GetName(),
|
|
|
|
(const wxChar*) alias->GetComponent()->GetName(),
|
2009-09-14 13:24:17 +00:00
|
|
|
(const wxChar*) newCmp->GetName() );
|
2009-09-18 14:56:05 +00:00
|
|
|
}
|
2009-09-14 13:24:17 +00:00
|
|
|
}
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
m_Entries.sort();
|
2009-09-18 14:56:05 +00:00
|
|
|
m_Entries.unique( DuplicateEntryName );
|
2009-09-02 18:12:45 +00:00
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
return newCmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
void CMP_LIBRARY::RemoveEntry( const wxString& name )
|
2009-09-02 18:12:45 +00:00
|
|
|
{
|
|
|
|
LIB_ENTRY_LIST::iterator i;
|
|
|
|
|
|
|
|
for( i = m_Entries.begin(); i < m_Entries.end(); i++ )
|
|
|
|
{
|
2009-09-14 13:24:17 +00:00
|
|
|
if( i->GetName().CmpNoCase( name ) == 0 )
|
2009-09-02 18:12:45 +00:00
|
|
|
{
|
|
|
|
m_Entries.erase( i );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
void CMP_LIBRARY::RemoveEntry( CMP_LIB_ENTRY* entry )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
|
|
|
wxASSERT( entry != NULL );
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
LIB_COMPONENT* Root;
|
|
|
|
LIB_ALIAS* Alias;
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
m_IsModified = true;
|
2009-08-27 11:41:56 +00:00
|
|
|
|
|
|
|
if( entry->Type == ALIAS )
|
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
Alias = (LIB_ALIAS*) entry;
|
|
|
|
Root = Alias->GetComponent();
|
2009-08-27 11:41:56 +00:00
|
|
|
|
|
|
|
/* Remove alias name from the root component alias list */
|
|
|
|
if( Root == NULL )
|
|
|
|
{
|
2009-09-04 18:57:37 +00:00
|
|
|
wxLogWarning( wxT( "No root component found for alias <%s> in \
|
|
|
|
library <%s>." ),
|
2009-09-14 13:24:17 +00:00
|
|
|
( const wxChar* ) entry->GetName(),
|
2009-09-18 14:56:05 +00:00
|
|
|
( const wxChar* ) m_fileName.GetName() );
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-14 13:24:17 +00:00
|
|
|
int index = Root->m_AliasList.Index( entry->GetName(), false );
|
2009-08-27 11:41:56 +00:00
|
|
|
|
|
|
|
if( index == wxNOT_FOUND )
|
|
|
|
wxLogWarning( wxT( "Alias <%s> not found in component <%s> \
|
|
|
|
alias list in library <%s>" ),
|
2009-09-14 13:24:17 +00:00
|
|
|
( const wxChar* ) entry->GetName(),
|
|
|
|
( const wxChar* ) Root->GetName(),
|
2009-09-18 14:56:05 +00:00
|
|
|
( const wxChar* ) m_fileName.GetName() );
|
2009-08-27 11:41:56 +00:00
|
|
|
else
|
|
|
|
Root->m_AliasList.RemoveAt( index );
|
|
|
|
}
|
|
|
|
|
2009-09-14 13:24:17 +00:00
|
|
|
RemoveEntry( Alias->GetName() );
|
2009-08-27 11:41:56 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
Root = ( LIB_COMPONENT* ) entry;
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
/* Entry is a component with no aliases so removal is simple. */
|
2009-08-27 11:41:56 +00:00
|
|
|
if( Root->m_AliasList.GetCount() == 0 )
|
|
|
|
{
|
2009-09-14 13:24:17 +00:00
|
|
|
RemoveEntry( Root->GetName() );
|
2009-08-27 11:41:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
/* Entry is a component with one or more alias. */
|
2009-08-27 11:41:56 +00:00
|
|
|
wxString AliasName = Root->m_AliasList[0];
|
|
|
|
|
|
|
|
/* The root component is not really deleted, it is renamed with the first
|
2009-09-02 18:12:45 +00:00
|
|
|
* alias name. */
|
2009-09-14 13:24:17 +00:00
|
|
|
Alias = FindAlias( AliasName );
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-14 13:24:17 +00:00
|
|
|
if( Alias == NULL )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
|
|
|
wxLogWarning( wxT( "Alias <%s> for component <%s> not found in \
|
|
|
|
library <%s>" ),
|
|
|
|
( const wxChar* ) AliasName,
|
2009-09-14 13:24:17 +00:00
|
|
|
( const wxChar* ) Root->GetName(),
|
2009-09-18 14:56:05 +00:00
|
|
|
( const wxChar* ) m_fileName.GetName() );
|
2009-09-02 18:12:45 +00:00
|
|
|
return;
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
/* Remove the first alias name from the component alias list. */
|
|
|
|
Root->m_AliasList.RemoveAt( 0 );
|
2009-04-05 20:49:15 +00:00
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
/* Rename the component to the name of the first alias. */
|
|
|
|
Root->m_Doc = Alias->m_Doc;
|
|
|
|
Root->m_KeyWord = Alias->m_KeyWord;
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
/* Remove the first alias from library. */
|
|
|
|
RemoveEntry( AliasName );
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
/* Change the root name. */
|
2009-09-02 18:12:45 +00:00
|
|
|
Root->m_Name.m_Text = AliasName;
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
LIB_COMPONENT* CMP_LIBRARY::ReplaceComponent( LIB_COMPONENT* oldComponent,
|
|
|
|
LIB_COMPONENT* newComponent )
|
2009-09-14 13:24:17 +00:00
|
|
|
{
|
|
|
|
wxASSERT( oldComponent != NULL && newComponent != NULL
|
|
|
|
&& oldComponent->GetName().CmpNoCase( newComponent->GetName() )== 0 );
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
int index;
|
2009-09-18 14:56:05 +00:00
|
|
|
LIB_ALIAS* alias;
|
2009-09-14 13:24:17 +00:00
|
|
|
|
|
|
|
if( oldComponent->m_AliasList != newComponent->m_AliasList )
|
|
|
|
{
|
|
|
|
/* Remove extra aliases. */
|
|
|
|
for( i = 0; i < oldComponent->m_AliasList.GetCount(); i++ )
|
|
|
|
{
|
|
|
|
index =
|
|
|
|
newComponent->m_AliasList.Index( oldComponent->m_AliasList[ i ] );
|
|
|
|
|
|
|
|
if( index != wxNOT_FOUND )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
wxLogDebug( wxT( "Removing extra alias <%s> from component <%s> \
|
|
|
|
in library <%s>." ),
|
|
|
|
(const wxChar*) oldComponent->m_AliasList[ i ],
|
|
|
|
(const wxChar*) oldComponent->GetName(),
|
2009-09-18 14:56:05 +00:00
|
|
|
(const wxChar*) m_fileName.GetName() );
|
2009-09-14 13:24:17 +00:00
|
|
|
|
|
|
|
RemoveEntry( oldComponent->m_AliasList[ i ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new aliases. */
|
|
|
|
for( i = 0; i < newComponent->m_AliasList.GetCount(); i++ )
|
|
|
|
{
|
|
|
|
index =
|
|
|
|
oldComponent->m_AliasList.Index( newComponent->m_AliasList[ i ] );
|
|
|
|
|
|
|
|
if( index != wxNOT_FOUND
|
|
|
|
|| FindEntry( newComponent->m_AliasList[ i ] ) != NULL )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
wxLogDebug( wxT( "Adding extra alias <%s> from component <%s> \
|
|
|
|
in library <%s>." ),
|
|
|
|
(const wxChar*) newComponent->m_AliasList[ i ],
|
|
|
|
(const wxChar*) newComponent->GetName(),
|
2009-09-18 14:56:05 +00:00
|
|
|
(const wxChar*) m_fileName.GetName() );
|
2009-09-14 13:24:17 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
alias = new LIB_ALIAS( newComponent->m_AliasList[ i ],
|
|
|
|
newComponent );
|
2009-09-14 13:24:17 +00:00
|
|
|
m_Entries.push_back( alias );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoveEntry( oldComponent->GetName() );
|
|
|
|
|
2009-09-25 18:49:04 +00:00
|
|
|
LIB_COMPONENT* newCmp = new LIB_COMPONENT( *newComponent, this );
|
2009-09-14 13:24:17 +00:00
|
|
|
|
|
|
|
if( newCmp == NULL )
|
|
|
|
return NULL;
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
m_Entries.push_back( (CMP_LIB_ENTRY*) newCmp );
|
2009-09-14 13:24:17 +00:00
|
|
|
m_Entries.sort();
|
|
|
|
m_IsModified = true;
|
|
|
|
return newCmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
CMP_LIB_ENTRY* CMP_LIBRARY::GetNextEntry( const wxChar* name )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-02 18:12:45 +00:00
|
|
|
size_t i;
|
2009-09-18 14:56:05 +00:00
|
|
|
CMP_LIB_ENTRY* entry = NULL;
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
for( i = 0; i < m_Entries.size(); i++ )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-14 13:24:17 +00:00
|
|
|
if( m_Entries[i].GetName().CmpNoCase( name ) == 0 )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-02 18:12:45 +00:00
|
|
|
if( i < m_Entries.size() - 1 )
|
|
|
|
{
|
|
|
|
entry = &m_Entries[ i + 1 ];
|
|
|
|
break;
|
|
|
|
}
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( entry == NULL )
|
2009-09-02 18:12:45 +00:00
|
|
|
entry = &m_Entries.front();
|
2009-08-27 11:41:56 +00:00
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
CMP_LIB_ENTRY* CMP_LIBRARY::GetPreviousEntry( const wxChar* name )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-02 18:12:45 +00:00
|
|
|
size_t i;
|
2009-09-18 14:56:05 +00:00
|
|
|
CMP_LIB_ENTRY* entry = NULL;
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
for( i = 0; i < m_Entries.size(); i++ )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-14 13:24:17 +00:00
|
|
|
if( m_Entries[i].GetName().CmpNoCase( name ) == 0 && entry )
|
2009-09-02 18:12:45 +00:00
|
|
|
break;
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
entry = &m_Entries[i];
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
return entry;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2007-08-24 15:10:46 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
bool CMP_LIBRARY::Load( wxString& errMsg )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
FILE* f;
|
|
|
|
int LineNum = 0;
|
|
|
|
char Line[1024];
|
|
|
|
LIB_COMPONENT* LibEntry;
|
|
|
|
wxString msg;
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
if( m_fileName.GetFullPath().IsEmpty() )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
errMsg = _( "The component library file name is not set." );
|
2009-08-27 11:41:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
f = wxFopen( m_fileName.GetFullPath(), wxT( "rt" ) );
|
2009-08-27 11:41:56 +00:00
|
|
|
|
|
|
|
if( f == NULL )
|
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
errMsg = _( "The file could not be opened." );
|
2009-08-27 11:41:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( GetLine( f, Line, &LineNum, sizeof( Line ) ) == NULL )
|
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
errMsg = _( "The file is empty!" );
|
2009-08-27 11:41:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There is no header if this is a symbol library. */
|
|
|
|
if( m_Type == LIBRARY_TYPE_EESCHEMA )
|
2009-09-18 14:56:05 +00:00
|
|
|
{
|
|
|
|
wxString tmp;
|
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
m_Header = CONV_FROM_UTF8( Line );
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
wxStringTokenizer tkn( m_Header );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The file header (first line) in library versions 2.0 and lower
|
|
|
|
* apparently started with EESchema-LIB. Sometime after 2.0, it
|
|
|
|
* was changed to EESchema-LIBRARY. Therefore, the test for
|
|
|
|
* EESchema-LIB will work in both cases. Don't change this unless
|
|
|
|
* backwards compatability is no longer required.
|
|
|
|
*/
|
|
|
|
if( !tkn.HasMoreTokens()
|
|
|
|
|| !tkn.GetNextToken().Upper().StartsWith(wxT( "EESCHEMA-LIB" ) ) )
|
|
|
|
{
|
|
|
|
errMsg = _( "The file is NOT an EESCHEMA library!" );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !tkn.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
errMsg = _( "The file header is missing version and time stamp \
|
|
|
|
information." );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tkn.GetNextToken() != wxT( "Version" ) || !tkn.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
errMsg = _( "The file header version information is invalid." );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
long major, minor;
|
|
|
|
wxStringTokenizer vers( tkn.GetNextToken(), wxT( "." ) );
|
|
|
|
|
|
|
|
if( !vers.HasMoreTokens() || !vers.GetNextToken().ToLong( &major )
|
|
|
|
|| major < 1L || !vers.HasMoreTokens()
|
|
|
|
|| !vers.GetNextToken().ToLong( & minor ) || minor < 0L
|
|
|
|
|| minor > 99 )
|
|
|
|
{
|
|
|
|
wxLogWarning( _( "The component library <%s> header version \
|
|
|
|
number is invalid.\n\nIn future versions of EESchema this library may not \
|
2009-09-22 12:27:57 +00:00
|
|
|
load correctly. To resolve this problem open the library in the library \
|
|
|
|
editor and save it. If this library is the project cache library, save \
|
2009-09-18 14:56:05 +00:00
|
|
|
the current schematic." ),
|
|
|
|
(const wxChar*) GetName() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_verMajor = (int) major;
|
|
|
|
m_verMinor = (int) minor;
|
|
|
|
|
|
|
|
wxLogDebug( wxT( "Component library <%s> is version %d.%d." ),
|
|
|
|
(const wxChar*) GetName(), m_verMajor, m_verMinor );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
while( GetLine( f, Line, &LineNum, sizeof( Line ) ) )
|
|
|
|
{
|
|
|
|
if( m_Type == LIBRARY_TYPE_EESCHEMA
|
|
|
|
&& strnicmp( Line, "$HEADER", 7 ) == 0 )
|
|
|
|
{
|
2009-09-02 18:12:45 +00:00
|
|
|
if( !LoadHeader( f, &LineNum ) )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
errMsg = _( "An error occured attempting to read the header." );
|
2009-08-27 11:41:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( strnicmp( Line, "DEF", 3 ) == 0 )
|
|
|
|
{
|
|
|
|
/* Read one DEF/ENDDEF part entry from library: */
|
2009-09-18 14:56:05 +00:00
|
|
|
LibEntry = new LIB_COMPONENT( wxEmptyString, this );
|
2009-08-27 11:41:56 +00:00
|
|
|
|
|
|
|
if( LibEntry->Load( f, Line, &LineNum, msg ) )
|
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
/* Check for duplicate entry names and warn the user about
|
|
|
|
* the potential conflict.
|
|
|
|
*/
|
|
|
|
if( FindEntry( LibEntry->GetName() ) != NULL )
|
|
|
|
{
|
|
|
|
wxLogWarning( duplicate_name_msg,
|
|
|
|
(const wxChar*) m_fileName.GetName(),
|
|
|
|
(const wxChar*) LibEntry->GetName() );
|
|
|
|
}
|
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
/* If we are here, this part is O.k. - put it in: */
|
2009-09-02 18:12:45 +00:00
|
|
|
m_Entries.push_back( LibEntry );
|
|
|
|
LoadAliases( LibEntry );
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
wxLogWarning( _( "Library <%s> component load error %s." ),
|
|
|
|
(const wxChar*) m_fileName.GetName(),
|
2009-08-27 11:41:56 +00:00
|
|
|
(const wxChar*) msg );
|
|
|
|
msg.Clear();
|
|
|
|
delete LibEntry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-02 18:12:45 +00:00
|
|
|
m_Entries.sort();
|
|
|
|
|
2009-08-27 11:41:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
void CMP_LIBRARY::LoadAliases( LIB_COMPONENT* component )
|
2009-08-27 11:41:56 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
wxASSERT( component != NULL && component->Type == ROOT );
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
LIB_ALIAS* alias;
|
|
|
|
unsigned ii;
|
2009-08-27 11:41:56 +00:00
|
|
|
|
|
|
|
for( ii = 0; ii < component->m_AliasList.GetCount(); ii++ )
|
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
if( FindEntry( component->m_AliasList[ii] ) != NULL )
|
|
|
|
{
|
|
|
|
wxLogError( duplicate_name_msg,
|
|
|
|
(const wxChar*) m_fileName.GetName(),
|
|
|
|
(const wxChar*) component->m_AliasList[ii] );
|
|
|
|
}
|
|
|
|
|
|
|
|
alias = new LIB_ALIAS( component->m_AliasList[ii], component, this );
|
|
|
|
m_Entries.push_back( alias );
|
2009-08-27 11:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
bool CMP_LIBRARY::LoadHeader( 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
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
bool CMP_LIBRARY::LoadDocs( wxString& errMsg )
|
2009-09-02 18:12:45 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
int LineNum = 0;
|
|
|
|
char Line[1024], * Name, * Text;
|
|
|
|
CMP_LIB_ENTRY* Entry;
|
|
|
|
FILE* f;
|
|
|
|
wxString msg;
|
|
|
|
wxFileName fn = m_fileName;
|
2009-09-02 18:12:45 +00:00
|
|
|
|
|
|
|
fn.SetExt( DOC_EXT );
|
|
|
|
|
|
|
|
f = wxFopen( fn.GetFullPath(), wxT( "rt" ) );
|
|
|
|
|
|
|
|
if( f == NULL )
|
|
|
|
{
|
2009-09-22 12:27:57 +00:00
|
|
|
errMsg.Printf( _( "Could not open component document libray file <%s>." ),
|
2009-09-02 18:12:45 +00:00
|
|
|
(const wxChar*) fn.GetFullPath() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( GetLine( f, Line, &LineNum, sizeof(Line) ) == NULL )
|
|
|
|
{
|
|
|
|
errMsg.Printf( _( "Component document libray file <%s> is empty." ),
|
|
|
|
(const wxChar*) fn.GetFullPath() );
|
|
|
|
fclose( f );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( strnicmp( Line, DOCFILE_IDENT, 10 ) != 0 )
|
|
|
|
{
|
|
|
|
errMsg.Printf( _( "File <%s> is not a valid component library \
|
|
|
|
document file." ),
|
|
|
|
(const wxChar*) fn.GetFullPath() );
|
|
|
|
fclose( f );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while( GetLine( f, Line, &LineNum, sizeof(Line) ) )
|
|
|
|
{
|
|
|
|
if( strncmp( Line, "$CMP", 4 ) != 0 )
|
|
|
|
{
|
|
|
|
errMsg.Printf( wxT( "$CMP command expected in line %d, aborted." ),
|
|
|
|
LineNum );
|
|
|
|
fclose( f );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read one $CMP/$ENDCMP part entry from library: */
|
|
|
|
Name = strtok( Line + 5, "\n\r" );
|
|
|
|
|
|
|
|
wxString cmpname = CONV_FROM_UTF8( Name );
|
|
|
|
|
2009-09-14 13:24:17 +00:00
|
|
|
Entry = FindEntry( cmpname );
|
2009-09-02 18:12:45 +00:00
|
|
|
|
|
|
|
while( GetLine( f, Line, &LineNum, sizeof(Line) ) )
|
|
|
|
{
|
|
|
|
if( strncmp( Line, "$ENDCMP", 7 ) == 0 )
|
|
|
|
break;
|
|
|
|
Text = strtok( Line + 2, "\n\r" );
|
|
|
|
|
|
|
|
switch( Line[0] )
|
|
|
|
{
|
|
|
|
case 'D':
|
|
|
|
if( Entry )
|
|
|
|
Entry->m_Doc = CONV_FROM_UTF8( Text );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'K':
|
|
|
|
if( Entry )
|
|
|
|
Entry->m_KeyWord = CONV_FROM_UTF8( Text );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'F':
|
|
|
|
if( Entry )
|
|
|
|
Entry->m_DocFile = CONV_FROM_UTF8( Text );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose( f );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-25 18:49:04 +00:00
|
|
|
bool CMP_LIBRARY::Save( const wxString& FullFileName, bool oldDocFormat )
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
2009-09-25 18:49:04 +00:00
|
|
|
FILE* libfile;
|
2009-04-05 20:49:15 +00:00
|
|
|
wxString msg;
|
|
|
|
wxFileName libFileName = FullFileName;
|
|
|
|
wxFileName backupFileName = FullFileName;
|
|
|
|
|
|
|
|
/* the old .lib file is renamed .bak */
|
|
|
|
if( libFileName.FileExists() )
|
|
|
|
{
|
|
|
|
backupFileName.SetExt( wxT( "bak" ) );
|
|
|
|
wxRemoveFile( backupFileName.GetFullPath() );
|
|
|
|
|
|
|
|
if( !wxRenameFile( libFileName.GetFullPath(),
|
|
|
|
backupFileName.GetFullPath() ) )
|
|
|
|
{
|
2009-09-25 18:49:04 +00:00
|
|
|
libFileName.MakeAbsolute();
|
|
|
|
msg = wxT( "Failed to rename old component library file " ) +
|
2009-04-05 20:49:15 +00:00
|
|
|
backupFileName.GetFullPath();
|
2009-08-27 11:41:56 +00:00
|
|
|
DisplayError( NULL, msg );
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-25 18:49:04 +00:00
|
|
|
libfile = wxFopen( libFileName.GetFullPath(), wxT( "wt" ) );
|
|
|
|
|
|
|
|
if( libfile == NULL )
|
|
|
|
{
|
|
|
|
libFileName.MakeAbsolute();
|
|
|
|
msg = wxT( "Failed to create component library file " ) +
|
|
|
|
libFileName.GetFullPath();
|
|
|
|
DisplayError( NULL, msg );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_IsModified = false;
|
|
|
|
|
|
|
|
/* Creation de l'entete de la librairie */
|
|
|
|
m_TimeStamp = GetTimeStamp();
|
|
|
|
if( !SaveHeader( libfile ) )
|
|
|
|
{
|
|
|
|
fclose( libfile );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
BOOST_FOREACH( CMP_LIB_ENTRY& entry, m_Entries )
|
|
|
|
{
|
|
|
|
if ( entry.Type == ROOT )
|
|
|
|
{
|
|
|
|
LIB_COMPONENT* component = ( LIB_COMPONENT* ) &entry;
|
|
|
|
if ( !component->Save( libfile ) )
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fprintf( libfile, "#\n#End Library\n" ) < 0 )
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
fclose( libfile );
|
|
|
|
|
|
|
|
if( USE_OLD_DOC_FILE_FORMAT( m_verMajor, m_verMinor ) && oldDocFormat )
|
|
|
|
success = SaveDocFile( FullFileName );
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CMP_LIBRARY::SaveDocFile( const wxString& FullFileName )
|
|
|
|
{
|
|
|
|
FILE* docfile;
|
|
|
|
wxString msg;
|
|
|
|
wxFileName backupFileName = FullFileName;
|
|
|
|
wxFileName docFileName = FullFileName;
|
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
docFileName.SetExt( DOC_EXT );
|
2009-09-25 18:49:04 +00:00
|
|
|
|
|
|
|
/* Save current doc file as .bck */
|
|
|
|
if( docFileName.FileExists() )
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
|
|
|
backupFileName = docFileName;
|
|
|
|
backupFileName.SetExt( wxT( "bck" ) );
|
|
|
|
wxRemoveFile( backupFileName.GetFullPath() );
|
|
|
|
|
|
|
|
if( !wxRenameFile( docFileName.GetFullPath(),
|
|
|
|
backupFileName.GetFullPath() ) )
|
|
|
|
{
|
2009-09-25 18:49:04 +00:00
|
|
|
msg = wxT( "Failed to save old library document file " ) +
|
2009-04-05 20:49:15 +00:00
|
|
|
backupFileName.GetFullPath();
|
2009-08-27 11:41:56 +00:00
|
|
|
DisplayError( NULL, msg );
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
docfile = wxFopen( docFileName.GetFullPath(), wxT( "wt" ) );
|
|
|
|
|
|
|
|
if( docfile == NULL )
|
|
|
|
{
|
2009-09-25 18:49:04 +00:00
|
|
|
docFileName.MakeAbsolute();
|
2009-09-18 14:56:05 +00:00
|
|
|
msg = wxT( "Failed to create component document library file " ) +
|
2009-04-05 20:49:15 +00:00
|
|
|
docFileName.GetFullPath();
|
2009-08-27 11:41:56 +00:00
|
|
|
DisplayError( NULL, msg );
|
2009-09-25 18:49:04 +00:00
|
|
|
return false;
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char Line[256];
|
2009-09-25 18:49:04 +00:00
|
|
|
if( fprintf( docfile, "%s Date: %s\n", DOCFILE_IDENT,
|
|
|
|
DateAndTime( Line ) ) < 0 )
|
|
|
|
{
|
|
|
|
fclose( docfile );
|
|
|
|
return false;
|
|
|
|
}
|
2009-04-05 20:49:15 +00:00
|
|
|
|
|
|
|
bool success = true;
|
2009-08-27 11:41:56 +00:00
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
BOOST_FOREACH( CMP_LIB_ENTRY& entry, m_Entries )
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
2009-09-25 18:49:04 +00:00
|
|
|
if ( !entry.SaveDoc( docfile ) )
|
|
|
|
success = false;
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|
|
|
|
|
2009-09-25 18:49:04 +00:00
|
|
|
if ( fprintf( docfile, "#\n#End Doc Library\n" ) < 0 )
|
|
|
|
success = false;
|
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
fclose( docfile );
|
2009-09-25 18:49:04 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
bool CMP_LIBRARY::SaveHeader( FILE* file )
|
2009-09-02 18:12:45 +00:00
|
|
|
{
|
|
|
|
char BufLine[1024];
|
2009-09-25 18:49:04 +00:00
|
|
|
bool succes = true;
|
2009-09-02 18:12:45 +00:00
|
|
|
|
|
|
|
DateAndTime( BufLine );
|
|
|
|
if( fprintf( file, "%s %d.%d Date: %s\n", LIBFILE_IDENT,
|
|
|
|
LIB_VERSION_MAJOR, LIB_VERSION_MINOR, BufLine ) < 0 )
|
|
|
|
succes = false;
|
|
|
|
#if 0
|
|
|
|
if( ( fprintf( file, "$HEADER\n" ) < 0 )
|
|
|
|
|| ( fprintf( file, "TimeStamp %8.8lX\n", m_TimeStamp ) < 0 )
|
|
|
|
|| ( fprintf( file, "Parts %d\n", m_Entries.size() ) != 2 )
|
|
|
|
|| ( fprintf( file, "$ENDHEADER\n" ) != 1 ) )
|
|
|
|
succes = false;
|
|
|
|
#endif
|
|
|
|
return succes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 14:56:05 +00:00
|
|
|
/*
|
|
|
|
* The static library list and list management methods.
|
|
|
|
*/
|
|
|
|
CMP_LIBRARY_LIST CMP_LIBRARY::m_LibraryList;
|
|
|
|
wxArrayString CMP_LIBRARY::m_LibraryListSortOrder;
|
|
|
|
|
|
|
|
|
|
|
|
CMP_LIBRARY* CMP_LIBRARY::LoadLibrary( const wxFileName& fileName,
|
|
|
|
wxString& errMsg )
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
2009-09-18 14:56:05 +00:00
|
|
|
CMP_LIBRARY* lib = NULL;
|
|
|
|
|
|
|
|
lib = new CMP_LIBRARY( LIBRARY_TYPE_EESCHEMA, fileName );
|
|
|
|
|
|
|
|
wxBusyCursor ShowWait;
|
|
|
|
|
|
|
|
if( !lib->Load( errMsg ) )
|
|
|
|
{
|
|
|
|
delete lib;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-25 18:49:04 +00:00
|
|
|
if( USE_OLD_DOC_FILE_FORMAT( lib->m_verMajor, lib->m_verMinor ) )
|
|
|
|
lib->LoadDocs( errMsg );
|
2009-09-18 14:56:05 +00:00
|
|
|
|
|
|
|
return lib;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CMP_LIBRARY::AddLibrary( const wxFileName& fileName, wxString& errMsg )
|
|
|
|
{
|
|
|
|
CMP_LIBRARY* lib;
|
|
|
|
|
|
|
|
/* Don't reload the library if it is already loaded. */
|
|
|
|
lib = FindLibrary( fileName.GetName() );
|
|
|
|
|
|
|
|
if( lib != NULL )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
lib = LoadLibrary( fileName, errMsg );
|
|
|
|
|
|
|
|
if( lib == NULL )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_LibraryList.push_back( lib );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CMP_LIBRARY::AddLibrary( const wxFileName& fileName, wxString& errMsg,
|
|
|
|
CMP_LIBRARY_LIST::iterator& i )
|
|
|
|
{
|
|
|
|
CMP_LIBRARY* lib;
|
|
|
|
|
|
|
|
/* Don't reload the library if it is already loaded. */
|
|
|
|
lib = FindLibrary( fileName.GetName() );
|
|
|
|
|
|
|
|
if( lib != NULL )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
lib = LoadLibrary( fileName, errMsg );
|
|
|
|
|
|
|
|
if( lib == NULL )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( i >= m_LibraryList.begin() && i < m_LibraryList.end() )
|
|
|
|
m_LibraryList.insert( i, lib );
|
|
|
|
else
|
|
|
|
m_LibraryList.push_back( lib );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CMP_LIBRARY::RemoveLibrary( const wxString& name )
|
|
|
|
{
|
|
|
|
if( name.IsEmpty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
CMP_LIBRARY_LIST::iterator i;
|
|
|
|
|
|
|
|
for ( i = m_LibraryList.begin(); i < m_LibraryList.end(); i++ )
|
|
|
|
{
|
|
|
|
if( i->GetName().CmpNoCase( name ) == 0 )
|
|
|
|
{
|
|
|
|
CMP_LIBRARY::m_LibraryList.erase( i );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CMP_LIBRARY* CMP_LIBRARY::FindLibrary( const wxString& name )
|
|
|
|
{
|
|
|
|
BOOST_FOREACH( CMP_LIBRARY& lib, m_LibraryList )
|
|
|
|
{
|
|
|
|
if( lib == name )
|
|
|
|
return &lib;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxArrayString CMP_LIBRARY::GetLibraryNames( bool sorted )
|
|
|
|
{
|
|
|
|
wxString cacheName;
|
|
|
|
wxArrayString names;
|
|
|
|
|
|
|
|
BOOST_FOREACH( CMP_LIBRARY& lib, CMP_LIBRARY::m_LibraryList )
|
|
|
|
{
|
|
|
|
if( lib.m_IsCache && sorted )
|
|
|
|
cacheName = lib.GetName();
|
|
|
|
else
|
|
|
|
names.Add( lib.GetName() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Even sorted, the cache library is always at the end of the list. */
|
|
|
|
if( sorted )
|
|
|
|
names.Sort();
|
|
|
|
|
|
|
|
if( !cacheName.IsEmpty() )
|
|
|
|
names.Add( cacheName );
|
|
|
|
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LIB_COMPONENT* CMP_LIBRARY::FindLibraryComponent( const wxString& name,
|
|
|
|
const wxString& libName )
|
|
|
|
{
|
|
|
|
LIB_COMPONENT* component = NULL;
|
|
|
|
|
|
|
|
BOOST_FOREACH( CMP_LIBRARY& lib, m_LibraryList )
|
|
|
|
{
|
|
|
|
if( !libName.IsEmpty() && lib.GetName() != libName )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
component = lib.FindComponent( name );
|
|
|
|
|
|
|
|
if( component != NULL )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return component;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CMP_LIB_ENTRY* CMP_LIBRARY::FindLibraryEntry( const wxString& name,
|
|
|
|
const wxString& libName )
|
|
|
|
{
|
|
|
|
CMP_LIB_ENTRY* entry = NULL;
|
|
|
|
|
|
|
|
BOOST_FOREACH( CMP_LIBRARY& lib, m_LibraryList )
|
|
|
|
{
|
|
|
|
if( !libName.IsEmpty() && lib.GetName() != libName )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
entry = lib.FindEntry( name );
|
|
|
|
|
|
|
|
if( entry != NULL )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CMP_LIBRARY::RemoveCacheLibrary( void )
|
|
|
|
{
|
|
|
|
CMP_LIBRARY_LIST::iterator i;
|
|
|
|
|
|
|
|
for ( i = m_LibraryList.begin(); i < m_LibraryList.end(); i++ )
|
|
|
|
{
|
|
|
|
if( i->m_IsCache )
|
|
|
|
m_LibraryList.erase( i-- );
|
|
|
|
}
|
2009-04-05 20:49:15 +00:00
|
|
|
}
|