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