KiCad PCB EDA Suite
LIB_TABLE Class Referenceabstract

Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname. More...

#include <lib_table_base.h>

Inheritance diagram for LIB_TABLE:
PROJECT::_ELEM FP_LIB_TABLE SYMBOL_LIB_TABLE TEST_LIB_TABLE FP_LIB_TABLE_GRID SYMBOL_LIB_TABLE_GRID

Public Member Functions

virtual void Parse (LIB_TABLE_LEXER *aLexer)=0
 Parse the #LIB_TABLE_LEXER s-expression library table format into the appropriate LIB_TABLE_ROW objects. More...
 
virtual void Format (OUTPUTFORMATTER *aOutput, int aIndentLevel) const =0
 Generate the table in s-expression format to aOutput with an indentation level of aIndentLevel. More...
 
 LIB_TABLE (LIB_TABLE *aFallBackTable=nullptr)
 Build a library table by pre-pending this table fragment in front of aFallBackTable. More...
 
virtual ~LIB_TABLE ()
 
void Clear ()
 Delete all rows. More...
 
bool operator== (const LIB_TABLE &r) const
 Compares this table against another. More...
 
bool operator!= (const LIB_TABLE &r) const
 
unsigned GetCount () const
 Get the number of rows contained in the table. More...
 
LIB_TABLE_ROWAt (unsigned aIndex)
 Get the 'n'th LIB_TABLE_ROW object. More...
 
const LIB_TABLE_ROWAt (unsigned aIndex) const
 Get the 'n'th LIB_TABLE_ROW object. More...
 
bool IsEmpty (bool aIncludeFallback=true)
 Return true if the table is empty. More...
 
const wxString GetDescription (const wxString &aNickname)
 
bool HasLibrary (const wxString &aNickname, bool aCheckEnabled=false) const
 Test for the existence of aNickname in the library table. More...
 
bool HasLibraryWithPath (const wxString &aPath) const
 Test for the existence of aPath in the library table. More...
 
std::vector< wxString > GetLogicalLibs ()
 Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE. More...
 
wxString GetFullURI (const wxString &aLibNickname, bool aExpandEnvVars=true) const
 Return the full URI of the library mapped to aLibNickname. More...
 
bool InsertRow (LIB_TABLE_ROW *aRow, bool doReplace=false)
 Adds aRow if it does not already exist or if doReplace is true. More...
 
bool RemoveRow (const LIB_TABLE_ROW *aRow)
 Removes a row from the table. More...
 
const LIB_TABLE_ROWFindRowByURI (const wxString &aURI)
 
void Load (const wxString &aFileName)
 Load the library table using the path defined by aFileName aFallBackTable. More...
 
void Save (const wxString &aFileName) const
 Write this library table to aFileName in s-expression form. More...
 
int GetVersion () const
 Returns the version number (0 if unset) More...
 
virtual KICAD_T Type ()=0
 

Static Public Member Functions

static STRING_UTF8_MAPParseOptions (const std::string &aOptionsList)
 Parses aOptionsList and places the result into a #PROPERTIES object which is returned. More...
 
static UTF8 FormatOptions (const STRING_UTF8_MAP *aProperties)
 Returns a list of options from the aProperties parameter. More...
 

Protected Types

typedef std::map< wxString, int > INDEX
 this is a non-owning index into the LIB_TABLE_ROWS table More...
 
typedef INDEX::iterator INDEX_ITER
 
typedef INDEX::const_iterator INDEX_CITER
 
typedef INDEX::value_type INDEX_VALUE
 

Protected Member Functions

