KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gestfich.h
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) 2009-2014 Jerry Jacobs
5 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <map>
24#include <filesystem>
25#include <functional>
26#include <memory>
27#include <string>
28#include <unordered_set>
29#include <kicommon.h>
30#include <wx/arrstr.h>
31#include <wx/dir.h>
32#include <wx/filename.h>
33#include <wx/process.h>
34#include <wx/zipstrm.h>
35
36
40#define UNIX_STRING_DIR_SEP wxT( "/" )
41#define WIN_STRING_DIR_SEP wxT( "\\" )
42
43/* Forward class declarations. */
44class EDA_LIST_DIALOG;
45class wxFFile;
46class wxOutputStream;
47
48
50{
53};
54
56KICOMMON_API std::filesystem::path ToFsPath( const wxString& aPath );
57
58KICOMMON_API wxString FromFsPath( const std::filesystem::path& aPath );
59
62{
63public:
64 SCOPED_TEMP_PATH() = default;
68
70 bool MakeFile( const wxString& aPrefix );
71
74 bool MakeDirectory( const wxString& aPrefix, int aPermissions = wxS_DIR_DEFAULT );
75
76 const wxString& Path() const { return m_path; }
77
78private:
79 wxString m_path;
80};
81
82using ARCHIVE_STREAM_FACTORY = std::function<std::unique_ptr<wxOutputStream>( wxFFile& )>;
83
85KICOMMON_API bool WriteDirectoryArchive( const wxString& aSourceDir, wxOutputStream& aOut,
86 ARCHIVE_FORMAT aFormat, const wxString& aTopDir );
87
89KICOMMON_API bool WriteDirectoryArchive( const wxString& aSourceDir, const wxString& aArchive,
90 ARCHIVE_FORMAT aFormat, const wxString& aTopDir,
91 wxString* aError = nullptr,
92 const ARCHIVE_STREAM_FACTORY& aOpen = {} );
93
94
102KICOMMON_API bool OpenPDF( const wxString& file );
103
109KICOMMON_API void KiCopyFile( const wxString& aSrcPath, const wxString& aDestPath,
110 wxString& aErrors );
111
119KICOMMON_API void CopySexprFile( const wxString& aSrcPath, const wxString& aDestPath,
120 std::function<bool( const std::string& token, wxString& value )> aCallback,
121 wxString& aErrors );
122
133KICOMMON_API int ExecuteFile( const wxString& aEditorName,
134 const wxString& aFileName = wxEmptyString,
135 wxProcess* aCallback = nullptr, bool aFileForKicad = true );
136
149KICOMMON_API int ExecuteCommandThroughShell( const wxString& aCommand, wxProcess* aProcess = nullptr );
150
156KICOMMON_API void QuoteString( wxString& string );
157
168KICOMMON_API wxString FindKicadFile( const wxString& shortname );
169
180KICOMMON_API extern wxString QuoteFullPath( wxFileName& fn, wxPathFormat format = wxPATH_NATIVE );
181
182
187KICOMMON_API bool RmDirRecursive( const wxString& aDirName, wxString* aErrors = nullptr );
188
197KICOMMON_API bool CopyDirectory( const wxString& aSourceDir, const wxString& aDestDir,
198 const std::vector<wxString>& aOverwriteExclusions, wxString& aErrors );
199
200KICOMMON_API bool CopyFilesOrDirectory( const wxString& aSourceDir, const wxString& aDestDir, bool aAllowOverwrite,
201 wxString& aErrors, std::vector<wxString>& aPathsWritten );
202
211KICOMMON_API bool AddDirectoryToZip( wxZipOutputStream& aZip, const wxString& aSourceDir, wxString& aErrors,
212 const wxString& aParentDir = "" );
213
230KICOMMON_API void CollectFilesLoopSafe( const wxString& aRoot, wxArrayString& aFiles,
231 const wxString& aFileSpec = wxEmptyString,
232 int aFlags = wxDIR_DEFAULT );
233
245KICOMMON_API void CollectSubdirsLoopSafe( const wxString& aRoot, wxArrayString& aDirs,
246 int aFlags = wxDIR_DIRS );
247
248
249// how a symlink that resolves outside the scan root is treated
251{
252 CONFINE_TO_ROOT, // descend only into targets that stay inside the root subtree
253 BLOCK_ROOT_ESCAPE // descend anywhere except a link resolving to an ancestor of the root
254};
255
256
257// bounds a hand-rolled recursive dir walk against symlink cycles and root escapes
258// query ShouldDescend before recursing real dirs keyed by name links canonicalized
260{
261public:
262 explicit DIR_LOOP_GUARD( const wxString& aRoot,
264
265 // false when the root itself would not resolve caller must not walk
266 bool IsRooted() const { return !m_root.empty(); }
267
268 bool ShouldDescend( const wxString& aDir );
269
270private:
271 std::filesystem::path m_root;
273 std::unordered_set<std::string> m_visited;
274};
DIR_LOOP_POLICY m_policy
Definition gestfich.h:272
std::unordered_set< std::string > m_visited
Definition gestfich.h:273
bool IsRooted() const
Definition gestfich.h:266
DIR_LOOP_GUARD(const wxString &aRoot, DIR_LOOP_POLICY aPolicy=DIR_LOOP_POLICY::CONFINE_TO_ROOT)
std::filesystem::path m_root
Definition gestfich.h:271
A dialog which shows:
SCOPED_TEMP_PATH(const SCOPED_TEMP_PATH &)=delete
bool MakeDirectory(const wxString &aPrefix, int aPermissions=wxS_DIR_DEFAULT)
Create an empty directory with a unique name after aPrefix.
Definition gestfich.cpp:959
bool MakeFile(const wxString &aPrefix)
Create an empty file with a unique name after aPrefix.
Definition gestfich.cpp:952
SCOPED_TEMP_PATH & operator=(const SCOPED_TEMP_PATH &)=delete
SCOPED_TEMP_PATH()=default
wxString m_path
Definition gestfich.h:79
const wxString & Path() const
Definition gestfich.h:76
KICOMMON_API bool CopyFilesOrDirectory(const wxString &aSourceDir, const wxString &aDestDir, bool aAllowOverwrite, wxString &aErrors, std::vector< wxString > &aPathsWritten)
Definition gestfich.cpp:559
KICOMMON_API wxString QuoteFullPath(wxFileName &fn, wxPathFormat format=wxPATH_NATIVE)
Quote return value of wxFileName::GetFullPath().
Definition gestfich.cpp:451
KICOMMON_API 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 gestfich.cpp:806
KICOMMON_API bool CopyDirectory(const wxString &aSourceDir, const wxString &aDestDir, const std::vector< wxString > &aOverwriteExclusions, wxString &aErrors)
Copy a directory and its contents to another directory.
Definition gestfich.cpp:505
KICOMMON_API void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition gestfich.cpp:361
KICOMMON_API int ExecuteCommandThroughShell(const wxString &aCommand, wxProcess *aProcess=nullptr)
Run a user-supplied command line through the platform shell so glob expansion, pipes,...
Definition gestfich.cpp:277
KICOMMON_API bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition gestfich.cpp:325
KICOMMON_API void QuoteString(wxString &string)
Add un " to the start and the end of string (if not already done).
Definition gestfich.cpp:58
KICOMMON_API wxString FindKicadFile(const wxString &shortname)
Search the executable file shortname in KiCad binary path and return full file name if found or short...
Definition gestfich.cpp:68
KICOMMON_API void CollectSubdirsLoopSafe(const wxString &aRoot, wxArrayString &aDirs, int aFlags=wxDIR_DIRS)
Recursively collect every subdirectory under aRoot using the same loop detection as CollectFilesLoopS...
KICOMMON_API 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.
ARCHIVE_FORMAT
Definition gestfich.h:50
std::function< std::unique_ptr< wxOutputStream >(wxFFile &)> ARCHIVE_STREAM_FACTORY
Definition gestfich.h:82
KICOMMON_API wxString FromFsPath(const std::filesystem::path &aPath)
Definition gestfich.cpp:932
KICOMMON_API bool RmDirRecursive(const wxString &aDirName, wxString *aErrors=nullptr)
Remove the directory aDirName and all its contents including subdirectories and their files.
Definition gestfich.cpp:457
KICOMMON_API void CopySexprFile(const wxString &aSrcPath, const wxString &aDestPath, std::function< bool(const std::string &token, wxString &value)> aCallback, wxString &aErrors)
Definition gestfich.cpp:388
KICOMMON_API int ExecuteFile(const wxString &aEditorName, const wxString &aFileName=wxEmptyString, wxProcess *aCallback=nullptr, bool aFileForKicad=true)
Call the executable file aEditorName with the parameter aFileName.
Definition gestfich.cpp:166
KICOMMON_API std::filesystem::path ToFsPath(const wxString &aPath)
Give aPath to std::filesystem without a detour through the C locale.
Definition gestfich.cpp:922
DIR_LOOP_POLICY
Definition gestfich.h:251
KICOMMON_API bool AddDirectoryToZip(wxZipOutputStream &aZip, const wxString &aSourceDir, wxString &aErrors, const wxString &aParentDir="")
Add a directory and its contents to a zip file.
Definition gestfich.cpp:682
#define KICOMMON_API
Definition kicommon.h:27