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 <[email protected]>
5 * Copyright (C) 2015-2023 KiCad Developers, see AUTHORS.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 <mutex>
26
27#include <macros.h>
28#include <template_fieldnames.h>
29#include <pgm_base.h>
30
31using namespace TFIELD_T;
32
33// N.B. Do not change these values without transitioning the file format
34#define REFERENCE_CANONICAL "Reference"
35#define VALUE_CANONICAL "Value"
36#define FOOTPRINT_CANONICAL "Footprint"
37#define DATASHEET_CANONICAL "Datasheet"
38
43
44const wxString TEMPLATE_FIELDNAME::GetDefaultFieldName( int aFieldNdx, bool aTranslateForHI )
45{
46 if( !aTranslateForHI )
47 {
48 switch( aFieldNdx )
49 {
50 case REFERENCE_FIELD: return s_CanonicalReference; // The symbol reference, R1, C1, etc.
51 case VALUE_FIELD: return s_CanonicalValue; // The symbol value
52 case FOOTPRINT_FIELD: return s_CanonicalFootprint; // The footprint for use with Pcbnew
53 case DATASHEET_FIELD: return s_CanonicalDatasheet; // Link to a datasheet for symbol
54 default: return wxString::Format( wxT( "Field%d" ), aFieldNdx );
55 }
56 }
57
58 switch( aFieldNdx )
59 {
60 case REFERENCE_FIELD: return _( REFERENCE_CANONICAL ); // The symbol reference, R1, C1, etc.
61 case VALUE_FIELD: return _( VALUE_CANONICAL ); // The symbol value
62 case FOOTPRINT_FIELD: return _( FOOTPRINT_CANONICAL ); // The footprint for use with Pcbnew
63 case DATASHEET_FIELD: return _( DATASHEET_CANONICAL ); // Link to a datasheet for symbol
64 default: return wxString::Format( _( "Field%d" ), aFieldNdx );
65 }
66}
67
68
69void TEMPLATE_FIELDNAME::Format( OUTPUTFORMATTER* out, int nestLevel ) const
70{
71 out->Print( nestLevel, "(field (name %s)", out->Quotew( m_Name ).c_str() );
72
73 if( m_Visible )
74 out->Print( 0, " visible" );
75
76 if( m_URL )
77 out->Print( 0, " url" );
78
79 out->Print( 0, ")\n" );
80}
81
82
83void TEMPLATE_FIELDNAME::Parse( TEMPLATE_FIELDNAMES_LEXER* in )
84{
85 T tok;
86
87 in->NeedLEFT(); // begin (name ...)
88
89 if( ( tok = in->NextTok() ) != T_name )
90 in->Expecting( T_name );
91
92 in->NeedSYMBOLorNUMBER();
93
94 m_Name = FROM_UTF8( in->CurText() );
95
96 in->NeedRIGHT(); // end (name ...)
97
98 while( (tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
99 {
100 // "visible" has no '(' prefix, "value" does, so T_LEFT is optional.
101 if( tok == T_LEFT )
102 tok = in->NextTok();
103
104 switch( tok )
105 {
106 case T_value:
107 // older format; silently skip
108 in->NeedSYMBOLorNUMBER();
109 in->NeedRIGHT();
110 break;
111
112 case T_visible:
113 m_Visible = true;
114 break;
115
116 case T_url:
117 m_URL = true;
118 break;
119
120 default:
121 in->Expecting( "value|url|visible" );
122 break;
123 }
124 }
125}
126
127
128void TEMPLATES::Format( OUTPUTFORMATTER* out, int nestLevel, bool aGlobal ) const
129{
130 // We'll keep this general, and include the \n, even though the only known
131 // use at this time will not want the newlines or the indentation.
132 out->Print( nestLevel, "(templatefields" );
133
134 const TEMPLATE_FIELDNAMES& source = aGlobal ? m_globals : m_project;
135
136 for( const TEMPLATE_FIELDNAME& temp : source )
137 temp.Format( out, nestLevel+1 );
138
139 out->Print( 0, ")\n" );
140}
141
142
143void TEMPLATES::parse( TEMPLATE_FIELDNAMES_LEXER* in, bool aGlobal )
144{
145 T tok;
146
147 while( ( tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
148 {
149 if( tok == T_LEFT )
150 tok = in->NextTok();
151
152 switch( tok )
153 {
154 case T_templatefields: // a token indicating class TEMPLATES.
155
156 // Be flexible regarding the starting point of the TEMPLATE_FIELDNAMES_LEXER
157 // stream. Caller may not have read the first two tokens out of the
158 // stream: T_LEFT and T_templatefields, so ignore them if seen here.
159 break;
160
161 case T_field:
162 {
163 // instantiate on stack, so if exception is thrown,
164 // destructor runs
165 TEMPLATE_FIELDNAME field;
166
167 field.Parse( in );
168
169 // add the field
170 AddTemplateFieldName( field, aGlobal );
171 }
172 break;
173
174 default:
175 in->Unexpected( in->CurText() );
176 break;
177 }
178 }
179}
180
181
182/*
183 * Flatten project and global templates into a single list. (Project templates take
184 * precedence.)
185 */
187{
189
190 // Note: order N^2 algorithm. Would need changing if fieldname template sets ever
191 // get large.
192
193 for( const TEMPLATE_FIELDNAME& global : m_globals )
194 {
195 for( const TEMPLATE_FIELDNAME& project : m_project )
196 {
197 if( global.m_Name == project.m_Name )
198 continue;
199 }
200
201 m_resolved.push_back( global );
202 }
203
204 m_resolvedDirty = false;
205}
206
207
208void TEMPLATES::AddTemplateFieldName( const TEMPLATE_FIELDNAME& aFieldName, bool aGlobal )
209{
210 // Ensure that the template fieldname does not match a fixed fieldname.
211 for( int i = 0; i < MANDATORY_FIELDS; ++i )
212 {
213 if( TEMPLATE_FIELDNAME::GetDefaultFieldName( i ) == aFieldName.m_Name )
214 return;
215 }
216
217 TEMPLATE_FIELDNAMES& target = aGlobal ? m_globals : m_project;
218
219 // ensure uniqueness, overwrite any template fieldname by the same name.
220 for( TEMPLATE_FIELDNAME& temp : target )
221 {
222 if( temp.m_Name == aFieldName.m_Name )
223 {
224 temp = aFieldName;
225 m_resolvedDirty = true;
226 return;
227 }
228 }
229
230 // the name is legal and not previously added to the config container, append
231 // it and return its index within the container.
232 target.push_back( aFieldName );
233 m_resolvedDirty = true;
234}
235
236
237void TEMPLATES::AddTemplateFieldNames( const wxString& aSerializedFieldNames )
238{
239 TEMPLATE_FIELDNAMES_LEXER field_lexer( TO_UTF8( aSerializedFieldNames ) );
240
241 try
242 {
243 parse( &field_lexer, true );
244 }
245 catch( const IO_ERROR& )
246 {
247 }
248}
249
250
252{
253 if( aGlobal )
254 {
255 m_globals.clear();
257 }
258 else
259 {
260 m_project.clear();
262 }
263
264 m_resolvedDirty = false;
265}
266
267
269{
270 if( m_resolvedDirty )
272
273 return m_resolved;
274}
275
276
278{
279 if( aGlobal )
280 return m_globals;
281 else
282 return m_project;
283}
284
285
286const TEMPLATE_FIELDNAME* TEMPLATES::GetFieldName( const wxString& aName )
287{
288 if( m_resolvedDirty )
290
291 for( const TEMPLATE_FIELDNAME& field : m_resolved )
292 {
293 if( field.m_Name == aName )
294 return &field;
295 }
296
297 return nullptr;
298}
299
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:310
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:501
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:433
TEMPLATE_FIELDNAMES m_globals
void AddTemplateFieldName(const TEMPLATE_FIELDNAME &aFieldName, bool aGlobal)
Insert or append a wanted symbol field name into the field names template.
void AddTemplateFieldNames(const wxString &aSerializedFieldNames)
Add a serialized list of template field names.
TEMPLATE_FIELDNAMES m_project
TEMPLATE_FIELDNAMES m_resolved
const TEMPLATE_FIELDNAME * GetFieldName(const wxString &aName)
Search for aName in the template field name list.
void DeleteAllFieldNameTemplates(bool aGlobal)
Delete the entire contents.
const TEMPLATE_FIELDNAMES & GetTemplateFieldNames()
Return a template field name list for read only access.
void parse(TEMPLATE_FIELDNAMES_LEXER *in, bool aGlobal)
void Format(OUTPUTFORMATTER *out, int nestLevel, bool aGlobal) const
Serialize this object out as text into the given OUTPUTFORMATTER.
#define _(s)
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
Hold a name of a symbol's field, field value, and default visibility.
void Format(OUTPUTFORMATTER *out, int nestLevel) const
Serialize this object out as text into the given OUTPUTFORMATTER.
void Parse(TEMPLATE_FIELDNAMES_LEXER *aSpec)
Fill this object from information in the input stream aSpec, which is a #TEMPLATE_FIELDNAMES_LEXER.
static const wxString GetDefaultFieldName(int aFieldNdx, bool aTranslateForHI=false)
Return a default symbol field name for field aFieldNdx for all components.
#define VALUE_CANONICAL
#define FOOTPRINT_CANONICAL
static wxString s_CanonicalReference(REFERENCE_CANONICAL)
static wxString s_CanonicalValue(VALUE_CANONICAL)
static wxString s_CanonicalFootprint(FOOTPRINT_CANONICAL)
static wxString s_CanonicalDatasheet(DATASHEET_CANONICAL)
#define DATASHEET_CANONICAL
#define REFERENCE_CANONICAL
@ DATASHEET_FIELD
name of datasheet
@ FOOTPRINT_FIELD
Field Name Module PCB, i.e. "16DIP300".
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ MANDATORY_FIELDS
The first 4 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors.
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".
std::vector< TEMPLATE_FIELDNAME > TEMPLATE_FIELDNAMES