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