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.

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