KiCad PCB EDA Suite
common.cpp File Reference
#include <fctsys.h>
#include <eda_base_frame.h>
#include <project.h>
#include <common.h>
#include <reporter.h>
#include <macros.h>
#include <mutex>
#include <wx/process.h>
#include <wx/config.h>
#include <wx/utils.h>
#include <wx/stdpaths.h>
#include <wx/url.h>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/functional/hash.hpp>

Go to the source code of this file.

Macros

#define USE_WXLOCALE   1 /* 0 to call setlocale, 1 to call wxLocale */
 Global variables definitions. More...
 

Enumerations

enum  Bracket { Bracket_None, Bracket_Normal = ')', Bracket_Curly = '}', Bracket_Max }
 

Functions

KIIDNilUuid ()
 
wxSize GetTextSize (const wxString &aSingleLine, wxWindow *aWindow)
 Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currently set in that window. More...
 
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 it. More...
 
void SelectReferenceNumber (wxTextEntry *aTextEntry)
 Select the number (or "?") in a reference for ease of editing. More...
 
void wxStringSplit (const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
 Split aString to a string list separated at aSplitter. More...
 
int ProcessExecute (const wxString &aCommandLine, int aFlags, wxProcess *callback)
 Run a command in a child process. More...
 
wxString ExpandTextVars (const wxString &aSource, const std::function< bool(wxString *)> *aLocalResolver, const PROJECT *aProject)
 Expand '${var-name}' templates in text. More...
 
wxString KIwxExpandEnvVars (const wxString &str, const PROJECT *aProject)
 
const wxString ExpandEnvVarSubstitutions (const wxString &aString, PROJECT *aProject)
 Replace any environment variable & text variable references with their values. More...
 
const wxString ResolveUriByEnvVars (const wxString &aUri, PROJECT *aProject)
 Replace any environment and/or text variables in file-path uris (leaving network-path URIs alone). More...
 
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. More...
 
std::ostream & operator<< (std::ostream &out, const wxSize &size)
 Helper function to print the given wxSize to a stream. More...
 
std::ostream & operator<< (std::ostream &out, const wxPoint &pt)
 Helper function to print the given wxPoint to a stream. More...
 
bool matchWild (const char *pat, const char *text, bool dot_special)
 A copy of wxMatchWild(), which wxWidgets attributes to Douglas A. More...
 
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/filename.cpp. More...
 

Variables

static boost::uuids::random_generator randomGenerator
 
static boost::uuids::string_generator stringGenerator
 
static boost::uuids::nil_generator nilGenerator
 
KIID niluuid (0)
 

Macro Definition Documentation

◆ USE_WXLOCALE

#define USE_WXLOCALE   1 /* 0 to call setlocale, 1 to call wxLocale */

Global variables definitions.

TODO: All of these variables should be moved into the class were they are defined and used. Most of them probably belong in the application class.

Definition at line 206 of file common.cpp.

Enumeration Type Documentation

◆ Bracket

enum Bracket
Enumerator
Bracket_None 
Bracket_Normal 
Bracket_Curly 
Bracket_Max 

Definition at line 370 of file common.cpp.

371 {
372  Bracket_None,
373  Bracket_Normal = ')',
374  Bracket_Curly = '}',
375 #ifdef __WINDOWS__
376  Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
377 #endif
379 };

Function Documentation

◆ EnsureFileDirectoryExists()

bool EnsureFileDirectoryExists ( wxFileName *  aTargetFullFileName,
const wxString &  aBaseFilename,
REPORTER aReporter = NULL 
)

Make aTargetFullFileName absolute and create the path of this file if it doesn't yet exist.

Parameters
aTargetFullFileNamethe wxFileName containing the full path and file name to modify. The path may be absolute or relative to aBaseFilename .
aBaseFilenamea full filename. Only its path is used to set the aTargetFullFileName path.
aReportera point to a REPORTER object use to show messages (can be NULL)
Returns
true if aOutputDir already exists or was successfully created.

Definition at line 602 of file common.cpp.

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 }
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.
#define _(s)
Definition: 3d_actions.cpp:33

