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
462 static wxRegEx expr( wxS(
"^\\$\\{\\w*\\}$" ) );
463 return expr.Matches( aSource );
470 return wxT(
"<i>" ) +
_(
"unannotated footprint" ) + wxT(
" </i>" );
488 size_t strlen = str.length();
491 strResult.Alloc( strlen );
493 auto getVersionedEnvVar = [](
const wxString& aMatch, wxString& aResult ) ->
bool
497 if( var.Matches( aMatch ) )
512 for(
size_t n = 0; n < strlen; n++ )
514 wxUniChar str_n = str[n];
516 switch( str_n.GetValue() )
525 if( str_n == wxT(
'%' ) )
527 bracket = Bracket_Windows;
531 if( n == strlen - 1 )
537 switch( str[n + 1].GetValue() )
558 wxUniChar str_m = str[m];
560 while( wxIsalnum( str_m ) || str_m == wxT(
'_' ) || str_m == wxT(
':' ) )
571 wxString strVarName( str.c_str() + n + 1, m - n - 1 );
575 bool expanded =
false;
576 wxString tmp = strVarName;
583 else if( wxGetEnv( strVarName, &tmp ) )
592 else if( strVarName.Contains(
"KISYS3DMOD" ) || strVarName.Matches(
"KICAD*_3DMODEL_DIR" ) )
594 if( getVersionedEnvVar(
"KICAD*_3DMODEL_DIR", strResult ) )
597 else if( strVarName.Matches(
"KICAD*_SYMBOL_DIR" ) )
599 if( getVersionedEnvVar(
"KICAD*_SYMBOL_DIR", strResult ) )
602 else if( strVarName.Matches(
"KICAD*_FOOTPRINT_DIR" ) )
604 if( getVersionedEnvVar(
"KICAD*_FOOTPRINT_DIR", strResult ) )
607 else if( strVarName.Matches(
"KICAD*_3RD_PARTY" ) )
609 if( getVersionedEnvVar(
"KICAD*_3RD_PARTY", strResult ) )
616 if( bracket != Bracket_Windows )
619 strResult << str[n - 1];
621 strResult << str_n << strVarName;
630 auto isVersionedWildcard =
631 strVarName.Contains( wxT(
"KISYS3DMOD" ) )
632 || strVarName.Matches( wxT(
"KICAD*_3DMODEL_DIR" ) )
633 || strVarName.Matches( wxT(
"KICAD*_SYMBOL_DIR" ) )
634 || strVarName.Matches( wxT(
"KICAD*_FOOTPRINT_DIR" ) )
635 || strVarName.Matches( wxT(
"KICAD*_3RD_PARTY" ) );
637 if( isVersionedWildcard )
640 if( bracket != Bracket_Windows )
642 strResult << str[n - 1];
644 strResult << str_n << strVarName;
651 if( m == strlen || str_m != (wxChar) bracket )
660 wxLogWarning(
_(
"Environment variables expansion failed: missing '%c' "
661 "at position %u in '%s'." ),
662 (
char) bracket, (
unsigned int) ( m + 1 ), str.c_str() );
669 strResult << (wxChar) bracket;
682 if( n < strlen - 1 && ( str[n + 1] == wxT(
'%' ) || str[n + 1] == wxT(
'$' ) ) )
692 default: strResult += str_n;
696 std::set<wxString> loop_check;
697 auto first_pos = strResult.find_first_of( wxS(
"{(%" ) );
698 auto last_pos = strResult.find_last_of( wxS(
"})%" ) );
700 if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos )
711 static std::mutex getenv_mutex;
713 std::lock_guard<std::mutex> lock( getenv_mutex );
731 wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
735 if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
739 msg.Printf(
_(
"Cannot make path '%s' absolute with respect to '%s'." ), aTargetFullFileName->GetPath(),
748 wxString outputPath( aTargetFullFileName->GetPath() );
750 if( !wxFileName::DirExists( outputPath ) )
753 if( wxFileName::Mkdir( outputPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
757 msg.Printf(
_(
"Output directory '%s' created." ), outputPath );
766 msg.Printf(
_(
"Cannot create output directory '%s'." ), outputPath );
780 wxString newFilename( aFilename );
785 if( newFilename.Lower().AfterLast(
'.' ) != aExtension )
787 if( !newFilename.EndsWith(
'.' ) )
788 newFilename.Append(
'.' );
790 newFilename.Append( aExtension );
801 for(
const std::string& ext : aExts )
803 if( !joined.empty() )
804 joined << wxS(
", " );
806 joined << wxS(
"*." ) << ext;
835 const char *m = pat, *n =
text, *ma =
nullptr, *na =
nullptr;
836 int just = 0, acount = 0, count = 0;
838 if( dot_special && ( *n ==
'.' ) )
937 _(
"This operating system is not supported "
938 "by KiCad and its dependencies." ),
939 _(
"Unsupported Operating System" ), wxOK | wxICON_EXCLAMATION );
941 dialog.SetExtendedMessage(
_(
"Any issues with KiCad on this system cannot "
942 "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.