28#include <wx/filename.h>
33#include <lib_table_lexer.h>
40using namespace LIB_TABLE_T;
77 uri.Replace(
'\\',
'/' );
79 wxString extraOptions;
82 extraOptions +=
"(disabled)";
85 extraOptions +=
"(hidden)";
87 out->
Print( nestLevel,
"(lib (name %s)(type %s)(uri %s)(options %s)(descr %s)%s)\n",
90 out->
Quotew( uri ).c_str(),
93 extraOptions.ToStdString().c_str() );
118 m_fallBack( aFallBackTable ), m_version( 0 )
155 return wxEmptyString;
174 if( row.GetFullURI() == aPath )
202 std::shared_lock<std::shared_mutex> lock( cur->
m_mutex );
217 for(
const std::pair<const wxString, LIB_TABLE_ROWS_ITER>& entry : cur->
m_rowsMap )
219 wxString legacyLibName = entry.first;
220 legacyLibName.Replace(
" ",
"_" );
222 if( legacyLibName == aNickName )
224 row = &*entry.second;
232 }
while( ( cur = cur->
m_fallBack ) !=
nullptr );
244 for(
unsigned i = 0; i < cur->
m_rows.size(); i++ )
246 wxString tmp = cur->
m_rows[i].GetFullURI(
true );
248 if( tmp.Find(
"://" ) != wxNOT_FOUND )
255 wxFileName fn = aURI;
260 if( fn == wxFileName( tmp ) )
266 }
while( ( cur = cur->
m_fallBack ) !=
nullptr );
277 std::set<wxString> unique;
278 std::vector<wxString> ret;
289 }
while( ( cur = cur->
m_fallBack ) !=
nullptr );
291 ret.reserve( unique.size() );
294 for( std::set< wxString >::const_iterator it = unique.begin(); it!=unique.end(); ++it )
295 ret.push_back( *it );
298 std::sort( ret.begin(), ret.end(),
299 [](
const wxString& lhs,
const wxString& rhs )
301 return StrNumCmp( lhs, rhs, true ) < 0;
310 std::lock_guard<std::shared_mutex> lock(
m_mutex );
328 m_rows.replace( it->second, aRow );
343 std::lock_guard<std::shared_mutex> lock(
m_mutex );
350 if( &*it->second == aRow )
353 m_rows.erase( it->second );
360 for(
int i = (
int)
m_rows.size() - 1; i >= 0; --i )
380 std::lock_guard<std::shared_mutex> lock(
m_mutex );
382 if( aIndex >=
m_rows.size() )
387 m_rows.replace( aIndex, aRow );
395 std::lock_guard<std::shared_mutex> lock(
m_mutex );
397 if( aIndex >=
m_rows.size() )
400 int newPos =
static_cast<int>( aIndex ) + aOffset;
402 if( newPos < 0 || newPos >
static_cast<int>(
m_rows.size() ) - 1 )
405 auto element =
m_rows.release(
m_rows.begin() + aIndex );
407 m_rows.insert(
m_rows.begin() + newPos, element.release() );
416 std::lock_guard<std::shared_mutex> lock(
m_mutex );
419 m_rows.transfer(
m_rows.end(), aRowsList.begin(), aRowsList.end(), aRowsList );
431 it->SetParent(
this );
439 bool table_updated =
false;
443 bool row_updated =
false;
444 wxString uri = row.GetFullURI(
true );
448 row_updated |= ( uri.Replace( wxS(
"${KICAD5_" ), wxS(
"${KICAD7_" ),
false ) > 0 );
449 row_updated |= ( uri.Replace( wxS(
"${KICAD6_" ), wxS(
"${KICAD7_" ), false ) > 0 );
453 row.SetFullURI( uri );
454 table_updated =
true;
458 return table_updated;
464 std::lock_guard<std::shared_mutex> lock(
m_mutex );
468 if( wxFileName::IsFileReadable( aFileName ) )
471 LIB_TABLE_LEXER lexer( &reader );
475 if(
m_version != 7 &&
migrate() && wxFileName::IsFileWritable( aFileName ) )
495 if( aOptionsList.size() )
497 const char* cp = &aOptionsList[0];
498 const char* end = cp + aOptionsList.size();
500 std::map<std::string, UTF8> props;
509 while( cp < end && isspace( *cp ) )
515 if( *cp ==
'\\' && cp + 1 < end && cp[1] ==
OPT_SEP )
535 size_t eqNdx = pair.find(
'=' );
537 if( eqNdx != pair.npos )
539 std::string
name = pair.substr( 0, eqNdx );
540 std::string value = pair.substr( eqNdx + 1 );
551 return new std::map<std::string, UTF8>( props );
564 for( std::map<std::string, UTF8>::const_iterator it = aProperties->begin(); it != aProperties->end(); ++it )
566 const std::string&
name = it->first;
568 const UTF8& value = it->second;
580 for( std::string::const_iterator si = value.
begin(); si != value.
end(); ++si )
A LINE_READER that reads from an open file.
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
void SetFullURI(const wxString &aFullURI)
Change the full URI for the library.
bool visible
Whether the LIB_TABLE_ROW is visible in choosers.
std::unique_ptr< std::map< std::string, UTF8 > > properties
const wxString & GetOptions() const
Return the options string, which may hold a password or anything else needed to instantiate the under...
const wxString & GetDescr() const
Return the description of the library referenced by this row.
wxString uri_user
what user entered from UI or loaded from disk
virtual const wxString GetType() const =0
Return the type of library represented by this row.
void Format(OUTPUTFORMATTER *out, int nestLevel) const
Serialize this object as utf8 text to an OUTPUTFORMATTER, and tries to make it look good using multip...
bool enabled
Whether the LIB_TABLE_ROW is enabled.
void SetParent(LIB_TABLE *aParent)
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...
LIB_TABLE_ROW * clone() const
bool operator==(const LIB_TABLE_ROW &r) const
bool GetIsEnabled() const
void SetOptions(const wxString &aOptions)
Change the library options strings.
void setProperties(std::map< std::string, UTF8 > *aProperties)
bool GetIsVisible() const
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.
bool ReplaceRow(size_t aIndex, LIB_TABLE_ROW *aRow)
Replaces the Nth row with the given new row.
std::shared_mutex m_mutex
Mutex to protect access to the rows vector.
const wxString GetDescription(const wxString &aNickname)
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
static std::map< std::string, UTF8 > * ParseOptions(const std::string &aOptionsList)
Parses aOptionsList and places the result into a #PROPERTIES object which is returned.
static UTF8 FormatOptions(const std::map< std::string, UTF8 > *aProperties)
Returns a list of options from the aProperties parameter.
std::map< wxString, LIB_TABLE_ROWS_ITER > m_rowsMap
this is a non-owning index into the LIB_TABLE_ROWS 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.
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
void TransferRows(LIB_TABLE_ROWS &aRowsList)
Takes ownership of another list of rows; the original list will be freed.
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.
bool migrate()
Updates the env vars from older version of KiCad, provided they do not currently resolve to anything.
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.
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const =0
Generate the table in s-expression format to aOutput with an indentation level of aIndentLevel.
bool RemoveRow(const LIB_TABLE_ROW *aRow)
Removes a row from the table and frees the pointer.
wxString GetFullURI(const wxString &aLibNickname, bool aExpandEnvVars=true) const
Return the full URI of the library mapped to aLibNickname.
LIB_TABLE(LIB_TABLE *aFallBackTable=nullptr)
Build a library table by pre-pending this table fragment in front of aFallBackTable.
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.
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...
const LIB_TABLE_ROW * FindRowByURI(const wxString &aURI)
bool ChangeRowOrder(size_t aIndex, int aOffset)
Moves a row within 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,...
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
std::string::const_iterator begin() const
std::string::size_type size() const
std::string::const_iterator end() const
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
#define OPT_SEP
options separator character
LIB_TABLE_ROW * new_clone(const LIB_TABLE_ROW &aRow)
Allows boost pointer containers to make clones of the data stored in them.
boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS
LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.