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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include "template_fieldnames.h"
22
23#include <mutex>
24
25#include <template_fieldnames_lexer.h>
26#include <string_utils.h>
27
28using namespace TFIELD_T;
29
30// N.B. Do not change these values without transitioning the file format
31#define REFERENCE_UNTRANSLATED "Reference"
32#define VALUE_UNTRANSLATED "Value"
33#define FOOTPRINT_UNTRANSLATED "Footprint"
34#define DATASHEET_UNTRANSLATED "Datasheet"
35#define DESCRIPTION_UNTRANSLATED "Description"
36#define SHEET_NAME_UNTRANSLATED "Sheetname"
37#define SHEET_FILE_UNTRANSLATED "Sheetfile"
38#define SHEET_REF_SUFFIX_UNTRANSLATED "Reference Suffix"
39#define INTERSHEET_REFS_UNTRANSLATED "Intersheetrefs"
40#define USER_FIELD_UNTRANSLATED_FORMAT "Field%d"
41
51
52
53wxString GetDefaultFieldName( FIELD_T aFieldId, TRANSLATION aTranslation )
54{
55 if( aTranslation == UNTRANSLATED )
56 {
57 switch( aFieldId )
58 {
59 case FIELD_T::REFERENCE: return s_UntranslatedReference; // The symbol reference, R1, C1, etc.
60 case FIELD_T::VALUE: return s_UntranslatedValue; // The symbol value
61 case FIELD_T::FOOTPRINT: return s_UntranslatedFootprint; // The footprint for use with Pcbnew
62 case FIELD_T::DATASHEET: return s_UntranslatedDatasheet; // Link to a datasheet for symbol
63 case FIELD_T::DESCRIPTION: return s_UntranslatedDescription; // The symbol description
68 default: return GetUserFieldName( 42, aTranslation );
69 }
70 }
71 else
72 {
73 switch( aFieldId )
74 {
75 case FIELD_T::REFERENCE: return _( REFERENCE_UNTRANSLATED ); // The symbol reference, R1, C1, etc.
76 case FIELD_T::VALUE: return _( VALUE_UNTRANSLATED ); // The symbol value
77 case FIELD_T::FOOTPRINT: return _( FOOTPRINT_UNTRANSLATED ); // The footprint for use with Pcbnew
78 case FIELD_T::DATASHEET: return _( DATASHEET_UNTRANSLATED ); // Link to a datasheet for symbol
79 case FIELD_T::DESCRIPTION: return _( DESCRIPTION_UNTRANSLATED ); // The symbol description
84 default: return GetUserFieldName( 42, aTranslation );
85 }
86 }
87}
88
89
90wxString GetUserFieldName( int aFieldNdx, TRANSLATION aTranslation )
91{
92 if( aTranslation == UNTRANSLATED )
93 return wxString::Format( wxS( USER_FIELD_UNTRANSLATED_FORMAT ), aFieldNdx );
94 else
95 return wxString::Format( _( USER_FIELD_UNTRANSLATED_FORMAT ), aFieldNdx );
96}
97
98
99bool FieldNamesAreDuplicates( const wxString& aLhs, const wxString& aRhs,
100 std::initializer_list<FIELD_T> aMandatoryFields )
101{
102 if( aLhs == aRhs )
103 return true;
104
105 // If they don't even match case-insensitively they can't both be variants of the same
106 // untranslated mandatory field name.
107 if( aLhs.CmpNoCase( aRhs ) != 0 )
108 return false;
109
110 // Mandatory field names are folded case-insensitively by the s-expression parser, so any
111 // case variant of an untranslated mandatory name collides with that mandatory field.
112 for( FIELD_T fieldId : aMandatoryFields )
113 {
114 if( aLhs.CmpNoCase( GetDefaultFieldName( fieldId, UNTRANSLATED ) ) == 0 )
115 return true;
116 }
117
118 return false;
119}
120
121
122bool FieldNamesAreDuplicates( const wxString& aLhs, const wxString& aRhs )
123{
124 return FieldNamesAreDuplicates( aLhs, aRhs, MANDATORY_FIELDS );
125}
126
127
129{
130 out->Print( "(field (name %s)", out->Quotew( m_Name ).c_str() );
131
132 if( m_Visible )
133 out->Print( " visible" );
134
135 if( m_URL )
136 out->Print( " url" );
137
138 out->Print( ")" );
139}
140
141
142void TEMPLATE_FIELDNAME::Parse( TEMPLATE_FIELDNAMES_LEXER* in )
143{
144 T tok;
145
146 in->NeedLEFT(); // begin (name ...)
147
148 if( ( tok = in->NextTok() ) != T_name )
149 in->Expecting( T_name );
150
151 in->NeedSYMBOLorNUMBER();
152
153 m_Name = From_UTF8( in->CurText() );
154
155 in->NeedRIGHT(); // end (name ...)
156
157 while( (tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
158 {
159 // "visible" has no '(' prefix, "value" does, so T_LEFT is optional.
160 if( tok == T_LEFT )
161 tok = in->NextTok();
162
163 switch( tok )
164 {
165 case T_value:
166 // older format; silently skip
167 in->NeedSYMBOLorNUMBER();
168 in->NeedRIGHT();
169 break;
170
171 case T_visible:
172 m_Visible = true;
173 break;
174
175 case T_url:
176 m_URL = true;
177 break;
178
179 default:
180 in->Expecting( "value|url|visible" );
181 break;
182 }
183 }
184}
185
186
187void TEMPLATES::Format( OUTPUTFORMATTER* out, SCOPE aScope ) const
188{
189 // We'll keep this general, and include the \n, even though the only known
190 // use at this time will not want the newlines or the indentation.
191 out->Print( "(templatefields" );
192
193 const std::vector<TEMPLATE_FIELDNAME>& source = aScope == SCOPE::GLOBAL ? m_globals : m_project;
194
195 for( const TEMPLATE_FIELDNAME& temp : source )
196 {
197 if( !temp.m_Name.IsEmpty() )
198 temp.Format( out );
199 }
200
201 out->Print( ")" );
202}
203
204
205void TEMPLATES::parse( TEMPLATE_FIELDNAMES_LEXER* in, SCOPE aScope )
206{
207 T tok;
208
209 while( ( tok = in->NextTok() ) != T_RIGHT && tok != T_EOF )
210 {
211 if( tok == T_LEFT )
212 tok = in->NextTok();
213
214 switch( tok )
215 {
216 case T_templatefields: // a token indicating class TEMPLATES.
217
218 // Be flexible regarding the starting point of the TEMPLATE_FIELDNAMES_LEXER
219 // stream. Caller may not have read the first two tokens out of the
220 // stream: T_LEFT and T_templatefields, so ignore them if seen here.
221 break;
222
223 case T_field:
224 {
225 // instantiate on stack, so if exception is thrown,
226 // destructor runs
227 TEMPLATE_FIELDNAME field;
228
229 field.Parse( in );
230
231 // add the field
232 if( !field.m_Name.IsEmpty() )
233 AddTemplateFieldName( field, aScope );
234 }
235 break;
236
237 default:
238 in->Unexpected( in->CurText() );
239 break;
240 }
241 }
242}
243
244
250{
252
253 // Note: order N^2 algorithm. Would need changing if fieldname template sets ever
254 // get large.
255
256 for( const TEMPLATE_FIELDNAME& global : m_globals )
257 {
258 bool overriddenInProject = false;
259
260 for( const TEMPLATE_FIELDNAME& project : m_project )
261 {
262 if( global.m_Name == project.m_Name )
263 {
264 overriddenInProject = true;
265 break;
266 }
267 }
268
269 if( !overriddenInProject )
270 m_resolved.push_back( global );
271 }
272
273 m_resolvedDirty = false;
274}
275
276
278{
279 // Reject any case variant of a mandatory fieldname; the s-expression parser folds those onto
280 // the mandatory field with the matching untranslated name, so they can never become a
281 // distinct user field.
282 for( FIELD_T fieldId : MANDATORY_FIELDS )
283 {
284 if( GetDefaultFieldName( fieldId, UNTRANSLATED ).CmpNoCase( aFieldName.m_Name ) == 0 )
285 return;
286 }
287
288 std::vector<TEMPLATE_FIELDNAME>& target = aScope == SCOPE::GLOBAL ? m_globals : m_project;
289
290 // ensure uniqueness, overwrite any template fieldname by the same name.
291 for( TEMPLATE_FIELDNAME& temp : target )
292 {
293 if( temp.m_Name == aFieldName.m_Name )
294 {
295 temp = aFieldName;
296 m_resolvedDirty = true;
297 return;
298 }
299 }
300
301 // the name is legal and not previously added to the config container, append
302 // it and return its index within the container.
303 target.push_back( aFieldName );
304 m_resolvedDirty = true;
305}
306
307
308void TEMPLATES::AddTemplateFieldNames( const wxString& aSerializedFieldNames, SCOPE aScope )
309{
310 TEMPLATE_FIELDNAMES_LEXER field_lexer( TO_UTF8( aSerializedFieldNames ) );
311
312 try
313 {
314 parse( &field_lexer, aScope );
315 }
316 catch( const IO_ERROR& )
317 {
318 }
319}
320
321
323{
324 if( aScope == SCOPE::GLOBAL )
325 {
326 m_globals.clear();
328 }
329 else
330 {
331 m_project.clear();
333 }
334
335 m_resolvedDirty = false;
336}
337
338
339const std::vector<TEMPLATE_FIELDNAME>& TEMPLATES::GetResolvedTemplateFieldNames()
340{
341 if( m_resolvedDirty )
343
344 return m_resolved;
345}
346
347
348const std::vector<TEMPLATE_FIELDNAME>& TEMPLATES::GetTemplateFieldNames( SCOPE aScope )
349{
350 if( aScope == SCOPE::GLOBAL )
351 return m_globals;
352 else
353 return m_project;
354}
355
356
357const TEMPLATE_FIELDNAME* TEMPLATES::GetFieldName( const wxString& aName )
358{
359 if( m_resolvedDirty )
361
362 for( const TEMPLATE_FIELDNAME& field : m_resolved )
363 {
364 if( field.m_Name == aName )
365 return &field;
366 }
367
368 return nullptr;
369}
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
An interface used to output 8 bit text in a convenient way.
Definition richio.h:294
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:505
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:432
void AddTemplateFieldName(const TEMPLATE_FIELDNAME &aFieldName, SCOPE aScope)
Insert or append a wanted symbol field name into the field names template.
const std::vector< TEMPLATE_FIELDNAME > & GetResolvedTemplateFieldNames()
Return the resolved project and global template field name list for read only access.
std::vector< TEMPLATE_FIELDNAME > m_resolved
void resolveTemplates()
Flatten project and global templates into a single list.
std::vector< TEMPLATE_FIELDNAME > m_project
void AddTemplateFieldNames(const wxString &aSerializedFieldNames, SCOPE aScope)
Add a serialized list of template field names.
const std::vector< TEMPLATE_FIELDNAME > & GetTemplateFieldNames(SCOPE aScope)
Return a specific list (global or project) for read only access.
const TEMPLATE_FIELDNAME * GetFieldName(const wxString &aName)
Search for aName in the template field name list.
void Format(OUTPUTFORMATTER *out, SCOPE aScope) const
Serialize this object out as text into the given OUTPUTFORMATTER.
void DeleteFieldNameTemplates(SCOPE aScope)
Delete the contents of a scope.
std::vector< TEMPLATE_FIELDNAME > m_globals
void parse(TEMPLATE_FIELDNAMES_LEXER *in, SCOPE aScope)
#define _(s)
std::string source
wxString From_UTF8(const char *cstring)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
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 obj...
#define SHEET_REF_SUFFIX_UNTRANSLATED
#define INTERSHEET_REFS_UNTRANSLATED
static wxString s_UntranslatedSheetFile(SHEET_FILE_UNTRANSLATED)
static wxString s_UntranslatedFootprint(FOOTPRINT_UNTRANSLATED)
#define DESCRIPTION_UNTRANSLATED
bool FieldNamesAreDuplicates(const wxString &aLhs, const wxString &aRhs, std::initializer_list< FIELD_T > aMandatoryFields)
Test whether two field names should be treated as duplicates for the purposes of field name uniquenes...
#define SHEET_FILE_UNTRANSLATED
#define REFERENCE_UNTRANSLATED
static wxString s_UntranslatedIntersheetRefs(INTERSHEET_REFS_UNTRANSLATED)
static wxString s_UntranslatedValue(VALUE_UNTRANSLATED)
#define VALUE_UNTRANSLATED
static wxString s_UntranslatedSheetName(SHEET_NAME_UNTRANSLATED)
#define SHEET_NAME_UNTRANSLATED
wxString GetUserFieldName(int aFieldNdx, TRANSLATION aTranslation)
static wxString s_UntranslatedDatasheet(DATASHEET_UNTRANSLATED)
static wxString s_UntranslatedDescription(DESCRIPTION_UNTRANSLATED)
#define FOOTPRINT_UNTRANSLATED
#define USER_FIELD_UNTRANSLATED_FORMAT
static wxString s_UntranslatedSheetRefSuffix(SHEET_REF_SUFFIX_UNTRANSLATED)
static wxString s_UntranslatedReference(REFERENCE_UNTRANSLATED)
wxString GetDefaultFieldName(FIELD_T aFieldId, TRANSLATION aTranslation)
Return a default symbol field name for a mandatory field type.
#define DATASHEET_UNTRANSLATED
#define MANDATORY_FIELDS
FIELD_T
The set of all field indices assuming an array like sequence that a SCH_COMPONENT or LIB_PART can hol...
@ INTERSHEET_REFS
Global label cross-reference page numbers.
@ DESCRIPTION
Field Description of part, i.e. "1/4W 1% Metal Film Resistor".
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ SHEET_REF_SUFFIX
Optional sheet field appended to the references of the sheet's symbols.
@ DATASHEET
name of datasheet
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
@ UNTRANSLATED
KICOMMON_API wxString GetDefaultFieldName(FIELD_T aFieldId, TRANSLATION aTranslation)
Return a default symbol field name for a mandatory field type.