41#include <wx/stdpaths.h>
57 Bracket_Windows =
'%',
64 std::function<bool( wxString* )> projectResolver = [&]( wxString* token ) ->
bool
73wxString
ExpandTextVars(
const wxString& aSource,
const std::function<
bool( wxString* )>* aResolver,
int aFlags,
77 size_t sourceLen = aSource.length();
79 newbuf.Alloc( sourceLen );
84 for(
size_t i = 0; i < sourceLen; ++i )
88 if( i + 14 <= sourceLen && aSource.Mid( i, 14 ) == wxT(
"<<<ESC_DOLLAR:" ) )
91 newbuf.append( wxT(
"<<<ESC_DOLLAR:" ) );
96 while( i < sourceLen && braceCount > 0 )
98 if( aSource[i] ==
'{' )
100 else if( aSource[i] ==
'}' )
103 newbuf.append( aSource[i] );
109 else if( i + 10 <= sourceLen && aSource.Mid( i, 10 ) == wxT(
"<<<ESC_AT:" ) )
112 newbuf.append( wxT(
"<<<ESC_AT:" ) );
117 while( i < sourceLen && braceCount > 0 )
119 if( aSource[i] ==
'{' )
121 else if( aSource[i] ==
'}' )
124 newbuf.append( aSource[i] );
134 if( aSource[i] ==
'\\' && i + 1 < sourceLen )
136 if( ( aSource[i + 1] ==
'$' || aSource[i + 1] ==
'@' ) && i + 2 < sourceLen && aSource[i + 2] ==
'{' )
140 if( aSource[i + 1] ==
'$' )
141 newbuf.append( wxT(
"<<<ESC_DOLLAR:" ) );
143 newbuf.append( wxT(
"<<<ESC_AT:" ) );
148 for( i = i + 1; i < sourceLen && braceDepth > 0; ++i )
150 if( aSource[i] ==
'{' )
152 else if( aSource[i] ==
'}' )
155 newbuf.append( aSource[i] );
162 if( ( aSource[i] ==
'$' || aSource[i] ==
'@' ) && i + 1 < sourceLen && aSource[i + 1] ==
'{' )
164 bool isMathExpr = ( aSource[i] ==
'@' );
168 for( i = i + 2; i < sourceLen; ++i )
172 if( i + 14 <= sourceLen && aSource.Mid( i, 14 ) == wxT(
"<<<ESC_DOLLAR:" ) )
174 token.append( wxT(
"<<<ESC_DOLLAR:" ) );
178 int markerBraceCount = 1;
180 while( i < sourceLen && markerBraceCount > 0 )
182 if( aSource[i] ==
'{' )
184 else if( aSource[i] ==
'}' )
187 token.append( aSource[i] );
194 else if( i + 10 <= sourceLen && aSource.Mid( i, 10 ) == wxT(
"<<<ESC_AT:" ) )
196 token.append( wxT(
"<<<ESC_AT:" ) );
200 int markerBraceCount = 1;
202 while( i < sourceLen && markerBraceCount > 0 )
204 if( aSource[i] ==
'{' )
206 else if( aSource[i] ==
'}' )
209 token.append( aSource[i] );
217 if( aSource[i] ==
'{' )
220 token.append( aSource[i] );
222 else if( aSource[i] ==
'}' )
226 if( braceDepth == 0 )
229 token.append( aSource[i] );
233 token.append( aSource[i] );
237 if( token.IsEmpty() )
244 if( ( token.Contains( wxT(
"${" ) ) || token.Contains( wxT(
"@{" ) ) ) && aDepth < maxDepth )
251 newbuf.append( wxT(
"@{" ) + token + wxT(
"}" ) );
257 if( ( token.Contains( wxT(
"${" ) ) || token.Contains( wxT(
"@{" ) ) ) && aDepth < maxDepth )
262 if( token.Contains( wxT(
"@{" ) ) )
265 token = evaluator.
Evaluate( token );
270 && ( token.StartsWith( wxS(
"ERC_WARNING" ) ) || token.StartsWith( wxS(
"ERC_ERROR" ) )
271 || token.StartsWith( wxS(
"DRC_WARNING" ) ) || token.StartsWith( wxS(
"DRC_ERROR" ) ) ) )
275 else if( aResolver && ( *aResolver )( &token ) )
277 newbuf.append( token );
282 newbuf.append(
"${" + token +
"}" );
288 newbuf.append( aSource[i] );
296wxString
ResolveTextVars(
const wxString& aSource,
const std::function<
bool( wxString* )>* aResolver,
int& aDepth )
300 wxString
text = aSource;
305 while( (
text.Contains( wxT(
"${" ) ) ||
text.Contains( wxT(
"@{" ) ) ) && ++aDepth <= maxDepth )
310 if(
text.Contains( wxT(
"${" ) ) ||
text.Contains( wxT(
"@{" ) ) )
315 if(
text.Contains( wxT(
"@{" ) ) )
325 std::function<bool( wxString* )> tokenExtractor = [&]( wxString* token ) ->
bool
337 static wxRegEx expr( wxS(
"^\\$\\{\\w*\\}$" ) );
338 return expr.Matches( aSource );
345 return wxT(
"<i>" ) +
_(
"unannotated footprint" ) + wxT(
" </i>" );
363 size_t strlen = str.length();
366 strResult.Alloc( strlen );
368 auto getVersionedEnvVar = [](
const wxString& aMatch, wxString& aResult ) ->
bool
372 if( var.Matches( aMatch ) )
387 for(
size_t n = 0; n < strlen; n++ )
389 wxUniChar str_n = str[n];
391 switch( str_n.GetValue() )
400 if( str_n == wxT(
'%' ) )
402 bracket = Bracket_Windows;
406 if( n == strlen - 1 )
412 switch( str[n + 1].GetValue() )
433 wxUniChar str_m = str[m];
435 while( wxIsalnum( str_m ) || str_m == wxT(
'_' ) || str_m == wxT(
':' ) )
446 wxString strVarName( str.c_str() + n + 1, m - n - 1 );
450 bool expanded =
false;
451 wxString tmp = strVarName;
458 else if( wxGetEnv( strVarName, &tmp ) )
467 else if( strVarName.Contains(
"KISYS3DMOD" ) || strVarName.Matches(
"KICAD*_3DMODEL_DIR" ) )
469 if( getVersionedEnvVar(
"KICAD*_3DMODEL_DIR", strResult ) )
472 else if( strVarName.Matches(
"KICAD*_SYMBOL_DIR" ) )
474 if( getVersionedEnvVar(
"KICAD*_SYMBOL_DIR", strResult ) )
477 else if( strVarName.Matches(
"KICAD*_FOOTPRINT_DIR" ) )
479 if( getVersionedEnvVar(
"KICAD*_FOOTPRINT_DIR", strResult ) )
482 else if( strVarName.Matches(
"KICAD*_3RD_PARTY" ) )
484 if( getVersionedEnvVar(
"KICAD*_3RD_PARTY", strResult ) )
491 if( bracket != Bracket_Windows )
494 strResult << str[n - 1];
496 strResult << str_n << strVarName;
502 if( m == strlen || str_m != (wxChar) bracket )
511 wxLogWarning(
_(
"Environment variables expansion failed: missing '%c' "
512 "at position %u in '%s'." ),
513 (
char) bracket, (
unsigned int) ( m + 1 ), str.c_str() );
520 strResult << (wxChar) bracket;
533 if( n < strlen - 1 && ( str[n + 1] == wxT(
'%' ) || str[n + 1] == wxT(
'$' ) ) )
543 default: strResult += str_n;
547 std::set<wxString> loop_check;
548 auto first_pos = strResult.find_first_of( wxS(
"{(%" ) );
549 auto last_pos = strResult.find_last_of( wxS(
"})%" ) );
551 if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos )
562 static std::mutex getenv_mutex;
564 std::lock_guard<std::mutex> lock( getenv_mutex );
582 wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
586 if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
590 msg.Printf(
_(
"Cannot make path '%s' absolute with respect to '%s'." ), aTargetFullFileName->GetPath(),
599 wxString outputPath( aTargetFullFileName->GetPath() );
601 if( !wxFileName::DirExists( outputPath ) )
604 if( wxFileName::Mkdir( outputPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
608 msg.Printf(
_(
"Output directory '%s' created." ), outputPath );
617 msg.Printf(
_(
"Cannot create output directory '%s'." ), outputPath );
631 wxString newFilename( aFilename );
636 if( newFilename.Lower().AfterLast(
'.' ) != aExtension )
638 if( !newFilename.EndsWith(
'.' ) )
639 newFilename.Append(
'.' );
641 newFilename.Append( aExtension );
652 for(
const std::string& ext : aExts )
654 if( !joined.empty() )
655 joined << wxS(
", " );
657 joined << wxS(
"*." ) << ext;
686 const char *m = pat, *n =
text, *ma =
nullptr, *na =
nullptr;
687 int just = 0, acount = 0, count = 0;
689 if( dot_special && ( *n ==
'.' ) )
788 _(
"This operating system is not supported "
789 "by KiCad and its dependencies." ),
790 _(
"Unsupported Operating System" ), wxOK | wxICON_EXCLAMATION );
792 dialog.SetExtendedMessage(
_(
"Any issues with KiCad on this system cannot "
793 "be reported to the official bugtracker." ) );
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.
virtual bool TextVarResolver(wxString *aToken) const
A pure virtual class used to derive REPORTER objects from.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
wxString JoinExtensions(const std::vector< std::string > &aExts)
Join a list of file extensions for use in a file dialog.
wxString GetGeneratedFieldDisplayName(const wxString &aSource)
Returns any variables unexpanded, e.g.
const wxString ResolveUriByEnvVars(const wxString &aUri, const PROJECT *aProject)
Replace any environment and/or text variables in URIs.
wxString EnsureFileExtension(const wxString &aFilename, const wxString &aExtension)
It's annoying to throw up nag dialogs when the extension isn't right.
bool WarnUserIfOperatingSystemUnsupported()
Checks if the operating system is explicitly unsupported and displays a disclaimer message box.
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
bool matchWild(const char *pat, const char *text, bool dot_special)
Performance enhancements to file and directory operations.
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.
wxString KIwxExpandEnvVars(const wxString &str, const PROJECT *aProject, std::set< wxString > *aSet=nullptr)
bool IsGeneratedField(const wxString &aSource)
Returns true if the string is generated, e.g contains a single text var reference.
wxString DescribeRef(const wxString &aRef)
Returns a user-visible HTML string describing a footprint reference designator.
wxString ResolveTextVars(const wxString &aSource, const std::function< bool(wxString *)> *aResolver, int &aDepth)
Multi-pass text variable expansion and math expression evaluation.
#define FOR_ERC_DRC
Expand '${var-name}' templates in text.
This file is part of the common library.
#define KICAD_MESSAGE_DIALOG
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 ...
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.
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
wxString result
Test unit parsing edge cases and error handling.