21#include <boost/lexical_cast.hpp>
67 wxFileName fn( aPath );
69 m_path = fn.GetAbsolutePath();
71 if( !fn.FileExists() )
79 tl::expected<LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR> ir = parser.
Parse(
m_path.ToStdString() );
103 tl::expected<LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR> ir = parser.
ParseBuffer( aBuffer.ToStdString() );
107 m_ok = initFromIR( *ir );
112 m_errorDescription = ir.error().description;
135 catch(
const boost::bad_lexical_cast & )
152 row.
m_uri = wxString::FromUTF8( aIR.
uri );
161 m_rows.emplace_back( row );
168 static const std::map<LIBRARY_TABLE_TYPE, wxString> types = {
174 wxCHECK( types.contains(
Type() ), );
176 XNODE self( wxXML_ELEMENT_NODE, types.at(
Type() ) );
183 wxString uri = row.URI();
184 uri.Replace(
'\\',
'/' );
186 XNODE* rowNode =
new XNODE( wxXML_ELEMENT_NODE,
"lib" );
194 rowNode->AddChild(
new XNODE( wxXML_ELEMENT_NODE,
"disabled" ) );
197 rowNode->AddChild(
new XNODE( wxXML_ELEMENT_NODE,
"hidden" ) );
199 self.AddChild( rowNode );
227 if( row.Nickname() == aNickname )
236 bool aSubstituted )
const
240 if( !aSubstituted && row.URI() == aUri )
255 if( row.Nickname() == aNickname )
267 if( row.Nickname() == aNickname )
278 wxFileName fn(
Path() );
284 wxFFile existing( fn.GetFullPath(), wxT(
"rb" ) );
286 if( existing.IsOpened() )
288 wxFileOffset rawLen = existing.Length();
291 &&
static_cast<uint64_t
>( rawLen ) <= std::numeric_limits<size_t>::max() )
293 size_t len =
static_cast<size_t>( rawLen );
295 void* dst = len > 0 ? buf.GetWriteBuf( len ) :
nullptr;
297 if( len == 0 || existing.Read( dst, len ) == len )
299 buf.SetDataLen( len );
302 wxString bakPath = fn.GetFullPath() + wxT(
".bak" );
310 "Could not rotate library table backup to '%s': %s", bakPath,
337 std::map<std::string, UTF8> props;
339 if( aOptionsList.size() )
341 const char* cp = &aOptionsList[0];
342 const char*
end = cp + aOptionsList.size();
352 while( cp <
end && isspace( *cp ) )
358 if( *cp ==
'\\' && cp + 1 <
end && cp[1] ==
OPT_SEP )
378 size_t eqNdx = pair.find(
'=' );
380 if( eqNdx != pair.npos )
382 std::string
name = pair.substr( 0, eqNdx );
383 std::string value = pair.substr( eqNdx + 1 );
404 for( std::map<std::string, UTF8>::const_iterator it = aProperties->begin();
405 it != aProperties->end(); ++it )
407 const std::string&
name = it->first;
409 const UTF8& value = it->second;
421 for( std::string::const_iterator si = value.
begin(); si != value.
end(); ++si )
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
static wxString ExpandURI(const wxString &aShortURI, const PROJECT &aProject)
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > ParseBuffer(const std::string &aBuffer)
tl::expected< LIBRARY_TABLE_IR, LIBRARY_PARSE_ERROR > Parse(const std::filesystem::path &aPath)
LIBRARY_TABLE_ROW()=default
void SetOk(bool aOk=true)
bool operator==(const LIBRARY_TABLE_ROW &aOther) const
std::map< std::string, UTF8 > GetOptionsMap() const
static const wxString TABLE_TYPE_NAME
void SetScope(LIBRARY_TABLE_SCOPE aScope)
LIBRARY_TABLE_SCOPE m_scope
LIBRARY_RESULT< void > Save()
bool operator==(const LIBRARY_TABLE &aOther) const
void Format(OUTPUTFORMATTER *aOutput) const
static std::map< std::string, UTF8 > ParseOptions(const std::string &aOptionsList)
LIBRARY_TABLE_TYPE Type() const
LIBRARY_TABLE_TYPE m_type
What type of content this table contains (footprint, symbol, design block, etc)
std::optional< LIBRARY_TABLE_ROW * > Row(const wxString &aNickname)
LIBRARY_TABLE_ROW & InsertRow()
Builds a new row and inserts it at the end of the table; returning a reference to the row.
static UTF8 FormatOptions(const std::map< std::string, UTF8 > *aProperties)
const wxString & Path() const
wxString m_path
The full path to the file this table was parsed from, if any.
wxString m_errorDescription
LIBRARY_TABLE_ROW MakeRow() const
Builds a new row that is suitable for this table (does not insert it)
bool HasRow(const wxString &aNickname) const
LIBRARY_TABLE(const wxFileName &aPath, LIBRARY_TABLE_SCOPE aScope)
Creates a library table from a file on disk.
bool HasRowWithURI(const wxString &aUri, const PROJECT &aProject, bool aSubstituted=false) const
Returns true if the given (fully-expanded) URI exists as a library in this table.
const std::deque< LIBRARY_TABLE_ROW > & Rows() const
std::optional< int > m_version
The format version, if present in the parsed file.
std::deque< LIBRARY_TABLE_ROW > m_rows
bool addRowFromIR(const LIBRARY_TABLE_ROW_IR &aIR)
LIBRARY_TABLE_SCOPE m_scope
bool initFromIR(const LIBRARY_TABLE_IR &aIR)
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
static void ResolvePossibleSymlinks(wxFileName &aFilename)
An extension of wxXmlNode that can format its contents as KiCad-style s-expressions.
void Format(OUTPUTFORMATTER *out) const
Write this object as UTF8 out to an OUTPUTFORMATTER as an S-expression.
void AddAttribute(const wxString &aName, const wxString &aValue) override
const wxChar *const traceLibraries
Flag to enable library table and library manager tracing.
#define OPT_SEP
options separator character
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
The intermediate representation that a library table is parsed into.
std::vector< LIBRARY_TABLE_ROW_IR > rows
wxLogTrace helper definitions.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().