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>
|
|
|
|
* Copyright (C) 2010-2017 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>
|
2012-10-15 01:38:32 +00:00
|
|
|
#include <wx/wx.h> // _()
|
|
|
|
|
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>
|
2015-08-23 12:35:49 +00:00
|
|
|
#include <kicad_string.h>
|
2012-10-15 01:38:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
static inline bool isDigit( char c )
|
|
|
|
{
|
|
|
|
return c >= '0' && c <= '9';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* EndsWithRev( const char* start, const char* tail, char separator )
|
|
|
|
{
|
|
|
|
bool sawDigit = false;
|
|
|
|
|
|
|
|
while( tail > start && isDigit( *--tail ) )
|
|
|
|
{
|
|
|
|
sawDigit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if sawDigit, tail points to the 'v' here.
|
|
|
|
|
|
|
|
if( sawDigit && tail-3 >= start )
|
|
|
|
{
|
|
|
|
tail -= 3;
|
|
|
|
|
|
|
|
if( tail[0]==separator && tail[1]=='r' && tail[2]=='e' && tail[3]=='v' )
|
|
|
|
{
|
|
|
|
return tail+1; // omit separator, return "revN[N..]"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Modular-Kicad milestone B), major portions:
*) Rework the set language support, simplify it by using KIWAY. Now any major
frame with a "change language" menu can change the language for all KIWAY_PLAYERs
in the whole KIWAY. Multiple KIWAYs are not supported yet.
*) Simplify "modal wxFrame" support, and add that support exclusively to
KIWAY_PLAYER where it is inherited by all derivatives. The function
KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable.
*) Remove the requirements and assumptions that the wxFrame hierarchy always
had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers
and editors. This is no longer the case, nor required.
*) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the
KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame
quickly. It also gives control to the KIWAY as to frame hierarchical
relationships.
*) Change single_top to use the KIWAY for loading a KIFACE and instantiating
the single KIWAY_PLAYER, see bullet immediately above.
*) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this
gives the KIFACEs a chance to save their final configuration dope to disk.
*) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and
these modal frames are distinctly different than their non-modal equivalents.
KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor,
so this is another important reason for having a dedicated FRAME_T for each
modal wxFrame.
On balance, more lines were deleted than were added to achieve all this.
2014-05-03 17:40:19 +00:00
|
|
|
#if 0 // Not used
|
2012-10-15 01:38:32 +00:00
|
|
|
int RevCmp( const char* s1, const char* s2 )
|
|
|
|
{
|
|
|
|
int r = strncmp( s1, s2, 3 );
|
|
|
|
|
|
|
|
if( r || strlen(s1)<4 || strlen(s2)<4 )
|
|
|
|
{
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rnum1 = atoi( s1+3 );
|
|
|
|
int rnum2 = atoi( s2+3 );
|
|
|
|
|
|
|
|
return -(rnum1 - rnum2); // swap the sign, higher revs first
|
|
|
|
}
|
Modular-Kicad milestone B), major portions:
*) Rework the set language support, simplify it by using KIWAY. Now any major
frame with a "change language" menu can change the language for all KIWAY_PLAYERs
in the whole KIWAY. Multiple KIWAYs are not supported yet.
*) Simplify "modal wxFrame" support, and add that support exclusively to
KIWAY_PLAYER where it is inherited by all derivatives. The function
KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable.
*) Remove the requirements and assumptions that the wxFrame hierarchy always
had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers
and editors. This is no longer the case, nor required.
*) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the
KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame
quickly. It also gives control to the KIWAY as to frame hierarchical
relationships.
*) Change single_top to use the KIWAY for loading a KIFACE and instantiating
the single KIWAY_PLAYER, see bullet immediately above.
*) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this
gives the KIFACEs a chance to save their final configuration dope to disk.
*) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and
these modal frames are distinctly different than their non-modal equivalents.
KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor,
so this is another important reason for having a dedicated FRAME_T for each
modal wxFrame.
On balance, more lines were deleted than were added to achieve all this.
2014-05-03 17:40:19 +00:00
|
|
|
#endif
|
2012-10-15 01:38:32 +00:00
|
|
|
|
|
|
|
//----<Policy and field test functions>-------------------------------------
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-25 19:14:31 +00:00
|
|
|
static int okRevision( const UTF8& aField )
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
2018-03-06 13:25:18 +00:00
|
|
|
char rev[32]; // C string for speed
|
2012-10-15 01:38:32 +00:00
|
|
|
|
|
|
|
if( aField.size() >= 4 )
|
|
|
|
{
|
2018-03-06 13:25:18 +00:00
|
|
|
strncpy( rev, "x/", sizeof( rev ) );
|
2015-08-10 08:17:22 +00:00
|
|
|
strncat( rev, aField.c_str(), sizeof(rev)-strlen(rev)-1 );
|
2012-10-15 01:38:32 +00:00
|
|
|
|
2013-02-22 20:03:00 +00:00
|
|
|
if( EndsWithRev( rev, rev + strlen(rev), '/' ) == rev+2 )
|
2012-10-15 01:38:32 +00:00
|
|
|
return -1; // success
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; // first character position "is in error", is best we can do.
|
|
|
|
}
|
|
|
|
|
2012-10-15 20:48:01 +00:00
|
|
|
|
2012-10-15 01:38:32 +00:00
|
|
|
//----</Policy and field test functions>-------------------------------------
|
|
|
|
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
void LIB_ID::clear()
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
2013-02-22 20:03:00 +00:00
|
|
|
nickname.clear();
|
2016-11-20 23:35:08 +00:00
|
|
|
item_name.clear();
|
2012-10-15 01:38:32 +00:00
|
|
|
revision.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
int LIB_ID::Parse( const UTF8& aId )
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
2015-08-23 12:35:49 +00:00
|
|
|
const char* buffer = aId.c_str();
|
|
|
|
const char* rev = EndsWithRev( buffer, buffer+aId.length(), '/' );
|
2012-10-15 01:38:32 +00:00
|
|
|
size_t revNdx;
|
|
|
|
size_t partNdx;
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
//=====<revision>=========================================
|
2016-11-20 23:35:08 +00:00
|
|
|
// in a LIB_ID like discret:R3/rev4
|
2012-10-15 01:38:32 +00:00
|
|
|
if( rev )
|
|
|
|
{
|
2015-08-23 12:35:49 +00:00
|
|
|
revNdx = rev - buffer;
|
2012-10-15 01:38:32 +00:00
|
|
|
|
|
|
|
// no need to check revision, EndsWithRev did that.
|
|
|
|
revision = aId.substr( revNdx );
|
2012-10-15 20:48:01 +00:00
|
|
|
--revNdx; // back up to omit the '/' which precedes the rev
|
2012-10-15 01:38:32 +00:00
|
|
|
}
|
|
|
|
else
|
2012-10-15 20:48:01 +00:00
|
|
|
{
|
2012-10-15 01:38:32 +00:00
|
|
|
revNdx = aId.size();
|
2012-10-15 20:48:01 +00:00
|
|
|
}
|
2012-10-15 01:38:32 +00:00
|
|
|
|
2013-02-22 20:03:00 +00:00
|
|
|
//=====<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>====================================
|
2013-02-22 20:03:00 +00:00
|
|
|
if( partNdx >= revNdx )
|
2016-11-20 23:35:08 +00:00
|
|
|
return partNdx; // Error: no library item name.
|
2013-02-22 20:03:00 +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.
|
2015-08-23 12:35:49 +00:00
|
|
|
std::string fpname = aId.substr( partNdx, revNdx-partNdx );
|
|
|
|
ReplaceIllegalFileNameChars( &fpname, '_' );
|
2017-07-24 19:02:59 +00:00
|
|
|
SetLibItemName( UTF8( fpname ) );
|
2013-02-22 20:03:00 +00:00
|
|
|
|
2012-10-15 01:38:32 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-25 19:14:31 +00:00
|
|
|
LIB_ID::LIB_ID( const UTF8& aId )
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
|
|
|
int offset = Parse( aId );
|
|
|
|
|
|
|
|
if( offset != -1 )
|
|
|
|
{
|
2016-11-20 23:35:08 +00:00
|
|
|
THROW_PARSE_ERROR( _( "Illegal character found in LIB_ID string" ),
|
2012-10-15 01:38:32 +00:00
|
|
|
wxString::FromUTF8( aId.c_str() ),
|
|
|
|
aId.c_str(),
|
|
|
|
0,
|
|
|
|
offset );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-08 21:47:21 +00:00
|
|
|
LIB_ID::LIB_ID( const wxString& aId )
|
2013-09-08 18:31:21 +00:00
|
|
|
{
|
2017-07-24 19:02:59 +00:00
|
|
|
UTF8 id = aId;
|
2013-09-08 18:31:21 +00:00
|
|
|
|
|
|
|
int offset = Parse( id );
|
|
|
|
|
|
|
|
if( offset != -1 )
|
|
|
|
{
|
2016-11-20 23:35:08 +00:00
|
|
|
THROW_PARSE_ERROR( _( "Illegal character found in LIB_ID string" ),
|
Modular-Kicad milestone B), major portions:
*) Rework the set language support, simplify it by using KIWAY. Now any major
frame with a "change language" menu can change the language for all KIWAY_PLAYERs
in the whole KIWAY. Multiple KIWAYs are not supported yet.
*) Simplify "modal wxFrame" support, and add that support exclusively to
KIWAY_PLAYER where it is inherited by all derivatives. The function
KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable.
*) Remove the requirements and assumptions that the wxFrame hierarchy always
had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers
and editors. This is no longer the case, nor required.
*) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the
KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame
quickly. It also gives control to the KIWAY as to frame hierarchical
relationships.
*) Change single_top to use the KIWAY for loading a KIFACE and instantiating
the single KIWAY_PLAYER, see bullet immediately above.
*) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this
gives the KIFACEs a chance to save their final configuration dope to disk.
*) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and
these modal frames are distinctly different than their non-modal equivalents.
KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor,
so this is another important reason for having a dedicated FRAME_T for each
modal wxFrame.
On balance, more lines were deleted than were added to achieve all this.
2014-05-03 17:40:19 +00:00
|
|
|
aId,
|
2013-09-08 18:31:21 +00:00
|
|
|
id.c_str(),
|
|
|
|
0,
|
|
|
|
offset );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-11 18:43:41 +00:00
|
|
|
LIB_ID::LIB_ID( const wxString& aLibName, const wxString& aLibItemName,
|
|
|
|
const wxString& aRevision ) :
|
2017-07-24 19:02:59 +00:00
|
|
|
nickname( aLibName ),
|
|
|
|
item_name( aLibItemName ),
|
|
|
|
revision( aRevision )
|
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 )
|
|
|
|
{
|
2013-02-22 20:03:00 +00:00
|
|
|
nickname = aLogical;
|
2012-10-15 01:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-11 18:43:41 +00:00
|
|
|
int LIB_ID::SetLibItemName( const UTF8& aLibItemName, bool aTestForRev )
|
2013-09-08 18:31:21 +00:00
|
|
|
{
|
2016-11-20 23:35:08 +00:00
|
|
|
int separation = int( aLibItemName.find_first_of( "/" ) );
|
2012-10-15 01:38:32 +00:00
|
|
|
|
2017-02-11 18:43:41 +00:00
|
|
|
if( aTestForRev && separation != -1 )
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
2016-11-20 23:35:08 +00:00
|
|
|
item_name = aLibItemName.substr( 0, separation-1 );
|
2015-08-23 12:35:49 +00:00
|
|
|
return separation;
|
2012-10-15 01:38:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-20 23:35:08 +00:00
|
|
|
item_name = aLibItemName;
|
2012-10-15 01:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
int LIB_ID::SetRevision( const UTF8& aRevision )
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
|
|
|
int offset = okRevision( aRevision );
|
|
|
|
|
|
|
|
if( offset == -1 )
|
|
|
|
{
|
|
|
|
revision = aRevision;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-02-22 20:03:00 +00:00
|
|
|
if( nickname.size() )
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
2013-02-22 20:03:00 +00:00
|
|
|
ret += nickname;
|
2012-10-15 01:38:32 +00:00
|
|
|
ret += ':';
|
|
|
|
}
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
ret += item_name;
|
2013-02-22 20:03:00 +00:00
|
|
|
|
2012-10-15 01:38:32 +00:00
|
|
|
if( revision.size() )
|
|
|
|
{
|
|
|
|
ret += '/';
|
|
|
|
ret += revision;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
UTF8 LIB_ID::GetLibItemNameAndRev() 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
|
|
|
|
|
|
|
if( revision.size() )
|
|
|
|
{
|
|
|
|
ret += '/';
|
|
|
|
ret += revision;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
UTF8 LIB_ID::Format( const UTF8& aLogicalLib, const UTF8& aLibItemName, const UTF8& aRevision )
|
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;
|
|
|
|
|
|
|
|
if( aLogicalLib.size() )
|
|
|
|
{
|
|
|
|
offset = okLogical( aLogicalLib );
|
|
|
|
|
|
|
|
if( offset != -1 )
|
|
|
|
{
|
2012-10-15 20:48:01 +00:00
|
|
|
THROW_PARSE_ERROR( _( "Illegal character found in logical library name" ),
|
2012-10-15 01:38:32 +00:00
|
|
|
wxString::FromUTF8( aLogicalLib.c_str() ),
|
2017-04-16 11:53:56 +00:00
|
|
|
aLogicalLib.c_str(), 0, offset );
|
2012-10-15 01:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret += aLogicalLib;
|
|
|
|
ret += ':';
|
|
|
|
}
|
|
|
|
|
2017-04-16 11:53:56 +00:00
|
|
|
ret += aLibItemName; // TODO: Add validity test.
|
|
|
|
|
2012-10-15 01:38:32 +00:00
|
|
|
if( aRevision.size() )
|
|
|
|
{
|
|
|
|
offset = okRevision( aRevision );
|
|
|
|
|
|
|
|
if( offset != -1 )
|
|
|
|
{
|
|
|
|
THROW_PARSE_ERROR( _( "Illegal character found in revision" ),
|
|
|
|
wxString::FromUTF8( aRevision.c_str() ),
|
|
|
|
aRevision.c_str(),
|
|
|
|
0,
|
|
|
|
offset );
|
|
|
|
}
|
|
|
|
|
|
|
|
ret += '/';
|
|
|
|
ret += aRevision;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
int retv = nickname.compare( aLibId.nickname );
|
2013-04-25 16:29:35 +00:00
|
|
|
|
|
|
|
if( retv != 0 )
|
|
|
|
return retv;
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
retv = item_name.compare( aLibId.item_name );
|
2013-04-25 16:29:35 +00:00
|
|
|
|
|
|
|
if( retv != 0 )
|
|
|
|
return retv;
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
return revision.compare( aLibId.revision );
|
2013-04-25 16:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-13 09:26:28 +00:00
|
|
|
bool LIB_ID::HasIllegalChars( const UTF8& aLibItemName, LIB_ID_TYPE aType )
|
2017-11-23 15:52:28 +00:00
|
|
|
{
|
|
|
|
for( auto ch : aLibItemName )
|
|
|
|
{
|
2018-04-13 09:26:28 +00:00
|
|
|
if( !isLegalChar( ch, aType ) )
|
2017-11-23 15:52:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-13 09:26:28 +00:00
|
|
|
UTF8 LIB_ID::FixIllegalChars( const UTF8& aLibItemName, LIB_ID_TYPE aType )
|
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-04-13 09:26:28 +00:00
|
|
|
fixedName += isLegalChar( ch, aType ) ? ch : '_';
|
2017-11-23 15:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fixedName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-13 09:26:28 +00:00
|
|
|
unsigned LIB_ID::FindIllegalChar( const UTF8& aNickname, LIB_ID_TYPE aType )
|
|
|
|
{
|
|
|
|
for( unsigned ch : aNickname )
|
|
|
|
{
|
|
|
|
if( !isLegalChar( ch, aType ) )
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///> Set of characters not accepted in library and entry names
|
|
|
|
#define BASE_ILLEGAL_CHARS '\t', '\n', '\r', ':', '/', '\\'
|
|
|
|
const unsigned schIllegalChars[] = { BASE_ILLEGAL_CHARS, ' ' };
|
|
|
|
const unsigned pcbIllegalChars[] = { BASE_ILLEGAL_CHARS, 0 };
|
|
|
|
#define ILL_CHAR_SIZE (sizeof(schIllegalChars) / sizeof(int))
|
|
|
|
|
|
|
|
bool LIB_ID::isLegalChar( unsigned aChar, LIB_ID_TYPE aType )
|
|
|
|
{
|
|
|
|
const unsigned (&illegalChars)[ILL_CHAR_SIZE] =
|
|
|
|
aType == ID_SCH ? schIllegalChars : pcbIllegalChars;
|
|
|
|
|
|
|
|
for( const unsigned ch : illegalChars )
|
|
|
|
{
|
|
|
|
if( ch == aChar )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !wxIsascii( aChar ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-15 01:38:32 +00:00
|
|
|
#if 0 && defined(DEBUG)
|
|
|
|
|
|
|
|
// build this with Debug CMAKE_BUILD_TYPE
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
void LIB_ID::Test()
|
2012-10-15 01:38:32 +00:00
|
|
|
{
|
|
|
|
static const char* lpids[] = {
|
2012-10-15 20:48:01 +00:00
|
|
|
"smt:R_0805/rev0",
|
|
|
|
"mysmt:R_0805/rev2",
|
|
|
|
"device:AXIAL-0500",
|
2012-10-15 01:38:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for( unsigned i=0; i<sizeof(lpids)/sizeof(lpids[0]); ++i )
|
|
|
|
{
|
|
|
|
// test some round tripping
|
|
|
|
|
2016-11-20 23:35:08 +00:00
|
|
|
LIB_ID lpid( lpids[i] ); // parse
|
2012-10-15 01:38:32 +00:00
|
|
|
|
|
|
|
// format
|
2016-11-20 23:35:08 +00:00
|
|
|
printf( "input:'%s' full:'%s' nickname: %s item_name:'%s' rev:'%s'\n",
|
2012-10-15 01:38:32 +00:00
|
|
|
lpids[i],
|
|
|
|
lpid.Format().c_str(),
|
2013-02-22 20:03:00 +00:00
|
|
|
lpid.GetLibNickname().c_str(),
|
2016-11-20 23:35:08 +00:00
|
|
|
lpid.GetLibItemName().c_str(),
|
2012-10-15 01:38:32 +00:00
|
|
|
lpid.GetRevision().c_str() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main( int argc, char** argv )
|
|
|
|
{
|
2016-11-20 23:35:08 +00:00
|
|
|
LIB_ID::Test();
|
2012-10-15 01:38:32 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|