References _, REPORTER::Report(), RPT_SEVERITY_ERROR, and RPT_SEVERITY_INFO.

Referenced by DIALOG_GEN_FOOTPRINT_POSITION::CreateAsciiFiles(), DIALOG_GEN_FOOTPRINT_POSITION::CreateGerberFiles(), DIALOG_PLOT_SCHEMATIC::createPlotFileName(), DIALOG_EXPORT_SVG::ExportSVGFile(), DIALOG_GENDRILL::GenDrillAndMapFiles(), PLOT_CONTROLLER::OpenPlotfile(), and DIALOG_PLOT::Plot().

◆ EnsureTextCtrlWidth()

bool EnsureTextCtrlWidth ( wxTextCtrl *  aCtrl,
const wxString *  aString = NULL 
)

Set the minimum pixel width on a text control in order to make a text string be fully visible within it.

The current font within the text control is considered. The text can come either from the control or be given as an argument. If the text control is larger than needed, then nothing is done.

Parameters
aCtrlthe text control to potentially make wider.
aStringthe text that is used in sizing the control's pixel width. If NULL, then the text already within the control is used.
Returns
bool - true if the aCtrl had its size changed, else false.

Definition at line 283 of file common.cpp.

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 }
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

References GetTextSize().

Referenced by GERBVIEW_FRAME::UpdateTitleAndInfo().

◆ ExpandEnvVarSubstitutions()

const wxString ExpandEnvVarSubstitutions ( const wxString &  aString,
PROJECT aProject 
)

Replace any environment variable & text variable references with their values.

Parameters
aString= a string containing (perhaps) references to env var
Returns
a string where env var are replaced by their value

Definition at line 574 of file common.cpp.

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 }
wxString KIwxExpandEnvVars(const wxString &str, const PROJECT *aProject)
Definition: common.cpp:435

References KIwxExpandEnvVars().

Referenced by FILENAME_RESOLVER::addPath(), GITHUB_PLUGIN::cacheLib(), FILENAME_RESOLVER::checkEnvVarPath(), S3D_PLUGIN_MANAGER::checkPluginName(), S3D_PLUGIN_MANAGER::checkPluginPath(), DIALOG_GEN_FOOTPRINT_POSITION::CreateGerberFiles(), FILENAME_RESOLVER::createPathList(), DIALOG_PLOT_SCHEMATIC::createPlotFileName(), DIALOG_EXPORT_SVG::ExportSVGFile(), DIALOG_GENDRILL::GenDrillAndMapFiles(), LIB_TABLE_ROW::GetFullURI(), WS_DATA_MODEL::MakeFullFileName(), TEXT_BUTTON_FILE_BROWSER::OnButtonClick(), DIALOG_GENDRILL::OnGenReportFile(), DIALOG_EXPORT_SVG::OnOutputDirectoryBrowseClicked(), DIALOG_PLOT::OnOutputDirectoryBrowseClicked(), DIALOG_PLOT_SCHEMATIC::OnOutputDirectoryBrowseClicked(), DIALOG_GEN_FOOTPRINT_POSITION::OnOutputDirectoryBrowseClicked(), DIALOG_GENDRILL::OnOutputDirectoryBrowseClicked(), DIALOG_PLOT::Plot(), FILENAME_RESOLVER::ResolvePath(), ResolveUriByEnvVars(), FILENAME_RESOLVER::Set3DConfigDir(), S3D_CACHE::Set3DConfigDir(), FILENAME_RESOLVER::SetProject(), FILENAME_RESOLVER::ShortenPath(), and ERC_TESTER::TestTextVars().

◆ ExpandTextVars()

