KiCad PCB EDA Suite
lib_id.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 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2012 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 2010-2020 KiCad Developers, see change_log.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 #include <cstring>
27 #include <memory>
28 #include <wx/wx.h> // _()
29 
30 #include <macros.h> // TO_UTF8()
31 #include <lib_id.h>
32 #include <kicad_string.h>
33 
34 
35 static inline bool isDigit( char c )
36 {
37  return c >= '0' && c <= '9';
38 }
39 
40 
41 const char* EndsWithRev( const char* start, const char* tail, char separator )
42 {
43  bool sawDigit = false;
44 
45  while( tail > start && isDigit( *--tail ) )
46  {
47  sawDigit = true;
48  }
49 
50  // if sawDigit, tail points to the 'v' here.
51 
52  if( sawDigit && tail-3 >= start )
53  {
54  tail -= 3;
55 
56  if( tail[0]==separator && tail[1]=='r' && tail[2]=='e' && tail[3]=='v' )
57  {
58  return tail+1; // omit separator, return "revN[N..]"
59  }
60  }
61 
62  return 0;
63 }
64 
65 
66 #if 0 // Not used
67 int RevCmp( const char* s1, const char* s2 )
68 {
69  int r = strncmp( s1, s2, 3 );
70 
71  if( r || strlen(s1)<4 || strlen(s2)<4 )
72  {
73  return r;
74  }
75 
76  int rnum1 = atoi( s1+3 );
77  int rnum2 = atoi( s2+3 );
78 
79  return -(rnum1 - rnum2); // swap the sign, higher revs first
80 }
81 #endif
82 
83 //----<Policy and field test functions>-------------------------------------
84 
85 
86 static inline int okLogical( const UTF8& aField )
87 {
88  // std::string::npos is largest positive number, casting to int makes it -1.
89  // Returning that means success.
90  return int( aField.find_first_of( ":" ) );
91 }
92 
93 
94 static int okRevision( const UTF8& aField )
95 {
96  char rev[32]; // C string for speed
97 
98  if( aField.size() >= 4 )
99  {
100  strncpy( rev, "x/", sizeof( rev ) );
101  strncat( rev, aField.c_str(), sizeof(rev)-strlen(rev)-1 );
102 
103  if( EndsWithRev( rev, rev + strlen(rev), '/' ) == rev+2 )
104  return -1; // success
105  }
106 
107  return 0; // first character position "is in error", is best we can do.
108 }
109 
110 
111 //----</Policy and field test functions>-------------------------------------
112 
113 
115 {
117  m_itemName.clear();
118  m_revision.clear();
119 }
120 
121 
122 int LIB_ID::Parse( const UTF8& aId, bool aFix )
123 {
124  clear();
125 
126  const char* buffer = aId.c_str();
127  const char* rev = EndsWithRev( buffer, buffer+aId.length(), '/' );
128  size_t revNdx;
129  size_t partNdx;
130  int offset = -1;
131 
132  //=====<revision>=====================================
133  // in a LIB_ID like discret:R3/rev4
134  if( rev )
135  {
136  revNdx = rev - buffer;
137 
138  // no need to check revision, EndsWithRev did that.
139  m_revision = aId.substr( revNdx );
140  --revNdx; // back up to omit the '/' which precedes the rev
141  }
142  else
143  {
144  revNdx = aId.size();
145  }
146 
147  //=====<name>=========================================
148  if( ( partNdx = aId.find( ':' ) ) != aId.npos )
149  {
150  offset = SetLibNickname( aId.substr( 0, partNdx ) );
151 
152  if( offset > -1 )
153  return offset;
154 
155  ++partNdx; // skip ':'
156  }
157  else
158  {
159  partNdx = 0;
160  }
161 
162  //=====<item name>====================================
163  if( partNdx >= revNdx )
164  return partNdx; // Error: no library item name.
165 
166  UTF8 fpname = aId.substr( partNdx, revNdx-partNdx );
167 
168  // Be sure the item name is valid.
169  // Some chars can be found in legacy files converted files from other EDA tools.
170  if( aFix )
171  fpname = FixIllegalChars( fpname, false );
172  else
173  offset = HasIllegalChars( fpname );
174 
175  if( offset > -1 )
176  return offset;
177 
178  SetLibItemName( fpname );
179 
180  return -1;
181 }
182 
183 
184 LIB_ID::LIB_ID( const wxString& aLibraryName, const wxString& aItemName,
185  const wxString& aRevision ) :
186  m_libraryName( aLibraryName ),
187  m_itemName( aItemName ),
188  m_revision( aRevision )
189 {
190 }
191 
192 
193 int LIB_ID::SetLibNickname( const UTF8& aLogical )
194 {
195  int offset = okLogical( aLogical );
196 
197  if( offset == -1 )
198  m_libraryName = aLogical;
199 
200  return offset;
201 }
202 
203 
204 int LIB_ID::SetLibItemName( const UTF8& aLibItemName, bool aTestForRev )
205 {
206  int separation = int( aLibItemName.find_first_of( "/" ) );
207 
208  if( aTestForRev && separation != -1 )
209  {
210  m_itemName = aLibItemName.substr( 0, separation-1 );
211  return separation;
212  }
213  else
214  {
215  m_itemName = aLibItemName;
216  }
217 
218  return -1;
219 }
220 
221 
222 int LIB_ID::SetRevision( const UTF8& aRevision )
223 {
224  int offset = okRevision( aRevision );
225 
226  if( offset == -1 )
227  m_revision = aRevision;
228 
229  return offset;
230 }
231 
232 
234 {
235  UTF8 ret;
236 
237  if( m_libraryName.size() )
238  {
239  ret += m_libraryName;
240  ret += ':';
241  }
242 
243  ret += m_itemName;
244 
245  if( m_revision.size() )
246  {
247  ret += '/';
248  ret += m_revision;
249  }
250 
251  return ret;
252 }
253 
254 
256 {
257  UTF8 ret = m_itemName;
258 
259  if( m_revision.size() )
260  {
261  ret += '/';
262  ret += m_revision;
263  }
264 
265  return ret;
266 }
267 
268 
269 UTF8 LIB_ID::Format( const UTF8& aLibraryName, const UTF8& aLibItemName, const UTF8& aRevision )
270 {
271  UTF8 ret;
272  int offset;
273 
274  if( aLibraryName.size() )
275  {
276  offset = okLogical( aLibraryName );
277 
278  if( offset != -1 )
279  {
280  THROW_PARSE_ERROR( _( "Illegal character found in logical library name" ),
281  wxString::FromUTF8( aLibraryName.c_str() ), aLibraryName.c_str(), 0, offset );
282  }
283 
284  ret += aLibraryName;
285  ret += ':';
286  }
287 
288  ret += aLibItemName; // TODO: Add validity test.
289 
290  if( aRevision.size() )
291  {
292  offset = okRevision( aRevision );
293 
294  if( offset != -1 )
295  {
296  THROW_PARSE_ERROR( _( "Illegal character found in revision" ),
297  wxString::FromUTF8( aRevision.c_str() ),
298  aRevision.c_str(),
299  0,
300  offset );
301  }
302 
303  ret += '/';
304  ret += aRevision;
305  }
306 
307  return ret;
308 }
309 
310 
311 int LIB_ID::compare( const LIB_ID& aLibId ) const
312 {
313  // Don't bother comparing the same object.
314  if( this == &aLibId )
315  return 0;
316 
317  int retv = m_libraryName.compare( aLibId.m_libraryName );
318 
319  if( retv != 0 )
320  return retv;
321 
322  retv = m_itemName.compare( aLibId.m_itemName );
323 
324  if( retv != 0 )
325  return retv;
326 
327  return m_revision.compare( aLibId.m_revision );
328 }
329 
330 
331 int LIB_ID::HasIllegalChars( const UTF8& aLibItemName )
332 {
333  int offset = 0;
334 
335  for( auto ch : aLibItemName )
336  {
337  if( !isLegalChar( ch ) )
338  return offset;
339  else
340  ++offset;
341  }
342 
343  return -1;
344 }
345 
346 
347 UTF8 LIB_ID::FixIllegalChars( const UTF8& aLibItemName, bool aLib )
348 {
349  UTF8 fixedName;
350 
351  for( UTF8::uni_iter chIt = aLibItemName.ubegin(); chIt < aLibItemName.uend(); ++chIt )
352  {
353  auto ch = *chIt;
354  if( aLib )
355  fixedName += isLegalLibraryNameChar( ch ) ? ch : '_';
356  else
357  fixedName += isLegalChar( ch ) ? ch : '_';
358  }
359 
360  return fixedName;
361 }
362 
363 
364 bool LIB_ID::isLegalChar( unsigned aUniChar )
365 {
366  bool const space_allowed = true;
367  bool const illegal_filename_chars_allowed = false;
368 
369  if( aUniChar < ' ' )
370  return false;
371 
372  // This list of characters is also duplicated in validators.cpp and footprint.cpp
373  // TODO: Unify forbidden character lists
374  switch( aUniChar )
375  {
376  case ':':
377  case '/':
378  case '\t':
379  case '\n':
380  case '\r':
381  return false;
382 
383  case '\\':
384  case '<':
385  case '>':
386  case '"':
387  return illegal_filename_chars_allowed;
388 
389  case ' ':
390  return space_allowed;
391 
392  default:
393  return true;
394  }
395 }
396 
397 
398 unsigned LIB_ID::FindIllegalLibraryNameChar( const UTF8& aLibraryName )
399 {
400  for( unsigned ch : aLibraryName )
401  {
402  if( !isLegalLibraryNameChar( ch ) )
403  return ch;
404  }
405 
406  return 0;
407 }
408 
409 
410 bool LIB_ID::isLegalLibraryNameChar( unsigned aUniChar )
411 {
412  bool const space_allowed = true;
413 
414  if( aUniChar < ' ' )
415  return false;
416 
417  switch( aUniChar )
418  {
419  case '\\':
420  case ':':
421  return false;
422 
423  case ' ':
424  return space_allowed;
425 
426  default:
427  return true;
428  }
429 }
430 
431 
432 #if 0
433 
434 // @todo Move this test into the unit test framework.
435 
436 void LIB_ID::Test()
437 {
438  static const char* lpids[] = {
439  "smt:R_0805/rev0",
440  "mysmt:R_0805/rev2",
441  "device:AXIAL-0500",
442  };
443 
444  for( unsigned i=0; i<sizeof(lpids)/sizeof(lpids[0]); ++i )
445  {
446  // test some round tripping
447 
448  LIB_ID lpid( lpids[i] ); // parse
449 
450  // format
451  printf( "input:'%s' full:'%s' nickname: %s m_itemName:'%s' rev:'%s'\n",
452  lpids[i],
453  lpid.Format().c_str(),
454  lpid.GetLibNickname().c_str(),
455  lpid.GetLibItemName().c_str(),
456  lpid.GetRevision().c_str() );
457  }
458 }
459 
460 #endif
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:70
static constexpr std::string::size_type npos
Definition: utf8.h:151
const char * EndsWithRev(const char *start, const char *tail, char separator)
Definition: lib_id.cpp:41
int compare(const std::string &s) const
Definition: utf8.h:111
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib=false)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition: lib_id.cpp:347
UTF8 m_revision
The revision of the entry.
Definition: lib_id.h:269
static int okLogical(const UTF8 &aField)
Definition: lib_id.cpp:86
uni_iter uend() const
Return a uni_iter initialized to the end of "this" UTF8 byte sequence.
Definition: utf8.h:287
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
static bool isLegalLibraryNameChar(unsigned aUniChar)
Tests whether a Unicode character is a legal LIB_ID library nickname character.
Definition: lib_id.cpp:410
static bool isLegalChar(unsigned aUniChar)
Tests whether a Unicode character is a legal LIB_ID item name character.
Definition: lib_id.cpp:364
int compare(const LIB_ID &aLibId) const
Compare the contents of LIB_ID objects by performing a std::string comparison of the library nickname...
Definition: lib_id.cpp:311
This file contains miscellaneous commonly used macros and functions.
const char * c_str() const
Definition: utf8.h:102
UTF8 GetLibItemNameAndRev() const
Definition: lib_id.cpp:255
uni_iter ubegin() const
Returns a uni_iter initialized to the start of "this" UTF8 byte sequence.
Definition: utf8.h:279
std::string::size_type find_first_of(const std::string &str, std::string::size_type pos=0) const
Definition: utf8.h:117
std::string::size_type find(char c) const
Definition: utf8.h:105
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:164
static bool isDigit(char c)
Definition: lib_id.cpp:35
std::string::size_type length() const
Definition: utf8.h:109
int SetRevision(const UTF8 &aRevision)
Definition: lib_id.cpp:222
void clear()
Clear the contents of the library nickname, library entry name, and revision strings.
Definition: lib_id.cpp:114
UTF8 m_libraryName
The nickname of the library or empty.
Definition: lib_id.h:267
UTF8 Format() const
Definition: lib_id.cpp:233
int SetLibItemName(const UTF8 &aLibItemName, bool aTestForRev=true)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:204
int SetLibNickname(const UTF8 &aNickname)
Override the logical library name portion of the LIB_ID to aNickname.
Definition: lib_id.cpp:193
uni_iter is a non-mutating iterator that walks through unicode code points in the UTF8 encoded string...
Definition: utf8.h:203
LIB_ID()
Definition: lib_id.h:54
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:122
static int HasIllegalChars(const UTF8 &aLibItemName)
Examine aLibItemName for invalid LIB_ID item name characters.
Definition: lib_id.cpp:331
#define _(s)
Definition: 3d_actions.cpp:33
UTF8 m_itemName
The name of the entry in the logical library.
Definition: lib_id.h:268
std::string substr(size_t pos=0, size_t len=npos) const
Definition: utf8.h:178
std::string::size_type size() const
Definition: utf8.h:110
void clear()
Definition: utf8.h:108
static int okRevision(const UTF8 &aField)
Definition: lib_id.cpp:94
static unsigned FindIllegalLibraryNameChar(const UTF8 &aLibraryName)
Looks for characters that are illegal in library nicknames.
Definition: lib_id.cpp:398