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.

398 lines
8.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
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-2016 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2010-2017 KiCad Developers, see AUTHORS.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 std::string& 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 std::string& aField )
  74. {
  75. char rev[32]; // C string for speed
  76. if( aField.size() >= 4 )
  77. {
  78. strcpy( rev, "x/" );
  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 )
  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;
  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. {
  119. return offset;
  120. }
  121. ++partNdx; // skip ':'
  122. }
  123. else
  124. {
  125. partNdx = 0;
  126. }
  127. //=====<item name>====================================
  128. if( partNdx >= revNdx )
  129. return partNdx; // Error: no library item name.
  130. // Be sure the item name is valid.
  131. // Some chars can be found in legacy files converted files from an other EDA tools.
  132. std::string fpname = aId.substr( partNdx, revNdx-partNdx );
  133. ReplaceIllegalFileNameChars( &fpname, '_' );
  134. SetLibItemName( fpname );
  135. return -1;
  136. }
  137. LIB_ID::LIB_ID( const std::string& aId )
  138. {
  139. int offset = Parse( aId );
  140. if( offset != -1 )
  141. {
  142. THROW_PARSE_ERROR( _( "Illegal character found in LIB_ID string" ),
  143. wxString::FromUTF8( aId.c_str() ),
  144. aId.c_str(),
  145. 0,
  146. offset );
  147. }
  148. }
  149. LIB_ID::LIB_ID( const wxString& aId )
  150. {
  151. UTF8 id = TO_UTF8( aId );
  152. int offset = Parse( id );
  153. if( offset != -1 )
  154. {
  155. THROW_PARSE_ERROR( _( "Illegal character found in LIB_ID string" ),
  156. aId,
  157. id.c_str(),
  158. 0,
  159. offset );
  160. }
  161. }
  162. LIB_ID::LIB_ID( const wxString& aLibName, const wxString& aLibItemName,
  163. const wxString& aRevision ) :
  164. nickname( TO_UTF8( aLibName ) ),
  165. item_name( TO_UTF8( aLibItemName ) ),
  166. revision( TO_UTF8( aRevision ) )
  167. {
  168. }
  169. int LIB_ID::SetLibNickname( const UTF8& aLogical )
  170. {
  171. int offset = okLogical( aLogical );
  172. if( offset == -1 )
  173. {
  174. nickname = aLogical;
  175. }
  176. return offset;
  177. }
  178. int LIB_ID::SetLibItemName( const UTF8& aLibItemName, bool aTestForRev )
  179. {
  180. int separation = int( aLibItemName.find_first_of( "/" ) );
  181. if( aTestForRev && separation != -1 )
  182. {
  183. item_name = aLibItemName.substr( 0, separation-1 );
  184. return separation;
  185. }
  186. else
  187. {
  188. item_name = aLibItemName;
  189. }
  190. return -1;
  191. }
  192. int LIB_ID::SetRevision( const UTF8& aRevision )
  193. {
  194. int offset = okRevision( aRevision );
  195. if( offset == -1 )
  196. {
  197. revision = aRevision;
  198. }
  199. return offset;
  200. }
  201. UTF8 LIB_ID::Format() const
  202. {
  203. UTF8 ret;
  204. if( nickname.size() )
  205. {
  206. ret += nickname;
  207. ret += ':';
  208. }
  209. ret += item_name;
  210. if( revision.size() )
  211. {
  212. ret += '/';
  213. ret += revision;
  214. }
  215. return ret;
  216. }
  217. UTF8 LIB_ID::GetLibItemNameAndRev() const
  218. {
  219. UTF8 ret;
  220. if( revision.size() )
  221. {
  222. ret += '/';
  223. ret += revision;
  224. }
  225. return ret;
  226. }
  227. UTF8 LIB_ID::Format( const UTF8& aLogicalLib, const UTF8& aLibItemName, const UTF8& aRevision )
  228. {
  229. UTF8 ret;
  230. int offset;
  231. if( aLogicalLib.size() )
  232. {
  233. offset = okLogical( aLogicalLib );
  234. if( offset != -1 )
  235. {
  236. THROW_PARSE_ERROR( _( "Illegal character found in logical library name" ),
  237. wxString::FromUTF8( aLogicalLib.c_str() ),
  238. aLogicalLib.c_str(), 0, offset );
  239. }
  240. ret += aLogicalLib;
  241. ret += ':';
  242. }
  243. ret += aLibItemName; // TODO: Add validity test.
  244. if( aRevision.size() )
  245. {
  246. offset = okRevision( aRevision );
  247. if( offset != -1 )
  248. {
  249. THROW_PARSE_ERROR( _( "Illegal character found in revision" ),
  250. wxString::FromUTF8( aRevision.c_str() ),
  251. aRevision.c_str(),
  252. 0,
  253. offset );
  254. }
  255. ret += '/';
  256. ret += aRevision;
  257. }
  258. return ret;
  259. }
  260. int LIB_ID::compare( const LIB_ID& aLibId ) const
  261. {
  262. // Don't bother comparing the same object.
  263. if( this == &aLibId )
  264. return 0;
  265. int retv = nickname.compare( aLibId.nickname );
  266. if( retv != 0 )
  267. return retv;
  268. retv = item_name.compare( aLibId.item_name );
  269. if( retv != 0 )
  270. return retv;
  271. return revision.compare( aLibId.revision );
  272. }
  273. #if 0 && defined(DEBUG)
  274. // build this with Debug CMAKE_BUILD_TYPE
  275. void LIB_ID::Test()
  276. {
  277. static const char* lpids[] = {
  278. "smt:R_0805/rev0",
  279. "mysmt:R_0805/rev2",
  280. "device:AXIAL-0500",
  281. };
  282. for( unsigned i=0; i<sizeof(lpids)/sizeof(lpids[0]); ++i )
  283. {
  284. // test some round tripping
  285. LIB_ID lpid( lpids[i] ); // parse
  286. // format
  287. printf( "input:'%s' full:'%s' nickname: %s item_name:'%s' rev:'%s'\n",
  288. lpids[i],
  289. lpid.Format().c_str(),
  290. lpid.GetLibNickname().c_str(),
  291. lpid.GetLibItemName().c_str(),
  292. lpid.GetRevision().c_str() );
  293. }
  294. }
  295. int main( int argc, char** argv )
  296. {
  297. LIB_ID::Test();
  298. return 0;
  299. }
  300. #endif