KiCad PCB EDA Suite
Loading...
Searching...
No Matches
common.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) 2014-2020 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2008 Wayne Stambaugh <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <eda_base_frame.h>
27#include <kiplatform/app.h>
28#include <project.h>
29#include <common.h>
30#include <confirm.h>
31#include <env_vars.h>
32#include <advanced_config.h>
33#include <reporter.h>
34#include <macros.h>
35#include <string_utils.h>
37#include <text_var_dependency.h>
38#include <mutex>
39#include <wx/config.h>
40#include <wx/log.h>
41#include <wx/msgdlg.h>
42#include <wx/stdpaths.h>
43#include <wx/url.h>
44#include <wx/utils.h>
45#include <wx/regex.h>
46
47#ifdef _WIN32
48#include <windows.h>
49#endif
50
51
53{
57#ifdef __WINDOWS__
58 Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
59#endif
61};
62
63wxString ExpandTextVars( const wxString& aSource, const PROJECT* aProject, int aFlags )
64{
65 std::function<bool( wxString* )> projectResolver = [&]( wxString* token ) -> bool
66 {
67 return aProject->TextVarResolver( token );
68 };
69
70 return ExpandTextVars( aSource, &projectResolver, aFlags );
71}
72
73
74wxString ExpandTextVars( const wxString& aSource, const std::function<bool( wxString* )>* aResolver, int aFlags,
75 int aDepth )
76{
77 wxString newbuf;
78 size_t sourceLen = aSource.length();
79
80 newbuf.Alloc( sourceLen ); // best guess (improves performance)
81
82 // Get the maximum recursion depth from advanced config
84
85 for( size_t i = 0; i < sourceLen; ++i )
86 {
87 // Skip over existing escape markers without processing their contents
88 // This prevents expanding ${} or @{} that are inside escaped expressions
89 if( i + 14 <= sourceLen && aSource.Mid( i, 14 ) == wxT( "<<<ESC_DOLLAR:" ) )
90 {
91 // Copy the entire escape marker including contents until matching closing }
92 newbuf.append( wxT( "<<<ESC_DOLLAR:" ) );
93 i += 14;
94
95 // Count braces to find the matching closing }
96 int braceCount = 1;
97 while( i < sourceLen && braceCount > 0 )
98 {
99 if( aSource[i] == '{' )
100 braceCount++;
101 else if( aSource[i] == '}' )
102 braceCount--;
103
104 newbuf.append( aSource[i] );
105 i++;
106 }
107 i--; // Back up one since the for loop will increment
108 continue;
109 }
110 else if( i + 10 <= sourceLen && aSource.Mid( i, 10 ) == wxT( "<<<ESC_AT:" ) )
111 {
112 // Copy the entire escape marker including contents until matching closing }
113 newbuf.append( wxT( "<<<ESC_AT:" ) );
114 i += 10;
115
116 // Count braces to find the matching closing }
117 int braceCount = 1;
118 while( i < sourceLen && braceCount > 0 )
119 {
120 if( aSource[i] == '{' )
121 braceCount++;
122 else if( aSource[i] == '}' )
123 braceCount--;
124
125 newbuf.append( aSource[i] );
126 i++;
127 }
128 i--; // Back up one since the for loop will increment
129 continue;
130 }
131
132 // Handle escaped variable references: \${...} or \@{...}
133 // Replace with escape markers that won't be expanded by multi-pass loops
134 // The markers will be converted back to ${...} or @{...} only at the final display stage
135 if( aSource[i] == '\\' && i + 1 < sourceLen )
136 {
137 if( ( aSource[i + 1] == '$' || aSource[i + 1] == '@' ) && i + 2 < sourceLen && aSource[i + 2] == '{' )
138 {
139 // Replace \${ with <<<ESC_DOLLAR: and \@{ with <<<ESC_AT:
140 // Using unique delimiters without braces to avoid confusing the expression evaluator
141 if( aSource[i + 1] == '$' )
142 newbuf.append( wxT( "<<<ESC_DOLLAR:" ) );
143 else
144 newbuf.append( wxT( "<<<ESC_AT:" ) );
145 i += 2;
146
147 // Copy everything until the matching closing brace, including the brace
148 int braceDepth = 1;
149 for( i = i + 1; i < sourceLen && braceDepth > 0; ++i )
150 {
151 if( aSource[i] == '{' )
152 braceDepth++;
153 else if( aSource[i] == '}' )
154 braceDepth--;
155
156 newbuf.append( aSource[i] );
157 }
158 i--; // Adjust because loop will increment
159 continue;
160 }
161 }
162
163 if( ( aSource[i] == '$' || aSource[i] == '@' ) && i + 1 < sourceLen && aSource[i + 1] == '{' )
164 {
165 bool isMathExpr = ( aSource[i] == '@' );
166 wxString token;
167 int braceDepth = 1; // Track brace depth for nested expressions like @{${VAR}}
168
169 for( i = i + 2; i < sourceLen; ++i )
170 {
171 // Skip over escape markers - don't count their braces
172 // This prevents <<<ESC_DOLLAR:X} from interfering with outer brace counting
173 if( i + 14 <= sourceLen && aSource.Mid( i, 14 ) == wxT( "<<<ESC_DOLLAR:" ) )
174 {
175 token.append( wxT( "<<<ESC_DOLLAR:" ) );
176 i += 14;
177
178 // Copy contents until matching closing brace (tracking nested braces)
179 int markerBraceCount = 1;
180
181 while( i < sourceLen && markerBraceCount > 0 )
182 {
183 if( aSource[i] == '{' )
184 markerBraceCount++;
185 else if( aSource[i] == '}' )
186 markerBraceCount--;
187
188 token.append( aSource[i] );
189 i++;
190 }
191
192 i--; // Adjust for outer loop increment
193 continue;
194 }
195 else if( i + 10 <= sourceLen && aSource.Mid( i, 10 ) == wxT( "<<<ESC_AT:" ) )
196 {
197 token.append( wxT( "<<<ESC_AT:" ) );
198 i += 10;
199
200 // Copy contents until matching closing brace (tracking nested braces)
201 int markerBraceCount = 1;
202
203 while( i < sourceLen && markerBraceCount > 0 )
204 {
205 if( aSource[i] == '{' )
206 markerBraceCount++;
207 else if( aSource[i] == '}' )
208 markerBraceCount--;
209
210 token.append( aSource[i] );
211 i++;
212 }
213
214 i--; // Adjust for outer loop increment
215 continue;
216 }
217
218 if( aSource[i] == '{' )
219 {
220 braceDepth++;
221 token.append( aSource[i] );
222 }
223 else if( aSource[i] == '}' )
224 {
225 braceDepth--;
226
227 if( braceDepth == 0 )
228 break; // Found the matching closing brace
229 else
230 token.append( aSource[i] );
231 }
232 else
233 {
234 token.append( aSource[i] );
235 }
236 }
237
238 if( token.IsEmpty() )
239 continue;
240
241 // For math expressions @{...}, recursively expand any nested ${...} variables
242 // but DON'T evaluate the math - leave that for EvaluateText() called by the user
243 if( isMathExpr )
244 {
245 if( ( token.Contains( wxT( "${" ) ) || token.Contains( wxT( "@{" ) ) ) && aDepth < maxDepth )
246 {
247 token = ExpandTextVars( token, aResolver, aFlags, aDepth + 1 );
248 }
249
250 // Return the expression with variables expanded but NOT evaluated
251 // The caller will use EvaluateText() to handle the math evaluation
252 newbuf.append( wxT( "@{" ) + token + wxT( "}" ) );
253 }
254 else // Variable reference ${...}
255 {
256 // Recursively expand nested variables BEFORE passing to resolver
257 // This ensures innermost variables are expanded first (standard evaluation order)
258 if( ( token.Contains( wxT( "${" ) ) || token.Contains( wxT( "@{" ) ) ) && aDepth < maxDepth )
259 {
260 token = ExpandTextVars( token, aResolver, aFlags, aDepth + 1 );
261
262 // Also evaluate math expressions after expanding variables
263 if( token.Contains( wxT( "@{" ) ) )
264 {
265 // Must not be static. ExpandTextVars runs on parallel workers
266 // (e.g. CONNECTION_GRAPH) and a shared evaluator races on its
267 // internal error collector.
268 EXPRESSION_EVALUATOR evaluator;
269 token = evaluator.Evaluate( token );
270 }
271 }
272
273 if( ( aFlags & FOR_ERC_DRC ) == 0
274 && ( token.StartsWith( wxS( "ERC_WARNING" ) ) || token.StartsWith( wxS( "ERC_ERROR" ) )
275 || token.StartsWith( wxS( "DRC_WARNING" ) ) || token.StartsWith( wxS( "DRC_ERROR" ) ) ) )
276 {
277 // Only show user-defined warnings/errors during ERC/DRC
278 }
279 else if( aResolver && ( *aResolver )( &token ) )
280 {
281 newbuf.append( token );
282 }
283 else
284 {
285 // Token not resolved: leave the reference unchanged
286 newbuf.append( "${" + token + "}" );
287 }
288 }
289 }
290 else
291 {
292 newbuf.append( aSource[i] );
293 }
294 }
295
296 return newbuf;
297}
298
299
300wxString ResolveTextVars( const wxString& aSource, const std::function<bool( wxString* )>* aResolver, int& aDepth )
301{
302 // Multi-pass resolution to handle nested variables like ${J601:UNIT(${ROW})}
303 // and math expressions like @{${ROW}-1}
304 wxString text = aSource;
306
307 // Must not be static. ResolveTextVars runs on parallel workers (e.g.
308 // CONNECTION_GRAPH) and a shared evaluator races on its internal error
309 // collector.
310 EXPRESSION_EVALUATOR evaluator;
311
312 while( ( text.Contains( wxT( "${" ) ) || text.Contains( wxT( "@{" ) ) ) && ++aDepth <= maxDepth )
313 {
314 // Always expand when ${} or @{} present to handle escape sequences (\${} and \@{})
315 // ExpandTextVars converts escapes to markers and expands ${} variables
316 // Don't expand if the only remaining $ or @ are in escape markers like <<<ESC_DOLLAR: or <<<ESC_AT:
317 if( text.Contains( wxT( "${" ) ) || text.Contains( wxT( "@{" ) ) )
318 text = ExpandTextVars( text, aResolver );
319
320 // Only evaluate if there are @{} expressions present (not escape markers)
321 // Don't evaluate if the only remaining @ are in escape markers like <<<ESC_AT:
322 if( text.Contains( wxT( "@{" ) ) )
323 text = evaluator.Evaluate( text ); // Evaluate math expressions
324 }
325
326 return text;
327}
328
329
330namespace
331{
332// Scan @p aText beginning at @p aStart (pointing just past an opening `${` or
333// `@{`), collect every nested and sibling reference, and advance @p aStart past
334// the matching closing brace. Returns the raw token body for the outer
335// reference so the caller can classify it. Escape markers (\${…} and \@{…})
336// are skipped. Brace nesting mirrors the ExpandTextVars state machine so
337// tokens like `${FOO:BAR(${BAZ})}` are captured as a single outer token with
338// BAZ picked up as a nested sibling.
339wxString walkRef( const wxString& aText, std::size_t& aPos, std::vector<TEXT_VAR_REF_KEY>& aOut );
340
341void collectRefsFrom( const wxString& aText, std::size_t aStart, std::size_t aEnd,
342 std::vector<TEXT_VAR_REF_KEY>& aOut )
343{
344 for( std::size_t i = aStart; i < aEnd; ++i )
345 {
346 const wxUniChar c = aText[i];
347
348 // Escape sequences: skip the whole escaped expression; its contents
349 // are a user literal, not a dependency source.
350 if( c == wxT( '\\' ) && i + 2 < aEnd && ( aText[i + 1] == wxT( '$' ) || aText[i + 1] == wxT( '@' ) )
351 && aText[i + 2] == wxT( '{' ) )
352 {
353 std::size_t j = i + 3;
354 int depth = 1;
355
356 while( j < aEnd && depth > 0 )
357 {
358 if( aText[j] == wxT( '{' ) )
359 depth++;
360 else if( aText[j] == wxT( '}' ) )
361 depth--;
362
363 ++j;
364 }
365
366 i = j - 1;
367 continue;
368 }
369
370 if( ( c == wxT( '$' ) || c == wxT( '@' ) ) && i + 1 < aEnd && aText[i + 1] == wxT( '{' ) )
371 {
372 std::size_t pos = i + 2;
373 const wxString token = walkRef( aText, pos, aOut );
374
375 // Math expressions (`@{...}`) contribute their nested variables as
376 // dependency edges but do not produce a named edge themselves — the
377 // expression text is not a resolvable source name.
378 if( c == wxT( '$' ) && !token.IsEmpty() )
379 aOut.push_back( TEXT_VAR_REF_KEY::FromToken( token ) );
380
381 // Clamp to aEnd - 1 so the outer for-loop increment doesn't go past
382 // the end on a malformed token.
383 i = ( pos > 0 ? pos - 1 : pos );
384 }
385 }
386}
387
388
389wxString walkRef( const wxString& aText, std::size_t& aPos, std::vector<TEXT_VAR_REF_KEY>& aOut )
390{
391 const std::size_t len = aText.length();
392 const std::size_t start = aPos;
393 int depth = 1;
394
395 while( aPos < len )
396 {
397 const wxUniChar c = aText[aPos];
398
399 if( c == wxT( '{' ) )
400 {
401 depth++;
402 }
403 else if( c == wxT( '}' ) )
404 {
405 depth--;
406
407 if( depth == 0 )
408 {
409 wxString body = aText.Mid( start, aPos - start );
410 aPos++; // consume the matching close-brace
411
412 // Recurse into the body so nested references are captured
413 // regardless of whether the outer resolves. ExpandTextVars'
414 // resolver path would suppress this for outer tokens beginning
415 // with ERC_WARNING / DRC_WARNING; a dependency tracker must
416 // not care about that (codex finding 3).
417 collectRefsFrom( body, 0, body.length(), aOut );
418 return body;
419 }
420 }
421
422 aPos++;
423 }
424
425 // Malformed token (ran off the end without a matching close-brace).
426 // Recurse into the partial body so nested inner references are still
427 // captured — `${FOO${BAR}` at EOF must still produce a BAR dependency.
428 wxString partial = aText.Mid( start, aPos - start );
429 collectRefsFrom( partial, 0, partial.length(), aOut );
430 return partial;
431}
432}
433
434
435std::vector<TEXT_VAR_REF_KEY> ExtractTextVarReferences( const wxString& aSource )
436{
437 std::vector<TEXT_VAR_REF_KEY> refs;
438
439 // Fast path: no reference syntax at all.
440 if( !aSource.Contains( wxT( "${" ) ) && !aSource.Contains( wxT( "@{" ) ) )
441 return refs;
442
443 collectRefsFrom( aSource, 0, aSource.length(), refs );
444 return refs;
445}
446
447
448wxString GetGeneratedFieldDisplayName( const wxString& aSource )
449{
450 std::function<bool( wxString* )> tokenExtractor = [&]( wxString* token ) -> bool
451 {
452 *token = *token; // token value is the token name
453 return true;
454 };
455
456 return ExpandTextVars( aSource, &tokenExtractor );
457}
458
459
460bool IsGeneratedField( const wxString& aSource )
461{
462 // Per-thread regex. Callers include parallel ERC/connection-graph workers.
463 thread_local wxRegEx expr( wxS( "^\\$\\{\\w*\\}$" ) );
464 return expr.Matches( aSource );
465}
466
467
468wxString DescribeRef( const wxString& aRef )
469{
470 if( aRef.IsEmpty() )
471 return wxT( "<i>" ) + _( "unannotated footprint" ) + wxT( " </i>" );
472 else
473 return EscapeHTML( aRef );
474}
475
476
477//
478// Stolen from wxExpandEnvVars and then heavily optimized
479//
480wxString KIwxExpandEnvVars( const wxString& str, const PROJECT* aProject, std::set<wxString>* aSet = nullptr )
481{
482 // If the same string is inserted twice, we have a loop
483 if( aSet )
484 {
485 if( auto [_, result] = aSet->insert( str ); !result )
486 return str;
487 }
488
489 size_t strlen = str.length();
490
491 wxString strResult;
492 strResult.Alloc( strlen ); // best guess (improves performance)
493
494 auto getVersionedEnvVar = []( const wxString& aMatch, wxString& aResult ) -> bool
495 {
496 for( const wxString& var : ENV_VAR::GetPredefinedEnvVars() )
497 {
498 if( var.Matches( aMatch ) )
499 {
500 const auto value = ENV_VAR::GetEnvVar<wxString>( var );
501
502 if( !value )
503 continue;
504
505 aResult += *value;
506 return true;
507 }
508 }
509
510 return false;
511 };
512
513 for( size_t n = 0; n < strlen; n++ )
514 {
515 wxUniChar str_n = str[n];
516
517 switch( str_n.GetValue() )
518 {
519#ifdef __WINDOWS__
520 case wxT( '%' ):
521#endif // __WINDOWS__
522 case wxT( '$' ):
523 {
524 Bracket bracket;
525#ifdef __WINDOWS__
526 if( str_n == wxT( '%' ) )
527 {
528 bracket = Bracket_Windows;
529 }
530 else
531#endif // __WINDOWS__
532 if( n == strlen - 1 )
533 {
534 bracket = Bracket_None;
535 }
536 else
537 {
538 switch( str[n + 1].GetValue() )
539 {
540 case wxT( '(' ):
541 bracket = Bracket_Normal;
542 str_n = str[++n]; // skip the bracket
543 break;
544
545 case wxT( '{' ):
546 bracket = Bracket_Curly;
547 str_n = str[++n]; // skip the bracket
548 break;
549
550 default: bracket = Bracket_None;
551 }
552 }
553
554 size_t m = n + 1;
555
556 if( m >= strlen )
557 break;
558
559 wxUniChar str_m = str[m];
560
561 while( wxIsalnum( str_m ) || str_m == wxT( '_' ) || str_m == wxT( ':' ) )
562 {
563 if( ++m == strlen )
564 {
565 str_m = 0;
566 break;
567 }
568
569 str_m = str[m];
570 }
571
572 wxString strVarName( str.c_str() + n + 1, m - n - 1 );
573
574 // NB: use wxGetEnv instead of wxGetenv as otherwise variables
575 // set through wxSetEnv may not be read correctly!
576 bool expanded = false;
577 wxString tmp = strVarName;
578
579 if( aProject && aProject->TextVarResolver( &tmp ) )
580 {
581 strResult += tmp;
582 expanded = true;
583 }
584 else if( wxGetEnv( strVarName, &tmp ) )
585 {
586 strResult += tmp;
587 expanded = true;
588 }
589 // Replace unmatched older variables with current locations
590 // If the user has the older location defined, that will be matched
591 // first above. But if they do not, this will ensure that their board still
592 // displays correctly
593 else if( strVarName.Contains( "KISYS3DMOD" ) || strVarName.Matches( "KICAD*_3DMODEL_DIR" ) )
594 {
595 if( getVersionedEnvVar( "KICAD*_3DMODEL_DIR", strResult ) )
596 expanded = true;
597 }
598 else if( strVarName.Matches( "KICAD*_SYMBOL_DIR" ) )
599 {
600 if( getVersionedEnvVar( "KICAD*_SYMBOL_DIR", strResult ) )
601 expanded = true;
602 }
603 else if( strVarName.Matches( "KICAD*_FOOTPRINT_DIR" ) )
604 {
605 if( getVersionedEnvVar( "KICAD*_FOOTPRINT_DIR", strResult ) )
606 expanded = true;
607 }
608 else if( strVarName.Matches( "KICAD*_3RD_PARTY" ) )
609 {
610 if( getVersionedEnvVar( "KICAD*_3RD_PARTY", strResult ) )
611 expanded = true;
612 }
613 else
614 {
615 // variable doesn't exist => don't change anything
616#ifdef __WINDOWS__
617 if( bracket != Bracket_Windows )
618#endif
619 if( bracket != Bracket_None )
620 strResult << str[n - 1];
621
622 strResult << str_n << strVarName;
623 }
624
625 // When a versioned-wildcard branch matched but no env var was found, emit
626 // the original ${VARNAME} text so the closing-bracket handler can append the
627 // closing bracket. Without this, the handler emits only '}', producing a
628 // garbage path like "}/Device.kicad_sym" instead of the full unexpanded var.
629 if( !expanded && bracket != Bracket_None )
630 {
631 auto isVersionedWildcard =
632 strVarName.Contains( wxT( "KISYS3DMOD" ) )
633 || strVarName.Matches( wxT( "KICAD*_3DMODEL_DIR" ) )
634 || strVarName.Matches( wxT( "KICAD*_SYMBOL_DIR" ) )
635 || strVarName.Matches( wxT( "KICAD*_FOOTPRINT_DIR" ) )
636 || strVarName.Matches( wxT( "KICAD*_3RD_PARTY" ) );
637
638 if( isVersionedWildcard )
639 {
640#ifdef __WINDOWS__
641 if( bracket != Bracket_Windows )
642#endif
643 strResult << str[n - 1];
644
645 strResult << str_n << strVarName;
646 }
647 }
648
649 // check the closing bracket
650 if( bracket != Bracket_None )
651 {
652 if( m == strlen || str_m != (wxChar) bracket )
653 {
654 // under MSW it's common to have '%' characters in the registry
655 // and it's annoying to have warnings about them each time, so
656 // ignore them silently if they are not used for env vars
657 //
658 // under Unix, OTOH, this warning could be useful for the user to
659 // understand why isn't the variable expanded as intended
660#ifndef __WINDOWS__
661 wxLogWarning( _( "Environment variables expansion failed: missing '%c' "
662 "at position %u in '%s'." ),
663 (char) bracket, (unsigned int) ( m + 1 ), str.c_str() );
664#endif // __WINDOWS__
665 }
666 else
667 {
668 // skip closing bracket unless the variables wasn't expanded
669 if( !expanded )
670 strResult << (wxChar) bracket;
671
672 m++;
673 }
674 }
675
676 n = m - 1; // skip variable name
677 str_n = str[n];
678 }
679 break;
680
681 case wxT( '\\' ):
682 // backslash can be used to suppress special meaning of % and $
683 if( n < strlen - 1 && ( str[n + 1] == wxT( '%' ) || str[n + 1] == wxT( '$' ) ) )
684 {
685 str_n = str[++n];
686 strResult += str_n;
687
688 break;
689 }
690
692
693 default: strResult += str_n;
694 }
695 }
696
697 std::set<wxString> loop_check;
698 auto first_pos = strResult.find_first_of( wxS( "{(%" ) );
699 auto last_pos = strResult.find_last_of( wxS( "})%" ) );
700
701 if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos )
702 strResult = KIwxExpandEnvVars( strResult, aProject, aSet ? aSet : &loop_check );
703
704 return strResult;
705}
706
707
708const wxString ExpandEnvVarSubstitutions( const wxString& aString, const PROJECT* aProject )
709{
710 // wxGetenv( wchar_t* ) is not re-entrant on linux.
711 // Put a lock on multithreaded use of wxGetenv( wchar_t* ), called from wxEpandEnvVars(),
712 static std::mutex getenv_mutex;
713
714 std::lock_guard<std::mutex> lock( getenv_mutex );
715
716 // We reserve the right to do this another way, by providing our own member function.
717 return KIwxExpandEnvVars( aString, aProject );
718}
719
720
721const wxString ResolveUriByEnvVars( const wxString& aUri, const PROJECT* aProject )
722{
723 wxString uri = ExpandTextVars( aUri, aProject );
724
725 return ExpandEnvVarSubstitutions( uri, aProject );
726}
727
728
729bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName, const wxString& aBaseFilename, REPORTER* aReporter )
730{
731 wxString msg;
732 wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
733
734 // make aTargetFullFileName path, which is relative to aBaseFilename path (if it is not
735 // already an absolute path) absolute:
736 if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
737 {
738 if( aReporter )
739 {
740 msg.Printf( _( "Cannot make path '%s' absolute with respect to '%s'." ), aTargetFullFileName->GetPath(),
741 baseFilePath );
742 aReporter->Report( msg, RPT_SEVERITY_ERROR );
743 }
744
745 return false;
746 }
747
748 // Ensure the path of aTargetFullFileName exists, and create it if needed:
749 wxString outputPath( aTargetFullFileName->GetPath() );
750
751 if( !wxFileName::DirExists( outputPath ) )
752 {
753 // Make every directory provided when the provided path doesn't exist
754 if( wxFileName::Mkdir( outputPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
755 {
756 if( aReporter )
757 {
758 msg.Printf( _( "Output directory '%s' created." ), outputPath );
759 aReporter->Report( msg, RPT_SEVERITY_INFO );
760 return true;
761 }
762 }
763 else
764 {
765 if( aReporter )
766 {
767 msg.Printf( _( "Cannot create output directory '%s'." ), outputPath );
768 aReporter->Report( msg, RPT_SEVERITY_ERROR );
769 }
770
771 return false;
772 }
773 }
774
775 return true;
776}
777
778
779wxString EnsureFileExtension( const wxString& aFilename, const wxString& aExtension )
780{
781 wxString newFilename( aFilename );
782
783 // It's annoying to throw up nag dialogs when the extension isn't right. Just fix it,
784 // but be careful not to destroy existing after-dot-text that isn't actually a bad
785 // extension, such as "Schematic_1.1".
786 if( newFilename.Lower().AfterLast( '.' ) != aExtension )
787 {
788 if( !newFilename.EndsWith( '.' ) )
789 newFilename.Append( '.' );
790
791 newFilename.Append( aExtension );
792 }
793
794 return newFilename;
795}
796
797
798wxString JoinExtensions( const std::vector<std::string>& aExts )
799{
800 wxString joined;
801
802 for( const std::string& ext : aExts )
803 {
804 if( !joined.empty() )
805 joined << wxS( ", " );
806
807 joined << wxS( "*." ) << ext;
808 }
809
810 return joined;
811}
812
813
821
828bool matchWild( const char* pat, const char* text, bool dot_special )
829{
830 if( !*text )
831 {
832 /* Match if both are empty. */
833 return !*pat;
834 }
835
836 const char *m = pat, *n = text, *ma = nullptr, *na = nullptr;
837 int just = 0, acount = 0, count = 0;
838
839 if( dot_special && ( *n == '.' ) )
840 {
841 /* Never match so that hidden Unix files
842 * are never found. */
843 return false;
844 }
845
846 for( ;; )
847 {
848 if( *m == '*' )
849 {
850 ma = ++m;
851 na = n;
852 just = 1;
853 acount = count;
854 }
855 else if( *m == '?' )
856 {
857 m++;
858
859 if( !*n++ )
860 return false;
861 }
862 else
863 {
864 if( *m == '\\' )
865 {
866 m++;
867
868 /* Quoting "nothing" is a bad thing */
869 if( !*m )
870 return false;
871 }
872
873 if( !*m )
874 {
875 /*
876 * If we are out of both strings or we just
877 * saw a wildcard, then we can say we have a
878 * match
879 */
880 if( !*n )
881 return true;
882
883 if( just )
884 return true;
885
886 just = 0;
887 goto not_matched;
888 }
889
890 /*
891 * We could check for *n == NULL at this point, but
892 * since it's more common to have a character there,
893 * check to see if they match first (m and n) and
894 * then if they don't match, THEN we can check for
895 * the NULL of n
896 */
897 just = 0;
898
899 if( *m == *n )
900 {
901 m++;
902 count++;
903 n++;
904 }
905 else
906 {
907 not_matched:
908
909 /*
910 * If there are no more characters in the
911 * string, but we still need to find another
912 * character (*m != NULL), then it will be
913 * impossible to match it
914 */
915 if( !*n )
916 return false;
917
918 if( ma )
919 {
920 m = ma;
921 n = ++na;
922 count = acount;
923 }
924 else
925 return false;
926 }
927 }
928 }
929}
930
931
933{
935 return false;
936
937 KICAD_MESSAGE_DIALOG dialog( nullptr,
938 _( "This operating system is not supported "
939 "by KiCad and its dependencies." ),
940 _( "Unsupported Operating System" ), wxOK | wxICON_EXCLAMATION );
941
942 dialog.SetExtendedMessage( _( "Any issues with KiCad on this system cannot "
943 "be reported to the official bugtracker." ) );
944 dialog.ShowModal();
945
946 return true;
947}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
High-level wrapper for evaluating mathematical and string expressions in wxString format.
wxString Evaluate(const wxString &aInput)
Main evaluation function - processes input string and evaluates all} expressions.
Container for project specific data.
Definition project.h:66
virtual bool TextVarResolver(wxString *aToken) const
Definition project.cpp:85
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:75
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:104
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition common.cpp:708
wxString JoinExtensions(const std::vector< std::string > &aExts)
Join a list of file extensions for use in a file dialog.
Definition common.cpp:798
wxString GetGeneratedFieldDisplayName(const wxString &aSource)
Returns any variables unexpanded, e.g.
Definition common.cpp:448
const wxString ResolveUriByEnvVars(const wxString &aUri, const PROJECT *aProject)
Replace any environment and/or text variables in URIs.
Definition common.cpp:721
wxString EnsureFileExtension(const wxString &aFilename, const wxString &aExtension)
It's annoying to throw up nag dialogs when the extension isn't right.
Definition common.cpp:779
bool WarnUserIfOperatingSystemUnsupported()
Checks if the operating system is explicitly unsupported and displays a disclaimer message box.
Definition common.cpp:932
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
Definition common.cpp:63
bool matchWild(const char *pat, const char *text, bool dot_special)
Performance enhancements to file and directory operations.
Definition common.cpp:828
bool EnsureFileDirectoryExists(wxFileName *aTargetFullFileName, const wxString &aBaseFilename, REPORTER *aReporter)
Make aTargetFullFileName absolute and create the path of this file if it doesn't yet exist.
Definition common.cpp:729
wxString KIwxExpandEnvVars(const wxString &str, const PROJECT *aProject, std::set< wxString > *aSet=nullptr)
Definition common.cpp:480
Bracket
Definition common.cpp:53
@ Bracket_Max
Definition common.cpp:60
@ Bracket_None
Definition common.cpp:54
@ Bracket_Normal
Definition common.cpp:55
@ Bracket_Curly
Definition common.cpp:56
bool IsGeneratedField(const wxString &aSource)
Returns true if the string is generated, e.g contains a single text var reference.
Definition common.cpp:460
wxString DescribeRef(const wxString &aRef)
Returns a user-visible HTML string describing a footprint reference designator.
Definition common.cpp:468
wxString ResolveTextVars(const wxString &aSource, const std::function< bool(wxString *)> *aResolver, int &aDepth)
Multi-pass text variable expansion and math expression evaluation.
Definition common.cpp:300
std::vector< TEXT_VAR_REF_KEY > ExtractTextVarReferences(const wxString &aSource)
Lex-scan aSource and return every ${...} reference that appears, without resolving.
Definition common.cpp:435
The common library.
#define FOR_ERC_DRC
Expand '${var-name}' templates in text.
Definition common.h:99
This file is part of the common library.
#define KICAD_MESSAGE_DIALOG
Definition confirm.h:52
#define _(s)
Base window classes and related definitions.
Functions related to environment variables, including help functions.
int m_ResolveTextRecursionDepth
The number of recursions to resolve text variables.
This file contains miscellaneous commonly used macros and functions.
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition macros.h:83
KICOMMON_API std::optional< VAL_TYPE > GetEnvVar(const wxString &aEnvVarName)
Get an environment variable as a specific type, if set correctly.
KICOMMON_API const std::vector< wxString > & GetPredefinedEnvVars()
Get the list of pre-defined environment variables.
Definition env_vars.cpp:61
bool IsOperatingSystemUnsupported()
Checks if the Operating System is explicitly unsupported and we want to prevent users from sending bu...
Definition unix/app.cpp:70
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
static TEXT_VAR_REF_KEY FromToken(const wxString &aToken)
Parse a raw token (the text between ${ and }) into a key using lexical classification only — no looku...
wxString result
Test unit parsing edge cases and error handling.