io_mgr.cpp 7.34 KB
Newer Older
1 2 3
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
4
 * Copyright (C) 2011-2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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
 */

25
#include <wx/filename.h>
26
#include <wx/uri.h>
27 28

#include <io_mgr.h>
29
#include <legacy_plugin.h>
30
#include <kicad_plugin.h>
31
#include <eagle_plugin.h>
32
#include <pcad2kicadpcb_plugin/pcad_plugin.h>
33
#include <gpcb_plugin.h>
34
#include <config.h>
35 36 37 38 39

#if defined(BUILD_GITHUB_PLUGIN)
 #include <github/github_plugin.h>
#endif

40
#include <wildcards_and_files_ext.h>
41

42 43 44 45
#define FMT_UNIMPLEMENTED   _( "Plugin '%s' does not implement the '%s' function." )
#define FMT_NOTFOUND        _( "Plugin type '%s' is not found." )


46
// is there a better place for this function?
47
bool PROPERTIES::Value( const char* aName, UTF8* aFetchedValue ) const
48 49 50 51 52 53 54 55 56 57 58 59 60
{
    PROPERTIES::const_iterator it = find( aName );

    if( it != end() )
    {
        if( aFetchedValue )
            *aFetchedValue = it->second;
        return true;
    }
    return false;
}


61 62
// Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
// and code size.  Until then, use the simplest method:
63 64

// This implementation is one of two which could be done.
65
// The other one would cater to DLL/DSO's.  But since it would be nearly
66
// impossible to link a KICAD type DLL/DSO right now without pulling in all
67
// ::Draw() functions, I forgo that option temporarily.
68

69 70
// Some day it may be possible to have some built in AND some DLL/DSO
// plugins coexisting.
71

72

73 74
PLUGIN* IO_MGR::PluginFind( PCB_FILE_T aFileType )
{
75 76 77
    // This implementation is subject to change, any magic is allowed here.
    // The public IO_MGR API is the only pertinent public information.

78 79
    switch( aFileType )
    {
80 81 82
    case LEGACY:
        return new LEGACY_PLUGIN();

83
    case KICAD:
84
        return new PCB_IO();
85 86

    case EAGLE:
87
        return new EAGLE_PLUGIN();
88

89 90 91
    case PCAD:
        return new PCAD_PLUGIN();

92 93
    case GEDA_PCB:
        return new GPCB_PLUGIN();
94 95 96 97

    case GITHUB:
#if defined(BUILD_GITHUB_PLUGIN)
        return new GITHUB_PLUGIN();
98 99
#else
        THROW_IO_ERROR( "BUILD_GITHUB_PLUGIN not enabled in cmake build environment" );
100
#endif
101 102 103 104 105 106 107 108 109 110 111
    }

    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.
112 113

    delete aPlugin;
114 115 116
}


Dick Hollenbeck's avatar
Dick Hollenbeck committed
117
const wxString IO_MGR::ShowType( PCB_FILE_T aType )
118
{
Dick Hollenbeck's avatar
Dick Hollenbeck committed
119 120 121 122 123
    // 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 )
124 125
    {
    default:
Dick Hollenbeck's avatar
Dick Hollenbeck committed
126
        return wxString::Format( _( "Unknown PCB_FILE_T value: %d" ), aType );
127

128
    case LEGACY:
Dick Hollenbeck's avatar
Dick Hollenbeck committed
129
        return wxString( wxT( "Legacy" ) );
130

131 132
    case KICAD:
        return wxString( wxT( "KiCad" ) );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
133 134 135

    case EAGLE:
        return wxString( wxT( "Eagle" ) );
136

137 138 139
    case PCAD:
        return wxString( wxT( "P-Cad" ) );

140 141
    case GEDA_PCB:
        return wxString( wxT( "Geda-PCB" ) );
142 143 144

    case GITHUB:
        return wxString( wxT( "Github" ) );
145 146 147 148
    }
}


Dick Hollenbeck's avatar
Dick Hollenbeck committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
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;

164 165 166
    if( aType == wxT( "P-Cad" ) )
        return PCAD;

167 168 169
    if( aType == wxT( "Geda-PCB" ) )
        return GEDA_PCB;

170 171 172
    if( aType == wxT( "Github" ) )
        return GITHUB;

Dick Hollenbeck's avatar
Dick Hollenbeck committed
173 174 175 176 177 178
    // wxASSERT( blow up here )

    return PCB_FILE_T( -1 );
}


179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
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;
}


194 195
IO_MGR::PCB_FILE_T IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
{
196 197
    PCB_FILE_T  ret = KICAD;        // default guess, unless detected otherwise.
    wxFileName  fn( aLibPath );
198 199

    if( fn.GetExt() == LegacyFootprintLibPathExtension )
200
    {
201
        ret = LEGACY;
202 203 204 205 206
    }
    else if( fn.GetExt() == GedaPcbFootprintLibFileExtension )
    {
        ret = GEDA_PCB;
    }
207 208 209 210
    else if( fn.GetExt() == EagleFootprintLibPathExtension )
    {
        ret = EAGLE;
    }
211

212
    // Test this one anyways, even though it's the default guess, to avoid
213 214 215 216 217
    // 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.
218 219 220 221 222
    //
    // 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" ) ) )
223 224 225
    {
        ret = KICAD;
    }
226 227 228 229 230 231 232 233 234 235
    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;
        }
    }
236 237 238 239 240

    return ret;
}


241
BOARD* IO_MGR::Load( PCB_FILE_T aFileType, const wxString& aFileName,
242
                     BOARD* aAppendToMe, const PROPERTIES* aProperties )
243 244
{
    // release the PLUGIN even if an exception is thrown.
245
    PLUGIN::RELEASER pi( PluginFind( aFileType ) );
246 247 248 249 250 251

    if( (PLUGIN*) pi )  // test pi->plugin
    {
        return pi->Load( aFileName, aAppendToMe, aProperties );  // virtual
    }

252
    THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
253 254 255
}


256
void IO_MGR::Save( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aBoard, const PROPERTIES* aProperties )
257 258
{
    // release the PLUGIN even if an exception is thrown.
259
    PLUGIN::RELEASER pi( PluginFind( aFileType ) );
260 261 262 263 264 265 266

    if( (PLUGIN*) pi )  // test pi->plugin
    {
        pi->Save( aFileName, aBoard, aProperties );  // virtual
        return;
    }

267
    THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
268 269
}