You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.8 KiB

  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. #include <unordered_set>
  20. #include <io/io_base.h>
  21. #include <progress_reporter.h>
  22. #include <ki_exception.h>
  23. #include <reporter.h>
  24. #include <wildcards_and_files_ext.h>
  25. #include <wx/filename.h>
  26. #include <wx/translation.h>
  27. #include <wx/dir.h>
  28. #define FMT_UNIMPLEMENTED wxT( "IO interface \"%s\" does not implement the \"%s\" function." )
  29. #define NOT_IMPLEMENTED( aCaller ) \
  30. THROW_IO_ERROR( wxString::Format( FMT_UNIMPLEMENTED, \
  31. GetName(), \
  32. wxString::FromUTF8( aCaller ) ) );
  33. wxString IO_BASE::IO_FILE_DESC::FileFilter() const
  34. {
  35. return wxGetTranslation( m_Description ) + AddFileExtListToFilter( m_FileExtensions );
  36. }
  37. void IO_BASE::CreateLibrary( const wxString& aLibraryPath,
  38. const std::map<std::string, UTF8>* aProperties )
  39. {
  40. NOT_IMPLEMENTED( __FUNCTION__ );
  41. }
  42. bool IO_BASE::DeleteLibrary( const wxString& aLibraryPath,
  43. const std::map<std::string, UTF8>* aProperties )
  44. {
  45. NOT_IMPLEMENTED( __FUNCTION__ );
  46. }
  47. bool IO_BASE::IsLibraryWritable( const wxString& aLibraryPath )
  48. {
  49. NOT_IMPLEMENTED( __FUNCTION__ );
  50. }
  51. void IO_BASE::GetLibraryOptions( std::map<std::string, UTF8>* aListToAppendTo ) const
  52. {
  53. // No global options to append
  54. }
  55. bool IO_BASE::CanReadLibrary( const wxString& aFileName ) const
  56. {
  57. const IO_BASE::IO_FILE_DESC& desc = GetLibraryDesc();
  58. if( desc.m_IsFile )
  59. {
  60. const std::vector<std::string>& exts = desc.m_FileExtensions;
  61. wxString fileExt = wxFileName( aFileName ).GetExt().Lower();
  62. for( const std::string& ext : exts )
  63. {
  64. if( fileExt == wxString( ext ).Lower() )
  65. return true;
  66. }
  67. }
  68. else
  69. {
  70. wxDir dir( aFileName );
  71. if( !dir.IsOpened() )
  72. return false;
  73. std::vector<std::string> exts = desc.m_ExtensionsInDir;
  74. std::unordered_set<wxString> lowerExts;
  75. for( const std::string& ext : exts )
  76. lowerExts.emplace( wxString( ext ).MakeLower() );
  77. wxString filenameStr;
  78. bool cont = dir.GetFirst( &filenameStr, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN );
  79. while( cont )
  80. {
  81. wxString ext = wxS( "" );
  82. int idx = filenameStr.Find( '.', true );
  83. if( idx != -1 )
  84. ext = filenameStr.Mid( idx + 1 ).MakeLower();
  85. if( lowerExts.count( ext ) )
  86. return true;
  87. cont = dir.GetNext( &filenameStr );
  88. }
  89. }
  90. return false;
  91. }
  92. void IO_BASE::Report( const wxString& aText, SEVERITY aSeverity )
  93. {
  94. if( !m_reporter )
  95. return;
  96. m_reporter->Report( aText, aSeverity );
  97. }
  98. void IO_BASE::AdvanceProgressPhase()
  99. {
  100. if( !m_progressReporter )
  101. return;
  102. if( !m_progressReporter->KeepRefreshing() )
  103. THROW_IO_ERROR( _( "Loading file canceled by user." ) );
  104. m_progressReporter->AdvancePhase();
  105. }