forked from KiCad/kicad-source-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_id.h
273 lines (242 loc) · 10.4 KB
/
lib_id.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2010-2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
* Copyright (C) 2012 Wayne Stambaugh <[email protected]>
* Copyright (C) 2010-2020 KiCad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef _LIB_ID_H_
#define _LIB_ID_H_
#include <richio.h>
#include <utf8.h>
/**
* A logical library item identifier and consists of various portions much like a URI.
*
* It consists of of triad of the library nickname, the name of the item in the library,
* and an optional revision of the item. This is a generic library identifier that can be
* used for any type of library that contains multiple named items such as footprint or
* symbol libraries.
*
* Example LIB_ID string:
* "smt:R_0805/rev0".
*
* - "smt" is the logical library name used to look up library information saved in the #LIB_TABLE.
* - "R" is the name of the item within the library.
* - "rev0" is the revision, which is optional. If missing then its delimiter should also not
* be present. A revision must begin with "rev" and be followed by at least one or more
* decimal digits.
*
* @author Dick Hollenbeck
*/
class LIB_ID
{
public:
LIB_ID() {}
// NOTE: don't define any constructors which call Parse() on their arguments. We want it
// to be obvious to callers that parsing is involved (and that valid IDs are guaranteed in
// the presence of disallowed characters, malformed ids, etc.).
/**
* This LIB_ID ctor is a special version which ignores the parsing due to symbol
* names allowing '/' as a valid character. This was causing the symbol names to
* be truncated at the first occurrence of '/' in the symbol name.
*
* @param aLibraryName is the library name used to look up the library item in the #LIB_TABLE.
* @param aItemName is the name of the library item which is not parsed by the standard
* LIB_ID::Parse() function.
* @param aRevision is the revision of the library item.
*/
LIB_ID( const wxString& aLibraryName, const wxString& aItemName,
const wxString& aRevision = wxEmptyString );
/**
* Parse LIB_ID with the information from @a aId.
*
* A typical LIB_ID string consists of a library nickname followed by a library item name.
* e.g.: "smt:R_0805", or
* e.g.: "mylib:R_0805", or
* e.g.: "ttl:7400"
*
* @param aId is the string to populate the #LIB_ID object.
* @param aFix indicates invalid chars should be replaced with '_'.
*
* @return minus 1 (i.e. -1) means success, >= 0 indicates the character offset into
* aId at which an error was detected.
*/
int Parse( const UTF8& aId, bool aFix = false );
/**
* Return the logical library name portion of a LIB_ID.
*/
const UTF8& GetLibNickname() const { return m_libraryName; }
/**
* Override the logical library name portion of the LIB_ID to @a aNickname.
*
* @return int - minus 1 (i.e. -1) means success, >= 0 indicates the character offset
* into the parameter at which an error was detected, usually because it
* contained '/' or ':'.
*/
int SetLibNickname( const UTF8& aNickname );
/**
* @return the library item name, i.e. footprintName, in UTF8.
*/
const UTF8& GetLibItemName() const { return m_itemName; }
/**
* Get strings for display messages in dialogs.
*
* Equivalent to m_itemName.wx_str(), but more explicit when building a Unicode string
* in messages.
*
* @return the library item name, i.e. footprintName in a wxString (UTF16 or 32).
*/
const wxString GetUniStringLibItemName() const { return m_itemName.wx_str(); }
/**
* Override the library item name portion of the LIB_ID to @a aLibItemName
*
* @return int - minus 1 (i.e. -1) means success, >= 0 indicates the character offset
* into the parameter at which an error was detected, usually because it
* contained '/'.
*/
int SetLibItemName( const UTF8& aLibItemName, bool aTestForRev = true );
int SetRevision( const UTF8& aRevision );
const UTF8& GetRevision() const { return m_revision; }
UTF8 GetLibItemNameAndRev() const;
/**
* @return the fully formatted text of the LIB_ID in a UTF8 string.
*/
UTF8 Format() const;
/**
* @return the fully formatted text of the LIB_ID in a wxString (UTF16 or UTF32),
* suitable to display the LIB_ID in dialogs.
*/
wxString GetUniStringLibId() const
{
return Format().wx_str();
}
/**
* @return a string in the proper format as an LIB_ID for a combination of
* aLibraryName, aLibItemName, and aRevision.
*
* @throw PARSE_ERROR if any of the pieces are illegal.
*/
static UTF8 Format( const UTF8& aLibraryName, const UTF8& aLibItemName,
const UTF8& aRevision = "" );
/**
* Check if this LID_ID is valid.
*
* A valid #LIB_ID must have both the library nickname and the library item name defined.
* The revision field is optional.
*
* @note A return value of true does not indicated that the #LIB_ID is a valid #LIB_TABLE
* entry.
*
* @return true is the #LIB_ID is valid.
*
*/
bool IsValid() const
{
return !m_libraryName.empty() && !m_itemName.empty();
}
/**
* @return true if the #LIB_ID only has the #m_itemName name defined.
*/
bool IsLegacy() const
{
return m_libraryName.empty() && !m_itemName.empty() && m_revision.empty();
}
/**
* Clear the contents of the library nickname, library entry name, and revision strings.
*/
void clear();
/**
* @return a boolean true value if the LIB_ID is empty. Otherwise return false.
*/
bool empty() const
{
return m_libraryName.empty() && m_itemName.empty() && m_revision.empty();
}
/**
* Compare the contents of LIB_ID objects by performing a std::string comparison of the
* library nickname, library entry name, and revision strings respectively.
*
* @param aLibId is the LIB_ID to compare against.
* @return -1 if less than \a aLibId, 1 if greater than \a aLibId, and 0 if equal to \a aLibId.
*/
int compare( const LIB_ID& aLibId ) const;
bool operator < ( const LIB_ID& aLibId ) const { return this->compare( aLibId ) < 0; }
bool operator > ( const LIB_ID& aLibId ) const { return this->compare( aLibId ) > 0; }
bool operator ==( const LIB_ID& aLibId ) const { return this->compare( aLibId ) == 0; }
bool operator !=( const LIB_ID& aLibId ) const { return !(*this == aLibId); }
/**
* Examine \a aLibItemName for invalid #LIB_ID item name characters.
*
* @param aLibItemName is the #LIB_ID name to test for illegal characters.
* @return offset of first illegal character otherwise -1.
*/
static int HasIllegalChars( const UTF8& aLibItemName );
/**
* Replace illegal #LIB_ID item name characters with underscores '_'.
*
* @param aLibItemName is the #LIB_ID item name to replace illegal characters.
* @param aLib True if we are checking library names, false if we are checking item names
* @return the corrected version of \a aLibItemName.
*/
static UTF8 FixIllegalChars( const UTF8& aLibItemName, bool aLib = false );
/**
* Looks for characters that are illegal in library nicknames.
*
* @param aLibraryName is the logical library name to be tested.
* @return Invalid character found in the name or 0 is the name is valid.
*/
static unsigned FindIllegalLibraryNameChar( const UTF8& aLibraryName );
protected:
/**
* Tests whether a Unicode character is a legal LIB_ID item name character.
*
* The criteria for legal LIB_ID character is as follows:
* - For both symbol and footprint names, neither '/' or ':' are legal. They are
* reserved characters used by #LIB_ID::Parse.
* - Spaces are allowed in footprint names as they are a legal filename character
* on all operating systems.
* - Spaces are not allowed in symbol names since symbol names are not quoted in the
* schematic or symbol library file formats.
* - Spaces are allowed in footprint library nicknames as they are quoted in the
* footprint library table file format.
* - Spaces are now also allowed in symbol library nicknames since they are quoted in
* the new symbol library sexpr file format.
* - Illegal file name characters are not allowed in footprint names since the file
* name is the footprint name.
* - Illegal file name characters except '/' are allowed in symbol names since the
* name is not the file name.
*
*
* @note @a aUniChar is expected to be a 32 bit Unicode character, not a UTF8 char, that use
* a variable length coding value.
*/
static bool isLegalChar( unsigned aUniChar );
/**
* Tests whether a Unicode character is a legal LIB_ID library nickname character
*
* @note @a aUniChar is expected to be a 32 bit Unicode character, not a UTF8 char, that use
* a variable length coding value.
*/
static bool isLegalLibraryNameChar( unsigned aUniChar );
UTF8 m_libraryName; ///< The nickname of the library or empty.
UTF8 m_itemName; ///< The name of the entry in the logical library.
UTF8 m_revision; ///< The revision of the entry.
};
#endif // _LIB_ID_H_