37#include <wx/stdpaths.h>
53 Bracket_Windows =
'%',
61 std::function<bool( wxString* )> projectResolver =
62 [&]( wxString* token ) ->
bool
72 const std::function<
bool( wxString* )>* aResolver,
int aFlags )
74 static wxRegEx userDefinedWarningError( wxS(
"^(ERC|DRC)_(WARNING|ERROR).*$" ) );
76 size_t sourceLen = aSource.length();
78 newbuf.Alloc( sourceLen );
80 for(
size_t i = 0; i < sourceLen; ++i )
82 if( aSource[i] ==
'$' && i + 1 < sourceLen && aSource[i+1] ==
'{' )
86 for( i = i + 2; i < sourceLen; ++i )
88 if( aSource[i] ==
'}' )
91 token.append( aSource[i] );
97 if( ( aFlags &
FOR_ERC_DRC ) == 0 && userDefinedWarningError.Matches( token ) )
101 else if( aResolver && (*aResolver)( &token ) )
103 newbuf.append( token );
108 newbuf.append(
"${" + token +
"}" );
113 newbuf.append( aSource[i] );
123 std::function<bool( wxString* )> tokenExtractor =
124 [&]( wxString* token ) ->
bool
135 return aSource.StartsWith( wxS(
"${" ) );
143 std::set<wxString>* aSet =
nullptr )
148 if(
auto [
_, result ] = aSet->insert( str ); !result )
152 size_t strlen = str.length();
155 strResult.Alloc( strlen );
157 auto getVersionedEnvVar =
158 [](
const wxString& aMatch, wxString& aResult ) ->
bool
162 if( var.Matches( aMatch ) )
164 const auto value = ENV_VAR::GetEnvVar<wxString>( var );
177 for(
size_t n = 0; n < strlen; n++ )
179 wxUniChar str_n = str[n];
181 switch( str_n.GetValue() )
190 if( str_n == wxT(
'%' ) )
192 bracket = Bracket_Windows;
196 if( n == strlen - 1 )
202 switch( str[n + 1].GetValue() )
224 wxUniChar str_m = str[m];
226 while( wxIsalnum( str_m ) || str_m == wxT(
'_' ) || str_m == wxT(
':' ) )
237 wxString strVarName( str.c_str() + n + 1, m - n - 1 );
241 bool expanded =
false;
242 wxString tmp = strVarName;
249 else if( wxGetEnv( strVarName, &tmp ) )
258 else if( strVarName.Contains(
"KISYS3DMOD")
259 || strVarName.Matches(
"KICAD*_3DMODEL_DIR" ) )
261 if( getVersionedEnvVar(
"KICAD*_3DMODEL_DIR", strResult ) )
264 else if( strVarName.Matches(
"KICAD*_SYMBOL_DIR" ) )
266 if( getVersionedEnvVar(
"KICAD*_SYMBOL_DIR", strResult ) )
269 else if( strVarName.Matches(
"KICAD*_FOOTPRINT_DIR" ) )
271 if( getVersionedEnvVar(
"KICAD*_FOOTPRINT_DIR", strResult ) )
274 else if( strVarName.Matches(
"KICAD*_3RD_PARTY" ) )
276 if( getVersionedEnvVar(
"KICAD*_3RD_PARTY", strResult ) )
283 if ( bracket != Bracket_Windows )
286 strResult << str[n - 1];
288 strResult << str_n << strVarName;
294 if( m == strlen || str_m != (wxChar)bracket )
303 wxLogWarning(
_(
"Environment variables expansion failed: missing '%c' "
304 "at position %u in '%s'." ),
305 (
char)bracket, (
unsigned int) (m + 1), str.c_str() );
312 strResult << (wxChar)bracket;
325 if( n < strlen - 1 && (str[n + 1] == wxT(
'%' ) || str[n + 1] == wxT(
'$' ) ) )
340 std::set<wxString> loop_check;
341 auto first_pos = strResult.find_first_of( wxS(
"{(%" ) );
342 auto last_pos = strResult.find_last_of( wxS(
"})%" ) );
344 if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos )
355 static std::mutex getenv_mutex;
357 std::lock_guard<std::mutex> lock( getenv_mutex );
373 const wxString& aBaseFilename,
377 wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
381 if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
385 msg.Printf(
_(
"Cannot make path '%s' absolute with respect to '%s'." ),
386 aTargetFullFileName->GetPath(),
395 wxString outputPath( aTargetFullFileName->GetPath() );
397 if( !wxFileName::DirExists( outputPath ) )
400 if( wxFileName::Mkdir( outputPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
404 msg.Printf(
_(
"Output directory '%s' created." ), outputPath );
413 msg.Printf(
_(
"Cannot create output directory '%s'." ), outputPath );
427 wxString newFilename( aFilename );
432 if( newFilename.Lower().AfterLast(
'.' ) != aExtension )
434 if( newFilename.Last() !=
'.' )
435 newFilename.Append(
'.' );
437 newFilename.Append( aExtension );
474 if( dot_special && (*n ==
'.') )
571#if wxUSE_DATETIME && defined( __WIN32__ ) && !defined( __WXMICROWIN__ )
577static wxInt64 EPOCH_OFFSET_IN_MSEC = wxLL( 11644473600000 );
580static void ConvertFileTimeToWx( wxDateTime* dt,
const FILETIME& ft )
582 wxLongLong t( ft.dwHighDateTime, ft.dwLowDateTime );
584 t -= EPOCH_OFFSET_IN_MSEC;
586 *dt = wxDateTime( t );
600long long TimestampDir(
const wxString& aDirPath,
const wxString& aFilespec )
602 long long timestamp = 0;
604#if defined( __WIN32__ )
609 std::wstring filespec( aDirPath.t_str() );
611 filespec += aFilespec.t_str();
613 WIN32_FIND_DATA findData;
614 wxDateTime lastModDate;
616 HANDLE fileHandle = ::FindFirstFile( filespec.data(), &findData );
618 if( fileHandle != INVALID_HANDLE_VALUE )
622 ConvertFileTimeToWx( &lastModDate, findData.ftLastWriteTime );
623 timestamp += lastModDate.GetValue().GetValue();
626 timestamp += findData.nFileSizeLow;
628 while ( FindNextFile( fileHandle, &findData ) != 0 );
631 FindClose( fileHandle );
635 std::string filespec( aFilespec.fn_str() );
636 std::string dir_path( aDirPath.fn_str() );
638 DIR* dir = opendir( dir_path.c_str() );
642 for( dirent* dir_entry = readdir( dir ); dir_entry; dir_entry = readdir( dir ) )
644 if( !
matchWild( filespec.c_str(), dir_entry->d_name,
true ) )
647 std::string entry_path = dir_path +
'/' + dir_entry->d_name;
648 struct stat entry_stat;
650 if( wxCRT_Lstat( entry_path.c_str(), &entry_stat ) == 0 )
653 if( S_ISLNK( entry_stat.st_mode ) )
655 char buffer[ PATH_MAX + 1 ];
656 ssize_t pathLen = readlink( entry_path.c_str(), buffer, PATH_MAX );
660 struct stat linked_stat;
661 buffer[ pathLen ] =
'\0';
662 entry_path = dir_path + buffer;
664 if( wxCRT_Lstat( entry_path.c_str(), &linked_stat ) == 0 )
666 entry_stat = linked_stat;
676 if( S_ISREG( entry_stat.st_mode ) )
678 timestamp += entry_stat.st_mtime * 1000;
681 timestamp += entry_stat.st_size;
687 timestamp += (signed) std::hash<std::string>{}( std::string( dir_entry->d_name ) );
704 wxMessageDialog dialog(
nullptr,
_(
"This operating system is not supported "
705 "by KiCad and its dependencies." ),
706 _(
"Unsupported Operating System" ),
707 wxOK | wxICON_EXCLAMATION );
709 dialog.SetExtendedMessage(
_(
"Any issues with KiCad on this system cannot "
710 "be reported to the official bugtracker." ) );
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)=0
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.
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 IsTextVar(const wxString &aSource)
Returns true if the string is a text var, e.g starts with ${.
wxString GetTextVars(const wxString &aSource)
Returns any variables unexpanded, e.g.
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.
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 const ENV_VAR_LIST & GetPredefinedEnvVars()
Get the list of pre-defined environment variables.