|
KiCad PCB EDA Suite
|
Functions for file management. More...
#include <wx/mimetype.h>#include <wx/dir.h>#include <wx/stdpaths.h>#include <pgm_base.h>#include <kiplatform/io.h>#include <confirm.h>#include <core/arraydim.h>#include <gestfich.h>#include <string_utils.h>#include <launch_ext.h>#include "wx/tokenzr.h"#include <richio.h>#include <sexpr/sexpr.h>#include <sexpr/sexpr_parser.h>#include <wx/wfstream.h>#include <wx/fs_zip.h>#include <wx/zipstrm.h>#include <wx/tarstrm.h>#include <wx/zstream.h>#include <algorithm>#include <filesystem>#include <string>#include <system_error>#include <unordered_set>#include <vector>#include <core/kicad_algo.h>Go to the source code of this file.
Functions | |
| void | QuoteString (wxString &string) |
| Add un " to the start and the end of string (if not already done). | |
| wxString | FindKicadFile (const wxString &shortname) |
| Search the executable file shortname in KiCad binary path and return full file name if found or shortname if the kicad binary path is kicad/bin. | |
| int | ExecuteFile (const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback, bool aFileForKicad) |
| Call the executable file aEditorName with the parameter aFileName. | |
| int | ExecuteCommandThroughShell (const wxString &aCommand, wxProcess *aProcess) |
| Run a user-supplied command line through the platform shell so glob expansion, pipes, and other shell features work consistently. | |
| bool | OpenPDF (const wxString &file) |
| Run the PDF viewer and display a PDF file. | |
| void | KiCopyFile (const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors) |
| static void | traverseSEXPR (SEXPR::SEXPR *aNode, const std::function< void(SEXPR::SEXPR *)> &aVisitor) |
| void | CopySexprFile (const wxString &aSrcPath, const wxString &aDestPath, std::function< bool(const std::string &token, wxString &value)> aCallback, wxString &aErrors) |
| wxString | QuoteFullPath (wxFileName &fn, wxPathFormat format) |
| Quote return value of wxFileName::GetFullPath(). | |
| bool | RmDirRecursive (const wxString &aFileName, wxString *aErrors) |
| Remove the directory aDirName and all its contents including subdirectories and their files. | |
| 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 | CopyFilesOrDirectory (const wxString &aSourcePath, const wxString &aDestDir, bool aAllowOverwrites, wxString &aErrors, std::vector< wxString > &aPathsWritten) |
| bool | AddDirectoryToZip (wxZipOutputStream &aZip, const wxString &aSourceDir, wxString &aErrors, const wxString &aParentDir) |
| Add a directory and its contents to a zip file. | |
| bool | WriteDirectoryArchive (const wxString &aSourceDir, wxOutputStream &aOut, ARCHIVE_FORMAT aFormat, const wxString &aTopDir) |
| Write directory entries in sorted archive path order, using the archive root when aTopDir is empty. | |
| bool | WriteDirectoryArchive (const wxString &aSourceDir, const wxString &aArchive, ARCHIVE_FORMAT aFormat, const wxString &aTopDir, wxString *aError, const ARCHIVE_STREAM_FACTORY &aOpen) |
| Replace the archive atomically after writing and closing a sibling temporary file. | |
| std::filesystem::path | ToFsPath (const wxString &aPath) |
| Give aPath to std::filesystem without a detour through the C locale. | |
| wxString | FromFsPath (const std::filesystem::path &aPath) |
| void | CollectFilesLoopSafe (const wxString &aRoot, wxArrayString &aFiles, const wxString &aFileSpec, int aFlags) |
| Recursively collect every file under aRoot, deduplicating subdirectories by their resolved path. | |
| void | CollectSubdirsLoopSafe (const wxString &aRoot, wxArrayString &aDirs, int aFlags) |
| Recursively collect every subdirectory under aRoot using the same loop detection as CollectFilesLoopSafe(). | |
Functions for file management.
Definition in file gestfich.cpp.
| bool AddDirectoryToZip | ( | wxZipOutputStream & | aZip, |
| const wxString & | aSourceDir, | ||
| wxString & | aErrors, | ||
| const wxString & | aParentDir = "" ) |
Add a directory and its contents to a zip file.
| aZip | is the zip file to add to. |
| aSourceDir | is the directory to add. |
| aErrors | is a string to append any errors to. |
| aParentDir | is the parent directory to add to the zip file. |
Definition at line 669 of file gestfich.cpp.
References _, and AddDirectoryToZip().
Referenced by AddDirectoryToZip(), and JOBS_OUTPUT_ARCHIVE::HandleOutputs().
| void CollectFilesLoopSafe | ( | const wxString & | aRoot, |
| wxArrayString & | aFiles, | ||
| const wxString & | aFileSpec = wxEmptyString, | ||
| int | aFlags = wxDIR_DEFAULT ) |
Recursively collect every file under aRoot, deduplicating subdirectories by their resolved path.
Legitimate one-level symlinks resolve normally, but recursive symlinks (a Wine dosdevices/z: -> / loop, a self-referencing . link, etc.) are visited at most once instead of recursing until OOM. This is a loop-safe replacement for wxDir::GetAllFiles() when scanning user-controlled directory trees.
| aRoot | is the directory to walk. |
| aFiles | receives the full path of every matching file found. |
| aFileSpec | is an optional wildcard filter (e.g. *.step); empty matches all. |
| aFlags | is the wxDir traversal flag set; defaults to wxDIR_DEFAULT to match wxDir::GetAllFiles(). Pass wxDIR_FILES | wxDIR_DIRS to exclude hidden entries. wxDIR_FILES and wxDIR_DIRS are always implied so recursion and file collection happen regardless. |
Definition at line 1110 of file gestfich.cpp.
Referenced by BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), MODEL_SUBSTITUTION::STEP_CATALOG::Build(), PCB_IO_GEDA::DeleteLibrary(), PCB_IO_KICAD_SEXPR::DeleteLibrary(), PCB_IO_SPRINT_LAYOUT::FootprintEnumerate(), and DIALOG_MIGRATE_3D_MODELS::scanDirectory().
| void CollectSubdirsLoopSafe | ( | const wxString & | aRoot, |
| wxArrayString & | aDirs, | ||
| int | aFlags = wxDIR_DIRS ) |
Recursively collect every subdirectory under aRoot using the same loop detection as CollectFilesLoopSafe().
Loop-safe replacement for wxDir::GetAllFiles() called with the wxDIR_DIRS flag.
| aRoot | is the directory to walk. |
| aDirs | receives the full path of every subdirectory found. |
| aFlags | is the wxDir traversal flag set; defaults to wxDIR_DIRS (hidden directories excluded). Pass wxDIR_DIRS | wxDIR_HIDDEN to include them. wxDIR_DIRS is always implied. |
Definition at line 1120 of file gestfich.cpp.
Referenced by BOOST_AUTO_TEST_CASE(), and DESIGN_BLOCK_IO::DeleteLibrary().
| bool CopyDirectory | ( | const wxString & | aSourceDir, |
| const wxString & | aDestDir, | ||
| const std::vector< wxString > & | aOverwriteExclusions, | ||
| wxString & | aErrors ) |
Copy a directory and its contents to another directory.
| aSourceDir | is the directory to copy. |
| aDestDir | is the directory to copy to. |
| aOverwriteExclusions | |
| aErrors | is a string to append any errors to. |
Definition at line 492 of file gestfich.cpp.
References _, alg::contains(), and CopyDirectory().
Referenced by CopyDirectory(), and JOBS_OUTPUT_FOLDER::HandleOutputs().
| bool CopyFilesOrDirectory | ( | const wxString & | aSourcePath, |
| const wxString & | aDestDir, | ||
| bool | aAllowOverwrites, | ||
| wxString & | aErrors, | ||
| std::vector< wxString > & | aPathsWritten ) |
Definition at line 546 of file gestfich.cpp.
References _, and CopyFilesOrDirectory().
Referenced by CopyFilesOrDirectory(), and JOBS_RUNNER::runSpecialCopyFiles().
| void CopySexprFile | ( | const wxString & | aSrcPath, |
| const wxString & | aDestPath, | ||
| std::function< bool(const std::string &token, wxString &value)> | aCallback, | ||
| wxString & | aErrors ) |
| aSrcPath | is the full filename of the source. | |
| [in] | aDestPath | is the full filename of the target. |
| [in] | aCallback | a facility to allow modification of the values of particular tokens. Normally used to update paths in the copied document. |
| [out] | aErrors | a wxString to append any errors to. |
Definition at line 375 of file gestfich.cpp.
References PRETTIFIED_FILE_OUTPUTFORMATTER::Finish(), SEXPR::PARSER::ParseFromFile(), OUTPUTFORMATTER::Print(), TO_UTF8, and traverseSEXPR().
Referenced by PCB::IFACE::SaveFileAs(), and SCH::IFACE::SaveFileAs().
| int ExecuteCommandThroughShell | ( | const wxString & | aCommand, |
| wxProcess * | aProcess = nullptr ) |
Run a user-supplied command line through the platform shell so glob expansion, pipes, and other shell features work consistently.
On Windows the command is wrapped as cmd.exe /d /s /c "<cmd>" so that absolute paths containing spaces or quotes survive cmd.exe's quote handling intact. On POSIX the command is handed to /bin/sh -c as a single argument.
| aCommand | the command line exactly as entered by the user. |
| aProcess | an optional wxProcess for the call; pass a Redirect()ed process to capture output. |
Definition at line 277 of file gestfich.cpp.
References flag.
Referenced by executeViaShell(), and JOBS_RUNNER::runSpecialExecute().
| int ExecuteFile | ( | const wxString & | aEditorName, |
| const wxString & | aFileName = wxEmptyString, | ||
| wxProcess * | aCallback = nullptr, | ||
| bool | aFileForKicad = true ) |
Call the executable file aEditorName with the parameter aFileName.
| [in] | aEditorName | is the full filename for the binary. |
| [in] | aFileName | is the full filename of the file to open. |
| [in] | aCallback | a wxProcess* for the call. |
| aFileForKicad | a boolean to flag if aFileName runs with a KiCad binary. In this case aFileName is a shortname and FindKicadFile() is called to return the path. In the other case, aFileName is a full file name (passed prefixed with the path). |
Definition at line 166 of file gestfich.cpp.
References _, DisplayErrorMessage(), and FindKicadFile().
Referenced by COMMON_CONTROL::Execute(), DIALOG_CONFIG_EQUFILES::OnEditEquFile(), DIALOG_BOM::OnEditGenerator(), SCH_EDIT_FRAME::OnOpenPcbnew(), PROJECT_TREE_PANE::onOpenSelectedFileWithTextEditor(), FOOTPRINT_EDITOR_CONTROL::OpenWithTextEditor(), SYMBOL_EDITOR_CONTROL::OpenWithTextEditor(), BOARD_EDITOR_CONTROL::ShowEeschema(), and GERBVIEW_INSPECTION_TOOL::ShowSource().
| wxString FindKicadFile | ( | const wxString & | shortname | ) |
Search the executable file shortname in KiCad binary path and return full file name if found or shortname if the kicad binary path is kicad/bin.
The binary path is found from:
Definition at line 67 of file gestfich.cpp.
References arrayDim(), PGM_BASE::GetExecutablePath(), PGM_BASE::GetKicadEnvVariable(), and Pgm().
Referenced by API_PLUGIN_MANAGER::doInvokeAction(), KICAD_MANAGER_FRAME::DoWithAcceptedFiles(), ExecuteFile(), PYTHON_MANAGER::FindPythonInterpreter(), API_HANDLER_COMMON::handleGetKiCadBinaryPath(), and API_PLUGIN_MANAGER::processNextJob().
| wxString FromFsPath | ( | const std::filesystem::path & | aPath | ) |
Definition at line 919 of file gestfich.cpp.
Referenced by generateOneODBPackage().
| void KiCopyFile | ( | const wxString & | aSrcPath, |
| const wxString & | aDestPath, | ||
| wxString & | aErrors ) |
| aSrcPath | is the full filename of the source. | |
| [in] | aDestPath | is the full filename of the target. |
| [out] | aErrors | a wxString to append any errors to. |
Definition at line 348 of file gestfich.cpp.
References _.
Referenced by MIGRATION_TRAVERSER::OnFile(), PROJECT_TREE_TRAVERSER::OnFile(), GERBV::IFACE::SaveFileAs(), PGE::IFACE::SaveFileAs(), HEADLESS_PCB_CONTEXT::SavePcbCopy(), PCB_EDIT_FRAME::SavePcbCopy(), and PCB_EDIT_FRAME::SavePcbFile().
| bool OpenPDF | ( | const wxString & | file | ) |
Run the PDF viewer and display a PDF file.
| file | the PDF file to open. |
| true | if PDF viewer found. |
| false | if no PDF viewer found. |
Definition at line 312 of file gestfich.cpp.
References _, DisplayErrorMessage(), PGM_BASE::GetPdfBrowserName(), LaunchExternal(), Pgm(), and PGM_BASE::ReadPdfBrowserInfos().
Referenced by PROJECT_TREE_ITEM::Activate(), GetAssociatedDocument(), and DIALOG_PLOT_SCHEMATIC::plotSchematic().
| wxString QuoteFullPath | ( | wxFileName & | fn, |
| wxPathFormat | format = wxPATH_NATIVE ) |
Quote return value of wxFileName::GetFullPath().
This allows file name paths with spaces to be used as parameters to ProcessExecute function calls.
| fn | is the filename to wrap. |
| format | if provided, can be used to transform the nature of the wrapped filename to another platform. |
Definition at line 438 of file gestfich.cpp.
| void QuoteString | ( | wxString & | string | ) |
Add un " to the start and the end of string (if not already done).
| string | string to modify. |
Definition at line 57 of file gestfich.cpp.
| bool RmDirRecursive | ( | const wxString & | aFileName, |
| wxString * | aErrors ) |
Remove the directory aDirName and all its contents including subdirectories and their files.
Definition at line 444 of file gestfich.cpp.
Referenced by LIBGIT_BACKEND::RemoveVCS(), and DIALOG_GIT_REPOSITORY::~DIALOG_GIT_REPOSITORY().
| std::filesystem::path ToFsPath | ( | const wxString & | aPath | ) |
Give aPath to std::filesystem without a detour through the C locale.
Definition at line 909 of file gestfich.cpp.
Referenced by BOOST_FIXTURE_TEST_CASE(), DIR_LOOP_GUARD::DIR_LOOP_GUARD(), generateOneODBPackage(), and DIR_LOOP_GUARD::ShouldDescend().
|
static |
Definition at line 363 of file gestfich.cpp.
References SEXPR::SEXPR::GetChild(), SEXPR::SEXPR::GetNumberOfChildren(), SEXPR::SEXPR::IsList(), and traverseSEXPR().
Referenced by CopySexprFile(), and traverseSEXPR().
| bool WriteDirectoryArchive | ( | const wxString & | aSourceDir, |
| const wxString & | aArchive, | ||
| ARCHIVE_FORMAT | aFormat, | ||
| const wxString & | aTopDir, | ||
| wxString * | aError, | ||
| const ARCHIVE_STREAM_FACTORY & | aOpen ) |
Replace the archive atomically after writing and closing a sibling temporary file.
Definition at line 831 of file gestfich.cpp.
References _, KIPLATFORM::IO::CLOUD_SYNC_BUFFER_SIZE, KIPLATFORM::IO::CommitTempFile(), KIPLATFORM::IO::FlushToDisk(), KIPLATFORM::IO::OpenUniqueSiblingTempFile(), and WriteDirectoryArchive().
| bool WriteDirectoryArchive | ( | const wxString & | aSourceDir, |
| wxOutputStream & | aOut, | ||
| ARCHIVE_FORMAT | aFormat, | ||
| const wxString & | aTopDir ) |
Write directory entries in sorted archive path order, using the archive root when aTopDir is empty.
Definition at line 793 of file gestfich.cpp.
References DIR_LOOP_GUARD::IsRooted(), and TGZ.
Referenced by GenerateIpc2581File(), generateOneODBPackage(), and WriteDirectoryArchive().