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 <[email protected]>
5 * Copyright (C) 2012 Wayne Stambaugh <[email protected]>
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/translation.h>
29
30#include <macros.h> // TO_UTF8()
31#include <lib_id.h>
32
33
34static inline int okLogical( const UTF8& aField )
35{
36 // std::string::npos is largest positive number, casting to int makes it -1.
37 // Returning that means success.
38 return int( aField.find_first_of( ":" ) );
39}
40
41
43{
47}
48
49
50int LIB_ID::Parse( const UTF8& aId, bool aFix )
51{
52 clear();
53
54 size_t partNdx;
55 int offset = -1;
56
57 //=====<library nickname>=============================
58 if( ( partNdx = aId.find( ':' ) ) != aId.npos )
59 {
60 offset = SetLibNickname( aId.substr( 0, partNdx ) );
61
62 if( offset > -1 )
63 return offset;
64
65 ++partNdx; // skip ':'
66 }
67 else
68 {
69 partNdx = 0;
70 }
71
72 //=====<item name>====================================
73 UTF8 fpname = aId.substr( partNdx );
74
75 // Be sure the item name is valid.
76 // Some chars can be found in legacy files converted files from other EDA tools.
77 if( aFix )
78 fpname = FixIllegalChars( fpname, false );
79 else
80 offset = HasIllegalChars( fpname );
81
82 if( offset > -1 )
83 return offset;
84
85 SetLibItemName( fpname );
86
87 return -1;
88}
89
90
91LIB_ID::LIB_ID( const wxString& aLibraryName, const wxString& aItemName ) :
92 m_libraryName( aLibraryName ),
93 m_itemName( aItemName )
94{
95}
96
97
98int LIB_ID::SetLibNickname( const UTF8& aLogical )
99{
100 int offset = okLogical( aLogical );
101
102 if( offset == -1 )
103 m_libraryName = aLogical;
104
105 return offset;
106}
107
108
109int LIB_ID::SetLibItemName( const UTF8& aLibItemName )
110{
111 m_itemName = aLibItemName;
112
113 return -1;
114}
115
116
118{
119 UTF8 ret;
120
121 if( m_libraryName.size() )
122 {
123 ret += m_libraryName;
124 ret += ':';
125 }
126
127 ret += m_itemName;
128
129 return ret;
130}
131
132
133UTF8 LIB_ID::Format( const UTF8& aLibraryName, const UTF8& aLibItemName )
134{
135 UTF8 ret;
136 int offset;
137
138 if( aLibraryName.size() )
139 {
140 offset = okLogical( aLibraryName );
141
142 if( offset != -1 )
143 {
144 THROW_PARSE_ERROR( _( "Illegal character found in logical library name" ),
145 wxString::FromUTF8( aLibraryName.c_str() ), aLibraryName.c_str(),
146 0, offset );
147 }
148
149 ret += aLibraryName;
150 ret += ':';
151 }
152
153 ret += aLibItemName; // TODO: Add validity test.
154
155 return ret;
156}
157
158
159int LIB_ID::compare( const LIB_ID& aLibId ) const
160{
161 // Don't bother comparing the same object.
162 if( this == &aLibId )
163 return 0;
164
165 int retv = m_libraryName.compare( aLibId.m_libraryName );
166
167 if( retv != 0 )
168 return retv;
169
170 return m_itemName.compare( aLibId.m_itemName );
171}
172
173
174int LIB_ID::HasIllegalChars( const UTF8& aLibItemName )
175{
176 int offset = 0;
177
178 for( auto& ch : aLibItemName )
179 {
180 if( !isLegalChar( ch ) )
181 return offset;
182 else
183 ++offset;
184 }
185
186 return -1;
187}
188
189
190UTF8 LIB_ID::FixIllegalChars( const UTF8& aLibItemName, bool aLib )
191{
192 UTF8 fixedName;
193
194 for( UTF8::uni_iter chIt = aLibItemName.ubegin(); chIt < aLibItemName.uend(); ++chIt )
195 {
196 auto ch = *chIt;
197 if( aLib )
198 fixedName += isLegalLibraryNameChar( ch ) ? ch : '_';
199 else
200 fixedName += isLegalChar( ch ) ? ch : '_';
201 }
202
203 return fixedName;
204}
205
206
207bool LIB_ID::isLegalChar( unsigned aUniChar )
208{
209 bool const space_allowed = true;
210 bool const illegal_filename_chars_allowed = false;
211
212 if( aUniChar < ' ' )
213 return false;
214
215 // This list of characters is also duplicated in validators.cpp and footprint.cpp
216 // TODO: Unify forbidden character lists - Warning, invalid filename characters are not the same
217 // as invalid LIB_ID characters. We will need to separate the FP filenames from FP names before this
218 // can be unified
219 switch( aUniChar )
220 {
221 case ':':
222 case '\t':
223 case '\n':
224 case '\r':
225 return false;
226
227 case '\\':
228 case '<':
229 case '>':
230 case '"':
231 return illegal_filename_chars_allowed;
232
233 case ' ':
234 return space_allowed;
235
236 default:
237 return true;
238 }
239}
240
241
242unsigned LIB_ID::FindIllegalLibraryNameChar( const UTF8& aLibraryName )
243{
244 for( unsigned ch : aLibraryName )
245 {
246 if( !isLegalLibraryNameChar( ch ) )
247 return ch;
248 }
249
250 return 0;
251}
252
253
254bool LIB_ID::isLegalLibraryNameChar( unsigned aUniChar )
255{
256 bool const space_allowed = true;
257
258 if( aUniChar < ' ' )
259 return false;
260
261 switch( aUniChar )
262 {
263 case '\\':
264 case ':':
265 return false;
266
267 case ' ':
268 return space_allowed;
269
270 default:
271 return true;
272 }
273}
274
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:50
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition: lib_id.cpp:109
LIB_ID()
Definition: lib_id.h:51
static int HasIllegalChars(const UTF8 &aLibItemName)
Examine aLibItemName for invalid LIB_ID item name characters.
Definition: lib_id.cpp:174
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:159
static bool isLegalChar(unsigned aUniChar)
Tests whether a Unicode character is a legal LIB_ID item name character.
Definition: lib_id.cpp:207
static unsigned FindIllegalLibraryNameChar(const UTF8 &aLibraryName)
Looks for characters that are illegal in library nicknames.
Definition: lib_id.cpp:242
UTF8 Format() const
Definition: lib_id.cpp:117
UTF8 m_libraryName
The nickname of the library or empty.
Definition: lib_id.h:264
int SetLibNickname(const UTF8 &aNickname)
Override the logical library name portion of the LIB_ID to aNickname.
Definition: lib_id.cpp:98
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition: lib_id.cpp:190
UTF8 m_itemName
The name of the entry in the logical library.
Definition: lib_id.h:265
void clear()
Clear the contents of the library nickname, library entry name.
Definition: lib_id.cpp:42
UTF8 m_subLibraryName
Optional sub-library name used for grouping within a library.
Definition: lib_id.h:266
static bool isLegalLibraryNameChar(unsigned aUniChar)
Tests whether a Unicode character is a legal LIB_ID library nickname character.
Definition: lib_id.cpp:254
uni_iter is a non-mutating iterator that walks through unicode code points in the UTF8 encoded string...
Definition: utf8.h:204
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:71
static constexpr std::string::size_type npos
Definition: utf8.h:151
std::string::size_type find(char c) const
Definition: utf8.h:105
void clear()
Definition: utf8.h:108
std::string substr(size_t pos=0, size_t len=npos) const
Definition: utf8.h:178
uni_iter uend() const
Return a uni_iter initialized to the end of "this" UTF8 byte sequence.
Definition: utf8.h:287
int compare(const std::string &s) const
Definition: utf8.h:111
std::string::size_type find_first_of(const std::string &str, std::string::size_type pos=0) const
Definition: utf8.h:117
const char * c_str() const
Definition: utf8.h:102
std::string::size_type size() const
Definition: utf8.h:110
uni_iter ubegin() const
Returns a uni_iter initialized to the start of "this" UTF8 byte sequence.
Definition: utf8.h:279
#define _(s)
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:164
static int okLogical(const UTF8 &aField)
Definition: lib_id.cpp:34
This file contains miscellaneous commonly used macros and functions.