42#include <wx/stdpaths.h>
58 Bracket_Windows =
'%',
65 std::function<bool( wxString* )> projectResolver = [&]( wxString* token ) ->
bool
74wxString
ExpandTextVars(
const wxString& aSource,
const std::function<
bool( wxString* )>* aResolver,
int aFlags,
78 size_t sourceLen = aSource.length();
80 newbuf.Alloc( sourceLen );
85 for(
size_t i = 0; i < sourceLen; ++i )
89 if( i + 14 <= sourceLen && aSource.Mid( i, 14 ) == wxT(
"<<<ESC_DOLLAR:" ) )
92 newbuf.append( wxT(
"<<<ESC_DOLLAR:" ) );
97 while( i < sourceLen && braceCount > 0 )
99 if( aSource[i] ==
'{' )
101 else if( aSource[i] ==
'}' )
104 newbuf.append( aSource[i] );
110 else if( i + 10 <= sourceLen && aSource.Mid( i, 10 ) == wxT(
"<<<ESC_AT:" ) )
113 newbuf.append( wxT(
"<<<ESC_AT:" ) );
118 while( i < sourceLen && braceCount > 0 )
120 if( aSource[i] ==
'{' )
122 else if( aSource[i] ==
'}' )
125 newbuf.append( aSource[i] );
135 if( aSource[i] ==
'\\' && i + 1 < sourceLen )
137 if( ( aSource[i + 1] ==
'$' || aSource[i + 1] ==
'@' ) && i + 2 < sourceLen && aSource[i + 2] ==
'{' )
141 if( aSource[i + 1] ==
'$' )
142 newbuf.append( wxT(
"<<<ESC_DOLLAR:" ) );
144 newbuf.append( wxT(
"<<<ESC_AT:" ) );
149 for( i = i + 1; i < sourceLen && braceDepth > 0; ++i )
151 if( aSource[i] ==
'{' )
153 else if( aSource[i] ==
'}' )
156 newbuf.append( aSource[i] );
163 if( ( aSource[i] ==
'$' || aSource[i] ==
'@' ) && i + 1 < sourceLen && aSource[i + 1] ==
'{' )
165 bool isMathExpr = ( aSource[i] ==
'@' );
169 for( i = i + 2; i < sourceLen; ++i )
173 if( i + 14 <= sourceLen && aSource.Mid( i, 14 ) == wxT(
"<<<ESC_DOLLAR:" ) )
175 token.append( wxT(
"<<<ESC_DOLLAR:" ) );
179 int markerBraceCount = 1;
181 while( i < sourceLen && markerBraceCount > 0 )
183 if( aSource[i] ==
'{' )
185 else if( aSource[i] ==
'}' )
188 token.append( aSource[i] );
195 else if( i + 10 <= sourceLen && aSource.Mid( i, 10 ) == wxT(
"<<<ESC_AT:" ) )
197 token.append( wxT(
"<<<ESC_AT:" ) );
201 int markerBraceCount = 1;
203 while( i < sourceLen && markerBraceCount > 0 )
205 if( aSource[i] ==
'{' )
207 else if( aSource[i] ==
'}' )
210 token.append( aSource[i] );
218 if( aSource[i] ==
'{' )
221 token.append( aSource[i] );
223 else if( aSource[i] ==
'}' )
227 if( braceDepth == 0 )
230 token.append( aSource[i] );
234 token.append( aSource[i] );
238 if( token.IsEmpty() )
245 if( ( token.Contains( wxT(
"${" ) ) || token.Contains( wxT(
"@{" ) ) ) && aDepth < maxDepth )
252 newbuf.append( wxT(
"@{" ) + token + wxT(
"}" ) );
258 if( ( token.Contains( wxT(
"${" ) ) || token.Contains( wxT(
"@{" ) ) ) && aDepth < maxDepth )
263 if( token.Contains( wxT(
"@{" ) ) )
269 token = evaluator.
Evaluate( token );
274 && ( token.StartsWith( wxS(
"ERC_WARNING" ) ) || token.StartsWith( wxS(
"ERC_ERROR" ) )
275 || token.StartsWith( wxS(
"DRC_WARNING" ) ) || token.StartsWith( wxS(
"DRC_ERROR" ) ) ) )
279 else if( aResolver && ( *aResolver )( &token ) )
281 newbuf.append( token );
286 newbuf.append(
"${" + token +
"}" );
292 newbuf.append( aSource[i] );
300wxString
ResolveTextVars(
const wxString& aSource,
const std::function<
bool( wxString* )>* aResolver,
int& aDepth )
304 wxString
text = aSource;
312 while( (
text.Contains( wxT(
"${" ) ) ||
text.Contains( wxT(
"@{" ) ) ) && ++aDepth <= maxDepth )
317 if(
text.Contains( wxT(
"${" ) ) ||
text.Contains( wxT(
"@{" ) ) )
322 if(
text.Contains( wxT(
"@{" ) ) )
339wxString walkRef(
const wxString& aText, std::size_t& aPos, std::vector<TEXT_VAR_REF_KEY>& aOut );
341void collectRefsFrom(
const wxString& aText, std::size_t aStart, std::size_t aEnd,
342 std::vector<TEXT_VAR_REF_KEY>& aOut )
344 for( std::size_t i = aStart; i < aEnd; ++i )
346 const wxUniChar c = aText[i];
350 if( c == wxT(
'\\' ) && i + 2 < aEnd && ( aText[i + 1] == wxT(
'$' ) || aText[i + 1] == wxT(
'@' ) )
351 && aText[i + 2] == wxT(
'{' ) )
353 std::size_t j = i + 3;
356 while( j < aEnd && depth > 0 )
358 if( aText[j] == wxT(
'{' ) )
360 else if( aText[j] == wxT(
'}' ) )
370 if( ( c == wxT(
'$' ) || c == wxT(
'@' ) ) && i + 1 < aEnd && aText[i + 1] == wxT(
'{' ) )
372 std::size_t pos = i + 2;
373 const wxString token = walkRef( aText, pos, aOut );
378 if( c == wxT(
'$' ) && !token.IsEmpty() )
383 i = ( pos > 0 ? pos - 1 : pos );
389wxString walkRef(
const wxString& aText, std::size_t& aPos, std::vector<TEXT_VAR_REF_KEY>& aOut )
391 const std::size_t len = aText.length();
392 const std::size_t start = aPos;
397 const wxUniChar c = aText[aPos];
399 if( c == wxT(
'{' ) )
403 else if( c == wxT(
'}' ) )
409 wxString body = aText.Mid( start, aPos - start );
417 collectRefsFrom( body, 0, body.length(), aOut );
428 wxString partial = aText.Mid( start, aPos - start );
429 collectRefsFrom( partial, 0, partial.length(), aOut );
437 std::vector<TEXT_VAR_REF_KEY> refs;
440 if( !aSource.Contains( wxT(
"${" ) ) && !aSource.Contains( wxT(
"@{" ) ) )
443 collectRefsFrom( aSource, 0, aSource.length(), refs );
450 std::function<bool( wxString* )> tokenExtractor = [&]( wxString* token ) ->
bool
463 thread_local wxRegEx expr( wxS(
"^\\$\\{\\w*\\}$" ) );
464 return expr.Matches( aSource );
471 return wxT(
"<i>" ) +
_(
"unannotated footprint" ) + wxT(
" </i>" );
489 size_t strlen = str.length();
492 strResult.Alloc( strlen );
494 auto getVersionedEnvVar = [](
const wxString& aMatch, wxString& aResult ) ->
bool
498 if( var.Matches( aMatch ) )
513 for(
size_t n = 0; n < strlen; n++ )
515 wxUniChar str_n = str[n];
517 switch( str_n.GetValue() )
526 if( str_n == wxT(
'%' ) )
528 bracket = Bracket_Windows;
532 if( n == strlen - 1 )
538 switch( str[n + 1].GetValue() )
559 wxUniChar str_m = str[m];
561 while( wxIsalnum( str_m ) || str_m == wxT(
'_' ) || str_m == wxT(
':' ) )
572 wxString strVarName( str.c_str() + n + 1, m - n - 1 );
576 bool expanded =
false;
577 wxString tmp = strVarName;
584 else if( wxGetEnv( strVarName, &tmp ) )
593 else if( strVarName.Contains(
"KISYS3DMOD" ) || strVarName.Matches(
"KICAD*_3DMODEL_DIR" ) )
595 if( getVersionedEnvVar(
"KICAD*_3DMODEL_DIR", strResult ) )
598 else if( strVarName.Matches(
"KICAD*_SYMBOL_DIR" ) )
600 if( getVersionedEnvVar(
"KICAD*_SYMBOL_DIR", strResult ) )
603 else if( strVarName.Matches(
"KICAD*_FOOTPRINT_DIR" ) )
605 if( getVersionedEnvVar(
"KICAD*_FOOTPRINT_DIR", strResult ) )
608 else if( strVarName.Matches(
"KICAD*_3RD_PARTY" ) )
610 if( getVersionedEnvVar(
"KICAD*_3RD_PARTY", strResult ) )
617 if( bracket != Bracket_Windows )
620 strResult << str[n - 1];
622 strResult << str_n << strVarName;
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" ) );
638 if( isVersionedWildcard )
641 if( bracket != Bracket_Windows )
643 strResult << str[n - 1];
645 strResult << str_n << strVarName;
652 if( m == strlen || str_m != (wxChar) bracket )
661 wxLogWarning(
_(
"Environment variables expansion failed: missing '%c' "
662 "at position %u in '%s'." ),
663 (
char) bracket, (
unsigned int) ( m + 1 ), str.c_str() );
670 strResult << (wxChar) bracket;
683 if( n < strlen - 1 && ( str[n + 1] == wxT(
'%' ) || str[n + 1] == wxT(
'$' ) ) )
693 default: strResult += str_n;
697 std::set<wxString> loop_check;
698 auto first_pos = strResult.find_first_of( wxS(
"{(%" ) );
699 auto last_pos = strResult.find_last_of( wxS(
"})%" ) );
701 if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos )
712 static std::mutex getenv_mutex;
714 std::lock_guard<std::mutex> lock( getenv_mutex );
732 wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
736 if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
740 msg.Printf(
_(
"Cannot make path '%s' absolute with respect to '%s'." ), aTargetFullFileName->GetPath(),
749 wxString outputPath( aTargetFullFileName->GetPath() );
751 if( !wxFileName::DirExists( outputPath ) )
754 if( wxFileName::Mkdir( outputPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
758 msg.Printf(
_(
"Output directory '%s' created." ), outputPath );
767 msg.Printf(
_(
"Cannot create output directory '%s'." ), outputPath );
781 wxString newFilename( aFilename );
786 if( newFilename.Lower().AfterLast(
'.' ) != aExtension )
788 if( !newFilename.EndsWith(
'.' ) )
789 newFilename.Append(
'.' );
791 newFilename.Append( aExtension );
802 for(
const std::string& ext : aExts )
804 if( !joined.empty() )
805 joined << wxS(
", " );
807 joined << wxS(
"*." ) << ext;
836 const char *m = pat, *n =
text, *ma =
nullptr, *na =
nullptr;
837 int just = 0, acount = 0, count = 0;
839 if( dot_special && ( *n ==
'.' ) )
938 _(
"This operating system is not supported "
939 "by KiCad and its dependencies." ),
940 _(
"Unsupported Operating System" ), wxOK | wxICON_EXCLAMATION );
942 dialog.SetExtendedMessage(
_(
"Any issues with KiCad on this system cannot "
943 "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.