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.

670 lines
21 KiB

16 years ago
16 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-2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2004-2015 KiCad Developers, see change_log.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.h
  27. * @brief Definition for part library class.
  28. */
  29. #ifndef CLASS_LIBRARY_H
  30. #define CLASS_LIBRARY_H
  31. #include <wx/filename.h>
  32. #include <class_libentry.h>
  33. #include <project.h>
  34. class LINE_READER;
  35. class OUTPUTFORMATTER;
  36. /*
  37. * Part Library version and file header macros.
  38. */
  39. #define LIB_VERSION_MAJOR 2
  40. #define LIB_VERSION_MINOR 3
  41. /* Must be the first line of part library (.lib) files. */
  42. #define LIBFILE_IDENT "EESchema-LIBRARY Version"
  43. #define LIB_VERSION( major, minor ) ( major * 100 + minor )
  44. #define IS_LIB_CURRENT_VERSION( major, minor ) \
  45. ( \
  46. LIB_VERSION( major1, minor1 ) == \
  47. LIB_VERSION( LIB_VERSION_MAJOR, LIB_VERSION_MINOR) \
  48. )
  49. /*
  50. * Library versions 2.3 and lower use the old separate library (.lib) and
  51. * document (.dcm) files. Part libraries after 2.3 merged the library
  52. * and document files into a single library file. This macro checks if the
  53. * library version supports the old format
  54. */
  55. #define USE_OLD_DOC_FILE_FORMAT( major, minor ) \
  56. ( LIB_VERSION( major, minor ) <= LIB_VERSION( 2, 3 ) )
  57. /* Must be the first line of part library document (.dcm) files. */
  58. #define DOCFILE_IDENT "EESchema-DOCLIB Version 2.0"
  59. #define DOC_EXT wxT( "dcm" )
  60. // Helper class to filter a list of libraries, and/or a list of PART_LIB
  61. // in dialogs
  62. class SCHLIB_FILTER
  63. {
  64. wxArrayString m_allowedLibs; ///< a list of lib names to list some libraries
  65. ///< if empty: no filter
  66. bool m_filterPowerParts; ///< true to filter (show only) power parts
  67. bool m_forceLoad; // When true, load a part lib from the lib
  68. // which is given in m_allowedLibs[0]
  69. public:
  70. SCHLIB_FILTER()
  71. {
  72. m_filterPowerParts = false;
  73. m_forceLoad = false;
  74. }
  75. /**
  76. * add a lib name to the allowed libraries
  77. */
  78. void AddLib( const wxString& aLibName )
  79. {
  80. m_allowedLibs.Add( aLibName );
  81. m_forceLoad = false;
  82. }
  83. /**
  84. * add a lib name to the allowed libraries
  85. */
  86. void LoadFrom( const wxString& aLibName )
  87. {
  88. m_allowedLibs.Clear();
  89. m_allowedLibs.Add( aLibName );
  90. m_forceLoad = true;
  91. }
  92. /**
  93. * Clear the allowed libraries list (allows all libs)
  94. */
  95. void ClearLibList()
  96. {
  97. m_allowedLibs.Clear();
  98. m_forceLoad = false;
  99. }
  100. /**
  101. * set the filtering of power parts
  102. */
  103. void FilterPowerParts( bool aFilterEnable )
  104. {
  105. m_filterPowerParts = aFilterEnable;
  106. }
  107. // Accessors
  108. /**
  109. * Function GetFilterPowerParts
  110. * @return true if the filtering of power parts is on
  111. */
  112. bool GetFilterPowerParts() const { return m_filterPowerParts; }
  113. /**
  114. * Function GetAllowedLibList
  115. * @return am wxArrayString of the names of allowed libs
  116. */
  117. const wxArrayString& GetAllowedLibList() const { return m_allowedLibs; }
  118. /**
  119. * Function GetLibSource
  120. * @return the name of the lib to use to load a part, or an a emty string
  121. * Useful to load (in lib editor or lib viewer) a part from a given library
  122. */
  123. const wxString& GetLibSource() const
  124. {
  125. static wxString dummy;
  126. if( m_forceLoad && m_allowedLibs.GetCount() > 0 )
  127. return m_allowedLibs[0];
  128. else
  129. return dummy;
  130. }
  131. };
  132. /* Helpers for creating a list of part libraries. */
  133. class PART_LIB;
  134. class wxRegEx;
  135. /**
  136. * LIB_ALIAS map sorting.
  137. */
  138. struct AliasMapSort
  139. {
  140. bool operator() ( const wxString& aItem1, const wxString& aItem2 ) const
  141. {
  142. return Cmp_KEEPCASE( aItem1, aItem2 ) < 0;
  143. }
  144. };
  145. /// Alias map used by part library object.
  146. typedef std::map< wxString, LIB_ALIAS*, AliasMapSort > LIB_ALIAS_MAP;
  147. typedef std::vector< LIB_ALIAS* > LIB_ALIASES;
  148. typedef boost::ptr_vector< PART_LIB > PART_LIBS_BASE;
  149. /**
  150. * Class PART_LIBS
  151. * is a collection of PART_LIBs. It extends from PROJECT::_ELEM so it can be
  152. * hung in the PROJECT. It does not use any UI calls, but rather simply throws
  153. * an IO_ERROR when there is a problem.
  154. */
  155. class PART_LIBS : public PART_LIBS_BASE, public PROJECT::_ELEM
  156. {
  157. public:
  158. static int s_modify_generation; ///< helper for GetModifyHash()
  159. PART_LIBS()
  160. {
  161. ++s_modify_generation;
  162. }
  163. /// Return the modification hash for all libraries. The value returned
  164. /// changes on every library modification.
  165. int GetModifyHash();
  166. /**
  167. * Function AddLibrary
  168. * allocates and adds a part library to the library list.
  169. *
  170. * @param aFileName - File name object of part library.
  171. * @throw IO_ERROR if there's any problem loading.
  172. */
  173. PART_LIB* AddLibrary( const wxString& aFileName ) throw( IO_ERROR, boost::bad_pointer );
  174. /**
  175. * Function AddLibrary
  176. * inserts a part library into the library list.
  177. *
  178. * @param aFileName - File name object of part library.
  179. * @param aIterator - Iterator to insert library in front of.
  180. * @return PART_LIB* - the new PART_LIB, which remains owned by this PART_LIBS container.
  181. * @throw IO_ERROR if there's any problem loading.
  182. */
  183. PART_LIB* AddLibrary( const wxString& aFileName, PART_LIBS::iterator& aIterator )
  184. throw( IO_ERROR, boost::bad_pointer );
  185. /**
  186. * Function RemoveLibrary
  187. * removes a part library from the library list.
  188. *
  189. * @param aName - Name of part library to remove.
  190. */
  191. void RemoveLibrary( const wxString& aName );
  192. void RemoveAllLibraries() { clear(); }
  193. /**
  194. * Function LoadAllLibraries
  195. * loads all of the project's libraries into this container, which should
  196. * be cleared before calling it.
  197. */
  198. void LoadAllLibraries( PROJECT* aProject ) throw( IO_ERROR, boost::bad_pointer );
  199. /**
  200. * Function LibNamesAndPaths
  201. * either saves or loads the names of the currently configured part libraries
  202. * (without paths).
  203. */
  204. static void LibNamesAndPaths( PROJECT* aProject, bool doSave,
  205. wxString* aPaths, wxArrayString* aNames=NULL )
  206. throw( IO_ERROR, boost::bad_pointer );
  207. /**
  208. * Function cacheName
  209. * returns the name of the cache library after potentially fixing it from
  210. * an older naming scheme. That is, the old file is renamed if needed.
  211. * @param aFullProjectFilename - the *.pro filename with absolute path.
  212. */
  213. static const wxString CacheName( const wxString& aFullProjectFilename );
  214. /**
  215. * Function FindLibrary
  216. * finds a part library by \a aName.
  217. *
  218. * @param aName - Library file name without path or extension to find.
  219. * @return Part library if found, otherwise NULL.
  220. */
  221. PART_LIB* FindLibrary( const wxString& aName );
  222. /**
  223. * Function GetLibraryNames
  224. * returns the list of part library file names without path and extension.
  225. *
  226. * @param aSorted - Sort the list of name if true. Otherwise use the
  227. * library load order.
  228. * @return The list of library names.
  229. */
  230. wxArrayString GetLibraryNames( bool aSorted = true );
  231. /**
  232. * Function FindLibPart
  233. * searches all libraries in the list for a part.
  234. *
  235. * A part object will always be returned. If the entry found
  236. * is an alias. The root part will be found and returned.
  237. *
  238. * @param aPartName - Name of part to search for.
  239. * @param aLibraryName - Name of the library to search for part.
  240. * @return LIB_PART* - The part object if found, otherwise NULL.
  241. */
  242. LIB_PART* FindLibPart( const wxString& aPartName, const wxString& aLibraryName = wxEmptyString );
  243. /**
  244. * Function FindLibraryEntry
  245. * searches all libraries in the list for an entry.
  246. *
  247. * The object can be either a part or an alias.
  248. *
  249. * @param aEntryName - Name of entry to search for (case sensitive).
  250. * @param aLibraryName - Name of the library to search.
  251. * @return The entry object if found, otherwise NULL.
  252. */
  253. LIB_ALIAS* FindLibraryEntry( const wxString& aEntryName,
  254. const wxString& aLibraryName = wxEmptyString );
  255. /**
  256. * Function FindLibraryEntries
  257. * searches all libraries in the list for an entry, returns all matches.
  258. *
  259. * @param aEntryName - Name of entry to search for (case sensitive).
  260. * @param aEntries - a std::vector to store entries
  261. */
  262. void FindLibraryEntries( const wxString& aEntryName, std::vector<LIB_ALIAS*>& aEntries );
  263. /**
  264. * Function FindLibraryNearEntries
  265. * Searches all libraries in the list for an entry, using a case insensitive comparison.
  266. * Helper function used in dialog to find all candidates.
  267. * During a long time, eeschema was using a case insensitive search.
  268. * Therefore, for old schematics (<= 2013), or libs, for some components,
  269. * the chip name (name of alias in lib) can be broken.
  270. * This function can be used to display a list of candidates, in component properties dialog.
  271. *
  272. * @param aEntryName - Name of entries to search for (case insensitive).
  273. * @param aLibraryName - Name of the library to search.
  274. * @param aCandidates - a std::vector to store candidates
  275. */
  276. void FindLibraryNearEntries( std::vector<LIB_ALIAS*>& aCandidates, const wxString& aEntryName,
  277. const wxString& aLibraryName = wxEmptyString );
  278. /**
  279. * Function RemoveCacheLibrary
  280. * removes all cache libraries from library list.
  281. */
  282. //void RemoveCacheLibrary();
  283. int GetLibraryCount() { return size(); }
  284. };
  285. /**
  286. * Class PART_LIB
  287. * is used to load, save, search, and otherwise manipulate
  288. * part library files.
  289. */
  290. class PART_LIB
  291. {
  292. int type; ///< Library type indicator.
  293. wxFileName fileName; ///< Library file name.
  294. wxDateTime timeStamp; ///< Library save time and date.
  295. int versionMajor; ///< Library major version number.
  296. int versionMinor; ///< Library minor version number.
  297. bool isCache; /**< False for the "standard" libraries,
  298. True for the library cache */
  299. wxString header; ///< first line of loaded library.
  300. bool isModified; ///< Library modification status.
  301. LIB_ALIAS_MAP m_amap; ///< Map of alias objects associated with the library.
  302. int m_mod_hash; ///< incremented each time library is changed.
  303. friend class LIB_PART;
  304. friend class PART_LIBS;
  305. public:
  306. PART_LIB( int aType, const wxString& aFileName );
  307. ~PART_LIB();
  308. /**
  309. * Function Save
  310. * writes library to \a aFormatter.
  311. *
  312. * @param aFormatter An #OUTPUTFORMATTER object to write the library to.
  313. * @return True if success writing to \a aFormatter.
  314. */
  315. bool Save( OUTPUTFORMATTER& aFormatter );
  316. /**
  317. * Function SaveDocs
  318. * write the library document information to \a aFormatter.
  319. *
  320. * @param aFormatter An #OUTPUTFORMATTER object to write the library documentation to.
  321. * @return True if success writing to \a aFormatter.
  322. */
  323. bool SaveDocs( OUTPUTFORMATTER& aFormatter );
  324. /**
  325. * Load library from file.
  326. *
  327. * @param aErrorMsg - Error message if load fails.
  328. * @return True if load was successful otherwise false.
  329. */
  330. bool Load( wxString& aErrorMsg );
  331. bool LoadDocs( wxString& aErrorMsg );
  332. private:
  333. bool SaveHeader( OUTPUTFORMATTER& aFormatter );
  334. bool LoadHeader( LINE_READER& aLineReader );
  335. void LoadAliases( LIB_PART* aPart );
  336. public:
  337. /**
  338. * Get library entry status.
  339. *
  340. * @return True if there are no entries in the library.
  341. */
  342. bool IsEmpty() const
  343. {
  344. return m_amap.empty();
  345. }
  346. /**
  347. * Function GetCount
  348. * returns the number of entries in the library.
  349. *
  350. * @return The number of part and alias entries.
  351. */
  352. int GetCount() const
  353. {
  354. return m_amap.size();
  355. }
  356. bool IsModified() const
  357. {
  358. return isModified;
  359. }
  360. bool IsCache() const { return isCache; }
  361. void SetCache( void ) { isCache = true; }
  362. /**
  363. * Function IsReadOnly
  364. * @return true if current user does not have write access to the library file.
  365. */
  366. bool IsReadOnly() const { return !fileName.IsFileWritable(); }
  367. /**
  368. * Load a string array with the names of all the entries in this library.
  369. *
  370. * @param aNames - String array to place entry names into.
  371. * @param aSort - Sort names if true.
  372. * @param aMakeUpperCase - Force entry names to upper case.
  373. */
  374. void GetEntryNames( wxArrayString& aNames, bool aSort = true,
  375. bool aMakeUpperCase = false );
  376. /**
  377. * Load a string array with the names of entries of type POWER in this library.
  378. *
  379. * @param aNames - String array to place entry names into.
  380. * @param aSort - Sort names if true.
  381. * @param aMakeUpperCase - Force entry names to upper case.
  382. */
  383. void GetEntryTypePowerNames( wxArrayString& aNames, bool aSort = true,
  384. bool aMakeUpperCase = false );
  385. /**
  386. * Load string array with entry names matching name and/or key word.
  387. *
  388. * This currently mimics the old behavior of calling KeyWordOk() and
  389. * WildCompareString(). The names array will be populated with the
  390. * library entry names that meat the search criteria on exit.
  391. *
  392. * @param aNames - String array to place entry names into.
  393. * @param aNameSearch - Name wild card search criteria.
  394. * @param aKeySearch - Key word search criteria.
  395. * @param aSort - Sort names if true.
  396. */
  397. void SearchEntryNames( std::vector<wxArrayString>& aNames,
  398. const wxString& aNameSearch = wxEmptyString,
  399. const wxString& aKeySearch = wxEmptyString,
  400. bool aSort = true );
  401. /**
  402. * Find parts in library by key word regular expression search.
  403. *
  404. * @param aNames - String array to place found part names into.
  405. * @param aRe - Regular expression used to search part key words.
  406. * @param aSort - Sort part name list.
  407. */
  408. void SearchEntryNames( wxArrayString& aNames, const wxRegEx& aRe, bool aSort = true );
  409. /**
  410. * Checks \a aPart for name conflict in the library.
  411. *
  412. * @param aPart - The part to check.
  413. * @return True if a conflict exists. Otherwise false.
  414. */
  415. bool Conflicts( LIB_PART* aPart );
  416. /**
  417. * Find entry by name.
  418. *
  419. * @param aName - Name of entry, case sensitive.
  420. * @return Entry if found. NULL if not found.
  421. */
  422. LIB_ALIAS* FindEntry( const wxString& aName );
  423. /**
  424. * Find part by \a aName.
  425. *
  426. * This is a helper for FindEntry so casting a LIB_ALIAS pointer to
  427. * a LIB_PART pointer is not required.
  428. *
  429. * @param aName - Name of part, case sensitive.
  430. * @return LIB_PART* - part if found, else NULL.
  431. */
  432. LIB_PART* FindPart( const wxString& aName );
  433. /**
  434. * Find alias by \a nName.
  435. *
  436. * @param aName - Name of alias, case sensitive.
  437. * @return Alias if found. NULL if not found.
  438. */
  439. LIB_ALIAS* FindAlias( const wxString& aName )
  440. {
  441. return (LIB_ALIAS*) FindEntry( aName );
  442. }
  443. /**
  444. * Add a new \a aAlias entry to the library.
  445. *
  446. * First check if a part or alias with the same name already exists
  447. * in the library and add alias if no conflict occurs. Once the alias
  448. * is added to the library it is owned by the library. Deleting the
  449. * alias pointer will render the library unstable. Use RemoveEntry to
  450. * remove the alias from the library.
  451. *
  452. * @param aAlias - Alias to add to library.
  453. * @return True if alias added to library. False if a conflict exists.
  454. */
  455. bool AddAlias( LIB_ALIAS* aAlias );
  456. /**
  457. * Add \a aPart entry to library.
  458. * Note a part can have an alias list,
  459. * so these alias will be added in library.
  460. * Conflicts can happen if aliases are already existing.
  461. * User is asked to choose what alias is removed (existing, or new)
  462. *
  463. * @param aPart - Part to add, caller retains ownership, a clone is added.
  464. * @return bool - true iff successful.
  465. */
  466. bool AddPart( LIB_PART* aPart );
  467. /**
  468. * Safely remove \a aEntry from the library and return the next entry.
  469. *
  470. * The next entry returned depends on the entry being removed. If the entry being
  471. * remove also removes the part, then the next entry from the list is returned.
  472. * If the entry being used only removes an alias from a part, then the next alias
  473. * of the part is returned.
  474. *
  475. * @param aEntry - Entry to remove from library.
  476. * @return The next entry in the library or NULL if the library is empty.
  477. */
  478. LIB_ALIAS* RemoveEntry( LIB_ALIAS* aEntry );
  479. /**
  480. * Replace an existing part entry in the library.
  481. * Note a part can have an alias list,
  482. * so these alias will be added in library (and previously existing alias removed)
  483. * @param aOldPart - The part to replace.
  484. * @param aNewPart - The new part.
  485. */
  486. LIB_PART* ReplacePart( LIB_PART* aOldPart, LIB_PART* aNewPart );
  487. /**
  488. * Return the first entry in the library.
  489. *
  490. * @return The first entry or NULL if the library has no entries.
  491. */
  492. LIB_ALIAS* GetFirstEntry();
  493. /**
  494. * Find next library entry by \a aName.
  495. *
  496. * If the name of the entry is the last entry in the library, the first
  497. * entry in the list is returned.
  498. *
  499. * @param aName - Name of current entry.
  500. * @return Next entry if entry name is found. Otherwise NULL.
  501. */
  502. LIB_ALIAS* GetNextEntry( const wxString& aName );
  503. /**
  504. * Find previous library entry by \a aName.
  505. *
  506. * If the name of the entry is the first entry in the library, the last
  507. * entry in the list is returned.
  508. *
  509. * @param aName - Name of current entry.
  510. * @return Previous entry if entry name is found, otherwise NULL.
  511. */
  512. LIB_ALIAS* GetPreviousEntry( const wxString& aName );
  513. /**
  514. * Return the file name without path or extension.
  515. *
  516. * @return Name of library file.
  517. */
  518. const wxString GetName() const { return fileName.GetName(); }
  519. /**
  520. * Function GetFullFileName
  521. * returns the full file library name with path and extension.
  522. *
  523. * @return wxString - Full library file name with path and extension.
  524. */
  525. wxString GetFullFileName() { return fileName.GetFullPath(); }
  526. /**
  527. * Function GetLogicalName
  528. * returns the logical name of the library.
  529. * @return wxString - The logical name of this library.
  530. */
  531. const wxString GetLogicalName()
  532. {
  533. /* for now is the filename without path or extension.
  534. Technically the library should not know its logical name!
  535. This will eventually come out of a pair of lookup tables using a
  536. reverse lookup using the full name or library pointer as a key.
  537. Search will be by project lookup table and then user lookup table if
  538. not found.
  539. */
  540. return fileName.GetName();
  541. }
  542. /**
  543. * Function SetFileName
  544. * sets the part library file name.
  545. *
  546. * @param aFileName - New library file name.
  547. */
  548. void SetFileName( const wxString& aFileName )
  549. {
  550. if( aFileName != fileName.GetFullName() )
  551. fileName = aFileName;
  552. }
  553. /**
  554. * Function LoadLibrary
  555. * allocates and loads a part library file.
  556. *
  557. * @param aFileName - File name of the part library to load.
  558. * @return PART_LIB* - the allocated and loaded PART_LIB, which is owned by
  559. * the caller.
  560. * @throw IO_ERROR if there's any problem loading the library.
  561. */
  562. static PART_LIB* LoadLibrary( const wxString& aFileName ) throw( IO_ERROR, boost::bad_pointer );
  563. /**
  564. * Function HasPowerParts
  565. * @return true if at least one power part is found in lib
  566. * Useful to select or list only libs containing power parts
  567. */
  568. bool HasPowerParts();
  569. };
  570. /**
  571. * Case insensitive library name comparison.
  572. */
  573. bool operator==( const PART_LIB& aLibrary, const wxString& aName );
  574. bool operator!=( const PART_LIB& aLibrary, const wxString& aName );
  575. #endif // CLASS_LIBRARY_H