38#include <wx/stdpaths.h>
54 Bracket_Windows =
'%',
61 std::function<bool( wxString* )> projectResolver = [&]( wxString* token ) ->
bool
70wxString
ExpandTextVars(
const wxString& aSource,
const std::function<
bool( wxString* )>* aResolver,
int aFlags,
74 size_t sourceLen = aSource.length();
76 newbuf.Alloc( sourceLen );
81 for(
size_t i = 0; i < sourceLen; ++i )
85 if( i + 14 <= sourceLen && aSource.Mid( i, 14 ) == wxT(
"<<<ESC_DOLLAR:" ) )
88 newbuf.append( wxT(
"<<<ESC_DOLLAR:" ) );
93 while( i < sourceLen && braceCount > 0 )
95 if( aSource[i] ==
'{' )
97 else if( aSource[i] ==
'}' )
100 newbuf.append( aSource[i] );
106 else if( i + 10 <= sourceLen && aSource.Mid( i, 10 ) == wxT(
"<<<ESC_AT:" ) )
109 newbuf.append( wxT(
"<<<ESC_AT:" ) );
114 while( i < sourceLen && braceCount > 0 )
116 if( aSource[i] ==
'{' )
118 else if( aSource[i] ==
'}' )
121 newbuf.append( aSource[i] );
131 if( aSource[i] ==
'\\' && i + 1 < sourceLen )
133 if( ( aSource[i + 1] ==
'$' || aSource[i + 1] ==
'@' ) && i + 2 < sourceLen && aSource[i + 2] ==
'{' )
137 if( aSource[i + 1] ==
'$' )
138 newbuf.append( wxT(
"<<<ESC_DOLLAR:" ) );
140 newbuf.append( wxT(
"<<<ESC_AT:" ) );
145 for( i = i + 1; i < sourceLen && braceDepth > 0; ++i )
147 if( aSource[i] ==
'{' )
149 else if( aSource[i] ==
'}' )
152 newbuf.append( aSource[i] );
159 if( ( aSource[i] ==
'$' || aSource[i] ==
'@' ) && i + 1 < sourceLen && aSource[i + 1] ==
'{' )
161 bool isMathExpr = ( aSource[i] ==
'@' );
165 for( i = i + 2; i < sourceLen; ++i )
169 if( i + 14 <= sourceLen && aSource.Mid( i, 14 ) == wxT(
"<<<ESC_DOLLAR:" ) )
171 token.append( wxT(
"<<<ESC_DOLLAR:" ) );
175 int markerBraceCount = 1;
177 while( i < sourceLen && markerBraceCount > 0 )
179 if( aSource[i] ==
'{' )
181 else if( aSource[i] ==
'}' )
184 token.append( aSource[i] );
191 else if( i + 10 <= sourceLen && aSource.Mid( i, 10 ) == wxT(
"<<<ESC_AT:" ) )
193 token.append( wxT(
"<<<ESC_AT:" ) );
197 int markerBraceCount = 1;
199 while( i < sourceLen && markerBraceCount > 0 )
201 if( aSource[i] ==
'{' )
203 else if( aSource[i] ==
'}' )
206 token.append( aSource[i] );
214 if( aSource[i] ==
'{' )
217 token.append( aSource[i] );
219 else if( aSource[i] ==
'}' )
223 if( braceDepth == 0 )
226 token.append( aSource[i] );
230 token.append( aSource[i] );
234 if( token.IsEmpty() )
241 if( ( token.Contains( wxT(
"${" ) ) || token.Contains( wxT(
"@{" ) ) ) && aDepth < maxDepth )
248 newbuf.append( wxT(
"@{" ) + token + wxT(
"}" ) );
254 if( ( token.Contains( wxT(
"${" ) ) || token.Contains( wxT(
"@{" ) ) ) && aDepth < maxDepth )
259 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;
308 while( (
text.Contains( wxT(
"${" ) ) ||
text.Contains( wxT(
"@{" ) ) ) && ++aDepth <= maxDepth )
313 if(
text.Contains( wxT(
"${" ) ) ||
text.Contains( wxT(
"@{" ) ) )
318 if(
text.Contains( wxT(
"@{" ) ) )
335wxString walkRef(
const wxString& aText, std::size_t& aPos, std::vector<TEXT_VAR_REF_KEY>& aOut );
337void collectRefsFrom(
const wxString& aText, std::size_t aStart, std::size_t aEnd,
338 std::vector<TEXT_VAR_REF_KEY>& aOut )
340 for( std::size_t i = aStart; i < aEnd; ++i )
342 const wxUniChar c = aText[i];
346 if( c == wxT(
'\\' ) && i + 2 < aEnd && ( aText[i + 1] == wxT(
'$' ) || aText[i + 1] == wxT(
'@' ) )
347 && aText[i + 2] == wxT(
'{' ) )
349 std::size_t j = i + 3;
352 while( j < aEnd && depth > 0 )
354 if( aText[j] == wxT(
'{' ) )
356 else if( aText[j] == wxT(
'}' ) )
366 if( ( c == wxT(
'$' ) || c == wxT(
'@' ) ) && i + 1 < aEnd && aText[i + 1] == wxT(
'{' ) )
368 std::size_t pos = i + 2;
369 const wxString token = walkRef( aText, pos, aOut );
374 if( c == wxT(
'$' ) && !token.IsEmpty() )
379 i = ( pos > 0 ? pos - 1 : pos );
385wxString walkRef(
const wxString& aText, std::size_t& aPos, std::vector<TEXT_VAR_REF_KEY>& aOut )
387 const std::size_t len = aText.length();
388 const std::size_t start = aPos;
393 const wxUniChar c = aText[aPos];
395 if( c == wxT(
'{' ) )
399 else if( c == wxT(
'}' ) )
405 wxString body = aText.Mid( start, aPos - start );
413 collectRefsFrom( body, 0, body.length(), aOut );
424 wxString partial = aText.Mid( start, aPos - start );
425 collectRefsFrom( partial, 0, partial.length(), aOut );
433 std::vector<TEXT_VAR_REF_KEY> refs;
436 if( !aSource.Contains( wxT(
"${" ) ) && !aSource.Contains( wxT(
"@{" ) ) )
439 collectRefsFrom( aSource, 0, aSource.length(), refs );
446 std::function<bool( wxString* )> tokenExtractor = [&]( wxString* token ) ->
bool
459 thread_local wxRegEx expr( wxS(
"^\\$\\{\\w*\\}$" ) );
460 return expr.Matches( aSource );
467 return wxT(
"<i>" ) +
_(
"unannotated footprint" ) + wxT(
" </i>" );
485 size_t strlen = str.length();
488 strResult.Alloc( strlen );
490 auto getVersionedEnvVar = [](
const wxString& aMatch, wxString& aResult ) ->
bool
494 if( var.Matches( aMatch ) )
509 for(
size_t n = 0; n < strlen; n++ )
511 wxUniChar str_n = str[n];
513 switch( str_n.GetValue() )
522 if( str_n == wxT(
'%' ) )
524 bracket = Bracket_Windows;
528 if( n == strlen - 1 )
534 switch( str[n + 1].GetValue() )
555 wxUniChar str_m = str[m];
557 while( wxIsalnum( str_m ) || str_m == wxT(
'_' ) || str_m == wxT(
':' ) )
568 wxString strVarName( str.c_str() + n + 1, m - n - 1 );
572 bool expanded =
false;
573 wxString tmp = strVarName;
580 else if( wxGetEnv( strVarName, &tmp ) )
589 else if( strVarName.Contains(
"KISYS3DMOD" ) || strVarName.Matches(
"KICAD*_3DMODEL_DIR" ) )
591 if( getVersionedEnvVar(
"KICAD*_3DMODEL_DIR", strResult ) )
594 else if( strVarName.Matches(
"KICAD*_SYMBOL_DIR" ) )
596 if( getVersionedEnvVar(
"KICAD*_SYMBOL_DIR", strResult ) )
599 else if( strVarName.Matches(
"KICAD*_FOOTPRINT_DIR" ) )
601 if( getVersionedEnvVar(
"KICAD*_FOOTPRINT_DIR", strResult ) )
604 else if( strVarName.Matches(
"KICAD*_3RD_PARTY" ) )
606 if( getVersionedEnvVar(
"KICAD*_3RD_PARTY", strResult ) )
613 if( bracket != Bracket_Windows )
616 strResult << str[n - 1];
618 strResult << str_n << strVarName;
627 auto isVersionedWildcard =
628 strVarName.Contains( wxT(
"KISYS3DMOD" ) )
629 || strVarName.Matches( wxT(
"KICAD*_3DMODEL_DIR" ) )
630 || strVarName.Matches( wxT(
"KICAD*_SYMBOL_DIR" ) )
631 || strVarName.Matches( wxT(
"KICAD*_FOOTPRINT_DIR" ) )
632 || strVarName.Matches( wxT(
"KICAD*_3RD_PARTY" ) );
634 if( isVersionedWildcard )
637 if( bracket != Bracket_Windows )
639 strResult << str[n - 1];
641 strResult << str_n << strVarName;
648 if( m == strlen || str_m != (wxChar) bracket )
657 wxLogWarning(
_(
"Environment variables expansion failed: missing '%c' "
658 "at position %u in '%s'." ),
659 (
char) bracket, (
unsigned int) ( m + 1 ), str.c_str() );
666 strResult << (wxChar) bracket;
679 if( n < strlen - 1 && ( str[n + 1] == wxT(
'%' ) || str[n + 1] == wxT(
'$' ) ) )
689 default: strResult += str_n;
693 std::set<wxString> loop_check;
694 auto first_pos = strResult.find_first_of( wxS(
"{(%" ) );
695 auto last_pos = strResult.find_last_of( wxS(
"})%" ) );
697 if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos )
708 static std::mutex getenv_mutex;
710 std::lock_guard<std::mutex> lock( getenv_mutex );
728 wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
732 if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
736 msg.Printf(
_(
"Cannot make path '%s' absolute with respect to '%s'." ), aTargetFullFileName->GetPath(),
745 wxString outputPath( aTargetFullFileName->GetPath() );
747 if( !wxFileName::DirExists( outputPath ) )
750 if( wxFileName::Mkdir( outputPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
754 msg.Printf(
_(
"Output directory '%s' created." ), outputPath );
763 msg.Printf(
_(
"Cannot create output directory '%s'." ), outputPath );
777 wxString newFilename( aFilename );
782 if( newFilename.Lower().AfterLast(
'.' ) != aExtension )
784 if( !newFilename.EndsWith(
'.' ) )
785 newFilename.Append(
'.' );
787 newFilename.Append( aExtension );
798 for(
const std::string& ext : aExts )
800 if( !joined.empty() )
801 joined << wxS(
", " );
803 joined << wxS(
"*." ) << ext;
832 const char *m = pat, *n =
text, *ma =
nullptr, *na =
nullptr;
833 int just = 0, acount = 0, count = 0;
835 if( dot_special && ( *n ==
'.' ) )
934 _(
"This operating system is not supported "
935 "by KiCad and its dependencies." ),
936 _(
"Unsupported Operating System" ), wxOK | wxICON_EXCLAMATION );
938 dialog.SetExtendedMessage(
_(
"Any issues with KiCad on this system cannot "
939 "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.
std::vector< TEXT_VAR_REF_KEY > ExtractTextVarReferences(const wxString &aSource)
Lex-scan aSource and return every ${...} reference that appears, without resolving.
#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.
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.