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 #include <mutex>
38 
39 #include <wx/process.h>
40 #include <wx/config.h>
41 #include <wx/utils.h>
42 #include <wx/stdpaths.h>
43 #include <wx/url.h>
44 
45 #include <pgm_base.h>
46 
47 using KIGFX::COLOR4D;
48 
49 
59 
60 
61 #if defined( _WIN32 ) && defined( DEBUG )
62 // a wxAssertHandler_t function to filter wxWidgets alert messages when reading/writing a file
63 // when switching the locale to LC_NUMERIC, "C"
64 // It is used in class LOCALE_IO to hide a useless (in kicad) wxWidgets alert message
65 void KiAssertFilter( const wxString &file, int line,
66  const wxString &func, const wxString &cond,
67  const wxString &msg)
68 {
69  if( !msg.Contains( "Decimal separator mismatch" ) )
70  wxTheApp->OnAssertFailure( file, line, func, cond, msg );
71 }
72 #endif
73 
74 
75 std::atomic<unsigned int> LOCALE_IO::m_c_count( 0 );
76 
77 
78 // Note on Windows, setlocale( LC_NUMERIC, "C" ) works fine to read/write
79 // files with floating point numbers, but generates a overzealous wx alert
80 // in some cases (reading a bitmap for instance)
81 // So we disable alerts during the time a file is read or written
82 
84 {
85  // use thread safe, atomic operation
86  if( m_c_count++ == 0 )
87  {
88  // Store the user locale name, to restore this locale later, in dtor
89  m_user_locale = setlocale( LC_NUMERIC, nullptr );
90 #if defined( _WIN32 ) && defined( DEBUG )
91  // Disable wxWidgets alerts
92  wxSetAssertHandler( KiAssertFilter );
93 #endif
94  // Switch the locale to C locale, to read/write files with fp numbers
95  setlocale( LC_NUMERIC, "C" );
96  }
97 }
98 
99 
101 {
102  // use thread safe, atomic operation
103  if( --m_c_count == 0 )
104  {
105  // revert to the user locale
106  setlocale( LC_NUMERIC, m_user_locale.c_str() );
107 #if defined( _WIN32 ) && defined( DEBUG )
108  // Enable wxWidgets alerts
109  wxSetDefaultAssertHandler();
110 #endif
111  }
112 }
113 
114 
115 wxSize GetTextSize( const wxString& aSingleLine, wxWindow* aWindow )
116 {
117  wxCoord width;
118  wxCoord height;
119 
120  {
121  wxClientDC dc( aWindow );
122  dc.SetFont( aWindow->GetFont() );
123  dc.GetTextExtent( aSingleLine, &width, &height );
124  }
125 
126  return wxSize( width, height );
127 }
128 
129 
130 bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString )
131 {
132  wxWindow* window = aCtrl->GetParent();
133 
134  if( !window )
135  window = aCtrl;
136 
137  wxString ctrlText;
138 
139  if( !aString )
140  {
141  ctrlText = aCtrl->GetValue();
142  aString = &ctrlText;
143  }
144 
145  wxSize textz = GetTextSize( *aString, window );
146  wxSize ctrlz = aCtrl->GetSize();
147 
148  if( ctrlz.GetWidth() < textz.GetWidth() + 10 )
149  {
150  ctrlz.SetWidth( textz.GetWidth() + 10 );
151  aCtrl->SetSizeHints( ctrlz );
152  return true;
153  }
154 
155  return false;
156 }
157 
158 
159 void SelectReferenceNumber( wxTextEntry* aTextEntry )
160 {
161  wxString ref = aTextEntry->GetValue();
162 
163  if( ref.find_first_of( '?' ) != ref.npos )
164  {
165  aTextEntry->SetSelection( ref.find_first_of( '?' ), ref.find_last_of( '?' ) + 1 );
166  }
167  else
168  {
169  wxString num = ref;
170 
171  while( !num.IsEmpty() && ( !isdigit( num.Last() ) || !isdigit( num.GetChar( 0 ) ) ) )
172  {
173  if( !isdigit( num.Last() ) )
174  num.RemoveLast();
175 
176  if( !isdigit( num.GetChar ( 0 ) ) )
177  num = num.Right( num.Length() - 1);
178  }
179 
180  aTextEntry->SetSelection( ref.Find( num ), ref.Find( num ) + num.Length() );
181 
182  if( num.IsEmpty() )
183  aTextEntry->SetSelection( -1, -1 );
184  }
185 }
186 
187 
188 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter )
189 {
190  wxString tmp;
191 
192  for( unsigned ii = 0; ii < aText.Length(); ii++ )
193  {
194  if( aText[ii] == aSplitter )
195  {
196  aStrings.Add( tmp );
197  tmp.Clear();
198  }
199 
200  else
201  tmp << aText[ii];
202  }
203 
204  if( !tmp.IsEmpty() )
205  {
206  aStrings.Add( tmp );
207  }
208 }
209 
210 
211 int ProcessExecute( const wxString& aCommandLine, int aFlags, wxProcess *callback )
212 {
213  return wxExecute( aCommandLine, aFlags, callback );
214 }
215 
216 
218 {
219  static timestamp_t oldTimeStamp;
220  timestamp_t newTimeStamp;
221 
222  newTimeStamp = time( NULL );
223 
224  if( newTimeStamp <= oldTimeStamp )
225  newTimeStamp = oldTimeStamp + 1;
226 
227  oldTimeStamp = newTimeStamp;
228 
229  return newTimeStamp;
230 }
231 
232 
233 double RoundTo0( double x, double precision )
234 {
235  assert( precision != 0 );
236 
237  long long ix = KiROUND( x * precision );
238 
239  if ( x < 0.0 )
240  ix = -ix;
241 
242  int remainder = ix % 10; // remainder is in precision mm
243 
244  if( remainder <= 2 )
245  ix -= remainder; // truncate to the near number
246  else if( remainder >= 8 )
247  ix += 10 - remainder; // round to near number
248 
249  if ( x < 0 )
250  ix = -ix;
251 
252  return (double) ix / precision;
253 }
254 
255 
256 std::unique_ptr<wxConfigBase> GetNewConfig( const wxString& aProgName )
257 {
258  wxFileName configname;
259  configname.AssignDir( GetKicadConfigPath() );
260  configname.SetFullName( aProgName );
261 
262  // explicitly use wxFileConfig to prevent storing any settings in the system registry on Windows
263  return std::make_unique<wxFileConfig>( wxT( "" ), wxT( "" ), configname.GetFullPath() );
264 }
265 
266 
268 {
269  wxFileName cfgpath;
270 
271  // http://docs.wxwidgets.org/3.0/classwx_standard_paths.html#a7c7cf595d94d29147360d031647476b0
272  cfgpath.AssignDir( wxStandardPaths::Get().GetUserConfigDir() );
273 
274  // GetUserConfigDir() does not default to ~/.config which is the current standard
275  // configuration file location on Linux. This has been fixed in later versions of wxWidgets.
276 #if !defined( __WXMSW__ ) && !defined( __WXMAC__ )
277  wxArrayString dirs = cfgpath.GetDirs();
278 
279  if( dirs.Last() != ".config" )
280  cfgpath.AppendDir( ".config" );
281 #endif
282 
283  wxString envstr;
284 
285  // This shouldn't cause any issues on Windows or MacOS.
286  if( wxGetEnv( wxT( "XDG_CONFIG_HOME" ), &envstr ) && !envstr.IsEmpty() )
287  {
288  // Override the assignment above with XDG_CONFIG_HOME
289  cfgpath.AssignDir( envstr );
290  }
291 
292  cfgpath.AppendDir( TO_STR( KICAD_CONFIG_DIR ) );
293 
294  // Use KICAD_CONFIG_HOME to allow the user to force a specific configuration path.
295  if( wxGetEnv( wxT( "KICAD_CONFIG_HOME" ), &envstr ) && !envstr.IsEmpty() )
296  {
297  // Override the assignment above with KICAD_CONFIG_HOME
298  cfgpath.AssignDir( envstr );
299  }
300 
301  if( !cfgpath.DirExists() )
302  {
303  cfgpath.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
304  }
305 
306  return cfgpath.GetPath();
307 }
308 
309 
311 {
315 #ifdef __WINDOWS__
316  Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
317 #endif
319 };
320 
321 
322 //
323 // Stolen from wxExpandEnvVars and then heavily optimized
324 //
325 wxString KIwxExpandEnvVars(const wxString& str)
326 {
327  size_t strlen = str.length();
328 
329  wxString strResult;
330  strResult.Alloc( strlen );
331 
332  for( size_t n = 0; n < strlen; n++ )
333  {
334  wxUniChar str_n = str[n];
335 
336  switch( str_n.GetValue() )
337  {
338 #ifdef __WINDOWS__
339  case wxT( '%' ):
340 #endif // __WINDOWS__
341  case wxT( '$' ):
342  {
343  Bracket bracket;
344 #ifdef __WINDOWS__
345  if( str_n == wxT( '%' ) )
346  bracket = Bracket_Windows;
347  else
348 #endif // __WINDOWS__
349  if( n == strlen - 1 )
350  {
351  bracket = Bracket_None;
352  }
353  else
354  {
355  switch( str[n + 1].GetValue() )
356  {
357  case wxT( '(' ):
358  bracket = Bracket_Normal;
359  str_n = str[++n]; // skip the bracket
360  break;
361 
362  case wxT( '{' ):
363  bracket = Bracket_Curly;
364  str_n = str[++n]; // skip the bracket
365  break;
366 
367  default:
368  bracket = Bracket_None;
369  }
370  }
371 
372  size_t m = n + 1;
373  wxUniChar str_m = str[m];
374 
375  while( m < strlen && ( wxIsalnum( str_m ) || str_m == wxT( '_' ) ) )
376  str_m = str[++m];
377 
378  wxString strVarName( str.c_str() + n + 1, m - n - 1 );
379 
380 #ifdef __WXWINCE__
381  const bool expanded = false;
382 #else
383  // NB: use wxGetEnv instead of wxGetenv as otherwise variables
384  // set through wxSetEnv may not be read correctly!
385  bool expanded = false;
386  wxString tmp;
387 
388  if( wxGetEnv( strVarName, &tmp ) )
389  {
390  strResult += tmp;
391  expanded = true;
392  }
393  else
394 #endif
395  {
396  // variable doesn't exist => don't change anything
397 #ifdef __WINDOWS__
398  if ( bracket != Bracket_Windows )
399 #endif
400  if ( bracket != Bracket_None )
401  strResult << str[n - 1];
402 
403  strResult << str_n << strVarName;
404  }
405 
406  // check the closing bracket
407  if( bracket != Bracket_None )
408  {
409  if( m == strlen || str_m != (wxChar)bracket )
410  {
411  // under MSW it's common to have '%' characters in the registry
412  // and it's annoying to have warnings about them each time, so
413  // ignore them silently if they are not used for env vars
414  //
415  // under Unix, OTOH, this warning could be useful for the user to
416  // understand why isn't the variable expanded as intended
417 #ifndef __WINDOWS__
418  wxLogWarning( _( "Environment variables expansion failed: missing '%c' "
419  "at position %u in '%s'." ),
420  (char)bracket, (unsigned int) (m + 1), str.c_str() );
421 #endif // __WINDOWS__
422  }
423  else
424  {
425  // skip closing bracket unless the variables wasn't expanded
426  if( !expanded )
427  strResult << (wxChar)bracket;
428 
429  str_m = str[++m];
430  }
431  }
432 
433  n = m - 1; // skip variable name
434  str_n = str[n];
435  }
436  break;
437 
438  case wxT( '\\' ):
439  // backslash can be used to suppress special meaning of % and $
440  if( n != strlen - 1 && (str[n + 1] == wxT( '%' ) || str[n + 1] == wxT( '$' )) )
441  {
442  str_n = str[++n];
443  strResult += str_n;
444 
445  break;
446  }
447  //else: fall through
448 
449  default:
450  strResult += str_n;
451  }
452  }
453 
454  return strResult;
455 }
456 
457 
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 std::mutex getenv_mutex;
463 
464  std::lock_guard<std::mutex> 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  auto xhash = std::hash<int>()( k.x );
598 
599  // 0x9e3779b9 is 2^33 / ( 1 + sqrt(5) )
600  // Adding this value ensures that consecutive bits of y will not be close to each other
601  // decreasing the likelihood of hash collision in similar values of x and y
602  return xhash ^ ( std::hash<int>()( k.y ) + 0x9e3779b9 + ( xhash << 6 ) + ( xhash >> 2 ) );
603 }
604 
605 bool std::less<wxPoint>::operator()( const wxPoint& aA, const wxPoint& aB ) const
606 {
607  if( aA.x == aB.x )
608  return aA.y < aB.y;
609 
610  return aA.x < aB.x;
611 }
612 #endif
613 
614 
630 WX_FILENAME::WX_FILENAME( const wxString& aPath, const wxString& aFilename ) :
631  m_fn( aPath, aFilename ),
632  m_path( aPath ),
633  m_fullName( aFilename )
634 { }
635 
636 
637 void WX_FILENAME::SetFullName( const wxString& aFileNameAndExtension )
638 {
639  m_fullName = aFileNameAndExtension;
640 }
641 
642 
643 wxString WX_FILENAME::GetName() const
644 {
645  size_t dot = m_fullName.find_last_of( wxT( '.' ) );
646  return m_fullName.substr( 0, dot );
647 }
648 
649 
650 wxString WX_FILENAME::GetFullName() const
651 {
652  return m_fullName;
653 }
654 
655 
656 wxString WX_FILENAME::GetPath() const
657 {
658  return m_path;
659 }
660 
661 
662 wxString WX_FILENAME::GetFullPath() const
663 {
664  return m_path + wxT( '/' ) + m_fullName;
665 }
666 
667 
668 // Write locally-cached values to the wxFileName. MUST be called before using m_fn.
670 {
671  size_t dot = m_fullName.find_last_of( wxT( '.' ) );
672  m_fn.SetName( m_fullName.substr( 0, dot ) );
673  m_fn.SetExt( m_fullName.substr( dot + 1 ) );
674 }
675 
676 
678 {
679  resolve();
680 
681  if( m_fn.FileExists() )
682  return m_fn.GetModificationTime().GetValue().GetValue();
683 
684  return 0;
685 }
686 
687 
694 bool matchWild( const char* pat, const char* text, bool dot_special )
695 {
696  if( !*text )
697  {
698  /* Match if both are empty. */
699  return !*pat;
700  }
701 
702  const char *m = pat,
703  *n = text,
704  *ma = NULL,
705  *na = NULL;
706  int just = 0,
707  acount = 0,
708  count = 0;
709 
710  if( dot_special && (*n == '.') )
711  {
712  /* Never match so that hidden Unix files
713  * are never found. */
714  return false;
715  }
716 
717  for(;;)
718  {
719  if( *m == '*' )
720  {
721  ma = ++m;
722  na = n;
723  just = 1;
724  acount = count;
725  }
726  else if( *m == '?' )
727  {
728  m++;
729 
730  if( !*n++ )
731  return false;
732  }
733  else
734  {
735  if( *m == '\\' )
736  {
737  m++;
738 
739  /* Quoting "nothing" is a bad thing */
740  if( !*m )
741  return false;
742  }
743  if( !*m )
744  {
745  /*
746  * If we are out of both strings or we just
747  * saw a wildcard, then we can say we have a
748  * match
749  */
750  if( !*n )
751  return true;
752 
753  if( just )
754  return true;
755 
756  just = 0;
757  goto not_matched;
758  }
759 
760  /*
761  * We could check for *n == NULL at this point, but
762  * since it's more common to have a character there,
763  * check to see if they match first (m and n) and
764  * then if they don't match, THEN we can check for
765  * the NULL of n
766  */
767  just = 0;
768 
769  if( *m == *n )
770  {
771  m++;
772  count++;
773  n++;
774  }
775  else
776  {
777  not_matched:
778 
779  /*
780  * If there are no more characters in the
781  * string, but we still need to find another
782  * character (*m != NULL), then it will be
783  * impossible to match it
784  */
785  if( !*n )
786  return false;
787 
788  if( ma )
789  {
790  m = ma;
791  n = ++na;
792  count = acount;
793  }
794  else
795  return false;
796  }
797  }
798  }
799 }
800 
801 
806 #if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
807 
808 // Convert between wxDateTime and FILETIME which is a 64-bit value representing
809 // the number of 100-nanosecond intervals since January 1, 1601 UTC.
810 //
811 // This is the offset between FILETIME epoch and the Unix/wxDateTime Epoch.
812 static wxInt64 EPOCH_OFFSET_IN_MSEC = wxLL(11644473600000);
813 
814 
815 static void ConvertFileTimeToWx( wxDateTime *dt, const FILETIME &ft )
816 {
817  wxLongLong t( ft.dwHighDateTime, ft.dwLowDateTime );
818  t /= 10000; // Convert hundreds of nanoseconds to milliseconds.
819  t -= EPOCH_OFFSET_IN_MSEC;
820 
821  *dt = wxDateTime( t );
822 }
823 
824 #endif // wxUSE_DATETIME && __WIN32__
825 
826 
836 long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec )
837 {
838  long long timestamp = 0;
839 
840 #if defined( __WIN32__ )
841  // Win32 version.
842  // Save time by not searching for each path twice: once in wxDir.GetNext() and once in
843  // wxFileName.GetModificationTime(). Also cuts out wxWidgets' string-matching and case
844  // conversion by staying on the MSW side of things.
845  std::wstring filespec( aDirPath.t_str() );
846  filespec += '\\';
847  filespec += aFilespec.t_str();
848 
849  WIN32_FIND_DATA findData;
850  wxDateTime lastModDate;
851 
852  HANDLE fileHandle = ::FindFirstFile( filespec.data(), &findData );
853 
854  if( fileHandle != INVALID_HANDLE_VALUE )
855  {
856  do
857  {
858  ConvertFileTimeToWx( &lastModDate, findData.ftLastWriteTime );
859  timestamp += lastModDate.GetValue().GetValue();
860  }
861  while ( FindNextFile( fileHandle, &findData ) != 0 );
862  }
863 
864  FindClose( fileHandle );
865 #else
866  // POSIX version.
867  // Save time by not converting between encodings -- do everything on the file-system side.
868  std::string filespec( aFilespec.fn_str() );
869  std::string dir_path( aDirPath.fn_str() );
870 
871  DIR* dir = opendir( dir_path.c_str() );
872 
873  if( dir )
874  {
875  for( dirent* dir_entry = readdir( dir ); dir_entry; dir_entry = readdir( dir ) )
876  {
877  if( !matchWild( filespec.c_str(), dir_entry->d_name, true ) )
878  continue;
879 
880  std::string entry_path = dir_path + '/' + dir_entry->d_name;
881  struct stat entry_stat;
882 
883  wxCRT_Lstat( entry_path.c_str(), &entry_stat );
884 
885  // Timestamp the source file, not the symlink
886  if( S_ISLNK( entry_stat.st_mode ) ) // wxFILE_EXISTS_SYMLINK
887  {
888  char buffer[ PATH_MAX + 1 ];
889  ssize_t pathLen = readlink( entry_path.c_str(), buffer, PATH_MAX );
890 
891  if( pathLen > 0 )
892  {
893  buffer[ pathLen ] = '\0';
894  entry_path = dir_path + buffer;
895 
896  wxCRT_Lstat( entry_path.c_str(), &entry_stat );
897  }
898  }
899 
900  if( S_ISREG( entry_stat.st_mode ) ) // wxFileExists()
901  timestamp += entry_stat.st_mtime * 1000;
902  }
903 
904  closedir( dir );
905  }
906 #endif
907 
908  return timestamp;
909 }
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: common.cpp:188
WX_FILENAME(const wxString &aPath, const wxString &aFilename)
Performance enhancements to file and directory operations.
Definition: common.cpp:630
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:211
~LOCALE_IO()
Definition: common.cpp:100
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:118
#define TO_STR(x)
Definition: macros.h:55
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:256
double RoundTo0(double x, double precision)
Round to the nearest precision.
Definition: common.cpp:233
wxString KIwxExpandEnvVars(const wxString &str)
Definition: common.cpp:325
wxString GetName() const
Definition: common.cpp:643
wxString GetFullName() const
Definition: common.cpp:650
wxString m_path
Definition: common.h:417
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:637
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:115
wxFileName m_fn
Definition: common.h:416
static std::atomic< unsigned int > m_c_count
Definition: common.h:185
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: common.cpp:159
long long GetTimestamp()
Definition: common.cpp:677
timestamp_t GetNewTimeStamp()
Definition: common.cpp:217
void resolve()
Definition: common.cpp:669
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:130
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:310
bool operator()(const wxPoint &aA, const wxPoint &aB) const
Definition: common.cpp:605
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:836
LOCALE_IO()
Definition: common.cpp:83
wxString GetKicadConfigPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: common.cpp:267
bool matchWild(const char *pat, const char *text, bool dot_special)
A copy of wxMatchWild(), which wxWidgets attributes to Douglas A.
Definition: common.cpp:694
wxString GetPath() const
Definition: common.cpp:656
wxString GetFullPath() const
Definition: common.cpp:662
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:100
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.
uint32_t timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: common.h:53
Basic classes for most KiCad items.
wxString m_fullName
Definition: common.h:418
COLOR4D g_GhostColor
Global variables definitions.
Definition: common.cpp:58
std::string m_user_locale
Definition: common.h:189
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