2011-11-28 04:32:29 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2012-04-17 14:54:48 +00:00
|
|
|
* Copyright (C) 2011-2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2011-11-28 04:32:29 +00:00
|
|
|
* Copyright (C) 2011 KiCad Developers, see change_log.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2012-11-19 16:19:38 +00:00
|
|
|
#include <wx/filename.h>
|
2013-09-23 15:19:39 +00:00
|
|
|
#include <wx/uri.h>
|
2011-11-28 04:32:29 +00:00
|
|
|
|
|
|
|
#include <io_mgr.h>
|
2012-04-07 18:05:56 +00:00
|
|
|
#include <legacy_plugin.h>
|
2011-11-28 04:32:29 +00:00
|
|
|
#include <kicad_plugin.h>
|
2012-05-20 13:14:46 +00:00
|
|
|
#include <eagle_plugin.h>
|
2012-12-29 09:54:25 +00:00
|
|
|
#include <pcad2kicadpcb_plugin/pcad_plugin.h>
|
2012-12-28 20:52:12 +00:00
|
|
|
#include <gpcb_plugin.h>
|
2013-11-11 08:01:04 +00:00
|
|
|
#include <config.h>
|
2013-09-21 07:30:23 +00:00
|
|
|
|
|
|
|
#if defined(BUILD_GITHUB_PLUGIN)
|
|
|
|
#include <github/github_plugin.h>
|
|
|
|
#endif
|
|
|
|
|
2012-11-19 16:19:38 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2011-11-28 04:32:29 +00:00
|
|
|
|
2012-04-16 03:18:41 +00:00
|
|
|
#define FMT_UNIMPLEMENTED _( "Plugin '%s' does not implement the '%s' function." )
|
|
|
|
#define FMT_NOTFOUND _( "Plugin type '%s' is not found." )
|
|
|
|
|
|
|
|
|
2013-11-02 19:49:46 +00:00
|
|
|
// is there a better place for this function?
|
2014-01-02 02:17:07 +00:00
|
|
|
bool PROPERTIES::Value( const char* aName, UTF8* aFetchedValue ) const
|
2013-11-02 19:49:46 +00:00
|
|
|
{
|
|
|
|
PROPERTIES::const_iterator it = find( aName );
|
|
|
|
|
|
|
|
if( it != end() )
|
|
|
|
{
|
|
|
|
if( aFetchedValue )
|
|
|
|
*aFetchedValue = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-30 07:43:46 +00:00
|
|
|
// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
|
|
|
|
// and code size. Until then, use the simplest method:
|
2011-11-28 04:32:29 +00:00
|
|
|
|
|
|
|
// This implementation is one of two which could be done.
|
2011-11-30 07:43:46 +00:00
|
|
|
// The other one would cater to DLL/DSO's. But since it would be nearly
|
2011-11-28 04:32:29 +00:00
|
|
|
// impossible to link a KICAD type DLL/DSO right now without pulling in all
|
2011-11-30 07:43:46 +00:00
|
|
|
// ::Draw() functions, I forgo that option temporarily.
|
2011-11-28 04:32:29 +00:00
|
|
|
|
2011-11-30 07:43:46 +00:00
|
|
|
// Some day it may be possible to have some built in AND some DLL/DSO
|
|
|
|
// plugins coexisting.
|
2011-11-28 04:32:29 +00:00
|
|
|
|
2011-11-30 07:43:46 +00:00
|
|
|
|
2011-11-28 04:32:29 +00:00
|
|
|
PLUGIN* IO_MGR::PluginFind( PCB_FILE_T aFileType )
|
|
|
|
{
|
2011-11-30 07:43:46 +00:00
|
|
|
// This implementation is subject to change, any magic is allowed here.
|
|
|
|
// The public IO_MGR API is the only pertinent public information.
|
|
|
|
|
2011-11-28 04:32:29 +00:00
|
|
|
switch( aFileType )
|
|
|
|
{
|
2012-04-07 18:05:56 +00:00
|
|
|
case LEGACY:
|
|
|
|
return new LEGACY_PLUGIN();
|
|
|
|
|
2011-12-05 17:22:19 +00:00
|
|
|
case KICAD:
|
2012-04-22 15:16:39 +00:00
|
|
|
return new PCB_IO();
|
2011-12-05 17:22:19 +00:00
|
|
|
|
|
|
|
case EAGLE:
|
2012-05-20 13:14:46 +00:00
|
|
|
return new EAGLE_PLUGIN();
|
2012-12-28 20:52:12 +00:00
|
|
|
|
2012-12-29 09:54:25 +00:00
|
|
|
case PCAD:
|
|
|
|
return new PCAD_PLUGIN();
|
|
|
|
|
2012-12-28 20:52:12 +00:00
|
|
|
case GEDA_PCB:
|
|
|
|
return new GPCB_PLUGIN();
|
2013-09-21 07:30:23 +00:00
|
|
|
|
|
|
|
case GITHUB:
|
|
|
|
#if defined(BUILD_GITHUB_PLUGIN)
|
|
|
|
return new GITHUB_PLUGIN();
|
2013-10-17 18:46:20 +00:00
|
|
|
#else
|
|
|
|
THROW_IO_ERROR( "BUILD_GITHUB_PLUGIN not enabled in cmake build environment" );
|
2013-09-21 07:30:23 +00:00
|
|
|
#endif
|
2011-11-28 04:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IO_MGR::PluginRelease( PLUGIN* aPlugin )
|
|
|
|
{
|
|
|
|
// This function is a place holder for a future point in time where
|
|
|
|
// the plugin is a DLL/DSO. It could do reference counting, and then
|
|
|
|
// unload the DLL/DSO when count goes to zero.
|
2011-12-05 17:22:19 +00:00
|
|
|
|
|
|
|
delete aPlugin;
|
2011-11-28 04:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-17 15:12:17 +00:00
|
|
|
const wxString IO_MGR::ShowType( PCB_FILE_T aType )
|
2011-11-28 04:32:29 +00:00
|
|
|
{
|
2012-10-17 15:12:17 +00:00
|
|
|
// keep this function in sync with EnumFromStr() relative to the
|
|
|
|
// text spellings. If you change the spellings, you will obsolete
|
|
|
|
// library tables, so don't do change, only additions are ok.
|
|
|
|
|
|
|
|
switch( aType )
|
2011-11-28 04:32:29 +00:00
|
|
|
{
|
|
|
|
default:
|
2012-10-17 15:12:17 +00:00
|
|
|
return wxString::Format( _( "Unknown PCB_FILE_T value: %d" ), aType );
|
2011-12-02 21:56:47 +00:00
|
|
|
|
2012-04-07 18:05:56 +00:00
|
|
|
case LEGACY:
|
2012-10-17 15:12:17 +00:00
|
|
|
return wxString( wxT( "Legacy" ) );
|
2012-04-07 18:05:56 +00:00
|
|
|
|
2011-12-02 21:56:47 +00:00
|
|
|
case KICAD:
|
|
|
|
return wxString( wxT( "KiCad" ) );
|
2012-10-17 15:12:17 +00:00
|
|
|
|
|
|
|
case EAGLE:
|
|
|
|
return wxString( wxT( "Eagle" ) );
|
2012-12-28 20:52:12 +00:00
|
|
|
|
2012-12-29 09:54:25 +00:00
|
|
|
case PCAD:
|
|
|
|
return wxString( wxT( "P-Cad" ) );
|
|
|
|
|
2012-12-28 20:52:12 +00:00
|
|
|
case GEDA_PCB:
|
|
|
|
return wxString( wxT( "Geda-PCB" ) );
|
2013-09-21 07:30:23 +00:00
|
|
|
|
|
|
|
case GITHUB:
|
|
|
|
return wxString( wxT( "Github" ) );
|
2011-11-28 04:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-17 15:12:17 +00:00
|
|
|
IO_MGR::PCB_FILE_T IO_MGR::EnumFromStr( const wxString& aType )
|
|
|
|
{
|
|
|
|
// keep this function in sync with ShowType() relative to the
|
|
|
|
// text spellings. If you change the spellings, you will obsolete
|
|
|
|
// library tables, so don't do change, only additions are ok.
|
|
|
|
|
|
|
|
if( aType == wxT( "KiCad" ) )
|
|
|
|
return KICAD;
|
|
|
|
|
|
|
|
if( aType == wxT( "Legacy" ) )
|
|
|
|
return LEGACY;
|
|
|
|
|
|
|
|
if( aType == wxT( "Eagle" ) )
|
|
|
|
return EAGLE;
|
|
|
|
|
2012-12-29 09:54:25 +00:00
|
|
|
if( aType == wxT( "P-Cad" ) )
|
|
|
|
return PCAD;
|
|
|
|
|
2012-12-28 20:52:12 +00:00
|
|
|
if( aType == wxT( "Geda-PCB" ) )
|
|
|
|
return GEDA_PCB;
|
|
|
|
|
2013-09-21 07:30:23 +00:00
|
|
|
if( aType == wxT( "Github" ) )
|
|
|
|
return GITHUB;
|
|
|
|
|
2012-10-17 15:12:17 +00:00
|
|
|
// wxASSERT( blow up here )
|
|
|
|
|
|
|
|
return PCB_FILE_T( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-07 18:05:56 +00:00
|
|
|
const wxString IO_MGR::GetFileExtension( PCB_FILE_T aFileType )
|
|
|
|
{
|
|
|
|
wxString ext = wxEmptyString;
|
|
|
|
PLUGIN* plugin = PluginFind( aFileType );
|
|
|
|
|
|
|
|
if( plugin != NULL )
|
|
|
|
{
|
|
|
|
ext = plugin->GetFileExtension();
|
|
|
|
PluginRelease( plugin );
|
|
|
|
}
|
|
|
|
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-19 16:19:38 +00:00
|
|
|
IO_MGR::PCB_FILE_T IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
|
|
|
|
{
|
2013-09-23 15:19:39 +00:00
|
|
|
PCB_FILE_T ret = KICAD; // default guess, unless detected otherwise.
|
|
|
|
wxFileName fn( aLibPath );
|
2012-11-19 16:19:38 +00:00
|
|
|
|
|
|
|
if( fn.GetExt() == LegacyFootprintLibPathExtension )
|
2012-12-28 20:52:12 +00:00
|
|
|
{
|
2012-11-19 16:19:38 +00:00
|
|
|
ret = LEGACY;
|
2012-12-28 20:52:12 +00:00
|
|
|
}
|
|
|
|
else if( fn.GetExt() == GedaPcbFootprintLibFileExtension )
|
|
|
|
{
|
|
|
|
ret = GEDA_PCB;
|
|
|
|
}
|
2013-01-03 16:33:22 +00:00
|
|
|
else if( fn.GetExt() == EagleFootprintLibPathExtension )
|
|
|
|
{
|
|
|
|
ret = EAGLE;
|
|
|
|
}
|
2013-09-21 07:30:23 +00:00
|
|
|
|
2013-09-23 15:57:15 +00:00
|
|
|
// Test this one anyways, even though it's the default guess, to avoid
|
2013-09-23 15:19:39 +00:00
|
|
|
// the wxURI instantiation below.
|
|
|
|
// We default ret to KICAD above, because somebody might have
|
|
|
|
// mistakenly put a pretty library into a directory other than
|
|
|
|
// *.pretty/ with *.kicad_mod in there., and I don't want to return -1,
|
|
|
|
// since we only claimed to be guessing.
|
2014-09-18 10:29:52 +00:00
|
|
|
//
|
|
|
|
// However libraries on GitHub have names ending by .pretty
|
|
|
|
// so test also this is not a name starting by http (including https).
|
|
|
|
else if( fn.GetExt() == KiCadFootprintLibPathExtension &&
|
|
|
|
!aLibPath.StartsWith( wxT( "http" ) ) )
|
2012-11-19 16:19:38 +00:00
|
|
|
{
|
|
|
|
ret = KICAD;
|
|
|
|
}
|
2013-09-23 15:19:39 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// There is no extension for a remote repo, so test the server name.
|
|
|
|
wxURI uri( aLibPath );
|
|
|
|
|
|
|
|
if( uri.HasServer() && uri.GetServer() == wxT( "github.com" ) )
|
|
|
|
{
|
|
|
|
ret = GITHUB;
|
|
|
|
}
|
|
|
|
}
|
2012-11-19 16:19:38 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-28 04:32:29 +00:00
|
|
|
BOARD* IO_MGR::Load( PCB_FILE_T aFileType, const wxString& aFileName,
|
2013-09-25 19:17:06 +00:00
|
|
|
BOARD* aAppendToMe, const PROPERTIES* aProperties )
|
2011-11-28 04:32:29 +00:00
|
|
|
{
|
|
|
|
// release the PLUGIN even if an exception is thrown.
|
2013-10-05 10:34:55 +00:00
|
|
|
PLUGIN::RELEASER pi( PluginFind( aFileType ) );
|
2011-11-28 04:32:29 +00:00
|
|
|
|
|
|
|
if( (PLUGIN*) pi ) // test pi->plugin
|
|
|
|
{
|
|
|
|
return pi->Load( aFileName, aAppendToMe, aProperties ); // virtual
|
|
|
|
}
|
|
|
|
|
2012-04-16 03:18:41 +00:00
|
|
|
THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
|
2011-11-28 04:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-25 19:17:06 +00:00
|
|
|
void IO_MGR::Save( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aBoard, const PROPERTIES* aProperties )
|
2011-12-02 16:57:44 +00:00
|
|
|
{
|
|
|
|
// release the PLUGIN even if an exception is thrown.
|
2013-10-05 10:34:55 +00:00
|
|
|
PLUGIN::RELEASER pi( PluginFind( aFileType ) );
|
2011-12-02 16:57:44 +00:00
|
|
|
|
|
|
|
if( (PLUGIN*) pi ) // test pi->plugin
|
|
|
|
{
|
|
|
|
pi->Save( aFileName, aBoard, aProperties ); // virtual
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-16 03:18:41 +00:00
|
|
|
THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
|
2011-12-02 16:57:44 +00:00
|
|
|
}
|
|
|
|
|