2014-10-19 22:17:43 +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>
|
2015-04-26 16:32:16 +00:00
|
|
|
* Copyright (C) 2015 KiCad Developers, see CHANGELOG.TXT for contributors.
|
2014-10-19 22:17:43 +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
|
|
|
|
*/
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <template_fieldnames.h>
|
|
|
|
#include <dsnlexer.h>
|
|
|
|
#include <fctsys.h>
|
|
|
|
#include <macros.h>
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2011-01-17 17:26:19 +00:00
|
|
|
using namespace TFIELD_T;
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2015-04-26 16:32:16 +00:00
|
|
|
const wxString TEMPLATE_FIELDNAME::GetDefaultFieldName( int aFieldNdx )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
|
|
|
// Fixed values for the first few default fields used by EESCHEMA
|
2015-04-26 16:32:16 +00:00
|
|
|
// (mandatory fields)
|
|
|
|
switch( aFieldNdx )
|
|
|
|
{
|
2016-05-16 18:59:35 +00:00
|
|
|
case REFERENCE:
|
|
|
|
return _( "Reference" ); // The component reference, R1, C1, etc.
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2016-05-16 18:59:35 +00:00
|
|
|
case VALUE:
|
|
|
|
return _( "Value" ); // The component value + name
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2016-05-16 18:59:35 +00:00
|
|
|
case FOOTPRINT:
|
|
|
|
return _( "Footprint" ); // The footprint for use with Pcbnew
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2016-05-16 18:59:35 +00:00
|
|
|
case DATASHEET:
|
|
|
|
return _( "Datasheet" ); // Link to a datasheet for component
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2016-05-16 18:59:35 +00:00
|
|
|
default:
|
|
|
|
break;
|
2010-06-17 16:30:10 +00:00
|
|
|
}
|
2015-04-26 16:32:16 +00:00
|
|
|
|
|
|
|
// Other fields are use fields, give a default name:
|
|
|
|
wxString fieldName = _( "Field" );
|
|
|
|
fieldName << aFieldNdx;
|
|
|
|
return fieldName;
|
2010-06-17 16:30:10 +00:00
|
|
|
}
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
void TEMPLATE_FIELDNAME::Format( OUTPUTFORMATTER* out, int nestLevel ) const throw( IO_ERROR )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2011-02-02 19:41:35 +00:00
|
|
|
out->Print( nestLevel, "(field (name %s)", out->Quotew( m_Name ).c_str() );
|
2010-06-17 16:30:10 +00:00
|
|
|
|
|
|
|
if( !m_Value.IsEmpty() )
|
2011-02-02 19:41:35 +00:00
|
|
|
out->Print( 0, "(value %s)", out->Quotew( m_Value ).c_str() );
|
2010-06-17 16:30:10 +00:00
|
|
|
|
|
|
|
if( m_Visible )
|
|
|
|
out->Print( 0, " visible" );
|
|
|
|
|
|
|
|
out->Print( 0, ")\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
void TEMPLATE_FIELDNAME::Parse( TEMPLATE_FIELDNAMES_LEXER* in ) throw( IO_ERROR )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2011-01-17 17:26:19 +00:00
|
|
|
T tok;
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2010-06-18 16:00:16 +00:00
|
|
|
in->NeedLEFT(); // begin (name ...)
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
if( (tok = in->NextTok()) != T_name )
|
2010-06-17 16:30:10 +00:00
|
|
|
in->Expecting( T_name );
|
|
|
|
|
2010-06-18 16:00:16 +00:00
|
|
|
in->NeedSYMBOLorNUMBER();
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2011-02-02 15:31:48 +00:00
|
|
|
m_Name = FROM_UTF8( in->CurText() );
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2010-06-18 16:00:16 +00:00
|
|
|
in->NeedRIGHT(); // end (name ...)
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
while( (tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2010-06-18 16:00:16 +00:00
|
|
|
// "visible" has no '(' prefix, "value" does, so T_LEFT is optional.
|
2010-06-17 16:30:10 +00:00
|
|
|
if( tok == T_LEFT )
|
2010-08-09 02:03:16 +00:00
|
|
|
tok = in->NextTok();
|
2010-06-17 16:30:10 +00:00
|
|
|
|
|
|
|
switch( tok )
|
|
|
|
{
|
|
|
|
case T_value:
|
2010-06-18 16:00:16 +00:00
|
|
|
in->NeedSYMBOLorNUMBER();
|
2011-02-02 15:31:48 +00:00
|
|
|
m_Value = FROM_UTF8( in->CurText() );
|
2010-06-18 16:00:16 +00:00
|
|
|
in->NeedRIGHT();
|
2010-06-17 16:30:10 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_visible:
|
|
|
|
m_Visible = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-01-19 20:46:07 +00:00
|
|
|
in->Expecting( "value|visible" );
|
2010-06-17 16:30:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
void TEMPLATES::Format( OUTPUTFORMATTER* out, int nestLevel ) const throw( IO_ERROR )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2010-08-09 02:03:16 +00:00
|
|
|
// We'll keep this general, and include the \n, even though the only known
|
|
|
|
// use at this time will not want the newlines or the indentation.
|
2010-06-17 16:30:10 +00:00
|
|
|
out->Print( nestLevel, "(templatefields" );
|
2014-10-19 22:17:43 +00:00
|
|
|
|
2010-06-17 16:30:10 +00:00
|
|
|
for( unsigned i=0; i<m_Fields.size(); ++i )
|
|
|
|
m_Fields[i].Format( out, nestLevel+1 );
|
2014-10-19 22:17:43 +00:00
|
|
|
|
2010-06-17 16:30:10 +00:00
|
|
|
out->Print( 0, ")\n" );
|
|
|
|
}
|
|
|
|
|
2011-01-17 17:26:19 +00:00
|
|
|
|
2010-11-09 15:45:11 +00:00
|
|
|
void TEMPLATES::Parse( TEMPLATE_FIELDNAMES_LEXER* in ) throw( IO_ERROR )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
2011-01-17 17:26:19 +00:00
|
|
|
T tok;
|
2010-06-17 16:30:10 +00:00
|
|
|
|
2012-10-08 19:34:04 +00:00
|
|
|
while( ( tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
|
|
|
if( tok == T_LEFT )
|
2010-08-09 02:03:16 +00:00
|
|
|
tok = in->NextTok();
|
2010-06-17 16:30:10 +00:00
|
|
|
|
|
|
|
switch( tok )
|
|
|
|
{
|
|
|
|
case T_templatefields: // a token indicating class TEMPLATES.
|
|
|
|
|
2010-08-09 02:03:16 +00:00
|
|
|
// Be flexible regarding the starting point of the TEMPLATE_FIELDNAMES_LEXER
|
2010-06-17 16:30:10 +00:00
|
|
|
// stream. Caller may not have read the first two tokens out of the
|
|
|
|
// stream: T_LEFT and T_templatefields, so ignore them if seen here.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_field:
|
|
|
|
{
|
|
|
|
// instantiate on stack, so if exception is thrown,
|
|
|
|
// destructor runs
|
|
|
|
TEMPLATE_FIELDNAME field;
|
|
|
|
|
|
|
|
field.Parse( in );
|
|
|
|
|
|
|
|
// add the field
|
|
|
|
AddTemplateFieldName( field );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-01-19 20:46:07 +00:00
|
|
|
in->Unexpected( in->CurText() );
|
2010-06-17 16:30:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int TEMPLATES::AddTemplateFieldName( const TEMPLATE_FIELDNAME& aFieldName )
|
|
|
|
{
|
|
|
|
// Ensure that the template fieldname does not match a fixed fieldname.
|
|
|
|
for( int i=0; i<MANDATORY_FIELDS; ++i )
|
|
|
|
{
|
2014-10-19 22:17:43 +00:00
|
|
|
if( TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) == aFieldName.m_Name )
|
2010-06-17 16:30:10 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure uniqueness, overwrite any template fieldname by the same name.
|
|
|
|
for( unsigned i=0; i<m_Fields.size(); ++i )
|
|
|
|
{
|
|
|
|
if( m_Fields[i].m_Name == aFieldName.m_Name )
|
|
|
|
{
|
2014-10-19 22:17:43 +00:00
|
|
|
// DBG( printf( "inserting template fieldname:'%s' at %d\n",
|
|
|
|
// TO_UTF8( aFieldName.m_Name ), i ); )
|
2010-06-17 16:30:10 +00:00
|
|
|
|
|
|
|
m_Fields[i] = aFieldName;
|
|
|
|
return i; // return the container index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 15:07:46 +00:00
|
|
|
// DBG(printf("appending template fieldname:'%s'\n", aFieldName.m_Name.utf8_str() );)
|
2010-06-17 16:30:10 +00:00
|
|
|
|
|
|
|
// the name is legal and not previously added to the config container, append
|
|
|
|
// it and return its index within the container.
|
|
|
|
m_Fields.push_back( aFieldName );
|
|
|
|
|
|
|
|
return m_Fields.size() - 1; // return the index of insertion.
|
|
|
|
}
|
|
|
|
|
2011-08-30 19:24:28 +00:00
|
|
|
|
|
|
|
bool TEMPLATES::HasFieldName( const wxString& aName ) const
|
|
|
|
{
|
|
|
|
for( size_t i=0; i<m_Fields.size(); ++i )
|
|
|
|
{
|
|
|
|
if( m_Fields[i].m_Name == aName )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|