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