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.

448 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-2017 Wayne Stambaugh <stambaughw@gmail.com>
  6. * Copyright (C) 2012-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 <wx/filename.h>
  26. #include <wx/uri.h>
  27. #include <set>
  28. #include <kiface_i.h>
  29. #include <lib_table_base.h>
  30. #include <lib_table_lexer.h>
  31. #include <settings/app_settings.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:
  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 );
  123. if( row == nullptr )
  124. return false;
  125. if( aCheckEnabled && !row->GetIsEnabled() )
  126. return false;
  127. return true;
  128. }
  129. wxString LIB_TABLE::GetFullURI( const wxString& aNickname, bool aExpandEnvVars ) const
  130. {
  131. const LIB_TABLE_ROW* row = findRow( aNickname );
  132. wxString retv;
  133. if( row )
  134. retv = row->GetFullURI( aExpandEnvVars );
  135. return retv;
  136. }
  137. LIB_TABLE_ROW* LIB_TABLE::findRow( const wxString& aNickName ) const
  138. {
  139. LIB_TABLE* cur = (LIB_TABLE*) this;
  140. do
  141. {
  142. cur->ensureIndex();
  143. INDEX_CITER it = cur->nickIndex.find( aNickName );
  144. if( it != cur->nickIndex.end() )
  145. {
  146. return &cur->rows[it->second]; // found
  147. }
  148. // not found, search fall back table(s), if any
  149. } while( ( cur = cur->fallBack ) != 0 );
  150. return nullptr; // not found
  151. }
  152. LIB_TABLE_ROW* LIB_TABLE::findRow( const wxString& aNickName )
  153. {
  154. LIB_TABLE* cur = (LIB_TABLE*) this;
  155. do
  156. {
  157. cur->ensureIndex();
  158. INDEX_ITER it = cur->nickIndex.find( aNickName );
  159. if( it != cur->nickIndex.end() )
  160. {
  161. return &cur->rows[it->second]; // found
  162. }
  163. // not found, search fall back table(s), if any
  164. } while( ( cur = cur->fallBack ) != 0 );
  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 ) != 0 );
  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
  198. // quietly reject any duplicates, which can happen when encountering a duplicate
  199. // nickname from one of the fall back table(s).
  200. std::set< wxString > unique;
  201. std::vector< wxString > ret;
  202. const LIB_TABLE* cur = this;
  203. do
  204. {
  205. for( LIB_TABLE_ROWS_CITER it = cur->rows.begin(); it!=cur->rows.end(); ++it )
  206. {
  207. if( it->GetIsEnabled() )
  208. {
  209. unique.insert( it->GetNickName() );
  210. }
  211. }
  212. } while( ( cur = cur->fallBack ) != 0 );
  213. ret.reserve( unique.size() );
  214. // return a sorted, unique set of nicknames in a std::vector<wxString> to caller
  215. for( std::set< wxString >::const_iterator it = unique.begin(); it!=unique.end(); ++it )
  216. {
  217. ret.push_back( *it );
  218. }
  219. // We want to allow case-sensitive duplicates but sort by case-insensitive ordering
  220. std::sort( ret.begin(), ret.end(), []( const wxString& lhs, const wxString& rhs )
  221. {
  222. return lhs.CmpNoCase( rhs ) < 0;
  223. } );
  224. return ret;
  225. }
  226. bool LIB_TABLE::InsertRow( LIB_TABLE_ROW* aRow, bool doReplace )
  227. {
  228. ensureIndex();
  229. INDEX_CITER it = nickIndex.find( aRow->GetNickName() );
  230. if( it == nickIndex.end() )
  231. {
  232. rows.push_back( aRow );
  233. nickIndex.insert( INDEX_VALUE( aRow->GetNickName(), rows.size() - 1 ) );
  234. return true;
  235. }
  236. if( doReplace )
  237. {
  238. rows.replace( it->second, aRow );
  239. return true;
  240. }
  241. return false;
  242. }
  243. void LIB_TABLE::Load( const wxString& aFileName )
  244. {
  245. // It's OK if footprint library tables are missing.
  246. if( wxFileName::IsFileReadable( aFileName ) )
  247. {
  248. FILE_LINE_READER reader( aFileName );
  249. LIB_TABLE_LEXER lexer( &reader );
  250. Parse( &lexer );
  251. }
  252. }
  253. void LIB_TABLE::Save( const wxString& aFileName ) const
  254. {
  255. FILE_OUTPUTFORMATTER sf( aFileName );
  256. Format( &sf, 0 );
  257. }
  258. PROPERTIES* LIB_TABLE::ParseOptions( const std::string& aOptionsList )
  259. {
  260. if( aOptionsList.size() )
  261. {
  262. const char* cp = &aOptionsList[0];
  263. const char* end = cp + aOptionsList.size();
  264. PROPERTIES props;
  265. std::string pair;
  266. // Parse all name=value pairs
  267. while( cp < end )
  268. {
  269. pair.clear();
  270. // Skip leading white space.
  271. while( cp < end && isspace( *cp ) )
  272. ++cp;
  273. // Find the end of pair/field
  274. while( cp < end )
  275. {
  276. if( *cp == '\\' && cp + 1 < end && cp[1] == OPT_SEP )
  277. {
  278. ++cp; // skip the escape
  279. pair += *cp++; // add the separator
  280. }
  281. else if( *cp == OPT_SEP )
  282. {
  283. ++cp; // skip the separator
  284. break; // process the pair
  285. }
  286. else
  287. pair += *cp++;
  288. }
  289. // stash the pair
  290. if( pair.size() )
  291. {
  292. // first equals sign separates 'name' and 'value'.
  293. size_t eqNdx = pair.find( '=' );
  294. if( eqNdx != pair.npos )
  295. {
  296. std::string name = pair.substr( 0, eqNdx );
  297. std::string value = pair.substr( eqNdx + 1 );
  298. props[name] = value;
  299. }
  300. else
  301. props[pair] = ""; // property is present, but with no value.
  302. }
  303. }
  304. if( props.size() )
  305. return new PROPERTIES( props );
  306. }
  307. return nullptr;
  308. }
  309. UTF8 LIB_TABLE::FormatOptions( const PROPERTIES* aProperties )
  310. {
  311. UTF8 ret;
  312. if( aProperties )
  313. {
  314. for( PROPERTIES::const_iterator it = aProperties->begin(); it != aProperties->end(); ++it )
  315. {
  316. const std::string& name = it->first;
  317. const UTF8& value = it->second;
  318. if( ret.size() )
  319. ret += OPT_SEP;
  320. ret += name;
  321. // the separation between name and value is '='
  322. if( value.size() )
  323. {
  324. ret += '=';
  325. for( std::string::const_iterator si = value.begin(); si != value.end(); ++si )
  326. {
  327. // escape any separator in the value.
  328. if( *si == OPT_SEP )
  329. ret += '\\';
  330. ret += *si;
  331. }
  332. }
  333. }
  334. }
  335. return ret;
  336. }