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 <fctsys.h>
33 #include <common.h>
34 #include <macros.h>
35 #include <kiface_i.h>
36 #include <lib_table_lexer.h>
37 #include <lib_table_base.h>
38 
39 
40 #define OPT_SEP '|'
41 
42 
43 using namespace LIB_TABLE_T;
44 
45 
47 {
48  return aRow.clone();
49 }
50 
51 
53 {
54  properties.reset( aProperties );
55 }
56 
57 
58 void LIB_TABLE_ROW::SetFullURI( const wxString& aFullURI )
59 {
60  uri_user = aFullURI;
61 
62 #if !FP_LATE_ENVVAR
63  uri_expanded = FP_LIB_TABLE::ExpandSubstitutions( aFullURI );
64 #endif
65 }
66 
67 
68 const wxString LIB_TABLE_ROW::GetFullURI( bool aSubstituted ) const
69 {
70  if( aSubstituted )
71  {
72 #if !FP_LATE_ENVVAR // early expansion
73  return uri_expanded;
74 
75 #else // late expansion
76  return LIB_TABLE::ExpandSubstitutions( uri_user );
77 #endif
78  }
79 
80  return uri_user;
81 }
82 
83 
84 void LIB_TABLE_ROW::Format( OUTPUTFORMATTER* out, int nestLevel ) const
85 {
86  // In Kicad, we save path and file names using the Unix notation (separator = '/')
87  // So ensure separator is always '/' is saved URI string
88  wxString uri = GetFullURI();
89  uri.Replace( '\\', '/' );
90 
91  wxString extraOptions;
92 
93  if( !GetIsEnabled() )
94  {
95  extraOptions += "(disabled)";
96  }
97 
98  out->Print( nestLevel, "(lib (name %s)(type %s)(uri %s)(options %s)(descr %s)%s)\n",
99  out->Quotew( GetNickName() ).c_str(),
100  out->Quotew( GetType() ).c_str(),
101  out->Quotew( uri ).c_str(),
102  out->Quotew( GetOptions() ).c_str(),
103  out->Quotew( GetDescr() ).c_str(),
104  extraOptions.ToStdString().c_str()
105  );
106 }
107 
108 
109 void LIB_TABLE_ROW::Parse( std::unique_ptr< LIB_TABLE_ROW >& aRow, LIB_TABLE_LEXER* in )
110 {
111  /*
112  * (lib (name NICKNAME)(descr DESCRIPTION)(type TYPE)(full_uri FULL_URI)(options OPTIONS))
113  *
114  * Elements after (name) are order independent.
115  */
116 
117  T tok = in->NextTok();
118 
119  if( tok != T_lib )
120  in->Expecting( T_lib );
121 
122  // (name NICKNAME)
123  in->NeedLEFT();
124 
125  if( ( tok = in->NextTok() ) != T_name )
126  in->Expecting( T_name );
127 
128  in->NeedSYMBOLorNUMBER();
129 
130  aRow->SetNickName( in->FromUTF8() );
131 
132  in->NeedRIGHT();
133 
134  // After (name), remaining (lib) elements are order independent, and in
135  // some cases optional.
136  bool sawType = false;
137  bool sawOpts = false;
138  bool sawDesc = false;
139  bool sawUri = false;
140 
141  while( ( tok = in->NextTok() ) != T_RIGHT )
142  {
143  if( tok == T_EOF )
144  in->Unexpected( T_EOF );
145 
146  if( tok != T_LEFT )
147  in->Expecting( T_LEFT );
148 
149  tok = in->NeedSYMBOLorNUMBER();
150 
151  switch( tok )
152  {
153  case T_uri:
154  if( sawUri )
155  in->Duplicate( tok );
156  sawUri = true;
157  in->NeedSYMBOLorNUMBER();
158  // Saved path and file names use the Unix notation (separator = '/')
159  // However old files, and files edited by hands can use the woindows
160  // separator. Force the unix notation
161  // (It works on windows, and moreover, wxFileName and wxDir takes care to that
162  // on windows)
163  // moreover, URLs use the '/' as separator
164  {
165  wxString uri = in->FromUTF8();
166  uri.Replace( '\\', '/' );
167  aRow->SetFullURI( uri );
168  }
169  break;
170 
171  case T_type:
172  if( sawType )
173  in->Duplicate( tok );
174  sawType = true;
175  in->NeedSYMBOLorNUMBER();
176  aRow->SetType( in->FromUTF8() );
177  break;
178 
179  case T_options:
180  if( sawOpts )
181  in->Duplicate( tok );
182  sawOpts = true;
183  in->NeedSYMBOLorNUMBER();
184  aRow->SetOptions( in->FromUTF8() );
185  break;
186 
187  case T_descr:
188  if( sawDesc )
189  in->Duplicate( tok );
190  sawDesc = true;
191  in->NeedSYMBOLorNUMBER();
192  aRow->SetDescr( in->FromUTF8() );
193  break;
194 
195  default:
196  in->Unexpected( tok );
197  }
198 
199  in->NeedRIGHT();
200  }
201 
202  if( !sawType )
203  in->Expecting( T_type );
204 
205  if( !sawUri )
206  in->Expecting( T_uri );
207 }
208 
209 
211 {
212  return nickName == r.nickName
213  && uri_user == r.uri_user
214  && options == r.options
215  && description == r.description
216  && enabled == r.enabled;
217 }
218 
219 
220 void LIB_TABLE_ROW::SetOptions( const wxString& aOptions )
221 {
222  options = aOptions;
223 
224  // set PROPERTIES* from options
225  setProperties( LIB_TABLE::ParseOptions( TO_UTF8( aOptions ) ) );
226 }
227 
228 
229 LIB_TABLE::LIB_TABLE( LIB_TABLE* aFallBackTable ) :
230  fallBack( aFallBackTable )
231 {
232  // not copying fall back, simply search aFallBackTable separately
233  // if "nickName not found".
234 }
235 
236 
238 {
239  // *fallBack is not owned here.
240 }
241 
242 
243 bool LIB_TABLE::IsEmpty( bool aIncludeFallback )
244 {
245  if( !aIncludeFallback || !fallBack )
246  return rows.empty();
247 
248  return rows.empty() && fallBack->IsEmpty( true );
249 }
250 
251 
252 const wxString LIB_TABLE::GetDescription( const wxString& aNickname )
253 {
254  // use "no exception" form of find row:
255  const LIB_TABLE_ROW* row = findRow( aNickname );
256 
257  if( row )
258  return row->GetDescr();
259  else
260  return wxEmptyString;
261 }
262 
263 
264 bool LIB_TABLE::HasLibrary( const wxString& aNickname ) const
265 {
266  const LIB_TABLE_ROW* row = findRow( aNickname );
267 
268  return row != nullptr;
269 }
270 
271 
272 wxString LIB_TABLE::GetFullURI( const wxString& aNickname, bool aExpandEnvVars ) const
273 {
274  const LIB_TABLE_ROW* row = findRow( aNickname );
275 
276  wxString retv;
277 
278  if( row )
279  retv = row->GetFullURI( aExpandEnvVars );
280 
281  return retv;
282 }
283 
284 
285 LIB_TABLE_ROW* LIB_TABLE::findRow( const wxString& aNickName ) const
286 {
287  LIB_TABLE* cur = (LIB_TABLE*) this;
288 
289  do
290  {
291  cur->ensureIndex();
292 
293  INDEX_CITER it = cur->nickIndex.find( aNickName );
294 
295  if( it != cur->nickIndex.end() )
296  {
297  return &cur->rows[it->second]; // found
298  }
299 
300  // not found, search fall back table(s), if any
301  } while( ( cur = cur->fallBack ) != 0 );
302 
303  return NULL; // not found
304 }
305 
306 
307 LIB_TABLE_ROW* LIB_TABLE::findRow( const wxString& aNickName )
308 {
309  LIB_TABLE* cur = (LIB_TABLE*) this;
310 
311  do
312  {
313  cur->ensureIndex();
314 
315  INDEX_ITER it = cur->nickIndex.find( aNickName );
316 
317  if( it != cur->nickIndex.end() )
318  {
319  return &cur->rows[it->second]; // found
320  }
321 
322  // not found, search fall back table(s), if any
323  } while( ( cur = cur->fallBack ) != 0 );
324 
325  return NULL; // not found
326 }
327 
328 
329 const LIB_TABLE_ROW* LIB_TABLE::FindRowByURI( const wxString& aURI )
330 {
331  LIB_TABLE* cur = this;
332 
333  do
334  {
335  cur->ensureIndex();
336 
337  for( unsigned i = 0; i < cur->rows.size(); i++ )
338  {
339  wxString tmp = cur->rows[i].GetFullURI( true );
340 
341  if( tmp.Find( "://" ) != wxNOT_FOUND )
342  {
343  if( tmp == aURI )
344  return &cur->rows[i]; // found as URI
345  }
346  else
347  {
348  wxFileName fn = aURI;
349 
350  // This will also test if the file is a symlink so if we are comparing
351  // a symlink to the same real file, the comparison will be true. See
352  // wxFileName::SameAs() in the wxWidgets source.
353  if( fn == wxFileName( tmp ) )
354  return &cur->rows[i]; // found as full path and file name
355  }
356  }
357 
358  // not found, search fall back table(s), if any
359  } while( ( cur = cur->fallBack ) != 0 );
360 
361  return NULL; // not found
362 }
363 
364 
365 std::vector<wxString> LIB_TABLE::GetLogicalLibs()
366 {
367  // Only return unique logical library names. Use std::set::insert() to
368  // quietly reject any duplicates, which can happen when encountering a duplicate
369  // nickname from one of the fall back table(s).
370 
371  std::set< wxString > unique;
372  std::vector< wxString > ret;
373  const LIB_TABLE* cur = this;
374 
375  do
376  {
377  for( LIB_TABLE_ROWS_CITER it = cur->rows.begin(); it!=cur->rows.end(); ++it )
378  {
379  if( it->GetIsEnabled() )
380  {
381  unique.insert( it->GetNickName() );
382  }
383  }
384 
385  } while( ( cur = cur->fallBack ) != 0 );
386 
387  ret.reserve( unique.size() );
388 
389  // return a sorted, unique set of nicknames in a std::vector<wxString> to caller
390  for( std::set< wxString >::const_iterator it = unique.begin(); it!=unique.end(); ++it )
391  {
392  ret.push_back( *it );
393  }
394 
395  return ret;
396 }
397 
398 
399 bool LIB_TABLE::InsertRow( LIB_TABLE_ROW* aRow, bool doReplace )
400 {
401  ensureIndex();
402 
403  INDEX_CITER it = nickIndex.find( aRow->GetNickName() );
404 
405  if( it == nickIndex.end() )
406  {
407  rows.push_back( aRow );
408  nickIndex.insert( INDEX_VALUE( aRow->GetNickName(), rows.size() - 1 ) );
409  return true;
410  }
411 
412  if( doReplace )
413  {
414  rows.replace( it->second, aRow );
415  return true;
416  }
417 
418  return false;
419 }
420 
421 
422 void LIB_TABLE::Load( const wxString& aFileName )
423 
424 {
425  // It's OK if footprint library tables are missing.
426  if( wxFileName::IsFileReadable( aFileName ) )
427  {
428  FILE_LINE_READER reader( aFileName );
429  LIB_TABLE_LEXER lexer( &reader );
430 
431  Parse( &lexer );
432  }
433 }
434 
435 
436 void LIB_TABLE::Save( const wxString& aFileName ) const
437 {
438  FILE_OUTPUTFORMATTER sf( aFileName );
439  Format( &sf, 0 );
440 }
441 
442 
443 size_t LIB_TABLE::GetEnvVars( wxArrayString& aEnvVars ) const
444 {
445  const LIB_TABLE* cur = this;
446 
447  do
448  {
449  for( unsigned i = 0; i < cur->rows.size(); i++ )
450  {
451  wxString uri = cur->rows[i].GetFullURI( false );
452 
453  int start = uri.Find( "${" );
454 
455  if( start == wxNOT_FOUND )
456  continue;
457 
458  int end = uri.Find( '}' );
459 
460  if( end == wxNOT_FOUND || end < start+2 )
461  continue;
462 
463  wxString envVar = uri.Mid( start+2, end - (start+2) );
464 
465  if( aEnvVars.Index( envVar, false ) == wxNOT_FOUND )
466  aEnvVars.Add( envVar );
467  }
468 
469  // not found, search fall back table(s), if any
470  } while( ( cur = cur->fallBack ) != 0 );
471 
472  return aEnvVars.GetCount();
473 }
474 
475 
476 PROPERTIES* LIB_TABLE::ParseOptions( const std::string& aOptionsList )
477 {
478  if( aOptionsList.size() )
479  {
480  const char* cp = &aOptionsList[0];
481  const char* end = cp + aOptionsList.size();
482 
483  PROPERTIES props;
484  std::string pair;
485 
486  // Parse all name=value pairs
487  while( cp < end )
488  {
489  pair.clear();
490 
491  // Skip leading white space.
492  while( cp < end && isspace( *cp ) )
493  ++cp;
494 
495  // Find the end of pair/field
496  while( cp < end )
497  {
498  if( *cp == '\\' && cp + 1 < end && cp[1] == OPT_SEP )
499  {
500  ++cp; // skip the escape
501  pair += *cp++; // add the separator
502  }
503  else if( *cp == OPT_SEP )
504  {
505  ++cp; // skip the separator
506  break; // process the pair
507  }
508  else
509  pair += *cp++;
510  }
511 
512  // stash the pair
513  if( pair.size() )
514  {
515  // first equals sign separates 'name' and 'value'.
516  size_t eqNdx = pair.find( '=' );
517 
518  if( eqNdx != pair.npos )
519  {
520  std::string name = pair.substr( 0, eqNdx );
521  std::string value = pair.substr( eqNdx + 1 );
522  props[name] = value;
523  }
524  else
525  props[pair] = ""; // property is present, but with no value.
526  }
527  }
528 
529  if( props.size() )
530  return new PROPERTIES( props );
531  }
532 
533  return NULL;
534 }
535 
536 
538 {
539  UTF8 ret;
540 
541  if( aProperties )
542  {
543  for( PROPERTIES::const_iterator it = aProperties->begin(); it != aProperties->end(); ++it )
544  {
545  const std::string& name = it->first;
546 
547  const UTF8& value = it->second;
548 
549  if( ret.size() )
550  ret += OPT_SEP;
551 
552  ret += name;
553 
554  // the separation between name and value is '='
555  if( value.size() )
556  {
557  ret += '=';
558 
559  for( std::string::const_iterator si = value.begin(); si != value.end(); ++si )
560  {
561  // escape any separator in the value.
562  if( *si == OPT_SEP )
563  ret += '\\';
564 
565  ret += *si;
566  }
567  }
568  }
569  }
570 
571  return ret;
572 }
573 
574 
575 const wxString LIB_TABLE::ExpandSubstitutions( const wxString& aString )
576 {
577  return ExpandEnvVarSubstitutions( aString );
578 }
Class UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion supp...
Definition: utf8.h:73
void ensureIndex()
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
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...
size_t GetEnvVars(wxArrayString &aEnvVars) const
Search the paths all of the LIB_TABLE_ROWS of the LIB_TABLE and add all of the environment variable s...
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
LIB_TABLE_ROW * clone() const
T
enum T contains all this lexer's tokens.
static const wxString ExpandSubstitutions(const wxString &aString)
Replaces any environment variable references with their values and is here to fully embellish the TAB...
bool enabled
Whether the LIB_TABLE_ROW is enabled.
INDEX::const_iterator INDEX_CITER
void setProperties(PROPERTIES *aProperties)
void Unexpected(int aTok)
Function Unexpected throws an IO_ERROR exception with an input file specific error message...
Definition: dsnlexer.cpp:369
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
LIB_TABLE_ROWS rows
std::string::size_type size() const
Definition: utf8.h:115
wxString FromUTF8()
Function FromUTF8 returns the current token text as a wxString, assuming that the input byte stream i...
Definition: dsnlexer.h:498
Class PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
LIB_TABLE * fallBack
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...
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
virtual ~LIB_TABLE()
wxString description
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
void NeedLEFT()
Function NeedLEFT calls NextTok() and then verifies that the token read in is a DSN_LEFT.
Definition: dsnlexer.cpp:393
static PROPERTIES * ParseOptions(const std::string &aOptionsList)
Parses aOptionsList and places the result into a PROPERTIES object which is returned.
Class FILE_LINE_READER is a LINE_READER that reads from an open file.
Definition: richio.h:180
const wxString ExpandEnvVarSubstitutions(const wxString &aString)
Function ExpandEnvVarSubstitutions replaces any environment variable references with their values...
Definition: common.cpp:254
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 object...
LIB_TABLE_T::T NeedSYMBOLorNUMBER()
Function NeedSYMBOLorNUMBER calls NextTok() and then verifies that the token read in satisfies bool I...
const wxString GetDescription(const wxString &aNickname)
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.
bool operator==(const LIB_TABLE_ROW &r) const
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:482
std::string::const_iterator end() const
Definition: utf8.h:197
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...
void Expecting(int aTok)
Function Expecting throws an IO_ERROR exception with an input file specific error message...
Definition: dsnlexer.cpp:353
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
const wxString & GetDescr() const
Return the description of the library referenced by this row.
std::string::const_iterator begin() const
Definition: utf8.h:196
wxString GetFullURI(const wxString &aLibNickname, bool aExpandEnvVars=true) const
Return the full URI of the library mapped to aLibNickname.
const wxString & GetNickName() const
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
LIB_TABLE_T::T NextTok()
Function NextTok returns the next token found in the input file or T_EOF when reaching the end of fil...
wxString options
void Duplicate(int aTok)
Function Duplicate throws an IO_ERROR exception with a message saying specifically that aTok is a dup...
Definition: dsnlexer.cpp:377
The common library.
const char * name
static void Parse(std::unique_ptr< LIB_TABLE_ROW > &aRow, LIB_TABLE_LEXER *in)
Class FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
LIB_TABLE(LIB_TABLE *aFallBackTable=NULL)
Build a library table by pre-pending this table fragment in front of aFallBackTable.
bool HasLibrary(const wxString &aNickname) const
Test for the existence of aNickname in the library table.
void NeedRIGHT()
Function NeedRIGHT calls NextTok() and then verifies that the token read in is a DSN_RIGHT.
Definition: dsnlexer.cpp:401
Class LIB_TABLE_LEXER is an automatically generated class using the TokenList2DnsLexer.cmake technology, based on keywords provided by file: /home/kicad/workspace/kicad-doxygen/common/lib_table.keywords.
void SetOptions(const wxString &aOptions)
Change the library options strings.
wxString nickName
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404
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
C++ does not put enum values in separate namespaces unless the enum itself is in a separate namespace...
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.