37#include <wx/stdpaths.h>
52 Bracket_Windows =
'%',
60 std::function<bool( wxString* )> projectResolver =
61 [&]( wxString* token ) ->
bool
71 const std::function<
bool( wxString* )>* aResolver )
74 size_t sourceLen = aSource.length();
76 newbuf.Alloc( sourceLen );
78 for(
size_t i = 0; i < sourceLen; ++i )
80 if( aSource[i] ==
'$' && i + 1 < sourceLen && aSource[i+1] ==
'{' )
84 for( i = i + 2; i < sourceLen; ++i )
86 if( aSource[i] ==
'}' )
89 token.append( aSource[i] );
95 if( aResolver && (*aResolver)( &token ) )
97 newbuf.append( token );
102 newbuf.append(
"${" + token +
"}" );
107 newbuf.append( aSource[i] );
123 if(
auto [
_, result ] = aSet->insert( str ); !result )
127 size_t strlen = str.length();
130 strResult.Alloc( strlen );
132 for(
size_t n = 0; n < strlen; n++ )
134 wxUniChar str_n = str[n];
136 switch( str_n.GetValue() )
145 if( str_n == wxT(
'%' ) )
146 bracket = Bracket_Windows;
149 if( n == strlen - 1 )
155 switch( str[n + 1].GetValue() )
177 wxUniChar str_m = str[m];
179 while( wxIsalnum( str_m ) || str_m == wxT(
'_' ) || str_m == wxT(
':' ) )
190 wxString strVarName( str.c_str() + n + 1, m - n - 1 );
194 bool expanded =
false;
195 wxString tmp = strVarName;
202 else if( wxGetEnv( strVarName, &tmp ) )
211 else if( strVarName.Contains(
"KISYS3DMOD") || strVarName.Matches(
"KICAD*_3DMODEL_DIR" ) )
215 if( var.Matches(
"KICAD*_3DMODEL_DIR" ) )
217 const auto value = ENV_VAR::GetEnvVar<wxString>( var );
232 if ( bracket != Bracket_Windows )
235 strResult << str[n - 1];
237 strResult << str_n << strVarName;
243 if( m == strlen || str_m != (wxChar)bracket )
252 wxLogWarning(
_(
"Environment variables expansion failed: missing '%c' "
253 "at position %u in '%s'." ),
254 (
char)bracket, (
unsigned int) (m + 1), str.c_str() );
261 strResult << (wxChar)bracket;
274 if( n < strlen - 1 && (str[n + 1] == wxT(
'%' ) || str[n + 1] == wxT(
'$' )) )
288 std::set<wxString> loop_check;
289 auto first_pos = strResult.find_first_of( wxS(
"{(%" ) );
290 auto last_pos = strResult.find_last_of( wxS(
"})%" ) );
292 if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos )
303 static std::mutex getenv_mutex;
305 std::lock_guard<std::mutex> lock( getenv_mutex );
319 if( url.GetError() == wxURL_NOERR )
328 const wxString& aBaseFilename,
332 wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
336 if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
340 msg.Printf(
_(
"Cannot make path '%s' absolute with respect to '%s'." ),
341 aTargetFullFileName->GetPath(),
350 wxString outputPath( aTargetFullFileName->GetPath() );
352 if( !wxFileName::DirExists( outputPath ) )
355 if( wxFileName::Mkdir( outputPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
359 msg.Printf(
_(
"Output directory '%s' created." ), outputPath );
368 msg.Printf(
_(
"Cannot create output directory '%s'." ), outputPath );
394bool matchWild(
const char* pat,
const char* text,
bool dot_special )
410 if( dot_special && (*n ==
'.') )
507#if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
513static wxInt64 EPOCH_OFFSET_IN_MSEC = wxLL(11644473600000);
516static void ConvertFileTimeToWx( wxDateTime* dt,
const FILETIME& ft )
518 wxLongLong t( ft.dwHighDateTime, ft.dwLowDateTime );
520 t -= EPOCH_OFFSET_IN_MSEC;
522 *dt = wxDateTime( t );
536long long TimestampDir(
const wxString& aDirPath,
const wxString& aFilespec )
538 long long timestamp = 0;
540#if defined( __WIN32__ )
545 std::wstring filespec( aDirPath.t_str() );
547 filespec += aFilespec.t_str();
549 WIN32_FIND_DATA findData;
550 wxDateTime lastModDate;
552 HANDLE fileHandle = ::FindFirstFile( filespec.data(), &findData );
554 if( fileHandle != INVALID_HANDLE_VALUE )
558 ConvertFileTimeToWx( &lastModDate, findData.ftLastWriteTime );
559 timestamp += lastModDate.GetValue().GetValue();
562 timestamp += findData.nFileSizeLow;
564 while ( FindNextFile( fileHandle, &findData ) != 0 );
567 FindClose( fileHandle );
571 std::string filespec( aFilespec.fn_str() );
572 std::string dir_path( aDirPath.fn_str() );
574 DIR* dir = opendir( dir_path.c_str() );
578 for( dirent* dir_entry = readdir( dir ); dir_entry; dir_entry = readdir( dir ) )
580 if( !
matchWild( filespec.c_str(), dir_entry->d_name,
true ) )
583 std::string entry_path = dir_path +
'/' + dir_entry->d_name;
584 struct stat entry_stat;
586 if( wxCRT_Lstat( entry_path.c_str(), &entry_stat ) == 0 )
589 if( S_ISLNK( entry_stat.st_mode ) )
591 char buffer[ PATH_MAX + 1 ];
592 ssize_t pathLen = readlink( entry_path.c_str(), buffer, PATH_MAX );
596 struct stat linked_stat;
597 buffer[ pathLen ] =
'\0';
598 entry_path = dir_path + buffer;
600 if( wxCRT_Lstat( entry_path.c_str(), &linked_stat ) == 0 )
602 entry_stat = linked_stat;
612 if( S_ISREG( entry_stat.st_mode ) )
614 timestamp += entry_stat.st_mtime * 1000;
617 timestamp += entry_stat.st_size;
623 timestamp += (signed) std::hash<std::string>{}( std::string( dir_entry->d_name ) );
640 wxMessageDialog dialog(
nullptr,
_(
"This operating system is not supported "
641 "by KiCad and its dependencies." ),
642 _(
"Unsupported Operating System" ),
643 wxOK | wxICON_EXCLAMATION );
645 dialog.SetExtendedMessage(
_(
"Any issues with KiCad on this system cannot "
646 "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.
bool WarnUserIfOperatingSystemUnsupported()
Checks if the operating system is explicitly unsupported and displays a disclaimer message box.
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)
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject)
const wxString ResolveUriByEnvVars(const wxString &aUri, PROJECT *aProject)
Replace any environment and/or text variables in file-path uris (leaving network-path URIs alone).
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/...
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 ...
const ENV_VAR_LIST & GetPredefinedEnvVars()
Get the list of pre-defined environment variables.