KiCad PCB EDA Suite
Loading...
Searching...
No Matches
common.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2014-2020 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2008 Wayne Stambaugh <[email protected]>
6 * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <eda_base_frame.h>
27#include <kiplatform/app.h>
28#include <project.h>
29#include <common.h>
30#include <env_vars.h>
31#include <reporter.h>
32#include <macros.h>
33#include <mutex>
34#include <wx/config.h>
35#include <wx/log.h>
36#include <wx/msgdlg.h>
37#include <wx/stdpaths.h>
38#include <wx/url.h>
39#include <wx/utils.h>
40
41#ifdef _WIN32
42#include <Windows.h>
43#endif
44
45
47{
51#ifdef __WINDOWS__
52 Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
53#endif
55};
56
57
58wxString ExpandTextVars( const wxString& aSource, const PROJECT* aProject )
59{
60 std::function<bool( wxString* )> projectResolver =
61 [&]( wxString* token ) -> bool
62 {
63 return aProject->TextVarResolver( token );
64 };
65
66 return ExpandTextVars( aSource, &projectResolver );
67}
68
69
70wxString ExpandTextVars( const wxString& aSource,
71 const std::function<bool( wxString* )>* aResolver )
72{
73 wxString newbuf;
74 size_t sourceLen = aSource.length();
75
76 newbuf.Alloc( sourceLen ); // best guess (improves performance)
77
78 for( size_t i = 0; i < sourceLen; ++i )
79 {
80 if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
81 {
82 wxString token;
83
84 for( i = i + 2; i < sourceLen; ++i )
85 {
86 if( aSource[i] == '}' )
87 break;
88 else
89 token.append( aSource[i] );
90 }
91
92 if( token.IsEmpty() )
93 continue;
94
95 if( aResolver && (*aResolver)( &token ) )
96 {
97 newbuf.append( token );
98 }
99 else
100 {
101 // Token not resolved: leave the reference unchanged
102 newbuf.append( "${" + token + "}" );
103 }
104 }
105 else
106 {
107 newbuf.append( aSource[i] );
108 }
109 }
110
111 return newbuf;
112}
113
114
115wxString GetTextVars( const wxString& aSource )
116{
117 std::function<bool( wxString* )> tokenExtractor =
118 [&]( wxString* token ) -> bool
119 {
120 return true;
121 };
122
123 return ExpandTextVars( aSource, &tokenExtractor );
124}
125
126
127bool IsTextVar( const wxString& aSource )
128{
129 return aSource.StartsWith( wxS( "${" ) );
130}
131
132
133//
134// Stolen from wxExpandEnvVars and then heavily optimized
135//
136wxString KIwxExpandEnvVars( const wxString& str, const PROJECT* aProject, std::set<wxString>* aSet = nullptr )
137{
138 // If the same string is inserted twice, we have a loop
139 if( aSet )
140 {
141 if( auto [ _, result ] = aSet->insert( str ); !result )
142 return str;
143 }
144
145 size_t strlen = str.length();
146
147 wxString strResult;
148 strResult.Alloc( strlen ); // best guess (improves performance)
149
150 auto getVersionedEnvVar = []( const wxString& aMatch, wxString& aResult ) -> bool
151 {
152 for ( const wxString& var : ENV_VAR::GetPredefinedEnvVars() )
153 {
154 if( var.Matches( aMatch ) )
155 {
156 const auto value = ENV_VAR::GetEnvVar<wxString>( var );
157
158 if( !value )
159 continue;
160
161 aResult += *value;
162 return true;
163 }
164 }
165
166 return false;
167 };
168
169 for( size_t n = 0; n < strlen; n++ )
170 {
171 wxUniChar str_n = str[n];
172
173 switch( str_n.GetValue() )
174 {
175#ifdef __WINDOWS__
176 case wxT( '%' ):
177#endif // __WINDOWS__
178 case wxT( '$' ):
179 {
180 Bracket bracket;
181#ifdef __WINDOWS__
182 if( str_n == wxT( '%' ) )
183 bracket = Bracket_Windows;
184 else
185#endif // __WINDOWS__
186 if( n == strlen - 1 )
187 {
188 bracket = Bracket_None;
189 }
190 else
191 {
192 switch( str[n + 1].GetValue() )
193 {
194 case wxT( '(' ):
195 bracket = Bracket_Normal;
196 str_n = str[++n]; // skip the bracket
197 break;
198
199 case wxT( '{' ):
200 bracket = Bracket_Curly;
201 str_n = str[++n]; // skip the bracket
202 break;
203
204 default:
205 bracket = Bracket_None;
206 }
207 }
208
209 size_t m = n + 1;
210
211 if( m >= strlen )
212 break;
213
214 wxUniChar str_m = str[m];
215
216 while( wxIsalnum( str_m ) || str_m == wxT( '_' ) || str_m == wxT( ':' ) )
217 {
218 if( ++m == strlen )
219 {
220 str_m = 0;
221 break;
222 }
223
224 str_m = str[m];
225 }
226
227 wxString strVarName( str.c_str() + n + 1, m - n - 1 );
228
229 // NB: use wxGetEnv instead of wxGetenv as otherwise variables
230 // set through wxSetEnv may not be read correctly!
231 bool expanded = false;
232 wxString tmp = strVarName;
233
234 if( aProject && aProject->TextVarResolver( &tmp ) )
235 {
236 strResult += tmp;
237 expanded = true;
238 }
239 else if( wxGetEnv( strVarName, &tmp ) )
240 {
241 strResult += tmp;
242 expanded = true;
243 }
244 // Replace unmatched older variables with current locations
245 // If the user has the older location defined, that will be matched
246 // first above. But if they do not, this will ensure that their board still
247 // displays correctly
248 else if( strVarName.Contains( "KISYS3DMOD") || strVarName.Matches( "KICAD*_3DMODEL_DIR" ) )
249 {
250 if( getVersionedEnvVar( "KICAD*_3DMODEL_DIR", strResult ) )
251 expanded = true;
252 }
253 else if( strVarName.Matches( "KICAD*_SYMBOL_DIR" ) )
254 {
255 if( getVersionedEnvVar( "KICAD*_SYMBOL_DIR", strResult ) )
256 expanded = true;
257 }
258 else if( strVarName.Matches( "KICAD*_FOOTPRINT_DIR" ) )
259 {
260 if( getVersionedEnvVar( "KICAD*_FOOTPRINT_DIR", strResult ) )
261 expanded = true;
262 }
263 else
264 {
265 // variable doesn't exist => don't change anything
266#ifdef __WINDOWS__
267 if ( bracket != Bracket_Windows )
268#endif
269 if ( bracket != Bracket_None )
270 strResult << str[n - 1];
271
272 strResult << str_n << strVarName;
273 }
274
275 // check the closing bracket
276 if( bracket != Bracket_None )
277 {
278 if( m == strlen || str_m != (wxChar)bracket )
279 {
280 // under MSW it's common to have '%' characters in the registry
281 // and it's annoying to have warnings about them each time, so
282 // ignore them silently if they are not used for env vars
283 //
284 // under Unix, OTOH, this warning could be useful for the user to
285 // understand why isn't the variable expanded as intended
286#ifndef __WINDOWS__
287 wxLogWarning( _( "Environment variables expansion failed: missing '%c' "
288 "at position %u in '%s'." ),
289 (char)bracket, (unsigned int) (m + 1), str.c_str() );
290#endif // __WINDOWS__
291 }
292 else
293 {
294 // skip closing bracket unless the variables wasn't expanded
295 if( !expanded )
296 strResult << (wxChar)bracket;
297
298 m++;
299 }
300 }
301
302 n = m - 1; // skip variable name
303 str_n = str[n];
304 }
305 break;
306
307 case wxT( '\\' ):
308 // backslash can be used to suppress special meaning of % and $
309 if( n < strlen - 1 && (str[n + 1] == wxT( '%' ) || str[n + 1] == wxT( '$' )) )
310 {
311 str_n = str[++n];
312 strResult += str_n;
313
314 break;
315 }
317
318 default:
319 strResult += str_n;
320 }
321 }
322
323 std::set<wxString> loop_check;
324 auto first_pos = strResult.find_first_of( wxS( "{(%" ) );
325 auto last_pos = strResult.find_last_of( wxS( "})%" ) );
326
327 if( first_pos != strResult.npos && last_pos != strResult.npos && first_pos != last_pos )
328 strResult = KIwxExpandEnvVars( strResult, aProject, aSet ? aSet : &loop_check );
329
330 return strResult;
331}
332
333
334const wxString ExpandEnvVarSubstitutions( const wxString& aString, const PROJECT* aProject )
335{
336 // wxGetenv( wchar_t* ) is not re-entrant on linux.
337 // Put a lock on multithreaded use of wxGetenv( wchar_t* ), called from wxEpandEnvVars(),
338 static std::mutex getenv_mutex;
339
340 std::lock_guard<std::mutex> lock( getenv_mutex );
341
342 // We reserve the right to do this another way, by providing our own member function.
343 return KIwxExpandEnvVars( aString, aProject );
344}
345
346
347const wxString ResolveUriByEnvVars( const wxString& aUri, PROJECT* aProject )
348{
349 wxString uri = ExpandTextVars( aUri, aProject );
350
351 // URL-like URI: return as is.
352 wxURL url( uri );
353
354 if( url.GetError() == wxURL_NOERR )
355 return uri;
356
357 // Otherwise, the path points to a local file. Resolve environment variables if any.
358 return ExpandEnvVarSubstitutions( aUri, aProject );
359}
360
361
362bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName,
363 const wxString& aBaseFilename,
364 REPORTER* aReporter )
365{
366 wxString msg;
367 wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
368
369 // make aTargetFullFileName path, which is relative to aBaseFilename path (if it is not
370 // already an absolute path) absolute:
371 if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
372 {
373 if( aReporter )
374 {
375 msg.Printf( _( "Cannot make path '%s' absolute with respect to '%s'." ),
376 aTargetFullFileName->GetPath(),
377 baseFilePath );
378 aReporter->Report( msg, RPT_SEVERITY_ERROR );
379 }
380
381 return false;
382 }
383
384 // Ensure the path of aTargetFullFileName exists, and create it if needed:
385 wxString outputPath( aTargetFullFileName->GetPath() );
386
387 if( !wxFileName::DirExists( outputPath ) )
388 {
389 // Make every directory provided when the provided path doesn't exist
390 if( wxFileName::Mkdir( outputPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
391 {
392 if( aReporter )
393 {
394 msg.Printf( _( "Output directory '%s' created." ), outputPath );
395 aReporter->Report( msg, RPT_SEVERITY_INFO );
396 return true;
397 }
398 }
399 else
400 {
401 if( aReporter )
402 {
403 msg.Printf( _( "Cannot create output directory '%s'." ), outputPath );
404 aReporter->Report( msg, RPT_SEVERITY_ERROR );
405 }
406
407 return false;
408 }
409 }
410
411 return true;
412}
413
414
415wxString EnsureFileExtension( const wxString& aFilename, const wxString& aExtension )
416{
417 wxString newFilename( aFilename );
418
419 // It's annoying to throw up nag dialogs when the extension isn't right. Just fix it,
420 // but be careful not to destroy existing after-dot-text that isn't actually a bad
421 // extension, such as "Schematic_1.1".
422 if( newFilename.Lower().AfterLast( '.' ) != aExtension )
423 {
424 if( newFilename.Last() != '.' )
425 newFilename.Append( '.' );
426
427 newFilename.Append( aExtension );
428 }
429
430 return newFilename;
431}
432
433
448bool matchWild( const char* pat, const char* text, bool dot_special )
449{
450 if( !*text )
451 {
452 /* Match if both are empty. */
453 return !*pat;
454 }
455
456 const char *m = pat,
457 *n = text,
458 *ma = nullptr,
459 *na = nullptr;
460 int just = 0,
461 acount = 0,
462 count = 0;
463
464 if( dot_special && (*n == '.') )
465 {
466 /* Never match so that hidden Unix files
467 * are never found. */
468 return false;
469 }
470
471 for(;;)
472 {
473 if( *m == '*' )
474 {
475 ma = ++m;
476 na = n;
477 just = 1;
478 acount = count;
479 }
480 else if( *m == '?' )
481 {
482 m++;
483
484 if( !*n++ )
485 return false;
486 }
487 else
488 {
489 if( *m == '\\' )
490 {
491 m++;
492
493 /* Quoting "nothing" is a bad thing */
494 if( !*m )
495 return false;
496 }
497
498 if( !*m )
499 {
500 /*
501 * If we are out of both strings or we just
502 * saw a wildcard, then we can say we have a
503 * match
504 */
505 if( !*n )
506 return true;
507
508 if( just )
509 return true;
510
511 just = 0;
512 goto not_matched;
513 }
514
515 /*
516 * We could check for *n == NULL at this point, but
517 * since it's more common to have a character there,
518 * check to see if they match first (m and n) and
519 * then if they don't match, THEN we can check for
520 * the NULL of n
521 */
522 just = 0;
523
524 if( *m == *n )
525 {
526 m++;
527 count++;
528 n++;
529 }
530 else
531 {
532 not_matched:
533
534 /*
535 * If there are no more characters in the
536 * string, but we still need to find another
537 * character (*m != NULL), then it will be
538 * impossible to match it
539 */
540 if( !*n )
541 return false;
542
543 if( ma )
544 {
545 m = ma;
546 n = ++na;
547 count = acount;
548 }
549 else
550 return false;
551 }
552 }
553 }
554}
555
556
561#if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
562
563// Convert between wxDateTime and FILETIME which is a 64-bit value representing
564// the number of 100-nanosecond intervals since January 1, 1601 UTC.
565//
566// This is the offset between FILETIME epoch and the Unix/wxDateTime Epoch.
567static wxInt64 EPOCH_OFFSET_IN_MSEC = wxLL(11644473600000);
568
569
570static void ConvertFileTimeToWx( wxDateTime* dt, const FILETIME& ft )
571{
572 wxLongLong t( ft.dwHighDateTime, ft.dwLowDateTime );
573 t /= 10000; // Convert hundreds of nanoseconds to milliseconds.
574 t -= EPOCH_OFFSET_IN_MSEC;
575
576 *dt = wxDateTime( t );
577}
578
579#endif // wxUSE_DATETIME && __WIN32__
580
581
590long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec )
591{
592 long long timestamp = 0;
593
594#if defined( __WIN32__ )
595 // Win32 version.
596 // Save time by not searching for each path twice: once in wxDir.GetNext() and once in
597 // wxFileName.GetModificationTime(). Also cuts out wxWidgets' string-matching and case
598 // conversion by staying on the MSW side of things.
599 std::wstring filespec( aDirPath.t_str() );
600 filespec += '\\';
601 filespec += aFilespec.t_str();
602
603 WIN32_FIND_DATA findData;
604 wxDateTime lastModDate;
605
606 HANDLE fileHandle = ::FindFirstFile( filespec.data(), &findData );
607
608 if( fileHandle != INVALID_HANDLE_VALUE )
609 {
610 do
611 {
612 ConvertFileTimeToWx( &lastModDate, findData.ftLastWriteTime );
613 timestamp += lastModDate.GetValue().GetValue();
614
615 // Get the file size (partial) as well to check for sneaky changes.
616 timestamp += findData.nFileSizeLow;
617 }
618 while ( FindNextFile( fileHandle, &findData ) != 0 );
619 }
620
621 FindClose( fileHandle );
622#else
623 // POSIX version.
624 // Save time by not converting between encodings -- do everything on the file-system side.
625 std::string filespec( aFilespec.fn_str() );
626 std::string dir_path( aDirPath.fn_str() );
627
628 DIR* dir = opendir( dir_path.c_str() );
629
630 if( dir )
631 {
632 for( dirent* dir_entry = readdir( dir ); dir_entry; dir_entry = readdir( dir ) )
633 {
634 if( !matchWild( filespec.c_str(), dir_entry->d_name, true ) )
635 continue;
636
637 std::string entry_path = dir_path + '/' + dir_entry->d_name;
638 struct stat entry_stat;
639
640 if( wxCRT_Lstat( entry_path.c_str(), &entry_stat ) == 0 )
641 {
642 // Timestamp the source file, not the symlink
643 if( S_ISLNK( entry_stat.st_mode ) ) // wxFILE_EXISTS_SYMLINK
644 {
645 char buffer[ PATH_MAX + 1 ];
646 ssize_t pathLen = readlink( entry_path.c_str(), buffer, PATH_MAX );
647
648 if( pathLen > 0 )
649 {
650 struct stat linked_stat;
651 buffer[ pathLen ] = '\0';
652 entry_path = dir_path + buffer;
653
654 if( wxCRT_Lstat( entry_path.c_str(), &linked_stat ) == 0 )
655 {
656 entry_stat = linked_stat;
657 }
658 else
659 {
660 // if we couldn't lstat the linked file we'll have to just use
661 // the symbolic link info
662 }
663 }
664 }
665
666 if( S_ISREG( entry_stat.st_mode ) ) // wxFileExists()
667 {
668 timestamp += entry_stat.st_mtime * 1000;
669
670 // Get the file size as well to check for sneaky changes.
671 timestamp += entry_stat.st_size;
672 }
673 }
674 else
675 {
676 // if we couldn't lstat the file itself all we can do is use the name
677 timestamp += (signed) std::hash<std::string>{}( std::string( dir_entry->d_name ) );
678 }
679 }
680
681 closedir( dir );
682 }
683#endif
684
685 return timestamp;
686}
687
688
690{
692 return false;
693
694 wxMessageDialog dialog( nullptr, _( "This operating system is not supported "
695 "by KiCad and its dependencies." ),
696 _( "Unsupported Operating System" ),
697 wxOK | wxICON_EXCLAMATION );
698
699 dialog.SetExtendedMessage( _( "Any issues with KiCad on this system cannot "
700 "be reported to the official bugtracker." ) );
701 dialog.ShowModal();
702
703 return true;
704}
Container for project specific data.
Definition: project.h:62
virtual bool TextVarResolver(wxString *aToken) const
Definition: project.cpp:72
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
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.
Definition: common.cpp:334
wxString EnsureFileExtension(const wxString &aFilename, const wxString &aExtension)
It's annoying to throw up nag dialogs when the extension isn't right.
Definition: common.cpp:415
bool WarnUserIfOperatingSystemUnsupported()
Checks if the operating system is explicitly unsupported and displays a disclaimer message box.
Definition: common.cpp:689
bool matchWild(const char *pat, const char *text, bool dot_special)
Performance enhancements to file and directory operations.
Definition: common.cpp:448
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.
Definition: common.cpp:362
wxString KIwxExpandEnvVars(const wxString &str, const PROJECT *aProject, std::set< wxString > *aSet=nullptr)
Definition: common.cpp:136
Bracket
Definition: common.cpp:47
@ Bracket_Max
Definition: common.cpp:54
@ Bracket_None
Definition: common.cpp:48
@ Bracket_Normal
Definition: common.cpp:49
@ Bracket_Curly
Definition: common.cpp:50
bool IsTextVar(const wxString &aSource)
Returns true if the string is a text var, e.g starts with ${.
Definition: common.cpp:127
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject)
Definition: common.cpp:58
const wxString ResolveUriByEnvVars(const wxString &aUri, PROJECT *aProject)
Replace any environment and/or text variables in file-path uris (leaving network-path URIs alone).
Definition: common.cpp:347
wxString GetTextVars(const wxString &aSource)
Returns any variables unexpanded, e.g.
Definition: common.cpp:115
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/...
Definition: common.cpp:590
The common library.
#define _(s)
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 ...
Definition: macros.h:83
KICOMMON_API const ENV_VAR_LIST & GetPredefinedEnvVars()
Get the list of pre-defined environment variables.
Definition: env_vars.cpp:68
bool IsOperatingSystemUnsupported()
Checks if the Operating System is explicitly unsupported and we want to prevent users from sending bu...
Definition: gtk/app.cpp:58
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_INFO