28#include <lib_table_lexer.h>
42using namespace LIB_TABLE_T;
69 if( SCH_IO_MGR::SCH_FILE_T( -1 ) ==
type )
70 type = SCH_IO_MGR::SCH_LEGACY;
80 wxArrayString dummyList;
124 if( in->CurTok() != T_sym_lib_table )
128 if( ( tok = in->NextTok() ) != T_sym_lib_table )
129 in->Expecting( T_sym_lib_table );
132 while( ( tok = in->NextTok() ) != T_RIGHT )
134 std::unique_ptr< SYMBOL_LIB_TABLE_ROW > row = std::make_unique<SYMBOL_LIB_TABLE_ROW>();
137 in->Expecting( T_RIGHT );
140 in->Expecting( T_LEFT );
143 int lineNum = in->CurLineNumber();
147 if( tok == T_version )
149 in->NeedNUMBER(
"version" );
156 in->Expecting( T_lib );
161 if( ( tok = in->NextTok() ) != T_name )
162 in->Expecting( T_name );
164 in->NeedSYMBOLorNUMBER();
166 row->SetNickName( in->FromUTF8() );
172 bool sawType =
false;
173 bool sawOpts =
false;
174 bool sawDesc =
false;
176 bool sawDisabled =
false;
177 bool sawHidden =
false;
179 while( ( tok = in->NextTok() ) != T_RIGHT )
182 in->Unexpected( T_EOF );
185 in->Expecting( T_LEFT );
187 tok = in->NeedSYMBOLorNUMBER();
193 in->Duplicate( tok );
195 in->NeedSYMBOLorNUMBER();
196 row->SetFullURI( in->FromUTF8() );
201 in->Duplicate( tok );
203 in->NeedSYMBOLorNUMBER();
204 row->SetType( in->FromUTF8() );
209 in->Duplicate( tok );
211 in->NeedSYMBOLorNUMBER();
212 row->SetOptions( in->FromUTF8() );
217 in->Duplicate( tok );
219 in->NeedSYMBOLorNUMBER();
220 row->SetDescr( in->FromUTF8() );
225 in->Duplicate( tok );
227 row->SetEnabled(
false );
232 in->Duplicate( tok );
234 row->SetVisible(
false );
238 in->Unexpected( tok );
245 in->Expecting( T_type );
248 in->Expecting( T_uri );
254 wxString nickname = row->GetNickName();
262 wxString msg =
wxString::Format(
_(
"Duplicate library nickname '%s' found in symbol "
263 "library table file line %d" ),
267 if( !errMsg.IsEmpty() )
274 if( !errMsg.IsEmpty() )
281 aOutput->
Print( aIndentLevel,
"(sym_lib_table\n" );
286 it->Format( aOutput, aIndentLevel+1 );
289 aOutput->
Print( aIndentLevel,
")\n" );
298 for(
const auto& libName : libNames )
302 if( !row || !row->
plugin )
318 bool aPowerSymbolsOnly )
321 wxCHECK( row && row->
plugin, );
325 if( aPowerSymbolsOnly )
332 if( aPowerSymbolsOnly )
359 const wxString& aNickname,
bool aPowerSymbolsOnly )
362 wxCHECK( row && row->
plugin, );
364 std::lock_guard<std::mutex> lock( row->
GetMutex() );
368 if( aPowerSymbolsOnly )
376 if( aPowerSymbolsOnly )
385 LIB_ID id = symbol->GetLibId();
388 symbol->SetLibId(
id );
397 if( !row || !row->
plugin )
401 std::unique_lock<std::mutex> lock( row->
GetMutex(), std::try_to_lock );
403 if( !lock.owns_lock() )
420 SIM_MODEL::MigrateSimModel<LIB_SYMBOL, LIB_FIELD>( *symbol,
nullptr );
466 wxCHECK( row && row->
plugin, );
474 wxCHECK( row && row->
plugin,
false );
481 wxCHECK( row,
false );
489 wxCHECK( row && row->
plugin, );
497 wxCHECK( row && row->
plugin, );
507 if( nickname.size() )
517 for(
unsigned i = 0; i < nicks.size(); ++i )
534 return "KICAD7_SYMBOL_DIR";
542 const wxString& aPrefix ) :
547 wxFileName f( aPath,
"" );
551 wxDirTraverseResult
OnFile(
const wxString& aFilePath )
override
553 wxFileName file = wxFileName::FileName( aFilePath );
557 if( file.GetExt() == wxT(
"kicad_sym" ) && file.GetDirCount() >=
m_prefix_dir_count + 2 )
559 wxArrayString parts = file.GetDirs();
561 parts.Insert(
"${KICAD7_3RD_PARTY}", 0 );
562 parts.Add( file.GetFullName() );
564 wxString libPath = wxJoin( parts,
'/' );
568 wxString
name = parts.Last().substr( 0, parts.Last().length() - 10 );
583 _(
"Added by Plugin and Content Manager" ) ) );
587 return wxDIR_CONTINUE;
590 wxDirTraverseResult
OnDir(
const wxString& dirPath )
override {
return wxDIR_CONTINUE; }
602 bool tableExists =
true;
605 if( !fn.FileExists() )
609 if( !fn.DirExists() && !fn.Mkdir( 0x777, wxPATH_MKDIR_FULL ) )
621 wxString templatePath =
622 Pgm().GetLocalEnvVariables().at( wxT(
"KICAD7_TEMPLATE_DIR" ) ).GetValue();
624 if( !templatePath.IsEmpty() )
630 if( fileName.IsEmpty() || !::wxCopyFile( fileName, fn.GetFullPath(),
false ) )
634 emptyTable.
Save( fn.GetFullPath() );
639 aTable.
Load( fn.GetFullPath() );
644 wxString packagesPath =
Pgm().GetLocalEnvVariables().at( wxT(
"KICAD7_3RD_PARTY" ) ).GetValue();
649 wxFileName d( packagesPath,
"" );
650 d.AppendDir(
"symbols" );
655 wxDir dir( d.GetPath() );
657 dir.Traverse( traverser );
664 std::vector<wxString> to_remove;
666 for(
size_t i = 0; i < aTable.
GetCount(); i++ )
671 if(
path.StartsWith( packagesPath ) && !wxFile::Exists(
path ) )
675 for(
const wxString& nickName : to_remove )
690 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.
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.
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,...
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
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)
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(bool aLoadNow=true)
Returns a handle to the a given settings by type If the settings have already been loaded,...
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
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
void setPlugin(SCH_PLUGIN *aPlugin)
bool Refresh()
Attempt to reload the library.
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
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()
#define THROW_IO_ERROR(msg)
LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
SYMBOL_LIB_TABLE g_symbolLibraryTable
The global symbol library table.
static const wxString global_tbl_name("sym-lib-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.