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(
"@{" ) ) )
268 token = evaluator.
Evaluate( token );
273 && ( token.StartsWith( wxS(
"ERC_WARNING" ) ) || token.StartsWith( wxS(
"ERC_ERROR" ) )
274 || token.StartsWith( wxS(
"DRC_WARNING" ) ) || token.StartsWith( wxS(
"DRC_ERROR" ) ) ) )
278 else if( aResolver && ( *aResolver )( &token ) )
280 newbuf.append( token );
285 newbuf.append(
"${" + token +
"}" );
291 newbuf.append( aSource[i] );
299wxString
ResolveTextVars(
const wxString& aSource,
const std::function<
bool( wxString* )>* aResolver,
int& aDepth )
303 wxString
text = aSource;
311 while( (
text.Contains( wxT(
"${" ) ) ||
text.Contains( wxT(
"@{" ) ) ) && ++aDepth <= maxDepth )
316 if(
text.Contains( wxT(
"${" ) ) ||
text.Contains( wxT(
"@{" ) ) )
321 if(
text.Contains( wxT(
"@{" ) ) )
331 std::function<bool( wxString* )> tokenExtractor = [&]( wxString* token ) ->
bool
343 static wxRegEx expr( wxS(
"^\\$\\{\\w*\\}$" ) );
344 return expr.Matches( aSource );
351 return wxT(
"<i>" ) +
_(
"unannotated footprint" ) + wxT(
" </i>" );
369 size_t strlen = str.length();
372 strResult.Alloc( strlen );
374 auto getVersionedEnvVar = [](
const wxString& aMatch, wxString& aResult ) ->
bool
378 if( var.Matches( aMatch ) )
393 for(
size_t n = 0; n < strlen; n++ )
395 wxUniChar str_n = str[n];
397 switch( str_n.GetValue() )
406 if( str_n == wxT(
'%' ) )
408 bracket = Bracket_Windows;
412 if( n == strlen - 1 )
418 switch( str[n + 1].GetValue() )
439 wxUniChar str_m = str[m];
441 while( wxIsalnum( str_m ) || str_m == wxT(
'_' ) || str_m == wxT(
':' ) )
452 wxString strVarName( str.c_str() + n + 1, m - n - 1 );
456 bool expanded =
false;
457 wxString tmp = strVarName;
464 else if( wxGetEnv( strVarName, &tmp ) )
473 else if( strVarName.Contains(
"KISYS3DMOD" ) || strVarName.Matches(
"KICAD*_3DMODEL_DIR" ) )
475 if( getVersionedEnvVar(
"KICAD*_3DMODEL_DIR", strResult ) )
478 else if( strVarName.Matches(
"KICAD*_SYMBOL_DIR" ) )
480 if( getVersionedEnvVar(
"KICAD*_SYMBOL_DIR", strResult ) )
483 else if( strVarName.Matches(
"KICAD*_FOOTPRINT_DIR" ) )
485 if( getVersionedEnvVar(
"KICAD*_FOOTPRINT_DIR", strResult ) )
488 else if( strVarName.Matches(
"KICAD*_3RD_PARTY" ) )
490 if( getVersionedEnvVar(
"KICAD*_3RD_PARTY", strResult ) )
497 if( bracket != Bracket_Windows )
500 strResult << str[n - 1];
502 strResult << str_n << strVarName;
511 auto isVersionedWildcard =
512 strVarName.Contains( wxT(
"KISYS3DMOD" ) )
513 || strVarName.Matches( wxT(
"KICAD*_3DMODEL_DIR" ) )
514 || strVarName.Matches( wxT(
"KICAD*_SYMBOL_DIR" ) )
515 || strVarName.Matches( wxT(
"KICAD*_FOOTPRINT_DIR" ) )
516 || strVarName.Matches( wxT(
"KICAD*_3RD_PARTY" ) );
518 if( isVersionedWildcard )
521 if( bracket != Bracket_Windows )
523 strResult << str[n - 1];
525 strResult << str_n << strVarName;
532 if( m == strlen || str_m != (wxChar) bracket )
541 wxLogWarning(
_(
"Environment variables expansion failed: missing '%c' "
542 "at position %u in '%s'." ),
543 (
char) bracket, (
unsigned int) ( m + 1 ), str.c_str() );
550 strResult << (wxChar) bracket;
563 if( n < strlen - 1 && ( str[n + 1] == wxT(
'%' ) || str[n + 1] == wxT(
'$' ) ) )
573 default: strResult += str_n;
577 std::set<wxString> loop_check;
578 auto first_pos = strResult.find_first_of( wxS(
"{(%" ) );
579 auto last_pos = strResult.find_last_of( wxS(
"})%" ) );
581 if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos )
592 static std::mutex getenv_mutex;
594 std::lock_guard<std::mutex> lock( getenv_mutex );
612 wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
616 if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
620 msg.Printf(
_(
"Cannot make path '%s' absolute with respect to '%s'." ), aTargetFullFileName->GetPath(),
629 wxString outputPath( aTargetFullFileName->GetPath() );
631 if( !wxFileName::DirExists( outputPath ) )
634 if( wxFileName::Mkdir( outputPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
638 msg.Printf(
_(
"Output directory '%s' created." ), outputPath );
647 msg.Printf(
_(
"Cannot create output directory '%s'." ), outputPath );
661 wxString newFilename( aFilename );
666 if( newFilename.Lower().AfterLast(
'.' ) != aExtension )
668 if( !newFilename.EndsWith(
'.' ) )
669 newFilename.Append(
'.' );
671 newFilename.Append( aExtension );
682 for(
const std::string& ext : aExts )
684 if( !joined.empty() )
685 joined << wxS(
", " );
687 joined << wxS(
"*." ) << ext;
716 const char *m = pat, *n =
text, *ma =
nullptr, *na =
nullptr;
717 int just = 0, acount = 0, count = 0;
719 if( dot_special && ( *n ==
'.' ) )
818 _(
"This operating system is not supported "
819 "by KiCad and its dependencies." ),
820 _(
"Unsupported Operating System" ), wxOK | wxICON_EXCLAMATION );
822 dialog.SetExtendedMessage(
_(
"Any issues with KiCad on this system cannot "
823 "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.