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
465
472bool matchWild( const char* pat, const char* text, bool dot_special )
473{
474 if( !*text )
475 {
476 /* Match if both are empty. */
477 return !*pat;
478 }
479
480 const char *m = pat,
481 *n = text,
482 *ma = nullptr,
483 *na = nullptr;
484 int just = 0,
485 acount = 0,
486 count = 0;
487
488 if( dot_special && (*n == '.') )
489 {
490 /* Never match so that hidden Unix files
491 * are never found. */
492 return false;
493 }
494
495 for( ;; )
496 {
497 if( *m == '*' )
498 {
499 ma = ++m;
500 na = n;
501 just = 1;
502 acount = count;
503 }
504 else if( *m == '?' )
505 {
506 m++;
507
508 if( !*n++ )
509 return false;
510 }
511 else
512 {
513 if( *m == '\\' )
514 {
515 m++;
516
517 /* Quoting "nothing" is a bad thing */
518 if( !*m )
519 return false;
520 }
521
522 if( !*m )
523 {
524 /*
525 * If we are out of both strings or we just
526 * saw a wildcard, then we can say we have a
527 * match
528 */
529 if( !*n )
530 return true;
531
532 if( just )
533 return true;
534
535 just = 0;
536 goto not_matched;
537 }
538
539 /*
540 * We could check for *n == NULL at this point, but
541 * since it's more common to have a character there,
542 * check to see if they match first (m and n) and
543 * then if they don't match, THEN we can check for
544 * the NULL of n
545 */
546 just = 0;
547
548 if( *m == *n )
549 {
550 m++;
551 count++;
552 n++;
553 }
554 else
555 {
556 not_matched:
557
558 /*
559 * If there are no more characters in the
560 * string, but we still need to find another
561 * character (*m != NULL), then it will be
562 * impossible to match it
563 */
564 if( !*n )
565 return false;
566
567 if( ma )
568 {
569 m = ma;
570 n = ++na;
571 count = acount;
572 }
573 else
574 return false;
575 }
576 }
577 }
578}
579
580
585#if wxUSE_DATETIME && defined( __WIN32__ ) && !defined( __WXMICROWIN__ )
586
587// Convert between wxDateTime and FILETIME which is a 64-bit value representing
588// the number of 100-nanosecond intervals since January 1, 1601 UTC.
589//
590// This is the offset between FILETIME epoch and the Unix/wxDateTime Epoch.
591static wxInt64 EPOCH_OFFSET_IN_MSEC = wxLL( 11644473600000 );
592
593
594static void ConvertFileTimeToWx( wxDateTime* dt, const FILETIME& ft )
595{
596 wxLongLong t( ft.dwHighDateTime, ft.dwLowDateTime );
597 t /= 10000; // Convert hundreds of nanoseconds to milliseconds.
598 t -= EPOCH_OFFSET_IN_MSEC;
599
600 *dt = wxDateTime( t );
601}
602
603#endif // wxUSE_DATETIME && __WIN32__
604
605
614long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec )
615{
616 long long timestamp = 0;
617
618#if defined( __WIN32__ )
619 // Win32 version.
620 // Save time by not searching for each path twice: once in wxDir.GetNext() and once in
621 // wxFileName.GetModificationTime(). Also cuts out wxWidgets' string-matching and case
622 // conversion by staying on the MSW side of things.
623 std::wstring filespec( aDirPath.t_str() );
624 filespec += '\\';
625 filespec += aFilespec.t_str();
626
627 WIN32_FIND_DATA findData;
628 wxDateTime lastModDate;
629
630 HANDLE fileHandle = ::FindFirstFile( filespec.data(), &findData );
631
632 if( fileHandle != INVALID_HANDLE_VALUE )
633 {
634 do
635 {
636 ConvertFileTimeToWx( &lastModDate, findData.ftLastWriteTime );
637 timestamp += lastModDate.GetValue().GetValue();
638
639 // Get the file size (partial) as well to check for sneaky changes.
640 timestamp += findData.nFileSizeLow;
641 }
642 while ( FindNextFile( fileHandle, &findData ) != 0 );
643 }
644
645 FindClose( fileHandle );
646#else
647 // POSIX version.
648 // Save time by not converting between encodings -- do everything on the file-system side.
649 std::string filespec( aFilespec.fn_str() );
650 std::string dir_path( aDirPath.fn_str() );
651
652 DIR* dir = opendir( dir_path.c_str() );
653
654 if( dir )
655 {
656 for( dirent* dir_entry = readdir( dir ); dir_entry; dir_entry = readdir( dir ) )
657 {
658 if( !matchWild( filespec.c_str(), dir_entry->d_name, true ) )
659 continue;
660
661 std::string entry_path = dir_path + '/' + dir_entry->d_name;
662 struct stat entry_stat;
663
664 if( wxCRT_Lstat( entry_path.c_str(), &entry_stat ) == 0 )
665 {
666 // Timestamp the source file, not the symlink
667 if( S_ISLNK( entry_stat.st_mode ) ) // wxFILE_EXISTS_SYMLINK
668 {
669 char buffer[ PATH_MAX + 1 ];
670 ssize_t pathLen = readlink( entry_path.c_str(), buffer, PATH_MAX );
671
672 if( pathLen > 0 )
673 {
674 struct stat linked_stat;
675 buffer[ pathLen ] = '\0';
676 entry_path = dir_path + buffer;
677
678 if( wxCRT_Lstat( entry_path.c_str(), &linked_stat ) == 0 )
679 {
680 entry_stat = linked_stat;
681 }
682 else
683 {
684 // if we couldn't lstat the linked file we'll have to just use
685 // the symbolic link info
686 }
687 }
688 }
689
690 if( S_ISREG( entry_stat.st_mode ) ) // wxFileExists()
691 {
692 timestamp += entry_stat.st_mtime * 1000;
693
694 // Get the file size as well to check for sneaky changes.
695 timestamp += entry_stat.st_size;
696 }
697 }
698 else
699 {
700 // if we couldn't lstat the file itself all we can do is use the name
701 timestamp += (signed) std::hash<std::string>{}( std::string( dir_entry->d_name ) );
702 }
703 }
704
705 closedir( dir );
706 }
707#endif
708
709 return timestamp;
710}
711
712
714{
716 return false;
717
718 wxMessageDialog dialog( nullptr, _( "This operating system is not supported "
719 "by KiCad and its dependencies." ),
720 _( "Unsupported Operating System" ),
721 wxOK | wxICON_EXCLAMATION );
722
723 dialog.SetExtendedMessage( _( "Any issues with KiCad on this system cannot "
724 "be reported to the official bugtracker." ) );
725 dialog.ShowModal();
726
727 return true;
728}
Container for project specific data.
Definition project.h:66
virtual bool TextVarResolver(wxString *aToken) const
Definition project.cpp:82
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 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:713
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:472
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:614
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 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.