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