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 <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, const STRING_UTF8_MAP* aProperties )
47{
48 NOT_IMPLEMENTED( __FUNCTION__ );
49}
50
51
52bool IO_BASE::DeleteLibrary( const wxString& aLibraryPath, const STRING_UTF8_MAP* aProperties )
53{
54 NOT_IMPLEMENTED( __FUNCTION__ );
55}
56
57
58bool IO_BASE::IsLibraryWritable( const wxString& aLibraryPath )
59{
60 NOT_IMPLEMENTED( __FUNCTION__ );
61}
62
63void IO_BASE::GetLibraryOptions( STRING_UTF8_MAP* aListToAppendTo ) const
64{
65 // No global options to append
66}
67
68
69bool IO_BASE::CanReadLibrary( const wxString& aFileName ) const
70{
72
73 if( desc.m_IsFile )
74 {
75 const std::vector<std::string>& exts = desc.m_FileExtensions;
76
77 wxString fileExt = wxFileName( aFileName ).GetExt().MakeLower();
78
79 for( const std::string& ext : exts )
80 {
81 if( fileExt == wxString( ext ).Lower() )
82 return true;
83 }
84 }
85 else
86 {
87 wxDir dir( aFileName );
88
89 if( !dir.IsOpened() )
90 return false;
91
92 std::vector<std::string> exts = desc.m_ExtensionsInDir;
93 std::unordered_set<wxString> lowerExts;
94
95 for( const std::string& ext : exts )
96 lowerExts.emplace( wxString( ext ).MakeLower() );
97
98 wxString filenameStr;
99
100 bool cont = dir.GetFirst( &filenameStr, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN );
101
102 while( cont )
103 {
104 wxString ext = wxS( "" );
105
106 int idx = filenameStr.Find( '.', true );
107
108 if( idx != -1 )
109 ext = filenameStr.Mid( idx + 1 ).MakeLower();
110
111 if( lowerExts.count( ext ) )
112 return true;
113
114 cont = dir.GetNext( &filenameStr );
115 }
116 }
117
118 return false;
119}
120
121
122void IO_BASE::Report( const wxString& aText, SEVERITY aSeverity )
123{
124 if( !m_reporter )
125 return;
126
127 m_reporter->Report( aText, aSeverity );
128}
129
130
132{
133 if( !m_progressReporter )
134 return;
135
137 THROW_IO_ERROR( _( "Loading file canceled by user." ) );
138
140}
virtual void AdvanceProgressPhase()
Definition: io_base.cpp:131
REPORTER * m_reporter
Reporter to log errors/warnings to, may be nullptr.
Definition: io_base.h:215
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:52
virtual void GetLibraryOptions(STRING_UTF8_MAP *aListToAppendTo) const
Append supported IO options to aListToAppenTo along with internationalized descriptions.
Definition: io_base.cpp:63
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:46
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:218
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition: io_base.cpp:69
virtual bool IsLibraryWritable(const wxString &aLibraryPath)
Return true if the library at aLibraryPath is writable.
Definition: io_base.cpp:58
virtual void Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Definition: io_base.cpp:122
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.
A name/value tuple with unique names and optional values.
#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:40
std::vector< std::string > m_ExtensionsInDir
In case of folders: extensions of files inside.
Definition: io_base.h:43
bool m_IsFile
Whether the library is a folder or a file.
Definition: io_base.h:44
wxString m_Description
Description shown in the file picker dialog.
Definition: io_base.h:41
std::vector< std::string > m_FileExtensions
Filter used for file pickers if m_IsFile is true.
Definition: io_base.h:42
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.