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.

283 lines
6.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2010 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. #include <cstring>
  26. #include <memory>
  27. #include <wx/translation.h>
  28. #include <ki_exception.h>
  29. #include <macros.h> // TO_UTF8()
  30. #include <lib_id.h>
  31. static int checkLibNickname( const UTF8& aField )
  32. {
  33. // std::string::npos is largest positive number, casting to int makes it -1.
  34. // Returning that means success.
  35. return int( aField.find_first_of( ":" ) );
  36. }
  37. void LIB_ID::clear()
  38. {
  39. m_libraryName.clear();
  40. m_itemName.clear();
  41. m_subLibraryName.clear();
  42. }
  43. int LIB_ID::Parse( const UTF8& aId, bool aFix )
  44. {
  45. clear();
  46. size_t partNdx;
  47. int offset = -1;
  48. //=====<library nickname>=============================
  49. if( ( partNdx = aId.find( ':' ) ) != aId.npos )
  50. {
  51. offset = SetLibNickname( aId.substr( 0, partNdx ) );
  52. if( offset > -1 )
  53. return offset;
  54. ++partNdx; // skip ':'
  55. }
  56. else
  57. {
  58. partNdx = 0;
  59. }
  60. //=====<item name>====================================
  61. UTF8 fpname = aId.substr( partNdx );
  62. // Be sure the item name is valid.
  63. // Some chars can be found in legacy files converted files from other EDA tools.
  64. if( aFix )
  65. fpname = FixIllegalChars( fpname, false );
  66. else
  67. offset = HasIllegalChars( fpname );
  68. if( offset > -1 )
  69. return offset;
  70. SetLibItemName( fpname );
  71. return -1;
  72. }
  73. LIB_ID::LIB_ID( const wxString& aLibraryName, const wxString& aItemName ) :
  74. m_libraryName( aLibraryName ),
  75. m_itemName( aItemName )
  76. {
  77. }
  78. int LIB_ID::SetLibNickname( const UTF8& aLibNickname )
  79. {
  80. int offset = checkLibNickname( aLibNickname );
  81. if( offset == -1 )
  82. m_libraryName = aLibNickname;
  83. return offset;
  84. }
  85. int LIB_ID::SetLibItemName( const UTF8& aLibItemName )
  86. {
  87. m_itemName = aLibItemName;
  88. return -1;
  89. }
  90. UTF8 LIB_ID::Format() const
  91. {
  92. UTF8 ret;
  93. if( m_libraryName.size() )
  94. {
  95. ret += m_libraryName;
  96. ret += ':';
  97. }
  98. ret += m_itemName;
  99. return ret;
  100. }
  101. UTF8 LIB_ID::Format( const UTF8& aLibraryName, const UTF8& aLibItemName )
  102. {
  103. UTF8 ret;
  104. int offset;
  105. if( aLibraryName.size() )
  106. {
  107. offset = checkLibNickname( aLibraryName );
  108. if( offset != -1 )
  109. {
  110. THROW_PARSE_ERROR( _( "Illegal character found in library nickname" ),
  111. wxString::FromUTF8( aLibraryName.c_str() ), aLibraryName.c_str(),
  112. 0, offset );
  113. }
  114. ret += aLibraryName;
  115. ret += ':';
  116. }
  117. ret += aLibItemName;
  118. return ret;
  119. }
  120. int LIB_ID::compare( const LIB_ID& aLibId ) const
  121. {
  122. // Don't bother comparing the same object.
  123. if( this == &aLibId )
  124. return 0;
  125. int retv = m_libraryName.compare( aLibId.m_libraryName );
  126. if( retv != 0 )
  127. return retv;
  128. return m_itemName.compare( aLibId.m_itemName );
  129. }
  130. int LIB_ID::HasIllegalChars( const UTF8& aLibItemName )
  131. {
  132. int offset = 0;
  133. for( auto& ch : aLibItemName )
  134. {
  135. if( !isLegalChar( ch ) )
  136. return offset;
  137. else
  138. ++offset;
  139. }
  140. return -1;
  141. }
  142. UTF8 LIB_ID::FixIllegalChars( const UTF8& aLibItemName, bool aLib )
  143. {
  144. UTF8 fixedName;
  145. for( UTF8::uni_iter chIt = aLibItemName.ubegin(); chIt < aLibItemName.uend(); ++chIt )
  146. {
  147. auto ch = *chIt;
  148. if( aLib )
  149. fixedName += isLegalLibraryNameChar( ch ) ? ch : '_';
  150. else
  151. fixedName += isLegalChar( ch ) ? ch : '_';
  152. }
  153. return fixedName;
  154. }
  155. bool LIB_ID::isLegalChar( unsigned aUniChar )
  156. {
  157. bool const space_allowed = true;
  158. bool const illegal_filename_chars_allowed = false;
  159. if( aUniChar < ' ' )
  160. return false;
  161. // This list of characters is also duplicated in validators.cpp and footprint.cpp
  162. // TODO: Unify forbidden character lists - Warning, invalid filename characters are not the same
  163. // as invalid LIB_ID characters. We will need to separate the FP filenames from FP names before this
  164. // can be unified
  165. switch( aUniChar )
  166. {
  167. case ':':
  168. case '\t':
  169. case '\n':
  170. case '\r':
  171. return false;
  172. case '\\':
  173. case '<':
  174. case '>':
  175. case '"':
  176. return illegal_filename_chars_allowed;
  177. case ' ':
  178. return space_allowed;
  179. default:
  180. return true;
  181. }
  182. }
  183. unsigned LIB_ID::FindIllegalLibraryNameChar( const UTF8& aLibraryName )
  184. {
  185. for( unsigned ch : aLibraryName )
  186. {
  187. if( !isLegalLibraryNameChar( ch ) )
  188. return ch;
  189. }
  190. return 0;
  191. }
  192. bool LIB_ID::isLegalLibraryNameChar( unsigned aUniChar )
  193. {
  194. bool const space_allowed = true;
  195. if( aUniChar < ' ' )
  196. return false;
  197. switch( aUniChar )
  198. {
  199. case '\\':
  200. case ':':
  201. return false;
  202. case ' ':
  203. return space_allowed;
  204. default:
  205. return true;
  206. }
  207. }
  208. const wxString LIB_ID::GetFullLibraryName() const
  209. {
  210. wxString suffix = m_subLibraryName.wx_str().IsEmpty()
  211. ? wxString( wxS( "" ) )
  212. : wxString::Format( wxT( " - %s" ), m_subLibraryName.wx_str() );
  213. return wxString::Format( wxT( "%s%s" ), m_libraryName.wx_str(), suffix );
  214. }