2016-11-20 23:35:08 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2017-11-23 15:52:28 +00:00
|
|
|
* Copyright (C) 2012 Wayne Stambaugh <stambaughw@gmail.com>
|
2020-02-07 20:27:48 +00:00
|
|
|
* Copyright (C) 2010-2020 KiCad Developers, see change_log.txt for contributors.
|
2016-11-20 23:35:08 +00:00
|
|
|
*
|
|
|
|
* 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_
|
|
|
|
|
2023-09-08 02:09:26 +00:00
|
|
|
#include <core/utf8.h>
|
2016-11-20 23:35:08 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-23 15:52:28 +00:00
|
|
|
* A logical library item identifier and consists of various portions much like a URI.
|
2016-11-20 23:35:08 +00:00
|
|
|
*
|
2022-08-18 19:49:40 +00:00
|
|
|
* It consists of of a dyad of the library nickname and the name of the item in the library
|
|
|
|
* This is a generic library identifier that can be
|
2016-11-20 23:35:08 +00:00
|
|
|
* used for any type of library that contains multiple named items such as footprint or
|
|
|
|
* symbol libraries.
|
|
|
|
*
|
|
|
|
* Example LIB_ID string:
|
2022-08-18 19:49:40 +00:00
|
|
|
* "smt:R_0805".
|
2016-11-20 23:35:08 +00:00
|
|
|
*
|
2017-11-23 15:52:28 +00:00
|
|
|
* - "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.
|
2016-11-20 23:35:08 +00:00
|
|
|
*
|
|
|
|
* @author Dick Hollenbeck
|
|
|
|
*/
|
|
|
|
class LIB_ID
|
|
|
|
{
|
|
|
|
public:
|
2018-07-26 14:38:30 +00:00
|
|
|
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.).
|
|
|
|
|
2017-02-11 18:43:41 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2020-12-17 23:32:23 +00:00
|
|
|
* @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
|
2017-02-11 18:43:41 +00:00
|
|
|
* LIB_ID::Parse() function.
|
|
|
|
*/
|
2021-06-30 11:00:06 +00:00
|
|
|
LIB_ID( const wxString& aLibraryName, const wxString& aItemName );
|
2017-02-11 18:43:41 +00:00
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
/**
|
2017-11-23 15:52:28 +00:00
|
|
|
* Parse LIB_ID with the information from @a aId.
|
2016-11-20 23:35:08 +00:00
|
|
|
*
|
2018-07-26 14:38:30 +00:00
|
|
|
* 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"
|
|
|
|
*
|
2016-11-20 23:35:08 +00:00
|
|
|
* @param aId is the string to populate the #LIB_ID object.
|
2018-07-26 14:38:30 +00:00
|
|
|
* @param aFix indicates invalid chars should be replaced with '_'.
|
2016-11-20 23:35:08 +00:00
|
|
|
*
|
2020-12-19 23:29:10 +00:00
|
|
|
* @return minus 1 (i.e. -1) means success, >= 0 indicates the character offset into
|
|
|
|
* aId at which an error was detected.
|
2016-11-20 23:35:08 +00:00
|
|
|
*/
|
2020-12-17 23:32:23 +00:00
|
|
|
int Parse( const UTF8& aId, bool aFix = false );
|
2016-11-20 23:35:08 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-23 15:52:28 +00:00
|
|
|
* Return the logical library name portion of a LIB_ID.
|
2016-11-20 23:35:08 +00:00
|
|
|
*/
|
2020-12-17 23:32:23 +00:00
|
|
|
const UTF8& GetLibNickname() const { return m_libraryName; }
|
2023-01-23 00:25:15 +00:00
|
|
|
const wxString GetUniStringLibNickname() const { return m_libraryName.wx_str(); }
|
2016-11-20 23:35:08 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-23 15:52:28 +00:00
|
|
|
* Override the logical library name portion of the LIB_ID to @a aNickname.
|
2016-11-20 23:35:08 +00:00
|
|
|
*
|
|
|
|
* @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 );
|
|
|
|
|
|
|
|
/**
|
2018-04-29 13:45:11 +00:00
|
|
|
* @return the library item name, i.e. footprintName, in UTF8.
|
2016-11-20 23:35:08 +00:00
|
|
|
*/
|
2020-12-17 23:32:23 +00:00
|
|
|
const UTF8& GetLibItemName() const { return m_itemName; }
|
2016-11-20 23:35:08 +00:00
|
|
|
|
2018-04-29 13:45:11 +00:00
|
|
|
/**
|
2020-12-19 23:29:10 +00:00
|
|
|
* Get strings for display messages in dialogs.
|
|
|
|
*
|
|
|
|
* Equivalent to m_itemName.wx_str(), but more explicit when building a Unicode string
|
|
|
|
* in messages.
|
|
|
|
*
|
2018-04-29 13:45:11 +00:00
|
|
|
* @return the library item name, i.e. footprintName in a wxString (UTF16 or 32).
|
|
|
|
*/
|
2020-12-17 23:32:23 +00:00
|
|
|
const wxString GetUniStringLibItemName() const { return m_itemName.wx_str(); }
|
2018-04-29 13:45:11 +00:00
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
/**
|
2017-11-23 15:52:28 +00:00
|
|
|
* Override the library item name portion of the LIB_ID to @a aLibItemName
|
2016-11-20 23:35:08 +00:00
|
|
|
*
|
|
|
|
* @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 '/'.
|
|
|
|
*/
|
2021-06-30 11:00:06 +00:00
|
|
|
int SetLibItemName( const UTF8& aLibItemName );
|
2016-11-20 23:35:08 +00:00
|
|
|
|
2022-08-27 03:52:56 +00:00
|
|
|
/**
|
|
|
|
* Some LIB_IDs can have a sub-library identifier in addition to a library nickname.
|
|
|
|
* This identifier is *not* part of the canonical LIB_ID and is not written out / parsed.
|
|
|
|
* It is only used for internal sorting/grouping, if present.
|
|
|
|
*
|
|
|
|
* @return the sub-library name for this LIB_ID, if one exists
|
|
|
|
*/
|
|
|
|
UTF8 GetSubLibraryName() const { return m_subLibraryName; }
|
|
|
|
void SetSubLibraryName( const UTF8& aName ) { m_subLibraryName = aName; }
|
2023-01-23 00:25:15 +00:00
|
|
|
const wxString GetUniStringSubLibraryName() const { return m_subLibraryName.wx_str(); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a display-formatted name of the library and sublibrary (if present)
|
|
|
|
*/
|
|
|
|
const wxString GetFullLibraryName() const;
|
2022-08-27 03:52:56 +00:00
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
/**
|
2018-04-29 13:45:11 +00:00
|
|
|
* @return the fully formatted text of the LIB_ID in a UTF8 string.
|
2016-11-20 23:35:08 +00:00
|
|
|
*/
|
|
|
|
UTF8 Format() const;
|
|
|
|
|
2018-04-29 13:45:11 +00:00
|
|
|
/**
|
|
|
|
* @return the fully formatted text of the LIB_ID in a wxString (UTF16 or UTF32),
|
2020-12-19 23:29:10 +00:00
|
|
|
* suitable to display the LIB_ID in dialogs.
|
2018-04-29 13:45:11 +00:00
|
|
|
*/
|
|
|
|
wxString GetUniStringLibId() const
|
|
|
|
{
|
|
|
|
return Format().wx_str();
|
|
|
|
}
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
/**
|
|
|
|
* @return a string in the proper format as an LIB_ID for a combination of
|
2022-08-18 19:49:40 +00:00
|
|
|
* aLibraryName, aLibItemName
|
2016-11-20 23:35:08 +00:00
|
|
|
*
|
|
|
|
* @throw PARSE_ERROR if any of the pieces are illegal.
|
2017-04-16 11:53:56 +00:00
|
|
|
*/
|
2021-06-30 11:00:06 +00:00
|
|
|
static UTF8 Format( const UTF8& aLibraryName, const UTF8& aLibItemName );
|
2016-11-20 23:35:08 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-19 23:29:10 +00:00
|
|
|
* Check if this LID_ID is valid.
|
2016-11-20 23:35:08 +00:00
|
|
|
*
|
|
|
|
* A valid #LIB_ID must have both the library nickname and the library item name defined.
|
|
|
|
*
|
|
|
|
* @note A return value of true does not indicated that the #LIB_ID is a valid #LIB_TABLE
|
|
|
|
* entry.
|
2020-12-19 23:29:10 +00:00
|
|
|
*
|
|
|
|
* @return true is the #LIB_ID is valid.
|
|
|
|
*
|
2016-11-20 23:35:08 +00:00
|
|
|
*/
|
2020-12-17 23:32:23 +00:00
|
|
|
bool IsValid() const
|
|
|
|
{
|
|
|
|
return !m_libraryName.empty() && !m_itemName.empty();
|
|
|
|
}
|
2016-11-20 23:35:08 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-17 23:32:23 +00:00
|
|
|
* @return true if the #LIB_ID only has the #m_itemName name defined.
|
2016-11-20 23:35:08 +00:00
|
|
|
*/
|
2020-12-17 23:32:23 +00:00
|
|
|
bool IsLegacy() const
|
|
|
|
{
|
2021-06-30 11:00:06 +00:00
|
|
|
return m_libraryName.empty() && !m_itemName.empty();
|
2020-12-17 23:32:23 +00:00
|
|
|
}
|
2016-11-20 23:35:08 +00:00
|
|
|
|
|
|
|
/**
|
2022-08-18 19:49:40 +00:00
|
|
|
* Clear the contents of the library nickname, library entry name
|
2016-11-20 23:35:08 +00:00
|
|
|
*/
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a boolean true value if the LIB_ID is empty. Otherwise return false.
|
|
|
|
*/
|
2020-12-17 23:32:23 +00:00
|
|
|
bool empty() const
|
|
|
|
{
|
2021-06-30 11:00:06 +00:00
|
|
|
return m_libraryName.empty() && m_itemName.empty();
|
2020-12-17 23:32:23 +00:00
|
|
|
}
|
2016-11-20 23:35:08 +00:00
|
|
|
|
|
|
|
/**
|
2017-06-13 23:47:05 +00:00
|
|
|
* Compare the contents of LIB_ID objects by performing a std::string comparison of the
|
2022-08-18 19:49:40 +00:00
|
|
|
* library nickname, library entry name
|
2016-11-20 23:35:08 +00:00
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
2017-06-13 23:47:05 +00:00
|
|
|
int compare( const LIB_ID& aLibId ) const;
|
2016-11-20 23:35:08 +00:00
|
|
|
|
|
|
|
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); }
|
|
|
|
|
2017-11-23 15:52:28 +00:00
|
|
|
/**
|
|
|
|
* Examine \a aLibItemName for invalid #LIB_ID item name characters.
|
|
|
|
*
|
|
|
|
* @param aLibItemName is the #LIB_ID name to test for illegal characters.
|
2018-07-26 14:38:30 +00:00
|
|
|
* @return offset of first illegal character otherwise -1.
|
2017-11-23 15:52:28 +00:00
|
|
|
*/
|
2020-12-17 23:32:23 +00:00
|
|
|
static int HasIllegalChars( const UTF8& aLibItemName );
|
2017-11-23 15:52:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace illegal #LIB_ID item name characters with underscores '_'.
|
|
|
|
*
|
|
|
|
* @param aLibItemName is the #LIB_ID item name to replace illegal characters.
|
2018-06-21 04:12:11 +00:00
|
|
|
* @param aLib True if we are checking library names, false if we are checking item names
|
2017-11-23 15:52:28 +00:00
|
|
|
* @return the corrected version of \a aLibItemName.
|
|
|
|
*/
|
2021-06-30 10:53:04 +00:00
|
|
|
static UTF8 FixIllegalChars( const UTF8& aLibItemName, bool aLib );
|
2018-04-13 09:26:28 +00:00
|
|
|
|
|
|
|
/**
|
2018-06-20 18:34:06 +00:00
|
|
|
* Looks for characters that are illegal in library nicknames.
|
2018-04-13 09:26:28 +00:00
|
|
|
*
|
2020-12-17 23:32:23 +00:00
|
|
|
* @param aLibraryName is the logical library name to be tested.
|
2018-04-13 09:26:28 +00:00
|
|
|
* @return Invalid character found in the name or 0 is the name is valid.
|
|
|
|
*/
|
2020-12-17 23:32:23 +00:00
|
|
|
static unsigned FindIllegalLibraryNameChar( const UTF8& aLibraryName );
|
2017-11-23 15:52:28 +00:00
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
protected:
|
2018-06-20 18:34:06 +00:00
|
|
|
/**
|
2020-12-19 23:29:10 +00:00
|
|
|
* Tests whether a Unicode character is a legal LIB_ID item name character.
|
2018-10-07 13:08:30 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2020-12-17 23:32:23 +00:00
|
|
|
* - Spaces are now also allowed in symbol library nicknames since they are quoted in
|
|
|
|
* the new symbol library sexpr file format.
|
2018-10-07 13:08:30 +00:00
|
|
|
* - 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.
|
|
|
|
*
|
2018-06-20 18:34:06 +00:00
|
|
|
*
|
2020-12-19 23:29:10 +00:00
|
|
|
* @note @a aUniChar is expected to be a 32 bit Unicode character, not a UTF8 char, that use
|
|
|
|
* a variable length coding value.
|
2018-04-30 09:44:35 +00:00
|
|
|
*/
|
2020-12-17 23:32:23 +00:00
|
|
|
static bool isLegalChar( unsigned aUniChar );
|
2018-04-13 09:26:28 +00:00
|
|
|
|
2018-06-20 18:34:06 +00:00
|
|
|
/**
|
2020-12-19 23:29:10 +00:00
|
|
|
* Tests whether a Unicode character is a legal LIB_ID library nickname character
|
2018-06-20 18:34:06 +00:00
|
|
|
*
|
2020-12-19 23:29:10 +00:00
|
|
|
* @note @a aUniChar is expected to be a 32 bit Unicode character, not a UTF8 char, that use
|
|
|
|
* a variable length coding value.
|
2018-06-20 18:34:06 +00:00
|
|
|
*/
|
2020-12-17 23:32:23 +00:00
|
|
|
static bool isLegalLibraryNameChar( unsigned aUniChar );
|
2018-06-20 18:34:06 +00:00
|
|
|
|
2020-12-17 23:32:23 +00:00
|
|
|
UTF8 m_libraryName; ///< The nickname of the library or empty.
|
|
|
|
UTF8 m_itemName; ///< The name of the entry in the logical library.
|
2022-08-27 03:52:56 +00:00
|
|
|
UTF8 m_subLibraryName; ///< Optional sub-library name used for grouping within a library
|
2016-11-20 23:35:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _LIB_ID_H_
|