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.

447 lines
12 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) 2012-2021 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 <wx/filename.h>
  26. #include <set>
  27. #include <common.h>
  28. #include <kiface_base.h>
  29. #include <lib_table_base.h>
  30. #include <lib_table_lexer.h>
  31. #include <macros.h>
  32. #define OPT_SEP '|' ///< options separator character
  33. using namespace LIB_TABLE_T;
  34. LIB_TABLE_ROW* new_clone( const LIB_TABLE_ROW& aRow )
  35. {
  36. return aRow.clone();
  37. }
  38. void LIB_TABLE_ROW::setProperties( PROPERTIES* aProperties )
  39. {
  40. properties.reset( aProperties );
  41. }
  42. void LIB_TABLE_ROW::SetFullURI( const wxString& aFullURI )
  43. {
  44. uri_user = aFullURI;
  45. #if !FP_LATE_ENVVAR
  46. uri_expanded = FP_LIB_TABLE::ExpandSubstitutions( aFullURI );
  47. #endif
  48. }
  49. const wxString LIB_TABLE_ROW::GetFullURI( bool aSubstituted ) const
  50. {
  51. if( aSubstituted )
  52. {
  53. #if !FP_LATE_ENVVAR // early expansion
  54. return uri_expanded;
  55. #else // late expansion
  56. return ExpandEnvVarSubstitutions( uri_user, nullptr );
  57. #endif
  58. }
  59. return uri_user;
  60. }
  61. void LIB_TABLE_ROW::Format( OUTPUTFORMATTER* out, int nestLevel ) const
  62. {
  63. // In Kicad, we save path and file names using the Unix notation (separator = '/')
  64. // So ensure separator is always '/' is saved URI string
  65. wxString uri = GetFullURI();
  66. uri.Replace( '\\', '/' );
  67. wxString extraOptions;
  68. if( !GetIsEnabled() )
  69. {
  70. extraOptions += "(disabled)";
  71. }
  72. out->Print( nestLevel, "(lib (name %s)(type %s)(uri %s)(options %s)(descr %s)%s)\n",
  73. out->Quotew( GetNickName() ).c_str(),
  74. out->Quotew( GetType() ).c_str(),
  75. out->Quotew( uri ).c_str(),
  76. out->Quotew( GetOptions() ).c_str(),
  77. out->Quotew( GetDescr() ).c_str(),
  78. extraOptions.ToStdString().c_str()
  79. );
  80. }
  81. bool LIB_TABLE_ROW::operator==( const LIB_TABLE_ROW& r ) const
  82. {
  83. return nickName == r.nickName
  84. && uri_user == r.uri_user
  85. && options == r.options
  86. && description == r.description
  87. && enabled == r.enabled;
  88. }
  89. void LIB_TABLE_ROW::SetOptions( const wxString& aOptions )
  90. {
  91. options = aOptions;
  92. // set PROPERTIES* from options
  93. setProperties( LIB_TABLE::ParseOptions( TO_UTF8( aOptions ) ) );
  94. }
  95. LIB_TABLE::LIB_TABLE( LIB_TABLE* aFallBackTable ) :
  96. fallBack( aFallBackTable )
  97. {
  98. // not copying fall back, simply search aFallBackTable separately
  99. // if "nickName not found".
  100. }
  101. LIB_TABLE::~LIB_TABLE()
  102. {
  103. // *fallBack is not owned here.
  104. }
  105. bool LIB_TABLE::IsEmpty( bool aIncludeFallback )
  106. {
  107. if( !aIncludeFallback || !fallBack )
  108. return rows.empty();
  109. return rows.empty() && fallBack->IsEmpty( true );
  110. }
  111. const wxString LIB_TABLE::GetDescription( const wxString& aNickname )
  112. {
  113. // Use "no exception" form of find row and ignore disabled flag.
  114. const LIB_TABLE_ROW* row = findRow( aNickname );
  115. if( row )
  116. return row->GetDescr();
  117. else
  118. return wxEmptyString;
  119. }
  120. bool LIB_TABLE::HasLibrary( const wxString& aNickname, bool aCheckEnabled ) const
  121. {
  122. const LIB_TABLE_ROW* row = findRow( aNickname, aCheckEnabled );
  123. if( row == nullptr )
  124. return false;
  125. return true;
  126. }
  127. wxString LIB_TABLE::GetFullURI( const wxString& aNickname, bool aExpandEnvVars ) const
  128. {
  129. const LIB_TABLE_ROW* row = findRow( aNickname, true );
  130. wxString retv;
  131. if( row )
  132. retv = row->GetFullURI( aExpandEnvVars );
  133. return retv;
  134. }
  135. LIB_TABLE_ROW* LIB_TABLE::findRow( const wxString& aNickName, bool aCheckIfEnabled ) const
  136. {
  137. LIB_TABLE_ROW* row = nullptr;
  138. LIB_TABLE* cur = (LIB_TABLE*) this;
  139. std::lock_guard<std::recursive_mutex> lock( m_nickIndexMutex );
  140. do
  141. {
  142. cur->ensureIndex();
  143. for( const std::pair<const wxString, int>& entry : cur->nickIndex )
  144. {
  145. if( entry.first == aNickName )
  146. {
  147. row = &cur->rows[entry.second];
  148. if( !aCheckIfEnabled || ( aCheckIfEnabled && row->GetIsEnabled() ) )
  149. return row;
  150. }
  151. }
  152. // Repeat, this time looking for names that were "fixed" by legacy versions because
  153. // the old eeschema file format didn't support spaces in tokens.
  154. for( const std::pair<const wxString, int>& entry : cur->nickIndex )
  155. {
  156. wxString legacyLibName = entry.first;
  157. legacyLibName.Replace( " ", "_" );
  158. if( legacyLibName == aNickName )
  159. {
  160. row = &cur->rows[entry.second];
  161. if( !aCheckIfEnabled || ( aCheckIfEnabled && row->GetIsEnabled() ) )
  162. return row;
  163. }
  164. }
  165. // not found, search fall back table(s), if any
  166. } while( ( cur = cur->fallBack ) != nullptr );
  167. return nullptr; // not found
  168. }
  169. const LIB_TABLE_ROW* LIB_TABLE::FindRowByURI( const wxString& aURI )
  170. {
  171. LIB_TABLE* cur = this;
  172. do
  173. {
  174. cur->ensureIndex();
  175. for( unsigned i = 0; i < cur->rows.size(); i++ )
  176. {
  177. wxString tmp = cur->rows[i].GetFullURI( true );
  178. if( tmp.Find( "://" ) != wxNOT_FOUND )
  179. {
  180. if( tmp == aURI )
  181. return &cur->rows[i]; // found as URI
  182. }
  183. else
  184. {
  185. wxFileName fn = aURI;
  186. // This will also test if the file is a symlink so if we are comparing
  187. // a symlink to the same real file, the comparison will be true. See
  188. // wxFileName::SameAs() in the wxWidgets source.
  189. if( fn == wxFileName( tmp ) )
  190. return &cur->rows[i]; // found as full path and file name
  191. }
  192. }
  193. // not found, search fall back table(s), if any
  194. } while( ( cur = cur->fallBack ) != nullptr );
  195. return nullptr; // not found
  196. }
  197. std::vector<wxString> LIB_TABLE::GetLogicalLibs()
  198. {
  199. // Only return unique logical library names. Use std::set::insert() to
  200. // quietly reject any duplicates, which can happen when encountering a duplicate
  201. // nickname from one of the fall back table(s).
  202. std::set< wxString > unique;
  203. std::vector< wxString > ret;
  204. const LIB_TABLE* cur = this;
  205. do
  206. {
  207. for( LIB_TABLE_ROWS_CITER it = cur->rows.begin(); it!=cur->rows.end(); ++it )
  208. {
  209. if( it->GetIsEnabled() )
  210. {
  211. unique.insert( it->GetNickName() );
  212. }
  213. }
  214. } while( ( cur = cur->fallBack ) != nullptr );
  215. ret.reserve( unique.size() );
  216. // return a sorted, unique set of nicknames in a std::vector<wxString> to caller
  217. for( std::set< wxString >::const_iterator it = unique.begin(); it!=unique.end(); ++it )
  218. {
  219. ret.push_back( *it );
  220. }
  221. // We want to allow case-sensitive duplicates but sort by case-insensitive ordering
  222. std::sort( ret.begin(), ret.end(),
  223. []( const wxString& lhs, const wxString& rhs )
  224. {
  225. return lhs.CmpNoCase( rhs ) < 0;
  226. } );
  227. return ret;
  228. }
  229. bool LIB_TABLE::InsertRow( LIB_TABLE_ROW* aRow, bool doReplace )
  230. {
  231. std::lock_guard<std::recursive_mutex> lock( m_nickIndexMutex );
  232. ensureIndex();
  233. INDEX_CITER it = nickIndex.find( aRow->GetNickName() );
  234. if( it == nickIndex.end() )
  235. {
  236. rows.push_back( aRow );
  237. nickIndex.insert( INDEX_VALUE( aRow->GetNickName(), rows.size() - 1 ) );
  238. return true;
  239. }
  240. if( doReplace )
  241. {
  242. rows.replace( it->second, aRow );
  243. return true;
  244. }
  245. return false;
  246. }
  247. void LIB_TABLE::Load( const wxString& aFileName )
  248. {
  249. // It's OK if footprint library tables are missing.
  250. if( wxFileName::IsFileReadable( aFileName ) )
  251. {
  252. FILE_LINE_READER reader( aFileName );
  253. LIB_TABLE_LEXER lexer( &reader );
  254. Parse( &lexer );
  255. }
  256. }
  257. void LIB_TABLE::Save( const wxString& aFileName ) const
  258. {
  259. FILE_OUTPUTFORMATTER sf( aFileName );
  260. Format( &sf, 0 );
  261. }
  262. PROPERTIES* LIB_TABLE::ParseOptions( const std::string& aOptionsList )
  263. {
  264. if( aOptionsList.size() )
  265. {
  266. const char* cp = &aOptionsList[0];
  267. const char* end = cp + aOptionsList.size();
  268. PROPERTIES props;
  269. std::string pair;
  270. // Parse all name=value pairs
  271. while( cp < end )
  272. {
  273. pair.clear();
  274. // Skip leading white space.
  275. while( cp < end && isspace( *cp ) )
  276. ++cp;
  277. // Find the end of pair/field
  278. while( cp < end )
  279. {
  280. if( *cp == '\\' && cp + 1 < end && cp[1] == OPT_SEP )
  281. {
  282. ++cp; // skip the escape
  283. pair += *cp++; // add the separator
  284. }
  285. else if( *cp == OPT_SEP )
  286. {
  287. ++cp; // skip the separator
  288. break; // process the pair
  289. }
  290. else
  291. pair += *cp++;
  292. }
  293. // stash the pair
  294. if( pair.size() )
  295. {
  296. // first equals sign separates 'name' and 'value'.
  297. size_t eqNdx = pair.find( '=' );
  298. if( eqNdx != pair.npos )
  299. {
  300. std::string name = pair.substr( 0, eqNdx );
  301. std::string value = pair.substr( eqNdx + 1 );
  302. props[name] = value;
  303. }
  304. else
  305. props[pair] = ""; // property is present, but with no value.
  306. }
  307. }
  308. if( props.size() )
  309. return new PROPERTIES( props );
  310. }
  311. return nullptr;
  312. }
  313. UTF8 LIB_TABLE::FormatOptions( const PROPERTIES* aProperties )
  314. {
  315. UTF8 ret;
  316. if( aProperties )
  317. {
  318. for( PROPERTIES::const_iterator it = aProperties->begin(); it != aProperties->end(); ++it )
  319. {
  320. const std::string& name = it->first;
  321. const UTF8& value = it->second;
  322. if( ret.size() )
  323. ret += OPT_SEP;
  324. ret += name;
  325. // the separation between name and value is '='
  326. if( value.size() )
  327. {
  328. ret += '=';
  329. for( std::string::const_iterator si = value.begin(); si != value.end(); ++si )
  330. {
  331. // escape any separator in the value.
  332. if( *si == OPT_SEP )
  333. ret += '\\';
  334. ret += *si;
  335. }
  336. }
  337. }
  338. }
  339. return ret;
  340. }