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.

263 lines
10 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2010-2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2012 Wayne Stambaugh <stambaughw@gmail.com>
  6. * Copyright (C) 2010-2020 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. #ifndef _LIB_ID_H_
  26. #define _LIB_ID_H_
  27. #include <richio.h>
  28. #include <utf8.h>
  29. /**
  30. * A logical library item identifier and consists of various portions much like a URI.
  31. *
  32. * It consists of of triad of the library nickname, the name of the item in the library,
  33. * and an optional revision of the item. This is a generic library identifier that can be
  34. * used for any type of library that contains multiple named items such as footprint or
  35. * symbol libraries.
  36. *
  37. * Example LIB_ID string:
  38. * "smt:R_0805/rev0".
  39. *
  40. * - "smt" is the logical library name used to look up library information saved in the #LIB_TABLE.
  41. * - "R" is the name of the item within the library.
  42. * - "rev0" is the revision, which is optional. If missing then its delimiter should also not
  43. * be present. A revision must begin with "rev" and be followed by at least one or more
  44. * decimal digits.
  45. *
  46. * @author Dick Hollenbeck
  47. */
  48. class LIB_ID
  49. {
  50. public:
  51. LIB_ID() {}
  52. // NOTE: don't define any constructors which call Parse() on their arguments. We want it
  53. // to be obvious to callers that parsing is involved (and that valid IDs are guaranteed in
  54. // the presence of disallowed characters, malformed ids, etc.).
  55. /**
  56. * This LIB_ID ctor is a special version which ignores the parsing due to symbol
  57. * names allowing '/' as a valid character. This was causing the symbol names to
  58. * be truncated at the first occurrence of '/' in the symbol name.
  59. *
  60. * @param aLibraryName is the library name used to look up the library item in the #LIB_TABLE.
  61. * @param aItemName is the name of the library item which is not parsed by the standard
  62. * LIB_ID::Parse() function.
  63. */
  64. LIB_ID( const wxString& aLibraryName, const wxString& aItemName );
  65. /**
  66. * Parse LIB_ID with the information from @a aId.
  67. *
  68. * A typical LIB_ID string consists of a library nickname followed by a library item name.
  69. * e.g.: "smt:R_0805", or
  70. * e.g.: "mylib:R_0805", or
  71. * e.g.: "ttl:7400"
  72. *
  73. * @param aId is the string to populate the #LIB_ID object.
  74. * @param aFix indicates invalid chars should be replaced with '_'.
  75. *
  76. * @return minus 1 (i.e. -1) means success, >= 0 indicates the character offset into
  77. * aId at which an error was detected.
  78. */
  79. int Parse( const UTF8& aId, bool aFix = false );
  80. /**
  81. * Return the logical library name portion of a LIB_ID.
  82. */
  83. const UTF8& GetLibNickname() const { return m_libraryName; }
  84. /**
  85. * Override the logical library name portion of the LIB_ID to @a aNickname.
  86. *
  87. * @return int - minus 1 (i.e. -1) means success, >= 0 indicates the character offset
  88. * into the parameter at which an error was detected, usually because it
  89. * contained '/' or ':'.
  90. */
  91. int SetLibNickname( const UTF8& aNickname );
  92. /**
  93. * @return the library item name, i.e. footprintName, in UTF8.
  94. */
  95. const UTF8& GetLibItemName() const { return m_itemName; }
  96. /**
  97. * Get strings for display messages in dialogs.
  98. *
  99. * Equivalent to m_itemName.wx_str(), but more explicit when building a Unicode string
  100. * in messages.
  101. *
  102. * @return the library item name, i.e. footprintName in a wxString (UTF16 or 32).
  103. */
  104. const wxString GetUniStringLibItemName() const { return m_itemName.wx_str(); }
  105. /**
  106. * Override the library item name portion of the LIB_ID to @a aLibItemName
  107. *
  108. * @return int - minus 1 (i.e. -1) means success, >= 0 indicates the character offset
  109. * into the parameter at which an error was detected, usually because it
  110. * contained '/'.
  111. */
  112. int SetLibItemName( const UTF8& aLibItemName );
  113. /**
  114. * @return the fully formatted text of the LIB_ID in a UTF8 string.
  115. */
  116. UTF8 Format() const;
  117. /**
  118. * @return the fully formatted text of the LIB_ID in a wxString (UTF16 or UTF32),
  119. * suitable to display the LIB_ID in dialogs.
  120. */
  121. wxString GetUniStringLibId() const
  122. {
  123. return Format().wx_str();
  124. }
  125. /**
  126. * @return a string in the proper format as an LIB_ID for a combination of
  127. * aLibraryName, aLibItemName, and aRevision.
  128. *
  129. * @throw PARSE_ERROR if any of the pieces are illegal.
  130. */
  131. static UTF8 Format( const UTF8& aLibraryName, const UTF8& aLibItemName );
  132. /**
  133. * Check if this LID_ID is valid.
  134. *
  135. * A valid #LIB_ID must have both the library nickname and the library item name defined.
  136. * The revision field is optional.
  137. *
  138. * @note A return value of true does not indicated that the #LIB_ID is a valid #LIB_TABLE
  139. * entry.
  140. *
  141. * @return true is the #LIB_ID is valid.
  142. *
  143. */
  144. bool IsValid() const
  145. {
  146. return !m_libraryName.empty() && !m_itemName.empty();
  147. }
  148. /**
  149. * @return true if the #LIB_ID only has the #m_itemName name defined.
  150. */
  151. bool IsLegacy() const
  152. {
  153. return m_libraryName.empty() && !m_itemName.empty();
  154. }
  155. /**
  156. * Clear the contents of the library nickname, library entry name, and revision strings.
  157. */
  158. void clear();
  159. /**
  160. * @return a boolean true value if the LIB_ID is empty. Otherwise return false.
  161. */
  162. bool empty() const
  163. {
  164. return m_libraryName.empty() && m_itemName.empty();
  165. }
  166. /**
  167. * Compare the contents of LIB_ID objects by performing a std::string comparison of the
  168. * library nickname, library entry name, and revision strings respectively.
  169. *
  170. * @param aLibId is the LIB_ID to compare against.
  171. * @return -1 if less than \a aLibId, 1 if greater than \a aLibId, and 0 if equal to \a aLibId.
  172. */
  173. int compare( const LIB_ID& aLibId ) const;
  174. bool operator < ( const LIB_ID& aLibId ) const { return this->compare( aLibId ) < 0; }
  175. bool operator > ( const LIB_ID& aLibId ) const { return this->compare( aLibId ) > 0; }
  176. bool operator ==( const LIB_ID& aLibId ) const { return this->compare( aLibId ) == 0; }
  177. bool operator !=( const LIB_ID& aLibId ) const { return !(*this == aLibId); }
  178. /**
  179. * Examine \a aLibItemName for invalid #LIB_ID item name characters.
  180. *
  181. * @param aLibItemName is the #LIB_ID name to test for illegal characters.
  182. * @return offset of first illegal character otherwise -1.
  183. */
  184. static int HasIllegalChars( const UTF8& aLibItemName );
  185. /**
  186. * Replace illegal #LIB_ID item name characters with underscores '_'.
  187. *
  188. * @param aLibItemName is the #LIB_ID item name to replace illegal characters.
  189. * @param aLib True if we are checking library names, false if we are checking item names
  190. * @return the corrected version of \a aLibItemName.
  191. */
  192. static UTF8 FixIllegalChars( const UTF8& aLibItemName, bool aLib );
  193. /**
  194. * Looks for characters that are illegal in library nicknames.
  195. *
  196. * @param aLibraryName is the logical library name to be tested.
  197. * @return Invalid character found in the name or 0 is the name is valid.
  198. */
  199. static unsigned FindIllegalLibraryNameChar( const UTF8& aLibraryName );
  200. protected:
  201. /**
  202. * Tests whether a Unicode character is a legal LIB_ID item name character.
  203. *
  204. * The criteria for legal LIB_ID character is as follows:
  205. * - For both symbol and footprint names, neither '/' or ':' are legal. They are
  206. * reserved characters used by #LIB_ID::Parse.
  207. * - Spaces are allowed in footprint names as they are a legal filename character
  208. * on all operating systems.
  209. * - Spaces are not allowed in symbol names since symbol names are not quoted in the
  210. * schematic or symbol library file formats.
  211. * - Spaces are allowed in footprint library nicknames as they are quoted in the
  212. * footprint library table file format.
  213. * - Spaces are now also allowed in symbol library nicknames since they are quoted in
  214. * the new symbol library sexpr file format.
  215. * - Illegal file name characters are not allowed in footprint names since the file
  216. * name is the footprint name.
  217. * - Illegal file name characters except '/' are allowed in symbol names since the
  218. * name is not the file name.
  219. *
  220. *
  221. * @note @a aUniChar is expected to be a 32 bit Unicode character, not a UTF8 char, that use
  222. * a variable length coding value.
  223. */
  224. static bool isLegalChar( unsigned aUniChar );
  225. /**
  226. * Tests whether a Unicode character is a legal LIB_ID library nickname character
  227. *
  228. * @note @a aUniChar is expected to be a 32 bit Unicode character, not a UTF8 char, that use
  229. * a variable length coding value.
  230. */
  231. static bool isLegalLibraryNameChar( unsigned aUniChar );
  232. UTF8 m_libraryName; ///< The nickname of the library or empty.
  233. UTF8 m_itemName; ///< The name of the entry in the logical library.
  234. };
  235. #endif // _LIB_ID_H_