28#include <lib_table_lexer.h>
45using namespace LIB_TABLE_T;
72 if(
type == SCH_IO_MGR::SCH_FILE_UNKNOWN )
73 type = SCH_IO_MGR::SCH_KICAD;
83 wxArrayString dummyList;
110 if(
type != SCH_IO_MGR::SCH_DATABASE )
140 if( in->CurTok() != T_sym_lib_table )
144 if( ( tok = in->NextTok() ) != T_sym_lib_table )
145 in->Expecting( T_sym_lib_table );
148 while( ( tok = in->NextTok() ) != T_RIGHT )
150 std::unique_ptr< SYMBOL_LIB_TABLE_ROW > row = std::make_unique<SYMBOL_LIB_TABLE_ROW>();
153 in->Expecting( T_RIGHT );
156 in->Expecting( T_LEFT );
159 int lineNum = in->CurLineNumber();
163 if( tok == T_version )
165 in->NeedNUMBER(
"version" );
172 in->Expecting( T_lib );
177 if( ( tok = in->NextTok() ) != T_name )
178 in->Expecting( T_name );
180 in->NeedSYMBOLorNUMBER();
182 row->SetNickName( in->FromUTF8() );
188 bool sawType =
false;
189 bool sawOpts =
false;
190 bool sawDesc =
false;
192 bool sawDisabled =
false;
193 bool sawHidden =
false;
195 while( ( tok = in->NextTok() ) != T_RIGHT )
198 in->Unexpected( T_EOF );
201 in->Expecting( T_LEFT );
203 tok = in->NeedSYMBOLorNUMBER();
209 in->Duplicate( tok );
211 in->NeedSYMBOLorNUMBER();
212 row->SetFullURI( in->FromUTF8() );
217 in->Duplicate( tok );
219 in->NeedSYMBOLorNUMBER();
220 row->SetType( in->FromUTF8() );
225 in->Duplicate( tok );
227 in->NeedSYMBOLorNUMBER();
228 row->SetOptions( in->FromUTF8() );
233 in->Duplicate( tok );
235 in->NeedSYMBOLorNUMBER();
236 row->SetDescr( in->FromUTF8() );
241 in->Duplicate( tok );
243 row->SetEnabled(
false );
248 in->Duplicate( tok );
250 row->SetVisible(
false );
254 in->Unexpected( tok );
261 in->Expecting( T_type );
264 in->Expecting( T_uri );
270 wxString nickname = row->GetNickName();
278 wxString msg = wxString::Format(
_(
"Duplicate library nickname '%s' found in symbol "
279 "library table file line %d" ),
283 if( !errMsg.IsEmpty() )
290 if( !errMsg.IsEmpty() )
297 aOutput->
Print( aIndentLevel,
"(sym_lib_table\n" );
301 row.Format( aOutput, aIndentLevel + 1 );
303 aOutput->
Print( aIndentLevel,
")\n" );
312 for(
const auto& libName : libNames )
316 if( !row || !row->
plugin )
332 bool aPowerSymbolsOnly )
335 wxCHECK( row && row->
plugin, );
339 if( aPowerSymbolsOnly )
346 if( aPowerSymbolsOnly )
373 const wxString& aNickname,
bool aPowerSymbolsOnly )
377 if( !row || !row->
plugin )
380 std::lock_guard<std::mutex> lock( row->
GetMutex() );
384 if( aPowerSymbolsOnly )
392 if( aPowerSymbolsOnly )
401 LIB_ID id = symbol->GetLibId();
404 symbol->SetLibId(
id );
413 if( !row || !row->
plugin )
417 std::unique_lock<std::mutex> lock( row->
GetMutex(), std::try_to_lock );
419 if( !lock.owns_lock() )
436 SIM_MODEL::MigrateSimModel<LIB_SYMBOL, LIB_FIELD>( *symbol,
nullptr );
482 wxCHECK( row && row->
plugin, );
490 wxCHECK( row && row->
plugin,
false );
497 wxCHECK( row,
false );
505 wxCHECK( row && row->
plugin, );
513 wxCHECK( row && row->
plugin, );
523 if( nickname.size() )
533 for(
unsigned i = 0; i < nicks.size(); ++i )
550 return "KICAD7_SYMBOL_DIR";
558 const wxString& aPrefix ) :
563 wxFileName f( aPath,
"" );
567 wxDirTraverseResult
OnFile(
const wxString& aFilePath )
override
569 wxFileName file = wxFileName::FileName( aFilePath );
573 if( file.GetExt() == wxT(
"kicad_sym" ) && file.GetDirCount() >=
m_prefix_dir_count + 2 )
575 wxArrayString parts = file.GetDirs();
577 parts.Insert(
"${KICAD7_3RD_PARTY}", 0 );
578 parts.Add( file.GetFullName() );
580 wxString libPath = wxJoin( parts,
'/' );
584 wxString
name = parts.Last().substr( 0, parts.Last().length() - 10 );
599 _(
"Added by Plugin and Content Manager" ) ) );
603 return wxDIR_CONTINUE;
606 wxDirTraverseResult
OnDir(
const wxString& dirPath )
override {
return wxDIR_CONTINUE; }
618 bool tableExists =
true;
621 if( !fn.FileExists() )
625 if( !fn.DirExists() && !fn.Mkdir( 0x777, wxPATH_MKDIR_FULL ) )
627 THROW_IO_ERROR( wxString::Format(
_(
"Cannot create global library table path '%s'." ),
637 wxString templatePath =
638 Pgm().GetLocalEnvVariables().at( wxT(
"KICAD7_TEMPLATE_DIR" ) ).GetValue();
640 if( !templatePath.IsEmpty() )
646 if( fileName.IsEmpty() || !::wxCopyFile( fileName, fn.GetFullPath(),
false ) )
650 emptyTable.
Save( fn.GetFullPath() );
655 aTable.
Load( fn.GetFullPath() );
660 wxCHECK( settings,
false );
662 wxString packagesPath =
Pgm().GetLocalEnvVariables().at( wxT(
"KICAD7_3RD_PARTY" ) ).GetValue();
667 wxFileName d( packagesPath,
"" );
668 d.AppendDir(
"symbols" );
673 wxDir dir( d.GetPath() );
675 dir.Traverse( traverser );
682 std::vector<wxString> to_remove;
684 for(
size_t i = 0; i < aTable.
GetCount(); i++ )
689 if(
path.StartsWith( packagesPath ) && !wxFile::Exists(
path ) )
693 for(
const wxString& nickName : to_remove )
697 wxCHECK2( row,
continue );
714 for( i = 0; i <
m_rows.size(); ++i )
719 if( curr != curr_other )
734 return fn.GetFullPath();
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
A logical library item identifier and consists of various portions much like a URI.
int SetLibNickname(const UTF8 &aNickname)
Override the logical library name portion of the LIB_ID to aNickname.
const UTF8 & GetLibItemName() const
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Define a library symbol object.
LIB_ID GetLibId() const override
void SetLibId(const LIB_ID &aLibId)
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
const wxString & GetOptions() const
Return the options string, which may hold a password or anything else needed to instantiate the under...
LIB_TABLE * GetParent() const
void SetLoaded(bool aLoaded)
Mark the row as being a loaded library.
const wxString & GetNickName() const
const wxString GetFullURI(bool aSubstituted=false) const
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
bool operator==(const LIB_TABLE_ROW &r) const
void SetOptions(const wxString &aOptions)
Change the library options strings.
const STRING_UTF8_MAP * GetProperties() const
Return the constant #PROPERTIES for this library (LIB_TABLE_ROW).
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
int m_version
Versioning to handle importing old tables.
LIB_TABLE_ROW & At(unsigned aIndex)
Get the 'n'th LIB_TABLE_ROW object.
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
LIB_TABLE_ROWS m_rows
Owning set of rows.
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
bool HasLibraryWithPath(const wxString &aPath) const
Test for the existence of aPath in the library table.
bool RemoveRow(const LIB_TABLE_ROW *aRow)
Removes a row from the table and frees the pointer.
void Clear()
Delete all rows.
unsigned GetCount() const
Get the number of rows contained in the table.
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
LIB_TABLE_ROW * findRow(const wxString &aNickname, bool aCheckIfEnabled=false) const
Return a LIB_TABLE_ROW if aNickname is found in this table or in any chained fallBack table fragment,...
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
wxDirTraverseResult OnFile(const wxString &aFilePath) override
wxDirTraverseResult OnDir(const wxString &dirPath) override
SYMBOL_LIB_TABLE & m_lib_table
PCM_SYM_LIB_TRAVERSER(const wxString &aPath, SYMBOL_LIB_TABLE &aTable, const wxString &aPrefix)
size_t m_prefix_dir_count
A KiCad database library provides both symbol and footprint metadata, so there are "shim" plugins on ...
static SCH_FILE_T EnumFromStr(const wxString &aFileType)
Return the #SCH_FILE_T from the corresponding plugin type name: "kicad", "legacy",...
void set(SCH_PLUGIN *aPlugin)
Base class that schematic file and library loading and saving plugins should derive from.
virtual void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
virtual int GetModifyHash() const =0
Return the modification hash from the library cache.
virtual void SaveSymbol(const wxString &aLibraryPath, const LIB_SYMBOL *aSymbol, const STRING_UTF8_MAP *aProperties=nullptr)
Write aSymbol to an existing library located at aLibraryPath.
virtual void DeleteSymbol(const wxString &aLibraryPath, const wxString &aSymbolName, const STRING_UTF8_MAP *aProperties=nullptr)
Delete the entire LIB_SYMBOL associated with aAliasName from the library aLibraryPath.
virtual LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aPartName, const STRING_UTF8_MAP *aProperties=nullptr)
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
virtual void SetLibTable(SYMBOL_LIB_TABLE *aTable)
Some library plugins need to have access to their parent library table.
virtual void CreateSymbolLib(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
Create a new empty symbol library at aLibraryPath.
virtual bool DeleteSymbolLib(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
Delete an existing symbol library and returns true if successful, or if library does not exist return...
virtual void GetSubLibraryNames(std::vector< wxString > &aNames)
Retrieves a list of sub-libraries in this library.
virtual bool IsSymbolLibWritable(const wxString &aLibraryPath)
Return true if the library at aLibraryPath is writable.
Look for files in a number of paths.
void AddPaths(const wxString &aPaths, int aIndex=-1)
Insert or append path(s).
T * GetAppSettings()
Returns a handle to the a given settings by type If the settings have already been loaded,...
Hold a record identifying a symbol library accessed by the appropriate symbol library SCH_PLUGIN obje...
bool operator==(const SYMBOL_LIB_TABLE_ROW &aRow) const
void SetType(const wxString &aType) override
Change the schematic plugin type represented by this row.
void GetSubLibraryNames(std::vector< wxString > &aNames) const
bool Refresh() override
Attempt to reload the library.
void ShowSettingsDialog(wxWindow *aWindow) const override
void setPlugin(SCH_PLUGIN *aPlugin)
SCH_PLUGIN::SCH_PLUGIN_RELEASER plugin
void LoadSymbolLib(std::vector< LIB_SYMBOL * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
void DeleteSymbol(const wxString &aNickname, const wxString &aSymbolName)
Deletes the aSymbolName from the library given by aNickname.
static SYMBOL_LIB_TABLE & GetGlobalLibTable()
virtual void Parse(LIB_TABLE_LEXER *aLexer) override
Parse the #LIB_TABLE_LEXER s-expression library table format into the appropriate LIB_TABLE_ROW objec...
static const wxString & GetSymbolLibTableFileName()
SYMBOL_LIB_TABLE(SYMBOL_LIB_TABLE *aFallBackTable=nullptr)
Build a symbol library table by pre-pending this table fragment in front of aFallBackTable.
static const char * PropPowerSymsOnly
bool IsSymbolLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
bool IsSymbolLibLoaded(const wxString &aNickname)
Return true if the library given by aNickname was successfully loaded.
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const override
Generate the table in s-expression format to aOutput with an indentation level of aIndentLevel.
void DeleteSymbolLib(const wxString &aNickname)
void CreateSymbolLib(const wxString &aNickname)
static wxString GetGlobalTableFileName()
Fetch the global symbol library table file name.
void EnumerateSymbolLib(const wxString &aNickname, wxArrayString &aAliasNames, bool aPowerSymbolsOnly=false)
Return a list of symbol alias names contained within the library given by aNickname.
static bool LoadGlobalTable(SYMBOL_LIB_TABLE &aTable)
Load the global symbol library table into aTable.
static const wxString GlobalPathEnvVariableName()
Return the name of the environment variable used to hold the directory of locally installed "KiCad sp...
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
LIB_SYMBOL * LoadSymbolWithOptionalNickname(const LIB_ID &aId)
Load a LIB_SYMBOL having aFootprintId with possibly an empty library nickname.
static const char * PropNonPowerSymsOnly
bool operator==(const SYMBOL_LIB_TABLE &aOther) const
Compares this table against another.
SAVE_T SaveSymbol(const wxString &aNickname, const LIB_SYMBOL *aSymbol, bool aOverwrite=true)
Write aSymbol to an existing library given by aNickname.
SAVE_T
The set of return values from SaveSymbol() below.
SYMBOL_LIB_TABLE_ROW * FindRow(const wxString &aNickName, bool aCheckIfEnabled=false)
Return an SYMBOL_LIB_TABLE_ROW if aNickName is found in this table or in any chained fallBack table f...
static int m_modifyHash
helper for GetModifyHash()
static const wxChar global_tbl_name[]
#define THROW_IO_ERROR(msg)
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
SYMBOL_LIB_TABLE g_symbolLibraryTable
The global symbol library table.
void SystemDirsAppend(SEARCH_STACK *aSearchStack)
Append system places to aSearchStack in a platform specific way and pertinent to KiCad programs.
System directories search utilities.