LIB_TABLE_ROWfindRow (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, else NULL. More...
 
bool migrate ()
 Updates the env vars from older version of KiCad, provided they do not currently resolve to anything. More...
 
void reindex (bool aForce)
 Rebuilds the m_nickIndex. More...
 
void ensureIndex ()
 

Protected Attributes

LIB_TABLE_ROWS m_rows
 
INDEX m_nickIndex
 this particular key is the nickName within each row. More...
 
LIB_TABLEm_fallBack
 
int m_version
 Versioning to handle importing old tables. More...
 
std::shared_mutex m_nickIndexMutex
 Mutex to protect access to the nickIndex variable. More...
 

Friends

class PANEL_FP_LIB_TABLE
 
class LIB_TABLE_GRID
 

Detailed Description

Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.

This class owns the library table, which is like fstab in concept and maps logical library name to the library URI, type, and options. It is heavily based on the SWEET parser work done by Dick Hollenbeck and can be seen in new/sch_lib_table.h. A library table has the following columns:

  • Logical Library Name (Nickname)
  • Library Type, used to determine which plugin to load to access the library.
  • Library URI. The full URI to the library source, form dependent on Type.
  • Options, used for as yet to be defined information such as user names or passwords

The Library Type can be one of:

  • "file"
  • "ftp"
  • "http"

For now, the Library URI types needed to support the various types can be one of those shown below, which are typical of each type:

  • "file://C:/mylibdir"
  • "ftp://kicad.org/partlib/trunk"
  • "http://kicad.org/partlib"

The library table is built up from several additive entries (table fragments), and the final table is a (conceptual) merging of the table fragments. Two anticipated sources of the entries are a personal table saved in the KiCad configuration and a project resident table that resides in project file. The project footprint table entries are considered a higher priority in the final dynamically assembled library table. An row in the project file contribution to the library table takes precedence over the personal table if there is a collision of logical library names. Otherwise, the entries simply combine without issue to make up the applicable library table.

Author
Wayne Stambaugh

Definition at line 289 of file lib_table_base.h.

Member Typedef Documentation

◆ INDEX

typedef std::map<wxString,int> LIB_TABLE::INDEX
protected

this is a non-owning index into the LIB_TABLE_ROWS table

Definition at line 582 of file lib_table_base.h.

◆ INDEX_CITER

typedef INDEX::const_iterator LIB_TABLE::INDEX_CITER
protected

Definition at line 584 of file lib_table_base.h.

◆ INDEX_ITER

typedef INDEX::iterator LIB_TABLE::INDEX_ITER
protected

Definition at line 583 of file lib_table_base.h.

◆ INDEX_VALUE

typedef INDEX::value_type LIB_TABLE::INDEX_VALUE
protected

Definition at line 585 of file lib_table_base.h.

Constructor & Destructor Documentation

◆ LIB_TABLE()

LIB_TABLE::LIB_TABLE ( LIB_TABLE aFallBackTable = nullptr)

Build a library table by pre-pending this table fragment in front of aFallBackTable.

Loading of this table fragment is done by using Parse().

Parameters
aFallBackTableis another LIB_TABLE which is searched only when a row is not found in this table. No ownership is taken of aFallBackTable.

Definition at line 116 of file lib_table_base.cpp.

116 :
117 m_fallBack( aFallBackTable ), m_version( 0 )
118{
119 // not copying fall back, simply search aFallBackTable separately
120 // if "nickName not found".
121}
int m_version
Versioning to handle importing old tables.
LIB_TABLE * m_fallBack

◆ ~LIB_TABLE()

LIB_TABLE::~LIB_TABLE ( )
virtual

Definition at line 124 of file lib_table_base.cpp.

125{
126 // *fallBack is not owned here.
127}

Member Function Documentation

◆ At() [1/2]

LIB_TABLE_ROW & LIB_TABLE::At ( unsigned  aIndex)
inline

Get the 'n'th LIB_TABLE_ROW object.

Parameters
aIndexindex of row (must exist: from 0 to GetCount() - 1)
Returns
reference to the row

Definition at line 376 of file lib_table_base.h.

377 {
378 return m_rows[aIndex];
379 }
LIB_TABLE_ROWS m_rows

References m_rows.

Referenced by FP_LIB_TABLE::LoadGlobalTable(), SYMBOL_LIB_TABLE::LoadGlobalTable(), SYMBOL_GRID_TRICKS::paste_text(), FP_GRID_TRICKS::paste_text(), and PCB::IFACE::SaveFileAs().

◆ At() [2/2]

const LIB_TABLE_ROW & LIB_TABLE::At ( unsigned  aIndex) const
inline

Get the 'n'th LIB_TABLE_ROW object.

Parameters
aIndexindex of row (must exist: from 0 to GetCount() - 1)
Returns
reference to the row

Definition at line 384 of file lib_table_base.h.

385 {
386 return m_rows[aIndex];
387 }

References m_rows.

◆ Clear()

void LIB_TABLE::Clear ( )
inline

Delete all rows.

Definition at line 331 of file lib_table_base.h.

332 {
333 std::lock_guard<std::shared_mutex> lock( m_nickIndexMutex );
334
335 m_rows.clear();
336 m_nickIndex.clear();
337 }
INDEX m_nickIndex
this particular key is the nickName within each row.
std::shared_mutex m_nickIndexMutex
Mutex to protect access to the nickIndex variable.

References m_nickIndex, m_nickIndexMutex, and m_rows.

Referenced by FP_LIB_TABLE::LoadGlobalTable(), SYMBOL_LIB_TABLE::LoadGlobalTable(), PANEL_SYM_LIB_TABLE::TransferDataFromWindow(), and PANEL_FP_LIB_TABLE::TransferDataFromWindow().

◆ ensureIndex()

void LIB_TABLE::ensureIndex ( )
inlineprotected

Definition at line 565 of file lib_table_base.h.

566 {
567 // The dialog lib table editor may not maintain the nickIndex.
568 // Lazy indexing may be required. To handle lazy indexing, we must enforce
569 // that "nickIndex" is either empty or accurate, but never inaccurate.
570 if( !m_nickIndex.size() )
571 reindex( false );
572 }
void reindex(bool aForce)
Rebuilds the m_nickIndex.

References m_nickIndex, and reindex().

Referenced by findRow(), FindRowByURI(), and InsertRow().

◆ findRow()

LIB_TABLE_ROW * LIB_TABLE::findRow ( const wxString &  aNickname,
bool  aCheckIfEnabled = false 
) const
protected

Return a LIB_TABLE_ROW if aNickname is found in this table or in any chained fallBack table fragment, else NULL.

Parameters
aNicknameis the name of the library table entry to find.
aCheckIfEnabledis a flag to check if the library table entry is enabled.
Returns
a pointer to the LIB_TABLE_ROW found.

Definition at line 187 of file lib_table_base.cpp.

188{
189 LIB_TABLE_ROW* row = nullptr;
190 LIB_TABLE* cur = (LIB_TABLE*) this;
191
192 do
193 {
194 cur->ensureIndex();
195
196 std::shared_lock<std::shared_mutex> lock( cur->m_nickIndexMutex );
197
198 for( const std::pair<const wxString, int>& entry : cur->m_nickIndex )
199 {
200 if( entry.first == aNickName )
201 {
202 row = &cur->m_rows[entry.second];
203
204 if( !aCheckIfEnabled || row->GetIsEnabled() )
205 return row;
206 }
207 }
208
209 // Repeat, this time looking for names that were "fixed" by legacy versions because
210 // the old eeschema file format didn't support spaces in tokens.
211 for( const std::pair<const wxString, int>& entry : cur->m_nickIndex )
212 {
213 wxString legacyLibName = entry.first;
214 legacyLibName.Replace( " ", "_" );
215
216 if( legacyLibName == aNickName )
217 {
218 row = &cur->m_rows[entry.second];
219
220 if( !aCheckIfEnabled || row->GetIsEnabled() )
221 return row;
222 }
223 }
224
225 // not found, search fall back table(s), if any
226 } while( ( cur = cur->m_fallBack ) != nullptr );
227
228 return nullptr; // not found
229}
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
bool GetIsEnabled() const
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.
void ensureIndex()

References ensureIndex(), LIB_TABLE_ROW::GetIsEnabled(), m_fallBack, m_nickIndex, m_nickIndexMutex, and m_rows.

Referenced by SYMBOL_LIB_TABLE::FindRow(), FP_LIB_TABLE::FindRow(), GetDescription(), GetFullURI(), and HasLibrary().

◆ FindRowByURI()

const LIB_TABLE_ROW * LIB_TABLE::FindRowByURI ( const wxString &  aURI)
Returns
a LIB_TABLE_ROW pointer if aURI is found in this table or in any chained fallBack table fragments, else NULL.

Definition at line 232 of file lib_table_base.cpp.

233{
234 LIB_TABLE* cur = this;
235
236 do
237 {
238 cur->ensureIndex();
239
240 for( unsigned i = 0; i < cur->m_rows.size(); i++ )
241 {
242 wxString tmp = cur->m_rows[i].GetFullURI( true );
243
244 if( tmp.Find( "://" ) != wxNOT_FOUND )
245 {
246 if( tmp == aURI )
247 return &cur->m_rows[i]; // found as URI
248 }
249 else
250 {
251 wxFileName fn = aURI;
252
253 // This will also test if the file is a symlink so if we are comparing
254 // a symlink to the same real file, the comparison will be true. See
255 // wxFileName::SameAs() in the wxWidgets source.
256 if( fn == wxFileName( tmp ) )
257 return &cur->m_rows[i]; // found as full path and file name
258 }
259 }
260
261 // not found, search fall back table(s), if any
262 } while( ( cur = cur->m_fallBack ) != nullptr );
263
264 return nullptr; // not found
265}

References ensureIndex(), m_fallBack, and m_rows.

Referenced by PCB_EDIT_FRAME::ExportFootprintsToLibrary(), SYMBOL_EDIT_FRAME::KiwayMailIn(), and FOOTPRINT_EDIT_FRAME::KiwayMailIn().

◆ Format()

virtual void LIB_TABLE::Format ( OUTPUTFORMATTER aOutput,
int  aIndentLevel 
) const
pure virtual

Generate the table in s-expression format to aOutput with an indentation level of aIndentLevel.

Parameters
aOutputis the OUTPUTFORMATTER to format the table into.
aIndentLevelis the indentation level (2 spaces) to indent.
Exceptions
IO_ERRORif an I/O error occurs during output.
boost::interprocess::lock_exceptif separate process attempt to access the table.

Implemented in SYMBOL_LIB_TABLE, FP_LIB_TABLE, and TEST_LIB_TABLE.

Referenced by Save().

◆ FormatOptions()

UTF8 LIB_TABLE::FormatOptions ( const STRING_UTF8_MAP aProperties)
static

Returns a list of options from the aProperties parameter.

The name=value pairs will be separated with the '|' character. The =value portion may not be present. You might expect something like "name1=value1|name2=value2|flag_me". Notice that flag_me does not have a value. This is ok.

Parameters
aPropertiesis the PROPERTIES to format or NULL. If NULL the returned string will be empty.

Definition at line 447 of file lib_table_base.cpp.

448{
449 UTF8 ret;
450
451 if( aProperties )
452 {
453 for( STRING_UTF8_MAP::const_iterator it = aProperties->begin(); it != aProperties->end(); ++it )
454 {
455 const std::string& name = it->first;
456
457 const UTF8& value = it->second;
458
459 if( ret.size() )
460 ret += OPT_SEP;
461
462 ret += name;
463
464 // the separation between name and value is '='
465 if( value.size() )
466 {
467 ret += '=';
468
469 for( std::string::const_iterator si = value.begin(); si != value.end(); ++si )
470 {
471 // escape any separator in the value.
472 if( *si == OPT_SEP )
473 ret += '\\';
474
475 ret += *si;
476 }
477 }
478 }
479 }
480
481 return ret;
482}
const char * name
Definition: DXF_plotter.cpp:56
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:71
std::string::const_iterator begin() const
Definition: utf8.h:192
std::string::size_type size() const
Definition: utf8.h:110
std::string::const_iterator end() const
Definition: utf8.h:193
#define OPT_SEP
options separator character

References UTF8::begin(), UTF8::end(), name, OPT_SEP, and UTF8::size().

Referenced by DIALOG_FP_PLUGIN_OPTIONS::TransferDataFromWindow().

◆ GetCount()

unsigned LIB_TABLE::GetCount ( ) const
inline

◆ GetDescription()

const wxString LIB_TABLE::GetDescription ( const wxString &  aNickname)
Returns
the library description from aNickname, or an empty string if aNickname does not exist.

Definition at line 139 of file lib_table_base.cpp.

140{
141 // Use "no exception" form of find row and ignore disabled flag.
142 const LIB_TABLE_ROW* row = findRow( aNickname );
143
144 if( row )
145 return row->GetDescr();
146 else
147 return wxEmptyString;
148}
const wxString & GetDescr() const
Return the description of the library referenced by this row.
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,...

References findRow(), and LIB_TABLE_ROW::GetDescr().

Referenced by SYMBOL_TREE_MODEL_ADAPTER::AddLibraries(), SYMBOL_TREE_MODEL_ADAPTER::AddLibrary(), FOOTPRINT_EDIT_FRAME::buildSaveAsDialog(), SYMBOL_EDIT_FRAME::buildSaveAsDialog(), and PCB_BASE_FRAME::SelectLibrary().

◆ GetFullURI()

wxString LIB_TABLE::GetFullURI ( const wxString &  aLibNickname,
bool  aExpandEnvVars = true 
) const

Return the full URI of the library mapped to aLibNickname.

Definition at line 174 of file lib_table_base.cpp.

175{
176 const LIB_TABLE_ROW* row = findRow( aNickname, true );
177
178 wxString retv;
179
180 if( row )
181 retv = row->GetFullURI( aExpandEnvVars );
182
183 return retv;
184}
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...

References findRow(), and LIB_TABLE_ROW::GetFullURI().

Referenced by CVPCB_MAINFRAME::DisplayStatus(), SCH_EDIT_FRAME::LoadSheetFromFile(), NETLIST_EXPORTER_XML::makeLibraries(), and FOOTPRINT_EDITOR_CONTROL::SaveAs().

◆ GetLogicalLibs()

std::vector< wxString > LIB_TABLE::GetLogicalLibs ( )

Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.

Definition at line 268 of file lib_table_base.cpp.

269{
270 // Only return unique logical library names. Use std::set::insert() to quietly reject any
271 // duplicates (usually due to encountering a duplicate nickname in a fallback table).
272
273 std::set<wxString> unique;
274 std::vector<wxString> ret;
275 const LIB_TABLE* cur = this;
276
277 do
278 {
279 for( LIB_TABLE_ROWS_CITER it = cur->m_rows.begin(); it!=cur->m_rows.end(); ++it )
280 {
281 if( it->GetIsEnabled() )
282 unique.insert( it->GetNickName() );
283 }
284
285 } while( ( cur = cur->m_fallBack ) != nullptr );
286
287 ret.reserve( unique.size() );
288
289 // return a sorted, unique set of nicknames in a std::vector<wxString> to caller
290 for( std::set< wxString >::const_iterator it = unique.begin(); it!=unique.end(); ++it )
291 ret.push_back( *it );
292
293 // We want to allow case-sensitive duplicates but sort by case-insensitive ordering
294 std::sort( ret.begin(), ret.end(),
295 []( const wxString& lhs, const wxString& rhs )
296 {
297 return StrNumCmp( lhs, rhs, true /* ignore case */ ) < 0;
298 } );
299
300 return ret;
301}
LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER

References m_fallBack, and m_rows.

Referenced by FP_TREE_MODEL_ADAPTER::AddLibraries(), CVPCB_MAINFRAME::BuildLibrariesListBox(), FOOTPRINT_EDIT_FRAME::buildSaveAsDialog(), SYMBOL_EDIT_FRAME::buildSaveAsDialog(), DIALOG_SYMBOL_REMAP::createProjectSymbolLibTable(), FP_LIB_TABLE::FootprintLoadWithOptionalNickname(), FP_LIB_TABLE::GenerateTimestamp(), GetFootprintLibraries(), SYMBOL_LIBRARY_MANAGER::GetLibraryCount(), SYMBOL_LIB_TABLE::GetModifyHash(), guessNickname(), CVPCB_MAINFRAME::LoadFootprintFiles(), SYMBOL_LIB_TABLE::LoadSymbolWithOptionalNickname(), FOOTPRINT_VIEWER_FRAME::OnActivate(), SYMBOL_VIEWER_FRAME::OnSelectSymbol(), SCH_BASE_FRAME::PickSymbolFromLibTree(), FOOTPRINT_LIST_IMPL::ReadFootprintFiles(), SYMBOL_VIEWER_FRAME::ReCreateLibList(), FOOTPRINT_VIEWER_FRAME::ReCreateLibraryList(), PCB_BASE_FRAME::SelectLibrary(), and FP_TREE_SYNCHRONIZING_ADAPTER::Sync().

◆ GetVersion()

int LIB_TABLE::GetVersion ( ) const
inline

Returns the version number (0 if unset)

Returns
integer version number read from table

Definition at line 519 of file lib_table_base.h.

520 {
521 return m_version;
522 }

References m_version.

◆ HasLibrary()

bool LIB_TABLE::HasLibrary ( const wxString &  aNickname,
bool  aCheckEnabled = false 
) const

◆ HasLibraryWithPath()

bool LIB_TABLE::HasLibraryWithPath ( const wxString &  aPath) const

Test for the existence of aPath in the library table.

Parameters
aCheckEnabledif true will only return true for enabled libraries
Returns
true if a library aNickname exists in the table.

Definition at line 162 of file lib_table_base.cpp.

163{
164 for( const LIB_TABLE_ROW& row : m_rows )
165 {
166 if( row.GetFullURI() == aPath )
167 return true;
168 }
169
170 return false;
171}

References m_rows.

Referenced by PCM_FP_LIB_TRAVERSER::OnDir(), and PCM_SYM_LIB_TRAVERSER::OnFile().

◆ InsertRow()

bool LIB_TABLE::InsertRow ( LIB_TABLE_ROW aRow,
bool  doReplace = false 
)

Adds aRow if it does not already exist or if doReplace is true.

If doReplace is not true and the key for aRow already exists, the function fails and returns false.

The key for the table is the nickName, and all in this table must be unique.

Parameters
aRowis the new row to insert, or to forcibly add if doReplace is true.
doReplaceif true, means insert regardless of whether aRow's key already exists. If false, then fail if the key already exists.
Returns
bool - true if the operation succeeded.

Definition at line 304 of file lib_table_base.cpp.

305{
306 ensureIndex();
307
308 std::lock_guard<std::shared_mutex> lock( m_nickIndexMutex );
309
310 INDEX_CITER it = m_nickIndex.find( aRow->GetNickName() );
311
312 aRow->SetParent( this );
313
314 if( it == m_nickIndex.end() )
315 {
316 m_rows.push_back( aRow );
317 m_nickIndex.insert( INDEX_VALUE( aRow->GetNickName(), m_rows.size() - 1 ) );
318 return true;
319 }
320
321 if( doReplace )
322 {
323 m_rows.replace( it->second, aRow );
324 return true;
325 }
326
327 return false;
328}
void SetParent(LIB_TABLE *aParent)
const wxString & GetNickName() const
INDEX::value_type INDEX_VALUE
INDEX::const_iterator INDEX_CITER

References ensureIndex(), LIB_TABLE_ROW::GetNickName(), m_nickIndex, m_nickIndexMutex, m_rows, and LIB_TABLE_ROW::SetParent().

Referenced by SYMBOL_LIBRARY_MANAGER::addLibrary(), PCB_BASE_EDIT_FRAME::AddLibrary(), SYMBOL_EDIT_FRAME::addLibTableEntry(), DIALOG_SYMBOL_REMAP::createProjectSymbolLibTable(), LIB_TABLE_TEST_FIXTURE::LIB_TABLE_TEST_FIXTURE(), SCH_ALTIUM_PLUGIN::Load(), CADSTAR_SCH_ARCHIVE_PLUGIN::Load(), SCH_EAGLE_PLUGIN::Load(), PCM_FP_LIB_TRAVERSER::OnDir(), PCM_SYM_LIB_TRAVERSER::OnFile(), PCB_EDIT_FRAME::OpenProjectFiles(), SYMBOL_LIB_TABLE::Parse(), and FP_LIB_TABLE::Parse().

◆ IsEmpty()

bool LIB_TABLE::IsEmpty ( bool  aIncludeFallback = true)

Return true if the table is empty.

Parameters
aIncludeFallbackis used to determine if the fallback table should be included in the test.
Returns
true if the footprint library table is empty.

Definition at line 130 of file lib_table_base.cpp.

131{
132 if( !aIncludeFallback || !m_fallBack )
133 return m_rows.empty();
134
135 return m_rows.empty() && m_fallBack->IsEmpty( true );
136}
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.

References IsEmpty(), m_fallBack, and m_rows.

Referenced by BOOST_AUTO_TEST_CASE(), DIALOG_SYMBOL_REMAP::createProjectSymbolLibTable(), IsEmpty(), and SCH_EDIT_FRAME::LoadSheetFromFile().

◆ Load()

void LIB_TABLE::Load ( const wxString &  aFileName)

Load the library table using the path defined by aFileName aFallBackTable.

Parameters
aFileNamecontains the full path to the s-expression file.
Exceptions
IO_ERRORif an error occurs attempting to load the footprint library table.

Definition at line 356 of file lib_table_base.cpp.

357{
358 // It's OK if footprint library tables are missing.
359 if( wxFileName::IsFileReadable( aFileName ) )
360 {
361 FILE_LINE_READER reader( aFileName );
362 LIB_TABLE_LEXER lexer( &reader );
363
364 Parse( &lexer );
365
366 if( m_version != 7 && migrate() && wxFileName::IsFileWritable( aFileName ) )
367 Save( aFileName );
368 }
369}
A LINE_READER that reads from an open file.
Definition: richio.h:173
bool migrate()
Updates the env vars from older version of KiCad, provided they do not currently resolve to anything.
virtual void Parse(LIB_TABLE_LEXER *aLexer)=0
Parse the #LIB_TABLE_LEXER s-expression library table format into the appropriate LIB_TABLE_ROW objec...
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.

References m_version, migrate(), Parse(), and Save().

Referenced by FP_LIB_TABLE::LoadGlobalTable(), SYMBOL_LIB_TABLE::LoadGlobalTable(), SCH_EDIT_FRAME::LoadSheetFromFile(), PROJECT::PcbFootprintLibs(), PCB::IFACE::SaveFileAs(), DIALOG_GLOBAL_SYM_LIB_TABLE_CONFIG::TransferDataFromWindow(), and DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG::TransferDataFromWindow().

◆ migrate()

bool LIB_TABLE::migrate ( )
protected

Updates the env vars from older version of KiCad, provided they do not currently resolve to anything.

Returns
True if the tables were modified

Definition at line 331 of file lib_table_base.cpp.

332{
333 bool table_updated = false;
334
335 for( LIB_TABLE_ROW& row : m_rows )
336 {
337 bool row_updated = false;
338 wxString uri = row.GetFullURI( true );
339
340 // If the uri still has a variable in it, that means that the user does not have
341 // these vars defined. We update the old vars to the KICAD7 versions on load
342 row_updated |= ( uri.Replace( wxS( "${KICAD5_" ), wxS( "${KICAD7_" ), false ) > 0 );
343 row_updated |= ( uri.Replace( wxS( "${KICAD6_" ), wxS( "${KICAD7_" ), false ) > 0 );
344
345 if( row_updated )
346 {
347 row.SetFullURI( uri );
348 table_updated = true;
349 }
350 }
351
352 return table_updated;
353}

References m_rows.

Referenced by Load().

◆ operator!=()

bool LIB_TABLE::operator!= ( const LIB_TABLE r) const
inline

Definition at line 361 of file lib_table_base.h.

361{ return !( *this == r ); }

◆ operator==()

bool LIB_TABLE::operator== ( const LIB_TABLE r) const
inline

Compares this table against another.

This compares the row contents against each other. Any fallback tables are not checked.

Definition at line 345 of file lib_table_base.h.

346 {
347 if( m_rows.size() == r.m_rows.size() )
348 {
349 unsigned i;
350
351 for( i = 0; i < m_rows.size() && m_rows[i] == r.m_rows[i]; ++i )
352 ;
353
354 if( i == m_rows.size() )
355 return true;
356 }
357
358 return false;
359 }

References m_rows.

◆ Parse()

virtual void LIB_TABLE::Parse ( LIB_TABLE_LEXER *  aLexer)
pure virtual

Parse the #LIB_TABLE_LEXER s-expression library table format into the appropriate LIB_TABLE_ROW objects.

Parameters
aLexeris the lexer to parse.
Exceptions
IO_ERRORif an I/O error occurs during parsing.
PARSER_ERRORif the lexer format to parse is invalid.
boost::bad_pointerif an any attempt to add an invalid pointer to the boost::ptr_vector.
boost::bad_indexif an index outside the row table bounds is accessed.

Implemented in SYMBOL_LIB_TABLE, FP_LIB_TABLE, and TEST_LIB_TABLE.

Referenced by Load().

◆ ParseOptions()

STRING_UTF8_MAP * LIB_TABLE::ParseOptions ( const std::string &  aOptionsList)
static

Parses aOptionsList and places the result into a #PROPERTIES object which is returned.

If the options field is empty, then the returned PROPERTIES will be a NULL pointer.

Typically aOptionsList comes from the "options" field within a LIB_TABLE_ROW and the format is simply a comma separated list of name value pairs. e.g.: [name1[=value1][|name2[=value2]]] etc. When using the UI to create or edit a library table, this formatting is handled for you.

Definition at line 382 of file lib_table_base.cpp.

383{
384 if( aOptionsList.size() )
385 {
386 const char* cp = &aOptionsList[0];
387 const char* end = cp + aOptionsList.size();
388
389 STRING_UTF8_MAP props;
390 std::string pair;
391
392 // Parse all name=value pairs
393 while( cp < end )
394 {
395 pair.clear();
396
397 // Skip leading white space.
398 while( cp < end && isspace( *cp ) )
399 ++cp;
400
401 // Find the end of pair/field
402 while( cp < end )
403 {
404 if( *cp == '\\' && cp + 1 < end && cp[1] == OPT_SEP )
405 {
406 ++cp; // skip the escape
407 pair += *cp++; // add the separator
408 }
409 else if( *cp == OPT_SEP )
410 {
411 ++cp; // skip the separator
412 break; // process the pair
413 }
414 else
415 {
416 pair += *cp++;
417 }
418 }
419
420 // stash the pair
421 if( pair.size() )
422 {
423 // first equals sign separates 'name' and 'value'.
424 size_t eqNdx = pair.find( '=' );
425
426 if( eqNdx != pair.npos )
427 {
428 std::string name = pair.substr( 0, eqNdx );
429 std::string value = pair.substr( eqNdx + 1 );
430 props[name] = value;
431 }
432 else
433 {
434 props[pair] = ""; // property is present, but with no value.
435 }
436 }
437 }
438
439 if( props.size() )
440 return new STRING_UTF8_MAP( props );
441 }
442
443 return nullptr;
444}
A name/value tuple with unique names and optional values.

References name, and OPT_SEP.

Referenced by LIB_TABLE_ROW::SetOptions(), and DIALOG_FP_PLUGIN_OPTIONS::TransferDataToWindow().

◆ reindex()

void LIB_TABLE::reindex ( bool  aForce)
inlineprotected

Rebuilds the m_nickIndex.

Parameters
aForceis to avoid rebuilding the index multiple times because multiple threads hit ensureIndex at the same time

Definition at line 549 of file lib_table_base.h.

550 {
551 std::lock_guard<std::shared_mutex> lock( m_nickIndexMutex );
552
553 if( !aForce )
554 {
555 if( m_nickIndex.size() )
556 return;
557 }
558
559 m_nickIndex.clear();
560
561 for( LIB_TABLE_ROWS_ITER it = m_rows.begin(); it != m_rows.end(); ++it )
562 m_nickIndex.insert( INDEX_VALUE( it->GetNickName(), it - m_rows.begin() ) );
563 }
LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER

References m_nickIndex, m_nickIndexMutex, and m_rows.

Referenced by ensureIndex(), RemoveRow(), PANEL_SYM_LIB_TABLE::TransferDataFromWindow(), and PANEL_FP_LIB_TABLE::TransferDataFromWindow().

◆ RemoveRow()

bool LIB_TABLE::RemoveRow ( const LIB_TABLE_ROW aRow)
inline

Removes a row from the table.

Parameters
aRowis the row to remove
Returns
true if the row was found (and removed)

Definition at line 451 of file lib_table_base.h.

452 {
453 for( auto iter = m_rows.begin(); iter != m_rows.end(); ++iter )
454 {
455 if( *iter == *aRow )
456 {
457 m_rows.erase( iter, iter + 1 );
458 reindex( true );
459 return true;
460 }
461 }
462 return false;
463 }

References m_rows, and reindex().

Referenced by SYMBOL_LIBRARY_MANAGER::addLibrary(), FP_LIB_TABLE::LoadGlobalTable(), and SYMBOL_LIB_TABLE::LoadGlobalTable().

◆ Save()

void LIB_TABLE::Save ( const wxString &  aFileName) const

Write this library table to aFileName in s-expression form.

Parameters
aFileNameis the name of the file to write to.

Definition at line 372 of file lib_table_base.cpp.

373{
374 FILE_OUTPUTFORMATTER sf( aFileName );
375
376 // Force the lib table version to 7 before saving
377 m_version = 7;
378 Format( &sf, 0 );
379}
Used for text file output.
Definition: richio.h:457
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const =0
Generate the table in s-expression format to aOutput with an indentation level of aIndentLevel.

References Format(), and m_version.

Referenced by PCB_BASE_EDIT_FRAME::AddLibrary(), SYMBOL_EDIT_FRAME::addLibTableEntry(), SCH_EDITOR_CONTROL::ExportSymbolsToLibrary(), InvokePcbLibTableEditor(), InvokeSchEditSymbolLibTable(), Load(), FP_LIB_TABLE::LoadGlobalTable(), SYMBOL_LIB_TABLE::LoadGlobalTable(), PCB_EDIT_FRAME::OpenProjectFiles(), SYMBOL_EDIT_FRAME::replaceLibTableEntry(), PCB::IFACE::SaveFileAs(), SCH_BASE_FRAME::saveSymbolLibTables(), DIALOG_GLOBAL_SYM_LIB_TABLE_CONFIG::TransferDataFromWindow(), and DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG::TransferDataFromWindow().

◆ Type()

virtual KICAD_T PROJECT::_ELEM::Type ( )
pure virtualinherited

Friends And Related Function Documentation

◆ LIB_TABLE_GRID

friend class LIB_TABLE_GRID
friend

Definition at line 576 of file lib_table_base.h.

◆ PANEL_FP_LIB_TABLE

friend class PANEL_FP_LIB_TABLE
friend

Definition at line 575 of file lib_table_base.h.

Member Data Documentation

◆ m_fallBack

LIB_TABLE* LIB_TABLE::m_fallBack
protected

Definition at line 590 of file lib_table_base.h.

Referenced by findRow(), FindRowByURI(), GetLogicalLibs(), and IsEmpty().

◆ m_nickIndex

INDEX LIB_TABLE::m_nickIndex
protected

this particular key is the nickName within each row.

Definition at line 588 of file lib_table_base.h.

Referenced by Clear(), ensureIndex(), findRow(), InsertRow(), and reindex().

◆ m_nickIndexMutex

std::shared_mutex LIB_TABLE::m_nickIndexMutex
mutableprotected

Mutex to protect access to the nickIndex variable.

Definition at line 596 of file lib_table_base.h.

Referenced by Clear(), findRow(), InsertRow(), and reindex().

◆ m_rows

◆ m_version

int LIB_TABLE::m_version
mutableprotected

Versioning to handle importing old tables.

Definition at line 593 of file lib_table_base.h.

Referenced by SYMBOL_LIB_TABLE::Format(), FP_LIB_TABLE::Format(), GetVersion(), Load(), SYMBOL_LIB_TABLE::Parse(), FP_LIB_TABLE::Parse(), and Save().


The documentation for this class was generated from the following files: