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