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 #include <pgm_base.h>
30 
31 using namespace TFIELD_T;
32 
33 
34 const wxString TEMPLATE_FIELDNAME::GetDefaultFieldName( int aFieldNdx )
35 {
36  static void* locale = nullptr;
37  static wxString referenceDefault;
38  static wxString valueDefault;
39  static wxString footprintDefault;
40  static wxString datasheetDefault;
41  static wxString fieldDefault;
42 
43  // Fetching translations can take a surprising amount of time when loading libraries,
44  // so only do it when necessary.
45  if( Pgm().GetLocale() != locale )
46  {
47  referenceDefault = _( "Reference" );
48  valueDefault = _( "Value" );
49  footprintDefault = _( "Footprint" );
50  datasheetDefault = _( "Datasheet" );
51  fieldDefault = _( "Field" );
52  locale = Pgm().GetLocale();
53  }
54 
55  // Fixed values for the first few default fields used by EESCHEMA
56  // (mandatory fields)
57  switch( aFieldNdx )
58  {
59  case REFERENCE:
60  return referenceDefault; // The component reference, R1, C1, etc.
61 
62  case VALUE:
63  return valueDefault; // The component value + name
64 
65  case FOOTPRINT:
66  return footprintDefault; // The footprint for use with Pcbnew
67 
68  case DATASHEET:
69  return datasheetDefault; // Link to a datasheet for component
70 
71  default:
72  break;
73  }
74 
75  // Other fields are use fields, give a default name:
76  wxString fieldName = fieldDefault;
77  fieldName << aFieldNdx;
78  return fieldName;
79 }
80 
81 void TEMPLATE_FIELDNAME::Format( OUTPUTFORMATTER* out, int nestLevel ) const
82 {
83  out->Print( nestLevel, "(field (name %s)", out->Quotew( m_Name ).c_str() );
84 
85  if( m_Visible )
86  out->Print( 0, " visible" );
87 
88  if( m_URL )
89  out->Print( 0, " url" );
90 
91  out->Print( 0, ")\n" );
92 }
93 
94 
95 void TEMPLATE_FIELDNAME::Parse( TEMPLATE_FIELDNAMES_LEXER* in )
96 {
97  T tok;
98 
99  in->NeedLEFT(); // begin (name ...)
100 
101  if( (tok = in->NextTok()) != T_name )
102  in->Expecting( T_name );
103 
104  in->NeedSYMBOLorNUMBER();
105 
106  m_Name = FROM_UTF8( in->CurText() );
107 
108  in->NeedRIGHT(); // end (name ...)
109 
110  while( (tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
111  {
112  // "visible" has no '(' prefix, "value" does, so T_LEFT is optional.
113  if( tok == T_LEFT )
114  tok = in->NextTok();
115 
116  switch( tok )
117  {
118  case T_value:
119  // older format; silently skip
120  in->NeedSYMBOLorNUMBER();
121  in->NeedRIGHT();
122  break;
123 
124  case T_visible:
125  m_Visible = true;
126  break;
127 
128  case T_url:
129  m_URL = true;
130  break;
131 
132  default:
133  in->Expecting( "value|url|visible" );
134  break;
135  }
136  }
137 }
138 
139 
140 void TEMPLATES::Format( OUTPUTFORMATTER* out, int nestLevel ) const
141 {
142  // We'll keep this general, and include the \n, even though the only known
143  // use at this time will not want the newlines or the indentation.
144  out->Print( nestLevel, "(templatefields" );
145 
146  for( unsigned i=0; i<m_Fields.size(); ++i )
147  m_Fields[i].Format( out, nestLevel+1 );
148 
149  out->Print( 0, ")\n" );
150 }
151 
152 
153 void TEMPLATES::Parse( TEMPLATE_FIELDNAMES_LEXER* in )
154 {
155  T tok;
156 
157  while( ( tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
158  {
159  if( tok == T_LEFT )
160  tok = in->NextTok();
161 
162  switch( tok )
163  {
164  case T_templatefields: // a token indicating class TEMPLATES.
165 
166  // Be flexible regarding the starting point of the TEMPLATE_FIELDNAMES_LEXER
167  // stream. Caller may not have read the first two tokens out of the
168  // stream: T_LEFT and T_templatefields, so ignore them if seen here.
169  break;
170 
171  case T_field:
172  {
173  // instantiate on stack, so if exception is thrown,
174  // destructor runs
175  TEMPLATE_FIELDNAME field;
176 
177  field.Parse( in );
178 
179  // add the field
180  AddTemplateFieldName( field );
181  }
182  break;
183 
184  default:
185  in->Unexpected( in->CurText() );
186  break;
187  }
188  }
189 }
190 
191 
193 {
194  // Ensure that the template fieldname does not match a fixed fieldname.
195  for( int i=0; i<MANDATORY_FIELDS; ++i )
196  {
197  if( TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) == aFieldName.m_Name )
198  {
199  return -1;
200  }
201  }
202 
203  // ensure uniqueness, overwrite any template fieldname by the same name.
204  for( unsigned i=0; i<m_Fields.size(); ++i )
205  {
206  if( m_Fields[i].m_Name == aFieldName.m_Name )
207  {
208  // DBG( printf( "inserting template fieldname:'%s' at %d\n",
209  // TO_UTF8( aFieldName.m_Name ), i ); )
210 
211  m_Fields[i] = aFieldName;
212  return i; // return the container index
213  }
214  }
215 
216  // DBG(printf("appending template fieldname:'%s'\n", aFieldName.m_Name.utf8_str() );)
217 
218  // the name is legal and not previously added to the config container, append
219  // it and return its index within the container.
220  m_Fields.push_back( aFieldName );
221 
222  return m_Fields.size() - 1; // return the index of insertion.
223 }
224 
225 
226 const TEMPLATE_FIELDNAME* TEMPLATES::GetFieldName( const wxString& aName ) const
227 {
228  for( const TEMPLATE_FIELDNAME& field : m_Fields )
229  {
230  if( field.m_Name == aName )
231  return &field;
232  }
233 
234  return nullptr;
235 }
236 
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
The first 4 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors...
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...
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
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".
const TEMPLATE_FIELDNAME * GetFieldName(const wxString &aName) const
Function GetFieldName searches for aName in the the template field name list.
This file contains miscellaneous commonly used macros and functions.
wxString m_Name
The field name.
VTBL_ENTRY wxLocale * GetLocale()
Definition: pgm_base.h:217
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 Parse(TEMPLATE_FIELDNAMES_LEXER *in)
Function Parse fills this object from information in the input stream handled by TEMPLATE_FIELDNAMES_...
see class PGM_BASE
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
size_t i
Definition: json11.cpp:597
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&#39;s field, field value, and default visibility...
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404
#define VALUE