KiCad PCB EDA Suite
Loading...
Searching...
No Matches
io_base.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21#include <unordered_set>
22
23#include <io/io_base.h>
24#include <ki_exception.h>
26
27#include <wx/filename.h>
28#include <wx/translation.h>
29#include <wx/dir.h>
30
31#define FMT_UNIMPLEMENTED wxT( "IO interface \"%s\" does not implement the \"%s\" function." )
32#define NOT_IMPLEMENTED( aCaller ) \
33 THROW_IO_ERROR( wxString::Format( FMT_UNIMPLEMENTED, \
34 GetName(), \
35 wxString::FromUTF8( aCaller ) ) );
36
37
39{
40 return wxGetTranslation( m_Description ) + AddFileExtListToFilter( m_FileExtensions );
41}
42
43
44void IO_BASE::CreateLibrary( const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties )
45{
46 NOT_IMPLEMENTED( __FUNCTION__ );
47}
48
49
50bool IO_BASE::DeleteLibrary( const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties )
51{
52 NOT_IMPLEMENTED( __FUNCTION__ );
53}
54
55
56bool IO_BASE::IsLibraryWritable( const wxString& aLibraryPath )
57{
58 NOT_IMPLEMENTED( __FUNCTION__ );
59}
60
61void IO_BASE::GetLibraryOptions( STRING_UTF8_MAP* aListToAppendTo ) const
62{
63 // No global options to append
64}
65
66
67bool IO_BASE::CanReadLibrary( const wxString& aFileName ) const
68{
70
71 if( desc.m_IsFile )
72 {
73 const std::vector<std::string>& exts = desc.m_FileExtensions;
74
75 wxString fileExt = wxFileName( aFileName ).GetExt().MakeLower();
76
77 for( const std::string& ext : exts )
78 {
79 if( fileExt == wxString( ext ).Lower() )
80 return true;
81 }
82 }
83 else
84 {
85 wxDir dir( aFileName );
86
87 if( !dir.IsOpened() )
88 return false;
89
90 std::vector<std::string> exts = desc.m_ExtensionsInDir;
91 std::unordered_set<wxString> lowerExts;
92
93 for( const std::string& ext : exts )
94 lowerExts.emplace( wxString( ext ).MakeLower() );
95
96 wxString filenameStr;
97
98 bool cont = dir.GetFirst( &filenameStr, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN );
99
100 while( cont )
101 {
102 wxString ext = wxS( "" );
103
104 int idx = filenameStr.Find( '.', true );
105
106 if( idx != -1 )
107 ext = filenameStr.Mid( idx + 1 ).MakeLower();
108
109 if( lowerExts.count( ext ) )
110 return true;
111
112 cont = dir.GetNext( &filenameStr );
113 }
114 }
115
116 return false;
117}
virtual bool DeleteLibrary(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
Delete an existing library and returns true, or if library does not exist returns false,...
Definition: io_base.cpp:50
virtual void GetLibraryOptions(STRING_UTF8_MAP *aListToAppendTo) const
Append supported IO options to aListToAppenTo along with internationalized descriptions.
Definition: io_base.cpp:61
virtual const IO_FILE_DESC GetLibraryDesc() const =0
Get the descriptor for the library container that this IO plugin operates on.
virtual void CreateLibrary(const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr)
Create a new empty library at aLibraryPath empty.
Definition: io_base.cpp:44
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition: io_base.cpp:67
virtual bool IsLibraryWritable(const wxString &aLibraryPath)
Return true if the library at aLibraryPath is writable.
Definition: io_base.cpp:56
A name/value tuple with unique names and optional values.
#define NOT_IMPLEMENTED(aCaller)
Definition: io_base.cpp:32
Container that describes file type info.
Definition: io_base.h:39
std::vector< std::string > m_ExtensionsInDir
In case of folders: extensions of files inside.
Definition: io_base.h:42
bool m_IsFile
Whether the library is a folder or a file.
Definition: io_base.h:43
wxString m_Description
Description shown in the file picker dialog.
Definition: io_base.h:40
std::vector< std::string > m_FileExtensions
Filter used for file pickers if m_IsFile is true.
Definition: io_base.h:41
wxString FileFilter() const
Definition: io_base.cpp:38
wxString AddFileExtListToFilter(const std::vector< std::string > &aExts)
Build the wildcard extension file dialog wildcard filter to add to the base message dialog.
Definition of file extensions used in Kicad.