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 The 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 <progress_reporter.h>
25#include <ki_exception.h>
26#include <reporter.h>
28
29#include <wx/filename.h>
30#include <wx/translation.h>
31#include <wx/dir.h>
32
33#define FMT_UNIMPLEMENTED wxT( "IO interface \"%s\" does not implement the \"%s\" function." )
34#define NOT_IMPLEMENTED( aCaller ) \
35 THROW_IO_ERROR( wxString::Format( FMT_UNIMPLEMENTED, \
36 GetName(), \
37 wxString::FromUTF8( aCaller ) ) );
38
39
41{
42 return wxGetTranslation( m_Description ) + AddFileExtListToFilter( m_FileExtensions );
43}
44
45
46void IO_BASE::CreateLibrary( const wxString& aLibraryPath,
47 const std::map<std::string, UTF8>* aProperties )
48{
49 NOT_IMPLEMENTED( __FUNCTION__ );
50}
51
52
53bool IO_BASE::DeleteLibrary( const wxString& aLibraryPath,
54 const std::map<std::string, UTF8>* aProperties )
55{
56 NOT_IMPLEMENTED( __FUNCTION__ );
57}
58
59
60bool IO_BASE::IsLibraryWritable( const wxString& aLibraryPath )
61{
62 NOT_IMPLEMENTED( __FUNCTION__ );
63}
64
65void IO_BASE::GetLibraryOptions( std::map<std::string, UTF8>* aListToAppendTo ) const
66{
67 // No global options to append
68}
69
70
71bool IO_BASE::CanReadLibrary( const wxString& aFileName ) const
72{
74
75 if( desc.m_IsFile )
76 {
77 const std::vector<std::string>& exts = desc.m_FileExtensions;
78
79 wxString fileExt = wxFileName( aFileName ).GetExt().MakeLower();
80
81 for( const std::string& ext : exts )
82 {
83 if( fileExt == wxString( ext ).Lower() )
84 return true;
85 }
86 }
87 else
88 {
89 wxDir dir( aFileName );
90
91 if( !dir.IsOpened() )
92 return false;
93
94 std::vector<std::string> exts = desc.m_ExtensionsInDir;
95 std::unordered_set<wxString> lowerExts;
96
97 for( const std::string& ext : exts )
98 lowerExts.emplace( wxString( ext ).MakeLower() );
99
100 wxString filenameStr;
101
102 bool cont = dir.GetFirst( &filenameStr, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN );
103
104 while( cont )
105 {
106 wxString ext = wxS( "" );
107
108 int idx = filenameStr.Find( '.', true );
109
110 if( idx != -1 )
111 ext = filenameStr.Mid( idx + 1 ).MakeLower();
112
113 if( lowerExts.count( ext ) )
114 return true;
115
116 cont = dir.GetNext( &filenameStr );
117 }
118 }
119
120 return false;
121}
122
123
124void IO_BASE::Report( const wxString& aText, SEVERITY aSeverity )
125{
126 if( !m_reporter )
127 return;
128
129 m_reporter->Report( aText, aSeverity );
130}
131
132
134{
135 if( !m_progressReporter )
136 return;
137
139 THROW_IO_ERROR( _( "Loading file canceled by user." ) );
140
142}
virtual void AdvanceProgressPhase()
Definition: io_base.cpp:133
REPORTER * m_reporter
Reporter to log errors/warnings to, may be nullptr.
Definition: io_base.h:223
virtual bool DeleteLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr)
Delete an existing library and returns true, or if library does not exist returns false,...
Definition: io_base.cpp:53
virtual const IO_FILE_DESC GetLibraryDesc() const =0
Get the descriptor for the library container that this IO plugin operates on.
virtual void GetLibraryOptions(std::map< std::string, UTF8 > *aListToAppendTo) const
Append supported IO options to aListToAppenTo along with internationalized descriptions.
Definition: io_base.cpp:65
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:226
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition: io_base.cpp:71
virtual bool IsLibraryWritable(const wxString &aLibraryPath)
Return true if the library at aLibraryPath is writable.
Definition: io_base.cpp:60
virtual void CreateLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr)
Create a new empty library at aLibraryPath empty.
Definition: io_base.cpp:46
virtual void Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Definition: io_base.cpp:124
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual void AdvancePhase()=0
Use the next available virtual zone of the dialog progress bar.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
#define _(s)
#define NOT_IMPLEMENTED(aCaller)
Definition: io_base.cpp:34
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
SEVERITY
Container that describes file type info.
Definition: io_base.h:43
std::vector< std::string > m_ExtensionsInDir
Definition: io_base.h:50
bool m_IsFile
Whether the library is a folder or a file.
Definition: io_base.h:51
wxString m_Description
Description shown in the file picker dialog.
Definition: io_base.h:44
std::vector< std::string > m_FileExtensions
Filter used for file pickers if m_IsFile is true.
Definition: io_base.h:47
wxString FileFilter() const
Definition: io_base.cpp:40
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.