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.

468 lines
9.8 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-2018 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. nickname.clear();
  89. item_name.clear();
  90. revision.clear();
  91. }
  92. int LIB_ID::Parse( const UTF8& aId, LIB_ID_TYPE aType, 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. 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. //=====<nickname>==========================================
  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, aType, false );
  133. else
  134. offset = HasIllegalChars( fpname, aType );
  135. if( offset > -1 )
  136. return offset;
  137. SetLibItemName( fpname );
  138. return -1;
  139. }
  140. LIB_ID::LIB_ID( const wxString& aLibName, const wxString& aLibItemName,
  141. const wxString& aRevision ) :
  142. nickname( aLibName ),
  143. item_name( aLibItemName ),
  144. revision( aRevision )
  145. {
  146. }
  147. int LIB_ID::SetLibNickname( const UTF8& aLogical )
  148. {
  149. int offset = okLogical( aLogical );
  150. if( offset == -1 )
  151. {
  152. nickname = aLogical;
  153. }
  154. return offset;
  155. }
  156. int LIB_ID::SetLibItemName( const UTF8& aLibItemName, bool aTestForRev )
  157. {
  158. int separation = int( aLibItemName.find_first_of( "/" ) );
  159. if( aTestForRev && separation != -1 )
  160. {
  161. item_name = aLibItemName.substr( 0, separation-1 );
  162. return separation;
  163. }
  164. else
  165. {
  166. item_name = aLibItemName;
  167. }
  168. return -1;
  169. }
  170. int LIB_ID::SetRevision( const UTF8& aRevision )
  171. {
  172. int offset = okRevision( aRevision );
  173. if( offset == -1 )
  174. {
  175. revision = aRevision;
  176. }
  177. return offset;
  178. }
  179. UTF8 LIB_ID::Format() const
  180. {
  181. UTF8 ret;
  182. if( nickname.size() )
  183. {
  184. ret += nickname;
  185. ret += ':';
  186. }
  187. ret += item_name;
  188. if( revision.size() )
  189. {
  190. ret += '/';
  191. ret += revision;
  192. }
  193. return ret;
  194. }
  195. UTF8 LIB_ID::GetLibItemNameAndRev() const
  196. {
  197. UTF8 ret;
  198. if( revision.size() )
  199. {
  200. ret += '/';
  201. ret += revision;
  202. }
  203. return ret;
  204. }
  205. UTF8 LIB_ID::Format( const UTF8& aLogicalLib, const UTF8& aLibItemName, const UTF8& aRevision )
  206. {
  207. UTF8 ret;
  208. int offset;
  209. if( aLogicalLib.size() )
  210. {
  211. offset = okLogical( aLogicalLib );
  212. if( offset != -1 )
  213. {
  214. THROW_PARSE_ERROR( _( "Illegal character found in logical library name" ),
  215. wxString::FromUTF8( aLogicalLib.c_str() ),
  216. aLogicalLib.c_str(), 0, offset );
  217. }
  218. ret += aLogicalLib;
  219. ret += ':';
  220. }
  221. ret += aLibItemName; // TODO: Add validity test.
  222. if( aRevision.size() )
  223. {
  224. offset = okRevision( aRevision );
  225. if( offset != -1 )
  226. {
  227. THROW_PARSE_ERROR( _( "Illegal character found in revision" ),
  228. wxString::FromUTF8( aRevision.c_str() ),
  229. aRevision.c_str(),
  230. 0,
  231. offset );
  232. }
  233. ret += '/';
  234. ret += aRevision;
  235. }
  236. return ret;
  237. }
  238. int LIB_ID::compare( const LIB_ID& aLibId ) const
  239. {
  240. // Don't bother comparing the same object.
  241. if( this == &aLibId )
  242. return 0;
  243. int retv = nickname.compare( aLibId.nickname );
  244. if( retv != 0 )
  245. return retv;
  246. retv = item_name.compare( aLibId.item_name );
  247. if( retv != 0 )
  248. return retv;
  249. return revision.compare( aLibId.revision );
  250. }
  251. int LIB_ID::HasIllegalChars( const UTF8& aLibItemName, LIB_ID_TYPE aType )
  252. {
  253. int offset = 0;
  254. for( auto ch : aLibItemName )
  255. {
  256. if( !isLegalChar( ch, aType ) )
  257. return offset;
  258. else
  259. ++offset;
  260. }
  261. return -1;
  262. }
  263. UTF8 LIB_ID::FixIllegalChars( const UTF8& aLibItemName, LIB_ID_TYPE aType, bool aLib )
  264. {
  265. UTF8 fixedName;
  266. for( UTF8::uni_iter chIt = aLibItemName.ubegin(); chIt < aLibItemName.uend(); ++chIt )
  267. {
  268. auto ch = *chIt;
  269. if( aLib )
  270. fixedName += isLegalLibNicknameChar( ch, aType ) ? ch : '_';
  271. else
  272. fixedName += isLegalChar( ch, aType ) ? ch : '_';
  273. }
  274. return fixedName;
  275. }
  276. bool LIB_ID::isLegalChar( unsigned aUniChar, LIB_ID_TYPE aType )
  277. {
  278. bool const space_allowed = ( aType == ID_PCB );
  279. bool const illegal_filename_chars_allowed = ( aType == ID_SCH );
  280. if( aUniChar < ' ' )
  281. return false;
  282. switch( aUniChar )
  283. {
  284. case ':':
  285. case '/':
  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::FindIllegalLibNicknameChar( const UTF8& aNickname, LIB_ID_TYPE aType )
  299. {
  300. for( unsigned ch : aNickname )
  301. {
  302. if( !isLegalLibNicknameChar( ch, aType ) )
  303. return ch;
  304. }
  305. return 0;
  306. }
  307. bool LIB_ID::isLegalLibNicknameChar( unsigned aUniChar, LIB_ID_TYPE aType )
  308. {
  309. bool const space_allowed = ( aType != ID_SCH );
  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 && defined(DEBUG)
  324. // build this with Debug CMAKE_BUILD_TYPE
  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 item_name:'%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. int main( int argc, char** argv )
  346. {
  347. LIB_ID::Test();
  348. return 0;
  349. }
  350. #endif