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