wxString ExpandTextVars ( const wxString &  aSource,
const std::function< bool(wxString *)> *  aLocalResolver,
const PROJECT aProject 
)

Expand '${var-name}' templates in text.

The LocalResolver is given first crack at it, after which the PROJECT's resolver is called.

Definition at line 382 of file common.cpp.

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 }
VTBL_ENTRY bool TextVarResolver(wxString *aToken) const
Definition: project.cpp:67

References PROJECT::TextVarResolver().

Referenced by WS_DRAW_ITEM_LIST::BuildFullText(), TEXTE_PCB::GetShownText(), SCH_FIELD::GetShownText(), SCH_TEXT::GetShownText(), TEXTE_MODULE::GetShownText(), and ResolveUriByEnvVars().

◆ GetTextSize()

wxSize GetTextSize ( const wxString &  aSingleLine,
wxWindow *  aWindow 
)

Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currently set in that window.

Definition at line 268 of file common.cpp.

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 }

Referenced by DIALOG_EDIT_COMPONENTS_LIBID::AdjustGridColumns(), LIB_TREE_MODEL_ADAPTER::AttachTo(), DIALOG_FIELDS_EDITOR_GLOBAL::DIALOG_FIELDS_EDITOR_GLOBAL(), EDA_DRAW_FRAME::EDA_DRAW_FRAME(), EnsureTextCtrlWidth(), FIELDS_EDITOR_GRID_DATA_MODEL::GetDataWidth(), PL_EDITOR_FRAME::PL_EDITOR_FRAME(), DIALOG_FP_CONFLICT_ASSIGNMENT_SELECTOR::recalculateColumns(), PL_EDITOR_FRAME::ReCreateHToolbar(), ZONE_SETTINGS::SetupLayersList(), and NET_SELECTOR_COMBOPOPUP::updateSize().

◆ KIwxExpandEnvVars()

wxString KIwxExpandEnvVars ( const wxString &  str,
const PROJECT aProject 
)

Definition at line 435 of file common.cpp.

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 }
#define KI_FALLTHROUGH
Bracket
Definition: common.cpp:370
VTBL_ENTRY bool TextVarResolver(wxString *aToken) const
Definition: project.cpp:67
#define _(s)
Definition: 3d_actions.cpp:33

References _, Bracket_Curly, Bracket_None, Bracket_Normal, KI_FALLTHROUGH, and PROJECT::TextVarResolver().

Referenced by ExpandEnvVarSubstitutions().

◆ matchWild()

bool matchWild ( const char *  pat,
const char *  text,
bool  dot_special 
)

A copy of wxMatchWild(), which wxWidgets attributes to Douglas A.

Lewis dalew.nosp@m.is@c.nosp@m.s.Buf.nosp@m.falo.nosp@m..EDU and ircII's reg.c.

This version is modified to skip any encoding conversions (for performance).

Definition at line 824 of file common.cpp.

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 }
#define NULL

References NULL.

Referenced by TimestampDir().

◆ NilUuid()

KIID& NilUuid ( )

Definition at line 57 of file common.cpp.

58 {
59  static KIID nil( 0 );
60  return nil;
61 }
Definition: common.h:68

◆ operator<<() [1/2]

std::ostream& operator<< ( std::ostream &  out,
const wxSize &  size 
)

Helper function to print the given wxSize to a stream.

Used for debugging functions like EDA_ITEM::Show and also in unit testing fixtures.

Definition at line 731 of file common.cpp.

732 {
733  out << " width=\"" << size.GetWidth() << "\" height=\"" << size.GetHeight() << "\"";
734  return out;
735 }

◆ operator<<() [2/2]

std::ostream& operator<< ( std::ostream &  out,
const wxPoint pt 
)

Helper function to print the given wxPoint to a stream.

Used for debugging functions like EDA_ITEM::Show and also in unit testing fixtures.

Definition at line 738 of file common.cpp.

