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

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. /**
  82. * Save or load the names of the currently configured symbol libraries (without paths).
  83. */
  84. static void LibNamesAndPaths( PROJECT* aProject, bool doSave,
  85. wxString* aPaths, wxArrayString* aNames = nullptr );
  86. /**
  87. * Return the name of the cache library after potentially fixing it from
  88. * an older naming scheme. That is, the old file is renamed if needed.
  89. *
  90. * @param aFullProjectFilename is the *.pro filename with absolute path.
  91. */
  92. static const wxString CacheName( const wxString& aFullProjectFilename );
  93. /**
  94. * Find a symbol library by \a aName.
  95. *
  96. * @param aName is the library file name without path or extension to find.
  97. * @return the symbol library if found, otherwise NULL.
  98. */
  99. SYMBOL_LIB* FindLibrary( const wxString& aName );
  100. SYMBOL_LIB* FindLibraryByFullFileName( const wxString& aFullFileName );
  101. SYMBOL_LIB* GetCacheLibrary();
  102. /**
  103. * Return the list of symbol library file names without path and extension.
  104. *
  105. * @param aSorted sort the list of name if true. Otherwise use the library load order.
  106. * @return the list of library names.
  107. */
  108. wxArrayString GetLibraryNames( bool aSorted = true );
  109. /**
  110. * Search all libraries in the list for a symbol.
  111. *
  112. * A symbol object will always be returned. If the entry found
  113. * is an alias. The root symbol will be found and returned.
  114. *
  115. * @param aLibId is the #LIB_ID of the symbol to search for.
  116. * @param aLibraryName is the name of the library to search for symbol.
  117. * @return the symbol object if found, otherwise NULL.
  118. */
  119. LIB_SYMBOL* FindLibSymbol( const LIB_ID& aLibId, const wxString& aLibraryName = wxEmptyString );
  120. /**
  121. * Search all libraries in the list for a #LIB_SYMBOL using a case insensitive comparison.
  122. *
  123. * Helper function used in dialog to find all candidates.
  124. * During a long time, eeschema was using a case insensitive search.
  125. * Therefore, for old schematics (<= 2013), or libs, for some symbols,
  126. * the chip name (name of alias in lib) can be broken.
  127. * This function can be used to display a list of candidates, in symbol properties dialog.
  128. *
  129. * @param aEntryName is the name of entries to search for (case insensitive).
  130. * @param aLibraryName is the name of the library to search.
  131. * @param aCandidates is a std::vector to store candidates.
  132. */
  133. void FindLibraryNearEntries( std::vector<LIB_SYMBOL*>& aCandidates, const wxString& aEntryName,
  134. const wxString& aLibraryName = wxEmptyString );
  135. int GetLibraryCount() { return size(); }
  136. };
  137. /**
  138. * Object used to load, save, search, and otherwise manipulate symbol library files.
  139. *
  140. * @warning This code is obsolete with the exception of the cache library. All other
  141. * symbol library I/O is managed by the #SCH_IO_MGR object.
  142. */
  143. class SYMBOL_LIB
  144. {
  145. public:
  146. SYMBOL_LIB( SCH_LIB_TYPE aType, const wxString& aFileName,
  147. SCH_IO_MGR::SCH_FILE_T aPluginType = SCH_IO_MGR::SCH_LEGACY );
  148. ~SYMBOL_LIB();
  149. /**
  150. * @return a magic number that changes if the library has changed
  151. */
  152. int GetModHash() const { return m_mod_hash; }
  153. SCH_IO_MGR::SCH_FILE_T GetPluginType() const { return m_pluginType; }
  154. void SetPluginType( SCH_IO_MGR::SCH_FILE_T aPluginType );
  155. void Create( const wxString& aFileName = wxEmptyString );
  156. void SetFileName( const wxString& aFileName ) { fileName = aFileName; }
  157. bool IsModified() const
  158. {
  159. return isModified;
  160. }
  161. bool IsCache() const;
  162. void SetCache();
  163. bool IsBuffering() const;
  164. void EnableBuffering( bool aEnable = true );
  165. void Save( bool aSaveDocFile = true );
  166. /**
  167. * @return true if current user does not have write access to the library file.
  168. */
  169. bool IsReadOnly() const { return !fileName.IsFileWritable(); }
  170. /**
  171. * Load a string array with the names of all the entries in this library.
  172. *
  173. * @param aNames is the array to place entry names into.
  174. */
  175. void GetSymbolNames( wxArrayString& aNames ) const;
  176. /**
  177. * Load a vector with all the entries in this library.
  178. *
  179. * @param aSymbols is a vector to receive the aliases.
  180. */
  181. void GetSymbols( std::vector<LIB_SYMBOL*>& aSymbols ) const;
  182. /**
  183. * Find #LIB_SYMBOL by \a aName.
  184. *
  185. * @param aName is the name of the symbol, case sensitive.
  186. * @return LIB_SYMBOL pointer symbol if found, else NULL.
  187. */
  188. LIB_SYMBOL* FindSymbol( const wxString& aName ) const;
  189. LIB_SYMBOL* FindSymbol( const LIB_ID& aLibId ) const;
  190. /**
  191. * Add \a aSymbol entry to library.
  192. *
  193. * @note A #LIB_SYMBOL can have an alias list so these alias will be added in library.
  194. * and the any existing duplicate aliases will be removed from the library.
  195. *
  196. * @param aSymbol is the symbol to add, caller retains ownership, a clone is added.
  197. */
  198. void AddSymbol( LIB_SYMBOL* aSymbol );
  199. /**
  200. * Safely remove \a aEntry from the library and return the next entry.
  201. *
  202. * The next entry returned depends on the entry being removed. If the entry being
  203. * remove also removes the symbol, then the next entry from the list is returned.
  204. * If the entry being used only removes an alias from a symbol, then the next alias
  205. * of the symbol is returned.
  206. *
  207. * @param aEntry is the entry to remove from library.
  208. * @return The next entry in the library or NULL if the library is empty.
  209. */
  210. LIB_SYMBOL* RemoveSymbol( LIB_SYMBOL* aEntry );
  211. /**
  212. * Replace an existing symbol entry in the library.
  213. *
  214. * @note A symbol can have an alias list so these aliases will be added in library and
  215. * previously existing alias removed.
  216. *
  217. * @param aOldSymbol is the symbol to replace.
  218. * @param aNewSymbol is the new symbol.
  219. */
  220. LIB_SYMBOL* ReplaceSymbol( LIB_SYMBOL* aOldSymbol, LIB_SYMBOL* aNewSymbol );
  221. /**
  222. * Return the file name without path or extension.
  223. *
  224. * @return the name of library file.
  225. */
  226. const wxString GetName() const { return fileName.GetName(); }
  227. /**
  228. * Return the full file library name with path and extension.
  229. *
  230. * @return the full library file name with path and extension.
  231. */
  232. wxString GetFullFileName() const { return fileName.GetFullPath(); }
  233. /**
  234. * Return the logical name of the library.
  235. *
  236. * @return The logical name of this library.
  237. */
  238. const wxString GetLogicalName() const
  239. {
  240. /* for now is the filename without path or extension.
  241. Technically the library should not know its logical name!
  242. This will eventually come out of a pair of lookup tables using a
  243. reverse lookup using the full name or library pointer as a key.
  244. Search will be by project lookup table and then user lookup table if
  245. not found.
  246. */
  247. return fileName.GetName();
  248. }
  249. /**
  250. * Allocate and load a symbol library file.
  251. *
  252. * @param aFileName is the file name of the symbol library to load.
  253. * @return SYMBOL_LIB* is the allocated and loaded SYMBOL_LIB, which is owned by the caller.
  254. * @throw IO_ERROR if there's any problem loading the library.
  255. */
  256. static SYMBOL_LIB* LoadLibrary( const wxString& aFileName );
  257. private:
  258. SCH_LIB_TYPE type; ///< Library type indicator.
  259. wxFileName fileName; ///< Library file name.
  260. wxDateTime timeStamp; ///< Library save time and date.
  261. int versionMajor; ///< Library major version number.
  262. int versionMinor; ///< Library minor version number.
  263. wxString header; ///< first line of loaded library.
  264. bool isModified; ///< Library modification status.
  265. int m_mod_hash; ///< incremented each time library is changed.
  266. SCH_IO_MGR::SCH_FILE_T m_pluginType;
  267. std::unique_ptr< SCH_PLUGIN > m_plugin;
  268. std::unique_ptr< PROPERTIES > m_properties; ///< Library properties
  269. };
  270. /**
  271. * Case insensitive library name comparison.
  272. */
  273. bool operator==( const SYMBOL_LIB& aLibrary, const wxString& aName );
  274. bool operator!=( const SYMBOL_LIB& aLibrary, const wxString& aName );
  275. #endif // SYMBOL_LIBRARY_H