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 <kiface_i.h>
34 #include <lib_table_base.h>
35 #include <lib_table_lexer.h>
36 #include <macros.h>
37 #include <settings/app_settings.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 ExpandEnvVarSubstitutions( uri_user, nullptr );
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 
110 {
111  return nickName == r.nickName
112  && uri_user == r.uri_user
113  && options == r.options
114  && description == r.description
115  && enabled == r.enabled;
116 }
117 
118 
119 void LIB_TABLE_ROW::SetOptions( const wxString& aOptions )
120 {
121  options = aOptions;
122 
123  // set PROPERTIES* from options
124  setProperties( LIB_TABLE::ParseOptions( TO_UTF8( aOptions ) ) );
125 }
126 
127 
128 LIB_TABLE::LIB_TABLE( LIB_TABLE* aFallBackTable ) :
129  fallBack( aFallBackTable )
130 {
131  // not copying fall back, simply search aFallBackTable separately
132  // if "nickName not found".
133 }
134 
135 
137 {
138  // *fallBack is not owned here.
139 }
140 
141 
142 bool LIB_TABLE::IsEmpty( bool aIncludeFallback )
143 {
144  if( !aIncludeFallback || !fallBack )
145  return rows.empty();
146 
147  return rows.empty() && fallBack->IsEmpty( true );
148 }
149 
150 
151 const wxString LIB_TABLE::GetDescription( const wxString& aNickname )
152 {
153  // use "no exception" form of find row:
154  const LIB_TABLE_ROW* row = findRow( aNickname );
155 
156  if( row )
157  return row->GetDescr();
158  else
159  return wxEmptyString;
160 }
161 
162 
163 bool LIB_TABLE::HasLibrary( const wxString& aNickname, bool aCheckEnabled ) const
164 {
165  const LIB_TABLE_ROW* row = findRow( aNickname );
166 
167  if( row == nullptr )
168  return false;
169 
170  if( aCheckEnabled && !row->GetIsEnabled() )
171  return false;
172 
173  return true;
174 }
175 
176 
177 wxString LIB_TABLE::GetFullURI( const wxString& aNickname, bool aExpandEnvVars ) const
178 {
179  const LIB_TABLE_ROW* row = findRow( aNickname );
180 
181  wxString retv;
182 
183  if( row )
184  retv = row->GetFullURI( aExpandEnvVars );
185 
186  return retv;
187 }
188 
189 
190 LIB_TABLE_ROW* LIB_TABLE::findRow( const wxString& aNickName ) const
191 {
192  LIB_TABLE* cur = (LIB_TABLE*) this;
193 
194  do
195  {
196  cur->ensureIndex();
197 
198  INDEX_CITER it = cur->nickIndex.find( aNickName );
199 
200  if( it != cur->nickIndex.end() )
201  {
202  return &cur->rows[it->second]; // found
203  }
204 
205  // not found, search fall back table(s), if any
206  } while( ( cur = cur->fallBack ) != 0 );
207 
208  return nullptr; // not found
209 }
210 
211 
212 LIB_TABLE_ROW* LIB_TABLE::findRow( const wxString& aNickName )
213 {
214  LIB_TABLE* cur = (LIB_TABLE*) this;
215 
216  do
217  {
218  cur->ensureIndex();
219 
220  INDEX_ITER it = cur->nickIndex.find( aNickName );
221 
222  if( it != cur->nickIndex.end() )
223  {
224  return &cur->rows[it->second]; // found
225  }
226 
227  // not found, search fall back table(s), if any
228  } while( ( cur = cur->fallBack ) != 0 );
229 
230  return nullptr; // not found
231 }
232 
233 
234 const LIB_TABLE_ROW* LIB_TABLE::FindRowByURI( const wxString& aURI )
235 {
236  LIB_TABLE* cur = this;
237 
238  do
239  {
240  cur->ensureIndex();
241 
242  for( unsigned i = 0; i < cur->rows.size(); i++ )
243  {
244  wxString tmp = cur->rows[i].GetFullURI( true );
245 
246  if( tmp.Find( "://" ) != wxNOT_FOUND )
247  {
248  if( tmp == aURI )
249  return &cur->rows[i]; // found as URI
250  }
251  else
252  {
253  wxFileName fn = aURI;
254 
255  // This will also test if the file is a symlink so if we are comparing
256  // a symlink to the same real file, the comparison will be true. See
257  // wxFileName::SameAs() in the wxWidgets source.
258  if( fn == wxFileName( tmp ) )
259  return &cur->rows[i]; // found as full path and file name
260  }
261  }
262 
263  // not found, search fall back table(s), if any
264  } while( ( cur = cur->fallBack ) != 0 );
265 
266  return nullptr; // not found
267 }
268 
269 
270 std::vector<wxString> LIB_TABLE::GetLogicalLibs()
271 {
272  // Only return unique logical library names. Use std::set::insert() to
273  // quietly reject any duplicates, which can happen when encountering a duplicate
274  // nickname from one of the fall back table(s).
275 
276  std::set< wxString > unique;
277  std::vector< wxString > ret;
278  const LIB_TABLE* cur = this;
279 
280  do
281  {
282  for( LIB_TABLE_ROWS_CITER it = cur->rows.begin(); it!=cur->rows.end(); ++it )
283  {
284  if( it->GetIsEnabled() )
285  {
286  unique.insert( it->GetNickName() );
287  }
288  }
289 
290  } while( ( cur = cur->fallBack ) != 0 );
291 
292  ret.reserve( unique.size() );
293 
294  // return a sorted, unique set of nicknames in a std::vector<wxString> to caller
295  for( std::set< wxString >::const_iterator it = unique.begin(); it!=unique.end(); ++it )
296  {
297  ret.push_back( *it );
298  }
299 
300  // We want to allow case-sensitive duplicates but sort by case-insensitive ordering
301  std::sort( ret.begin(), ret.end(), []( const wxString& lhs, const wxString& rhs )
302  {
303  return lhs.CmpNoCase( rhs ) < 0;
304  } );
305 
306  return ret;
307 }
308 
309 
310 bool LIB_TABLE::InsertRow( LIB_TABLE_ROW* aRow, bool doReplace )
311 {
312  ensureIndex();
313 
314  INDEX_CITER it = nickIndex.find( aRow->GetNickName() );
315 
316  if( it == nickIndex.end() )
317  {
318  rows.push_back( aRow );
319  nickIndex.insert( INDEX_VALUE( aRow->GetNickName(), rows.size() - 1 ) );
320  return true;
321  }
322 
323  if( doReplace )
324  {
325  rows.replace( it->second, aRow );
326  return true;
327  }
328 
329  return false;
330 }
331 
332 
333 void LIB_TABLE::Load( const wxString& aFileName )
334 
335 {
336  // It's OK if footprint library tables are missing.
337  if( wxFileName::IsFileReadable( aFileName ) )
338  {
339  FILE_LINE_READER reader( aFileName );
340  LIB_TABLE_LEXER lexer( &reader );
341 
342  Parse( &lexer );
343  }
344 }
345 
346 
347 void LIB_TABLE::Save( const wxString& aFileName ) const
348 {
349  FILE_OUTPUTFORMATTER sf( aFileName );
350  Format( &sf, 0 );
351 }
352 
353 
354 PROPERTIES* LIB_TABLE::ParseOptions( const std::string& aOptionsList )
355 {
356  if( aOptionsList.size() )
357  {
358  const char* cp = &aOptionsList[0];
359  const char* end = cp + aOptionsList.size();
360 
361  PROPERTIES props;
362  std::string pair;
363 
364  // Parse all name=value pairs
365  while( cp < end )
366  {
367  pair.clear();
368 
369  // Skip leading white space.
370  while( cp < end && isspace( *cp ) )
371  ++cp;
372 
373  // Find the end of pair/field
374  while( cp < end )
375  {
376  if( *cp == '\\' && cp + 1 < end && cp[1] == OPT_SEP )
377  {
378  ++cp; // skip the escape
379  pair += *cp++; // add the separator
380  }
381  else if( *cp == OPT_SEP )
382  {
383  ++cp; // skip the separator
384  break; // process the pair
385  }
386  else
387  pair += *cp++;
388  }
389 
390  // stash the pair
391  if( pair.size() )
392  {
393  // first equals sign separates 'name' and 'value'.
394  size_t eqNdx = pair.find( '=' );
395 
396  if( eqNdx != pair.npos )
397  {
398  std::string name = pair.substr( 0, eqNdx );
399  std::string value = pair.substr( eqNdx + 1 );
400  props[name] = value;
401  }
402  else
403  props[pair] = ""; // property is present, but with no value.
404  }
405  }
406 
407  if( props.size() )
408  return new PROPERTIES( props );
409  }
410 
411  return nullptr;
412 }
413 
414 
416 {
417  UTF8 ret;
418 
419  if( aProperties )
420  {
421  for( PROPERTIES::const_iterator it = aProperties->begin(); it != aProperties->end(); ++it )
422  {
423  const std::string& name = it->first;
424 
425  const UTF8& value = it->second;
426 
427  if( ret.size() )
428  ret += OPT_SEP;
429 
430  ret += name;
431 
432  // the separation between name and value is '='
433  if( value.size() )
434  {
435  ret += '=';
436 
437  for( std::string::const_iterator si = value.begin(); si != value.end(); ++si )
438  {
439  // escape any separator in the value.
440  if( *si == OPT_SEP )
441  ret += '\\';
442 
443  ret += *si;
444  }
445  }
446  }
447  }
448 
449  return ret;
450 }
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:183
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:328
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:255
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...
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:95
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:181
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:476
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:59
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.
The common library.
FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:493
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:408
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.