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.

329 lines
11 KiB

3 years ago
8 years ago
8 years ago
15 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
  6. * Copyright (C) 2004-2022 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 symbol_library.h
  27. * @brief Definition for symbol library class.
  28. */
  29. #ifndef SYMBOL_LIBRARY_H
  30. #define SYMBOL_LIBRARY_H
  31. #include <mutex>
  32. #include <boost/ptr_container/ptr_vector.hpp>
  33. #include <wx/filename.h>
  34. #include <project.h>
  35. #include <sch_io_mgr.h>
  36. #include <symbol_library_common.h>
  37. class LIB_SYMBOL;
  38. class LIB_ID;
  39. class LINE_READER;
  40. class OUTPUTFORMATTER;
  41. class PROPERTIES;
  42. class SCH_PLUGIN;
  43. class SYMBOL_LIB;
  44. /* Helpers for creating a list of symbol libraries. */
  45. typedef boost::ptr_vector< SYMBOL_LIB > SYMBOL_LIBS_BASE;
  46. /**
  47. * A collection of #SYMBOL_LIB objects.
  48. *
  49. * It extends from PROJECT::_ELEM so it can be hung in the PROJECT. It does not use any
  50. * UI calls, but rather simply throws an IO_ERROR when there is a problem.
  51. */
  52. class SYMBOL_LIBS : public SYMBOL_LIBS_BASE, public PROJECT::_ELEM
  53. {
  54. public:
  55. KICAD_T Type() override { return SYMBOL_LIBS_T; }
  56. SYMBOL_LIBS() {}
  57. /**
  58. * Allocate and adds a symbol library to the library list.
  59. *
  60. * @param aFileName is the file name object of symbol library.
  61. * @throw IO_ERROR if there's any problem loading.
  62. */
  63. SYMBOL_LIB* AddLibrary( const wxString& aFileName );
  64. /**
  65. * Insert a symbol library into the library list.
  66. *
  67. * @param aFileName is the file name object of symbol library.
  68. * @param aIterator is an iterator to insert library in front of.
  69. * @return the new SYMBOL_LIB, which remains owned by this SYMBOL_LIBS container.
  70. * @throw IO_ERROR if there's any problem loading.
  71. */
  72. SYMBOL_LIB* AddLibrary( const wxString& aFileName, SYMBOL_LIBS::iterator& aIterator );
  73. /**
  74. * Load all of the project's libraries into this container, which should
  75. * be cleared before calling it.
  76. *
  77. * @note This method is only to be used when loading legacy projects. All further symbol
  78. * library access should be done via the symbol library table.
  79. */
  80. void LoadAllLibraries( PROJECT* aProject, bool aShowProgress=true );
  81. static void GetLibNamesAndPaths( PROJECT* aProject, wxString* aPaths,
  82. wxArrayString* aNames = nullptr );
  83. static void SetLibNamesAndPaths( PROJECT* aProject, const wxString& aPaths,
  84. const wxArrayString& aNames );
  85. /**
  86. * Return the name of the cache library after potentially fixing it from
  87. * an older naming scheme. That is, the old file is renamed if needed.
  88. *
  89. * @param aFullProjectFilename is the *.pro filename with absolute path.
  90. */
  91. static const wxString CacheName( const wxString& aFullProjectFilename );
  92. /**
  93. * Find a symbol library by \a aName.
  94. *
  95. * @param aName is the library file name without path or extension to find.
  96. * @return the symbol library if found, otherwise NULL.
  97. */
  98. SYMBOL_LIB* FindLibrary( const wxString& aName );
  99. SYMBOL_LIB* FindLibraryByFullFileName( const wxString& aFullFileName );
  100. SYMBOL_LIB* GetCacheLibrary();
  101. /**
  102. * Return the list of symbol library file names without path and extension.
  103. *
  104. * @param aSorted sort the list of name if true. Otherwise use the library load order.
  105. * @return the list of library names.
  106. */
  107. wxArrayString GetLibraryNames( bool aSorted = true );
  108. /**
  109. * Search all libraries in the list for a symbol.
  110. *
  111. * A symbol object will always be returned. If the entry found
  112. * is an alias. The root symbol will be found and returned.
  113. *
  114. * @param aLibId is the #LIB_ID of the symbol to search for.
  115. * @param aLibraryName is the name of the library to search for symbol.
  116. * @return the symbol object if found, otherwise NULL.
  117. */
  118. LIB_SYMBOL* FindLibSymbol( const LIB_ID& aLibId, const wxString& aLibraryName = wxEmptyString );
  119. /**
  120. * Search all libraries in the list for a #LIB_SYMBOL using a case insensitive comparison.
  121. *
  122. * Helper function used in dialog to find all candidates.
  123. * During a long time, eeschema was using a case insensitive search.
  124. * Therefore, for old schematics (<= 2013), or libs, for some symbols,
  125. * the chip name (name of alias in lib) can be broken.
  126. * This function can be used to display a list of candidates, in symbol properties dialog.
  127. *
  128. * @param aEntryName is the name of entries to search for (case insensitive).
  129. * @param aLibraryName is the name of the library to search.
  130. * @param aCandidates is a std::vector to store candidates.
  131. */
  132. void FindLibraryNearEntries( std::vector<LIB_SYMBOL*>& aCandidates, const wxString& aEntryName,
  133. const wxString& aLibraryName = wxEmptyString );
  134. int GetLibraryCount() { return size(); }
  135. };
  136. /**
  137. * Object used to load, save, search, and otherwise manipulate symbol library files.
  138. *
  139. * @warning This code is obsolete with the exception of the cache library. All other
  140. * symbol library I/O is managed by the #SCH_IO_MGR object.
  141. */
  142. class SYMBOL_LIB
  143. {
  144. public:
  145. SYMBOL_LIB( SCH_LIB_TYPE aType, const wxString& aFileName,
  146. SCH_IO_MGR::SCH_FILE_T aPluginType = SCH_IO_MGR::SCH_LEGACY );
  147. ~SYMBOL_LIB();
  148. /**
  149. * @return a magic number that changes if the library has changed
  150. */
  151. int GetModHash() const { return m_mod_hash; }
  152. SCH_IO_MGR::SCH_FILE_T GetPluginType() const { return m_pluginType; }
  153. void SetPluginType( SCH_IO_MGR::SCH_FILE_T aPluginType );
  154. void Create( const wxString& aFileName = wxEmptyString );
  155. void SetFileName( const wxString& aFileName ) { fileName = aFileName; }
  156. bool IsModified() const
  157. {
  158. return isModified;
  159. }
  160. bool IsCache() const;
  161. void SetCache();
  162. bool IsBuffering() const;
  163. void EnableBuffering( bool aEnable = true );
  164. void Save( bool aSaveDocFile = true );
  165. /**
  166. * @return true if current user does not have write access to the library file.
  167. */
  168. bool IsReadOnly() const { return !fileName.IsFileWritable(); }
  169. /**
  170. * Load a string array with the names of all the entries in this library.
  171. *
  172. * @param aNames is the array to place entry names into.
  173. */
  174. void GetSymbolNames( wxArrayString& aNames ) const;
  175. /**
  176. * Load a vector with all the entries in this library.
  177. *
  178. * @param aSymbols is a vector to receive the aliases.
  179. */
  180. void GetSymbols( std::vector<LIB_SYMBOL*>& aSymbols ) const;
  181. /**
  182. * Find #LIB_SYMBOL by \a aName.
  183. *
  184. * @param aName is the name of the symbol, case sensitive.
  185. * @return LIB_SYMBOL pointer symbol if found, else NULL.
  186. */
  187. LIB_SYMBOL* FindSymbol( const wxString& aName ) const;
  188. LIB_SYMBOL* FindSymbol( const LIB_ID& aLibId ) const;
  189. /**
  190. * Add \a aSymbol entry to library.
  191. *
  192. * @note A #LIB_SYMBOL can have an alias list so these alias will be added in library.
  193. * and the any existing duplicate aliases will be removed from the library.
  194. *
  195. * @param aSymbol is the symbol to add, caller retains ownership, a clone is added.
  196. */
  197. void AddSymbol( LIB_SYMBOL* aSymbol );
  198. /**
  199. * Safely remove \a aEntry from the library and return the next entry.
  200. *
  201. * The next entry returned depends on the entry being removed. If the entry being
  202. * remove also removes the symbol, then the next entry from the list is returned.
  203. * If the entry being used only removes an alias from a symbol, then the next alias
  204. * of the symbol is returned.
  205. *
  206. * @param aEntry is the entry to remove from library.
  207. * @return The next entry in the library or NULL if the library is empty.
  208. */
  209. LIB_SYMBOL* RemoveSymbol( LIB_SYMBOL* aEntry );
  210. /**
  211. * Replace an existing symbol entry in the library.
  212. *
  213. * @note A symbol can have an alias list so these aliases will be added in library and
  214. * previously existing alias removed.
  215. *
  216. * @param aOldSymbol is the symbol to replace.
  217. * @param aNewSymbol is the new symbol.
  218. */
  219. LIB_SYMBOL* ReplaceSymbol( LIB_SYMBOL* aOldSymbol, LIB_SYMBOL* aNewSymbol );
  220. /**
  221. * Return the file name without path or extension.
  222. *
  223. * @return the name of library file.
  224. */
  225. const wxString GetName() const { return fileName.GetName(); }
  226. /**
  227. * Return the full file library name with path and extension.
  228. *
  229. * @return the full library file name with path and extension.
  230. */
  231. wxString GetFullFileName() const { return fileName.GetFullPath(); }
  232. /**
  233. * Return the logical name of the library.
  234. *
  235. * @return The logical name of this library.
  236. */
  237. const wxString GetLogicalName() const
  238. {
  239. /* for now is the filename without path or extension.
  240. Technically the library should not know its logical name!
  241. This will eventually come out of a pair of lookup tables using a
  242. reverse lookup using the full name or library pointer as a key.
  243. Search will be by project lookup table and then user lookup table if
  244. not found.
  245. */
  246. return fileName.GetName();
  247. }
  248. /**
  249. * Allocate and load a symbol library file.
  250. *
  251. * @param aFileName is the file name of the symbol library to load.
  252. * @return SYMBOL_LIB* is the allocated and loaded SYMBOL_LIB, which is owned by the caller.
  253. * @throw IO_ERROR if there's any problem loading the library.
  254. */
  255. static SYMBOL_LIB* LoadLibrary( const wxString& aFileName );
  256. private:
  257. SCH_LIB_TYPE type; ///< Library type indicator.
  258. wxFileName fileName; ///< Library file name.
  259. wxDateTime timeStamp; ///< Library save time and date.
  260. int versionMajor; ///< Library major version number.
  261. int versionMinor; ///< Library minor version number.
  262. wxString header; ///< first line of loaded library.
  263. bool isModified; ///< Library modification status.
  264. int m_mod_hash; ///< incremented each time library is changed.
  265. SCH_IO_MGR::SCH_FILE_T m_pluginType;
  266. std::unique_ptr< SCH_PLUGIN > m_plugin;
  267. std::unique_ptr< PROPERTIES > m_properties; ///< Library properties
  268. };
  269. /**
  270. * Case insensitive library name comparison.
  271. */
  272. bool operator==( const SYMBOL_LIB& aLibrary, const wxString& aName );
  273. bool operator!=( const SYMBOL_LIB& aLibrary, const wxString& aName );
  274. #endif // SYMBOL_LIBRARY_H