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_Value.IsEmpty() )
86  out->Print( 0, "(value %s)", out->Quotew( m_Value ).c_str() );
87 
88  if( m_Visible )
89  out->Print( 0, " visible" );
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  in->NeedSYMBOLorNUMBER();
120  m_Value = FROM_UTF8( in->CurText() );
121  in->NeedRIGHT();
122  break;
123 
124  case T_visible:
125  m_Visible = true;
126  break;
127 
128  default:
129  in->Expecting( "value|visible" );
130  break;
131  }
132  }
133 }
134 
135 
136 void TEMPLATES::Format( OUTPUTFORMATTER* out, int nestLevel ) const
137 {
138  // We'll keep this general, and include the \n, even though the only known
139  // use at this time will not want the newlines or the indentation.
140  out->Print( nestLevel, "(templatefields" );
141 
142  for( unsigned i=0; i<m_Fields.size(); ++i )
143  m_Fields[i].Format( out, nestLevel+1 );
144 
145  out->Print( 0, ")\n" );
146 }
147 
148 
149 void TEMPLATES::Parse( TEMPLATE_FIELDNAMES_LEXER* in )
150 {
151  T tok;
152 
153  while( ( tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
154  {
155  if( tok == T_LEFT )
156  tok = in->NextTok();
157 
158  switch( tok )
159  {
160  case T_templatefields: // a token indicating class TEMPLATES.
161 
162  // Be flexible regarding the starting point of the TEMPLATE_FIELDNAMES_LEXER
163  // stream. Caller may not have read the first two tokens out of the
164  // stream: T_LEFT and T_templatefields, so ignore them if seen here.
165  break;
166 
167  case T_field:
168  {
169  // instantiate on stack, so if exception is thrown,
170  // destructor runs
171  TEMPLATE_FIELDNAME field;
172 
173  field.Parse( in );
174 
175  // add the field
176  AddTemplateFieldName( field );
177  }
178  break;
179 
180  default:
181  in->Unexpected( in->CurText() );
182  break;
183  }
184  }
185 }
186 
187 
189 {
190  // Ensure that the template fieldname does not match a fixed fieldname.
191  for( int i=0; i<MANDATORY_FIELDS; ++i )
192  {
193  if( TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) == aFieldName.m_Name )
194  {
195  return -1;
196  }
197  }
198 
199  // ensure uniqueness, overwrite any template fieldname by the same name.
200  for( unsigned i=0; i<m_Fields.size(); ++i )
201  {
202  if( m_Fields[i].m_Name == aFieldName.m_Name )
203  {
204  // DBG( printf( "inserting template fieldname:'%s' at %d\n",
205  // TO_UTF8( aFieldName.m_Name ), i ); )
206 
207  m_Fields[i] = aFieldName;
208  return i; // return the container index
209  }
210  }
211 
212  // DBG(printf("appending template fieldname:'%s'\n", aFieldName.m_Name.utf8_str() );)
213 
214  // the name is legal and not previously added to the config container, append
215  // it and return its index within the container.
216  m_Fields.push_back( aFieldName );
217 
218  return m_Fields.size() - 1; // return the index of insertion.
219 }
220 
221 
222 bool TEMPLATES::HasFieldName( const wxString& aName ) const
223 {
224  for( size_t i=0; i<m_Fields.size(); ++i )
225  {
226  if( m_Fields[i].m_Name == aName )
227  return true;
228  }
229 
230  return false;
231 }
232 
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:65
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.
wxString m_Name
The field name.
VTBL_ENTRY wxLocale * GetLocale()
Definition: pgm_base.h:175
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_...
bool HasFieldName(const wxString &aName) const
Function HasFieldName checks for aName in the the template field name list.
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
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