29#include <lib_table_lexer.h>
46using namespace LIB_TABLE_T;
73 if(
type == SCH_IO_MGR::SCH_FILE_UNKNOWN )
74 type = SCH_IO_MGR::SCH_KICAD;
84 wxArrayString dummyList;
86 plugin.reset( SCH_IO_MGR::FindPlugin(
type ) );
103 plugin->GetSubLibraryNames( aNames );
110 return wxEmptyString;
112 return plugin->GetSubLibraryDescription( aName );
120 if(
type != SCH_IO_MGR::SCH_DATABASE )
149 if( in->CurTok() != T_sym_lib_table )
153 if( ( tok = in->NextTok() ) != T_sym_lib_table )
154 in->Expecting( T_sym_lib_table );
157 while( ( tok = in->NextTok() ) != T_RIGHT )
159 std::unique_ptr< SYMBOL_LIB_TABLE_ROW > row = std::make_unique<SYMBOL_LIB_TABLE_ROW>();
162 in->Expecting( T_RIGHT );
165 in->Expecting( T_LEFT );
168 int lineNum = in->CurLineNumber();
172 if( tok == T_version )
174 in->NeedNUMBER(
"version" );
181 in->Expecting( T_lib );
186 if( ( tok = in->NextTok() ) != T_name )
187 in->Expecting( T_name );
189 in->NeedSYMBOLorNUMBER();
191 row->SetNickName( in->FromUTF8() );
197 bool sawType =
false;
198 bool sawOpts =
false;
199 bool sawDesc =
false;
201 bool sawDisabled =
false;
202 bool sawHidden =
false;
204 while( ( tok = in->NextTok() ) != T_RIGHT )
207 in->Unexpected( T_EOF );
210 in->Expecting( T_LEFT );
212 tok = in->NeedSYMBOLorNUMBER();
218 in->Duplicate( tok );
220 in->NeedSYMBOLorNUMBER();
221 row->SetFullURI( in->FromUTF8() );
226 in->Duplicate( tok );
228 in->NeedSYMBOLorNUMBER();
229 row->SetType( in->FromUTF8() );
234 in->Duplicate( tok );
236 in->NeedSYMBOLorNUMBER();
237 row->SetOptions( in->FromUTF8() );
242 in->Duplicate( tok );
244 in->NeedSYMBOLorNUMBER();
245 row->SetDescr( in->FromUTF8() );
250 in->Duplicate( tok );
252 row->SetEnabled(
false );
257 in->Duplicate( tok );
259 row->SetVisible(
false );
263 in->Unexpected( tok );
270 in->Expecting( T_type );
273 in->Expecting( T_uri );
278 wxString nickname = row->GetNickName();
280 bool doReplace =
false;
287 wxString msg = wxString::Format(
_(
"Duplicate library nickname '%s' found in symbol "
288 "library table file line %d" ),
292 if( !errMsg.IsEmpty() )
299 if( !errMsg.IsEmpty() )
306 aOutput->
Print( aIndentLevel,
"(sym_lib_table\n" );
310 row.Format( aOutput, aIndentLevel + 1 );
312 aOutput->
Print( aIndentLevel,
")\n" );
321 for(
const auto& libName : libNames )
325 if( !row || !row->
plugin )
330 hash += row->
plugin->GetModifyHash();
340 bool aPowerSymbolsOnly )
343 wxCHECK( row && row->
plugin, );
347 if( aPowerSymbolsOnly )
354 if( aPowerSymbolsOnly )
373 row->
plugin->SetLibTable(
this );
380 const wxString& aNickname,
bool aPowerSymbolsOnly )
384 if( !row || !row->
plugin )
387 std::lock_guard<std::mutex> lock( row->
GetMutex() );
391 if( aPowerSymbolsOnly )
395 row->
plugin->SetLibTable(
this );
399 if( aPowerSymbolsOnly )
408 LIB_ID id = symbol->GetLibId();
411 symbol->SetLibId(
id );
420 if( !row || !row->
plugin )
424 std::unique_lock<std::mutex> lock( row->
GetMutex(), std::try_to_lock );
426 if( !lock.owns_lock() )
443 SIM_MODEL::MigrateSimModel<LIB_SYMBOL>( *symbol,
nullptr );
466 std::unique_ptr<LIB_SYMBOL> symbol( row->
plugin->LoadSymbol( row->
GetFullURI(
true ),
489 wxCHECK( row && row->
plugin, );
497 wxCHECK( row && row->
plugin,
false );
504 wxCHECK( row,
false );
512 wxCHECK( row && row->
plugin, );
520 wxCHECK( row && row->
plugin, );
530 if( nickname.size() )
540 for(
unsigned i = 0; i < nicks.size(); ++i )
565 const wxString& aPrefix ) :
570 wxFileName f( aPath,
"" );
574 wxDirTraverseResult
OnFile(
const wxString& aFilePath )
override
576 wxFileName file = wxFileName::FileName( aFilePath );
580 if( file.GetExt() == wxT(
"kicad_sym" ) && file.GetDirCount() >=
m_prefix_dir_count + 2 )
582 wxString versionedPath = wxString::Format( wxS(
"${%s}" ),
585 wxArrayString parts = file.GetDirs();
587 parts.Insert( versionedPath, 0 );
588 parts.Add( file.GetFullName() );
590 wxString libPath = wxJoin( parts,
'/' );
594 wxString
name = parts.Last().substr( 0, parts.Last().length() - 10 );
609 _(
"Added by Plugin and Content Manager" ) ),
false );
613 return wxDIR_CONTINUE;
616 wxDirTraverseResult
OnDir(
const wxString& dirPath )
override {
return wxDIR_CONTINUE; }
628 bool tableExists =
true;
631 if( !fn.FileExists() )
635 if( !fn.DirExists() && !fn.Mkdir( 0x777, wxPATH_MKDIR_FULL ) )
637 THROW_IO_ERROR( wxString::Format(
_(
"Cannot create global library table path '%s'." ),
649 wxT(
"TEMPLATE_DIR" ) );
651 if( v && !v->IsEmpty() )
657 if( fileName.IsEmpty() || !::wxCopyFile( fileName, fn.GetFullPath(),
false ) )
661 emptyTable.
Save( fn.GetFullPath() );
665 aTable.
Load( fn.GetFullPath() );
670 wxCHECK( settings,
false );
672 wxString packagesPath;
681 wxFileName d( packagesPath,
"" );
682 d.AppendDir(
"symbols" );
687 wxDir dir( d.GetPath() );
689 dir.Traverse( traverser );
696 std::vector<wxString> to_remove;
698 for(
size_t i = 0; i < aTable.
GetCount(); i++ )
703 if(
path.StartsWith( packagesPath ) && !wxFile::Exists(
path ) )
707 for(
const wxString& nickName : to_remove )
711 wxCHECK2( row,
continue );
728 for( i = 0; i <
m_rows.size(); ++i )
733 if( curr != curr_other )
748 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 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
const UTF8 & GetLibItemName() const
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Define a library symbol object.
const 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
const std::map< std::string, UTF8 > * GetProperties() const
Return the constant #PROPERTIES for this library (LIB_TABLE_ROW).
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.
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.
bool doInsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Performs the mechanics of inserting a row, but without locking or reindexing.
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.
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
virtual ENV_VAR_MAP & GetLocalEnvVariables() const
virtual SETTINGS_MANAGER & GetSettingsManager() const
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",...
Look for files in a number of paths.
void AddPaths(const wxString &aPaths, int aIndex=-1)
Insert or append path(s).
T * GetAppSettings(const wxString &aFilename)
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_IO object i...
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 setPlugin(SCH_IO *aPlugin)
void ShowSettingsDialog(wxWindow *aWindow) const override
wxString GetSubLibraryDescription(const wxString &aName) const
IO_RELEASER< SCH_IO > 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[]
Functions related to environment variables, including help functions.
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
#define THROW_IO_ERROR(msg)
KICOMMON_API std::optional< wxString > GetVersionedEnvVarValue(const std::map< wxString, ENV_VAR_ITEM > &aMap, const wxString &aBaseName)
Attempts to retrieve the value of a versioned environment variable, such as KICAD8_TEMPLATE_DIR.
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Constructs a versioned environment variable based on this KiCad major version.
PGM_BASE & Pgm()
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.