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.

173 lines
5.0 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-2017 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 <sch_eagle_plugin.h>
  27. #include <wildcards_and_files_ext.h>
  28. #define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
  29. #define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
  30. // Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
  31. // and code size. Until then, use the simplest method:
  32. // This implementation is one of two which could be done.
  33. // The other one would cater to DLL/DSO's. But since it would be nearly
  34. // impossible to link a KICAD type DLL/DSO right now without pulling in all
  35. // ::Draw() functions, I forgo that option temporarily.
  36. // Some day it may be possible to have some built in AND some DLL/DSO
  37. // plugins coexisting.
  38. SCH_PLUGIN* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
  39. {
  40. // This implementation is subject to change, any magic is allowed here.
  41. // The public SCH_IO_MGR API is the only pertinent public information.
  42. switch( aFileType )
  43. {
  44. case SCH_LEGACY:
  45. return new SCH_LEGACY_PLUGIN();
  46. case SCH_EAGLE:
  47. return new SCH_EAGLE_PLUGIN();
  48. }
  49. return NULL;
  50. }
  51. void SCH_IO_MGR::ReleasePlugin( SCH_PLUGIN* aPlugin )
  52. {
  53. // This function is a place holder for a future point in time where
  54. // the plugin is a DLL/DSO. It could do reference counting, and then
  55. // unload the DLL/DSO when count goes to zero.
  56. delete aPlugin;
  57. }
  58. const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aType )
  59. {
  60. // keep this function in sync with EnumFromStr() relative to the
  61. // text spellings. If you change the spellings, you will obsolete
  62. // library tables, so don't do change, only additions are ok.
  63. switch( aType )
  64. {
  65. default:
  66. return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ), aType );
  67. case SCH_LEGACY:
  68. return wxString( wxT( "Legacy" ) );
  69. case SCH_EAGLE:
  70. return wxString( wxT( "EAGLE" ) );
  71. }
  72. }
  73. SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
  74. {
  75. // keep this function in sync with ShowType() relative to the
  76. // text spellings. If you change the spellings, you will obsolete
  77. // library tables, so don't do change, only additions are ok.
  78. if( aType == wxT( "Legacy" ) )
  79. return SCH_LEGACY;
  80. else if( aType == wxT( "EAGLE" ) )
  81. return SCH_EAGLE;
  82. // wxASSERT( blow up here )
  83. return SCH_FILE_T( -1 );
  84. }
  85. const wxString SCH_IO_MGR::GetFileExtension( SCH_FILE_T aFileType )
  86. {
  87. wxString ext = wxEmptyString;
  88. SCH_PLUGIN* plugin = FindPlugin( aFileType );
  89. if( plugin != NULL )
  90. {
  91. ext = plugin->GetFileExtension();
  92. ReleasePlugin( plugin );
  93. }
  94. return ext;
  95. }
  96. SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
  97. {
  98. SCH_FILE_T ret = SCH_LEGACY; // default guess, unless detected otherwise.
  99. wxFileName fn( aLibPath );
  100. if( fn.GetExt() == SchematicFileExtension )
  101. {
  102. ret = SCH_LEGACY;
  103. }
  104. return ret;
  105. }
  106. SCH_SHEET* SCH_IO_MGR::Load( SCH_FILE_T aFileType, const wxString& aFileName, KIWAY* aKiway,
  107. SCH_SHEET* aAppendToMe, const PROPERTIES* aProperties )
  108. {
  109. // release the SCH_PLUGIN even if an exception is thrown.
  110. SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( FindPlugin( aFileType ) );
  111. if( (SCH_PLUGIN*) pi ) // test pi->plugin
  112. {
  113. return pi->Load( aFileName, aKiway, aAppendToMe, aProperties ); // virtual
  114. }
  115. THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
  116. }
  117. void SCH_IO_MGR::Save( SCH_FILE_T aFileType, const wxString& aFileName,
  118. SCH_SCREEN* aSchematic, KIWAY* aKiway, const PROPERTIES* aProperties )
  119. {
  120. // release the SCH_PLUGIN even if an exception is thrown.
  121. SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( FindPlugin( aFileType ) );
  122. if( (SCH_PLUGIN*) pi ) // test pi->plugin
  123. {
  124. pi->Save( aFileName, aSchematic, aKiway, aProperties ); // virtual
  125. return;
  126. }
  127. THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
  128. }
  129. DECLARE_ENUM_VECTOR( SCH_IO_MGR, SCH_FILE_T )