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.

531 lines
17 KiB

15 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) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2008-2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2004-2011 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 component library class.
  28. */
  29. #ifndef CLASS_LIBRARY_H
  30. #define CLASS_LIBRARY_H
  31. #include <wx/filename.h>
  32. #include <class_libentry.h>
  33. class LINE_READER;
  34. class OUTPUTFORMATTER;
  35. /*
  36. * Component Library version and file header macros.
  37. */
  38. #define LIB_VERSION_MAJOR 2
  39. #define LIB_VERSION_MINOR 3
  40. /* Must be the first line of component library (.lib) files. */
  41. #define LIBFILE_IDENT "EESchema-LIBRARY Version"
  42. #define LIB_VERSION( major, minor ) ( major * 100 + minor )
  43. #define IS_LIB_CURRENT_VERSION( major, minor ) \
  44. ( \
  45. LIB_VERSION( major1, minor1 ) == \
  46. LIB_VERSION( LIB_VERSION_MAJOR, LIB_VERSION_MINOR) \
  47. )
  48. /*
  49. * Library versions 2.3 and lower use the old separate library (.lib) and
  50. * document (.dcm) files. Component libraries after 2.3 merged the library
  51. * and document files into a single library file. This macro checks if the
  52. * library version supports the old format
  53. */
  54. #define USE_OLD_DOC_FILE_FORMAT( major, minor ) \
  55. ( LIB_VERSION( major, minor ) <= LIB_VERSION( 2, 3 ) )
  56. /* Must be the first line of component library document (.dcm) files. */
  57. #define DOCFILE_IDENT "EESchema-DOCLIB Version 2.0"
  58. #define DOC_EXT wxT( "dcm" )
  59. /* Helpers for creating a list of component libraries. */
  60. class CMP_LIBRARY;
  61. class wxRegEx;
  62. typedef boost::ptr_vector< CMP_LIBRARY > CMP_LIBRARY_LIST;
  63. extern bool operator<( const CMP_LIBRARY& item1, const CMP_LIBRARY& item2 );
  64. /**
  65. * Class CMP_LIBRARY
  66. * is used to load, save, search, and otherwise manipulate
  67. * component library files.
  68. */
  69. class CMP_LIBRARY
  70. {
  71. int type; ///< Library type indicator.
  72. wxFileName fileName; ///< Library file name.
  73. wxDateTime timeStamp; ///< Library save time and date.
  74. int versionMajor; ///< Library major version number.
  75. int versionMinor; ///< Library minor version number.
  76. bool isCache; /**< False for the "standard" libraries,
  77. True for the library cache */
  78. wxString header; ///< first line of loaded library.
  79. bool isModified; ///< Library modification status.
  80. LIB_ALIAS_MAP aliases; ///< Map of aliases objects associated with the library.
  81. static CMP_LIBRARY_LIST libraryList;
  82. static wxArrayString libraryListSortOrder;
  83. friend class LIB_COMPONENT;
  84. public:
  85. CMP_LIBRARY( int aType, const wxFileName& aFileName );
  86. CMP_LIBRARY( int aType, const wxString& aFileName )
  87. {
  88. CMP_LIBRARY( aType, wxFileName( aFileName ) );
  89. }
  90. ~CMP_LIBRARY();
  91. /**
  92. * Function Save
  93. * writes library to \a aFormatter.
  94. *
  95. * @param aFormatter An #OUTPUTFORMATTER object to write the library to.
  96. * @return True if success writing to \a aFormatter.
  97. */
  98. bool Save( OUTPUTFORMATTER& aFormatter );
  99. /**
  100. * Function SaveDocs
  101. * write the library document information to \a aFormatter.
  102. *
  103. * @param aFormatter An #OUTPUTFORMATTER object to write the library documentation to.
  104. * @return True if success writing to \a aFormatter.
  105. */
  106. bool SaveDocs( OUTPUTFORMATTER& aFormatter );
  107. /**
  108. * Load library from file.
  109. *
  110. * @param aErrorMsg - Error message if load fails.
  111. * @return True if load was successful otherwise false.
  112. */
  113. bool Load( wxString& aErrorMsg );
  114. bool LoadDocs( wxString& aErrorMsg );
  115. private:
  116. bool SaveHeader( OUTPUTFORMATTER& aFormatter );
  117. bool LoadHeader( LINE_READER& aLineReader );
  118. void LoadAliases( LIB_COMPONENT* aComponent );
  119. public:
  120. /**
  121. * Get library entry status.
  122. *
  123. * @return True if there are no entries in the library.
  124. */
  125. bool IsEmpty() const
  126. {
  127. return aliases.empty();
  128. }
  129. /**
  130. * Function GetCount
  131. * returns the number of entries in the library.
  132. *
  133. * @return The number of component and alias entries.
  134. */
  135. int GetCount() const
  136. {
  137. return aliases.size();
  138. }
  139. bool IsModified() const
  140. {
  141. return isModified;
  142. }
  143. bool IsCache() const { return isCache; }
  144. void SetCache( void ) { isCache = true; }
  145. /**
  146. * Function IsReadOnly
  147. * @return true if current user does not have write access to the library file.
  148. */
  149. bool IsReadOnly() const { return !fileName.IsFileWritable(); }
  150. /**
  151. * Load a string array with the names of all the entries in this library.
  152. *
  153. * @param aNames - String array to place entry names into.
  154. * @param aSort - Sort names if true.
  155. * @param aMakeUpperCase - Force entry names to upper case.
  156. */
  157. void GetEntryNames( wxArrayString& aNames, bool aSort = true,
  158. bool aMakeUpperCase =
  159. #ifdef KICAD_KEEPCASE
  160. false
  161. #else
  162. true
  163. #endif
  164. );
  165. /**
  166. * Load string array with entry names matching name and/or key word.
  167. *
  168. * This currently mimics the old behavior of calling KeyWordOk() and
  169. * WildCompareString(). The names array will be populated with the
  170. * library entry names that meat the search criteria on exit.
  171. *
  172. * @param aNames - String array to place entry names into.
  173. * @param aNameSearch - Name wild card search criteria.
  174. * @param aKeySearch - Key word search criteria.
  175. * @param aSort - Sort names if true.
  176. */
  177. void SearchEntryNames( std::vector<wxArrayString>& aNames,
  178. const wxString& aNameSearch = wxEmptyString,
  179. const wxString& aKeySearch = wxEmptyString,
  180. bool aSort = true );
  181. /**
  182. * Find components in library by key word regular expression search.
  183. *
  184. * @param aNames - String array to place found component names into.
  185. * @param aRe - Regular expression used to search component key words.
  186. * @param aSort - Sort component name list.
  187. */
  188. void SearchEntryNames( wxArrayString& aNames, const wxRegEx& aRe, bool aSort = true );
  189. /**
  190. * Checks \a aComponent for name conflict in the library.
  191. *
  192. * @param aComponent - The component to check.
  193. * @return True if a conflict exists. Otherwise false.
  194. */
  195. bool Conflicts( LIB_COMPONENT* aComponent );
  196. /**
  197. * Find entry by name.
  198. *
  199. * @param aName - Name of entry, case insensitive.
  200. * @return Entry if found. NULL if not found.
  201. */
  202. LIB_ALIAS* FindEntry( const wxString& aName );
  203. /**
  204. * Find component by \a aName.
  205. *
  206. * This is a helper for FindEntry so casting a LIB_ALIAS pointer to
  207. * a LIB_COMPONENT pointer is not required.
  208. *
  209. * @param aName - Name of component, case insensitive.
  210. * @return Component if found. NULL if not found.
  211. */
  212. LIB_COMPONENT* FindComponent( const wxString& aName );
  213. /**
  214. * Find alias by \a nName.
  215. *
  216. * This is a helper for FindEntry so casting a LIB_ALIAS pointer to
  217. * a LIB_ALIAS pointer is not required.
  218. *
  219. * @param aName - Name of alias, case insensitive.
  220. * @return Alias if found. NULL if not found.
  221. */
  222. LIB_ALIAS* FindAlias( const wxString& aName )
  223. {
  224. return (LIB_ALIAS*) FindEntry( aName );
  225. }
  226. /**
  227. * Add a new \a aAlias entry to the library.
  228. *
  229. * First check if a component or alias with the same name already exists
  230. * in the library and add alias if no conflict occurs. Once the alias
  231. * is added to the library it is owned by the library. Deleting the
  232. * alias pointer will render the library unstable. Use RemoveEntry to
  233. * remove the alias from the library.
  234. *
  235. * @param aAlias - Alias to add to library.
  236. * @return True if alias added to library. False if a conflict exists.
  237. */
  238. bool AddAlias( LIB_ALIAS* aAlias );
  239. /**
  240. * Add \a aComponent entry to library.
  241. * Note a component can have an alias list,
  242. * so these alias will be added in library.
  243. * Conflicts can happen if aliases are already existing.
  244. * User is asked to choose what alias is removed (existing, or new)
  245. * @param aComponent - Component to add.
  246. * @return Added component if successful.
  247. */
  248. LIB_COMPONENT* AddComponent( LIB_COMPONENT* aComponent );
  249. /**
  250. * Safely remove \a aEntry from the library and return the next entry.
  251. *
  252. * The next entry returned depends on the entry being removed. If the entry being
  253. * remove also removes the component, then the next entry from the list is returned.
  254. * If the entry being used only removes an alias from a component, then the next alias
  255. * of the component is returned.
  256. *
  257. * @param aEntry - Entry to remove from library.
  258. * @return The next entry in the library or NULL if the library is empty.
  259. */
  260. LIB_ALIAS* RemoveEntry( LIB_ALIAS* aEntry );
  261. /**
  262. * Replace an existing component entry in the library.
  263. * Note a component can have an alias list,
  264. * so these alias will be added in library (and previously existing alias removed)
  265. * @param aOldComponent - The component to replace.
  266. * @param aNewComponent - The new component.
  267. */
  268. LIB_COMPONENT* ReplaceComponent( LIB_COMPONENT* aOldComponent,
  269. LIB_COMPONENT* aNewComponent );
  270. /**
  271. * Return the first entry in the library.
  272. *
  273. * @return The first entry or NULL if the library has no entries.
  274. */
  275. LIB_ALIAS* GetFirstEntry();
  276. /**
  277. * Find next library entry by \a aName.
  278. *
  279. * If the name of the entry is the last entry in the library, the first
  280. * entry in the list is returned.
  281. *
  282. * @param aName - Name of current entry.
  283. * @return Next entry if entry name is found. Otherwise NULL.
  284. */
  285. LIB_ALIAS* GetNextEntry( const wxString& aName );
  286. /**
  287. * Find previous library entry by \a aName.
  288. *
  289. * If the name of the entry is the first entry in the library, the last
  290. * entry in the list is returned.
  291. *
  292. * @param aName - Name of current entry.
  293. * @return Previous entry if entry name is found, otherwise NULL.
  294. */
  295. LIB_ALIAS* GetPreviousEntry( const wxString& aName );
  296. /**
  297. * Return the file name without path or extension.
  298. *
  299. * @return Name of library file.
  300. */
  301. wxString GetName() const { return fileName.GetName(); }
  302. /**
  303. * Function GetFullFileName
  304. * returns the full file library name with path and extension.
  305. *
  306. * @return wxString - Full library file name with path and extension.
  307. */
  308. wxString GetFullFileName() { return fileName.GetFullPath(); }
  309. /**
  310. * Function GetLogicalName
  311. * returns the logical name of the library.
  312. * @return wxString - The logical name of this library.
  313. */
  314. wxString GetLogicalName()
  315. {
  316. /* for now is the filename without path or extension.
  317. Technically the library should not know its logical name!
  318. This will eventually come out of a pair of lookup tables using a
  319. reverse lookup using the full name or library pointer as a key.
  320. Search will be by project lookup table and then user lookup table if
  321. not found.
  322. */
  323. return fileName.GetName();
  324. }
  325. /**
  326. * Function SetFileName
  327. * sets the component library file name.
  328. *
  329. * @param aFileName - New library file name.
  330. */
  331. void SetFileName( const wxFileName aFileName )
  332. {
  333. if( aFileName != fileName )
  334. fileName = aFileName;
  335. }
  336. /*
  337. * The following static methods are for manipulating the list of
  338. * component libraries. This eliminates the need for yet another
  339. * global variable ( formerly g_LibraryList ) and gives some measure
  340. * of safety from abusing the library list.
  341. */
  342. /**
  343. * Function LibraryExists
  344. * tests for existence of a library.
  345. *
  346. * @param aLibptr - aLibptr.
  347. * @return bool - true if exists, else false
  348. */
  349. static bool LibraryExists( const CMP_LIBRARY* aLibptr );
  350. /**
  351. * Function LoadLibrary
  352. * loads a component library file.
  353. *
  354. * @param aFileName - File name of the component library to load.
  355. * @param aErrorMsg - Error message if the component library failed to load.
  356. * @return Library object if library file loaded successfully,
  357. * otherwise NULL.
  358. */
  359. static CMP_LIBRARY* LoadLibrary( const wxFileName& aFileName, wxString& aErrorMsg );
  360. /**
  361. * Function AddLibrary
  362. * adds a component library to the library list.
  363. *
  364. * @param aFileName - File name object of component library.
  365. * @param aErrorMsg - Error message if the component library failed to load.
  366. * @return True if library loaded properly otherwise false.
  367. */
  368. static bool AddLibrary( const wxFileName& aFileName, wxString& aErrorMsg );
  369. /**
  370. * Function AddLibrary
  371. * inserts a component library into the library list.
  372. *
  373. * @param aFileName - File name object of component library.
  374. * @param aErrorMsg - Error message if the component library failed to load.
  375. * @param aIterator - Iterator to insert library in front of.
  376. * @return True if library loaded properly otherwise false.
  377. */
  378. static bool AddLibrary( const wxFileName& aFileName, wxString& aErrorMsg,
  379. CMP_LIBRARY_LIST::iterator& aIterator );
  380. /**
  381. * Function RemoveLibrary
  382. * removes a component library from the library list.
  383. *
  384. * @param aName - Name of component library to remove.
  385. */
  386. static void RemoveLibrary( const wxString& aName );
  387. static void RemoveAllLibraries() { libraryList.clear(); }
  388. /**
  389. * Function FindLibrary
  390. * finds a component library by \a aName.
  391. *
  392. * @param aName - Library file name without path or extension to find.
  393. * @return Component library if found, otherwise NULL.
  394. */
  395. static CMP_LIBRARY* FindLibrary( const wxString& aName );
  396. /**
  397. * Function GetLibraryNames
  398. * returns the list of component library file names without path and extension.
  399. *
  400. * @param aSorted - Sort the list of name if true. Otherwise use the
  401. * library load order.
  402. * @return The list of library names.
  403. */
  404. static wxArrayString GetLibraryNames( bool aSorted = true );
  405. /**
  406. * Function FindLibraryComponent
  407. * searches all libraries in the list for a component.
  408. *
  409. * A component object will always be returned. If the entry found
  410. * is an alias. The root component will be found and returned.
  411. *
  412. * @param aComponentName - Name of component to search for.
  413. * @param aLibraryName - Name of the library to search for component.
  414. * @return The component object if found, otherwise NULL.
  415. */
  416. static LIB_COMPONENT* FindLibraryComponent( const wxString& aComponentName,
  417. const wxString& aLibraryName = wxEmptyString );
  418. /**
  419. * Function FindLibraryEntry
  420. * searches all libraries in the list for an entry.
  421. *
  422. * The object can be either a component or an alias.
  423. *
  424. * @param aEntryName - Name of entry to search for.
  425. * @param aLibraryName - Name of the library to search.
  426. * @return The entry object if found, otherwise NULL.
  427. */
  428. static LIB_ALIAS* FindLibraryEntry( const wxString& aEntryName,
  429. const wxString& aLibraryName = wxEmptyString );
  430. /**
  431. * Function RemoveCacheLibrary
  432. * removes all cache libraries from library list.
  433. */
  434. static void RemoveCacheLibrary();
  435. static int GetLibraryCount() { return libraryList.size(); }
  436. static CMP_LIBRARY_LIST& GetLibraryList()
  437. {
  438. return libraryList;
  439. }
  440. static void SetSortOrder( const wxArrayString& aSortOrder )
  441. {
  442. libraryListSortOrder = aSortOrder;
  443. }
  444. static wxArrayString& GetSortOrder( void )
  445. {
  446. return libraryListSortOrder;
  447. }
  448. };
  449. /**
  450. * Case insensitive library name comparison.
  451. */
  452. extern bool operator==( const CMP_LIBRARY& aLibrary, const wxString& aName );
  453. extern bool operator!=( const CMP_LIBRARY& aLibrary, const wxString& aName );
  454. #endif // CLASS_LIBRARY_H