KiCad PCB EDA Suite
lib_table_base.cpp
Go to the documentation of this file.
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 
26 
27 #include <wx/filename.h>
28 #include <wx/uri.h>
29 
30 #include <set>
31 
32 #include <common.h>
33 #include <fctsys.h>
34 #include <kiface_i.h>
35 #include <lib_table_base.h>
36 #include <lib_table_lexer.h>
37 #include <macros.h>
38 #include <settings/app_settings.h>
39 
40 
41 #define OPT_SEP '|'
42 
43 
44 using namespace LIB_TABLE_T;
45 
46 
48 {
49  return aRow.clone();
50 }
51 
52 
54 {
55  properties.reset( aProperties );
56 }
57 
58 
59 void LIB_TABLE_ROW::SetFullURI( const wxString& aFullURI )
60 {
61  uri_user = aFullURI;
62 
63 #if !FP_LATE_ENVVAR
64  uri_expanded = FP_LIB_TABLE::ExpandSubstitutions( aFullURI );
65 #endif
66 }
67 
68 
69 const wxString LIB_TABLE_ROW::GetFullURI( bool aSubstituted ) const
70 {
71  if( aSubstituted )
72  {
73 #if !FP_LATE_ENVVAR // early expansion
74  return uri_expanded;
75 
76 #else // late expansion
77  return ExpandEnvVarSubstitutions( uri_user, nullptr );
78 #endif
79  }
80 
81  return uri_user;
82 }
83 
84 
85 void LIB_TABLE_ROW::Format( OUTPUTFORMATTER* out, int nestLevel ) const
86 {
87  // In Kicad, we save path and file names using the Unix notation (separator = '/')
88  // So ensure separator is always '/' is saved URI string
89  wxString uri = GetFullURI();
90  uri.Replace( '\\', '/' );
91 
92  wxString extraOptions;
93 
94  if( !GetIsEnabled() )
95  {
96  extraOptions += "(disabled)";
97  }
98 
99  out->Print( nestLevel, "(lib (name %s)(type %s)(uri %s)(options %s)(descr %s)%s)\n",
100  out->Quotew( GetNickName() ).c_str(),
101  out->Quotew( GetType() ).c_str(),
102  out->Quotew( uri ).c_str(),
103  out->Quotew( GetOptions() ).c_str(),
104  out->Quotew( GetDescr() ).c_str(),
105  extraOptions.ToStdString().c_str()
106  );
107 }
108 
109 
111 {
112  return nickName == r.nickName
113  && uri_user == r.uri_user
114  && options == r.options
115  && description == r.description
116  && enabled == r.enabled;
117 }
118 
119 
120 void LIB_TABLE_ROW::SetOptions( const wxString& aOptions )
121 {
122  options = aOptions;
123 
124  // set PROPERTIES* from options
125  setProperties( LIB_TABLE::ParseOptions( TO_UTF8( aOptions ) ) );
126 }
127 
128 
129 LIB_TABLE::LIB_TABLE( LIB_TABLE* aFallBackTable ) :
130  fallBack( aFallBackTable )
131 {
132  // not copying fall back, simply search aFallBackTable separately
133  // if "nickName not found".
134 }
135 
136 
138 {
139  // *fallBack is not owned here.
140 }
141 
142 
143 bool LIB_TABLE::IsEmpty( bool aIncludeFallback )
144 {
145  if( !aIncludeFallback || !fallBack )
146  return rows.empty();
147 
148  return rows.empty() && fallBack->IsEmpty( true );
149 }
150 
151 
152 const wxString LIB_TABLE::GetDescription( const wxString& aNickname )
153 {
154  // use "no exception" form of find row:
155  const LIB_TABLE_ROW* row = findRow( aNickname );
156 
157  if( row )
158  return row->GetDescr();
159  else
160  return wxEmptyString;
161 }
162 
163 
164 bool LIB_TABLE::HasLibrary( const wxString& aNickname, bool aCheckEnabled ) const
165 {
166  const LIB_TABLE_ROW* row = findRow( aNickname );
167 
168  if( row == nullptr )
169  return false;
170 
171  if( aCheckEnabled && !row->GetIsEnabled() )
172  return false;
173 
174  return true;
175 }
176 
177 
178 wxString LIB_TABLE::GetFullURI( const wxString& aNickname, bool aExpandEnvVars ) const
179 {
180  const LIB_TABLE_ROW* row = findRow( aNickname );
181 
182  wxString retv;
183 
184  if( row )
185  retv = row->GetFullURI( aExpandEnvVars );
186 
187  return retv;
188 }
189 
190 
191 LIB_TABLE_ROW* LIB_TABLE::findRow( const wxString& aNickName ) const
192 {
193  LIB_TABLE* cur = (LIB_TABLE*) this;
194 
195  do
196  {
197  cur->ensureIndex();
198 
199  INDEX_CITER it = cur->nickIndex.find( aNickName );
200 
201  if( it != cur->nickIndex.end() )
202  {
203  return &cur->rows[it->second]; // found
204  }
205 
206  // not found, search fall back table(s), if any
207  } while( ( cur = cur->fallBack ) != 0 );
208 
209  return nullptr; // not found
210 }
211 
212 
213 LIB_TABLE_ROW* LIB_TABLE::findRow( const wxString& aNickName )
214 {
215  LIB_TABLE* cur = (LIB_TABLE*) this;
216 
217  do
218  {
219  cur->ensureIndex();
220 
221  INDEX_ITER it = cur->nickIndex.find( aNickName );
222 
223  if( it != cur->nickIndex.end() )
224  {
225  return &cur->rows[it->second]; // found
226  }
227 
228  // not found, search fall back table(s), if any
229  } while( ( cur = cur->fallBack ) != 0 );
230 
231  return nullptr; // not found
232 }
233 
234 
235 const LIB_TABLE_ROW* LIB_TABLE::FindRowByURI( const wxString& aURI )
236 {
237  LIB_TABLE* cur = this;
238 
239  do
240  {
241  cur->ensureIndex();
242 
243  for( unsigned i = 0; i < cur->rows.size(); i++ )
244  {
245  wxString tmp = cur->rows[i].GetFullURI( true );
246 
247  if( tmp.Find( "://" ) != wxNOT_FOUND )
248  {
249  if( tmp == aURI )
250  return &cur->rows[i]; // found as URI
251  }
252  else
253  {
254  wxFileName fn = aURI;
255 
256  // This will also test if the file is a symlink so if we are comparing
257  // a symlink to the same real file, the comparison will be true. See
258  // wxFileName::SameAs() in the wxWidgets source.
259  if( fn == wxFileName( tmp ) )
260  return &cur->rows[i]; // found as full path and file name
261  }
262  }
263 
264  // not found, search fall back table(s), if any
265  } while( ( cur = cur->fallBack ) != 0 );
266 
267  return nullptr; // not found
268 }
269 
270 
271 std::vector<wxString> LIB_TABLE::GetLogicalLibs()
272 {
273  // Only return unique logical library names. Use std::set::insert() to
274  // quietly reject any duplicates, which can happen when encountering a duplicate
275  // nickname from one of the fall back table(s).
276 
277  std::set< wxString > unique;
278  std::vector< wxString > ret;
279  const LIB_TABLE* cur = this;
280 
281  do
282  {
283  for( LIB_TABLE_ROWS_CITER it = cur->rows.begin(); it!=cur->rows.end(); ++it )
284  {
285  if( it->GetIsEnabled() )
286  {
287  unique.insert( it->GetNickName() );
288  }
289  }
290 
291  } while( ( cur = cur->fallBack ) != 0 );
292 
293  ret.reserve( unique.size() );
294 
295  // return a sorted, unique set of nicknames in a std::vector<wxString> to caller
296  for( std::set< wxString >::const_iterator it = unique.begin(); it!=unique.end(); ++it )
297  {
298  ret.push_back( *it );
299  }
300 
301  // We want to allow case-sensitive duplicates but sort by case-insensitive ordering
302  std::sort( ret.begin(), ret.end(), []( const wxString& lhs, const wxString& rhs )
303  {
304  return lhs.CmpNoCase( rhs ) < 0;
305  } );
306 
307  return ret;
308 }
309 
310 
311 bool LIB_TABLE::InsertRow( LIB_TABLE_ROW* aRow, bool doReplace )
312 {
313  ensureIndex();
314 
315  INDEX_CITER it = nickIndex.find( aRow->GetNickName() );
316 
317  if( it == nickIndex.end() )
318  {
319  rows.push_back( aRow );
320  nickIndex.insert( INDEX_VALUE( aRow->GetNickName(), rows.size() - 1 ) );
321  return true;
322  }
323 
324  if( doReplace )
325  {
326  rows.replace( it->second, aRow );
327  return true;
328  }
329 
330  return false;
331 }
332 
333 
334 void LIB_TABLE::Load( const wxString& aFileName )
335 
336 {
337  // It's OK if footprint library tables are missing.
338  if( wxFileName::IsFileReadable( aFileName ) )
339  {
340  FILE_LINE_READER reader( aFileName );
341  LIB_TABLE_LEXER lexer( &reader );
342 
343  Parse( &lexer );
344  }
345 }
346 
347 
348 void LIB_TABLE::Save( const wxString& aFileName ) const
349 {
350  FILE_OUTPUTFORMATTER sf( aFileName );
351  Format( &sf, 0 );
352 }
353 
354 
355 PROPERTIES* LIB_TABLE::ParseOptions( const std::string& aOptionsList )
356 {
357  if( aOptionsList.size() )
358  {
359  const char* cp = &aOptionsList[0];
360  const char* end = cp + aOptionsList.size();
361 
362  PROPERTIES props;
363  std::string pair;
364 
365  // Parse all name=value pairs
366  while( cp < end )
367  {
368  pair.clear();
369 
370  // Skip leading white space.
371  while( cp < end && isspace( *cp ) )
372  ++cp;
373 
374  // Find the end of pair/field
375  while( cp < end )
376  {
377  if( *cp == '\\' && cp + 1 < end && cp[1] == OPT_SEP )
378  {
379  ++cp; // skip the escape
380  pair += *cp++; // add the separator
381  }
382  else if( *cp == OPT_SEP )
383  {
384  ++cp; // skip the separator
385  break; // process the pair
386  }
387  else
388  pair += *cp++;
389  }
390 
391  // stash the pair
392  if( pair.size() )
393  {
394  // first equals sign separates 'name' and 'value'.
395  size_t eqNdx = pair.find( '=' );
396 
397  if( eqNdx != pair.npos )
398  {
399  std::string name = pair.substr( 0, eqNdx );
400  std::string value = pair.substr( eqNdx + 1 );
401  props[name] = value;
402  }
403  else
404  props[pair] = ""; // property is present, but with no value.
405  }
406  }
407 
408  if( props.size() )
409  return new PROPERTIES( props );
410  }
411 
412  return nullptr;
413 }
414 
415 
417 {
418  UTF8 ret;
419 
420  if( aProperties )
421  {
422  for( PROPERTIES::const_iterator it = aProperties->begin(); it != aProperties->end(); ++it )
423  {
424  const std::string& name = it->first;
425 
426  const UTF8& value = it->second;
427 
428  if( ret.size() )
429  ret += OPT_SEP;
430 
431  ret += name;
432 
433  // the separation between name and value is '='
434  if( value.size() )
435  {
436  ret += '=';
437 
438  for( std::string::const_iterator si = value.begin(); si != value.end(); ++si )
439  {
440  // escape any separator in the value.
441  if( *si == OPT_SEP )
442  ret += '\\';
443 
444  ret += *si;
445  }
446  }
447  }
448  }
449 
450  return ret;
451 }
UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to...
Definition: utf8.h:73
void ensureIndex()
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
std::vector< PROPERTY > PROPERTIES
Definition: specctra.h:184
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
bool enabled
Whether the LIB_TABLE_ROW is enabled.
INDEX::const_iterator INDEX_CITER
void setProperties(PROPERTIES *aProperties)
LIB_TABLE(LIB_TABLE *aFallBackTable=nullptr)
Build a library table by pre-pending this table fragment in front of aFallBackTable.
OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a convenient ...
Definition: richio.h:327
LIB_TABLE_ROWS rows
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:574
PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
bool operator==(const LIB_TABLE_ROW &r) const
LIB_TABLE * fallBack
static UTF8 FormatOptions(const PROPERTIES *aProperties)
Returns a list of options from the aProperties parameter.
This file contains miscellaneous commonly used macros and functions.
INDEX::value_type INDEX_VALUE
const wxString & GetDescr() const
Return the description of the library referenced by this row.
virtual ~LIB_TABLE()
wxString description
const wxString GetFullURI(bool aSubstituted=false) const
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
const wxString & GetNickName() const
static PROPERTIES * ParseOptions(const std::string &aOptionsList)
Parses aOptionsList and places the result into a PROPERTIES object which is returned.
FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
LIB_TABLE_ROW * clone() const
bool GetIsEnabled() const
LIB_TABLE_ROW * new_clone(const LIB_TABLE_ROW &aRow)
Allows boost pointer containers to make clones of the data stored in them.
virtual void Parse(LIB_TABLE_LEXER *aLexer)=0
Parse the #LIB_TABLE_LEXER s-expression library table format into the appropriate LIB_TABLE_ROW objec...
const wxString GetDescription(const wxString &aNickname)
wxString GetFullURI(const wxString &aLibNickname, bool aExpandEnvVars=true) const
Return the full URI of the library mapped to aLibNickname.
const LIB_TABLE_ROW * FindRowByURI(const wxString &aURI)
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const =0
Generate the table in s-expression format to aOutput with an indention level of aIndentLevel.
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:472
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
void Format(OUTPUTFORMATTER *out, int nestLevel) const
Serialize this object as utf8 text to an OUTPUTFORMATTER, and tries to make it look good using multip...
void SetFullURI(const wxString &aFullURI)
Change the full URI for the library.
LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER
INDEX::iterator INDEX_ITER
const char * name
Definition: DXF_plotter.cpp:60
wxString options
LIB_TABLE_ROW * findRow(const wxString &aNickname) const
Return a LIB_TABLE_ROW if aNickname is found in this table or in any chained fallBack table fragment,...
std::string::const_iterator end() const
Definition: utf8.h:197
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
#define TO_UTF8(wxstring)
The common library.
FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
void SetOptions(const wxString &aOptions)
Change the library options strings.
wxString nickName
std::string::const_iterator begin() const
Definition: utf8.h:196
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404
std::string::size_type size() const
Definition: utf8.h:115
wxString uri_user
what user entered from UI or loaded from disk
INDEX nickIndex
this particular key is the nickName within each row.
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
#define OPT_SEP
options separator character
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.