38#include <wx/stdpaths.h>
54 Bracket_Windows =
'%',
61 std::function<bool( wxString* )> projectResolver = [&]( wxString* token ) ->
bool
72 wxString
path = aPath;
77 path.Replace( wxT(
"\\${" ), wxT(
"/${" ) );
78 path.Replace( wxT(
"\\@{" ), wxT(
"/@{" ) );
84wxString
ExpandTextVars(
const wxString& aSource,
const std::function<
bool( wxString* )>* aResolver,
int aFlags,
88 size_t sourceLen = aSource.length();
90 newbuf.Alloc( sourceLen );
95 for(
size_t i = 0; i < sourceLen; ++i )
99 if( i + 14 <= sourceLen && aSource.Mid( i, 14 ) == wxT(
"<<<ESC_DOLLAR:" ) )
102 newbuf.append( wxT(
"<<<ESC_DOLLAR:" ) );
107 while( i < sourceLen && braceCount > 0 )
109 if( aSource[i] ==
'{' )
111 else if( aSource[i] ==
'}' )
114 newbuf.append( aSource[i] );
120 else if( i + 10 <= sourceLen && aSource.Mid( i, 10 ) == wxT(
"<<<ESC_AT:" ) )
123 newbuf.append( wxT(
"<<<ESC_AT:" ) );
128 while( i < sourceLen && braceCount > 0 )
130 if( aSource[i] ==
'{' )
132 else if( aSource[i] ==
'}' )
135 newbuf.append( aSource[i] );
145 if( aSource[i] ==
'\\' && i + 1 < sourceLen )
147 if( ( aSource[i + 1] ==
'$' || aSource[i + 1] ==
'@' ) && i + 2 < sourceLen && aSource[i + 2] ==
'{' )
151 if( aSource[i + 1] ==
'$' )
152 newbuf.append( wxT(
"<<<ESC_DOLLAR:" ) );
154 newbuf.append( wxT(
"<<<ESC_AT:" ) );
159 for( i = i + 1; i < sourceLen && braceDepth > 0; ++i )
161 if( aSource[i] ==
'{' )
163 else if( aSource[i] ==
'}' )
166 newbuf.append( aSource[i] );
173 if( ( aSource[i] ==
'$' || aSource[i] ==
'@' ) && i + 1 < sourceLen && aSource[i + 1] ==
'{' )
175 bool isMathExpr = ( aSource[i] ==
'@' );
179 for( i = i + 2; i < sourceLen; ++i )
183 if( i + 14 <= sourceLen && aSource.Mid( i, 14 ) == wxT(
"<<<ESC_DOLLAR:" ) )
185 token.append( wxT(
"<<<ESC_DOLLAR:" ) );
189 int markerBraceCount = 1;
191 while( i < sourceLen && markerBraceCount > 0 )
193 if( aSource[i] ==
'{' )
195 else if( aSource[i] ==
'}' )
198 token.append( aSource[i] );
205 else if( i + 10 <= sourceLen && aSource.Mid( i, 10 ) == wxT(
"<<<ESC_AT:" ) )
207 token.append( wxT(
"<<<ESC_AT:" ) );
211 int markerBraceCount = 1;
213 while( i < sourceLen && markerBraceCount > 0 )
215 if( aSource[i] ==
'{' )
217 else if( aSource[i] ==
'}' )
220 token.append( aSource[i] );
228 if( aSource[i] ==
'{' )
231 token.append( aSource[i] );
233 else if( aSource[i] ==
'}' )
237 if( braceDepth == 0 )
240 token.append( aSource[i] );
244 token.append( aSource[i] );
248 if( token.IsEmpty() )
255 if( ( token.Contains( wxT(
"${" ) ) || token.Contains( wxT(
"@{" ) ) ) && aDepth < maxDepth )
262 newbuf.append( wxT(
"@{" ) + token + wxT(
"}" ) );
268 if( ( token.Contains( wxT(
"${" ) ) || token.Contains( wxT(
"@{" ) ) ) && aDepth < maxDepth )
273 if( token.Contains( wxT(
"@{" ) ) )
279 token = evaluator.
Evaluate( token );
284 && ( token.StartsWith( wxS(
"ERC_WARNING" ) ) || token.StartsWith( wxS(
"ERC_ERROR" ) )
285 || token.StartsWith( wxS(
"DRC_WARNING" ) ) || token.StartsWith( wxS(
"DRC_ERROR" ) ) ) )
289 else if( aResolver && ( *aResolver )( &token ) )
291 newbuf.append( token );
296 newbuf.append(
"${" + token +
"}" );
302 newbuf.append( aSource[i] );
310wxString
ResolveTextVars(
const wxString& aSource,
const std::function<
bool( wxString* )>* aResolver,
int& aDepth )
314 wxString
text = aSource;
322 while( (
text.Contains( wxT(
"${" ) ) ||
text.Contains( wxT(
"@{" ) ) ) && ++aDepth <= maxDepth )
327 if(
text.Contains( wxT(
"${" ) ) ||
text.Contains( wxT(
"@{" ) ) )
332 if(
text.Contains( wxT(
"@{" ) ) )
349wxString walkRef(
const wxString& aText, std::size_t& aPos, std::vector<TEXT_VAR_REF_KEY>& aOut );
351void collectRefsFrom(
const wxString& aText, std::size_t aStart, std::size_t aEnd,
352 std::vector<TEXT_VAR_REF_KEY>& aOut )
354 for( std::size_t i = aStart; i < aEnd; ++i )
356 const wxUniChar c = aText[i];
360 if( c == wxT(
'\\' ) && i + 2 < aEnd && ( aText[i + 1] == wxT(
'$' ) || aText[i + 1] == wxT(
'@' ) )
361 && aText[i + 2] == wxT(
'{' ) )
363 std::size_t j = i + 3;
366 while( j < aEnd && depth > 0 )
368 if( aText[j] == wxT(
'{' ) )
370 else if( aText[j] == wxT(
'}' ) )
380 if( ( c == wxT(
'$' ) || c == wxT(
'@' ) ) && i + 1 < aEnd && aText[i + 1] == wxT(
'{' ) )
382 std::size_t pos = i + 2;
383 const wxString token = walkRef( aText, pos, aOut );
388 if( c == wxT(
'$' ) && !token.IsEmpty() )
393 i = ( pos > 0 ? pos - 1 : pos );
399wxString walkRef(
const wxString& aText, std::size_t& aPos, std::vector<TEXT_VAR_REF_KEY>& aOut )
401 const std::size_t len = aText.length();
402 const std::size_t start = aPos;
407 const wxUniChar c = aText[aPos];
409 if( c == wxT(
'{' ) )
413 else if( c == wxT(
'}' ) )
419 wxString body = aText.Mid( start, aPos - start );
427 collectRefsFrom( body, 0, body.length(), aOut );
438 wxString partial = aText.Mid( start, aPos - start );
439 collectRefsFrom( partial, 0, partial.length(), aOut );
447 std::vector<TEXT_VAR_REF_KEY> refs;
450 if( !aSource.Contains( wxT(
"${" ) ) && !aSource.Contains( wxT(
"@{" ) ) )
453 collectRefsFrom( aSource, 0, aSource.length(), refs );
460 std::function<bool( wxString* )> tokenExtractor = [&]( wxString* token ) ->
bool
473 thread_local wxRegEx expr( wxS(
"^\\$\\{\\w*\\}$" ) );
474 return expr.Matches( aSource );
481 return wxT(
"<i>" ) +
_(
"unannotated footprint" ) + wxT(
" </i>" );
499 size_t strlen = str.length();
502 strResult.Alloc( strlen );
504 auto getVersionedEnvVar = [](
const wxString& aMatch, wxString& aResult ) ->
bool
508 if( var.Matches( aMatch ) )
523 for(
size_t n = 0; n < strlen; n++ )
525 wxUniChar str_n = str[n];
527 switch( str_n.GetValue() )
536 if( str_n == wxT(
'%' ) )
538 bracket = Bracket_Windows;
542 if( n == strlen - 1 )
548 switch( str[n + 1].GetValue() )
569 wxUniChar str_m = str[m];
571 while( wxIsalnum( str_m ) || str_m == wxT(
'_' ) || str_m == wxT(
':' ) )
582 wxString strVarName( str.c_str() + n + 1, m - n - 1 );
586 bool expanded =
false;
587 wxString tmp = strVarName;
594 else if( wxGetEnv( strVarName, &tmp ) )
603 else if( strVarName.Contains(
"KISYS3DMOD" )
606 if( getVersionedEnvVar(
"KICAD*_3DMODEL_DIR", strResult ) )
609 else if( strVarName ==
"KICAD_SYMBOL_DIR"
612 if( getVersionedEnvVar(
"KICAD*_SYMBOL_DIR", strResult ) )
617 if( getVersionedEnvVar(
"KICAD*_FOOTPRINT_DIR", strResult ) )
622 if( getVersionedEnvVar(
"KICAD*_3RD_PARTY", strResult ) )
629 if( bracket != Bracket_Windows )
632 strResult << str[n - 1];
634 strResult << str_n << strVarName;
643 auto isVersionedWildcard =
644 strVarName.Contains( wxT(
"KISYS3DMOD" ) )
645 || strVarName == wxT(
"KICAD_SYMBOL_DIR" )
651 if( isVersionedWildcard )
654 if( bracket != Bracket_Windows )
656 strResult << str[n - 1];
658 strResult << str_n << strVarName;
665 if( m == strlen || str_m != (wxChar) bracket )
674 wxLogWarning(
_(
"Environment variables expansion failed: missing '%c' "
675 "at position %u in '%s'." ),
676 (
char) bracket, (
unsigned int) ( m + 1 ), str.c_str() );
683 strResult << (wxChar) bracket;
696 if( n < strlen - 1 && ( str[n + 1] == wxT(
'%' ) || str[n + 1] == wxT(
'$' ) ) )
706 default: strResult += str_n;
710 std::set<wxString> loop_check;
711 auto first_pos = strResult.find_first_of( wxS(
"{(%" ) );
712 auto last_pos = strResult.find_last_of( wxS(
"})%" ) );
714 if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos )
725 static std::mutex getenv_mutex;
727 std::lock_guard<std::mutex> lock( getenv_mutex );
745 wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
749 if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
753 msg.Printf(
_(
"Cannot make path '%s' absolute with respect to '%s'." ), aTargetFullFileName->GetPath(),
762 wxString outputPath( aTargetFullFileName->GetPath() );
764 if( !wxFileName::DirExists( outputPath ) )
767 if( wxFileName::Mkdir( outputPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
771 msg.Printf(
_(
"Output directory '%s' created." ), outputPath );
780 msg.Printf(
_(
"Cannot create output directory '%s'." ), outputPath );
794 wxString newFilename( aFilename );
799 if( newFilename.Lower().AfterLast(
'.' ) != aExtension )
801 if( !newFilename.EndsWith(
'.' ) )
802 newFilename.Append(
'.' );
804 newFilename.Append( aExtension );
815 for(
const std::string& ext : aExts )
817 if( !joined.empty() )
818 joined << wxS(
", " );
820 joined << wxS(
"*." ) << ext;
849 const char *m = pat, *n =
text, *ma =
nullptr, *na =
nullptr;
850 int just = 0, acount = 0, count = 0;
852 if( dot_special && ( *n ==
'.' ) )
951 _(
"This operating system is not supported "
952 "by KiCad and its dependencies." ),
953 _(
"Unsupported Operating System" ), wxOK | wxICON_EXCLAMATION );
955 dialog.SetExtendedMessage(
_(
"Any issues with KiCad on this system cannot "
956 "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.
wxString NormalizeFilePathForTextVars(const wxString &aPath)
Normalize a file path so its text variables survive ExpandTextVars.
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 bool IsVersionedEnvVar(const wxString &aName, const wxString &aBaseName)
Test whether a name is a versioned KiCad environment variable for the given base, i....
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.