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.

460 lines
9.7 KiB

Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
12 years ago
Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
12 years ago
  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/wx.h> // _()
  28. #include <macros.h> // TO_UTF8()
  29. #include <lib_id.h>
  30. #include <kicad_string.h>
  31. static inline bool isDigit( char c )
  32. {
  33. return c >= '0' && c <= '9';
  34. }
  35. const char* EndsWithRev( const char* start, const char* tail, char separator )
  36. {
  37. bool sawDigit = false;
  38. while( tail > start && isDigit( *--tail ) )
  39. {
  40. sawDigit = true;
  41. }
  42. // if sawDigit, tail points to the 'v' here.
  43. if( sawDigit && tail-3 >= start )
  44. {
  45. tail -= 3;
  46. if( tail[0]==separator && tail[1]=='r' && tail[2]=='e' && tail[3]=='v' )
  47. {
  48. return tail+1; // omit separator, return "revN[N..]"
  49. }
  50. }
  51. return 0;
  52. }
  53. #if 0 // Not used
  54. int RevCmp( const char* s1, const char* s2 )
  55. {
  56. int r = strncmp( s1, s2, 3 );
  57. if( r || strlen(s1)<4 || strlen(s2)<4 )
  58. {
  59. return r;
  60. }
  61. int rnum1 = atoi( s1+3 );
  62. int rnum2 = atoi( s2+3 );
  63. return -(rnum1 - rnum2); // swap the sign, higher revs first
  64. }
  65. #endif
  66. //----<Policy and field test functions>-------------------------------------
  67. static inline int okLogical( const UTF8& aField )
  68. {
  69. // std::string::npos is largest positive number, casting to int makes it -1.
  70. // Returning that means success.
  71. return int( aField.find_first_of( ":" ) );
  72. }
  73. static int okRevision( const UTF8& aField )
  74. {
  75. char rev[32]; // C string for speed
  76. if( aField.size() >= 4 )
  77. {
  78. strncpy( rev, "x/", sizeof( rev ) );
  79. strncat( rev, aField.c_str(), sizeof(rev)-strlen(rev)-1 );
  80. if( EndsWithRev( rev, rev + strlen(rev), '/' ) == rev+2 )
  81. return -1; // success
  82. }
  83. return 0; // first character position "is in error", is best we can do.
  84. }
  85. //----</Policy and field test functions>-------------------------------------
  86. void LIB_ID::clear()
  87. {
  88. m_libraryName.clear();
  89. m_itemName.clear();
  90. m_revision.clear();
  91. }
  92. int LIB_ID::Parse( const UTF8& aId, bool aFix )
  93. {
  94. clear();
  95. const char* buffer = aId.c_str();
  96. const char* rev = EndsWithRev( buffer, buffer+aId.length(), '/' );
  97. size_t revNdx;
  98. size_t partNdx;
  99. int offset = -1;
  100. //=====<revision>=====================================
  101. // in a LIB_ID like discret:R3/rev4
  102. if( rev )
  103. {
  104. revNdx = rev - buffer;
  105. // no need to check revision, EndsWithRev did that.
  106. m_revision = aId.substr( revNdx );
  107. --revNdx; // back up to omit the '/' which precedes the rev
  108. }
  109. else
  110. {
  111. revNdx = aId.size();
  112. }
  113. //=====<name>=========================================
  114. if( ( partNdx = aId.find( ':' ) ) != aId.npos )
  115. {
  116. offset = SetLibNickname( aId.substr( 0, partNdx ) );
  117. if( offset > -1 )
  118. return offset;
  119. ++partNdx; // skip ':'
  120. }
  121. else
  122. {
  123. partNdx = 0;
  124. }
  125. //=====<item name>====================================
  126. if( partNdx >= revNdx )
  127. return partNdx; // Error: no library item name.
  128. UTF8 fpname = aId.substr( partNdx, revNdx-partNdx );
  129. // Be sure the item name is valid.
  130. // Some chars can be found in legacy files converted files from other EDA tools.
  131. if( aFix )
  132. fpname = FixIllegalChars( fpname, false );
  133. else
  134. offset = HasIllegalChars( fpname );
  135. if( offset > -1 )
  136. return offset;
  137. SetLibItemName( fpname );
  138. return -1;
  139. }
  140. LIB_ID::LIB_ID( const wxString& aLibraryName, const wxString& aItemName,
  141. const wxString& aRevision ) :
  142. m_libraryName( aLibraryName ),
  143. m_itemName( aItemName ),
  144. m_revision( aRevision )
  145. {
  146. }
  147. int LIB_ID::SetLibNickname( const UTF8& aLogical )
  148. {
  149. int offset = okLogical( aLogical );
  150. if( offset == -1 )
  151. m_libraryName = aLogical;
  152. return offset;
  153. }
  154. int LIB_ID::SetLibItemName( const UTF8& aLibItemName, bool aTestForRev )
  155. {
  156. int separation = int( aLibItemName.find_first_of( "/" ) );
  157. if( aTestForRev && separation != -1 )
  158. {
  159. m_itemName = aLibItemName.substr( 0, separation-1 );
  160. return separation;
  161. }
  162. else
  163. {
  164. m_itemName = aLibItemName;
  165. }
  166. return -1;
  167. }
  168. int LIB_ID::SetRevision( const UTF8& aRevision )
  169. {
  170. int offset = okRevision( aRevision );
  171. if( offset == -1 )
  172. m_revision = aRevision;
  173. return offset;
  174. }
  175. UTF8 LIB_ID::Format() const
  176. {
  177. UTF8 ret;
  178. if( m_libraryName.size() )
  179. {
  180. ret += m_libraryName;
  181. ret += ':';
  182. }
  183. ret += m_itemName;
  184. if( m_revision.size() )
  185. {
  186. ret += '/';
  187. ret += m_revision;
  188. }
  189. return ret;
  190. }
  191. UTF8 LIB_ID::GetLibItemNameAndRev() const
  192. {
  193. UTF8 ret = m_itemName;
  194. if( m_revision.size() )
  195. {
  196. ret += '/';
  197. ret += m_revision;
  198. }
  199. return ret;
  200. }
  201. UTF8 LIB_ID::Format( const UTF8& aLibraryName, const UTF8& aLibItemName, const UTF8& aRevision )
  202. {
  203. UTF8 ret;
  204. int offset;
  205. if( aLibraryName.size() )
  206. {
  207. offset = okLogical( aLibraryName );
  208. if( offset != -1 )
  209. {
  210. THROW_PARSE_ERROR( _( "Illegal character found in logical library name" ),
  211. wxString::FromUTF8( aLibraryName.c_str() ), aLibraryName.c_str(), 0, offset );
  212. }
  213. ret += aLibraryName;
  214. ret += ':';
  215. }
  216. ret += aLibItemName; // TODO: Add validity test.
  217. if( aRevision.size() )
  218. {
  219. offset = okRevision( aRevision );
  220. if( offset != -1 )
  221. {
  222. THROW_PARSE_ERROR( _( "Illegal character found in revision" ),
  223. wxString::FromUTF8( aRevision.c_str() ),
  224. aRevision.c_str(),
  225. 0,
  226. offset );
  227. }
  228. ret += '/';
  229. ret += aRevision;
  230. }
  231. return ret;
  232. }
  233. int LIB_ID::compare( const LIB_ID& aLibId ) const
  234. {
  235. // Don't bother comparing the same object.
  236. if( this == &aLibId )
  237. return 0;
  238. int retv = m_libraryName.compare( aLibId.m_libraryName );
  239. if( retv != 0 )
  240. return retv;
  241. retv = m_itemName.compare( aLibId.m_itemName );
  242. if( retv != 0 )
  243. return retv;
  244. return m_revision.compare( aLibId.m_revision );
  245. }
  246. int LIB_ID::HasIllegalChars( const UTF8& aLibItemName )
  247. {
  248. int offset = 0;
  249. for( auto ch : aLibItemName )
  250. {
  251. if( !isLegalChar( ch ) )
  252. return offset;
  253. else
  254. ++offset;
  255. }
  256. return -1;
  257. }
  258. UTF8 LIB_ID::FixIllegalChars( const UTF8& aLibItemName, bool aLib )
  259. {
  260. UTF8 fixedName;
  261. for( UTF8::uni_iter chIt = aLibItemName.ubegin(); chIt < aLibItemName.uend(); ++chIt )
  262. {
  263. auto ch = *chIt;
  264. if( aLib )
  265. fixedName += isLegalLibraryNameChar( ch ) ? ch : '_';
  266. else
  267. fixedName += isLegalChar( ch ) ? ch : '_';
  268. }
  269. return fixedName;
  270. }
  271. bool LIB_ID::isLegalChar( unsigned aUniChar )
  272. {
  273. bool const space_allowed = true;
  274. bool const illegal_filename_chars_allowed = false;
  275. if( aUniChar < ' ' )
  276. return false;
  277. // This list of characters is also duplicated in validators.cpp and footprint.cpp
  278. // TODO: Unify forbidden character lists
  279. switch( aUniChar )
  280. {
  281. case ':':
  282. case '/':
  283. case '\t':
  284. case '\n':
  285. case '\r':
  286. return false;
  287. case '\\':
  288. case '<':
  289. case '>':
  290. case '"':
  291. return illegal_filename_chars_allowed;
  292. case ' ':
  293. return space_allowed;
  294. default:
  295. return true;
  296. }
  297. }
  298. unsigned LIB_ID::FindIllegalLibraryNameChar( const UTF8& aLibraryName )
  299. {
  300. for( unsigned ch : aLibraryName )
  301. {
  302. if( !isLegalLibraryNameChar( ch ) )
  303. return ch;
  304. }
  305. return 0;
  306. }
  307. bool LIB_ID::isLegalLibraryNameChar( unsigned aUniChar )
  308. {
  309. bool const space_allowed = true;
  310. if( aUniChar < ' ' )
  311. return false;
  312. switch( aUniChar )
  313. {
  314. case '\\':
  315. case ':':
  316. return false;
  317. case ' ':
  318. return space_allowed;
  319. default:
  320. return true;
  321. }
  322. }
  323. #if 0
  324. // @todo Move this test into the unit test framework.
  325. void LIB_ID::Test()
  326. {
  327. static const char* lpids[] = {
  328. "smt:R_0805/rev0",
  329. "mysmt:R_0805/rev2",
  330. "device:AXIAL-0500",
  331. };
  332. for( unsigned i=0; i<sizeof(lpids)/sizeof(lpids[0]); ++i )
  333. {
  334. // test some round tripping
  335. LIB_ID lpid( lpids[i] ); // parse
  336. // format
  337. printf( "input:'%s' full:'%s' nickname: %s m_itemName:'%s' rev:'%s'\n",
  338. lpids[i],
  339. lpid.Format().c_str(),
  340. lpid.GetLibNickname().c_str(),
  341. lpid.GetLibItemName().c_str(),
  342. lpid.GetRevision().c_str() );
  343. }
  344. }
  345. #endif