29#include <lib_table_lexer.h>
46using namespace LIB_TABLE_T;
69 if(
type == SCH_IO_MGR::SCH_FILE_UNKNOWN )
70 type = SCH_IO_MGR::SCH_KICAD;
80 wxArrayString dummyList;
82 plugin.reset( SCH_IO_MGR::FindPlugin(
type ) );
99 plugin->GetSubLibraryNames( aNames );
106 return wxEmptyString;
108 return plugin->GetSubLibraryDescription( aName );
116 if(
type != SCH_IO_MGR::SCH_DATABASE )
145 if( in->CurTok() != T_sym_lib_table )
149 if( ( tok = in->NextTok() ) != T_sym_lib_table )
150 in->Expecting( T_sym_lib_table );
153 while( ( tok = in->NextTok() ) != T_RIGHT )
155 std::unique_ptr< SYMBOL_LIB_TABLE_ROW > row = std::make_unique<SYMBOL_LIB_TABLE_ROW>();
158 in->Expecting( T_RIGHT );
161 in->Expecting( T_LEFT );
164 int lineNum = in->CurLineNumber();
168 if( tok == T_version )
170 in->NeedNUMBER(
"version" );
177 in->Expecting( T_lib );
182 if( ( tok = in->NextTok() ) != T_name )
183 in->Expecting( T_name );
185 in->NeedSYMBOLorNUMBER();
187 row->SetNickName( in->FromUTF8() );
193 bool sawType =
false;
194 bool sawOpts =
false;
195 bool sawDesc =
false;
197 bool sawDisabled =
false;
198 bool sawHidden =
false;
200 while( ( tok = in->NextTok() ) != T_RIGHT )
203 in->Unexpected( T_EOF );
206 in->Expecting( T_LEFT );
208 tok = in->NeedSYMBOLorNUMBER();
214 in->Duplicate( tok );
216 in->NeedSYMBOLorNUMBER();
217 row->SetFullURI( in->FromUTF8() );
222 in->Duplicate( tok );
224 in->NeedSYMBOLorNUMBER();
225 row->SetType( in->FromUTF8() );
230 in->Duplicate( tok );
232 in->NeedSYMBOLorNUMBER();
233 row->SetOptions( in->FromUTF8() );
238 in->Duplicate( tok );
240 in->NeedSYMBOLorNUMBER();
241 row->SetDescr( in->FromUTF8() );
246 in->Duplicate( tok );
248 row->SetEnabled(
false );
253 in->Duplicate( tok );
255 row->SetVisible(
false );
259 in->Unexpected( tok );
266 in->Expecting( T_type );
269 in->Expecting( T_uri );
274 wxString nickname = row->GetNickName();
276 bool doReplace =
false;
283 wxString msg = wxString::Format(
_(
"Duplicate library nickname '%s' found in symbol "
284 "library table file line %d" ),
288 if( !errMsg.IsEmpty() )
295 if( !errMsg.IsEmpty() )
302 aOutput->
Print( aIndentLevel,
"(sym_lib_table\n" );
306 row.Format( aOutput, aIndentLevel + 1 );
308 aOutput->
Print( aIndentLevel,
")\n" );
317 for(
const auto& libName : libNames )
321 if( !row || !row->
plugin )
326 hash += row->
plugin->GetModifyHash();
336 bool aPowerSymbolsOnly )
339 wxCHECK( row && row->
plugin, );
343 if( aPowerSymbolsOnly )
350 if( aPowerSymbolsOnly )
369 row->
plugin->SetLibTable(
this );
376 const wxString& aNickname,
bool aPowerSymbolsOnly )
380 if( !row || !row->
plugin )
383 std::lock_guard<std::mutex> lock( row->
GetMutex() );
387 if( aPowerSymbolsOnly )
391 row->
plugin->SetLibTable(
this );
395 if( aPowerSymbolsOnly )
404 LIB_ID id = symbol->GetLibId();
407 symbol->SetLibId(
id );
416 if( !row || !row->
plugin )
420 std::unique_lock<std::mutex> lock( row->
GetMutex(), std::try_to_lock );
422 if( !lock.owns_lock() )
439 SIM_MODEL::MigrateSimModel<LIB_SYMBOL>( *symbol,
nullptr );
462 std::unique_ptr<LIB_SYMBOL> symbol( row->
plugin->LoadSymbol( row->
GetFullURI(
true ),
485 wxCHECK( row && row->
plugin, );
493 wxCHECK( row && row->
plugin,
false );
500 wxCHECK( row,
false );
508 wxCHECK( row && row->
plugin, );
516 wxCHECK( row && row->
plugin, );
526 if( nickname.size() )
536 for(
unsigned i = 0; i < nicks.size(); ++i )
561 const wxString& aPrefix ) :
566 wxFileName f( aPath,
"" );
570 wxDirTraverseResult
OnFile(
const wxString& aFilePath )
override
572 wxFileName file = wxFileName::FileName( aFilePath );
576 if( file.GetExt() == wxT(
"kicad_sym" ) && file.GetDirCount() >=
m_prefix_dir_count + 2 )
578 wxString versionedPath = wxString::Format( wxS(
"${%s}" ),
581 wxArrayString parts = file.GetDirs();
583 parts.Insert( versionedPath, 0 );
584 parts.Add( file.GetFullName() );
586 wxString libPath = wxJoin( parts,
'/' );
590 wxString
name = parts.Last().substr( 0, parts.Last().length() - 10 );
605 _(
"Added by Plugin and Content Manager" ) ),
false );
609 return wxDIR_CONTINUE;
612 wxDirTraverseResult
OnDir(
const wxString& dirPath )
override {
return wxDIR_CONTINUE; }
624 bool tableExists =
true;
627 if( !fn.FileExists() )
631 if( !fn.DirExists() && !fn.Mkdir( 0x777, wxPATH_MKDIR_FULL ) )
633 THROW_IO_ERROR( wxString::Format(
_(
"Cannot create global library table path '%s'." ),
645 wxT(
"TEMPLATE_DIR" ) );
647 if( v && !v->IsEmpty() )
653 if( fileName.IsEmpty() || !::wxCopyFile( fileName, fn.GetFullPath(),
false ) )
657 emptyTable.
Save( fn.GetFullPath() );
661 aTable.
Load( fn.GetFullPath() );
666 wxCHECK( settings,
false );
668 wxString packagesPath;
677 wxFileName d( packagesPath,
"" );
678 d.AppendDir(
"symbols" );
683 wxDir dir( d.GetPath() );
685 dir.Traverse( traverser );
692 std::vector<wxString> to_remove;
694 for(
size_t i = 0; i < aTable.
GetCount(); i++ )
699 if(
path.StartsWith( packagesPath ) && !wxFile::Exists(
path ) )
703 for(
const wxString& nickName : to_remove )
707 wxCHECK2( row,
continue );
724 for( i = 0; i <
m_rows.size(); ++i )
729 if( curr != curr_other )
744 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)
Return a handle to the a given settings by type.
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()
Functions related to environment variables, including help functions.
static const std::string SymbolLibraryTableFileName
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)
Attempt to retrieve the value of a versioned environment variable, such as KICAD8_TEMPLATE_DIR.
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Construct 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.