KiCad PCB EDA Suite
sch_io_mgr.cpp
Go to the documentation of this file.
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-2020 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 
23 #include <wx/filename.h>
24 #include <wx/uri.h>
25 
26 #include <sch_io_mgr.h>
30 
34 
35 #define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
36 #define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
37 
38 
39 
40 // Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
41 // and code size. Until then, use the simplest method:
42 
43 // This implementation is one of two which could be done.
44 // The other one would cater to DLL/DSO's. But since it would be nearly
45 // impossible to link a KICAD type DLL/DSO right now without pulling in all
46 // ::Draw() functions, I forgo that option temporarily.
47 
48 // Some day it may be possible to have some built in AND some DLL/DSO
49 // plugins coexisting.
50 
51 
52 SCH_PLUGIN* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
53 {
54  // This implementation is subject to change, any magic is allowed here.
55  // The public SCH_IO_MGR API is the only pertinent public information.
56 
57  switch( aFileType )
58  {
59  case SCH_LEGACY:
60  return new SCH_LEGACY_PLUGIN();
61  case SCH_KICAD:
62  return new SCH_SEXPR_PLUGIN();
63  case SCH_ALTIUM:
64  return new SCH_ALTIUM_PLUGIN();
65  case SCH_CADSTAR_ARCHIVE:
66  return new CADSTAR_SCH_ARCHIVE_PLUGIN();
67  case SCH_EAGLE:
68  return new SCH_EAGLE_PLUGIN();
69  default:
70  ;
71  }
72 
73  return NULL;
74 }
75 
76 
78 {
79  // This function is a place holder for a future point in time where
80  // the plugin is a DLL/DSO. It could do reference counting, and then
81  // unload the DLL/DSO when count goes to zero.
82 
83  delete aPlugin;
84 }
85 
86 
87 const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aType )
88 {
89  // keep this function in sync with EnumFromStr() relative to the
90  // text spellings. If you change the spellings, you will obsolete
91  // library tables, so don't do change, only additions are ok.
92 
93  switch( aType )
94  {
95  default:
96  return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ), aType );
97 
98  case SCH_LEGACY:
99  return wxString( wxT( "Legacy" ) );
100 
101  case SCH_KICAD:
102  return wxString( wxT( "KiCad" ) );
103 
104  case SCH_ALTIUM:
105  return wxString( wxT( "Altium" ) );
106 
107  case SCH_CADSTAR_ARCHIVE:
108  return wxString( wxT( "CADSTAR Schematic Archive" ) );
109 
110  case SCH_EAGLE:
111  return wxString( wxT( "EAGLE" ) );
112  }
113 }
114 
115 
116 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
117 {
118  // keep this function in sync with ShowType() relative to the
119  // text spellings. If you change the spellings, you will obsolete
120  // library tables, so don't do change, only additions are ok.
121 
122  if( aType == wxT( "Legacy" ) )
123  return SCH_LEGACY;
124  else if( aType == wxT( "KiCad" ) )
125  return SCH_KICAD;
126  else if( aType == wxT( "Altium" ) )
127  return SCH_ALTIUM;
128  else if( aType == wxT( "CADSTAR Schematic Archive" ) )
129  return SCH_CADSTAR_ARCHIVE;
130  else if( aType == wxT( "EAGLE" ) )
131  return SCH_EAGLE;
132 
133  // wxASSERT( blow up here )
134 
135  return SCH_FILE_T( -1 );
136 }
137 
138 
139 const wxString SCH_IO_MGR::GetFileExtension( SCH_FILE_T aFileType )
140 {
141  wxString ext = wxEmptyString;
142  SCH_PLUGIN* plugin = FindPlugin( aFileType );
143 
144  if( plugin != NULL )
145  {
146  ext = plugin->GetFileExtension();
147  ReleasePlugin( plugin );
148  }
149 
150  return ext;
151 }
152 
153 
154 const wxString SCH_IO_MGR::GetLibraryFileExtension( SCH_FILE_T aFileType )
155 {
156  wxString ext = wxEmptyString;
157  SCH_PLUGIN* plugin = FindPlugin( aFileType );
158 
159  if( plugin != NULL )
160  {
161  ext = plugin->GetLibraryFileExtension();
162  ReleasePlugin( plugin );
163  }
164 
165  return ext;
166 }
167 
168 
169 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
170 {
171  SCH_FILE_T ret = SCH_KICAD; // default guess, unless detected otherwise.
172  wxFileName fn( aLibPath );
173 
174  if( fn.GetExt() == LegacySymbolLibFileExtension )
175  {
176  ret = SCH_LEGACY;
177  }
178  else if( fn.GetExt() == KiCadSymbolLibFileExtension )
179  {
180  ret = SCH_KICAD;
181  }
182 
183  return ret;
184 }
185 
186 
187 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromSchPath( const wxString& aSchematicPath )
188 {
189  SCH_FILE_T ret = SCH_KICAD; // default guess, unless detected otherwise.
190  wxFileName fn( aSchematicPath );
191 
192  if( fn.GetExt() == LegacySchematicFileExtension )
193  {
194  ret = SCH_LEGACY;
195  }
196  else if( fn.GetExt() == KiCadSchematicFileExtension )
197  {
198  ret = SCH_KICAD;
199  }
200 
201  return ret;
202 }
203 
204 
205 DECLARE_ENUM_VECTOR( SCH_IO_MGR, SCH_FILE_T )
SCH_IO_MGR is a factory which returns an instance of a SCH_PLUGIN.
Definition: sch_io_mgr.h:46
static SCH_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath)
Return a plugin type given a symbol library using the file extension of aLibPath.
Definition: sch_io_mgr.cpp:169
virtual const wxString GetLibraryFileExtension() const =0
Return the library file extension for the SCH_PLUGIN object.
const std::string LegacySymbolLibFileExtension
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
Definition: sch_io_mgr.cpp:87
static const wxString GetLibraryFileExtension(SCH_FILE_T aFileType)
Return the symbol library file extension (if any) for aFileType.
Definition: sch_io_mgr.cpp:154
SCH_EAGLE_PLUGIN is a SCH_PLUGIN derivation for loading 6.x+ Eagle schematic files.
static SCH_FILE_T GuessPluginTypeFromSchPath(const wxString &aSchematicPath)
Return a plugin type given a schematic using the file extension of aSchematicPath.
Definition: sch_io_mgr.cpp:187
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:153
#define NULL
#define DECLARE_ENUM_VECTOR(class_name, enum_name)
Definition: enum_vector.h:43
static const wxString GetFileExtension(SCH_FILE_T aFileType)
Return the schematic file extension for aFileType.
Definition: sch_io_mgr.cpp:139
Definition of file extensions used in Kicad.
SCH_ALTIUM_PLUGIN is a SCH_PLUGIN derivation for loading Altium .SchDoc schematic files.
const std::string LegacySchematicFileExtension
A SCH_PLUGIN derivation for loading schematic files using the new s-expression file format.
A SCH_PLUGIN derivation for loading schematic files created before the new s-expression file format.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
static SCH_FILE_T EnumFromStr(const wxString &aFileType)
Return the #SCH_FILE_T from the corresponding plugin type name: "kicad", "legacy",...
Definition: sch_io_mgr.cpp:116
#define _(s)
Definition: 3d_actions.cpp:33
const std::string KiCadSchematicFileExtension
virtual const wxString GetFileExtension() const =0
Returns the file extension for the SCH_PLUGIN.
static void ReleasePlugin(SCH_PLUGIN *aPlugin)
Release a SCH_PLUGIN back to the system, and may cause it to be unloaded from memory.
Definition: sch_io_mgr.cpp:77
const std::string KiCadSymbolLibFileExtension