KiCad PCB EDA Suite
template_fieldnames.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2015 KiCad Developers, see CHANGELOG.TXT for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <template_fieldnames.h>
26 #include <dsnlexer.h>
27 #include <fctsys.h>
28 #include <macros.h>
29 
30 using namespace TFIELD_T;
31 
32 const wxString TEMPLATE_FIELDNAME::GetDefaultFieldName( int aFieldNdx )
33 {
34  // Fixed values for the first few default fields used by EESCHEMA
35  // (mandatory fields)
36  switch( aFieldNdx )
37  {
38  case REFERENCE:
39  return _( "Reference" ); // The component reference, R1, C1, etc.
40 
41  case VALUE:
42  return _( "Value" ); // The component value + name
43 
44  case FOOTPRINT:
45  return _( "Footprint" ); // The footprint for use with Pcbnew
46 
47  case DATASHEET:
48  return _( "Datasheet" ); // Link to a datasheet for component
49 
50  default:
51  break;
52  }
53 
54  // Other fields are use fields, give a default name:
55  wxString fieldName = _( "Field" );
56  fieldName << aFieldNdx;
57  return fieldName;
58 }
59 
60 void TEMPLATE_FIELDNAME::Format( OUTPUTFORMATTER* out, int nestLevel ) const
61 {
62  out->Print( nestLevel, "(field (name %s)", out->Quotew( m_Name ).c_str() );
63 
64  if( !m_Value.IsEmpty() )
65  out->Print( 0, "(value %s)", out->Quotew( m_Value ).c_str() );
66 
67  if( m_Visible )
68  out->Print( 0, " visible" );
69 
70  out->Print( 0, ")\n" );
71 }
72 
73 
75 {
76  T tok;
77 
78  in->NeedLEFT(); // begin (name ...)
79 
80  if( (tok = in->NextTok()) != T_name )
81  in->Expecting( T_name );
82 
83  in->NeedSYMBOLorNUMBER();
84 
85  m_Name = FROM_UTF8( in->CurText() );
86 
87  in->NeedRIGHT(); // end (name ...)
88 
89  while( (tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
90  {
91  // "visible" has no '(' prefix, "value" does, so T_LEFT is optional.
92  if( tok == T_LEFT )
93  tok = in->NextTok();
94 
95  switch( tok )
96  {
97  case T_value:
98  in->NeedSYMBOLorNUMBER();
99  m_Value = FROM_UTF8( in->CurText() );
100  in->NeedRIGHT();
101  break;
102 
103  case T_visible:
104  m_Visible = true;
105  break;
106 
107  default:
108  in->Expecting( "value|visible" );
109  break;
110  }
111  }
112 }
113 
114 
115 void TEMPLATES::Format( OUTPUTFORMATTER* out, int nestLevel ) const
116 {
117  // We'll keep this general, and include the \n, even though the only known
118  // use at this time will not want the newlines or the indentation.
119  out->Print( nestLevel, "(templatefields" );
120 
121  for( unsigned i=0; i<m_Fields.size(); ++i )
122  m_Fields[i].Format( out, nestLevel+1 );
123 
124  out->Print( 0, ")\n" );
125 }
126 
127 
129 {
130  T tok;
131 
132  while( ( tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
133  {
134  if( tok == T_LEFT )
135  tok = in->NextTok();
136 
137  switch( tok )
138  {
139  case T_templatefields: // a token indicating class TEMPLATES.
140 
141  // Be flexible regarding the starting point of the TEMPLATE_FIELDNAMES_LEXER
142  // stream. Caller may not have read the first two tokens out of the
143  // stream: T_LEFT and T_templatefields, so ignore them if seen here.
144  break;
145 
146  case T_field:
147  {
148  // instantiate on stack, so if exception is thrown,
149  // destructor runs
150  TEMPLATE_FIELDNAME field;
151 
152  field.Parse( in );
153 
154  // add the field
155  AddTemplateFieldName( field );
156  }
157  break;
158 
159  default:
160  in->Unexpected( in->CurText() );
161  break;
162  }
163  }
164 }
165 
166 
168 {
169  // Ensure that the template fieldname does not match a fixed fieldname.
170  for( int i=0; i<MANDATORY_FIELDS; ++i )
171  {
172  if( TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) == aFieldName.m_Name )
173  {
174  return -1;
175  }
176  }
177 
178  // ensure uniqueness, overwrite any template fieldname by the same name.
179  for( unsigned i=0; i<m_Fields.size(); ++i )
180  {
181  if( m_Fields[i].m_Name == aFieldName.m_Name )
182  {
183  // DBG( printf( "inserting template fieldname:'%s' at %d\n",
184  // TO_UTF8( aFieldName.m_Name ), i ); )
185 
186  m_Fields[i] = aFieldName;
187  return i; // return the container index
188  }
189  }
190 
191  // DBG(printf("appending template fieldname:'%s'\n", aFieldName.m_Name.utf8_str() );)
192 
193  // the name is legal and not previously added to the config container, append
194  // it and return its index within the container.
195  m_Fields.push_back( aFieldName );
196 
197  return m_Fields.size() - 1; // return the index of insertion.
198 }
199 
200 
201 bool TEMPLATES::HasFieldName( const wxString& aName ) const
202 {
203  for( size_t i=0; i<m_Fields.size(); ++i )
204  {
205  if( m_Fields[i].m_Name == aName )
206  return true;
207  }
208 
209  return false;
210 }
211 
name of datasheet
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
Class TEMPLATE_FIELDNAMES_LEXER is an automatically generated class using the TokenList2DnsLexer.cmake technology, based on keywords provided by file: /home/kicad/workspace/kicad-doxygen/eeschema/template_fieldnames.keywords.
The first 4 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors...
void Unexpected(int aTok)
Function Unexpected throws an IO_ERROR exception with an input file specific error message...
Definition: dsnlexer.cpp:369
void Parse(TEMPLATE_FIELDNAMES_LEXER *aSpec)
Function Parse fills this object from information in the input stream aSpec, which is a TEMPLATE_FIEL...
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
int AddTemplateFieldName(const TEMPLATE_FIELDNAME &aFieldName)
Function AddTemplateFieldName inserts or appends a wanted symbol field name into the fieldnames templ...
static const wxString GetDefaultFieldName(int aFieldNdx)
Function GetDefaultFieldName returns a default symbol field name for field aFieldNdx for all componen...
Field Name Module PCB, i.e. "16DIP300".
Field Reference of part, i.e. "IC21".
This file contains miscellaneous commonly used macros and functions.
TFIELD_T::T NeedSYMBOLorNUMBER()
Function NeedSYMBOLorNUMBER calls NextTok() and then verifies that the token read in satisfies bool I...
wxString m_Name
The field name.
T
enum T contains all this lexer's tokens.
void NeedLEFT()
Function NeedLEFT calls NextTok() and then verifies that the token read in is a DSN_LEFT.
Definition: dsnlexer.cpp:393
void Format(OUTPUTFORMATTER *out, int nestLevel) const
Function Format serializes this object out as text into the given OUTPUTFORMATTER.
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:482
void Expecting(int aTok)
Function Expecting throws an IO_ERROR exception with an input file specific error message...
Definition: dsnlexer.cpp:353
C++ does not put enum values in separate namespaces unless the enum itself is in a separate namespace...
void Parse(TEMPLATE_FIELDNAMES_LEXER *in)
Function Parse fills this object from information in the input stream handled by TEMPLATE_FIELDNAMES_...
Field Value of part, i.e. "3.3K".
bool HasFieldName(const wxString &aName) const
Function HasFieldName checks for aName in the the template field name list.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void Format(OUTPUTFORMATTER *out, int nestLevel) const
Function Format serializes this object out as text into the given OUTPUTFORMATTER.
Struct TEMPLATE_FIELDNAME holds a name of a component's field, field value, and default visibility...
void NeedRIGHT()
Function NeedRIGHT calls NextTok() and then verifies that the token read in is a DSN_RIGHT.
Definition: dsnlexer.cpp:401
TFIELD_T::T NextTok()
Function NextTok returns the next token found in the input file or T_EOF when reaching the end of fil...
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404
const char * CurText()
Function CurText returns a pointer to the current token's text.
Definition: dsnlexer.h:479