40#include <wx/stdpaths.h>
56 Bracket_Windows =
'%',
63 std::function<bool( wxString* )> projectResolver = [&]( wxString* token ) ->
bool
72wxString
ExpandTextVars(
const wxString& aSource,
const std::function<
bool( wxString* )>* aResolver,
int aFlags,
76 size_t sourceLen = aSource.length();
78 newbuf.Alloc( sourceLen );
83 for(
size_t i = 0; i < sourceLen; ++i )
87 if( i + 14 <= sourceLen && aSource.Mid( i, 14 ) == wxT(
"<<<ESC_DOLLAR:" ) )
90 newbuf.append( wxT(
"<<<ESC_DOLLAR:" ) );
95 while( i < sourceLen && braceCount > 0 )
97 if( aSource[i] ==
'{' )
99 else if( aSource[i] ==
'}' )
102 newbuf.append( aSource[i] );
108 else if( i + 10 <= sourceLen && aSource.Mid( i, 10 ) == wxT(
"<<<ESC_AT:" ) )
111 newbuf.append( wxT(
"<<<ESC_AT:" ) );
116 while( i < sourceLen && braceCount > 0 )
118 if( aSource[i] ==
'{' )
120 else if( aSource[i] ==
'}' )
123 newbuf.append( aSource[i] );
133 if( aSource[i] ==
'\\' && i + 1 < sourceLen )
135 if( ( aSource[i + 1] ==
'$' || aSource[i + 1] ==
'@' ) && i + 2 < sourceLen && aSource[i + 2] ==
'{' )
139 if( aSource[i + 1] ==
'$' )
140 newbuf.append( wxT(
"<<<ESC_DOLLAR:" ) );
142 newbuf.append( wxT(
"<<<ESC_AT:" ) );
147 for( i = i + 1; i < sourceLen && braceDepth > 0; ++i )
149 if( aSource[i] ==
'{' )
151 else if( aSource[i] ==
'}' )
154 newbuf.append( aSource[i] );
161 if( ( aSource[i] ==
'$' || aSource[i] ==
'@' ) && i + 1 < sourceLen && aSource[i + 1] ==
'{' )
163 bool isMathExpr = ( aSource[i] ==
'@' );
167 for( i = i + 2; i < sourceLen; ++i )
169 if( aSource[i] ==
'{' )
172 token.append( aSource[i] );
174 else if( aSource[i] ==
'}' )
177 if( braceDepth == 0 )
180 token.append( aSource[i] );
184 token.append( aSource[i] );
188 if( token.IsEmpty() )
195 if( ( token.Contains( wxT(
"${" ) ) || token.Contains( wxT(
"@{" ) ) ) && aDepth < maxDepth )
202 newbuf.append( wxT(
"@{" ) + token + wxT(
"}" ) );
208 if( ( token.Contains( wxT(
"${" ) ) || token.Contains( wxT(
"@{" ) ) ) && aDepth < maxDepth )
213 if( token.Contains( wxT(
"@{" ) ) )
216 token = evaluator.
Evaluate( token );
221 && ( token.StartsWith( wxS(
"ERC_WARNING" ) ) || token.StartsWith( wxS(
"ERC_ERROR" ) )
222 || token.StartsWith( wxS(
"DRC_WARNING" ) ) || token.StartsWith( wxS(
"DRC_ERROR" ) ) ) )
226 else if( aResolver && ( *aResolver )( &token ) )
228 newbuf.append( token );
233 newbuf.append(
"${" + token +
"}" );
239 newbuf.append( aSource[i] );
247wxString
ResolveTextVars(
const wxString& aSource,
const std::function<
bool( wxString* )>* aResolver,
int& aDepth )
251 wxString
text = aSource;
256 while( (
text.Contains( wxT(
"${" ) ) ||
text.Contains( wxT(
"@{" ) ) ) && ++aDepth <= maxDepth )
261 if(
text.Contains( wxT(
"${" ) ) ||
text.Contains( wxT(
"@{" ) ) )
266 if(
text.Contains( wxT(
"@{" ) ) )
276 std::function<bool( wxString* )> tokenExtractor = [&]( wxString* token ) ->
bool
288 static wxRegEx expr( wxS(
"^\\$\\{\\w*\\}$" ) );
289 return expr.Matches( aSource );
296 return wxT(
"<i>" ) +
_(
"unannotated footprint" ) + wxT(
" </i>" );
314 size_t strlen = str.length();
317 strResult.Alloc( strlen );
319 auto getVersionedEnvVar = [](
const wxString& aMatch, wxString& aResult ) ->
bool
323 if( var.Matches( aMatch ) )
338 for(
size_t n = 0; n < strlen; n++ )
340 wxUniChar str_n = str[n];
342 switch( str_n.GetValue() )
351 if( str_n == wxT(
'%' ) )
353 bracket = Bracket_Windows;
357 if( n == strlen - 1 )
363 switch( str[n + 1].GetValue() )
384 wxUniChar str_m = str[m];
386 while( wxIsalnum( str_m ) || str_m == wxT(
'_' ) || str_m == wxT(
':' ) )
397 wxString strVarName( str.c_str() + n + 1, m - n - 1 );
401 bool expanded =
false;
402 wxString tmp = strVarName;
409 else if( wxGetEnv( strVarName, &tmp ) )
418 else if( strVarName.Contains(
"KISYS3DMOD" ) || strVarName.Matches(
"KICAD*_3DMODEL_DIR" ) )
420 if( getVersionedEnvVar(
"KICAD*_3DMODEL_DIR", strResult ) )
423 else if( strVarName.Matches(
"KICAD*_SYMBOL_DIR" ) )
425 if( getVersionedEnvVar(
"KICAD*_SYMBOL_DIR", strResult ) )
428 else if( strVarName.Matches(
"KICAD*_FOOTPRINT_DIR" ) )
430 if( getVersionedEnvVar(
"KICAD*_FOOTPRINT_DIR", strResult ) )
433 else if( strVarName.Matches(
"KICAD*_3RD_PARTY" ) )
435 if( getVersionedEnvVar(
"KICAD*_3RD_PARTY", strResult ) )
442 if( bracket != Bracket_Windows )
445 strResult << str[n - 1];
447 strResult << str_n << strVarName;
453 if( m == strlen || str_m != (wxChar) bracket )
462 wxLogWarning(
_(
"Environment variables expansion failed: missing '%c' "
463 "at position %u in '%s'." ),
464 (
char) bracket, (
unsigned int) ( m + 1 ), str.c_str() );
471 strResult << (wxChar) bracket;
484 if( n < strlen - 1 && ( str[n + 1] == wxT(
'%' ) || str[n + 1] == wxT(
'$' ) ) )
494 default: strResult += str_n;
498 std::set<wxString> loop_check;
499 auto first_pos = strResult.find_first_of( wxS(
"{(%" ) );
500 auto last_pos = strResult.find_last_of( wxS(
"})%" ) );
502 if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos )
513 static std::mutex getenv_mutex;
515 std::lock_guard<std::mutex> lock( getenv_mutex );
533 wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
537 if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
541 msg.Printf(
_(
"Cannot make path '%s' absolute with respect to '%s'." ), aTargetFullFileName->GetPath(),
550 wxString outputPath( aTargetFullFileName->GetPath() );
552 if( !wxFileName::DirExists( outputPath ) )
555 if( wxFileName::Mkdir( outputPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
559 msg.Printf(
_(
"Output directory '%s' created." ), outputPath );
568 msg.Printf(
_(
"Cannot create output directory '%s'." ), outputPath );
582 wxString newFilename( aFilename );
587 if( newFilename.Lower().AfterLast(
'.' ) != aExtension )
589 if( !newFilename.EndsWith(
'.' ) )
590 newFilename.Append(
'.' );
592 newFilename.Append( aExtension );
603 for(
const std::string& ext : aExts )
605 if( !joined.empty() )
606 joined << wxS(
", " );
608 joined << wxS(
"*." ) << ext;
637 const char *m = pat, *n =
text, *ma =
nullptr, *na =
nullptr;
638 int just = 0, acount = 0, count = 0;
640 if( dot_special && ( *n ==
'.' ) )
737#if wxUSE_DATETIME && defined( __WIN32__ ) && !defined( __WXMICROWIN__ )
743static wxInt64 EPOCH_OFFSET_IN_MSEC = wxLL( 11644473600000 );
746static void ConvertFileTimeToWx( wxDateTime* dt,
const FILETIME& ft )
748 wxLongLong t( ft.dwHighDateTime, ft.dwLowDateTime );
750 t -= EPOCH_OFFSET_IN_MSEC;
752 *dt = wxDateTime( t );
766long long TimestampDir(
const wxString& aDirPath,
const wxString& aFilespec )
768 long long timestamp = 0;
770#if defined( __WIN32__ )
775 std::wstring filespec( aDirPath.t_str() );
777 filespec += aFilespec.t_str();
779 WIN32_FIND_DATA findData;
780 wxDateTime lastModDate;
782 HANDLE fileHandle = ::FindFirstFile( filespec.data(), &findData );
784 if( fileHandle != INVALID_HANDLE_VALUE )
788 ConvertFileTimeToWx( &lastModDate, findData.ftLastWriteTime );
789 timestamp += lastModDate.GetValue().GetValue();
792 timestamp += findData.nFileSizeLow;
793 }
while( FindNextFile( fileHandle, &findData ) != 0 );
796 FindClose( fileHandle );
800 std::string filespec( aFilespec.fn_str() );
801 std::string dir_path( aDirPath.fn_str() );
803 DIR* dir = opendir( dir_path.c_str() );
807 for( dirent* dir_entry = readdir( dir ); dir_entry; dir_entry = readdir( dir ) )
809 if( !
matchWild( filespec.c_str(), dir_entry->d_name,
true ) )
812 std::string entry_path = dir_path +
'/' + dir_entry->d_name;
813 struct stat entry_stat;
815 if( wxCRT_Lstat( entry_path.c_str(), &entry_stat ) == 0 )
818 if( S_ISLNK( entry_stat.st_mode ) )
820 char buffer[PATH_MAX + 1];
821 ssize_t pathLen = readlink( entry_path.c_str(), buffer, PATH_MAX );
825 struct stat linked_stat;
826 buffer[pathLen] =
'\0';
827 entry_path = dir_path + buffer;
829 if( wxCRT_Lstat( entry_path.c_str(), &linked_stat ) == 0 )
831 entry_stat = linked_stat;
841 if( S_ISREG( entry_stat.st_mode ) )
843 timestamp += entry_stat.st_mtime * 1000;
846 timestamp += entry_stat.st_size;
852 timestamp += (signed) std::hash<std::string>{}( std::string( dir_entry->d_name ) );
869 wxMessageDialog dialog(
nullptr,
870 _(
"This operating system is not supported "
871 "by KiCad and its dependencies." ),
872 _(
"Unsupported Operating System" ), wxOK | wxICON_EXCLAMATION );
874 dialog.SetExtendedMessage(
_(
"Any issues with KiCad on this system cannot "
875 "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.
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function private to src/common/...
#define FOR_ERC_DRC
Expand '${var-name}' templates in text.
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.