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-2018 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 {
116  nickname.clear();
117  item_name.clear();
118  revision.clear();
119 }
120 
121 
122 int LIB_ID::Parse( const UTF8& aId, LIB_ID_TYPE aType, 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  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  //=====<nickname>==========================================
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, aType, false );
172  else
173  offset = HasIllegalChars( fpname, aType );
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& aLibName, const wxString& aLibItemName,
185  const wxString& aRevision ) :
186  nickname( aLibName ),
187  item_name( aLibItemName ),
188  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  {
199  nickname = aLogical;
200  }
201 
202  return offset;
203 }
204 
205 
206 int LIB_ID::SetLibItemName( const UTF8& aLibItemName, bool aTestForRev )
207 {
208  int separation = int( aLibItemName.find_first_of( "/" ) );
209 
210  if( aTestForRev && separation != -1 )
211  {
212  item_name = aLibItemName.substr( 0, separation-1 );
213  return separation;
214  }
215  else
216  {
217  item_name = aLibItemName;
218  }
219 
220  return -1;
221 }
222 
223 
224 int LIB_ID::SetRevision( const UTF8& aRevision )
225 {
226  int offset = okRevision( aRevision );
227 
228  if( offset == -1 )
229  {
230  revision = aRevision;
231  }
232 
233  return offset;
234 }
235 
236 
238 {
239  UTF8 ret;
240 
241  if( nickname.size() )
242  {
243  ret += nickname;
244  ret += ':';
245  }
246 
247  ret += item_name;
248 
249  if( revision.size() )
250  {
251  ret += '/';
252  ret += revision;
253  }
254 
255  return ret;
256 }
257 
258 
260 {
261  UTF8 ret;
262 
263  if( revision.size() )
264  {
265  ret += '/';
266  ret += revision;
267  }
268 
269  return ret;
270 }
271 
272 
273 UTF8 LIB_ID::Format( const UTF8& aLogicalLib, const UTF8& aLibItemName, const UTF8& aRevision )
274 {
275  UTF8 ret;
276  int offset;
277 
278  if( aLogicalLib.size() )
279  {
280  offset = okLogical( aLogicalLib );
281 
282  if( offset != -1 )
283  {
284  THROW_PARSE_ERROR( _( "Illegal character found in logical library name" ),
285  wxString::FromUTF8( aLogicalLib.c_str() ),
286  aLogicalLib.c_str(), 0, offset );
287  }
288 
289  ret += aLogicalLib;
290  ret += ':';
291  }
292 
293  ret += aLibItemName; // TODO: Add validity test.
294 
295  if( aRevision.size() )
296  {
297  offset = okRevision( aRevision );
298 
299  if( offset != -1 )
300  {
301  THROW_PARSE_ERROR( _( "Illegal character found in revision" ),
302  wxString::FromUTF8( aRevision.c_str() ),
303  aRevision.c_str(),
304  0,
305  offset );
306  }
307 
308  ret += '/';
309  ret += aRevision;
310  }
311 
312  return ret;
313 }
314 
315 
316 int LIB_ID::compare( const LIB_ID& aLibId ) const
317 {
318  // Don't bother comparing the same object.
319  if( this == &aLibId )
320  return 0;
321 
322  int retv = nickname.compare( aLibId.nickname );
323 
324  if( retv != 0 )
325  return retv;
326 
327  retv = item_name.compare( aLibId.item_name );
328 
329  if( retv != 0 )
330  return retv;
331 
332  return revision.compare( aLibId.revision );
333 }
334 
335 
336 int LIB_ID::HasIllegalChars( const UTF8& aLibItemName, LIB_ID_TYPE aType )
337 {
338  int offset = 0;
339 
340  for( auto ch : aLibItemName )
341  {
342  if( !isLegalChar( ch, aType ) )
343  return offset;
344  else
345  ++offset;
346  }
347 
348  return -1;
349 }
350 
351 
352 UTF8 LIB_ID::FixIllegalChars( const UTF8& aLibItemName, LIB_ID_TYPE aType, bool aLib )
353 {
354  UTF8 fixedName;
355 
356  for( UTF8::uni_iter chIt = aLibItemName.ubegin(); chIt < aLibItemName.uend(); ++chIt )
357  {
358  auto ch = *chIt;
359  if( aLib )
360  fixedName += isLegalLibNicknameChar( ch, aType ) ? ch : '_';
361  else
362  fixedName += isLegalChar( ch, aType ) ? ch : '_';
363  }
364 
365  return fixedName;
366 }
367 
368 
369 bool LIB_ID::isLegalChar( unsigned aUniChar, LIB_ID_TYPE aType )
370 {
371  bool const space_allowed = ( aType == ID_PCB );
372  bool const illegal_filename_chars_allowed = ( aType == ID_SCH );
373 
374  if( aUniChar < ' ' )
375  return false;
376 
377  switch( aUniChar )
378  {
379  case ':':
380  case '/':
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::FindIllegalLibNicknameChar( const UTF8& aNickname, LIB_ID_TYPE aType )
399 {
400  for( unsigned ch : aNickname )
401  {
402  if( !isLegalLibNicknameChar( ch, aType ) )
403  return ch;
404  }
405 
406  return 0;
407 }
408 
409 
410 bool LIB_ID::isLegalLibNicknameChar( unsigned aUniChar, LIB_ID_TYPE aType )
411 {
412  bool const space_allowed = ( aType != ID_SCH );
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 && defined(DEBUG)
433 
434 // build this with Debug CMAKE_BUILD_TYPE
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 item_name:'%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 
461 int main( int argc, char** argv )
462 {
463  LIB_ID::Test();
464 
465  return 0;
466 }
467 
468 #endif
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:316
Class UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion supp...
Definition: utf8.h:73
static constexpr std::string::size_type npos
Definition: utf8.h:155
const char * EndsWithRev(const char *start, const char *tail, char separator)
Definition: lib_id.cpp:41
static bool isLegalLibNicknameChar(unsigned aUniChar, LIB_ID_TYPE aType)
Tests whether a unicode character is a legal LIB_ID library nickname character.
Definition: lib_id.cpp:410
UTF8 revision
The revision of the entry.
Definition: lib_id.h:271
static int okLogical(const UTF8 &aField)
Definition: lib_id.cpp:86
std::string substr(size_t pos=0, size_t len=npos) const
Definition: utf8.h:182
static bool isLegalChar(unsigned aUniChar, LIB_ID_TYPE aType)
Tests whether a unicode character is a legal LIB_ID item name character.
Definition: lib_id.cpp:369
static unsigned FindIllegalLibNicknameChar(const UTF8 &aNickname, LIB_ID_TYPE aType)
Looks for characters that are illegal in library nicknames.
Definition: lib_id.cpp:398
static int HasIllegalChars(const UTF8 &aLibItemName, LIB_ID_TYPE aType)
Examine aLibItemName for invalid LIB_ID item name characters.
Definition: lib_id.cpp:336
std::string::size_type size() const
Definition: utf8.h:115
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
UTF8 GetLibItemNameAndRev() const
Definition: lib_id.cpp:259
uni_iter ubegin() const
Function ubegin returns a uni_iter initialized to the start of "this" UTF8 byte sequence.
Definition: utf8.h:285
This file contains miscellaneous commonly used macros and functions.
const UTF8 & GetRevision() const
Definition: lib_id.h:134
int main(int argc, char **argv)
std::string::size_type find_first_of(const std::string &str, std::string::size_type pos=0) const
Definition: utf8.h:122
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
static bool isDigit(char c)
Definition: lib_id.cpp:35
UTF8 item_name
The name of the entry in the logical library.
Definition: lib_id.h:270
int SetRevision(const UTF8 &aRevision)
Definition: lib_id.cpp:224
std::string::size_type find(char c) const
Definition: utf8.h:110
int compare(const std::string &s) const
Definition: utf8.h:116
std::string::size_type length() const
Definition: utf8.h:114
UTF8 nickname
The nickname of the library or empty.
Definition: lib_id.h:269
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
void clear()
Clear the contents of the library nickname, library entry name, and revision strings.
Definition: lib_id.cpp:114
int SetLibItemName(const UTF8 &aLibItemName, bool aTestForRev=true)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:206
int SetLibNickname(const UTF8 &aNickname)
Override the logical library name portion of the LIB_ID to aNickname.
Definition: lib_id.cpp:193
class uni_iter is a non-mutating iterator that walks through unicode code points in the UTF8 encoded ...
Definition: utf8.h:207
LIB_ID_TYPE
Types of library identifiers
Definition: lib_id.h:56
LIB_ID()
Definition: lib_id.h:58
uni_iter uend() const
Function uend returns a uni_iter initialized to the end of "this" UTF8 byte sequence.
Definition: utf8.h:294
const char * c_str() const
Definition: utf8.h:107
size_t i
Definition: json11.cpp:597
UTF8 Format() const
Definition: lib_id.cpp:237
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, LIB_ID_TYPE aType, bool aLib=false)
Replace illegal LIB_ID item name characters with underscores &#39;_&#39;.
Definition: lib_id.cpp:352
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
void clear()
Definition: utf8.h:113
int Parse(const UTF8 &aId, LIB_ID_TYPE aType, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:122
static int okRevision(const UTF8 &aField)
Definition: lib_id.cpp:94