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.

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