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