27#include <wx/mimetype.h>
36#include "wx/tokenzr.h"
41#include <wx/wfstream.h>
43#include <wx/zipstrm.h>
47#include <system_error>
48#include <unordered_set>
53 if( !
string.StartsWith( wxT(
"\"" ) ) )
55 string.Prepend ( wxT(
"\"" ) );
56 string.Append ( wxT(
"\"" ) );
70 if( wxFileExists( fullFileName ) )
73 if( wxGetEnv( wxT(
"KICAD_RUN_FROM_BUILD_DIR" ),
nullptr ) )
75 wxFileName buildDir(
Pgm().GetExecutablePath(), shortname );
78 if( !buildDir.GetDirs().IsEmpty()
79 && buildDir.GetDirs().Last().Lower().EndsWith( wxT(
".app" ) ) )
81 buildDir.RemoveLastDir();
84 buildDir.RemoveLastDir();
86 buildDir.AppendDir( shortname );
88 buildDir.AppendDir( shortname.BeforeLast(
'.' ) );
91 if( buildDir.GetDirs().Last() ==
"pl_editor" )
93 buildDir.RemoveLastDir();
94 buildDir.AppendDir(
"pagelayout_editor" );
98 buildDir.AppendDir( shortname + wxT(
".app" ) );
99 buildDir.AppendDir( wxT(
"Contents" ) );
100 buildDir.AppendDir( wxT(
"MacOS" ) );
103 if( wxFileExists( buildDir.GetFullPath() ) )
104 return buildDir.GetFullPath();
109 if(
Pgm().IsKicadEnvVariableDefined() )
113 if( wxFileExists( fullFileName ) )
117#if defined( __WINDOWS__ )
124 const static wxChar* possibilities[] = {
125#if defined( __WXMAC__ )
127 wxT(
"Contents/Applications/pcbnew.app/Contents/MacOS/" ),
128 wxT(
"Contents/Applications/eeschema.app/Contents/MacOS/" ),
129 wxT(
"Contents/Applications/gerbview.app/Contents/MacOS/" ),
130 wxT(
"Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
131 wxT(
"Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
132 wxT(
"Contents/Applications/pl_editor.app/Contents/MacOS/" ),
135 wxT(
"/usr/local/bin/" ),
136 wxT(
"/usr/local/kicad/bin/" ),
141 for(
unsigned i=0; i<
arrayDim(possibilities); ++i )
144 fullFileName = possibilities[i] + shortname;
150 if( wxFileExists( fullFileName ) )
160int ExecuteFile(
const wxString& aEditorName,
const wxString& aFileName, wxProcess* aCallback,
163 wxString fullEditorName;
164 std::vector<wxString> params;
168 bool inSingleQuotes =
false;
169 bool inDoubleQuotes =
false;
174 if( !param.IsEmpty() )
176 params.push_back( param );
181 for( wxUniChar ch : aEditorName )
188 inSingleQuotes =
false;
196 else if( inDoubleQuotes )
201 inDoubleQuotes =
false;
208 else if( ch ==
'\'' )
211 inSingleQuotes =
true;
216 inDoubleQuotes =
true;
233 fullEditorName = params[0];
235 params.erase( params.begin() );
241 fullEditorName = aEditorName;
244 if( wxFileExists( fullEditorName ) )
246 std::vector<const wchar_t*> args;
248 args.emplace_back( fullEditorName.wc_str() );
250 if( !params.empty() )
252 for(
const wxString& p : params )
253 args.emplace_back( p.wc_str() );
256 if( !aFileName.IsEmpty() )
257 args.emplace_back( aFileName.wc_str() );
259 args.emplace_back(
nullptr );
261 return wxExecute(
const_cast<wchar_t**
>( args.data() ), wxEXEC_ASYNC, aCallback );
265 msg.Printf(
_(
"Command '%s' could not be found." ), fullEditorName );
274 wxString filename = file;
278 if(
Pgm().UseSystemPdfBrowser() )
282 msg.Printf(
_(
"Unable to find a PDF viewer for '%s'." ), filename );
289 const wchar_t* args[3];
292 args[1] = filename.wc_str();
295 if( wxExecute(
const_cast<wchar_t**
>( args ) ) == -1 )
297 msg.Printf(
_(
"Problem while running the PDF viewer '%s'." ), args[0] );
307void KiCopyFile(
const wxString& aSrcPath,
const wxString& aDestPath, wxString& aErrors )
309 if( !wxCopyFile( aSrcPath, aDestPath ) )
313 if( !aErrors.IsEmpty() )
316 msg.Printf(
_(
"Cannot copy file '%s'." ), aDestPath );
335 std::function<
bool(
const std::string& token, wxString& value )> aCallback,
338 bool success =
false;
348 if( node->IsList() && node->GetNumberOfChildren() > 1 && node->GetChild( 0 )->IsSymbol() )
350 std::string token = node->GetChild( 0 )->GetSymbol();
351 SEXPR::SEXPR_STRING* pathNode = dynamic_cast<SEXPR::SEXPR_STRING*>( node->GetChild( 1 ) );
352 SEXPR::SEXPR_SYMBOL* symNode = dynamic_cast<SEXPR::SEXPR_SYMBOL*>( node->GetChild( 1 ) );
356 path = pathNode->m_value;
358 path = symNode->m_value;
360 if( aCallback( token, path ) )
363 pathNode->m_value = path;
365 symNode->m_value = path;
375 formatter.
Print(
"%s", sexpr->AsString( 0 ).c_str() );
377 success = formatter.
Finish();
388 if( !aErrors.empty() )
389 aErrors += wxS(
"\n" );
391 msg.Printf(
_(
"Cannot copy file '%s'." ), aDestPath );
399 return wxT(
"\"" ) + fn.GetFullPath( format ) + wxT(
"\"" );
405 namespace fs = std::filesystem;
407 std::string rmDir = aFileName.ToStdString();
409 if( rmDir.length() < 3 )
412 *aErrors =
_(
"Invalid directory name, cannot remove root" );
417 if( !fs::exists( rmDir ) )
420 *aErrors = wxString::Format(
_(
"Directory '%s' does not exist" ), aFileName );
425 fs::path
path( rmDir );
427 if( !fs::is_directory(
path ) )
430 *aErrors = wxString::Format(
_(
"'%s' is not a directory" ), aFileName );
437 fs::remove_all(
path );
439 catch(
const fs::filesystem_error& e )
442 *aErrors = wxString::Format(
_(
"Error removing directory '%s': %s" ), aFileName, e.what() );
452 const std::vector<wxString>& aPathsWithOverwriteDisallowed, wxString& aErrors )
454 wxDir dir( aSourceDir );
456 if( !dir.IsOpened() )
458 aErrors += wxString::Format(
_(
"Could not open source directory: %s" ), aSourceDir );
459 aErrors += wxT(
"\n" );
463 if( !wxFileName::Mkdir( aDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
465 aErrors += wxString::Format(
_(
"Could not create destination directory: %s" ), aDestDir );
466 aErrors += wxT(
"\n" );
471 bool cont = dir.GetFirst( &filename );
475 wxString sourcePath = dir.GetNameWithSep() + filename;
476 wxString destPath = aDestDir + wxFileName::GetPathSeparator() + filename;
478 if( wxFileName::DirExists( sourcePath ) )
481 if( !
CopyDirectory( sourcePath, destPath, aPathsWithOverwriteDisallowed, aErrors ) )
487 if(
alg::contains( aPathsWithOverwriteDisallowed, sourcePath ) && wxFileExists( destPath ) )
491 else if( !wxCopyFile( sourcePath, destPath ) )
493 aErrors += wxString::Format(
_(
"Could not copy file: %s to %s" ), sourcePath, destPath );
498 cont = dir.GetNext( &filename );
506 wxString& aErrors, std::vector<wxString>& aPathsWritten )
509 wxFileName sourceFn( aSourcePath );
510 wxString sourcePath = sourceFn.GetFullPath();
511 bool isSourceDirectory = wxFileName::DirExists( sourcePath );
512 wxString baseDestDir = aDestDir;
515 [&](
const wxString& src,
const wxString& dest ) ->
bool
517 if( wxCopyFile( src, dest, aAllowOverwrites ) )
519 aPathsWritten.push_back( dest );
523 aErrors += wxString::Format(
_(
"Could not copy file: %s to %s" ), src, dest );
524 aErrors += wxT(
"\n" );
528 auto processEntries =
529 [&](
const wxString& srcDir,
const wxString& pattern,
const wxString& destDir ) ->
bool
533 if( !dir.IsOpened() )
535 aErrors += wxString::Format(
_(
"Could not open source directory: %s" ), srcDir );
536 aErrors += wxT(
"\n" );
544 bool cont = dir.GetFirst( &filename, pattern, wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN );
548 const wxString entrySrc = srcDir + wxFileName::GetPathSeparator() + filename;
549 const wxString entryDest = destDir + wxFileName::GetPathSeparator() + filename;
551 if( !filename.Matches( wxT(
"~*.lck" ) ) && !filename.Matches( wxT(
"*.lck" ) ) )
553 if( wxFileName::DirExists( entrySrc ) )
558 aErrors += wxString::Format(
_(
"Could not copy directory: %s to %s" ),
559 entrySrc, entryDest );
560 aErrors += wxT(
"\n" );
568 if( !performCopy( entrySrc, entryDest ) )
575 cont = dir.GetNext( &filename );
582 if( isSourceDirectory )
584 wxString sourceDirName = sourceFn.GetFullName();
585 baseDestDir = wxFileName( aDestDir, sourceDirName ).GetFullPath();
589 if( !wxFileName::Mkdir( baseDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
591 aErrors += wxString::Format(
_(
"Could not create destination directory: %s" ), baseDestDir );
592 aErrors += wxT(
"\n" );
598 if( !isSourceDirectory )
600 const wxString fileName = sourceFn.GetFullName();
603 if( fileName.Contains(
'*' ) || fileName.Contains(
'?' ) )
605 const wxString dirPath = sourceFn.GetPath();
607 if( !wxFileName::DirExists( dirPath ) )
609 aErrors += wxString::Format(
_(
"Source directory does not exist: %s" ), dirPath );
610 aErrors += wxT(
"\n" );
616 return processEntries( dirPath, fileName, baseDestDir );
620 return performCopy( sourcePath, wxFileName( baseDestDir, fileName ).GetFullPath() );
624 return processEntries( sourcePath, wxEmptyString, baseDestDir );
629 const wxString& aParentDir )
631 wxDir dir( aSourceDir );
633 if( !dir.IsOpened() )
635 aErrors += wxString::Format(
_(
"Could not open source directory: %s" ), aSourceDir );
641 bool cont = dir.GetFirst( &filename );
645 wxString sourcePath = aSourceDir + wxFileName::GetPathSeparator() + filename;
646 wxString zipPath = aParentDir + filename;
648 if( wxFileName::DirExists( sourcePath ) )
651 aZip.PutNextDirEntry( zipPath +
"/" );
660 aZip.PutNextEntry( zipPath );
661 wxFFileInputStream fileStream( sourcePath );
663 if( !fileStream.IsOk() )
665 aErrors += wxString::Format(
_(
"Could not read file: %s" ), sourcePath );
669 aZip.Write( fileStream );
672 cont = dir.GetNext( &filename );
682std::filesystem::path toFsPath(
const wxString& aPath )
685 return std::filesystem::path( std::wstring( aPath.wc_str() ) );
687 return std::filesystem::path( aPath.utf8_string() );
695std::filesystem::path canonicalPath(
const std::filesystem::path& aPath )
698 std::filesystem::path canon = std::filesystem::weakly_canonical( aPath, ec );
700 return ec ? std::filesystem::path() : canon;
707bool isAncestorOrSame(
const std::filesystem::path& aAncestor,
708 const std::filesystem::path& aDescendant )
710 auto a = aAncestor.begin();
711 auto d = aDescendant.begin();
713 for( ; a != aAncestor.end() && d != aDescendant.end(); ++a, ++d )
719 return a == aAncestor.end();
726class LOOP_SAFE_COLLECTOR :
public wxDirTraverser
729 LOOP_SAFE_COLLECTOR( wxArrayString& aOutput,
const wxString& aRoot,
bool aCollectFiles,
730 bool aCollectDirs ) :
732 m_root( canonicalPath( toFsPath( aRoot ) ) ),
733 m_collectFiles( aCollectFiles ),
734 m_collectDirs( aCollectDirs )
736 m_visited.reserve( 256 );
738 if( !m_root.empty() )
739 m_visited.insert( m_root.generic_string() );
742 wxDirTraverseResult OnFile(
const wxString& aFilename )
override
745 m_output.Add( aFilename );
747 return wxDIR_CONTINUE;
750 wxDirTraverseResult OnDir(
const wxString& aDirname )
override
752 const std::filesystem::path raw = toFsPath( aDirname );
760 const bool isLink = std::filesystem::is_symlink( raw, ec );
762 std::filesystem::path key;
766 const std::filesystem::path canon = canonicalPath( raw );
774 if( !m_root.empty() && isAncestorOrSame( canon, m_root ) )
781 key = raw.lexically_normal();
784 if( !m_visited.insert( key.generic_string() ).second )
788 m_output.Add( aDirname );
790 return wxDIR_CONTINUE;
794 wxArrayString& m_output;
795 std::filesystem::path m_root;
798 std::unordered_set<std::string> m_visited;
802void traverseLoopSafe(
const wxString& aRoot, wxArrayString& aOutput,
bool aCollectFiles,
803 bool aCollectDirs,
const wxString& aFileSpec,
int aFlags )
807 if( !dir.IsOpened() )
810 LOOP_SAFE_COLLECTOR collector( aOutput, aRoot, aCollectFiles, aCollectDirs );
811 dir.Traverse( collector, aFileSpec, aFlags );
823 traverseLoopSafe( aRoot, aFiles,
true,
false, aFileSpec, aFlags | wxDIR_FILES | wxDIR_DIRS );
829 traverseLoopSafe( aRoot, aDirs,
false,
true, wxEmptyString, aFlags | wxDIR_DIRS );
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
virtual const wxString & GetKicadEnvVariable() const
virtual void ReadPdfBrowserInfos()
Read the PDF browser choice from the common configuration.
virtual const wxString & GetPdfBrowserName() const
virtual const wxString & GetExecutablePath() const
std::unique_ptr< SEXPR > ParseFromFile(const std::string &aFilename)
size_t GetNumberOfChildren() const
SEXPR * GetChild(size_t aIndex) const
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
This file is part of the common library.
wxString FindKicadFile(const wxString &shortname)
Search the executable file shortname in KiCad binary path and return full file name if found or short...
wxString QuoteFullPath(wxFileName &fn, wxPathFormat format)
Quote return value of wxFileName::GetFullPath().
void CopySexprFile(const wxString &aSrcPath, const wxString &aDestPath, std::function< bool(const std::string &token, wxString &value)> aCallback, wxString &aErrors)
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
int ExecuteFile(const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback, bool aFileForKicad)
Call the executable file aEditorName with the parameter aFileName.
bool RmDirRecursive(const wxString &aFileName, wxString *aErrors)
Remove the directory aDirName and all its contents including subdirectories and their files.
void QuoteString(wxString &string)
Add un " to the start and the end of string (if not already done).
bool CopyFilesOrDirectory(const wxString &aSourcePath, const wxString &aDestDir, bool aAllowOverwrites, wxString &aErrors, std::vector< wxString > &aPathsWritten)
void CollectSubdirsLoopSafe(const wxString &aRoot, wxArrayString &aDirs, int aFlags)
Recursively collect every subdirectory under aRoot using the same loop detection as CollectFilesLoopS...
void CollectFilesLoopSafe(const wxString &aRoot, wxArrayString &aFiles, const wxString &aFileSpec, int aFlags)
Recursively collect every file under aRoot, deduplicating subdirectories by their resolved path.
static void traverseSEXPR(SEXPR::SEXPR *aNode, const std::function< void(SEXPR::SEXPR *)> &aVisitor)
bool CopyDirectory(const wxString &aSourceDir, const wxString &aDestDir, const std::vector< wxString > &aPathsWithOverwriteDisallowed, wxString &aErrors)
Copy a directory and its contents to another directory.
bool AddDirectoryToZip(wxZipOutputStream &aZip, const wxString &aSourceDir, wxString &aErrors, const wxString &aParentDir)
Add a directory and its contents to a zip file.
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
PGM_BASE & Pgm()
The global program "get" accessor.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.