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  // NB: use wxGetEnv instead of wxGetenv as otherwise variables
381  // set through wxSetEnv may not be read correctly!
382  bool expanded = false;
383  wxString tmp;
384 
385  if( wxGetEnv( strVarName, &tmp ) )
386  {
387  strResult += tmp;
388  expanded = true;
389  }
390  else
391  {
392  // variable doesn't exist => don't change anything
393 #ifdef __WINDOWS__
394  if ( bracket != Bracket_Windows )
395 #endif
396  if ( bracket != Bracket_None )
397  strResult << str[n - 1];
398 
399  strResult << str_n << strVarName;
400  }
401 
402  // check the closing bracket
403  if( bracket != Bracket_None )
404  {
405  if( m == strlen || str_m != (wxChar)bracket )
406  {
407  // under MSW it's common to have '%' characters in the registry
408  // and it's annoying to have warnings about them each time, so
409  // ignore them silently if they are not used for env vars
410  //
411  // under Unix, OTOH, this warning could be useful for the user to
412  // understand why isn't the variable expanded as intended
413 #ifndef __WINDOWS__
414  wxLogWarning( _( "Environment variables expansion failed: missing '%c' "
415  "at position %u in '%s'." ),
416  (char)bracket, (unsigned int) (m + 1), str.c_str() );
417 #endif // __WINDOWS__
418  }
419  else
420  {
421  // skip closing bracket unless the variables wasn't expanded
422  if( !expanded )
423  strResult << (wxChar)bracket;
424 
425  m++;
426  }
427  }
428 
429  n = m - 1; // skip variable name
430  str_n = str[n];
431  }
432  break;
433 
434  case wxT( '\\' ):
435  // backslash can be used to suppress special meaning of % and $
436  if( n != strlen - 1 && (str[n + 1] == wxT( '%' ) || str[n + 1] == wxT( '$' )) )
437  {
438  str_n = str[++n];
439  strResult += str_n;
440 
441  break;
442  }
443  //else: fall through
444 
445  default:
446  strResult += str_n;
447  }
448  }
449 
450  return strResult;
451 }
452 
453 
454 const wxString ExpandEnvVarSubstitutions( const wxString& aString )
455 {
456  // wxGetenv( wchar_t* ) is not re-entrant on linux.
457  // Put a lock on multithreaded use of wxGetenv( wchar_t* ), called from wxEpandEnvVars(),
458  static std::mutex getenv_mutex;
459 
460  std::lock_guard<std::mutex> lock( getenv_mutex );
461 
462  // We reserve the right to do this another way, by providing our own member function.
463  return KIwxExpandEnvVars( aString );
464 }
465 
466 
467 const wxString ResolveUriByEnvVars( const wxString& aUri )
468 {
469  // URL-like URI: return as is.
470  wxURL url( aUri );
471 
472  if( url.GetError() == wxURL_NOERR )
473  return aUri;
474 
475  // Otherwise, the path points to a local file. Resolve environment
476  // variables if any.
477  return ExpandEnvVarSubstitutions( aUri );
478 }
479 
480 
481 bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName,
482  const wxString& aBaseFilename,
483  REPORTER* aReporter )
484 {
485  wxString msg;
486  wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
487 
488  // make aTargetFullFileName path, which is relative to aBaseFilename path (if it is not
489  // already an absolute path) absolute:
490  if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
491  {
492  if( aReporter )
493  {
494  msg.Printf( _( "Cannot make path \"%s\" absolute with respect to \"%s\"." ),
495  GetChars( aTargetFullFileName->GetPath() ),
496  GetChars( baseFilePath ) );
497  aReporter->Report( msg, REPORTER::RPT_ERROR );
498  }
499 
500  return false;
501  }
502 
503  // Ensure the path of aTargetFullFileName exists, and create it if needed:
504  wxString outputPath( aTargetFullFileName->GetPath() );
505 
506  if( !wxFileName::DirExists( outputPath ) )
507  {
508  if( wxMkdir( outputPath ) )
509  {
510  if( aReporter )
511  {
512  msg.Printf( _( "Output directory \"%s\" created.\n" ), GetChars( outputPath ) );
513  aReporter->Report( msg, REPORTER::RPT_INFO );
514  return true;
515  }
516  }
517  else
518  {
519  if( aReporter )
520  {
521  msg.Printf( _( "Cannot create output directory \"%s\".\n" ),
522  GetChars( outputPath ) );
523  aReporter->Report( msg, REPORTER::RPT_ERROR );
524  }
525 
526  return false;
527  }
528  }
529 
530  return true;
531 }
532 
533 
534 #ifdef __WXMAC__
535 wxString GetOSXKicadUserDataDir()
536 {
537  // According to wxWidgets documentation for GetUserDataDir:
538  // Mac: ~/Library/Application Support/appname
539  wxFileName udir( wxStandardPaths::Get().GetUserDataDir(), wxEmptyString );
540 
541  // Since appname is different if started via launcher or standalone binary
542  // map all to "kicad" here
543  udir.RemoveLastDir();
544  udir.AppendDir( "kicad" );
545 
546  return udir.GetPath();
547 }
548 
549 
550 wxString GetOSXKicadMachineDataDir()
551 {
552  return wxT( "/Library/Application Support/kicad" );
553 }
554 
555 
556 wxString GetOSXKicadDataDir()
557 {
558  // According to wxWidgets documentation for GetDataDir:
559  // Mac: appname.app/Contents/SharedSupport bundle subdirectory
560  wxFileName ddir( wxStandardPaths::Get().GetDataDir(), wxEmptyString );
561 
562  // This must be mapped to main bundle for everything but kicad.app
563  const wxArrayString dirs = ddir.GetDirs();
564  if( dirs[dirs.GetCount() - 3] != wxT( "kicad.app" ) )
565  {
566  // Bundle structure resp. current path is
567  // kicad.app/Contents/Applications/<standalone>.app/Contents/SharedSupport
568  // and will be mapped to
569  // kicad.app/Contents/SharedSupprt
570  ddir.RemoveLastDir();
571  ddir.RemoveLastDir();
572  ddir.RemoveLastDir();
573  ddir.RemoveLastDir();
574  ddir.AppendDir( wxT( "SharedSupport" ) );
575  }
576 
577  return ddir.GetPath();
578 }
579 #endif
580 
581 
582 // add this only if it is not in wxWidgets (for instance before 3.1.0)
583 #ifdef USE_KICAD_WXSTRING_HASH
584 size_t std::hash<wxString>::operator()( const wxString& s ) const
585 {
586  return std::hash<std::wstring>{}( s.ToStdWstring() );
587 }
588 #endif
589 
590 #ifdef USE_KICAD_WXPOINT_LESS_AND_HASH
591 size_t std::hash<wxPoint>::operator() ( const wxPoint& k ) const
592 {
593  auto xhash = std::hash<int>()( k.x );
594 
595  // 0x9e3779b9 is 2^33 / ( 1 + sqrt(5) )
596  // Adding this value ensures that consecutive bits of y will not be close to each other
597  // decreasing the likelihood of hash collision in similar values of x and y
598  return xhash ^ ( std::hash<int>()( k.y ) + 0x9e3779b9 + ( xhash << 6 ) + ( xhash >> 2 ) );
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 
611 std::ostream& operator<<( std::ostream& out, const wxSize& size )
612 {
613  out << " width=\"" << size.GetWidth() << "\" height=\"" << size.GetHeight() << "\"";
614  return out;
615 }
616 
617 
618 std::ostream& operator<<( std::ostream& out, const wxPoint& pt )
619 {
620  out << " x=\"" << pt.x << "\" y=\"" << pt.y << "\"";
621  return out;
622 }
623 
624 
640 WX_FILENAME::WX_FILENAME( const wxString& aPath, const wxString& aFilename ) :
641  m_fn( aPath, aFilename ),
642  m_path( aPath ),
643  m_fullName( aFilename )
644 { }
645 
646 
647 void WX_FILENAME::SetFullName( const wxString& aFileNameAndExtension )
648 {
649  m_fullName = aFileNameAndExtension;
650 }
651 
652 
653 wxString WX_FILENAME::GetName() const
654 {
655  size_t dot = m_fullName.find_last_of( wxT( '.' ) );
656  return m_fullName.substr( 0, dot );
657 }
658 
659 
660 wxString WX_FILENAME::GetFullName() const
661 {
662  return m_fullName;
663 }
664 
665 
666 wxString WX_FILENAME::GetPath() const
667 {
668  return m_path;
669 }
670 
671 
672 wxString WX_FILENAME::GetFullPath() const
673 {
674  return m_path + wxT( '/' ) + m_fullName;
675 }
676 
677 
678 // Write locally-cached values to the wxFileName. MUST be called before using m_fn.
680 {
681  size_t dot = m_fullName.find_last_of( wxT( '.' ) );
682  m_fn.SetName( m_fullName.substr( 0, dot ) );
683  m_fn.SetExt( m_fullName.substr( dot + 1 ) );
684 }
685 
686 
688 {
689  resolve();
690 
691  if( m_fn.FileExists() )
692  return m_fn.GetModificationTime().GetValue().GetValue();
693 
694  return 0;
695 }
696 
697 
704 bool matchWild( const char* pat, const char* text, bool dot_special )
705 {
706  if( !*text )
707  {
708  /* Match if both are empty. */
709  return !*pat;
710  }
711 
712  const char *m = pat,
713  *n = text,
714  *ma = NULL,
715  *na = NULL;
716  int just = 0,
717  acount = 0,
718  count = 0;
719 
720  if( dot_special && (*n == '.') )
721  {
722  /* Never match so that hidden Unix files
723  * are never found. */
724  return false;
725  }
726 
727  for(;;)
728  {
729  if( *m == '*' )
730  {
731  ma = ++m;
732  na = n;
733  just = 1;
734  acount = count;
735  }
736  else if( *m == '?' )
737  {
738  m++;
739 
740  if( !*n++ )
741  return false;
742  }
743  else
744  {
745  if( *m == '\\' )
746  {
747  m++;
748 
749  /* Quoting "nothing" is a bad thing */
750  if( !*m )
751  return false;
752  }
753  if( !*m )
754  {
755  /*
756  * If we are out of both strings or we just
757  * saw a wildcard, then we can say we have a
758  * match
759  */
760  if( !*n )
761  return true;
762 
763  if( just )
764  return true;
765 
766  just = 0;
767  goto not_matched;
768  }
769 
770  /*
771  * We could check for *n == NULL at this point, but
772  * since it's more common to have a character there,
773  * check to see if they match first (m and n) and
774  * then if they don't match, THEN we can check for
775  * the NULL of n
776  */
777  just = 0;
778 
779  if( *m == *n )
780  {
781  m++;
782  count++;
783  n++;
784  }
785  else
786  {
787  not_matched:
788 
789  /*
790  * If there are no more characters in the
791  * string, but we still need to find another
792  * character (*m != NULL), then it will be
793  * impossible to match it
794  */
795  if( !*n )
796  return false;
797 
798  if( ma )
799  {
800  m = ma;
801  n = ++na;
802  count = acount;
803  }
804  else
805  return false;
806  }
807  }
808  }
809 }
810 
811 
816 #if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
817 
818 // Convert between wxDateTime and FILETIME which is a 64-bit value representing
819 // the number of 100-nanosecond intervals since January 1, 1601 UTC.
820 //
821 // This is the offset between FILETIME epoch and the Unix/wxDateTime Epoch.
822 static wxInt64 EPOCH_OFFSET_IN_MSEC = wxLL(11644473600000);
823 
824 
825 static void ConvertFileTimeToWx( wxDateTime *dt, const FILETIME &ft )
826 {
827  wxLongLong t( ft.dwHighDateTime, ft.dwLowDateTime );
828  t /= 10000; // Convert hundreds of nanoseconds to milliseconds.
829  t -= EPOCH_OFFSET_IN_MSEC;
830 
831  *dt = wxDateTime( t );
832 }
833 
834 #endif // wxUSE_DATETIME && __WIN32__
835 
836 
846 long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec )
847 {
848  long long timestamp = 0;
849 
850 #if defined( __WIN32__ )
851  // Win32 version.
852  // Save time by not searching for each path twice: once in wxDir.GetNext() and once in
853  // wxFileName.GetModificationTime(). Also cuts out wxWidgets' string-matching and case
854  // conversion by staying on the MSW side of things.
855  std::wstring filespec( aDirPath.t_str() );
856  filespec += '\\';
857  filespec += aFilespec.t_str();
858 
859  WIN32_FIND_DATA findData;
860  wxDateTime lastModDate;
861 
862  HANDLE fileHandle = ::FindFirstFile( filespec.data(), &findData );
863 
864  if( fileHandle != INVALID_HANDLE_VALUE )
865  {
866  do
867  {
868  ConvertFileTimeToWx( &lastModDate, findData.ftLastWriteTime );
869  timestamp += lastModDate.GetValue().GetValue();
870  }
871  while ( FindNextFile( fileHandle, &findData ) != 0 );
872  }
873 
874  FindClose( fileHandle );
875 #else
876  // POSIX version.
877  // Save time by not converting between encodings -- do everything on the file-system side.
878  std::string filespec( aFilespec.fn_str() );
879  std::string dir_path( aDirPath.fn_str() );
880 
881  DIR* dir = opendir( dir_path.c_str() );
882 
883  if( dir )
884  {
885  for( dirent* dir_entry = readdir( dir ); dir_entry; dir_entry = readdir( dir ) )
886  {
887  if( !matchWild( filespec.c_str(), dir_entry->d_name, true ) )
888  continue;
889 
890  std::string entry_path = dir_path + '/' + dir_entry->d_name;
891  struct stat entry_stat;
892 
893  wxCRT_Lstat( entry_path.c_str(), &entry_stat );
894 
895  // Timestamp the source file, not the symlink
896  if( S_ISLNK( entry_stat.st_mode ) ) // wxFILE_EXISTS_SYMLINK
897  {
898  char buffer[ PATH_MAX + 1 ];
899  ssize_t pathLen = readlink( entry_path.c_str(), buffer, PATH_MAX );
900 
901  if( pathLen > 0 )
902  {
903  buffer[ pathLen ] = '\0';
904  entry_path = dir_path + buffer;
905 
906  wxCRT_Lstat( entry_path.c_str(), &entry_stat );
907  }
908  }
909 
910  if( S_ISREG( entry_stat.st_mode ) ) // wxFileExists()
911  timestamp += entry_stat.st_mtime * 1000;
912  }
913 
914  closedir( dir );
915  }
916 #endif
917 
918  return timestamp;
919 }
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:640
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
#define TO_STR(x)
Definition: macros.h:56
const wxString ResolveUriByEnvVars(const wxString &aUri)
Replace any environment variables in file-path uris (leaving network-path URIs alone).
Definition: common.cpp:467
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:653
wxString GetFullName() const
Definition: common.cpp:660
wxString m_path
Definition: common.h:410
size_t operator()(const wxPoint &k) const
Definition: common.cpp:591
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:647
const string & str
Definition: json11.cpp:648
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:409
static std::atomic< unsigned int > m_c_count
Definition: common.h:162
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:687
timestamp_t GetNewTimeStamp()
Definition: common.cpp:217
void resolve()
Definition: common.cpp:679
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:454
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:481
Bracket
Definition: common.cpp:310
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:846
LOCALE_IO()
Definition: common.cpp:83
wxString GetKicadConfigPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: common.cpp:267
#define _(s)
bool matchWild(const char *pat, const char *text, bool dot_special)
A copy of wxMatchWild(), which wxWidgets attributes to Douglas A.
Definition: common.cpp:704
wxString GetPath() const
Definition: common.cpp:666
wxString GetFullPath() const
Definition: common.cpp:672
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:101
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
wxString m_fullName
Definition: common.h:411
COLOR4D g_GhostColor
Global variables definitions.
Definition: common.cpp:58
std::string m_user_locale
Definition: common.h:166
std::ostream & operator<<(std::ostream &out, const wxSize &size)
Helper function to print the given wxSize to a stream.
Definition: common.cpp:611
size_t operator()(const wxString &s) const
Definition: common.cpp:584
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:114
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39