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.

676 lines
17 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004-2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2008-2017 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2004-2017 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file class_library.cpp
  27. */
  28. #include <algorithm>
  29. #include <fctsys.h>
  30. #include <kiface_i.h>
  31. #include <gr_basic.h>
  32. #include <macros.h>
  33. #include <kicad_string.h>
  34. #include <gestfich.h>
  35. #include <eda_doc.h>
  36. #include <wxstruct.h>
  37. #include <richio.h>
  38. #include <config_params.h>
  39. #include <wildcards_and_files_ext.h>
  40. #include <project_rescue.h>
  41. #include <properties.h>
  42. #include <general.h>
  43. #include <class_library.h>
  44. #include <sch_legacy_plugin.h>
  45. #include <wx/progdlg.h>
  46. #include <wx/tokenzr.h>
  47. #include <wx/regex.h>
  48. #define DUPLICATE_NAME_MSG \
  49. _( "Library '%s' has duplicate entry name '%s'.\n" \
  50. "This may cause some unexpected behavior when loading components into a schematic." )
  51. PART_LIB::PART_LIB( int aType, const wxString& aFileName, SCH_IO_MGR::SCH_FILE_T aPluginType ) :
  52. // start @ != 0 so each additional library added
  53. // is immediately detectable, zero would not be.
  54. m_mod_hash( PART_LIBS::s_modify_generation ),
  55. m_pluginType( aPluginType )
  56. {
  57. type = aType;
  58. isModified = false;
  59. timeStamp = 0;
  60. timeStamp = wxDateTime::Now();
  61. versionMajor = 0; // Will be updated after reading the lib file
  62. versionMinor = 0; // Will be updated after reading the lib file
  63. fileName = aFileName;
  64. if( !fileName.IsOk() )
  65. fileName = "unnamed.lib";
  66. m_plugin.reset( SCH_IO_MGR::FindPlugin( m_pluginType ) );
  67. m_properties = std::make_unique<PROPERTIES>();
  68. }
  69. PART_LIB::~PART_LIB()
  70. {
  71. }
  72. void PART_LIB::Save( bool aSaveDocFile )
  73. {
  74. wxCHECK_RET( m_plugin != NULL, wxString::Format( "no plugin defined for library `%s`.",
  75. fileName.GetFullPath() ) );
  76. PROPERTIES props;
  77. if( !aSaveDocFile )
  78. props[ SCH_LEGACY_PLUGIN::PropNoDocFile ] = "";
  79. m_plugin->SaveLibrary( fileName.GetFullPath(), &props );
  80. isModified = false;
  81. }
  82. void PART_LIB::Create( const wxString& aFileName )
  83. {
  84. wxString tmpFileName = fileName.GetFullPath();
  85. if( !aFileName.IsEmpty() )
  86. tmpFileName = aFileName;
  87. m_plugin->CreateSymbolLib( tmpFileName, m_properties.get() );
  88. }
  89. void PART_LIB::SetPluginType( SCH_IO_MGR::SCH_FILE_T aPluginType )
  90. {
  91. if( m_pluginType != aPluginType )
  92. {
  93. m_pluginType = aPluginType;
  94. m_plugin.reset( SCH_IO_MGR::FindPlugin( m_pluginType ) );
  95. }
  96. }
  97. bool PART_LIB::IsCache() const
  98. {
  99. return m_properties->Exists( SCH_LEGACY_PLUGIN::PropNoDocFile );
  100. }
  101. void PART_LIB::SetCache()
  102. {
  103. (*m_properties)[ SCH_LEGACY_PLUGIN::PropNoDocFile ] = "";
  104. }
  105. bool PART_LIB::IsBuffering() const
  106. {
  107. return m_properties->Exists( SCH_LEGACY_PLUGIN::PropBuffering );
  108. }
  109. void PART_LIB::EnableBuffering( bool aEnable )
  110. {
  111. if( aEnable )
  112. (*m_properties)[ SCH_LEGACY_PLUGIN::PropBuffering ] = "";
  113. else
  114. m_properties->Clear( SCH_LEGACY_PLUGIN::PropBuffering );
  115. }
  116. void PART_LIB::GetAliasNames( wxArrayString& aNames )
  117. {
  118. m_plugin->EnumerateSymbolLib( aNames, fileName.GetFullPath(), m_properties.get() );
  119. aNames.Sort();
  120. }
  121. void PART_LIB::GetAliases( std::vector<LIB_ALIAS*>& aAliases )
  122. {
  123. m_plugin->EnumerateSymbolLib( aAliases, fileName.GetFullPath(), m_properties.get() );
  124. std::sort( aAliases.begin(), aAliases.end(),
  125. [](LIB_ALIAS *lhs, LIB_ALIAS *rhs) -> bool
  126. { return lhs->GetName() < rhs->GetName(); });
  127. }
  128. void PART_LIB::GetEntryTypePowerNames( wxArrayString& aNames )
  129. {
  130. std::vector<LIB_ALIAS*> aliases;
  131. m_plugin->EnumerateSymbolLib( aliases, fileName.GetFullPath() );
  132. for( size_t i = 0; i < aliases.size(); i++ )
  133. {
  134. LIB_ALIAS* alias = aliases[i];
  135. LIB_PART* root = alias->GetPart();
  136. if( !root || !root->IsPower() )
  137. continue;
  138. aNames.Add( alias->GetName() );
  139. }
  140. aNames.Sort();
  141. }
  142. LIB_ALIAS* PART_LIB::FindAlias( const wxString& aName )
  143. {
  144. LIB_ALIAS* alias = m_plugin->LoadSymbol( fileName.GetFullPath(), aName, m_properties.get() );
  145. // Set the library to this even though technically the legacy cache plugin owns the
  146. // symbols. This allows the symbol library table conversion tool to determine the
  147. // correct library where the symbol was found.
  148. if( alias && alias->GetPart() && !alias->GetPart()->GetLib() )
  149. alias->GetPart()->SetLib( this );
  150. return alias;
  151. }
  152. LIB_PART* PART_LIB::FindPart( const wxString& aName )
  153. {
  154. LIB_ALIAS* alias = FindAlias( aName );
  155. if( alias != NULL )
  156. return alias->GetPart();
  157. return NULL;
  158. }
  159. bool PART_LIB::HasPowerParts()
  160. {
  161. // return true if at least one power part is found in lib
  162. std::vector<LIB_ALIAS*> aliases;
  163. m_plugin->EnumerateSymbolLib( aliases, fileName.GetFullPath(), m_properties.get() );
  164. for( size_t i = 0; i < aliases.size(); i++ )
  165. {
  166. LIB_ALIAS* alias = aliases[i];
  167. LIB_PART* root = alias->GetPart();
  168. if( !root || root->IsPower() )
  169. return true;
  170. }
  171. return false;
  172. }
  173. void PART_LIB::AddPart( LIB_PART* aPart )
  174. {
  175. // add a clone, not the caller's copy, the plugin take ownership of the new symbol.
  176. m_plugin->SaveSymbol( fileName.GetFullPath(), new LIB_PART( *aPart, this ), m_properties.get() );
  177. // If we are not buffering, the library file is updated immediately when the plugin
  178. // SaveSymbol() function is called.
  179. if( IsBuffering() )
  180. isModified = true;
  181. ++m_mod_hash;
  182. }
  183. LIB_ALIAS* PART_LIB::RemoveAlias( LIB_ALIAS* aEntry )
  184. {
  185. wxCHECK_MSG( aEntry != NULL, NULL, "NULL pointer cannot be removed from library." );
  186. m_plugin->DeleteAlias( fileName.GetFullPath(), aEntry->GetName(), m_properties.get() );
  187. // If we are not buffering, the library file is updated immediately when the plugin
  188. // SaveSymbol() function is called.
  189. if( IsBuffering() )
  190. isModified = true;
  191. ++m_mod_hash;
  192. return NULL;
  193. }
  194. LIB_PART* PART_LIB::ReplacePart( LIB_PART* aOldPart, LIB_PART* aNewPart )
  195. {
  196. wxASSERT( aOldPart != NULL );
  197. wxASSERT( aNewPart != NULL );
  198. m_plugin->DeleteSymbol( fileName.GetFullPath(), aOldPart->GetName(), m_properties.get() );
  199. LIB_PART* my_part = new LIB_PART( *aNewPart, this );
  200. m_plugin->SaveSymbol( fileName.GetFullPath(), my_part, m_properties.get() );
  201. // If we are not buffering, the library file is updated immediately when the plugin
  202. // SaveSymbol() function is called.
  203. if( IsBuffering() )
  204. isModified = true;
  205. ++m_mod_hash;
  206. return my_part;
  207. }
  208. PART_LIB* PART_LIB::LoadLibrary( const wxString& aFileName )
  209. {
  210. std::unique_ptr<PART_LIB> lib( new PART_LIB( LIBRARY_TYPE_EESCHEMA, aFileName ) );
  211. std::vector<LIB_ALIAS*> aliases;
  212. // This loads the library.
  213. lib->GetAliases( aliases );
  214. // Now, set the LIB_PART m_library member but it will only be used
  215. // when loading legacy libraries in the future. Once the symbols in the
  216. // schematic have a full #LIB_ID, this will not get called.
  217. for( size_t ii = 0; ii < aliases.size(); ii++ )
  218. {
  219. LIB_ALIAS* alias = aliases[ii];
  220. if( alias->GetPart() )
  221. alias->GetPart()->SetLib( lib.get() );
  222. }
  223. PART_LIB* ret = lib.release();
  224. return ret;
  225. }
  226. PART_LIB* PART_LIBS::AddLibrary( const wxString& aFileName )
  227. {
  228. PART_LIB* lib;
  229. wxFileName fn = aFileName;
  230. // Don't reload the library if it is already loaded.
  231. lib = FindLibrary( fn.GetName() );
  232. if( lib )
  233. return lib;
  234. lib = PART_LIB::LoadLibrary( aFileName );
  235. push_back( lib );
  236. return lib;
  237. }
  238. PART_LIB* PART_LIBS::AddLibrary( const wxString& aFileName, PART_LIBS::iterator& aIterator )
  239. {
  240. // Don't reload the library if it is already loaded.
  241. wxFileName fn( aFileName );
  242. PART_LIB* lib = FindLibrary( fn.GetName() );
  243. if( lib )
  244. return lib;
  245. lib = PART_LIB::LoadLibrary( aFileName );
  246. if( aIterator >= begin() && aIterator < end() )
  247. insert( aIterator, lib );
  248. else
  249. push_back( lib );
  250. return lib;
  251. }
  252. PART_LIB* PART_LIBS::FindLibrary( const wxString& aName )
  253. {
  254. for( PART_LIBS::iterator it = begin(); it!=end(); ++it )
  255. {
  256. if( it->GetName() == aName )
  257. return &*it;
  258. }
  259. return NULL;
  260. }
  261. PART_LIB* PART_LIBS::FindLibraryByFullFileName( const wxString& aFullFileName )
  262. {
  263. for( PART_LIBS::iterator it = begin(); it!=end(); ++it )
  264. {
  265. if( it->GetFullFileName() == aFullFileName )
  266. return &*it;
  267. }
  268. return NULL;
  269. }
  270. wxArrayString PART_LIBS::GetLibraryNames( bool aSorted )
  271. {
  272. wxArrayString cacheNames;
  273. wxArrayString names;
  274. for( PART_LIB& lib : *this )
  275. {
  276. if( lib.IsCache() && aSorted )
  277. cacheNames.Add( lib.GetName() );
  278. else
  279. names.Add( lib.GetName() );
  280. }
  281. // Even sorted, the cache library is always at the end of the list.
  282. if( aSorted )
  283. names.Sort();
  284. for( unsigned int i = 0; i<cacheNames.Count(); i++ )
  285. names.Add( cacheNames.Item( i ) );
  286. return names;
  287. }
  288. LIB_PART* PART_LIBS::FindLibPart( const LIB_ID& aLibId, const wxString& aLibraryName )
  289. {
  290. LIB_PART* part = NULL;
  291. for( PART_LIB& lib : *this )
  292. {
  293. if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
  294. continue;
  295. part = lib.FindPart( FROM_UTF8( aLibId.GetLibItemName() ) );
  296. if( part )
  297. break;
  298. }
  299. return part;
  300. }
  301. LIB_ALIAS* PART_LIBS::FindLibraryAlias( const LIB_ID& aLibId, const wxString& aLibraryName )
  302. {
  303. LIB_ALIAS* entry = NULL;
  304. for( PART_LIB& lib : *this )
  305. {
  306. if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
  307. continue;
  308. entry = lib.FindAlias( FROM_UTF8( aLibId.GetLibItemName() ) );
  309. if( entry )
  310. break;
  311. }
  312. return entry;
  313. }
  314. /* searches all libraries in the list for an entry, using a case insensitive comparison.
  315. * Used to find an entry, when the normal (case sensitive) search fails.
  316. */
  317. void PART_LIBS::FindLibraryNearEntries( std::vector<LIB_ALIAS*>& aCandidates,
  318. const wxString& aEntryName,
  319. const wxString& aLibraryName )
  320. {
  321. for( PART_LIB& lib : *this )
  322. {
  323. if( !aLibraryName.IsEmpty() && lib.GetName() != aLibraryName )
  324. continue;
  325. wxArrayString aliasNames;
  326. lib.GetAliasNames( aliasNames );
  327. if( aliasNames.IsEmpty() )
  328. continue;
  329. for( size_t i = 0; i < aliasNames.size(); i++ )
  330. {
  331. if( aliasNames[i].CmpNoCase( aEntryName ) == 0 )
  332. aCandidates.push_back( lib.FindAlias( aliasNames[i] ) );
  333. }
  334. }
  335. }
  336. int PART_LIBS::s_modify_generation = 1; // starts at 1 and goes up
  337. int PART_LIBS::GetModifyHash()
  338. {
  339. int hash = 0;
  340. for( PART_LIBS::const_iterator it = begin(); it != end(); ++it )
  341. {
  342. hash += it->GetModHash();
  343. }
  344. return hash;
  345. }
  346. void PART_LIBS::LibNamesAndPaths( PROJECT* aProject, bool doSave,
  347. wxString* aPaths, wxArrayString* aNames )
  348. {
  349. wxString pro = aProject->GetProjectFullName();
  350. PARAM_CFG_ARRAY ca;
  351. if( aPaths )
  352. ca.push_back( new PARAM_CFG_FILENAME( "LibDir", aPaths ) );
  353. if( aNames )
  354. ca.push_back( new PARAM_CFG_LIBNAME_LIST( wxT( "LibName" ), aNames, GROUP_SCH_LIBS ) );
  355. if( doSave )
  356. {
  357. aProject->ConfigSave( Kiface().KifaceSearch(), GROUP_SCH, ca );
  358. /*
  359. {
  360. wxString msg = wxString::Format( _(
  361. "Unable save project's '%s' file" ),
  362. GetChars( pro )
  363. );
  364. THROW_IO_ERROR( msg );
  365. }
  366. */
  367. }
  368. else
  369. {
  370. if( !aProject->ConfigLoad( Kiface().KifaceSearch(), GROUP_SCH, ca ) )
  371. {
  372. wxString msg = wxString::Format( _(
  373. "Unable to load project's '%s' file" ),
  374. GetChars( pro )
  375. );
  376. THROW_IO_ERROR( msg );
  377. }
  378. }
  379. }
  380. const wxString PART_LIBS::CacheName( const wxString& aFullProjectFilename )
  381. {
  382. /* until apr 2009 the project cache lib was named: <root_name>.cache.lib,
  383. * and after: <root_name>-cache.lib. So if the <name>-cache.lib is not found,
  384. * the old file will be renamed and returned.
  385. */
  386. wxFileName new_name = aFullProjectFilename;
  387. new_name.SetName( new_name.GetName() + "-cache" );
  388. new_name.SetExt( SchematicLibraryFileExtension );
  389. if( new_name.FileExists() )
  390. return new_name.GetFullPath();
  391. else
  392. {
  393. wxFileName old_name = aFullProjectFilename;
  394. old_name.SetExt( "cache.lib" );
  395. if( old_name.FileExists() )
  396. {
  397. wxRenameFile( old_name.GetFullPath(), new_name.GetFullPath() );
  398. return new_name.GetFullPath();
  399. }
  400. }
  401. return wxEmptyString;
  402. }
  403. void PART_LIBS::LoadAllLibraries( PROJECT* aProject, bool aShowProgress )
  404. {
  405. wxString filename;
  406. wxString libs_not_found;
  407. SEARCH_STACK* lib_search = aProject->SchSearchS();
  408. #if defined(DEBUG) && 0
  409. lib_search->Show( __func__ );
  410. #endif
  411. wxArrayString lib_names;
  412. LibNamesAndPaths( aProject, false, NULL, &lib_names );
  413. // If the list is empty, force loading the standard power symbol library.
  414. if( !lib_names.GetCount() )
  415. lib_names.Add( "power" );
  416. wxASSERT( !size() ); // expect to load into "this" empty container.
  417. wxProgressDialog lib_dialog( _( "Loading Symbol Libraries" ),
  418. wxEmptyString,
  419. lib_names.GetCount(),
  420. NULL,
  421. wxPD_APP_MODAL );
  422. if( aShowProgress )
  423. {
  424. lib_dialog.Show();
  425. }
  426. wxString progress_message;
  427. for( unsigned i = 0; i < lib_names.GetCount(); ++i )
  428. {
  429. if( aShowProgress )
  430. {
  431. lib_dialog.Update( i, _( "Loading " + lib_names[i] ) );
  432. }
  433. wxFileName fn = lib_names[i];
  434. // lib_names[] does not store the file extension. Set it:
  435. fn.SetExt( SchematicLibraryFileExtension );
  436. // Skip if the file name is not valid..
  437. if( !fn.IsOk() )
  438. continue;
  439. if( !fn.FileExists() )
  440. {
  441. filename = lib_search->FindValidPath( fn.GetFullPath() );
  442. if( !filename )
  443. {
  444. libs_not_found += fn.GetFullPath();
  445. libs_not_found += '\n';
  446. continue;
  447. }
  448. }
  449. else
  450. { // ensure the lib filename has a absolute path.
  451. // If the lib has no absolute path, and is found in the cwd by fn.FileExists(),
  452. // make a full absolute path, to avoid issues with load library functions which
  453. // expects an absolute path.
  454. if( !fn.IsAbsolute() )
  455. fn.MakeAbsolute();
  456. filename = fn.GetFullPath();
  457. }
  458. try
  459. {
  460. AddLibrary( filename );
  461. }
  462. catch( const IO_ERROR& ioe )
  463. {
  464. wxString msg;
  465. msg.Printf( _( "Part library '%s' failed to load. Error:\n %s" ),
  466. GetChars( filename ), GetChars( ioe.What() ) );
  467. wxLogError( msg );
  468. }
  469. }
  470. if( aShowProgress )
  471. {
  472. lib_dialog.Destroy();
  473. }
  474. // add the special cache library.
  475. wxString cache_name = CacheName( aProject->GetProjectFullName() );
  476. PART_LIB* cache_lib;
  477. if( !cache_name.IsEmpty() )
  478. {
  479. try
  480. {
  481. cache_lib = AddLibrary( cache_name );
  482. if( cache_lib )
  483. cache_lib->SetCache();
  484. }
  485. catch( const IO_ERROR& ioe )
  486. {
  487. wxString msg = wxString::Format( _(
  488. "Part library '%s' failed to load.\nError: %s" ),
  489. GetChars( cache_name ),
  490. GetChars( ioe.What() )
  491. );
  492. THROW_IO_ERROR( msg );
  493. }
  494. }
  495. // Print the libraries not found
  496. if( !libs_not_found.IsEmpty() )
  497. {
  498. // Use a different exception type so catch()er can route to proper use
  499. // of the HTML_MESSAGE_BOX.
  500. THROW_PARSE_ERROR( wxEmptyString, UTF8( __func__ ), UTF8( libs_not_found ), 0, 0 );
  501. }
  502. #if defined(DEBUG) && 1
  503. printf( "%s: lib_names:\n", __func__ );
  504. for( PART_LIBS::const_iterator it = begin(); it < end(); ++it )
  505. printf( " %s\n", TO_UTF8( it->GetName() ) );
  506. #endif
  507. }