2012-10-15 01:38:32 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 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.
|
2012-10-15 01:38:32 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstring>
|
2013-02-22 20:03:00 +00:00
|
|
|
#include <memory>
|
2021-06-07 22:20:47 +00:00
|
|
|
#include <wx/translation.h>
|
2012-10-15 01:38:32 +00:00
|
|
|
|
2013-09-08 18:31:21 +00:00
|
|
|
#include <macros.h> // TO_UTF8()
|
2016-11-20 23:35:08 +00:00
|
|
|
#include <lib_id.h>
|
2012-10-15 01:38:32 +00:00
|
|
|
|
|
|
|
|
2017-07-25 19:14:31 +00:00
|
|
|
static inline int okLogical( const UTF8& aField )
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
|
|
|
// std::string::npos is largest positive number, casting to int makes it -1.
|
|
|
|
// Returning that means success.
|
2012-10-15 20:48:01 +00:00
|
|
|
return int( aField.find_first_of( ":" ) );
|
2012-10-15 01:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
void LIB_ID::clear()
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
2020-12-17 23:32:23 +00:00
|
|
|
m_libraryName.clear();
|
|
|
|
m_itemName.clear();
|
2022-08-27 03:52:56 +00:00
|
|
|
m_subLibraryName.clear();
|
2012-10-15 01:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-17 23:32:23 +00:00
|
|
|
int LIB_ID::Parse( const UTF8& aId, bool aFix )
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
2021-06-30 11:00:06 +00:00
|
|
|
size_t partNdx;
|
|
|
|
int offset = -1;
|
2012-10-15 01:38:32 +00:00
|
|
|
|
2021-06-30 11:00:06 +00:00
|
|
|
//=====<library nickname>=============================
|
2012-10-15 01:38:32 +00:00
|
|
|
if( ( partNdx = aId.find( ':' ) ) != aId.npos )
|
|
|
|
{
|
2013-02-22 20:03:00 +00:00
|
|
|
offset = SetLibNickname( aId.substr( 0, partNdx ) );
|
2012-10-15 20:48:01 +00:00
|
|
|
|
2012-10-15 01:38:32 +00:00
|
|
|
if( offset > -1 )
|
|
|
|
return offset;
|
2012-10-15 20:48:01 +00:00
|
|
|
|
2012-10-15 01:38:32 +00:00
|
|
|
++partNdx; // skip ':'
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-15 20:48:01 +00:00
|
|
|
partNdx = 0;
|
2012-10-15 01:38:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
//=====<item name>====================================
|
2021-06-30 11:00:06 +00:00
|
|
|
UTF8 fpname = aId.substr( partNdx );
|
2018-07-26 14:38:30 +00:00
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
// Be sure the item name is valid.
|
2018-04-08 10:28:59 +00:00
|
|
|
// Some chars can be found in legacy files converted files from other EDA tools.
|
2018-07-26 14:38:30 +00:00
|
|
|
if( aFix )
|
2020-12-17 23:32:23 +00:00
|
|
|
fpname = FixIllegalChars( fpname, false );
|
2018-07-26 14:38:30 +00:00
|
|
|
else
|
2020-12-17 23:32:23 +00:00
|
|
|
offset = HasIllegalChars( fpname );
|
2012-10-15 01:38:32 +00:00
|
|
|
|
2018-07-26 14:38:30 +00:00
|
|
|
if( offset > -1 )
|
|
|
|
return offset;
|
2013-09-08 18:31:21 +00:00
|
|
|
|
2018-07-26 14:38:30 +00:00
|
|
|
SetLibItemName( fpname );
|
2013-09-08 18:31:21 +00:00
|
|
|
|
2018-07-26 14:38:30 +00:00
|
|
|
return -1;
|
2013-09-08 18:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-30 11:00:06 +00:00
|
|
|
LIB_ID::LIB_ID( const wxString& aLibraryName, const wxString& aItemName ) :
|
2020-12-17 23:32:23 +00:00
|
|
|
m_libraryName( aLibraryName ),
|
2021-06-30 11:00:06 +00:00
|
|
|
m_itemName( aItemName )
|
2017-02-11 18:43:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
int LIB_ID::SetLibNickname( const UTF8& aLogical )
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
|
|
|
int offset = okLogical( aLogical );
|
|
|
|
|
|
|
|
if( offset == -1 )
|
2020-12-17 23:32:23 +00:00
|
|
|
m_libraryName = aLogical;
|
2012-10-15 01:38:32 +00:00
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-30 11:00:06 +00:00
|
|
|
int LIB_ID::SetLibItemName( const UTF8& aLibItemName )
|
2013-09-08 18:31:21 +00:00
|
|
|
{
|
2021-06-30 11:00:06 +00:00
|
|
|
m_itemName = aLibItemName;
|
2012-10-15 01:38:32 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
UTF8 LIB_ID::Format() const
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
2014-01-02 02:17:07 +00:00
|
|
|
UTF8 ret;
|
2012-10-15 01:38:32 +00:00
|
|
|
|
2020-12-17 23:32:23 +00:00
|
|
|
if( m_libraryName.size() )
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
2020-12-17 23:32:23 +00:00
|
|
|
ret += m_libraryName;
|
2012-10-15 01:38:32 +00:00
|
|
|
ret += ':';
|
|
|
|
}
|
|
|
|
|
2020-12-17 23:32:23 +00:00
|
|
|
ret += m_itemName;
|
2013-02-22 20:03:00 +00:00
|
|
|
|
2012-10-15 01:38:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-30 11:00:06 +00:00
|
|
|
UTF8 LIB_ID::Format( const UTF8& aLibraryName, const UTF8& aLibItemName )
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
2014-01-02 02:17:07 +00:00
|
|
|
UTF8 ret;
|
2012-10-15 01:38:32 +00:00
|
|
|
int offset;
|
|
|
|
|
2020-12-17 23:32:23 +00:00
|
|
|
if( aLibraryName.size() )
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
2020-12-17 23:32:23 +00:00
|
|
|
offset = okLogical( aLibraryName );
|
2012-10-15 01:38:32 +00:00
|
|
|
|
|
|
|
if( offset != -1 )
|
|
|
|
{
|
2012-10-15 20:48:01 +00:00
|
|
|
THROW_PARSE_ERROR( _( "Illegal character found in logical library name" ),
|
2021-06-30 11:00:06 +00:00
|
|
|
wxString::FromUTF8( aLibraryName.c_str() ), aLibraryName.c_str(),
|
|
|
|
0, offset );
|
2012-10-15 01:38:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 23:32:23 +00:00
|
|
|
ret += aLibraryName;
|
2012-10-15 01:38:32 +00:00
|
|
|
ret += ':';
|
|
|
|
}
|
|
|
|
|
2017-04-16 11:53:56 +00:00
|
|
|
ret += aLibItemName; // TODO: Add validity test.
|
|
|
|
|
2012-10-15 01:38:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
int LIB_ID::compare( const LIB_ID& aLibId ) const
|
2013-04-25 16:29:35 +00:00
|
|
|
{
|
|
|
|
// Don't bother comparing the same object.
|
2016-11-20 23:35:08 +00:00
|
|
|
if( this == &aLibId )
|
2013-04-25 16:29:35 +00:00
|
|
|
return 0;
|
|
|
|
|
2020-12-17 23:32:23 +00:00
|
|
|
int retv = m_libraryName.compare( aLibId.m_libraryName );
|
2013-04-25 16:29:35 +00:00
|
|
|
|
|
|
|
if( retv != 0 )
|
|
|
|
return retv;
|
|
|
|
|
2021-06-30 11:00:06 +00:00
|
|
|
return m_itemName.compare( aLibId.m_itemName );
|
2013-04-25 16:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-17 23:32:23 +00:00
|
|
|
int LIB_ID::HasIllegalChars( const UTF8& aLibItemName )
|
2017-11-23 15:52:28 +00:00
|
|
|
{
|
2018-07-26 14:38:30 +00:00
|
|
|
int offset = 0;
|
|
|
|
|
2022-08-15 17:47:23 +00:00
|
|
|
for( auto& ch : aLibItemName )
|
2017-11-23 15:52:28 +00:00
|
|
|
{
|
2020-12-17 23:32:23 +00:00
|
|
|
if( !isLegalChar( ch ) )
|
2018-07-26 14:38:30 +00:00
|
|
|
return offset;
|
|
|
|
else
|
|
|
|
++offset;
|
2017-11-23 15:52:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 14:38:30 +00:00
|
|
|
return -1;
|
2017-11-23 15:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-17 23:32:23 +00:00
|
|
|
UTF8 LIB_ID::FixIllegalChars( const UTF8& aLibItemName, bool aLib )
|
2017-11-23 15:52:28 +00:00
|
|
|
{
|
|
|
|
UTF8 fixedName;
|
|
|
|
|
2017-12-14 09:37:32 +00:00
|
|
|
for( UTF8::uni_iter chIt = aLibItemName.ubegin(); chIt < aLibItemName.uend(); ++chIt )
|
2017-11-23 15:52:28 +00:00
|
|
|
{
|
2017-12-14 09:37:32 +00:00
|
|
|
auto ch = *chIt;
|
2018-06-21 04:12:11 +00:00
|
|
|
if( aLib )
|
2020-12-17 23:32:23 +00:00
|
|
|
fixedName += isLegalLibraryNameChar( ch ) ? ch : '_';
|
2018-06-21 04:12:11 +00:00
|
|
|
else
|
2020-12-17 23:32:23 +00:00
|
|
|
fixedName += isLegalChar( ch ) ? ch : '_';
|
2017-11-23 15:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fixedName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-17 23:32:23 +00:00
|
|
|
bool LIB_ID::isLegalChar( unsigned aUniChar )
|
2018-06-20 18:34:06 +00:00
|
|
|
{
|
2020-12-17 23:32:23 +00:00
|
|
|
bool const space_allowed = true;
|
|
|
|
bool const illegal_filename_chars_allowed = false;
|
2018-06-20 18:34:06 +00:00
|
|
|
|
|
|
|
if( aUniChar < ' ' )
|
|
|
|
return false;
|
|
|
|
|
2020-11-13 11:17:15 +00:00
|
|
|
// This list of characters is also duplicated in validators.cpp and footprint.cpp
|
2022-08-18 19:49:40 +00:00
|
|
|
// TODO: Unify forbidden character lists - Warning, invalid filename characters are not the same
|
|
|
|
// as invalid LIB_ID characters. We will need to separate the FP filenames from FP names before this
|
|
|
|
// can be unified
|
2018-06-29 07:41:44 +00:00
|
|
|
switch( aUniChar )
|
|
|
|
{
|
2018-10-07 13:08:30 +00:00
|
|
|
case ':':
|
2019-06-29 20:31:11 +00:00
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
2018-10-07 13:08:30 +00:00
|
|
|
return false;
|
|
|
|
|
2018-06-29 07:41:44 +00:00
|
|
|
case '\\':
|
2018-07-26 14:38:30 +00:00
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case '"':
|
|
|
|
return illegal_filename_chars_allowed;
|
2018-06-29 07:41:44 +00:00
|
|
|
|
|
|
|
case ' ':
|
|
|
|
return space_allowed;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-20 18:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-17 23:32:23 +00:00
|
|
|
unsigned LIB_ID::FindIllegalLibraryNameChar( const UTF8& aLibraryName )
|
2018-04-13 09:26:28 +00:00
|
|
|
{
|
2020-12-17 23:32:23 +00:00
|
|
|
for( unsigned ch : aLibraryName )
|
2018-04-13 09:26:28 +00:00
|
|
|
{
|
2020-12-17 23:32:23 +00:00
|
|
|
if( !isLegalLibraryNameChar( ch ) )
|
2018-04-13 09:26:28 +00:00
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-17 23:32:23 +00:00
|
|
|
bool LIB_ID::isLegalLibraryNameChar( unsigned aUniChar )
|
2018-04-13 09:26:28 +00:00
|
|
|
{
|
2020-12-17 23:32:23 +00:00
|
|
|
bool const space_allowed = true;
|
2018-04-13 09:26:28 +00:00
|
|
|
|
2018-04-30 09:44:35 +00:00
|
|
|
if( aUniChar < ' ' )
|
2018-04-13 09:26:28 +00:00
|
|
|
return false;
|
|
|
|
|
2018-06-29 07:41:44 +00:00
|
|
|
switch( aUniChar )
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
case ':':
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
return space_allowed;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-13 09:26:28 +00:00
|
|
|
}
|
|
|
|
|