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-2020 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 1992-2020 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 <fctsys.h>
27 #include <eda_base_frame.h>
28 #include <project.h>
29 #include <common.h>
30 #include <reporter.h>
31 #include <macros.h>
32 #include <mutex>
33 #include <wx/process.h>
34 #include <wx/config.h>
35 #include <wx/utils.h>
36 #include <wx/stdpaths.h>
37 #include <wx/url.h>
38 #include <boost/uuid/uuid_generators.hpp>
39 #include <boost/uuid/uuid_io.hpp>
40 #include <boost/functional/hash.hpp>
41 
42 using KIGFX::COLOR4D;
43 
44 
45 // Create only once, as seeding is *very* expensive
46 static boost::uuids::random_generator randomGenerator;
47 
48 // These don't have the same performance penalty, but might as well be consistent
49 static boost::uuids::string_generator stringGenerator;
50 static boost::uuids::nil_generator nilGenerator;
51 
52 // Global nil reference
53 KIID niluuid( 0 );
54 
55 
56 // For static initialization
58 {
59  static KIID nil( 0 );
60  return nil;
61 }
62 
63 
65  m_uuid( randomGenerator() ),
66  m_cached_timestamp( 0 )
67 {
68 }
69 
70 
71 KIID::KIID( int null ) :
72  m_uuid( nilGenerator() ),
73  m_cached_timestamp( 0 )
74 {
75  wxASSERT( null == 0 );
76 }
77 
78 
79 KIID::KIID( const wxString& aString ) :
80  m_uuid(),
81  m_cached_timestamp( 0 )
82 {
83  if( aString.length() == 8 )
84  {
85  // A legacy-timestamp-based UUID has only the last 4 octets filled in.
86  // Convert them individually to avoid stepping in the little-endian/big-endian
87  // doo-doo.
88  for( int i = 0; i < 4; ++i )
89  {
90  wxString octet = aString.substr( i * 2, 2 );
91  m_uuid.data[ i + 12 ] = strtol( octet.data(), NULL, 16 );
92  }
93 
94  m_cached_timestamp = strtol( aString.c_str(), NULL, 16 );
95  }
96  else
97  {
98  try
99  {
100  m_uuid = stringGenerator( aString.wc_str() );
101 
102  if( IsLegacyTimestamp() )
103  m_cached_timestamp = strtol( aString.substr( 28 ).c_str(), NULL, 16 );
104  }
105  catch( ... )
106  {
107  // Failed to parse string representation; best we can do is assign a new
108  // random one.
110  }
111  }
112 }
113 
114 
115 KIID::KIID( timestamp_t aTimestamp )
116 {
117  m_cached_timestamp = aTimestamp;
118 
119  // A legacy-timestamp-based UUID has only the last 4 octets filled in.
120  // Convert them individually to avoid stepping in the little-endian/big-endian
121  // doo-doo.
122  wxString str = AsLegacyTimestampString();
123 
124  for( int i = 0; i < 4; ++i )
125  {
126  wxString octet = str.substr( i * 2, 2 );
127  m_uuid.data[ i + 12 ] = strtol( octet.data(), NULL, 16 );
128  }
129 }
130 
131 
133 {
134  return !m_uuid.data[8] && !m_uuid.data[9] && !m_uuid.data[10] && !m_uuid.data[11];
135 }
136 
137 
139 {
140  return m_cached_timestamp;
141 }
142 
143 
144 size_t KIID::Hash() const
145 {
146  size_t hash = 0;
147 
148  // Note: this is NOT little-endian/big-endian safe, but as long as it's just used
149  // at runtime it won't matter.
150 
151  for( int i = 0; i < 4; ++i )
152  boost::hash_combine( hash, reinterpret_cast<const uint32_t*>( m_uuid.data )[i] );
153 
154  return hash;
155 }
156 
157 
158 void KIID::Clone( const KIID& aUUID )
159 {
160  m_uuid = aUUID.m_uuid;
162 }
163 
164 
165 wxString KIID::AsString() const
166 {
167  return boost::uuids::to_string( m_uuid );
168 }
169 
170 
172 {
173  return wxString::Format( "%8.8lX", (unsigned long) AsLegacyTimestamp() );
174 }
175 
176 
178 {
179  if( !IsLegacyTimestamp() )
180  return;
181 
182  m_cached_timestamp = 0;
184 }
185 
186 
195 // When reading/writing files, we need to swtich to setlocale( LC_NUMERIC, "C" ).
196 // Works fine to read/write files with floating point numbers.
197 // We can call setlocale( LC_NUMERIC, "C" ) of wxLocale( "C", "C", "C", false )
198 // wxWidgets discourage a direct call to setlocale
199 // However, for us, calling wxLocale( "C", "C", "C", false ) has a unwanted effect:
200 // The I18N translations are no longer active, because the English dixtionary is selected.
201 // To read files, this is not a major issues, but the resul can differ
202 // from using setlocale(xx, "C").
203 // Previouly, we called setlocale( LC_NUMERIC, "C" )
204 // The old code will be removed when calling wxLocale( "C", "C", "C", false )
205 // is fully tested, and all issues fixed
206 #define USE_WXLOCALE 1 /* 0 to call setlocale, 1 to call wxLocale */
207 
208 // On Windows, when using setlocale, a wx alert is generated
209 // in some cases (reading a bitmap for instance)
210 // So we disable alerts during the time a file is read or written
211 #if !USE_WXLOCALE
212 #if defined( _WIN32 ) && defined( DEBUG )
213 // a wxAssertHandler_t function to filter wxWidgets alert messages when reading/writing a file
214 // when switching the locale to LC_NUMERIC, "C"
215 // It is used in class LOCALE_IO to hide a useless (in kicad) wxWidgets alert message
216 void KiAssertFilter( const wxString &file, int line,
217  const wxString &func, const wxString &cond,
218  const wxString &msg)
219 {
220  if( !msg.Contains( "Decimal separator mismatch" ) )
221  wxTheApp->OnAssertFailure( file.c_str(), line, func.c_str(), cond.c_str(), msg.c_str() );
222 }
223 #endif
224 #endif
225 
226 std::atomic<unsigned int> LOCALE_IO::m_c_count( 0 );
227 LOCALE_IO::LOCALE_IO() : m_wxLocale( nullptr )
228 {
229  // use thread safe, atomic operation
230  if( m_c_count++ == 0 )
231  {
232 #if USE_WXLOCALE
233  m_wxLocale = new wxLocale( "C", "C", "C", false );
234 #else
235  // Store the user locale name, to restore this locale later, in dtor
236  m_user_locale = setlocale( LC_NUMERIC, nullptr );
237 #if defined( _WIN32 ) && defined( DEBUG )
238  // Disable wxWidgets alerts
239  wxSetAssertHandler( KiAssertFilter );
240 #endif
241  // Switch the locale to C locale, to read/write files with fp numbers
242  setlocale( LC_NUMERIC, "C" );
243 #endif
244  }
245 }
246 
247 
249 {
250  // use thread safe, atomic operation
251  if( --m_c_count == 0 )
252  {
253  // revert to the user locale
254 #if USE_WXLOCALE
255  delete m_wxLocale; // Deleting m_wxLocale restored previous locale
256  m_wxLocale = nullptr;
257 #else
258  setlocale( LC_NUMERIC, m_user_locale.c_str() );
259 #if defined( _WIN32 ) && defined( DEBUG )
260  // Enable wxWidgets alerts
261  wxSetDefaultAssertHandler();
262 #endif
263 #endif
264  }
265 }
266 
267 
268 wxSize GetTextSize( const wxString& aSingleLine, wxWindow* aWindow )
269 {
270  wxCoord width;
271  wxCoord height;
272 
273  {
274  wxClientDC dc( aWindow );
275  dc.SetFont( aWindow->GetFont() );
276  dc.GetTextExtent( aSingleLine, &width, &height );
277  }
278 
279  return wxSize( width, height );
280 }
281 
282 
283 bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString )
284 {
285  wxWindow* window = aCtrl->GetParent();
286 
287  if( !window )
288  window = aCtrl;
289 
290  wxString ctrlText;
291 
292  if( !aString )
293  {
294  ctrlText = aCtrl->GetValue();
295  aString = &ctrlText;
296  }
297 
298  wxSize textz = GetTextSize( *aString, window );
299  wxSize ctrlz = aCtrl->GetSize();
300 
301  if( ctrlz.GetWidth() < textz.GetWidth() + 10 )
302  {
303  ctrlz.SetWidth( textz.GetWidth() + 10 );
304  aCtrl->SetSizeHints( ctrlz );
305  return true;
306  }
307 
308  return false;
309 }
310 
311 
312 void SelectReferenceNumber( wxTextEntry* aTextEntry )
313 {
314  wxString ref = aTextEntry->GetValue();
315 
316  if( ref.find_first_of( '?' ) != ref.npos )
317  {
318  aTextEntry->SetSelection( ref.find_first_of( '?' ), ref.find_last_of( '?' ) + 1 );
319  }
320  else
321  {
322  wxString num = ref;
323 
324  while( !num.IsEmpty() && ( !isdigit( num.Last() ) || !isdigit( num.GetChar( 0 ) ) ) )
325  {
326  if( !isdigit( num.Last() ) )
327  num.RemoveLast();
328 
329  if( !isdigit( num.GetChar ( 0 ) ) )
330  num = num.Right( num.Length() - 1);
331  }
332 
333  aTextEntry->SetSelection( ref.Find( num ), ref.Find( num ) + num.Length() );
334 
335  if( num.IsEmpty() )
336  aTextEntry->SetSelection( -1, -1 );
337  }
338 }
339 
340 
341 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter )
342 {
343  wxString tmp;
344 
345  for( unsigned ii = 0; ii < aText.Length(); ii++ )
346  {
347  if( aText[ii] == aSplitter )
348  {
349  aStrings.Add( tmp );
350  tmp.Clear();
351  }
352 
353  else
354  tmp << aText[ii];
355  }
356 
357  if( !tmp.IsEmpty() )
358  {
359  aStrings.Add( tmp );
360  }
361 }
362 
363 
364 int ProcessExecute( const wxString& aCommandLine, int aFlags, wxProcess *callback )
365 {
366  return (int) wxExecute( aCommandLine, aFlags, callback );
367 }
368 
369 
371 {
375 #ifdef __WINDOWS__
376  Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
377 #endif
379 };
380 
381 
382 wxString ExpandTextVars( const wxString& aSource,
383  const std::function<bool( wxString* )>* aLocalResolver,
384  const PROJECT* aProject )
385 {
386  wxString newbuf;
387  size_t sourceLen = aSource.length();
388 
389  newbuf.Alloc( sourceLen ); // best guess (improves performance)
390 
391  for( size_t i = 0; i < sourceLen; ++i )
392  {
393  if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
394  {
395  wxString token;
396 
397  for( i = i + 2; i < sourceLen; ++i )
398  {
399  if( aSource[i] == '}' )
400  break;
401  else
402  token.append( aSource[i] );
403  }
404 
405  if( token.IsEmpty() )
406  continue;
407 
408  if( aLocalResolver && (*aLocalResolver)( &token ) )
409  {
410  newbuf.append( token );
411  }
412  else if( aProject && aProject->TextVarResolver( &token ) )
413  {
414  newbuf.append( token );
415  }
416  else
417  {
418  // Token not resolved: leave the reference unchanged
419  newbuf.append( "${" + token + "}" );
420  }
421  }
422  else
423  {
424  newbuf.append( aSource[i] );
425  }
426  }
427 
428  return newbuf;
429 }
430 
431 
432 //
433 // Stolen from wxExpandEnvVars and then heavily optimized
434 //
435 wxString KIwxExpandEnvVars( const wxString& str, const PROJECT* aProject )
436 {
437  size_t strlen = str.length();
438 
439  wxString strResult;
440  strResult.Alloc( strlen ); // best guess (improves performance)
441 
442  for( size_t n = 0; n < strlen; n++ )
443  {
444  wxUniChar str_n = str[n];
445 
446  switch( str_n.GetValue() )
447  {
448 #ifdef __WINDOWS__
449  case wxT( '%' ):
450 #endif // __WINDOWS__
451  case wxT( '$' ):
452  {
453  Bracket bracket;
454 #ifdef __WINDOWS__
455  if( str_n == wxT( '%' ) )
456  bracket = Bracket_Windows;
457  else
458 #endif // __WINDOWS__
459  if( n == strlen - 1 )
460  {
461  bracket = Bracket_None;
462  }
463  else
464  {
465  switch( str[n + 1].GetValue() )
466  {
467  case wxT( '(' ):
468  bracket = Bracket_Normal;
469  str_n = str[++n]; // skip the bracket
470  break;
471 
472  case wxT( '{' ):
473  bracket = Bracket_Curly;
474  str_n = str[++n]; // skip the bracket
475  break;
476 
477  default:
478  bracket = Bracket_None;
479  }
480  }
481 
482  size_t m = n + 1;
483  wxUniChar str_m = str[m];
484 
485  while( m < strlen && ( wxIsalnum( str_m ) || str_m == wxT( '_' ) || str_m == wxT( ':' ) ) )
486  str_m = str[++m];
487 
488  wxString strVarName( str.c_str() + n + 1, m - n - 1 );
489 
490  // NB: use wxGetEnv instead of wxGetenv as otherwise variables
491  // set through wxSetEnv may not be read correctly!
492  bool expanded = false;
493  wxString tmp = strVarName;
494 
495  if( aProject && aProject->TextVarResolver( &tmp ) )
496  {
497  strResult += tmp;
498  expanded = true;
499  }
500  else if( wxGetEnv( strVarName, &tmp ) )
501  {
502  strResult += tmp;
503  expanded = true;
504  }
505  else
506  {
507  // variable doesn't exist => don't change anything
508 #ifdef __WINDOWS__
509  if ( bracket != Bracket_Windows )
510 #endif
511  if ( bracket != Bracket_None )
512  strResult << str[n - 1];
513 
514  strResult << str_n << strVarName;
515  }
516 
517  // check the closing bracket
518  if( bracket != Bracket_None )
519  {
520  if( m == strlen || str_m != (wxChar)bracket )
521  {
522  // under MSW it's common to have '%' characters in the registry
523  // and it's annoying to have warnings about them each time, so
524  // ignore them silently if they are not used for env vars
525  //
526  // under Unix, OTOH, this warning could be useful for the user to
527  // understand why isn't the variable expanded as intended
528 #ifndef __WINDOWS__
529  wxLogWarning( _( "Environment variables expansion failed: missing '%c' "
530  "at position %u in '%s'." ),
531  (char)bracket, (unsigned int) (m + 1), str.c_str() );
532 #endif // __WINDOWS__
533  }
534  else
535  {
536  // skip closing bracket unless the variables wasn't expanded
537  if( !expanded )
538  strResult << (wxChar)bracket;
539 
540  m++;
541  }
542  }
543 
544  n = m - 1; // skip variable name
545  str_n = str[n];
546  }
547  break;
548 
549  case wxT( '\\' ):
550  // backslash can be used to suppress special meaning of % and $
551  if( n != strlen - 1 && (str[n + 1] == wxT( '%' ) || str[n + 1] == wxT( '$' )) )
552  {
553  str_n = str[++n];
554  strResult += str_n;
555 
556  break;
557  }
559 
560  default:
561  strResult += str_n;
562  }
563  }
564 
565 #ifndef __WINDOWS__
566  if( strResult.StartsWith( "~" ) )
567  strResult.Replace( "~", wxGetHomeDir(), false );
568 #endif // __WINDOWS__
569 
570  return strResult;
571 }
572 
573 
574 const wxString ExpandEnvVarSubstitutions( const wxString& aString, PROJECT* aProject )
575 {
576  // wxGetenv( wchar_t* ) is not re-entrant on linux.
577  // Put a lock on multithreaded use of wxGetenv( wchar_t* ), called from wxEpandEnvVars(),
578  static std::mutex getenv_mutex;
579 
580  std::lock_guard<std::mutex> lock( getenv_mutex );
581 
582  // We reserve the right to do this another way, by providing our own member function.
583  return KIwxExpandEnvVars( aString, aProject );
584 }
585 
586 
587 const wxString ResolveUriByEnvVars( const wxString& aUri, PROJECT* aProject )
588 {
589  wxString uri = ExpandTextVars( aUri, nullptr, aProject );
590 
591  // URL-like URI: return as is.
592  wxURL url( uri );
593 
594  if( url.GetError() == wxURL_NOERR )
595  return uri;
596 
597  // Otherwise, the path points to a local file. Resolve environment variables if any.
598  return ExpandEnvVarSubstitutions( aUri, aProject );
599 }
600 
601 
602 bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName,
603  const wxString& aBaseFilename,
604  REPORTER* aReporter )
605 {
606  wxString msg;
607  wxString baseFilePath = wxFileName( aBaseFilename ).GetPath();
608 
609  // make aTargetFullFileName path, which is relative to aBaseFilename path (if it is not
610  // already an absolute path) absolute:
611  if( !aTargetFullFileName->MakeAbsolute( baseFilePath ) )
612  {
613  if( aReporter )
614  {
615  msg.Printf( _( "Cannot make path \"%s\" absolute with respect to \"%s\"." ),
616  aTargetFullFileName->GetPath(),
617  baseFilePath );
618  aReporter->Report( msg, RPT_SEVERITY_ERROR );
619  }
620 
621  return false;
622  }
623 
624  // Ensure the path of aTargetFullFileName exists, and create it if needed:
625  wxString outputPath( aTargetFullFileName->GetPath() );
626 
627  if( !wxFileName::DirExists( outputPath ) )
628  {
629  if( wxMkdir( outputPath ) )
630  {
631  if( aReporter )
632  {
633  msg.Printf( _( "Output directory \"%s\" created.\n" ), outputPath );
634  aReporter->Report( msg, RPT_SEVERITY_INFO );
635  return true;
636  }
637  }
638  else
639  {
640  if( aReporter )
641  {
642  msg.Printf( _( "Cannot create output directory \"%s\".\n" ), outputPath );
643  aReporter->Report( msg, RPT_SEVERITY_ERROR );
644  }
645 
646  return false;
647  }
648  }
649 
650  return true;
651 }
652 
653 
654 #ifdef __WXMAC__
655 wxString GetOSXKicadUserDataDir()
656 {
657  // According to wxWidgets documentation for GetUserDataDir:
658  // Mac: ~/Library/Application Support/appname
659  wxFileName udir( wxStandardPaths::Get().GetUserDataDir(), wxEmptyString );
660 
661  // Since appname is different if started via launcher or standalone binary
662  // map all to "kicad" here
663  udir.RemoveLastDir();
664  udir.AppendDir( "kicad" );
665 
666  return udir.GetPath();
667 }
668 
669 
670 wxString GetOSXKicadMachineDataDir()
671 {
672  return wxT( "/Library/Application Support/kicad" );
673 }
674 
675 
676 wxString GetOSXKicadDataDir()
677 {
678  // According to wxWidgets documentation for GetDataDir:
679  // Mac: appname.app/Contents/SharedSupport bundle subdirectory
680  wxFileName ddir( wxStandardPaths::Get().GetDataDir(), wxEmptyString );
681 
682  // This must be mapped to main bundle for everything but kicad.app
683  const wxArrayString dirs = ddir.GetDirs();
684  if( dirs[dirs.GetCount() - 3] != wxT( "kicad.app" ) )
685  {
686  // Bundle structure resp. current path is
687  // kicad.app/Contents/Applications/<standalone>.app/Contents/SharedSupport
688  // and will be mapped to
689  // kicad.app/Contents/SharedSupprt
690  ddir.RemoveLastDir();
691  ddir.RemoveLastDir();
692  ddir.RemoveLastDir();
693  ddir.RemoveLastDir();
694  ddir.AppendDir( wxT( "SharedSupport" ) );
695  }
696 
697  return ddir.GetPath();
698 }
699 #endif
700 
701 
702 // add this only if it is not in wxWidgets (for instance before 3.1.0)
703 #ifdef USE_KICAD_WXSTRING_HASH
704 size_t std::hash<wxString>::operator()( const wxString& s ) const
705 {
706  return std::hash<std::wstring>{}( s.ToStdWstring() );
707 }
708 #endif
709 
710 #ifdef USE_KICAD_WXPOINT_LESS_AND_HASH
711 size_t std::hash<wxPoint>::operator() ( const wxPoint& k ) const
712 {
713  auto xhash = std::hash<int>()( k.x );
714 
715  // 0x9e3779b9 is 2^33 / ( 1 + sqrt(5) )
716  // Adding this value ensures that consecutive bits of y will not be close to each other
717  // decreasing the likelihood of hash collision in similar values of x and y
718  return xhash ^ ( std::hash<int>()( k.y ) + 0x9e3779b9 + ( xhash << 6 ) + ( xhash >> 2 ) );
719 }
720 
721 bool std::less<wxPoint>::operator()( const wxPoint& aA, const wxPoint& aB ) const
722 {
723  if( aA.x == aB.x )
724  return aA.y < aB.y;
725 
726  return aA.x < aB.x;
727 }
728 #endif
729 
730 
731 std::ostream& operator<<( std::ostream& out, const wxSize& size )
732 {
733  out << " width=\"" << size.GetWidth() << "\" height=\"" << size.GetHeight() << "\"";
734  return out;
735 }
736 
737 
738 std::ostream& operator<<( std::ostream& out, const wxPoint& pt )
739 {
740  out << " x=\"" << pt.x << "\" y=\"" << pt.y << "\"";
741  return out;
742 }
743 
744 
760 WX_FILENAME::WX_FILENAME( const wxString& aPath, const wxString& aFilename ) :
761  m_fn( aPath, aFilename ),
762  m_path( aPath ),
763  m_fullName( aFilename )
764 { }
765 
766 
767 void WX_FILENAME::SetFullName( const wxString& aFileNameAndExtension )
768 {
769  m_fullName = aFileNameAndExtension;
770 }
771 
772 
773 wxString WX_FILENAME::GetName() const
774 {
775  size_t dot = m_fullName.find_last_of( wxT( '.' ) );
776  return m_fullName.substr( 0, dot );
777 }
778 
779 
780 wxString WX_FILENAME::GetFullName() const
781 {
782  return m_fullName;
783 }
784 
785 
786 wxString WX_FILENAME::GetPath() const
787 {
788  return m_path;
789 }
790 
791 
792 wxString WX_FILENAME::GetFullPath() const
793 {
794  return m_path + wxT( '/' ) + m_fullName;
795 }
796 
797 
798 // Write locally-cached values to the wxFileName. MUST be called before using m_fn.
800 {
801  size_t dot = m_fullName.find_last_of( wxT( '.' ) );
802  m_fn.SetName( m_fullName.substr( 0, dot ) );
803  m_fn.SetExt( m_fullName.substr( dot + 1 ) );
804 }
805 
806 
808 {
809  resolve();
810 
811  if( m_fn.FileExists() )
812  return m_fn.GetModificationTime().GetValue().GetValue();
813 
814  return 0;
815 }
816 
817 
824 bool matchWild( const char* pat, const char* text, bool dot_special )
825 {
826  if( !*text )
827  {
828  /* Match if both are empty. */
829  return !*pat;
830  }
831 
832  const char *m = pat,
833  *n = text,
834  *ma = NULL,
835  *na = NULL;
836  int just = 0,
837  acount = 0,
838  count = 0;
839 
840  if( dot_special && (*n == '.') )
841  {
842  /* Never match so that hidden Unix files
843  * are never found. */
844  return false;
845  }
846 
847  for(;;)
848  {
849  if( *m == '*' )
850  {
851  ma = ++m;
852  na = n;
853  just = 1;
854  acount = count;
855  }
856  else if( *m == '?' )
857  {
858  m++;
859 
860  if( !*n++ )
861  return false;
862  }
863  else
864  {
865  if( *m == '\\' )
866  {
867  m++;
868 
869  /* Quoting "nothing" is a bad thing */
870  if( !*m )
871  return false;
872  }
873  if( !*m )
874  {
875  /*
876  * If we are out of both strings or we just
877  * saw a wildcard, then we can say we have a
878  * match
879  */
880  if( !*n )
881  return true;
882 
883  if( just )
884  return true;
885 
886  just = 0;
887  goto not_matched;
888  }
889 
890  /*
891  * We could check for *n == NULL at this point, but
892  * since it's more common to have a character there,
893  * check to see if they match first (m and n) and
894  * then if they don't match, THEN we can check for
895  * the NULL of n
896  */
897  just = 0;
898 
899  if( *m == *n )
900  {
901  m++;
902  count++;
903  n++;
904  }
905  else
906  {
907  not_matched:
908 
909  /*
910  * If there are no more characters in the
911  * string, but we still need to find another
912  * character (*m != NULL), then it will be
913  * impossible to match it
914  */
915  if( !*n )
916  return false;
917 
918  if( ma )
919  {
920  m = ma;
921  n = ++na;
922  count = acount;
923  }
924  else
925  return false;
926  }
927  }
928  }
929 }
930 
931 
936 #if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
937 
938 // Convert between wxDateTime and FILETIME which is a 64-bit value representing
939 // the number of 100-nanosecond intervals since January 1, 1601 UTC.
940 //
941 // This is the offset between FILETIME epoch and the Unix/wxDateTime Epoch.
942 static wxInt64 EPOCH_OFFSET_IN_MSEC = wxLL(11644473600000);
943 
944 
945 static void ConvertFileTimeToWx( wxDateTime *dt, const FILETIME &ft )
946 {
947  wxLongLong t( ft.dwHighDateTime, ft.dwLowDateTime );
948  t /= 10000; // Convert hundreds of nanoseconds to milliseconds.
949  t -= EPOCH_OFFSET_IN_MSEC;
950 
951  *dt = wxDateTime( t );
952 }
953 
954 #endif // wxUSE_DATETIME && __WIN32__
955 
956 
966 long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec )
967 {
968  long long timestamp = 0;
969 
970 #if defined( __WIN32__ )
971  // Win32 version.
972  // Save time by not searching for each path twice: once in wxDir.GetNext() and once in
973  // wxFileName.GetModificationTime(). Also cuts out wxWidgets' string-matching and case
974  // conversion by staying on the MSW side of things.
975  std::wstring filespec( aDirPath.t_str() );
976  filespec += '\\';
977  filespec += aFilespec.t_str();
978 
979  WIN32_FIND_DATA findData;
980  wxDateTime lastModDate;
981 
982  HANDLE fileHandle = ::FindFirstFile( filespec.data(), &findData );
983 
984  if( fileHandle != INVALID_HANDLE_VALUE )
985  {
986  do
987  {
988  ConvertFileTimeToWx( &lastModDate, findData.ftLastWriteTime );
989  timestamp += lastModDate.GetValue().GetValue();
990  }
991  while ( FindNextFile( fileHandle, &findData ) != 0 );
992  }
993 
994  FindClose( fileHandle );
995 #else
996  // POSIX version.
997  // Save time by not converting between encodings -- do everything on the file-system side.
998  std::string filespec( aFilespec.fn_str() );
999  std::string dir_path( aDirPath.fn_str() );
1000 
1001  DIR* dir = opendir( dir_path.c_str() );
1002 
1003  if( dir )
1004  {
1005  for( dirent* dir_entry = readdir( dir ); dir_entry; dir_entry = readdir( dir ) )
1006  {
1007  if( !matchWild( filespec.c_str(), dir_entry->d_name, true ) )
1008  continue;
1009 
1010  std::string entry_path = dir_path + '/' + dir_entry->d_name;
1011  struct stat entry_stat;
1012 
1013  if( wxCRT_Lstat( entry_path.c_str(), &entry_stat ) == 0 )
1014  {
1015  // Timestamp the source file, not the symlink
1016  if( S_ISLNK( entry_stat.st_mode ) ) // wxFILE_EXISTS_SYMLINK
1017  {
1018  char buffer[ PATH_MAX + 1 ];
1019  ssize_t pathLen = readlink( entry_path.c_str(), buffer, PATH_MAX );
1020 
1021  if( pathLen > 0 )
1022  {
1023  struct stat linked_stat;
1024  buffer[ pathLen ] = '\0';
1025  entry_path = dir_path + buffer;
1026 
1027  if( wxCRT_Lstat( entry_path.c_str(), &linked_stat ) == 0 )
1028  {
1029  entry_stat = linked_stat;
1030  }
1031  else
1032  {
1033  // if we couldn't lstat the linked file we'll have to just use
1034  // the symbolic link info
1035  }
1036  }
1037  }
1038 
1039  if( S_ISREG( entry_stat.st_mode ) ) // wxFileExists()
1040  timestamp += entry_stat.st_mtime * 1000;
1041  }
1042  else
1043  {
1044  // if we couldn't lstat the file itself all we can do is use the name
1045  timestamp += (signed) std::hash<std::string>{}( std::string( dir_entry->d_name ) );
1046  }
1047  }
1048 
1049  closedir( dir );
1050  }
1051 #endif
1052 
1053  return timestamp;
1054 }
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: common.cpp:341
WX_FILENAME(const wxString &aPath, const wxString &aFilename)
Performance enhancements to file and directory operations.
Definition: common.cpp:760
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:364
#define KI_FALLTHROUGH
~LOCALE_IO()
Definition: common.cpp:248
PROJECT holds project specific data.
Definition: project.h:61
KIID()
Definition: common.cpp:64
KIID & NilUuid()
Definition: common.cpp:57
wxLocale * m_wxLocale
Definition: common.h:229
wxString GetName() const
Definition: common.cpp:773
static boost::uuids::string_generator stringGenerator
Definition: common.cpp:49
wxString AsString() const
Definition: common.cpp:165
wxString GetFullName() const
Definition: common.cpp:780
wxString m_path
Definition: common.h:434
boost::uuids::uuid m_uuid
Definition: common.h:109
size_t operator()(const wxPoint &k) const
Definition: common.cpp:711
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:574
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
void SetFullName(const wxString &aFileNameAndExtension)
Definition: common.cpp:767
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
static boost::uuids::nil_generator nilGenerator
Definition: common.cpp:50
static void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition: hash_eda.h:64
bool IsLegacyTimestamp() const
Definition: common.cpp:132
timestamp_t m_cached_timestamp
Definition: common.h:111
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:268
wxFileName m_fn
Definition: common.h:433
static std::atomic< unsigned int > m_c_count
Definition: common.h:224
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: common.cpp:312
Definition: common.h:68
long long GetTimestamp()
Definition: common.cpp:807
void resolve()
Definition: common.cpp:799
size_t Hash() const
Definition: common.cpp:144
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:283
#define NULL
timestamp_t AsLegacyTimestamp() const
Definition: common.cpp:138
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:602
Bracket
Definition: common.cpp:370
bool operator()(const wxPoint &aA, const wxPoint &aB) const
Definition: common.cpp:721
KIID niluuid(0)
static boost::uuids::random_generator randomGenerator
Definition: common.cpp:46
const wxString ResolveUriByEnvVars(const wxString &aUri, PROJECT *aProject)
Replace any environment and/or text variables in file-path uris (leaving network-path URIs alone).
Definition: common.cpp:587
Base window classes and related definitions.
VTBL_ENTRY bool TextVarResolver(wxString *aToken) const
Definition: project.cpp:67
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:966
LOCALE_IO()
Definition: common.cpp:227
void ConvertTimestampToUuid()
Change an existing time stamp based UUID into a true UUID.
Definition: common.cpp:177
bool matchWild(const char *pat, const char *text, bool dot_special)
A copy of wxMatchWild(), which wxWidgets attributes to Douglas A.
Definition: common.cpp:824
wxString GetPath() const
Definition: common.cpp:786
void Clone(const KIID &aUUID)
Definition: common.cpp:158
wxString GetFullPath() const
Definition: common.cpp:792
wxString AsLegacyTimestampString() const
Definition: common.cpp:171
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define _(s)
Definition: 3d_actions.cpp:33
The common library.
wxString ExpandTextVars(const wxString &aSource, const std::function< bool(wxString *)> *aLocalResolver, const PROJECT *aProject)
Expand '${var-name}' templates in text.
Definition: common.cpp:382
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:56
wxString m_fullName
Definition: common.h:435
wxString KIwxExpandEnvVars(const wxString &str, const PROJECT *aProject)
Definition: common.cpp:435
std::string m_user_locale
Definition: common.h:228
std::ostream & operator<<(std::ostream &out, const wxSize &size)
Helper function to print the given wxSize to a stream.
Definition: common.cpp:731
size_t operator()(const wxString &s) const
Definition: common.cpp:704
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99