kicad/common/io/io_base.cpp

118 lines
3.2 KiB
C++
Raw Normal View History

2023-12-27 00:25:41 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2023-12-27 16:43:05 +00:00
#include <unordered_set>
2023-12-27 00:25:41 +00:00
#include <io/io_base.h>
#include <ki_exception.h>
2023-12-27 16:34:59 +00:00
#include <wildcards_and_files_ext.h>
2023-12-27 16:43:05 +00:00
#include <wx/filename.h>
2023-12-27 16:34:59 +00:00
#include <wx/translation.h>
2023-12-27 16:43:05 +00:00
#include <wx/dir.h>
2023-12-27 00:25:41 +00:00
#define FMT_UNIMPLEMENTED wxT( "IO interface \"%s\" does not implement the \"%s\" function." )
#define NOT_IMPLEMENTED( aCaller ) \
THROW_IO_ERROR( wxString::Format( FMT_UNIMPLEMENTED, \
GetName(), \
wxString::FromUTF8( aCaller ) ) );
2023-12-27 16:34:59 +00:00
wxString IO_BASE::IO_FILE_DESC::FileFilter() const
{
return wxGetTranslation( m_Description ) + AddFileExtListToFilter( m_FileExtensions );
}
2023-12-27 00:25:41 +00:00
void IO_BASE::CreateLibrary( const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties )
{
NOT_IMPLEMENTED( __FUNCTION__ );
}
bool IO_BASE::DeleteLibrary( const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties )
{
NOT_IMPLEMENTED( __FUNCTION__ );
}
bool IO_BASE::IsLibraryWritable( const wxString& aLibraryPath )
{
NOT_IMPLEMENTED( __FUNCTION__ );
}
void IO_BASE::GetLibraryOptions( STRING_UTF8_MAP* aListToAppendTo ) const
{
// No global options to append
}
bool IO_BASE::CanReadLibrary( const wxString& aFileName ) const
{
2023-12-27 16:43:05 +00:00
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 );
}
}
2023-12-27 00:25:41 +00:00
return false;
}