KiCad PCB EDA Suite
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-2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 1992-2019 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 
30 #include <fctsys.h>
31 #include <eda_base_frame.h>
32 #include <base_struct.h>
33 #include <common.h>
34 #include <macros.h>
35 #include <base_units.h>
36 #include <reporter.h>
37 
38 #include <wx/process.h>
39 #include <wx/config.h>
40 #include <wx/utils.h>
41 #include <wx/stdpaths.h>
42 #include <wx/url.h>
43 
44 #include <pgm_base.h>
45 
46 using KIGFX::COLOR4D;
47 
48 
58 
59 
60 #if defined( _WIN32 ) && defined( DEBUG )
61 // a wxAssertHandler_t function to filter wxWidgets alert messages when reading/writing a file
62 // when switching the locale to LC_NUMERIC, "C"
63 // It is used in class LOCALE_IO to hide a useless (in kicad) wxWidgets alert message
64 void KiAssertFilter( const wxString &file, int line,
65  const wxString &func, const wxString &cond,
66  const wxString &msg)
67 {
68  if( !msg.Contains( "Decimal separator mismatch" ) )
69  wxTheApp->OnAssertFailure( file, line, func, cond, msg );
70 }
71 #endif
72 
73 
74 std::atomic<unsigned int> LOCALE_IO::m_c_count( 0 );
75 
76 
77 // Note on Windows, setlocale( LC_NUMERIC, "C" ) works fine to read/write
78 // files with floating point numbers, but generates a overzealous wx alert
79 // in some cases (reading a bitmap for instance)
80 // So we disable alerts during the time a file is read or written
81 
83 {
84  // use thread safe, atomic operation
85  if( m_c_count++ == 0 )
86  {
87  // Store the user locale name, to restore this locale later, in dtor
88  m_user_locale = setlocale( LC_NUMERIC, nullptr );
89 #if defined( _WIN32 ) && defined( DEBUG )
90  // Disable wxWidgets alerts
91  wxSetAssertHandler( KiAssertFilter );
92 #endif
93  // Switch the locale to C locale, to read/write files with fp numbers
94  setlocale( LC_NUMERIC, "C" );
95  }
96 }
97 
98 
100 {
101  // use thread safe, atomic operation
102  if( --m_c_count == 0 )
103  {
104  // revert to the user locale
105  setlocale( LC_NUMERIC, m_user_locale.c_str() );
106 #if defined( _WIN32 ) && defined( DEBUG )
107  // Enable wxWidgets alerts
108  wxSetDefaultAssertHandler();
109 #endif
110  }
111 }
112 
113 
114 wxSize GetTextSize( const wxString& aSingleLine, wxWindow* aWindow )
115 {
116  wxCoord width;
117  wxCoord height;
118 
119  {
120  wxClientDC dc( aWindow );
121  dc.SetFont( aWindow->GetFont() );
122  dc.GetTextExtent( aSingleLine, &width, &height );
123  }
124 
125  return wxSize( width, height );
126 }
127 
128 
129 bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString )
130 {
131  wxWindow* window = aCtrl->GetParent();
132 
133  if( !window )
134  window = aCtrl;
135 
136  wxString ctrlText;
137 
138  if( !aString )
139  {
140  ctrlText = aCtrl->GetValue();
141  aString = &ctrlText;
142  }
143 
144  wxSize textz = GetTextSize( *aString, window );
145  wxSize ctrlz = aCtrl->GetSize();
146 
147  if( ctrlz.GetWidth() < textz.GetWidth() + 10 )
148  {
149  ctrlz.SetWidth( textz.GetWidth() + 10 );
150  aCtrl->SetSizeHints( ctrlz );
151  return true;
152  }
153 
154  return false;
155 }
156 
157 
158 void SelectReferenceNumber( wxTextEntry* aTextEntry )
159 {
160  wxString ref = aTextEntry->GetValue();
161 
162  if( ref.find_first_of( '?' ) != ref.npos )
163  {
164  aTextEntry->SetSelection( ref.find_first_of( '?' ), ref.find_last_of( '?' ) + 1 );
165  }
166  else
167  {
168  wxString num = ref;
169 
170  while( !num.IsEmpty() && ( !isdigit( num.Last() ) || !isdigit( num.GetChar( 0 ) ) ) )
171  {
172  if( !isdigit( num.Last() ) )
173  num.RemoveLast();
174 
175  if( !isdigit( num.GetChar ( 0 ) ) )
176  num = num.Right( num.Length() - 1);
177  }
178 
179  aTextEntry->SetSelection( ref.Find( num ), ref.Find( num ) + num.Length() );
180 
181  if( num.IsEmpty() )
182  aTextEntry->SetSelection( -1, -1 );
183  }
184 }
185 
186 
187 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter )
188 {
189  wxString tmp;
190 
191  for( unsigned ii = 0; ii < aText.Length(); ii++ )
192  {
193  if( aText[ii] == aSplitter )
194  {
195  aStrings.Add( tmp );
196  tmp.Clear();
197  }
198 
199  else
200  tmp << aText[ii];
201  }
202 
203  if( !tmp.IsEmpty() )
204  {
205  aStrings.Add( tmp );
206  }
207 }
208 
209 
210 int ProcessExecute( const wxString& aCommandLine, int aFlags, wxProcess *callback )
211 {
212  return wxExecute( aCommandLine, aFlags, callback );
213 }
214 
215 
217 {
218  static timestamp_t oldTimeStamp;
219  timestamp_t newTimeStamp;
220 
221  newTimeStamp = time( NULL );
222 
223  if( newTimeStamp <= oldTimeStamp )
224  newTimeStamp = oldTimeStamp + 1;
225 
226  oldTimeStamp = newTimeStamp;
227 
228  return newTimeStamp;
229 }
230 
231 
232 double RoundTo0( double x, double precision )
233 {
234  assert( precision != 0 );
235 
236  long long ix = KiROUND( x * precision );
237 
238  if ( x < 0.0 )
239  ix = -ix;
240 
241  int remainder = ix % 10; // remainder is in precision mm
242 
243  if( remainder <= 2 )
244  ix -= remainder; // truncate to the near number
245  else if( remainder >= 8 )
246  ix += 10 - remainder; // round to near number
247 
248  if ( x < 0 )
249  ix = -ix;
250 
251  return (double) ix / precision;
252 }
253 
254 
255 std::unique_ptr<wxConfigBase> GetNewConfig( const wxString& aProgName )
256 {
257  wxFileName configname;
258  configname.AssignDir( GetKicadConfigPath() );
259  configname.SetFullName( aProgName );
260 
261  // explicitly use wxFileConfig to prevent storing any settings in the system registry on Windows
262  return std::make_unique<wxFileConfig>( wxT( "" ), wxT( "" ), configname.GetFullPath() );
263 }
264 
265 
267 {
268  wxFileName cfgpath;
269 
270  // http://docs.wxwidgets.org/3.0/classwx_standard_paths.html#a7c7cf595d94d29147360d031647476b0
271  cfgpath.AssignDir( wxStandardPaths::Get().GetUserConfigDir() );
272 
273  // GetUserConfigDir() does not default to ~/.config which is the current standard
274  // configuration file location on Linux. This has been fixed in later versions of wxWidgets.
275 #if !defined( __WXMSW__ ) && !defined( __WXMAC__ )
276  wxArrayString dirs = cfgpath.GetDirs();
277 
278  if( dirs.Last() != ".config" )
279  cfgpath.AppendDir( ".config" );
280 #endif
281 
282  wxString envstr;
283 
284  // This shouldn't cause any issues on Windows or MacOS.
285  if( wxGetEnv( wxT( "XDG_CONFIG_HOME" ), &envstr ) && !envstr.IsEmpty() )
286  {
287  // Override the assignment above with XDG_CONFIG_HOME
288  cfgpath.AssignDir( envstr );
289  }
290 
291  cfgpath.AppendDir( wxT( "kicad" ) );
292 
293  // Use KICAD_CONFIG_HOME to allow the user to force a specific configuration path.
294  if( wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), &envstr ) && !envstr.IsEmpty() )
295  {
296  // Override the assignment above with KICAD_CONFIG_HOME
297  cfgpath.AssignDir( envstr );
298  }
299 
300  if( !cfgpath.DirExists() )
301  {
302  cfgpath.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
303  }
304 
305  return cfgpath.GetPath();
306 }
307 
308 
310 {
314 #ifdef __WINDOWS__
315  Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
316 #endif
318 };
319 
320 
321 //
322 // Stolen from wxExpandEnvVars and then heavily optimized
323 //
324 wxString KIwxExpandEnvVars(const wxString& str)
325 {
326  size_t strlen = str.length();
327 
328  wxString strResult;
329  strResult.Alloc( strlen );
330 
331  for( size_t n = 0; n < strlen; n++ )
332  {
333  wxUniChar str_n = str[n];
334 
335  switch( str_n.GetValue() )
336  {
337 #ifdef __WINDOWS__
338  case wxT( '%' ):
339 #endif // __WINDOWS__
340  case wxT( '$' ):
341  {
342  Bracket bracket;
343 #ifdef __WINDOWS__
344  if( str_n == wxT( '%' ) )
345  bracket = Bracket_Windows;
346  else
347 #endif // __WINDOWS__
348  if( n == strlen - 1 )
349  {
350  bracket = Bracket_None;
351  }
352  else
353  {
354  switch( str[n + 1].GetValue() )
355  {
356  case wxT( '(' ):
357  bracket = Bracket_Normal;
358  str_n = str[++n]; // skip the bracket
359  break;
360 
361  case wxT( '{' ):
362  bracket = Bracket_Curly;
363  str_n = str[++n]; // skip the bracket
364  break;
365 
366  default:
367  bracket = Bracket_None;
368  }
369  }
370 
371  size_t m = n + 1;
372  wxUniChar str_m = str[m];
373 
374  while( m < strlen && ( wxIsalnum( str_m ) || str_m == wxT( '_' ) ) )
375  str_m = str[++m];
376 
377  wxString strVarName( str.c_str() + n + 1, m - n - 1 );
378 
379 #ifdef __WXWINCE__
380  const bool expanded = false;
381 #else
382  // NB: use wxGetEnv instead of wxGetenv as otherwise variables
383  // set through wxSetEnv may not be read correctly!
384  bool expanded = false;
385  wxString tmp;
386 
387  if( wxGetEnv( strVarName, &tmp ) )
388  {
389  strResult += tmp;
390  expanded = true;
391  }
392  else
393 #endif
394  {
395  // variable doesn't exist => don't change anything
396 #ifdef __WINDOWS__
397  if ( bracket != Bracket_Windows )
398 #endif
399  if ( bracket != Bracket_None )
400  strResult << str[n - 1];
401 
402  strResult << str_n << strVarName;
403  }
404 
405  // check the closing bracket
406  if( bracket != Bracket_None )
407  {
408  if( m == strlen || str_m != (wxChar)bracket )
409  {
410  // under MSW it's common to have '%' characters in the registry
411  // and it's annoying to have warnings about them each time, so
412  // ignore them silently if they are not used for env vars
413  //
414  // under Unix, OTOH, this warning could be useful for the user to
415  // understand why isn't the variable expanded as intended
416 #ifndef __WINDOWS__
417  wxLogWarning( _( "Environment variables expansion failed: missing '%c' "
418  "at position %u in '%s'." ),
419  (char)bracket, (unsigned int) (m + 1), str.c_str() );
420 #endif // __WINDOWS__
421  }
422  else
423  {
424  // skip closing bracket unless the variables wasn't expanded
425  if( !expanded )
426  strResult << (wxChar)bracket;
427 
428  str_m = str[++m];
429  }
430  }
431 
432  n = m - 1; // skip variable name
433  str_n = str[n];
434  }
435  break;
436 
437  case wxT( '\\' ):
438  // backslash can be used to suppress special meaning of % and $
439  if( n != strlen - 1 && (str[n + 1] == wxT( '%' ) || str[n + 1] == wxT( '$' )) )
440  {
441  str_n = str[++n];
442  strResult += str_n;
443 
444  break;
445  }
446  //else: fall through
447 
448  default:
449  strResult += str_n;
450  }
451  }
452 
453  return strResult;
454 }
455 
456 
457 #include <ki_mutex.h>
458 const wxString ExpandEnvVarSubstitutions( const wxString& aString )
459 {
460  // wxGetenv( wchar_t* ) is not re-entrant on linux.
461  // Put a lock on multithreaded use of wxGetenv( wchar_t* ), called from wxEpandEnvVars(),
462  static MUTEX getenv_mutex;
463 
464  MUTLOCK lock( getenv_mutex );
465 
466  // We reserve the right to do this another way, by providing our own member function.
467  return KIwxExpandEnvVars( aString );
468 }
469 
470 
471 const wxString ResolveUriByEnvVars( const wxString& aUri )
472 {
473  // URL-like URI: return as is.
474  wxURL url( aUri );
475 
476  if( url.GetError() == wxURL_NOERR )
477  return aUri;
478 
479  // Otherwise, the path points to a local file. Resolve environment
480  // variables if any.
481  return ExpandEnvVarSubstitutions( aUri );
482 }
483 
484 
485 bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName,
486  const wxString& aBaseFilename,
487  REPORTER* aReporter )
488 {
489  wxString msg;
490  wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
491 
492  // make aTargetFullFileName path, which is relative to aBaseFilename path (if it is not
493  // already an absolute path) absolute:
494  if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
495  {
496  if( aReporter )
497  {
498  msg.Printf( _( "Cannot make path \"%s\" absolute with respect to \"%s\"." ),
499  GetChars( aTargetFullFileName->GetPath() ),
500  GetChars( baseFilePath ) );
501  aReporter->Report( msg, REPORTER::RPT_ERROR );
502  }
503 
504  return false;
505  }
506 
507  // Ensure the path of aTargetFullFileName exists, and create it if needed:
508  wxString outputPath( aTargetFullFileName->GetPath() );
509 
510  if( !wxFileName::DirExists( outputPath ) )
511  {
512  if( wxMkdir( outputPath ) )
513  {
514  if( aReporter )
515  {
516  msg.Printf( _( "Output directory \"%s\" created.\n" ), GetChars( outputPath ) );
517  aReporter->Report( msg, REPORTER::RPT_INFO );
518  return true;
519  }
520  }
521  else
522  {
523  if( aReporter )
524  {
525  msg.Printf( _( "Cannot create output directory \"%s\".\n" ),
526  GetChars( outputPath ) );
527  aReporter->Report( msg, REPORTER::RPT_ERROR );
528  }
529 
530  return false;
531  }
532  }
533 
534  return true;
535 }
536 
537 
538 #ifdef __WXMAC__
539 wxString GetOSXKicadUserDataDir()
540 {
541  // According to wxWidgets documentation for GetUserDataDir:
542  // Mac: ~/Library/Application Support/appname
543  wxFileName udir( wxStandardPaths::Get().GetUserDataDir(), wxEmptyString );
544 
545  // Since appname is different if started via launcher or standalone binary
546  // map all to "kicad" here
547  udir.RemoveLastDir();
548  udir.AppendDir( "kicad" );
549 
550  return udir.GetPath();
551 }
552 
553 
554 wxString GetOSXKicadMachineDataDir()
555 {
556  return wxT( "/Library/Application Support/kicad" );
557 }
558 
559 
560 wxString GetOSXKicadDataDir()
561 {
562  // According to wxWidgets documentation for GetDataDir:
563  // Mac: appname.app/Contents/SharedSupport bundle subdirectory
564  wxFileName ddir( wxStandardPaths::Get().GetDataDir(), wxEmptyString );
565 
566  // This must be mapped to main bundle for everything but kicad.app
567  const wxArrayString dirs = ddir.GetDirs();
568  if( dirs[dirs.GetCount() - 3] != wxT( "kicad.app" ) )
569  {
570  // Bundle structure resp. current path is
571  // kicad.app/Contents/Applications/<standalone>.app/Contents/SharedSupport
572  // and will be mapped to
573  // kicad.app/Contents/SharedSupprt
574  ddir.RemoveLastDir();
575  ddir.RemoveLastDir();
576  ddir.RemoveLastDir();
577  ddir.RemoveLastDir();
578  ddir.AppendDir( wxT( "SharedSupport" ) );
579  }
580 
581  return ddir.GetPath();
582 }
583 #endif
584 
585 
586 // add this only if it is not in wxWidgets (for instance before 3.1.0)
587 #ifdef USE_KICAD_WXSTRING_HASH
588 size_t std::hash<wxString>::operator()( const wxString& s ) const
589 {
590  return std::hash<std::wstring>{}( s.ToStdWstring() );
591 }
592 #endif
593 
594 #ifdef USE_KICAD_WXPOINT_LESS_AND_HASH
595 size_t std::hash<wxPoint>::operator() ( const wxPoint& k ) const
596 {
597  return ( ( std::hash<int>()( k.x )
598  ^ ( std::hash<int>()( k.y ) << 1 ) ) >> 1 );
599 }
600 
601 bool std::less<wxPoint>::operator()( const wxPoint& aA, const wxPoint& aB ) const
602 {
603  if( aA.x == aB.x )
604  return aA.y < aB.y;
605 
606  return aA.x < aB.x;
607 }
608 #endif
609 
610 
626 WX_FILENAME::WX_FILENAME( const wxString& aPath, const wxString& aFilename ) :
627  m_fn( aPath, aFilename ),
628  m_path( aPath ),
629  m_fullName( aFilename )
630 { }
631 
632 
633 void WX_FILENAME::SetFullName( const wxString& aFileNameAndExtension )
634 {
635  m_fullName = aFileNameAndExtension;
636 }
637 
638 
639 wxString WX_FILENAME::GetName() const
640 {
641  size_t dot = m_fullName.find_last_of( wxT( '.' ) );
642  return m_fullName.substr( 0, dot );
643 }
644 
645 
646 wxString WX_FILENAME::GetFullName() const
647 {
648  return m_fullName;
649 }
650 
651 
652 wxString WX_FILENAME::GetPath() const
653 {
654  return m_path;
655 }
656 
657 
658 wxString WX_FILENAME::GetFullPath() const
659 {
660  return m_path + wxT( '/' ) + m_fullName;
661 }
662 
663 
664 // Write locally-cached values to the wxFileName. MUST be called before using m_fn.
666 {
667  size_t dot = m_fullName.find_last_of( wxT( '.' ) );
668  m_fn.SetName( m_fullName.substr( 0, dot ) );
669  m_fn.SetExt( m_fullName.substr( dot + 1 ) );
670 }
671 
672 
674 {
675  resolve();
676 
677  if( m_fn.FileExists() )
678  return m_fn.GetModificationTime().GetValue().GetValue();
679 
680  return 0;
681 }
682 
683 
690 bool matchWild( const char* pat, const char* text, bool dot_special )
691 {
692  if( !*text )
693  {
694  /* Match if both are empty. */
695  return !*pat;
696  }
697 
698  const char *m = pat,
699  *n = text,
700  *ma = NULL,
701  *na = NULL;
702  int just = 0,
703  acount = 0,
704  count = 0;
705 
706  if( dot_special && (*n == '.') )
707  {
708  /* Never match so that hidden Unix files
709  * are never found. */
710  return false;
711  }
712 
713  for(;;)
714  {
715  if( *m == '*' )
716  {
717  ma = ++m;
718  na = n;
719  just = 1;
720  acount = count;
721  }
722  else if( *m == '?' )
723  {
724  m++;
725 
726  if( !*n++ )
727  return false;
728  }
729  else
730  {
731  if( *m == '\\' )
732  {
733  m++;
734 
735  /* Quoting "nothing" is a bad thing */
736  if( !*m )
737  return false;
738  }
739  if( !*m )
740  {
741  /*
742  * If we are out of both strings or we just
743  * saw a wildcard, then we can say we have a
744  * match
745  */
746  if( !*n )
747  return true;
748 
749  if( just )
750  return true;
751 
752  just = 0;
753  goto not_matched;
754  }
755 
756  /*
757  * We could check for *n == NULL at this point, but
758  * since it's more common to have a character there,
759  * check to see if they match first (m and n) and
760  * then if they don't match, THEN we can check for
761  * the NULL of n
762  */
763  just = 0;
764 
765  if( *m == *n )
766  {
767  m++;
768  count++;
769  n++;
770  }
771  else
772  {
773  not_matched:
774 
775  /*
776  * If there are no more characters in the
777  * string, but we still need to find another
778  * character (*m != NULL), then it will be
779  * impossible to match it
780  */
781  if( !*n )
782  return false;
783 
784  if( ma )
785  {
786  m = ma;
787  n = ++na;
788  count = acount;
789  }
790  else
791  return false;
792  }
793  }
794  }
795 }
796 
797 
802 #if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
803 
804 // Convert between wxDateTime and FILETIME which is a 64-bit value representing
805 // the number of 100-nanosecond intervals since January 1, 1601 UTC.
806 //
807 // This is the offset between FILETIME epoch and the Unix/wxDateTime Epoch.
808 static wxInt64 EPOCH_OFFSET_IN_MSEC = wxLL(11644473600000);
809 
810 
811 static void ConvertFileTimeToWx( wxDateTime *dt, const FILETIME &ft )
812 {
813  wxLongLong t( ft.dwHighDateTime, ft.dwLowDateTime );
814  t /= 10000; // Convert hundreds of nanoseconds to milliseconds.
815  t -= EPOCH_OFFSET_IN_MSEC;
816 
817  *dt = wxDateTime( t );
818 }
819 
820 #endif // wxUSE_DATETIME && __WIN32__
821 
822 
832 long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec )
833 {
834  long long timestamp = 0;
835 
836 #if defined( __WIN32__ )
837  // Win32 version.
838  // Save time by not searching for each path twice: once in wxDir.GetNext() and once in
839  // wxFileName.GetModificationTime(). Also cuts out wxWidgets' string-matching and case
840  // conversion by staying on the MSW side of things.
841  std::wstring filespec( aDirPath.t_str() );
842  filespec += '\\';
843  filespec += aFilespec.t_str();
844 
845  WIN32_FIND_DATA findData;
846  wxDateTime lastModDate;
847 
848  HANDLE fileHandle = ::FindFirstFile( filespec.data(), &findData );
849 
850  if( fileHandle != INVALID_HANDLE_VALUE )
851  {
852  do
853  {
854  ConvertFileTimeToWx( &lastModDate, findData.ftLastWriteTime );
855  timestamp += lastModDate.GetValue().GetValue();
856  }
857  while ( FindNextFile( fileHandle, &findData ) != 0 );
858  }
859 
860  FindClose( fileHandle );
861 #else
862  // POSIX version.
863  // Save time by not converting between encodings -- do everything on the file-system side.
864  std::string filespec( aFilespec.fn_str() );
865  std::string dir_path( aDirPath.fn_str() );
866 
867  DIR* dir = opendir( dir_path.c_str() );
868 
869  if( dir )
870  {
871  for( dirent* dir_entry = readdir( dir ); dir_entry; dir_entry = readdir( dir ) )
872  {
873  if( !matchWild( filespec.c_str(), dir_entry->d_name, true ) )
874  continue;
875 
876  std::string entry_path = dir_path + '/' + dir_entry->d_name;
877  struct stat entry_stat;
878 
879  wxCRT_Lstat( entry_path.c_str(), &entry_stat );
880 
881  // Timestamp the source file, not the symlink
882  if( S_ISLNK( entry_stat.st_mode ) ) // wxFILE_EXISTS_SYMLINK
883  {
884  char buffer[ PATH_MAX + 1 ];
885  ssize_t pathLen = readlink( entry_path.c_str(), buffer, PATH_MAX );
886 
887  if( pathLen > 0 )
888  {
889  buffer[ pathLen ] = '\0';
890  entry_path = dir_path + buffer;
891 
892  wxCRT_Lstat( entry_path.c_str(), &entry_stat );
893  }
894  }
895 
896  if( S_ISREG( entry_stat.st_mode ) ) // wxFileExists()
897  timestamp += entry_stat.st_mtime * 1000;
898  }
899 
900  closedir( dir );
901  }
902 #endif
903 
904  return timestamp;
905 }
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: common.cpp:187
WX_FILENAME(const wxString &aPath, const wxString &aFilename)
Performance enhancements to file and directory operations.
Definition: common.cpp:626
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:210
~LOCALE_IO()
Definition: common.cpp:99
long timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: common.h:53
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:121
const wxString ResolveUriByEnvVars(const wxString &aUri)
Replace any environment variables in file-path uris (leaving network-path URIs alone).
Definition: common.cpp:471
Implementation of conversion functions that require both schematic and board internal units.
std::unique_ptr< wxConfigBase > GetNewConfig(const wxString &aProgName)
Create a new wxConfig so we can put configuration files in a more proper place for each platform.
Definition: common.cpp:255
double RoundTo0(double x, double precision)
Round to the nearest precision.
Definition: common.cpp:232
wxString KIwxExpandEnvVars(const wxString &str)
Definition: common.cpp:324
wxString GetName() const
Definition: common.cpp:639
wxString GetFullName() const
Definition: common.cpp:646
boost::interprocess::interprocess_mutex MUTEX
Establish KiCad MUTEX choices here in this file: typedef MUTEX and typedef MUTLOCK.
Definition: ki_mutex.h:42
wxString m_path
Definition: common.h:420
size_t operator()(const wxPoint &k) const
Definition: common.cpp:595
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
void SetFullName(const wxString &aFileNameAndExtension)
Definition: common.cpp:633
const string & str
Definition: json11.cpp:596
This file contains miscellaneous commonly used macros and functions.
wxSize GetTextSize(const wxString &aSingleLine, wxWindow *aWindow)
Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currentl...
Definition: common.cpp:114
wxFileName m_fn
Definition: common.h:419
static std::atomic< unsigned int > m_c_count
Definition: common.h:188
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: common.cpp:158
long long GetTimestamp()
Definition: common.cpp:673
timestamp_t GetNewTimeStamp()
Definition: common.cpp:216
void resolve()
Definition: common.cpp:665
bool EnsureTextCtrlWidth(wxTextCtrl *aCtrl, const wxString *aString)
Set the minimum pixel width on a text control in order to make a text string be fully visible within ...
Definition: common.cpp:129
const wxString ExpandEnvVarSubstitutions(const wxString &aString)
Replace any environment variable references with their values.
Definition: common.cpp:458
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:485
Bracket
Definition: common.cpp:309
bool operator()(const wxPoint &aA, const wxPoint &aB) const
Definition: common.cpp:601
Base window classes and related definitions.
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:832
LOCALE_IO()
Definition: common.cpp:82
wxString GetKicadConfigPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: common.cpp:266
bool matchWild(const char *pat, const char *text, bool dot_special)
A copy of wxMatchWild(), which wxWidgets attributes to Douglas A.
Definition: common.cpp:690
wxString GetPath() const
Definition: common.cpp:652
wxString GetFullPath() const
Definition: common.cpp:658
boost::interprocess::scoped_lock< MUTEX > MUTLOCK
Definition: ki_mutex.h:43
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
see class PGM_BASE
The common library.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
Basic classes for most KiCad items.
wxString m_fullName
Definition: common.h:421
COLOR4D g_GhostColor
Global variables definitions.
Definition: common.cpp:57
std::string m_user_locale
Definition: common.h:192
size_t operator()(const wxString &s) const
Definition: common.cpp:588
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39