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.

164 lines
4.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 CERN
  5. * Copyright (C) 2016 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * @author Wayne Stambaugh <stambaughw@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <wx/filename.h>
  23. #include <wx/uri.h>
  24. #include <sch_io_mgr.h>
  25. #include <sch_legacy_plugin.h>
  26. #include <wildcards_and_files_ext.h>
  27. #define FMT_UNIMPLEMENTED _( "Plugin '%s' does not implement the '%s' function." )
  28. #define FMT_NOTFOUND _( "Plugin type '%s' is not found." )
  29. // Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
  30. // and code size. Until then, use the simplest method:
  31. // This implementation is one of two which could be done.
  32. // The other one would cater to DLL/DSO's. But since it would be nearly
  33. // impossible to link a KICAD type DLL/DSO right now without pulling in all
  34. // ::Draw() functions, I forgo that option temporarily.
  35. // Some day it may be possible to have some built in AND some DLL/DSO
  36. // plugins coexisting.
  37. SCH_PLUGIN* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
  38. {
  39. // This implementation is subject to change, any magic is allowed here.
  40. // The public SCH_IO_MGR API is the only pertinent public information.
  41. switch( aFileType )
  42. {
  43. case SCH_LEGACY:
  44. return new SCH_LEGACY_PLUGIN();
  45. case SCH_KICAD:
  46. return NULL;
  47. }
  48. return NULL;
  49. }
  50. void SCH_IO_MGR::ReleasePlugin( SCH_PLUGIN* aPlugin )
  51. {
  52. // This function is a place holder for a future point in time where
  53. // the plugin is a DLL/DSO. It could do reference counting, and then
  54. // unload the DLL/DSO when count goes to zero.
  55. delete aPlugin;
  56. }
  57. const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aType )
  58. {
  59. // keep this function in sync with EnumFromStr() relative to the
  60. // text spellings. If you change the spellings, you will obsolete
  61. // library tables, so don't do change, only additions are ok.
  62. switch( aType )
  63. {
  64. default:
  65. return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ), aType );
  66. case SCH_LEGACY:
  67. return wxString( wxT( "Legacy" ) );
  68. }
  69. }
  70. SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
  71. {
  72. // keep this function in sync with ShowType() relative to the
  73. // text spellings. If you change the spellings, you will obsolete
  74. // library tables, so don't do change, only additions are ok.
  75. if( aType == wxT( "Legacy" ) )
  76. return SCH_LEGACY;
  77. // wxASSERT( blow up here )
  78. return SCH_FILE_T( -1 );
  79. }
  80. const wxString SCH_IO_MGR::GetFileExtension( SCH_FILE_T aFileType )
  81. {
  82. wxString ext = wxEmptyString;
  83. SCH_PLUGIN* plugin = FindPlugin( aFileType );
  84. if( plugin != NULL )
  85. {
  86. ext = plugin->GetFileExtension();
  87. ReleasePlugin( plugin );
  88. }
  89. return ext;
  90. }
  91. SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
  92. {
  93. SCH_FILE_T ret = SCH_LEGACY; // default guess, unless detected otherwise.
  94. wxFileName fn( aLibPath );
  95. if( fn.GetExt() == SchematicFileWildcard )
  96. {
  97. ret = SCH_LEGACY;
  98. }
  99. return ret;
  100. }
  101. SCH_SHEET* SCH_IO_MGR::Load( SCH_FILE_T aFileType, const wxString& aFileName, KIWAY* aKiway,
  102. SCH_SHEET* aAppendToMe, const PROPERTIES* aProperties )
  103. {
  104. // release the SCH_PLUGIN even if an exception is thrown.
  105. SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( FindPlugin( aFileType ) );
  106. if( (SCH_PLUGIN*) pi ) // test pi->plugin
  107. {
  108. return pi->Load( aFileName, aKiway, aAppendToMe, aProperties ); // virtual
  109. }
  110. THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
  111. }
  112. void SCH_IO_MGR::Save( SCH_FILE_T aFileType, const wxString& aFileName,
  113. SCH_SCREEN* aSchematic, KIWAY* aKiway, const PROPERTIES* aProperties )
  114. {
  115. // release the SCH_PLUGIN even if an exception is thrown.
  116. SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( FindPlugin( aFileType ) );
  117. if( (SCH_PLUGIN*) pi ) // test pi->plugin
  118. {
  119. pi->Save( aFileName, aSchematic, aKiway, aProperties ); // virtual
  120. return;
  121. }
  122. THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
  123. }