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 );
111 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 )
321 hash += row->
plugin->GetModifyHash();
331 bool aPowerSymbolsOnly )
334 wxCHECK( row && row->
plugin, );
338 if( aPowerSymbolsOnly )
345 if( aPowerSymbolsOnly )
364 row->
plugin->SetLibTable(
this );
372 const wxString& aNickname,
bool aPowerSymbolsOnly )
376 if( !row || !row->
plugin )
379 std::lock_guard<std::mutex> lock( row->
GetMutex() );
383 if( aPowerSymbolsOnly )
387 row->
plugin->SetLibTable(
this );
391 if( aPowerSymbolsOnly )
400 LIB_ID id = symbol->GetLibId();
403 symbol->SetLibId(
id );
412 if( !row || !row->
plugin )
416 std::unique_lock<std::mutex> lock( row->
GetMutex(), std::try_to_lock );
418 if( !lock.owns_lock() )
435 SIM_MODEL::MigrateSimModel<LIB_SYMBOL, LIB_FIELD>( *symbol,
nullptr );
458 std::unique_ptr<LIB_SYMBOL> symbol( row->
plugin->LoadSymbol( row->
GetFullURI(
true ),
481 wxCHECK( row && row->
plugin, );
489 wxCHECK( row && row->
plugin,
false );
496 wxCHECK( row,
false );
504 wxCHECK( row && row->
plugin, );
512 wxCHECK( row && row->
plugin, );
522 if( nickname.size() )
532 for(
unsigned i = 0; i < nicks.size(); ++i )
557 const wxString& aPrefix ) :
562 wxFileName f( aPath,
"" );
566 wxDirTraverseResult
OnFile(
const wxString& aFilePath )
override
568 wxFileName file = wxFileName::FileName( aFilePath );
572 if( file.GetExt() == wxT(
"kicad_sym" ) && file.GetDirCount() >=
m_prefix_dir_count + 2 )
574 wxString versionedPath = wxString::Format( wxS(
"${%s}" ),
577 wxArrayString parts = file.GetDirs();
579 parts.Insert( versionedPath, 0 );
580 parts.Add( file.GetFullName() );
582 wxString libPath = wxJoin( parts,
'/' );
586 wxString
name = parts.Last().substr( 0, parts.Last().length() - 10 );
601 _(
"Added by Plugin and Content Manager" ) ) );
605 return wxDIR_CONTINUE;
608 wxDirTraverseResult
OnDir(
const wxString& dirPath )
override {
return wxDIR_CONTINUE; }
620 bool tableExists =
true;
623 if( !fn.FileExists() )
627 if( !fn.DirExists() && !fn.Mkdir( 0x777, wxPATH_MKDIR_FULL ) )
629 THROW_IO_ERROR( wxString::Format(
_(
"Cannot create global library table path '%s'." ),
641 wxT(
"TEMPLATE_DIR" ) );
643 if( v && !v->IsEmpty() )
649 if( fileName.IsEmpty() || !::wxCopyFile( fileName, fn.GetFullPath(),
false ) )
653 emptyTable.
Save( fn.GetFullPath() );
658 aTable.
Load( fn.GetFullPath() );
663 wxCHECK( settings,
false );
665 wxString packagesPath;
674 wxFileName d( packagesPath,
"" );
675 d.AppendDir(
"symbols" );
680 wxDir dir( d.GetPath() );
682 dir.Traverse( traverser );
689 std::vector<wxString> to_remove;
691 for(
size_t i = 0; i < aTable.
GetCount(); i++ )
696 if(
path.StartsWith( packagesPath ) && !wxFile::Exists(
path ) )
700 for(
const wxString& nickName : to_remove )
704 wxCHECK2( row,
continue );
721 for( i = 0; i <
m_rows.size(); ++i )
726 if( curr != curr_other )
741 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.
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",...
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_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
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()
Functions related to environment variables, including help functions.
static const wxChar global_tbl_name[]
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
#define THROW_IO_ERROR(msg)
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.
wxString GetVersionedEnvVarName(const wxString &aBaseName)
Constructs a versioned environment variable based on this KiCad major version.
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.