739 {
740  out << " x=\"" << pt.x << "\" y=\"" << pt.y << "\"";
741  return out;
742 }

References wxPoint::x, and wxPoint::y.

◆ ProcessExecute()

int ProcessExecute ( const wxString &  aCommandLine,
int  aFlags = wxEXEC_ASYNC,
wxProcess *  callback = NULL 
)

Run a command in a child process.

Parameters
aCommandLineThe process and any arguments to it all in a single string.
aFlagsThe same args as allowed for wxExecute()
callbackwxProcess implementing OnTerminate to be run when the child process finishes
Returns
int - pid of process, 0 in case of error (like return values of wxExecute())

Definition at line 364 of file common.cpp.

365 {
366  return (int) wxExecute( aCommandLine, aFlags, callback );
367 }

Referenced by doPrintFile(), ExecuteFile(), GetAssociatedDocument(), OpenFile(), OpenPDF(), and SCH_EDIT_FRAME::WriteNetListFile().

◆ ResolveUriByEnvVars()

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 at line 587 of file common.cpp.

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 }
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:574
wxString ExpandTextVars(const wxString &aSource, const std::function< bool(wxString *)> *aLocalResolver, const PROJECT *aProject)
Expand '${var-name}' templates in text.
Definition: common.cpp:382

References ExpandEnvVarSubstitutions(), and ExpandTextVars().

Referenced by GetAssociatedDocument().

◆ SelectReferenceNumber()

void SelectReferenceNumber ( wxTextEntry *  aTextEntry)

Select the number (or "?") in a reference for ease of editing.

Definition at line 312 of file common.cpp.

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 }

Referenced by DIALOG_EDIT_ONE_FIELD::OnSetFocusText(), DIALOG_FOOTPRINT_BOARD_EDITOR::OnUpdateUI(), and DIALOG_TEXT_PROPERTIES::TransferDataToWindow().

◆ TimestampDir()

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/filename.cpp.

TimestampDir

This routine offers SIGNIFICANT performance benefits over using wxWidgets to gather timestamps from matching files in a directory.

Parameters
aDirPaththe directory to search
aFilespeca (wildcarded) file spec to match against
Returns
a hash of the last-mod-dates of all matching files in the directory

Definition at line 966 of file common.cpp.

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 }
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

References matchWild().

Referenced by FP_CACHE::GetTimestamp(), and GPCB_FPL_CACHE::GetTimestamp().

◆ wxStringSplit()

void wxStringSplit ( const wxString &  aText,
wxArrayString &  aStrings,
wxChar  aSplitter 
)

Split aString to a string list separated at aSplitter.

Parameters
aTextis the text to split
aStringswill contain the splitted lines
aSplitteris the 'split' character

Definition at line 341 of file common.cpp.

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 }

Referenced by BOARD_ADAPTER::AddShapeWithClearanceToContainer(), export_vrml_pcbtext(), EDA_TEXT::GetTextBox(), HTML_MESSAGE_BOX::ListSet(), PANEL_SETUP_RULES::OnErrorLinkClicked(), DIALOG_FOOTPRINT_WIZARD_LIST::onShowTrace(), SCH_TEXT::Plot(), BRDITEMS_PLOTTER::PlotTextePcb(), EDA_TEXT::Print(), TEXTE_PCB::TransformShapeWithClearanceToPolygonSet(), and EDA_TEXT::TransformTextShapeToSegmentList().

Variable Documentation

◆ nilGenerator

boost::uuids::nil_generator nilGenerator
static

Definition at line 50 of file common.cpp.

◆ niluuid

◆ randomGenerator

boost::uuids::random_generator randomGenerator
static

Definition at line 46 of file common.cpp.

Referenced by KIID::ConvertTimestampToUuid(), and KIID::KIID().

◆ stringGenerator

boost::uuids::string_generator stringGenerator
static

Definition at line 49 of file common.cpp.

Referenced by KIID::KIID().