Push library reading check to IO_BASE

This commit is contained in:
Ian McInerney 2023-12-27 16:43:05 +00:00
parent f8688a922d
commit 4a67761d29
5 changed files with 52 additions and 111 deletions

View File

@ -18,10 +18,15 @@
*/ */
#include <unordered_set>
#include <io/io_base.h> #include <io/io_base.h>
#include <ki_exception.h> #include <ki_exception.h>
#include <wildcards_and_files_ext.h> #include <wildcards_and_files_ext.h>
#include <wx/filename.h>
#include <wx/translation.h> #include <wx/translation.h>
#include <wx/dir.h>
#define FMT_UNIMPLEMENTED wxT( "IO interface \"%s\" does not implement the \"%s\" function." ) #define FMT_UNIMPLEMENTED wxT( "IO interface \"%s\" does not implement the \"%s\" function." )
#define NOT_IMPLEMENTED( aCaller ) \ #define NOT_IMPLEMENTED( aCaller ) \
@ -61,6 +66,52 @@ void IO_BASE::GetLibraryOptions( STRING_UTF8_MAP* aListToAppendTo ) const
bool IO_BASE::CanReadLibrary( const wxString& aFileName ) const bool IO_BASE::CanReadLibrary( const wxString& aFileName ) const
{ {
// TODO: Push file extension based checks from PCB_IO and SCH_IO into this function const IO_BASE::IO_FILE_DESC& desc = GetLibraryDesc();
if( desc.m_IsFile )
{
const std::vector<std::string>& exts = desc.m_FileExtensions;
wxString fileExt = wxFileName( aFileName ).GetExt().MakeLower();
for( const std::string& ext : exts )
{
if( fileExt == wxString( ext ).Lower() )
return true;
}
}
else
{
wxDir dir( aFileName );
if( !dir.IsOpened() )
return false;
std::vector<std::string> exts = desc.m_ExtensionsInDir;
std::unordered_set<wxString> lowerExts;
for( const std::string& ext : exts )
lowerExts.emplace( wxString( ext ).MakeLower() );
wxString filenameStr;
bool cont = dir.GetFirst( &filenameStr, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN );
while( cont )
{
wxString ext = wxS( "" );
int idx = filenameStr.Find( '.', true );
if( idx != -1 )
ext = filenameStr.Mid( idx + 1 ).MakeLower();
if( lowerExts.count( ext ) )
return true;
cont = dir.GetNext( &filenameStr );
}
}
return false; return false;
} }

View File

@ -59,57 +59,6 @@ bool SCH_IO::CanReadSchematicFile( const wxString& aFileName ) const
} }
bool SCH_IO::CanReadLibrary( const wxString& aFileName ) const
{
const IO_BASE::IO_FILE_DESC& desc = GetLibraryFileDesc();
if( desc.m_IsFile )
{
const std::vector<std::string>& exts = desc.m_FileExtensions;
wxString fileExt = wxFileName( aFileName ).GetExt().MakeLower();
for( const std::string& ext : exts )
{
if( fileExt == wxString( ext ).Lower() )
return true;
}
}
else
{
wxDir dir( aFileName );
if( !dir.IsOpened() )
return false;
std::vector<std::string> exts = desc.m_ExtensionsInDir;
std::unordered_set<wxString> lowerExts;
for( const std::string& ext : exts )
lowerExts.emplace( wxString( ext ).MakeLower() );
wxString filenameStr;
bool cont = dir.GetFirst( &filenameStr, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN );
while( cont )
{
wxString ext = wxS( "" );
int idx = filenameStr.Find( '.', true );
if( idx != -1 )
ext = filenameStr.Mid( idx + 1 ).MakeLower();
if( lowerExts.count( ext ) )
return true;
cont = dir.GetNext( &filenameStr );
}
}
return false;
}
void SCH_IO::SaveLibrary( const wxString& aFileName, const STRING_UTF8_MAP* aProperties ) void SCH_IO::SaveLibrary( const wxString& aFileName, const STRING_UTF8_MAP* aProperties )
{ {
NOT_IMPLEMENTED( __FUNCTION__ ); NOT_IMPLEMENTED( __FUNCTION__ );

View File

@ -70,8 +70,6 @@ public:
*/ */
virtual bool CanReadSchematicFile( const wxString& aFileName ) const; virtual bool CanReadSchematicFile( const wxString& aFileName ) const;
bool CanReadLibrary( const wxString& aFileName ) const override;
/** /**
* Return the modification hash from the library cache. * Return the modification hash from the library cache.
* *

View File

@ -72,57 +72,6 @@ bool PCB_IO::CanReadFootprint( const wxString& aFileName ) const
} }
bool PCB_IO::CanReadLibrary( const wxString& aFileName ) const
{
const IO_BASE::IO_FILE_DESC& desc = GetLibraryDesc();
if( desc.m_IsFile )
{
const std::vector<std::string>& exts = desc.m_FileExtensions;
wxString fileExt = wxFileName( aFileName ).GetExt().MakeLower();
for( const std::string& ext : exts )
{
if( fileExt == wxString( ext ).Lower() )
return true;
}
}
else
{
wxDir dir( aFileName );
if( !dir.IsOpened() )
return false;
std::vector<std::string> exts = desc.m_ExtensionsInDir;
std::unordered_set<wxString> lowerExts;
for( const std::string& ext : exts )
lowerExts.emplace( wxString( ext ).MakeLower() );
wxString filenameStr;
bool cont = dir.GetFirst( &filenameStr, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN );
while( cont )
{
wxString ext = wxS( "" );
int idx = filenameStr.Find( '.', true );
if( idx != -1 )
ext = filenameStr.Mid( idx + 1 ).MakeLower();
if( lowerExts.count( ext ) )
return true;
cont = dir.GetNext( &filenameStr );
}
}
return false;
}
BOARD* PCB_IO::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe, BOARD* PCB_IO::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
const STRING_UTF8_MAP* aProperties, PROJECT* aProject, const STRING_UTF8_MAP* aProperties, PROJECT* aProject,
PROGRESS_REPORTER* aProgressReporter ) PROGRESS_REPORTER* aProgressReporter )

View File

@ -92,12 +92,6 @@ public:
*/ */
virtual bool CanReadFootprint( const wxString& aFileName ) const; virtual bool CanReadFootprint( const wxString& aFileName ) const;
/**
* Checks if this PCB_IO can read footprint library from specified file or directory.
* If not overriden, extension check is used.
*/
bool CanReadLibrary( const wxString& aFileName ) const override;
/** /**
* Registers a KIDIALOG callback for collecting info from the user. * Registers a KIDIALOG callback for collecting info from the user.
*/ */