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>
27 #include <sch_legacy_plugin.h>
28 #include <sch_eagle_plugin.h>
29 #include <sch_sexpr_plugin.h>
30 
32 
33 #define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
34 #define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
35 
36 
37 
38 // Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
39 // and code size. Until then, use the simplest method:
40 
41 // This implementation is one of two which could be done.
42 // The other one would cater to DLL/DSO's. But since it would be nearly
43 // impossible to link a KICAD type DLL/DSO right now without pulling in all
44 // ::Draw() functions, I forgo that option temporarily.
45 
46 // Some day it may be possible to have some built in AND some DLL/DSO
47 // plugins coexisting.
48 
49 
50 SCH_PLUGIN* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
51 {
52  // This implementation is subject to change, any magic is allowed here.
53  // The public SCH_IO_MGR API is the only pertinent public information.
54 
55  switch( aFileType )
56  {
57  case SCH_LEGACY:
58  return new SCH_LEGACY_PLUGIN();
59  case SCH_KICAD:
60  return new SCH_SEXPR_PLUGIN();
61  case SCH_EAGLE:
62  return new SCH_EAGLE_PLUGIN();
63  default:
64  ;
65  }
66 
67  return NULL;
68 }
69 
70 
72 {
73  // This function is a place holder for a future point in time where
74  // the plugin is a DLL/DSO. It could do reference counting, and then
75  // unload the DLL/DSO when count goes to zero.
76 
77  delete aPlugin;
78 }
79 
80 
81 const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aType )
82 {
83  // keep this function in sync with EnumFromStr() relative to the
84  // text spellings. If you change the spellings, you will obsolete
85  // library tables, so don't do change, only additions are ok.
86 
87  switch( aType )
88  {
89  default:
90  return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ), aType );
91 
92  case SCH_LEGACY:
93  return wxString( wxT( "Legacy" ) );
94 
95  case SCH_KICAD:
96  return wxString( wxT( "KiCad" ) );
97 
98  case SCH_EAGLE:
99  return wxString( wxT( "EAGLE" ) );
100  }
101 }
102 
103 
104 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
105 {
106  // keep this function in sync with ShowType() relative to the
107  // text spellings. If you change the spellings, you will obsolete
108  // library tables, so don't do change, only additions are ok.
109 
110  if( aType == wxT( "Legacy" ) )
111  return SCH_LEGACY;
112  else if( aType == wxT( "KiCad" ) )
113  return SCH_KICAD;
114  else if( aType == wxT( "EAGLE" ) )
115  return SCH_EAGLE;
116 
117  // wxASSERT( blow up here )
118 
119  return SCH_FILE_T( -1 );
120 }
121 
122 
123 const wxString SCH_IO_MGR::GetFileExtension( SCH_FILE_T aFileType )
124 {
125  wxString ext = wxEmptyString;
126  SCH_PLUGIN* plugin = FindPlugin( aFileType );
127 
128  if( plugin != NULL )
129  {
130  ext = plugin->GetFileExtension();
131  ReleasePlugin( plugin );
132  }
133 
134  return ext;
135 }
136 
137 
138 const wxString SCH_IO_MGR::GetLibraryFileExtension( SCH_FILE_T aFileType )
139 {
140  wxString ext = wxEmptyString;
141  SCH_PLUGIN* plugin = FindPlugin( aFileType );
142 
143  if( plugin != NULL )
144  {
145  ext = plugin->GetLibraryFileExtension();
146  ReleasePlugin( plugin );
147  }
148 
149  return ext;
150 }
151 
152 
153 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
154 {
155  SCH_FILE_T ret = SCH_KICAD; // default guess, unless detected otherwise.
156  wxFileName fn( aLibPath );
157 
158  if( fn.GetExt() == LegacySymbolLibFileExtension )
159  {
160  ret = SCH_LEGACY;
161  }
162  else if( fn.GetExt() == KiCadSymbolLibFileExtension )
163  {
164  ret = SCH_KICAD;
165  }
166 
167  return ret;
168 }
169 
170 
171 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromSchPath( const wxString& aSchematicPath )
172 {
173  SCH_FILE_T ret = SCH_KICAD; // default guess, unless detected otherwise.
174  wxFileName fn( aSchematicPath );
175 
176  if( fn.GetExt() == LegacySchematicFileExtension )
177  {
178  ret = SCH_LEGACY;
179  }
180  else if( fn.GetExt() == KiCadSchematicFileExtension )
181  {
182  ret = SCH_KICAD;
183  }
184 
185  return ret;
186 }
187 
188 
189 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:153
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:81
static const wxString GetLibraryFileExtension(SCH_FILE_T aFileType)
Return the symbol library file extension (if any) for aFileType.
Definition: sch_io_mgr.cpp:138
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:171
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:151
#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:123
Definition of file extensions used in Kicad.
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:205
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:104
#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:71
const std::string KiCadSymbolLibFileExtension